chaindb: better rescan algo.

This commit is contained in:
Christopher Jeffrey 2016-11-02 20:37:31 -07:00
parent f5b7d7f2d9
commit 015eaa1f2d
No known key found for this signature in database
GPG Key ID: 8962AB9DE6666BBD

View File

@ -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;
}