chain: use separate state caches for separate deployments.

This commit is contained in:
Christopher Jeffrey 2016-07-24 23:09:22 -07:00
parent d68d79841a
commit cc6018a94e
No known key found for this signature in database
GPG Key ID: 8962AB9DE6666BBD

View File

@ -99,6 +99,15 @@ utils.inherits(Chain, AsyncObject);
Chain.prototype._init = function _init() { Chain.prototype._init = function _init() {
var self = this; var self = this;
var i, keys, id;
// Setup state caches.
keys = Object.keys(this.network.deployments);
for (i = 0; i < keys.length; i++) {
id = keys[i];
this.stateCache[id] = {};
}
this.locker.on('purge', function(total, size) { this.locker.on('purge', function(total, size) {
self.logger.warning('Warning: %dmb of pending objects. Purging.', utils.mb(size)); self.logger.warning('Warning: %dmb of pending objects. Purging.', utils.mb(size));
@ -2135,6 +2144,7 @@ Chain.prototype.getState = function getState(prev, id, callback) {
var period = this.network.minerWindow; var period = this.network.minerWindow;
var threshold = this.network.activationThreshold; var threshold = this.network.activationThreshold;
var deployment = this.network.deployments[id]; var deployment = this.network.deployments[id];
var stateCache = this.stateCache[id];
var timeStart, timeTimeout, compute, height; var timeStart, timeTimeout, compute, height;
if (!deployment) if (!deployment)
@ -2174,8 +2184,8 @@ Chain.prototype.getState = function getState(prev, id, callback) {
if (!entry) if (!entry)
return walkForward(constants.thresholdStates.DEFINED); return walkForward(constants.thresholdStates.DEFINED);
if (self.stateCache[entry.hash] != null) if (stateCache[entry.hash] != null)
return walkForward(self.stateCache[entry.hash]); return walkForward(stateCache[entry.hash]);
return entry.getMedianTimeAsync(function(err, medianTime) { return entry.getMedianTimeAsync(function(err, medianTime) {
if (err) if (err)
@ -2207,16 +2217,16 @@ Chain.prototype.getState = function getState(prev, id, callback) {
return callback(err); return callback(err);
if (medianTime >= timeTimeout) { if (medianTime >= timeTimeout) {
self.stateCache[entry.hash] = constants.thresholdStates.FAILED; stateCache[entry.hash] = constants.thresholdStates.FAILED;
return walkForward(constants.thresholdStates.FAILED); return walkForward(constants.thresholdStates.FAILED);
} }
if (medianTime >= timeStart) { if (medianTime >= timeStart) {
self.stateCache[entry.hash] = constants.thresholdStates.STARTED; stateCache[entry.hash] = constants.thresholdStates.STARTED;
return walkForward(constants.thresholdStates.STARTED); return walkForward(constants.thresholdStates.STARTED);
} }
self.stateCache[entry.hash] = state; stateCache[entry.hash] = state;
return walkForward(state); return walkForward(state);
}); });
case constants.thresholdStates.STARTED: case constants.thresholdStates.STARTED:
@ -2225,7 +2235,7 @@ Chain.prototype.getState = function getState(prev, id, callback) {
return callback(err); return callback(err);
if (medianTime >= timeTimeout) { if (medianTime >= timeTimeout) {
self.stateCache[entry.hash] = constants.thresholdStates.FAILED; stateCache[entry.hash] = constants.thresholdStates.FAILED;
return walkForward(constants.thresholdStates.FAILED); return walkForward(constants.thresholdStates.FAILED);
} }
@ -2253,20 +2263,20 @@ Chain.prototype.getState = function getState(prev, id, callback) {
return callback(err); return callback(err);
if (count >= threshold) { if (count >= threshold) {
self.stateCache[entry.hash] = constants.thresholdStates.LOCKED_IN; stateCache[entry.hash] = constants.thresholdStates.LOCKED_IN;
return walkForward(constants.thresholdStates.LOCKED_IN); return walkForward(constants.thresholdStates.LOCKED_IN);
} }
self.stateCache[entry.hash] = state; stateCache[entry.hash] = state;
return walkForward(state); return walkForward(state);
} }
}); });
case constants.thresholdStates.LOCKED_IN: case constants.thresholdStates.LOCKED_IN:
self.stateCache[entry.hash] = constants.thresholdStates.ACTIVE; stateCache[entry.hash] = constants.thresholdStates.ACTIVE;
return walkForward(constants.thresholdStates.ACTIVE); return walkForward(constants.thresholdStates.ACTIVE);
case constants.thresholdStates.FAILED: case constants.thresholdStates.FAILED:
case constants.thresholdStates.ACTIVE: case constants.thresholdStates.ACTIVE:
self.stateCache[entry.hash] = state; stateCache[entry.hash] = state;
return walkForward(state); return walkForward(state);
} }