refactor.

This commit is contained in:
Christopher Jeffrey 2016-03-30 19:52:32 -07:00
parent f62ceb8515
commit 17d2f1586b

View File

@ -503,7 +503,7 @@ Chain.prototype._verify = function _verify(block, prev, callback) {
// lockFlags |= constants.flags.MEDIAN_TIME_PAST; // lockFlags |= constants.flags.MEDIAN_TIME_PAST;
if (network.type === 'segnet4') { if (network.type === 'segnet4') {
self.getState(prev, null, 'witness', function(err, state) { self.getState(prev, 'witness', function(err, state) {
if (err) if (err)
return callback(err); return callback(err);
@ -1777,7 +1777,7 @@ Chain.prototype.retarget = function retarget(last, first) {
}; };
// https://github.com/bitcoin/bitcoin/pull/7648/files // https://github.com/bitcoin/bitcoin/pull/7648/files
Chain.prototype.getState = function getState(prev, block, id, callback) { Chain.prototype.getState = function getState(prev, id, callback) {
var self = this; var self = this;
var period = network.minerConfirmationWindow; var period = network.minerConfirmationWindow;
var threshold = network.ruleChangeActivationThreshold; var threshold = network.ruleChangeActivationThreshold;
@ -1803,7 +1803,7 @@ Chain.prototype.getState = function getState(prev, block, id, callback) {
assert(ancestor.height === height); assert(ancestor.height === height);
assert(((ancestor.height + 1) % period) === 0); assert(((ancestor.height + 1) % period) === 0);
} }
return self.getState(ancestor, block, id, callback); return self.getState(ancestor, id, callback);
}); });
} }
@ -1912,7 +1912,7 @@ Chain.prototype.computeBlockVersion = function computeBlockVersion(prev, callbac
utils.forEachSerial(Object.keys(network.deployments), function(id, next) { utils.forEachSerial(Object.keys(network.deployments), function(id, next) {
var deployment = network.deployments[id]; var deployment = network.deployments[id];
self.getState(prev, null, id, function(err, state) { self.getState(prev, id, function(err, state) {
if (err) if (err)
return next(err); return next(err);
@ -1931,6 +1931,7 @@ Chain.prototype.computeBlockVersion = function computeBlockVersion(prev, callbac
return callback(null, constants.versionbits.LAST_OLD_BLOCK_VERSION); return callback(null, constants.versionbits.LAST_OLD_BLOCK_VERSION);
version |= constants.versionbits.TOP_BITS; version |= constants.versionbits.TOP_BITS;
version >>>= 0;
return callback(null, version); return callback(null, version);
}); });
@ -1938,34 +1939,17 @@ Chain.prototype.computeBlockVersion = function computeBlockVersion(prev, callbac
Chain.prototype.isSegwitActive = function isSegwitActive(callback) { Chain.prototype.isSegwitActive = function isSegwitActive(callback) {
var self = this; var self = this;
var tip = this.tip;
var unlock; var unlock;
if (this.segwitActive != null)
return utils.asyncify(callback)(null, this.segwitActive);
if (!network.witness) { if (!network.witness) {
this.segwitActive = false; this.segwitActive = false;
return utils.asyncify(callback)(null, false); return utils.asyncify(callback)(null, false);
} }
if (network.type === 'segnet4') { if (!this.tip)
return this.tip.getPrevious(function(err, prev) {
if (err)
return callback(err);
return self.getState(prev, null, 'witness', function(err, state) {
if (err)
return callback(err);
return callback(null, state === constants.thresholdStates.ACTIVE);
});
});
}
if (this.segwitActive != null)
return utils.asyncify(callback)(null, this.segwitActive);
if (!tip)
return utils.asyncify(callback)(null, false);
if (!(network.segwitHeight !== -1 && tip.height >= network.segwitHeight))
return utils.asyncify(callback)(null, false); return utils.asyncify(callback)(null, false);
unlock = this._lock(isSegwitActive, [callback]); unlock = this._lock(isSegwitActive, [callback]);
@ -1974,24 +1958,48 @@ Chain.prototype.isSegwitActive = function isSegwitActive(callback) {
callback = utils.wrap(callback, unlock); callback = utils.wrap(callback, unlock);
if (tip.ancestors.length) { if (network.type === 'segnet4') {
self.segwitActive = tip.isUpgraded(5); return this.tip.getPrevious(function(err, prev) {
return callback(null, self.segwitActive); if (err)
return callback(err);
return self.getState(prev, 'witness', function(err, state) {
if (err)
return callback(err);
self.segwitActive = state === constants.thresholdStates.ACTIVE;
return callback(null, self.segwitActive);
});
});
} }
tip.ensureAncestors(function(err) { assert(network.type === 'segnet3');
if (!(network.segwitHeight !== -1 && this.tip.height >= network.segwitHeight))
return utils.asyncify(callback)(null, false);
return this.tip.getPrevious(function(err, prev) {
if (err) if (err)
return callback(err); return callback(err);
if (!tip.isUpgraded(5)) { if (!prev) {
tip.free();
self.segwitActive = false; self.segwitActive = false;
return callback(null, false); return callback(null, false);
} }
tip.free(); prev.ensureAncestors(function(err) {
self.segwitActive = true; if (err)
return callback(null, true); return callback(err);
if (!prev.isUpgraded(5)) {
prev.free();
self.segwitActive = false;
return callback(null, false);
}
prev.free();
self.segwitActive = true;
return callback(null, true);
});
}); });
}; };