state caching.
This commit is contained in:
parent
4af0372ca8
commit
625fb729f5
@ -80,6 +80,7 @@ function Chain(options) {
|
|||||||
this.synced = false;
|
this.synced = false;
|
||||||
this.segwitActive = null;
|
this.segwitActive = null;
|
||||||
this.csvActive = null;
|
this.csvActive = null;
|
||||||
|
this.stateCache = {};
|
||||||
|
|
||||||
this.orphan = {
|
this.orphan = {
|
||||||
map: {},
|
map: {},
|
||||||
@ -2238,6 +2239,9 @@ 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])
|
||||||
|
return walkForward(self.stateCache[entry.hash]);
|
||||||
|
|
||||||
return entry.getMedianTimeAsync(function(err, medianTime) {
|
return entry.getMedianTimeAsync(function(err, medianTime) {
|
||||||
if (err)
|
if (err)
|
||||||
return walk(err);
|
return walk(err);
|
||||||
@ -2267,12 +2271,17 @@ Chain.prototype.getState = function getState(prev, id, callback) {
|
|||||||
if (err)
|
if (err)
|
||||||
return callback(err);
|
return callback(err);
|
||||||
|
|
||||||
if (medianTime >= timeTimeout)
|
if (medianTime >= timeTimeout) {
|
||||||
|
self.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;
|
||||||
return walkForward(constants.thresholdStates.STARTED);
|
return walkForward(constants.thresholdStates.STARTED);
|
||||||
|
}
|
||||||
|
|
||||||
|
self.stateCache[entry.hash] = state;
|
||||||
return walkForward(state);
|
return walkForward(state);
|
||||||
});
|
});
|
||||||
case constants.thresholdStates.STARTED:
|
case constants.thresholdStates.STARTED:
|
||||||
@ -2280,8 +2289,10 @@ Chain.prototype.getState = function getState(prev, id, callback) {
|
|||||||
if (err)
|
if (err)
|
||||||
return callback(err);
|
return callback(err);
|
||||||
|
|
||||||
if (medianTime >= timeTimeout)
|
if (medianTime >= timeTimeout) {
|
||||||
|
self.stateCache[entry.hash] = constants.thresholdStates.FAILED;
|
||||||
return walkForward(constants.thresholdStates.FAILED);
|
return walkForward(constants.thresholdStates.FAILED);
|
||||||
|
}
|
||||||
|
|
||||||
count = 0;
|
count = 0;
|
||||||
i = 0;
|
i = 0;
|
||||||
@ -2306,16 +2317,21 @@ Chain.prototype.getState = function getState(prev, id, callback) {
|
|||||||
if (err)
|
if (err)
|
||||||
return callback(err);
|
return callback(err);
|
||||||
|
|
||||||
if (count >= threshold)
|
if (count >= threshold) {
|
||||||
|
self.stateCache[entry.hash] = constants.thresholdStates.LOCKED_IN;
|
||||||
return walkForward(constants.thresholdStates.LOCKED_IN);
|
return walkForward(constants.thresholdStates.LOCKED_IN);
|
||||||
|
}
|
||||||
|
|
||||||
|
self.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;
|
||||||
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;
|
||||||
return walkForward(state);
|
return walkForward(state);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@ -82,7 +82,7 @@ function ChainDB(chain, options) {
|
|||||||
// We add a padding of 100 for forked chains,
|
// We add a padding of 100 for forked chains,
|
||||||
// reorgs, chain locator creation and the bip34
|
// reorgs, chain locator creation and the bip34
|
||||||
// check.
|
// check.
|
||||||
this.cacheWindow = (network.pow.retargetInterval + 1) * 2 + 100;
|
this.cacheWindow = (network.pow.retargetInterval + 1) * 4 + 100;
|
||||||
|
|
||||||
this.coinCache = new NullCache(100000);
|
this.coinCache = new NullCache(100000);
|
||||||
this.cacheHash = new bcoin.lru(this.cacheWindow);
|
this.cacheHash = new bcoin.lru(this.cacheWindow);
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user