smarter store update after replacing chains
concurrent mining validity checks for new transactions/blocks/metadata rdf store stores quads smarter clearing of transactions from pool when a new block is mined change querying to be readonly and generic moved some things around
This commit is contained in:
parent
ea81105df6
commit
b4c2a0c88d
7 changed files with 373 additions and 177 deletions
|
@ -1,12 +1,12 @@
|
|||
const ChainUtil = require('../chain-util');
|
||||
const Transaction = require('./transaction');
|
||||
const { INITIAL_BALANCE } = require('../config');
|
||||
const Metadata = require('./metadata');
|
||||
const ChainUtil = require('../chain-util');
|
||||
|
||||
class Wallet {
|
||||
constructor() {
|
||||
constructor(keyPair) {
|
||||
this.balance = INITIAL_BALANCE;
|
||||
this.keyPair = ChainUtil.genKeyPair();
|
||||
this.keyPair = keyPair;
|
||||
this.publicKey = this.keyPair.getPublic().encode('hex');
|
||||
}
|
||||
|
||||
|
@ -25,36 +25,16 @@ class Wallet {
|
|||
|
||||
if (amount > this.balance) {
|
||||
console.log(`Amount: ${amount} exceceds current balance: ${this.balance}`);
|
||||
return;
|
||||
return null;
|
||||
}
|
||||
|
||||
let transaction = transactionPool.existingTransaction(this.publicKey);
|
||||
|
||||
if (transaction) {
|
||||
transaction.update(this, recipient, amount);
|
||||
} else {
|
||||
transaction = Transaction.newTransaction(this, recipient, amount);
|
||||
transactionPool.updateOrAddTransaction(transaction);
|
||||
}
|
||||
|
||||
return transaction;
|
||||
return Transaction.newTransaction(this, recipient, amount);
|
||||
}
|
||||
|
||||
createMetadata(SSNmetadata, transactionPool){
|
||||
//let metadata = transactionPool.existingMetadata(this.publicKey);
|
||||
createMetadata(SSNmetadata) {
|
||||
return Metadata.newMetadata(this, SSNmetadata);
|
||||
}
|
||||
|
||||
// if (metaData) {
|
||||
// metadata.update(this, Geo, Std, Name,MetaHash,file);
|
||||
// } else {*/
|
||||
|
||||
let metadata= Metadata.newMetadata(this, SSNmetadata);
|
||||
transactionPool.AddMetadata(metadata);
|
||||
|
||||
//}
|
||||
return metadata;
|
||||
}
|
||||
|
||||
|
||||
calculateBalance(blockchain) {
|
||||
let balance = this.balance;
|
||||
let transactions = [];
|
||||
|
@ -93,7 +73,7 @@ class Wallet {
|
|||
}
|
||||
|
||||
static blockchainWallet() {
|
||||
const blockchainWallet = new this();
|
||||
const blockchainWallet = new this(ChainUtil.genKeyPair());
|
||||
blockchainWallet.address = 'blockchain-wallet';
|
||||
return blockchainWallet;
|
||||
}
|
||||
|
|
|
@ -1,29 +1,50 @@
|
|||
const Transaction = require('../wallet/transaction');
|
||||
const Metadata = require('../wallet/metadata')
|
||||
const Metadata = require('../wallet/metadata')
|
||||
|
||||
const Return = {
|
||||
add: 1,
|
||||
update: 2,
|
||||
error: 3
|
||||
};
|
||||
|
||||
class TransactionPool {
|
||||
constructor() {
|
||||
this.transactions = [];
|
||||
this.metadataS =[];
|
||||
}
|
||||
|
||||
//returns true on update, false on add
|
||||
updateOrAddTransaction(transaction) {
|
||||
let transactionWithId = this.transactions.find(t => t.id === transaction.id);
|
||||
if (!Transaction.verifyTransaction(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);
|
||||
|
||||
if (transactionWithId) {
|
||||
this.transactions[this.transactions.indexOf(transactionWithId)] = transaction;
|
||||
if (foundIndex !== -1) {
|
||||
this.transactions[foundIndex] = transaction;
|
||||
return Return.update;
|
||||
} else {
|
||||
this.transactions.push(transaction);
|
||||
return Return.add;
|
||||
}
|
||||
}
|
||||
|
||||
AddMetadata(metadata) {
|
||||
// let metadataWithId = this.metadataS.find(t => t.id === metadata.id);
|
||||
updateOrAddMetadata(metadata) {
|
||||
if (!Metadata.verifyMetadata(metadata)) {
|
||||
console.log("Couldn't update metdata, metadata couldn't be verified");
|
||||
return Return.error;
|
||||
}
|
||||
|
||||
// if (metadataWithId) {
|
||||
// this.metaDataS[this.metadataS.indexOf(metadataWithId)] = metadata;
|
||||
// } else {
|
||||
const foundIndex = this.metadataS.findIndex(t => t.id === metadata.id);
|
||||
|
||||
if (foundIndex !== -1) {
|
||||
this.metadataS[foundIndex] = metadata;
|
||||
return Return.update;
|
||||
} else {
|
||||
this.metadataS.push(metadata);
|
||||
// }
|
||||
return Return.add;
|
||||
}
|
||||
}
|
||||
|
||||
existingTransaction(address) {
|
||||
|
@ -64,10 +85,31 @@ class TransactionPool {
|
|||
});
|
||||
}
|
||||
|
||||
clear() {
|
||||
clearFromBlock(block) {
|
||||
const transactions = block.data[0];
|
||||
const metadatas = block.data[1];
|
||||
for (const transaction of transactions) {
|
||||
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);
|
||||
|
||||
if (foundMetadata !== -1) {
|
||||
this.metadataS.splice(foundMetadata, 1);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
clearAll() {
|
||||
this.transactions = [];
|
||||
this.metadataS = [];
|
||||
}
|
||||
}
|
||||
|
||||
module.exports = TransactionPool;
|
||||
module.exports = TransactionPool;
|
||||
module.exports.Return = Return;
|
Loading…
Add table
Add a link
Reference in a new issue