chaindb: refactor scanning.

This commit is contained in:
Christopher Jeffrey 2016-08-27 12:37:42 -07:00
parent df66dc1277
commit 51f817a60f
No known key found for this signature in database
GPG Key ID: 8962AB9DE6666BBD
2 changed files with 30 additions and 33 deletions

View File

@ -66,7 +66,7 @@ function Chain(options) {
this.network = bcoin.network.get(options.network);
this.logger = options.logger || bcoin.defaultLogger;
this.db = new bcoin.chaindb(this, options);
this.db = new bcoin.chaindb(this);
this.total = 0;
this.currentBlock = null;
this.orphanLimit = options.orphanLimit || (20 << 20);

View File

@ -150,19 +150,16 @@ if (utils.isBrowser)
* @emits ChainDB#error
*/
function ChainDB(chain, options) {
function ChainDB(chain) {
if (!(this instanceof ChainDB))
return new ChainDB(chain, options);
if (!options)
options = {};
return new ChainDB(chain);
AsyncObject.call(this);
this.options = options;
this.chain = chain;
this.options = chain.options;
this.logger = chain.logger;
this.network = this.chain.network;
this.network = chain.network;
this.db = bcoin.ldb({
location: this.options.location,
@ -174,8 +171,8 @@ function ChainDB(chain, options) {
bufferKeys: !utils.isBrowser
});
this.keepBlocks = options.keepBlocks || 288;
this.prune = !!options.prune;
this.keepBlocks = this.options.keepBlocks || 288;
this.prune = !!this.options.prune;
this.state = new ChainState();
this.pending = null;
this.current = null;
@ -1255,7 +1252,10 @@ ChainDB.prototype.getCoins = function getCoins(hash, callback) {
ChainDB.prototype.scan = function scan(start, filter, iter, callback) {
var self = this;
var total = 0;
var i, j, hashes, address, tx, txs;
var i, j, hashes, hash, tx, txs;
if (this.options.spv)
return callback(new Error('Cannot scan in spv mode.'));
if (start == null)
start = this.network.genesis.hash;
@ -1268,34 +1268,31 @@ ChainDB.prototype.scan = function scan(start, filter, iter, callback) {
if (Array.isArray(filter))
filter = utils.toMap(filter);
(function next(err, hash) {
this.getEntry(start, function(err, entry) {
if (err)
return callback(err);
if (hash == null) {
self.logger.info('Finished scanning %d blocks.', total);
return callback();
}
total++;
self.getEntry(hash, function(err, entry) {
(function next(err, entry) {
if (err)
return next(err);
return callback(err);
if (!entry)
return next();
if (!entry) {
self.logger.info('Finished scanning %d blocks.', total);
return callback();
}
self.getFullBlock(hash, function(err, block) {
total++;
self.getFullBlock(entry.hash, function(err, block) {
if (err)
return next(err);
if (!block)
return next();
return next(new Error('Block not found.'));
self.logger.info('Scanning block %s (%d).',
utils.revHex(entry.hash),
block.height);
self.logger.info(
'Scanning block %s (%d).',
entry.rhash, block.height);
txs = [];
@ -1304,8 +1301,8 @@ ChainDB.prototype.scan = function scan(start, filter, iter, callback) {
hashes = tx.getHashes('hex');
for (j = 0; j < hashes.length; j++) {
address = hashes[j];
if (filter[address]) {
hash = hashes[j];
if (filter[hash]) {
txs.push(tx);
break;
}
@ -1315,11 +1312,11 @@ ChainDB.prototype.scan = function scan(start, filter, iter, callback) {
iter(entry, txs, function(err) {
if (err)
return next(err);
self.getNextHash(entry.hash, next);
entry.getNext(next);
});
});
});
})(null, start);
})(null, entry);
});
};
/**