chaindb: refactor scanning.

This commit is contained in:
Christopher Jeffrey 2016-08-14 19:21:35 -07:00
parent 7259ffa91e
commit e7329b2d1a
No known key found for this signature in database
GPG Key ID: 8962AB9DE6666BBD
3 changed files with 33 additions and 35 deletions

View File

@ -1121,20 +1121,19 @@ ChainDB.prototype.getCoins = function getCoins(hash, callback) {
* @param {Function} callback
*/
ChainDB.prototype.scan = function scan(start, hashes, iter, callback) {
ChainDB.prototype.scan = function scan(start, filter, iter, callback) {
var self = this;
var hashMap = {};
var total = 0;
var i;
var i, j, hashes, address, tx, txs;
if (!start)
start = this.network.genesis.hash;
for (i = 0; i < hashes.length; i++)
hashMap[hashes[i]] = true;
this.logger.info('Scanning from block %s.', utils.revHex(start));
if (Array.isArray(filter))
filter = utils.toMap(filter);
(function next(err, hash) {
if (err)
return callback(err);
@ -1164,18 +1163,25 @@ ChainDB.prototype.scan = function scan(start, hashes, iter, callback) {
utils.revHex(hash),
block.height);
utils.forEachSerial(block.txs, function(tx, next) {
var hashes = tx.getHashes('hex');
var i, hash;
txs = [];
for (i = 0; i < hashes.length; i++) {
hash = hashes[i];
if (hashMap[hash])
return iter(tx, entry, next);
for (i = 0; i < block.txs.length; i++) {
tx = block.txs[i];
hashes = tx.getHashes('hex');
for (j = 0; j < hashes.length; j++) {
address = hashes[j];
if (filter[address]) {
txs.push(tx);
break;
}
}
}
next();
}, function(err) {
if (txs.length === 0)
return self.getNextHash(hash, next);
iter(txs, entry, function(err) {
if (err)
return next(err);
self.getNextHash(hash, next);

View File

@ -1349,12 +1349,15 @@ ClientSocket.prototype.testFilter = function testFilter(tx) {
ClientSocket.prototype.scan = function scan(start, callback) {
var self = this;
var hashes = Object.keys(this.filter);
var i;
start = utils.revHex(start);
this.chain.db.scan(start, hashes, function(tx, entry, next) {
self.emit('block tx', [tx.toJSON()], entry.toJSON());
this.chain.db.scan(start, this.filter, function(txs, entry, next) {
for (i = 0; i < txs.length; i++)
txs[i] = txs[i].toJSON();
self.emit('block tx', txs, entry.toJSON());
next();
}, function(err) {
if (err)

View File

@ -921,27 +921,16 @@ WalletDB.prototype.getWallets = function getWallets(callback) {
WalletDB.prototype.rescan = function rescan(chaindb, callback) {
var self = this;
this.getTip(function(err, hash, height) {
this.getAddresses(function(err, hashes) {
if (err)
return callback(err);
if (!hash)
return callback(new Error('Best hash not found.'));
self.logger.info('Scanning for %d addresses.', hashes.length);
self.getAddresses(function(err, hashes) {
if (err)
return callback(err);
self.logger.info('Scanning for %d addresses.', hashes.length);
chaindb.scan(hash, hashes, function(tx, block, next) {
self.addTX(tx, function(err) {
if (err)
return next(err);
self.setTip(block.hash, block.height, next);
});
}, callback);
});
chaindb.scan(self.tip, hashes, function(txs, block, next) {
self.addBlock(block, txs, next);
}, callback);
});
};