chain/wallet: handle caches better.

This commit is contained in:
Christopher Jeffrey 2016-11-26 01:12:38 -08:00
parent 3e37961927
commit c5a5d4498a
No known key found for this signature in database
GPG Key ID: 8962AB9DE6666BBD
3 changed files with 15 additions and 6 deletions

View File

@ -83,14 +83,17 @@ function ChainDB(chain) {
this.cacheWindow = (this.network.pow.retargetInterval + 1) * 2 + 100; this.cacheWindow = (this.network.pow.retargetInterval + 1) * 2 + 100;
// We want to keep the last 5 blocks of unspents in memory. // We want to keep the last 5 blocks of unspents in memory.
this.coinWindow = 65 << 20; this.coinWindow = 0;
this.coinCache = new LRU.Nil(); this.coinCache = new LRU.Nil();
this.cacheHash = new LRU(this.cacheWindow); this.cacheHash = new LRU(this.cacheWindow);
this.cacheHeight = new LRU(this.cacheWindow); this.cacheHeight = new LRU(this.cacheWindow);
if (chain.options.coinCache) if (chain.options.coinCache) {
assert(typeof chain.options.coinCache === 'number');
this.coinWindow = chain.options.coinCache;
this.coinCache = new LRU(this.coinWindow, getSize); this.coinCache = new LRU(this.coinWindow, getSize);
}
} }
util.inherits(ChainDB, AsyncObject); util.inherits(ChainDB, AsyncObject);
@ -621,6 +624,7 @@ ChainDB.prototype.getTips = function getTips() {
*/ */
ChainDB.prototype.getCoin = co(function* getCoin(hash, index) { ChainDB.prototype.getCoin = co(function* getCoin(hash, index) {
var state = this.state;
var coins; var coins;
if (this.options.spv) if (this.options.spv)
@ -636,7 +640,8 @@ ChainDB.prototype.getCoin = co(function* getCoin(hash, index) {
if (!coins) if (!coins)
return; return;
this.coinCache.set(hash, coins); if (state === this.state)
this.coinCache.set(hash, coins);
return Coins.parseCoin(coins, hash, index); return Coins.parseCoin(coins, hash, index);
}); });
@ -648,6 +653,7 @@ ChainDB.prototype.getCoin = co(function* getCoin(hash, index) {
*/ */
ChainDB.prototype.getCoins = co(function* getCoins(hash) { ChainDB.prototype.getCoins = co(function* getCoins(hash) {
var state = this.state;
var coins; var coins;
if (this.options.spv) if (this.options.spv)
@ -663,7 +669,8 @@ ChainDB.prototype.getCoins = co(function* getCoins(hash) {
if (!coins) if (!coins)
return; return;
this.coinCache.set(hash, coins); if (state === this.state)
this.coinCache.set(hash, coins);
return Coins.fromRaw(coins, hash); return Coins.fromRaw(coins, hash);
}); });

View File

@ -164,7 +164,7 @@ config.parseData = function parseData(data, prefix, dirname) {
options.forceWitness = bool(data.forcewitness); options.forceWitness = bool(data.forcewitness);
options.prune = bool(data.prune); options.prune = bool(data.prune);
options.useCheckpoints = bool(data.usecheckpoints); options.useCheckpoints = bool(data.usecheckpoints);
options.coinCache = bool(data.coincache); options.coinCache = num(data.coincache);
options.indexTX = bool(data.indextx); options.indexTX = bool(data.indextx);
options.indexAddress = bool(data.indexaddress); options.indexAddress = bool(data.indexaddress);

View File

@ -2552,6 +2552,7 @@ TXDB.prototype.getCoin = co(function* getCoin(hash, index) {
*/ */
TXDB.prototype.getCredit = co(function* getCredit(hash, index) { TXDB.prototype.getCredit = co(function* getCredit(hash, index) {
var state = this.state;
var key = hash + index; var key = hash + index;
var data = this.coinCache.get(key); var data = this.coinCache.get(key);
var credit; var credit;
@ -2572,7 +2573,8 @@ TXDB.prototype.getCredit = co(function* getCredit(hash, index) {
credit.coin.hash = hash; credit.coin.hash = hash;
credit.coin.index = index; credit.coin.index = index;
this.coinCache.set(key, data); if (state === this.state)
this.coinCache.set(key, data);
return credit; return credit;
}); });