state caching.

This commit is contained in:
Christopher Jeffrey 2016-05-05 18:54:18 -07:00
parent 4af0372ca8
commit 625fb729f5
No known key found for this signature in database
GPG Key ID: 8962AB9DE6666BBD
2 changed files with 21 additions and 5 deletions

View File

@ -80,6 +80,7 @@ function Chain(options) {
this.synced = false;
this.segwitActive = null;
this.csvActive = null;
this.stateCache = {};
this.orphan = {
map: {},
@ -2238,6 +2239,9 @@ Chain.prototype.getState = function getState(prev, id, callback) {
if (!entry)
return walkForward(constants.thresholdStates.DEFINED);
if (self.stateCache[entry.hash])
return walkForward(self.stateCache[entry.hash]);
return entry.getMedianTimeAsync(function(err, medianTime) {
if (err)
return walk(err);
@ -2267,12 +2271,17 @@ Chain.prototype.getState = function getState(prev, id, callback) {
if (err)
return callback(err);
if (medianTime >= timeTimeout)
if (medianTime >= timeTimeout) {
self.stateCache[entry.hash] = constants.thresholdStates.FAILED;
return walkForward(constants.thresholdStates.FAILED);
}
if (medianTime >= timeStart)
if (medianTime >= timeStart) {
self.stateCache[entry.hash] = constants.thresholdStates.STARTED;
return walkForward(constants.thresholdStates.STARTED);
}
self.stateCache[entry.hash] = state;
return walkForward(state);
});
case constants.thresholdStates.STARTED:
@ -2280,8 +2289,10 @@ Chain.prototype.getState = function getState(prev, id, callback) {
if (err)
return callback(err);
if (medianTime >= timeTimeout)
if (medianTime >= timeTimeout) {
self.stateCache[entry.hash] = constants.thresholdStates.FAILED;
return walkForward(constants.thresholdStates.FAILED);
}
count = 0;
i = 0;
@ -2306,16 +2317,21 @@ Chain.prototype.getState = function getState(prev, id, callback) {
if (err)
return callback(err);
if (count >= threshold)
if (count >= threshold) {
self.stateCache[entry.hash] = constants.thresholdStates.LOCKED_IN;
return walkForward(constants.thresholdStates.LOCKED_IN);
}
self.stateCache[entry.hash] = state;
return walkForward(state);
}
});
case constants.thresholdStates.LOCKED_IN:
self.stateCache[entry.hash] = constants.thresholdStates.ACTIVE;
return walkForward(constants.thresholdStates.ACTIVE);
case constants.thresholdStates.FAILED:
case constants.thresholdStates.ACTIVE:
self.stateCache[entry.hash] = state;
return walkForward(state);
}

View File

@ -82,7 +82,7 @@ function ChainDB(chain, options) {
// We add a padding of 100 for forked chains,
// reorgs, chain locator creation and the bip34
// check.
this.cacheWindow = (network.pow.retargetInterval + 1) * 2 + 100;
this.cacheWindow = (network.pow.retargetInterval + 1) * 4 + 100;
this.coinCache = new NullCache(100000);
this.cacheHash = new bcoin.lru(this.cacheWindow);