expose chain deployment state.

This commit is contained in:
Christopher Jeffrey 2016-07-14 12:17:20 -07:00
parent 196f3ca861
commit 3def705d35
No known key found for this signature in database
GPG Key ID: 8962AB9DE6666BBD
2 changed files with 12 additions and 22 deletions

View File

@ -35,8 +35,7 @@ var VerifyError = bcoin.errors.VerifyError;
* @property {Number} bestHeight * @property {Number} bestHeight
* @property {ChainEntry?} tip * @property {ChainEntry?} tip
* @property {Number} height * @property {Number} height
* @property {Boolean} segwitActive * @property {DeploymentState} state
* @property {Boolean} csvActive
* @property {Object} orphan - Orphan map. * @property {Object} orphan - Orphan map.
* @emits Chain#open * @emits Chain#open
* @emits Chain#error * @emits Chain#error
@ -77,8 +76,7 @@ function Chain(options) {
this.tip = null; this.tip = null;
this.height = -1; this.height = -1;
this.synced = false; this.synced = false;
this.segwitActive = null; this.state = new DeploymentState();
this.csvActive = null;
this.stateCache = {}; this.stateCache = {};
this.orphan = { this.orphan = {
@ -219,10 +217,10 @@ Chain.prototype._open = function open(callback) {
if (err) if (err)
return callback(err); return callback(err);
if (self.csvActive) if (self.state.hasCSV())
self.logger.info('CSV is active.'); self.logger.info('CSV is active.');
if (self.segwitActive) if (self.state.hasWitness())
self.logger.info('Segwit is active.'); self.logger.info('Segwit is active.');
self.logger.memory(); self.logger.memory();
@ -475,7 +473,7 @@ Chain.prototype.verify = function verify(block, prev, callback) {
// Skip the genesis block. Skip all blocks in spv mode. // Skip the genesis block. Skip all blocks in spv mode.
if (this.options.spv || this.isGenesis(block)) if (this.options.spv || this.isGenesis(block))
return callback(null, new DeploymentState()); return callback(null, this.state);
// Ensure it's not an orphan // Ensure it's not an orphan
if (!prev) { if (!prev) {
@ -511,9 +509,8 @@ Chain.prototype.verify = function verify(block, prev, callback) {
if (err) if (err)
return callback(err); return callback(err);
// Expose the state of csv and segwit globally. // Expose the state globally.
self.csvActive = state.hasCSV(); self.state = state;
self.segwitActive = state.hasWitness();
// Can't verify any further when merkleblock or headers. // Can't verify any further when merkleblock or headers.
if (self.options.spv) if (self.options.spv)
@ -2258,9 +2255,6 @@ Chain.prototype.computeBlockVersion = function computeBlockVersion(prev, callbac
Chain.prototype.getInitialState = function getInitialState(callback) { Chain.prototype.getInitialState = function getInitialState(callback) {
var self = this; var self = this;
if (this.segwitActive != null)
return utils.nextTick(callback);
if (!this.tip) if (!this.tip)
return utils.nextTick(callback); return utils.nextTick(callback);
@ -2268,11 +2262,8 @@ Chain.prototype.getInitialState = function getInitialState(callback) {
if (err) if (err)
return callback(err); return callback(err);
if (!prev) { if (!prev)
self.csvActive = false;
self.segwitActive = false;
return callback(); return callback();
}
prev.getRetargetAncestors(function(err, ancestors) { prev.getRetargetAncestors(function(err, ancestors) {
if (err) if (err)
@ -2282,8 +2273,7 @@ Chain.prototype.getInitialState = function getInitialState(callback) {
if (err) if (err)
return callback(err); return callback(err);
self.csvActive = state.hasCSV(); self.state = state;
self.segwitActive = state.hasWitness();
return callback(null, state); return callback(null, state);
}); });

View File

@ -691,7 +691,7 @@ Mempool.prototype.addTX = function addTX(tx, callback, force) {
} }
if (this.requireStandard) { if (this.requireStandard) {
if (!this.chain.csvActive && tx.version >= 2) { if (!this.chain.state.hasCSV() && tx.version >= 2) {
return callback(new VerifyError(tx, return callback(new VerifyError(tx,
'nonstandard', 'nonstandard',
'premature-version2-tx', 'premature-version2-tx',
@ -699,7 +699,7 @@ Mempool.prototype.addTX = function addTX(tx, callback, force) {
} }
} }
if (!this.chain.segwitActive && !this.prematureWitness) { if (!this.chain.state.hasWitness() && !this.prematureWitness) {
if (tx.hasWitness()) { if (tx.hasWitness()) {
return callback(new VerifyError(tx, return callback(new VerifyError(tx,
'nonstandard', 'nonstandard',
@ -971,7 +971,7 @@ Mempool.prototype.verify = function verify(entry, callback) {
var ret = {}; var ret = {};
var fee, modFee, now, size, rejectFee, minRelayFee, minRate; var fee, modFee, now, size, rejectFee, minRelayFee, minRate;
if (this.chain.segwitActive) if (this.chain.state.hasWitness())
mandatory |= constants.flags.VERIFY_WITNESS; mandatory |= constants.flags.VERIFY_WITNESS;
this.checkLocks(tx, lockFlags, function(err, result) { this.checkLocks(tx, lockFlags, function(err, result) {