From 015eaa1f2d08097f20dac926044bd4a8058cc582 Mon Sep 17 00:00:00 2001 From: Christopher Jeffrey Date: Wed, 2 Nov 2016 20:37:31 -0700 Subject: [PATCH] chaindb: better rescan algo. --- lib/chain/chaindb.js | 34 ++++++++++++++++++++++++++++------ 1 file changed, 28 insertions(+), 6 deletions(-) diff --git a/lib/chain/chaindb.js b/lib/chain/chaindb.js index f20dddd5..51519b1f 100644 --- a/lib/chain/chaindb.js +++ b/lib/chain/chaindb.js @@ -21,6 +21,7 @@ var ldb = require('../db/ldb'); var LRU = require('../utils/lru'); var Block = require('../primitives/block'); var Coin = require('../primitives/coin'); +var Outpoint = require('../primitives/outpoint'); var TX = require('../primitives/tx'); var Address = require('../primitives/address'); var ChainEntry = require('./chainentry'); @@ -1096,8 +1097,8 @@ ChainDB.prototype.getTXByAddress = co(function* getTXByAddress(addresses) { ChainDB.prototype.scan = co(function* scan(start, filter, iter) { var total = 0; - var i, j, entry, hashes; - var hash, tx, txs, block; + var i, j, entry, hash, tx, txs, block; + var found, input, output, prevout; if (start == null) start = this.network.genesis.hash; @@ -1116,7 +1117,7 @@ ChainDB.prototype.scan = co(function* scan(start, filter, iter) { throw new Error('Cannot rescan an alternate chain.'); while (entry) { - block = yield this.getFullBlock(entry.hash); + block = yield this.getBlock(entry.hash); txs = []; total++; @@ -1134,11 +1135,32 @@ ChainDB.prototype.scan = co(function* scan(start, filter, iter) { for (i = 0; i < block.txs.length; i++) { tx = block.txs[i]; - hashes = tx.getHashes(); + found = false; + + for (j = 0; j < tx.outputs.length; j++) { + output = tx.outputs[j]; + hash = output.getHash(); + + if (!hash) + continue; - for (j = 0; j < hashes.length; j++) { - hash = hashes[j]; if (filter.test(hash)) { + prevout = Outpoint.fromTX(tx, j); + filter.add(prevout.toRaw()); + found = true; + } + } + + if (found) { + txs.push(tx); + continue; + } + + for (j = 0; j < tx.inputs.length; j++) { + input = tx.inputs[j]; + prevout = input.prevout; + + if (filter.test(prevout.toRaw())) { txs.push(tx); break; }