From 51f817a60f02af649da468de5cde047b3199c519 Mon Sep 17 00:00:00 2001 From: Christopher Jeffrey Date: Sat, 27 Aug 2016 12:37:42 -0700 Subject: [PATCH] chaindb: refactor scanning. --- lib/chain/chain.js | 2 +- lib/chain/chaindb.js | 61 +++++++++++++++++++++----------------------- 2 files changed, 30 insertions(+), 33 deletions(-) diff --git a/lib/chain/chain.js b/lib/chain/chain.js index b5cf09c6..37e51fcd 100644 --- a/lib/chain/chain.js +++ b/lib/chain/chain.js @@ -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); diff --git a/lib/chain/chaindb.js b/lib/chain/chaindb.js index 05e06bcc..444a50c3 100644 --- a/lib/chain/chaindb.js +++ b/lib/chain/chaindb.js @@ -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); + }); }; /**