From 82ee87c0f521324dc3f36e767691ab65510d7b5c Mon Sep 17 00:00:00 2001 From: Christopher Jeffrey Date: Thu, 31 Mar 2016 00:45:30 -0700 Subject: [PATCH] refactor. --- lib/bcoin/block.js | 18 ++++++++++++++++++ lib/bcoin/chain.js | 20 ++++---------------- lib/bcoin/tx.js | 18 +++++++----------- 3 files changed, 29 insertions(+), 27 deletions(-) diff --git a/lib/bcoin/block.js b/lib/bcoin/block.js index 36178766..1eb42fc3 100644 --- a/lib/bcoin/block.js +++ b/lib/bcoin/block.js @@ -254,6 +254,24 @@ Block.prototype.getCoinbaseHeight = function getCoinbaseHeight() { return height; }; +Block.prototype.getReward = function getReward() { + var reward = Block.reward(this.height); + var i; + + assert(this.height !== -1); + + for (i = 1; i < this.txs.length; i++) + reward.iadd(this.txs[i].getFee()); + + return reward; +}; + +Block.prototype.getClaimed = function getClaimed() { + assert(this.txs[0]); + assert(this.txs[0].isCoinbase()); + return this.txs[0].getOutputValue(); +}; + Block.reward = function reward(height) { var halvings = height / network.halvingInterval | 0; var reward; diff --git a/lib/bcoin/chain.js b/lib/bcoin/chain.js index c9d925ff..f0d0e746 100644 --- a/lib/bcoin/chain.js +++ b/lib/bcoin/chain.js @@ -521,7 +521,7 @@ Chain.prototype._verify = function _verify(block, prev, callback) { flags |= constants.flags.VERIFY_CHECKLOCKTIMEVERIFY; // Segregrated witness is now usable - if (network.segwitHeight !== -1 && height >= network.segwitHeight) { + if (network.type === 'segnet3' && height >= network.segwitHeight) { if (block.version >= 5 && prev.isUpgraded(5)) { flags |= constants.flags.VERIFY_WITNESS; segwit = true; @@ -565,7 +565,7 @@ Chain.prototype._verify = function _verify(block, prev, callback) { return done(new VerifyError(block, 'invalid', 'bad-cb-height', 100)); } - if (block.version >= 5 && segwit) { + if (segwit) { if (block.commitmentHash !== block.getCommitmentHash('hex')) { return done(new VerifyError(block, 'invalid', @@ -582,7 +582,7 @@ Chain.prototype._verify = function _verify(block, prev, callback) { } // Get timestamp for tx.isFinal(). - ts = (lockFlags & constants.flags.MEDIAN_TIME_PAST) + ts = (lockFlags & constants.flags.MEDIAN_TIME_PAST) !== 0 ? medianTime : block.ts; @@ -668,7 +668,7 @@ Chain.prototype._checkInputs = function _checkInputs(block, prev, flags, callbac if (err) return callback(err); - if (!self._checkReward(block)) + if (block.getClaimed().cmp(block.getReward()) > 0) return callback(new VerifyError(block, 'invalid', 'bad-cb-amount', 100)); // Check all transactions @@ -757,18 +757,6 @@ Chain.prototype._checkInputs = function _checkInputs(block, prev, flags, callbac }); }; -Chain.prototype._checkReward = function _checkReward(block) { - var i, claimed, actual; - - claimed = block.txs[0].getOutputValue(); - actual = bcoin.block.reward(block.height); - - for (i = 1; i < block.txs.length; i++) - actual.iadd(block.txs[i].getFee()); - - return claimed.cmp(actual) <= 0; -}; - Chain.prototype.getHeight = function getHeight(hash) { if (Buffer.isBuffer(hash)) hash = utils.toHex(hash); diff --git a/lib/bcoin/tx.js b/lib/bcoin/tx.js index 901012cd..e47ec1ed 100644 --- a/lib/bcoin/tx.js +++ b/lib/bcoin/tx.js @@ -548,9 +548,6 @@ TX.prototype.testInputs = function testInputs(addressMap, index) { return this.inputs[index].test(addressMap); for (i = 0; i < this.inputs.length; i++) { - if (index != null && i !== index) - continue; - if (this.inputs[i].test(addressMap)) return true; } @@ -574,9 +571,6 @@ TX.prototype.testOutputs = function testOutputs(addressMap, index) { return this.outputs[index].test(addressMap); for (i = 0; i < this.outputs.length; i++) { - if (index != null && i !== index) - continue; - if (this.outputs[i].test(addressMap)) return true; } @@ -723,9 +717,9 @@ TX.prototype.getSigops = function getSigops(scriptHash, accurate) { // CheckTransaction TX.prototype.isSane = function isSane(ret) { - var uniq = {}; + var prevout = {}; var total = new bn(0); - var i, input, output, size; + var i, input, output, size, key; if (!ret) ret = {}; @@ -774,12 +768,13 @@ TX.prototype.isSane = function isSane(ret) { for (i = 0; i < this.inputs.length; i++) { input = this.inputs[i]; - if (uniq[input.prevout.hash]) { + key = input.prevout.hash + '/' + input.prevout.index; + if (prevout[key]) { ret.reason = 'bad-txns-inputs-duplicate'; ret.score = 100; return false; } - uniq[input.prevout.hash] = true; + prevout[key] = true; } if (this.isCoinbase()) { @@ -792,7 +787,8 @@ TX.prototype.isSane = function isSane(ret) { } else { for (i = 0; i < this.inputs.length; i++) { input = this.inputs[i]; - if (input.prevout.hash === constants.nullHash) { + if (input.prevout.hash === constants.nullHash + && input.prevout.index === 0xffffffff) { ret.reason = 'bad-txns-prevout-null'; ret.score = 10; return false;