diff --git a/lib/bcoin/blockdb.js b/lib/bcoin/blockdb.js index 2f659342..7b530160 100644 --- a/lib/bcoin/blockdb.js +++ b/lib/bcoin/blockdb.js @@ -198,27 +198,27 @@ BlockDB.prototype.connectBlock = function connectBlock(block, callback, batch) { var uniq = {}; tx.inputs.forEach(function(input) { - var type = input.getType(); - var address = input.getAddress(); + var address; if (input.isCoinbase()) return; assert(input.output); - if (type === 'pubkey' || type === 'multisig') - address = null; + if (self.options.indexAddress) { + address = input.getAddress(); - if (address && !uniq[address]) { - uniq[address] = true; - batch.put('t/a/' + address + '/' + hash, DUMMY); - } + if (address && !uniq[address]) { + uniq[address] = true; + batch.put('t/a/' + address + '/' + hash, DUMMY); + } - if (address) { - batch.del( - 'u/a/' + address - + '/' + input.prevout.hash - + '/' + input.prevout.index); + if (address) { + batch.del( + 'u/a/' + address + + '/' + input.prevout.hash + + '/' + input.prevout.index); + } } batch.del('u/t/' + input.prevout.hash + '/' + input.prevout.index); @@ -228,20 +228,20 @@ BlockDB.prototype.connectBlock = function connectBlock(block, callback, batch) { }); tx.outputs.forEach(function(output, i) { - var type = output.getType(); - var address = output.getAddress(); + var address; - if (type === 'pubkey' || type === 'multisig') - address = null; + if (self.options.indexAddress) { + address = output.getAddress(); - if (address && !uniq[address]) { - uniq[address] = true; - batch.put('t/a/' + address + '/' + hash, DUMMY); + if (address && !uniq[address]) { + uniq[address] = true; + batch.put('t/a/' + address + '/' + hash, DUMMY); + } + + if (address) + batch.put('u/a/' + address + '/' + hash + '/' + i, DUMMY); } - if (address) - batch.put('u/a/' + address + '/' + hash + '/' + i, DUMMY); - batch.put('u/t/' + hash + '/' + i, bcoin.coin(tx, i).toExtended()); }); }); @@ -284,28 +284,27 @@ BlockDB.prototype.disconnectBlock = function disconnectBlock(hash, callback, bat self.cache.tx.remove(hash); tx.inputs.forEach(function(input) { - var type = input.getType(); - var address = input.getAddress(); - var coin; + var coin, address; if (input.isCoinbase()) return; assert(input.output); - if (type === 'pubkey' || type === 'multisig') - address = null; + if (self.options.indexAddress) { + address = input.getAddress(); - if (address && !uniq[address]) { - uniq[address] = true; - batch.del('t/a/' + address + '/' + hash); - } + if (address && !uniq[address]) { + uniq[address] = true; + batch.del('t/a/' + address + '/' + hash); + } - if (address) { - batch.put('u/a/' + address - + '/' + input.prevout.hash - + '/' + input.prevout.index, - DUMMY); + if (address) { + batch.put('u/a/' + address + + '/' + input.prevout.hash + + '/' + input.prevout.index, + DUMMY); + } } batch.put('u/t/' @@ -315,20 +314,20 @@ BlockDB.prototype.disconnectBlock = function disconnectBlock(hash, callback, bat }); tx.outputs.forEach(function(output, i) { - var type = output.getType(); - var address = output.getAddress(); + var address; - if (type === 'pubkey' || type === 'multisig') - address = null; + if (self.options.indexAddress) { + address = output.getAddress(); - if (address && !uniq[address]) { - uniq[address] = true; - batch.del('t/a/' + address + '/' + hash); + if (address && !uniq[address]) { + uniq[address] = true; + batch.del('t/a/' + address + '/' + hash); + } + + if (address) + batch.del('u/a/' + address + '/' + hash + '/' + i); } - if (address) - batch.del('u/a/' + address + '/' + hash + '/' + i); - batch.del('u/t/' + hash + '/' + i); if (self.options.cache) diff --git a/lib/bcoin/lru.js b/lib/bcoin/lru.js index e6acae4b..0938b5ff 100644 --- a/lib/bcoin/lru.js +++ b/lib/bcoin/lru.js @@ -12,19 +12,33 @@ var assert = utils.assert; * LRU */ -function LRU(maxSize, getSize) { +function LRU(maxSize, getSize, onRemove) { if (!(this instanceof LRU)) return new LRU(maxSize, getSize); this.data = {}; this.size = 0; this.maxSize = maxSize; - this.getSize = getSize; + this.getSize = this._createGetSize(getSize); + this.onRemove = onRemove; this.head = null; this.tail = null; } +LRU.prototype._createGetSize = function _createGetSize(size) { + if (!size) + return; + + if (typeof size === 'number') + return function() { return size; }; + + if (typeof size === 'function') + return size; + + assert(false, 'Bad getSize callback.'); +}; + LRU.prototype._getSize = function _getSize(item) { var keySize = item.key.length * 2; @@ -57,6 +71,8 @@ LRU.prototype._compact = function _compact() { break; this.size -= this._getSize(item); delete this.data[item.key]; + if (this.onRemove) + this.onRemove(item.key, item.value); next = item.next; item.prev = null; item.next = null; @@ -77,6 +93,8 @@ LRU.prototype.reset = function reset() { for (item = this.head; item; item = next) { delete this.data[item.key]; + if (this.onRemove) + this.onRemove(item.key, item.value); next = item.next; item.prev = null; item.next = null; @@ -150,6 +168,8 @@ LRU.prototype.remove = function remove(key) { this.size -= this._getSize(item); delete this.data[key]; + if (this.onRemove) + this.onRemove(item.key, item.value); this._removeList(item);