minerblock: commit block once mined.

This commit is contained in:
Christopher Jeffrey 2016-11-23 00:56:56 -08:00
parent 9e48947d2f
commit 390f7d8ddb
No known key found for this signature in database
GPG Key ID: 8962AB9DE6666BBD
2 changed files with 28 additions and 11 deletions

View File

@ -1355,10 +1355,8 @@ RPC.prototype.__submitwork = co(function* _submitwork(data) {
return false; return false;
} }
block.nonce = header.nonce;
block.ts = header.ts; block.ts = header.ts;
block.mutable = false; attempt.commit(header.nonce);
block.txs[0].mutable = false;
try { try {
yield this.chain.add(block); yield this.chain.add(block);

View File

@ -62,9 +62,11 @@ function MinerBlock(options) {
this.witness = options.witness; this.witness = options.witness;
this.address = options.address; this.address = options.address;
this.network = Network.get(options.network); this.network = Network.get(options.network);
this.destroyed = false;
this.reward = btcutils.getReward(this.height, this.network.halvingInterval); this.reward = btcutils.getReward(this.height, this.network.halvingInterval);
this.destroyed = false;
this.committed = false;
this.sigops = 0; this.sigops = 0;
this.weight = 0; this.weight = 0;
this.fees = 0; this.fees = 0;
@ -289,7 +291,7 @@ MinerBlock.prototype.addTX = function addTX(tx) {
this.fees += tx.getFee(); this.fees += tx.getFee();
// Add the tx to our block // Add the tx to our block
this.block.addTX(tx.clone()); this.block.txs.push(tx.clone());
// Update coinbase value // Update coinbase value
this.updateCoinbase(); this.updateCoinbase();
@ -387,9 +389,7 @@ MinerBlock.prototype.mine = function mine() {
this.iterate(); this.iterate();
} }
this.block.nonce = nonce; this.commit(nonce);
this.block.mutable = false;
this.coinbase.mutable = false;
return this.block; return this.block;
}; };
@ -419,9 +419,7 @@ MinerBlock.prototype.mineAsync = co(function* mineAsync() {
this.iterate(); this.iterate();
} }
this.block.nonce = nonce; this.commit(nonce);
this.block.mutable = false;
this.coinbase.mutable = false;
return this.block; return this.block;
}); });
@ -457,6 +455,27 @@ MinerBlock.prototype.iterate = function iterate() {
this.updateNonce(); this.updateNonce();
}; };
/**
* Commit and finalize mined block.
* @returns {Block}
*/
MinerBlock.prototype.commit = function commit(nonce) {
var i, tx;
assert(!this.committed, 'Block is already committed.');
this.committed = true;
this.block.nonce = nonce;
this.block.mutable = false;
this.coinbase.mutable = false;
for (i = 0; i < this.block.txs.length; i++) {
tx = this.block.txs[i];
tx.setBlock(this.block, i);
}
};
/** /**
* Send a progress report (emits `status`). * Send a progress report (emits `status`).
*/ */