From 390f7d8ddbcd010257b60786d93bd17865fef2ef Mon Sep 17 00:00:00 2001 From: Christopher Jeffrey Date: Wed, 23 Nov 2016 00:56:56 -0800 Subject: [PATCH] minerblock: commit block once mined. --- lib/http/rpc.js | 4 +--- lib/mining/minerblock.js | 35 +++++++++++++++++++++++++++-------- 2 files changed, 28 insertions(+), 11 deletions(-) diff --git a/lib/http/rpc.js b/lib/http/rpc.js index 42bb895d..5383f62c 100644 --- a/lib/http/rpc.js +++ b/lib/http/rpc.js @@ -1355,10 +1355,8 @@ RPC.prototype.__submitwork = co(function* _submitwork(data) { return false; } - block.nonce = header.nonce; block.ts = header.ts; - block.mutable = false; - block.txs[0].mutable = false; + attempt.commit(header.nonce); try { yield this.chain.add(block); diff --git a/lib/mining/minerblock.js b/lib/mining/minerblock.js index 9cfd9df6..3004f6ab 100644 --- a/lib/mining/minerblock.js +++ b/lib/mining/minerblock.js @@ -62,9 +62,11 @@ function MinerBlock(options) { this.witness = options.witness; this.address = options.address; this.network = Network.get(options.network); - this.destroyed = false; this.reward = btcutils.getReward(this.height, this.network.halvingInterval); + this.destroyed = false; + this.committed = false; + this.sigops = 0; this.weight = 0; this.fees = 0; @@ -289,7 +291,7 @@ MinerBlock.prototype.addTX = function addTX(tx) { this.fees += tx.getFee(); // Add the tx to our block - this.block.addTX(tx.clone()); + this.block.txs.push(tx.clone()); // Update coinbase value this.updateCoinbase(); @@ -387,9 +389,7 @@ MinerBlock.prototype.mine = function mine() { this.iterate(); } - this.block.nonce = nonce; - this.block.mutable = false; - this.coinbase.mutable = false; + this.commit(nonce); return this.block; }; @@ -419,9 +419,7 @@ MinerBlock.prototype.mineAsync = co(function* mineAsync() { this.iterate(); } - this.block.nonce = nonce; - this.block.mutable = false; - this.coinbase.mutable = false; + this.commit(nonce); return this.block; }); @@ -457,6 +455,27 @@ MinerBlock.prototype.iterate = function iterate() { 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`). */