drop missing event. fix for spv chain. pool.search improvements.

This commit is contained in:
Christopher Jeffrey 2016-01-05 02:42:21 -08:00
parent 2f618c961b
commit 8a930ac037
2 changed files with 33 additions and 17 deletions

View File

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

View File

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