From 1c1c34e6a4fbfcace13ef0fc843f06fe8c750612 Mon Sep 17 00:00:00 2001 From: Christopher Jeffrey Date: Sat, 16 Apr 2016 20:33:58 -0700 Subject: [PATCH] refactor prune. --- lib/bcoin/chaindb.js | 37 +++++++++++++------------------------ 1 file changed, 13 insertions(+), 24 deletions(-) diff --git a/lib/bcoin/chaindb.js b/lib/bcoin/chaindb.js index 8917df78..5058f925 100644 --- a/lib/bcoin/chaindb.js +++ b/lib/bcoin/chaindb.js @@ -476,12 +476,7 @@ ChainDB.prototype.save = function save(entry, block, connect, callback) { this.saveBlock(block, batch, true, function(err) { if (err) return callback(err); - - self._pruneBlock(block, batch, function(err) { - if (err) - return callback(err); - return batch.write(callback); - }); + return batch.write(callback); }); }; @@ -790,8 +785,6 @@ ChainDB.prototype.connectBlock = function connectBlock(block, batch, callback) { } this._ensureBlock(block, function(err, block) { - var height; - if (err) return callback(err); @@ -849,7 +842,11 @@ ChainDB.prototype.connectBlock = function connectBlock(block, batch, callback) { self.emit('add block', block); - return callback(null, block); + self._pruneBlock(block, batch, function(err) { + if (err) + return callback(err); + return callback(null, block); + }); }); }; @@ -1540,8 +1537,7 @@ ChainDB.prototype.isSpentTX = function isSpentTX(hash, callback) { }; ChainDB.prototype._pruneBlock = function _pruneBlock(block, batch, callback) { - var futureHeight; - var i, j, tx, input; + var futureHeight, i, j, key, tx, input; if (this.options.spv) return callback(); @@ -1560,24 +1556,17 @@ ChainDB.prototype._pruneBlock = function _pruneBlock(block, batch, callback) { for (i = 0; i < block.txs.length; i++) { tx = block.txs[i]; - if (tx.isCoinbase()) - break; - for (j = 0; j < tx.inputs.length; j++) { input = tx.inputs[j]; + key = input.prevout.hash + '/' + input.prevout.index; + + if (tx.isCoinbase()) + break; assert(input.coin); - batch.put('u/x/' - + input.prevout.hash - + '/' + input.prevout.index, - input.coin.toRaw()); - - batch.put('u/q/' - + futureHeight - + '/' + input.prevout.hash - + '/' + input.prevout.index, - DUMMY); + batch.put('u/x/' + key, input.coin.toRaw()); + batch.put('u/q/' + futureHeight + '/' + key, DUMMY); } }