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
84
app/miner.js
84
app/miner.js
|
@ -1,34 +1,88 @@
|
|||
const Wallet = require('../wallet');
|
||||
const Transaction = require('../wallet/transaction');
|
||||
const Block = require('../blockchain/block');
|
||||
|
||||
|
||||
class Miner {
|
||||
static STATE_WAITING = 0;
|
||||
static STATE_RUNNING = 1;
|
||||
static STATE_INTERRUPTED = 2;
|
||||
static STATE_RESTARTING = 3;
|
||||
|
||||
constructor(blockchain, transactionPool, wallet, p2pServer) {
|
||||
this.blockchain = blockchain;
|
||||
this.transactionPool = transactionPool;
|
||||
this.wallet = wallet;
|
||||
this.p2pServer = p2pServer;
|
||||
}
|
||||
this.state = Miner.STATE_WAITING;
|
||||
this.mining = [[], []];
|
||||
this.lastBlock = null;
|
||||
}
|
||||
|
||||
interrupt() {
|
||||
if (this.state === Miner.STATE_RUNNING) {
|
||||
this.state = Miner.STATE_INTERRUPTED;
|
||||
}
|
||||
}
|
||||
|
||||
interruptIfContainsTransaction(transaction) {
|
||||
if (this.state === Miner.STATE_RUNNING && this.mining[0].find(t => t.id === transaction.id)) {
|
||||
this.state = Miner.STATE_INTERRUPTED;
|
||||
}
|
||||
}
|
||||
interruptIfContainsMetadata(metadata) {
|
||||
if (this.state === Miner.STATE_RUNNING && this.mining[1].find(t => t.id === metadata.id)) {
|
||||
this.state = Miner.STATE_INTERRUPTED;
|
||||
}
|
||||
}
|
||||
|
||||
startMine() {
|
||||
//only continue if state is waiting or restarting
|
||||
if (this.state !== Miner.STATE_WAITING && this.state !== Miner.STATE_RESTARTING) {
|
||||
return;
|
||||
}
|
||||
|
||||
mine() {
|
||||
const validTransactions = this.transactionPool.validTransactions();
|
||||
const validMetadataS = this.transactionPool.validMetadataS();
|
||||
|
||||
if (validTransactions.length === 0 && validMetadataS.length === 0) {
|
||||
this.state = Miner.STATE_WAITING;
|
||||
return;
|
||||
}
|
||||
|
||||
validTransactions.push(
|
||||
Transaction.rewardTransaction(this.wallet, Wallet.blockchainWallet())
|
||||
);
|
||||
console.log(validTransactions);
|
||||
console.log("//////");
|
||||
const validMetadataS = this.transactionPool.validMetadataS();
|
||||
// for (let i =0; i <validMetadataS.length; i++){
|
||||
// validTransactions.push(validMetadataS[i]);
|
||||
// }
|
||||
|
||||
console.log(validTransactions);
|
||||
// const validMetadataS = this.transactionPool.metadataS;
|
||||
const block = this.blockchain.addBlock([validTransactions, validMetadataS]);
|
||||
this.p2pServer.newBlock(block);
|
||||
this.transactionPool.clear();
|
||||
|
||||
return block;
|
||||
this.lastBlock = this.blockchain.chain[this.blockchain.chain.length - 1];
|
||||
|
||||
this.state = Miner.STATE_RUNNING;
|
||||
|
||||
this.mining = [validTransactions, validMetadataS];
|
||||
this.nonce = 0;
|
||||
this.mine();
|
||||
}
|
||||
|
||||
mine() {
|
||||
if (this.state !== Miner.STATE_RUNNING) {
|
||||
this.state = Miner.STATE_RESTARTING;
|
||||
startMine();
|
||||
return;
|
||||
}
|
||||
const timestamp = Date.now();
|
||||
const difficulty = Block.adjustDifficulty(this.lastBlock, timestamp);
|
||||
const hash = Block.hash(timestamp, this.lastBlock.hash, this.mining, this.nonce, difficulty);
|
||||
|
||||
if (hash.substring(0, difficulty) === '0'.repeat(difficulty)) {
|
||||
//success
|
||||
this.p2pServer.newBlock(new Block(timestamp, this.lastBlock.hash, hash, this.mining, this.nonce, difficulty));
|
||||
this.state = Miner.STATE_RESTARTING;
|
||||
setImmediate(() => { this.startMine() });
|
||||
} else {
|
||||
//failure
|
||||
this.nonce++;
|
||||
setImmediate(() => { this.mine() });
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue