From b55068f30d7fb2eea5ca64d080d9070d7a1b260b Mon Sep 17 00:00:00 2001 From: Christopher Jeffrey Date: Mon, 22 Aug 2016 21:46:34 -0700 Subject: [PATCH] chaindb: optimize. --- lib/bcoin/chaindb.js | 65 ++++++++++++++++++++++++-------------------- 1 file changed, 35 insertions(+), 30 deletions(-) diff --git a/lib/bcoin/chaindb.js b/lib/bcoin/chaindb.js index 5f51d13a..85cbc37e 100644 --- a/lib/bcoin/chaindb.js +++ b/lib/bcoin/chaindb.js @@ -994,7 +994,8 @@ ChainDB.prototype.removeBlock = function removeBlock(hash, callback) { ChainDB.prototype.connectBlock = function connectBlock(block, view, callback) { var undo = new BufferWriter(); - var i, j, tx, input, output, prev, hashes, address, hash, coins, raw; + var i, j, tx, input, output, prev; + var hashes, address, hash, coins, raw; if (this.options.spv) return utils.asyncify(callback)(null, block); @@ -1022,25 +1023,26 @@ ChainDB.prototype.connectBlock = function connectBlock(block, view, callback) { } } - for (j = 0; j < tx.inputs.length; j++) { - input = tx.inputs[j]; + if (!tx.isCoinbase()) { + for (j = 0; j < tx.inputs.length; j++) { + input = tx.inputs[j]; - if (tx.isCoinbase()) - break; + assert(input.coin); - assert(input.coin); - - if (this.options.indexAddress) { - address = input.getHash(); - if (address) { - prev = input.prevout; - this.del(layout.C(address, prev.hash, prev.index)); + if (this.options.indexAddress) { + address = input.getHash(); + if (address) { + prev = input.prevout; + this.del(layout.C(address, prev.hash, prev.index)); + } } + + // Add coin to set of undo + // coins for the block. + input.coin.toRaw(undo); + + this.pending.spend(input.coin); } - - input.coin.toRaw(undo); - - this.pending.spend(input.coin); } for (j = 0; j < tx.outputs.length; j++) { @@ -1059,6 +1061,7 @@ ChainDB.prototype.connectBlock = function connectBlock(block, view, callback) { } } + // Commit new coin state. view = view.toArray(); for (i = 0; i < view.length; i++) { @@ -1073,6 +1076,7 @@ ChainDB.prototype.connectBlock = function connectBlock(block, view, callback) { } } + // Write undo coins (if there are any). if (undo.written > 0) this.put(layout.u(block.hash()), undo.render()); @@ -1091,7 +1095,8 @@ ChainDB.prototype.connectBlock = function connectBlock(block, view, callback) { ChainDB.prototype.disconnectBlock = function disconnectBlock(block, callback) { var self = this; - var i, j, tx, input, output, prev, hashes, address, hash, coins, raw; + var i, j, tx, input, output, prev; + var hashes, address, hash, coins, raw; if (this.options.spv) return utils.asyncify(callback)(null, block); @@ -1117,23 +1122,22 @@ ChainDB.prototype.disconnectBlock = function disconnectBlock(block, callback) { } } - for (j = 0; j < tx.inputs.length; j++) { - input = tx.inputs[j]; + if (!tx.isCoinbase()) { + for (j = 0; j < tx.inputs.length; j++) { + input = tx.inputs[j]; - if (tx.isCoinbase()) - break; + assert(input.coin); - assert(input.coin); - - if (self.options.indexAddress) { - address = input.getHash(); - if (address) { - prev = input.prevout; - self.put(layout.C(address, prev.hash, prev.index), DUMMY); + if (self.options.indexAddress) { + address = input.getHash(); + if (address) { + prev = input.prevout; + self.put(layout.C(address, prev.hash, prev.index), DUMMY); + } } - } - self.pending.add(input.coin); + self.pending.add(input.coin); + } } // Add all of the coins we are about to @@ -1160,6 +1164,7 @@ ChainDB.prototype.disconnectBlock = function disconnectBlock(block, callback) { } } + // Commit new coin state. view = view.toArray(); for (i = 0; i < view.length; i++) {