From 9561b3ee74325078f7168308740cc06a009b454c Mon Sep 17 00:00:00 2001 From: Christopher Jeffrey Date: Sun, 18 Sep 2016 12:57:50 -0700 Subject: [PATCH] rpc: getblocktemplate - abide by bip145. --- lib/http/rpc.js | 45 +++++++++++++++++++++++++++++++-------------- 1 file changed, 31 insertions(+), 14 deletions(-) diff --git a/lib/http/rpc.js b/lib/http/rpc.js index c6801407..53375c77 100644 --- a/lib/http/rpc.js +++ b/lib/http/rpc.js @@ -1640,7 +1640,7 @@ RPC.prototype._tmpl = function _tmpl(version, coinbase, rules, callback) { var self = this; var txs = []; var txIndex = {}; - var i, j, tx, deps, input, dep, block; + var i, j, tx, deps, input, dep, block, output, raw, rwhash; var keys, vbavailable, vbrules, mutable, template; callback = this.locker.lock(_tmpl, [version, coinbase, rules, callback]); @@ -1729,10 +1729,6 @@ RPC.prototype._tmpl = function _tmpl(version, coinbase, rules, callback) { vbrequired: 0, previousblockhash: utils.revHex(block.prevBlock), transactions: txs, - coinbaseaux: { - flags: attempt.coinbaseFlags.toString('hex') - }, - coinbasevalue: attempt.coinbase.outputs[0].value, longpollid: self.chain.tip.rhash + utils.pad32(self._totalTX()), target: utils.revHex(attempt.target.toString('hex')), submitold: false, @@ -1747,25 +1743,46 @@ RPC.prototype._tmpl = function _tmpl(version, coinbase, rules, callback) { weightlimit: constants.block.MAX_WEIGHT, curtime: block.ts, bits: utils.hex32(block.bits), - height: attempt.height, - default_witness_commitment: attempt.witness - ? attempt.coinbase.outputs[1].script.toJSON() - : undefined + height: attempt.height }; if (coinbase) { - template.coinbaseaux = undefined; - template.coinbasevalue = undefined; - tx = block.txs[0]; + tx = attempt.coinbase; + + // We don't include the commitment + // output (see bip145). + if (attempt.witness) { + output = tx.outputs.pop(); + assert(output.script.isCommitment()); + raw = tx.toRaw(); + rwhash = tx.rwhash; + tx.outputs.push(output); + } else { + raw = tx.toRaw(); + rwhash = tx.rwhash; + } + template.coinbasetxn = { - data: tx.toRaw().toString('hex'), + data: raw.toString('hex'), txid: tx.rhash, - hash: tx.rwhash, + hash: rwhash, depends: [], fee: 0, sigops: tx.getSigops(), weight: tx.getWeight() }; + } else { + template.coinbaseaux = { + flags: attempt.coinbaseFlags.toString('hex') + }; + template.coinbasevalue = attempt.coinbase.getOutputValue(); + } + + if (attempt.witness) { + tx = attempt.coinbase; + output = tx.outputs[tx.outputs.length - 1]; + assert(output.script.isCommitment()); + template.default_witness_commitment = output.script.toJSON(); } callback(null, template);