From 8c923179dc6eba3aaf2461e90490c3eaa8ed70b3 Mon Sep 17 00:00:00 2001 From: Christopher Jeffrey Date: Mon, 26 Sep 2016 13:06:29 -0700 Subject: [PATCH] chain: refactor tx validation. --- lib/chain/chain.js | 27 +++++++++++++-------------- lib/utils/spawn.js | 21 +++++++++++++++++++++ lib/wallet/txdb.js | 1 + 3 files changed, 35 insertions(+), 14 deletions(-) diff --git a/lib/chain/chain.js b/lib/chain/chain.js index 18ed4757..c5ea5d15 100644 --- a/lib/chain/chain.js +++ b/lib/chain/chain.js @@ -604,7 +604,7 @@ Chain.prototype.checkInputs = co(function* checkInputs(block, prev, state) { var sigops = 0; var jobs = []; var ret = new VerifyResult(); - var i, view, tx, valid, result; + var i, view, tx, valid; if (this.options.spv) return; @@ -677,16 +677,13 @@ Chain.prototype.checkInputs = co(function* checkInputs(block, prev, state) { return view; // Verify all txs in parallel. - result = yield Promise.all(jobs); + valid = yield spawn.every(jobs); - for (i = 0; i < result.length; i++) { - valid = result[i]; - if (!valid) { - throw new VerifyError(block, - 'invalid', - 'mandatory-script-verify-flag-failed', - 100); - } + if (!valid) { + throw new VerifyError(block, + 'invalid', + 'mandatory-script-verify-flag-failed', + 100); } // Make sure the miner isn't trying to conjure more coins. @@ -1075,7 +1072,7 @@ Chain.prototype._add = co(function* add(block) { } // Special case for genesis block. - if (this.isGenesis(block)) + if (hash === this.network.genesis.hash) break; // Validate the block we want to add. @@ -1861,13 +1858,15 @@ Chain.prototype.getState = co(function* getState(prev, id) { if (block.hasBit(deployment)) count++; + if (count >= threshold) { + state = constants.thresholdStates.LOCKED_IN; + break; + } + block = yield block.getPrevious(); assert(block); } - if (count >= threshold) - state = constants.thresholdStates.LOCKED_IN; - break; case constants.thresholdStates.LOCKED_IN: state = constants.thresholdStates.ACTIVE; diff --git a/lib/utils/spawn.js b/lib/utils/spawn.js index 86abffac..137e5fc5 100644 --- a/lib/utils/spawn.js +++ b/lib/utils/spawn.js @@ -8,6 +8,7 @@ 'use strict'; var utils = require('./utils'); +var every; /** * Execute an instantiated generator. @@ -272,6 +273,25 @@ function promisify(func, ctx) { }; } +/** + * Execute each promise and + * have them pass a truth test. + * @param {Promise[]} jobs + * @returns {Promise} + */ + +every = co(function* every(jobs) { + var result = yield Promise.all(jobs); + var i; + + for (i = 0; i < result.length; i++) { + if (!result[i]) + return false; + } + + return true; +}); + /* * This drives me nuts. */ @@ -302,5 +322,6 @@ exports.timeout = timeout; exports.wrap = wrap; exports.call = call; exports.promisify = promisify; +exports.every = every; module.exports = spawn; diff --git a/lib/wallet/txdb.js b/lib/wallet/txdb.js index 69e08a77..10ce8d76 100644 --- a/lib/wallet/txdb.js +++ b/lib/wallet/txdb.js @@ -1832,6 +1832,7 @@ TXDB.prototype.getSpentCoin = co(function* getSpentCoin(spent, prevout) { coin = bcoin.coin.fromRaw(data); coin.hash = prevout.hash; coin.index = prevout.index; + return coin; });