miner mines continuously,
moved to an account model, transactions working, querying working, more checking of valid data
This commit is contained in:
parent
59bb42be11
commit
9847b2056b
12 changed files with 663 additions and 365 deletions
|
@ -1,5 +1,6 @@
|
|||
const Transaction = require('../wallet/transaction');
|
||||
const Metadata = require('../wallet/metadata')
|
||||
const Metadata = require('../wallet/metadata');
|
||||
const Block = require('../blockchain/block');
|
||||
|
||||
const Return = {
|
||||
add: 1,
|
||||
|
@ -10,16 +11,16 @@ const Return = {
|
|||
class TransactionPool {
|
||||
constructor() {
|
||||
this.transactions = [];
|
||||
this.metadataS =[];
|
||||
this.metadatas = [];
|
||||
}
|
||||
|
||||
//returns true on update, false on add
|
||||
updateOrAddTransaction(transaction) {
|
||||
if (!Transaction.verifyTransaction(transaction)) {
|
||||
if (!Transaction.verify(transaction)) {
|
||||
console.log("Couldn't update or add transaction, transaction couldn't be verified");
|
||||
return Return.error;
|
||||
}
|
||||
const foundIndex = this.transactions.findIndex(t => t.id === transaction.id);
|
||||
const foundIndex = this.transactions.findIndex(t => t.input === transaction.input && t.counter === transaction.counter);
|
||||
|
||||
if (foundIndex !== -1) {
|
||||
this.transactions[foundIndex] = transaction;
|
||||
|
@ -36,13 +37,13 @@ class TransactionPool {
|
|||
return Return.error;
|
||||
}
|
||||
|
||||
const foundIndex = this.metadataS.findIndex(t => t.id === metadata.id);
|
||||
const foundIndex = this.metadatas.findIndex(t => t.id === metadata.id);
|
||||
|
||||
if (foundIndex !== -1) {
|
||||
this.metadataS[foundIndex] = metadata;
|
||||
this.metadatas[foundIndex] = metadata;
|
||||
return Return.update;
|
||||
} else {
|
||||
this.metadataS.push(metadata);
|
||||
this.metadatas.push(metadata);
|
||||
return Return.add;
|
||||
}
|
||||
}
|
||||
|
@ -52,62 +53,41 @@ class TransactionPool {
|
|||
}
|
||||
|
||||
existingMetadata(address) {
|
||||
return this.metadataS.find(t => t.Signiture.address === address);
|
||||
return this.metadatas.find(t => t.Signiture.address === address);
|
||||
}
|
||||
|
||||
validTransactions() {
|
||||
return this.transactions.filter(transaction => {
|
||||
const outputTotal = transaction.outputs.reduce((total, output) => {
|
||||
return total + output.amount;
|
||||
}, 0);
|
||||
|
||||
if (transaction.input.amount !== outputTotal) {
|
||||
console.log(`Invalid transaction from ${transaction.input.address}.`);
|
||||
return;
|
||||
}
|
||||
|
||||
if (!Transaction.verifyTransaction(transaction)) {
|
||||
console.log(`Invalid signature from ${transaction.input.address}.`);
|
||||
return;
|
||||
}
|
||||
|
||||
return transaction;
|
||||
});
|
||||
//we could check for possible double spends here
|
||||
validTransactionsCopy() {
|
||||
return [...this.transactions];
|
||||
}
|
||||
|
||||
validMetadataS(){
|
||||
return this.metadataS.filter(metadata => {
|
||||
if (!Metadata.verifyMetadata(metadata)) {
|
||||
console.log(`Invalid signature from ${metadata.Signiture.address}.`);
|
||||
return;
|
||||
}
|
||||
return metadata;
|
||||
});
|
||||
validMetadatasCopy(){
|
||||
return [...this.metadatas];
|
||||
}
|
||||
|
||||
clearFromBlock(block) {
|
||||
const transactions = block.data[0];
|
||||
const metadatas = block.data[1];
|
||||
for (const transaction of transactions) {
|
||||
const blockTransactions = Block.getTransactions(block);
|
||||
const blockMetadatas = Block.getMetadatas(block);
|
||||
|
||||
for (const transaction of blockTransactions) {
|
||||
const foundTransaction = this.transactions.findIndex(t => t.id === transaction.id);
|
||||
|
||||
if (foundTransaction !== -1) {
|
||||
this.transactions.splice(foundTransaction, 1);
|
||||
}
|
||||
}
|
||||
|
||||
for (const metadata of metadatas) {
|
||||
const foundMetadata = this.metadataS.findIndex(m => m.id === metadata.id);
|
||||
for (const metadata of blockMetadatas) {
|
||||
const foundMetadata = this.metadatas.findIndex(m => m.id === metadata.id);
|
||||
|
||||
if (foundMetadata !== -1) {
|
||||
this.metadataS.splice(foundMetadata, 1);
|
||||
this.metadatas.splice(foundMetadata, 1);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
clearAll() {
|
||||
this.transactions = [];
|
||||
this.metadataS = [];
|
||||
this.metadatas = [];
|
||||
}
|
||||
}
|
||||
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue