chain: refactor getDeployments.

This commit is contained in:
Christopher Jeffrey 2017-03-13 20:52:40 -07:00
parent 023591978a
commit 5b3ca8ce6f
No known key found for this signature in database
GPG Key ID: 8962AB9DE6666BBD

View File

@ -226,7 +226,8 @@ Chain.prototype.isGenesis = function isGenesis(block) {
Chain.prototype.verify = co(function* verify(block, prev, flags) { Chain.prototype.verify = co(function* verify(block, prev, flags) {
var ret = new VerifyResult(); var ret = new VerifyResult();
var now = this.network.now(); 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; var commit, state, bits;
assert(typeof flags === 'number'); assert(typeof flags === 'number');
@ -297,12 +298,26 @@ Chain.prototype.verify = co(function* verify(block, prev, flags) {
true); 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. // Get the new deployment state.
state = yield this.getDeployments(block, prev); state = yield this.getDeployments(block.ts, prev);
// Get timestamp for tx.isFinal(). // Get timestamp for tx.isFinal().
ts = state.hasMTP() ? mtp : block.ts; ts = state.hasMTP() ? mtp : block.ts;
height = prev.height + 1;
// Transactions must be finalized with // Transactions must be finalized with
// regards to nSequence and nLockTime. // 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. * Check all deployments on a chain, ranging from p2sh to segwit.
* @method * @method
* @param {Block} block * @param {Number} ts
* @param {ChainEntry} prev * @param {ChainEntry} prev
* @returns {Promise} - Returns [{@link VerifyError}, {@link DeploymentState}]. * @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 deployments = this.network.deployments;
var height = prev.height + 1; var height = prev.height + 1;
var state = new DeploymentState(); var state = new DeploymentState();
var active; 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 // For some reason bitcoind has p2sh in the
// mandatory flags by default, when in reality // mandatory flags by default, when in reality
// it wasn't activated until march 30th 2012. // 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: // not have a signature. See:
// 6a26d2ecb67f27d1fa5524763b49029d7106e91e3cc05743073461a719776192 // 6a26d2ecb67f27d1fa5524763b49029d7106e91e3cc05743073461a719776192
// 9c08a4d78931342b37fd5f72900fb9983087e6f46c4a097d8a1f52c74e28eaf6 // 9c08a4d78931342b37fd5f72900fb9983087e6f46c4a097d8a1f52c74e28eaf6
if (block.ts >= consensus.BIP16_TIME) if (ts >= consensus.BIP16_TIME)
state.flags |= Script.flags.VERIFY_P2SH; state.flags |= Script.flags.VERIFY_P2SH;
// Coinbase heights are now enforced (bip34). // Coinbase heights are now enforced (bip34).
@ -2177,7 +2177,7 @@ Chain.prototype.getDeploymentState = co(function* getDeploymentState() {
if (this.options.spv) if (this.options.spv)
return this.state; return this.state;
return yield this.getDeployments(this.tip.toHeaders(), prev); return yield this.getDeployments(this.tip.ts, prev);
}); });
/** /**