From 8a930ac0374ca58c73ba06f4b20dd4727d168be4 Mon Sep 17 00:00:00 2001 From: Christopher Jeffrey Date: Tue, 5 Jan 2016 02:42:21 -0800 Subject: [PATCH] drop `missing` event. fix for spv chain. pool.search improvements. --- lib/bcoin/chain.js | 3 +++ lib/bcoin/pool.js | 47 +++++++++++++++++++++++++++++----------------- 2 files changed, 33 insertions(+), 17 deletions(-) diff --git a/lib/bcoin/chain.js b/lib/bcoin/chain.js index 614561c6..149d2feb 100644 --- a/lib/bcoin/chain.js +++ b/lib/bcoin/chain.js @@ -586,6 +586,9 @@ Chain.prototype.hashRange = function hashRange(start, end) { hashes = this.index.hashes.slice(start.index, end.index + 1); + if (!this.options.fullNode) + hashes = hashes.filter(Boolean); + return hashes; }; diff --git a/lib/bcoin/pool.js b/lib/bcoin/pool.js index 2aa3a3ec..1d693de3 100644 --- a/lib/bcoin/pool.js +++ b/lib/bcoin/pool.js @@ -165,14 +165,6 @@ Pool.prototype._init = function _init() { for (i = 0; i < this.size; i++) this._addPeer(0); - this.chain.on('missing', function(hash, preload, parent) { - if (!self.options.fullNode) { - self._request(self.block.type, hash, { force: true }); - self._scheduleRequests(); - // self._loadRange(preload); - } - }); - this.chain.on('block', function(block, peer) { self.emit('block', block, peer); }); @@ -572,7 +564,7 @@ Pool.prototype._loadRange = function _loadRange(hashes, force) { last = hashes[hashes.length - 1]; hashes.slice(0, -1).forEach(function(hash) { - this.peers.load.loadBlocks([hash], last); + this.peers.load.loadItems([hash], last); }, this); }; @@ -983,6 +975,11 @@ Pool.prototype.searchWallet = function(w) { utils.nextTick(function() { self.emit('debug', 'Wallet time: %s', new Date(ts * 1000)); + self.emit('debug', + 'Reverted chain to height=%d (%s)', + self.chain.height(), + new Date(self.chain.getTip().ts * 1000) + ); }); // this.search(ts); @@ -991,11 +988,16 @@ Pool.prototype.searchWallet = function(w) { Pool.prototype.search = function search(id, range, e) { var self = this; - var hashes, pending, listener, timeout, done, total; + var hashes, pending, listener, timeout, done, total, cb; if (this.options.fullNode) return; + if (typeof e === 'function') { + cb = e; + e = null; + } + e = e || new EventEmitter(); // Optional id argument @@ -1024,12 +1026,20 @@ Pool.prototype.search = function search(id, range, e) { if (!range.start) range.start = utils.now() - 432000; + if (cb) { + e.once('end', function(empty) { + if (empty) + return cb(new Error('Not found.'), false); + return cb(null, true); + }); + } + if (range.start < this.chain.index.lastTs) { if (id) this.watch(id); - done = function(res) { - e.emit('end', res); + done = function(empty) { + e.emit('end', empty); clearInterval(timeout); self.removeListener('block', listener); if (id) @@ -1068,6 +1078,13 @@ Pool.prototype.search = function search(id, range, e) { hashes = this.chain.hashRange(range.start, range.end); pending = hashes.length; + if (hashes.length === 0) { + bcoin.utils.nextTick(function() { + e.emit('end', true); + }); + return e; + } + if (id) this.watch(id); @@ -1086,11 +1103,7 @@ Pool.prototype.search = function search(id, range, e) { self._request('filtered', hash, { force: true }, done); }); - if (hashes.length === 0) { - bcoin.utils.nextTick(function() { - e.emit('end', true); - }); - } + this._scheduleRequests(); return e; };