From c5a5d4498a1101b2ef1c0c1e573556c6a7b61ab6 Mon Sep 17 00:00:00 2001 From: Christopher Jeffrey Date: Sat, 26 Nov 2016 01:12:38 -0800 Subject: [PATCH] chain/wallet: handle caches better. --- lib/blockchain/chaindb.js | 15 +++++++++++---- lib/node/config.js | 2 +- lib/wallet/txdb.js | 4 +++- 3 files changed, 15 insertions(+), 6 deletions(-) diff --git a/lib/blockchain/chaindb.js b/lib/blockchain/chaindb.js index 4385f17b..15a37299 100644 --- a/lib/blockchain/chaindb.js +++ b/lib/blockchain/chaindb.js @@ -83,14 +83,17 @@ function ChainDB(chain) { this.cacheWindow = (this.network.pow.retargetInterval + 1) * 2 + 100; // 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.cacheHash = 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); + } } util.inherits(ChainDB, AsyncObject); @@ -621,6 +624,7 @@ ChainDB.prototype.getTips = function getTips() { */ ChainDB.prototype.getCoin = co(function* getCoin(hash, index) { + var state = this.state; var coins; if (this.options.spv) @@ -636,7 +640,8 @@ ChainDB.prototype.getCoin = co(function* getCoin(hash, index) { if (!coins) return; - this.coinCache.set(hash, coins); + if (state === this.state) + this.coinCache.set(hash, coins); 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) { + var state = this.state; var coins; if (this.options.spv) @@ -663,7 +669,8 @@ ChainDB.prototype.getCoins = co(function* getCoins(hash) { if (!coins) return; - this.coinCache.set(hash, coins); + if (state === this.state) + this.coinCache.set(hash, coins); return Coins.fromRaw(coins, hash); }); diff --git a/lib/node/config.js b/lib/node/config.js index a5ee781c..cc3f152f 100644 --- a/lib/node/config.js +++ b/lib/node/config.js @@ -164,7 +164,7 @@ config.parseData = function parseData(data, prefix, dirname) { options.forceWitness = bool(data.forcewitness); options.prune = bool(data.prune); options.useCheckpoints = bool(data.usecheckpoints); - options.coinCache = bool(data.coincache); + options.coinCache = num(data.coincache); options.indexTX = bool(data.indextx); options.indexAddress = bool(data.indexaddress); diff --git a/lib/wallet/txdb.js b/lib/wallet/txdb.js index 5e2e2920..7042392e 100644 --- a/lib/wallet/txdb.js +++ b/lib/wallet/txdb.js @@ -2552,6 +2552,7 @@ TXDB.prototype.getCoin = co(function* getCoin(hash, index) { */ TXDB.prototype.getCredit = co(function* getCredit(hash, index) { + var state = this.state; var key = hash + index; var data = this.coinCache.get(key); var credit; @@ -2572,7 +2573,8 @@ TXDB.prototype.getCredit = co(function* getCredit(hash, index) { credit.coin.hash = hash; credit.coin.index = index; - this.coinCache.set(key, data); + if (state === this.state) + this.coinCache.set(key, data); return credit; });