From 5b3ca8ce6f3635e5b9da32035c01f85ed74e9019 Mon Sep 17 00:00:00 2001 From: Christopher Jeffrey Date: Mon, 13 Mar 2017 20:52:40 -0700 Subject: [PATCH] chain: refactor getDeployments. --- lib/blockchain/chain.js | 44 ++++++++++++++++++++--------------------- 1 file changed, 22 insertions(+), 22 deletions(-) diff --git a/lib/blockchain/chain.js b/lib/blockchain/chain.js index 841f7e95..6a8b107f 100644 --- a/lib/blockchain/chain.js +++ b/lib/blockchain/chain.js @@ -226,7 +226,8 @@ Chain.prototype.isGenesis = function isGenesis(block) { Chain.prototype.verify = co(function* verify(block, prev, flags) { var ret = new VerifyResult(); var now = this.network.now(); - var i, height, ts, tx, mtp; + var height = prev.height + 1; + var i, ts, tx, mtp; var commit, state, bits; assert(typeof flags === 'number'); @@ -297,12 +298,26 @@ Chain.prototype.verify = co(function* verify(block, prev, flags) { true); } + // Only allow version 2 blocks (coinbase height) + // once the majority of blocks are using it. + if (block.version < 2 && height >= this.network.block.bip34height) + throw new VerifyError(block, 'obsolete', 'bad-version', 0); + + // Only allow version 3 blocks (sig validation) + // once the majority of blocks are using it. + if (block.version < 3 && height >= this.network.block.bip66height) + throw new VerifyError(block, 'obsolete', 'bad-version', 0); + + // Only allow version 4 blocks (checklocktimeverify) + // once the majority of blocks are using it. + if (block.version < 4 && height >= this.network.block.bip65height) + throw new VerifyError(block, 'obsolete', 'bad-version', 0); + // Get the new deployment state. - state = yield this.getDeployments(block, prev); + state = yield this.getDeployments(block.ts, prev); // Get timestamp for tx.isFinal(). ts = state.hasMTP() ? mtp : block.ts; - height = prev.height + 1; // Transactions must be finalized with // regards to nSequence and nLockTime. @@ -383,32 +398,17 @@ Chain.prototype.verify = co(function* verify(block, prev, flags) { /** * Check all deployments on a chain, ranging from p2sh to segwit. * @method - * @param {Block} block + * @param {Number} ts * @param {ChainEntry} prev * @returns {Promise} - Returns [{@link VerifyError}, {@link DeploymentState}]. */ -Chain.prototype.getDeployments = co(function* getDeployments(block, prev) { +Chain.prototype.getDeployments = co(function* getDeployments(ts, prev) { var deployments = this.network.deployments; var height = prev.height + 1; var state = new DeploymentState(); var active; - // Only allow version 2 blocks (coinbase height) - // once the majority of blocks are using it. - if (block.version < 2 && height >= this.network.block.bip34height) - throw new VerifyError(block, 'obsolete', 'bad-version', 0); - - // Only allow version 3 blocks (sig validation) - // once the majority of blocks are using it. - if (block.version < 3 && height >= this.network.block.bip66height) - throw new VerifyError(block, 'obsolete', 'bad-version', 0); - - // Only allow version 4 blocks (checklocktimeverify) - // once the majority of blocks are using it. - if (block.version < 4 && height >= this.network.block.bip65height) - throw new VerifyError(block, 'obsolete', 'bad-version', 0); - // For some reason bitcoind has p2sh in the // mandatory flags by default, when in reality // it wasn't activated until march 30th 2012. @@ -417,7 +417,7 @@ Chain.prototype.getDeployments = co(function* getDeployments(block, prev) { // not have a signature. See: // 6a26d2ecb67f27d1fa5524763b49029d7106e91e3cc05743073461a719776192 // 9c08a4d78931342b37fd5f72900fb9983087e6f46c4a097d8a1f52c74e28eaf6 - if (block.ts >= consensus.BIP16_TIME) + if (ts >= consensus.BIP16_TIME) state.flags |= Script.flags.VERIFY_P2SH; // Coinbase heights are now enforced (bip34). @@ -2177,7 +2177,7 @@ Chain.prototype.getDeploymentState = co(function* getDeploymentState() { if (this.options.spv) return this.state; - return yield this.getDeployments(this.tip.toHeaders(), prev); + return yield this.getDeployments(this.tip.ts, prev); }); /**