diff --git a/lib/bcoin/chaindb.js b/lib/bcoin/chaindb.js index f34b74f7..0d3cd6d9 100644 --- a/lib/bcoin/chaindb.js +++ b/lib/bcoin/chaindb.js @@ -65,10 +65,9 @@ function ChainDB(chain, options) { else this._cacheWindow = network.block.majorityWindow + 1; - // this.cacheHash = new bcoin.lru(this._cacheWindow); - // this.cacheHeight = new bcoin.lru(this._cacheWindow); - this.cacheHash = new DumbCache(this._cacheWindow * 10); - this.cacheHeight = new DumbCache(this._cacheWindow * 10); + this.coinCache = new NullCache(100000); + this.cacheHash = new bcoin.lru(this._cacheWindow); + this.cacheHeight = new bcoin.lru(this._cacheWindow); this._init(); } @@ -777,7 +776,7 @@ ChainDB.prototype.removeBlock = function removeBlock(hash, batch, callback) { ChainDB.prototype.connectBlock = function connectBlock(block, batch, callback) { var self = this; - var i, j, tx, input, output, key, address, hash, uniq; + var i, j, tx, input, output, key, address, hash, uniq, coin; if (this.options.spv) { self.emit('add block', block); @@ -818,11 +817,13 @@ ChainDB.prototype.connectBlock = function connectBlock(block, batch, callback) { } batch.del('u/t/' + key); + self.coinCache.remove(key); } for (j = 0; j < tx.outputs.length; j++) { output = tx.outputs[j]; key = hash + '/' + j; + coin = bcoin.coin(tx, j); if (self.options.indexAddress) { address = output.getAddress(); @@ -836,7 +837,8 @@ ChainDB.prototype.connectBlock = function connectBlock(block, batch, callback) { batch.put('u/a/' + address + '/' + key, DUMMY); } - batch.put('u/t/' + key, bcoin.coin(tx, j).toRaw()); + batch.put('u/t/' + key, coin.toRaw()); + self.coinCache.set(key, coin); } } @@ -898,6 +900,7 @@ ChainDB.prototype.disconnectBlock = function disconnectBlock(block, batch, callb } batch.put('u/t/' + key, input.coin.toRaw()); + self.coinCache.set(key, input.coin); } for (j = 0; j < tx.outputs.length; j++) { @@ -917,6 +920,7 @@ ChainDB.prototype.disconnectBlock = function disconnectBlock(block, batch, callb } batch.del('u/t/' + key); + self.coinCache.remove(key); } } @@ -1113,10 +1117,15 @@ ChainDB.prototype.getCoinsByAddress = function getCoinsByAddress(addresses, call */ ChainDB.prototype.getCoin = function getCoin(hash, index, callback) { - var key = 'u/t/' + hash + '/' + index; + var self = this; + var key = hash + '/' + index; var coin; - this.db.get(key, function(err, data) { + coin = this.coinCache.get(key); + if (coin) + return utils.asyncify(callback)(null, coin); + + this.db.get('u/t/' + key, function(err, data) { if (err && err.type !== 'NotFoundError') return callback(err); @@ -1131,6 +1140,8 @@ ChainDB.prototype.getCoin = function getCoin(hash, index, callback) { return callback(e); } + self.coinCache.set(key, coin); + return callback(null, coin); }); };