From 9976208d0d445c81fea5300dcc7a7890daf8dd7f Mon Sep 17 00:00:00 2001 From: Christopher Jeffrey Date: Sat, 2 Jan 2016 00:16:36 -0800 Subject: [PATCH] comments. misc. --- lib/bcoin/pool.js | 28 +++++++++++++++++++++++----- 1 file changed, 23 insertions(+), 5 deletions(-) diff --git a/lib/bcoin/pool.js b/lib/bcoin/pool.js index 53bb4826..e235978a 100644 --- a/lib/bcoin/pool.js +++ b/lib/bcoin/pool.js @@ -263,10 +263,11 @@ Pool.prototype._addLoader = function _addLoader() { if (!self.options.fullNode) { // Request each block - hashes.forEach(function(hash) { - self._request('filtered', hash); - }); + for (i = 0; i < hashes.length; i++) { + self._request('filtered', hashes[i]); + } + // Push our getblocks packet self._scheduleRequests(); // The part of the response is in chain, no need to escalate requests @@ -295,25 +296,42 @@ Pool.prototype._addLoader = function _addLoader() { for (i = 0; i < hashes.length; i++) { hash = hashes[i]; + + // Resolve orphan chain if (self.chain.hasOrphan(hash)) { - peer.loadBlocks(self.chain.locatorHashes(), self.chain.getOrphanRoot(hash)); + peer.loadBlocks( + self.chain.locatorHashes(), + self.chain.getOrphanRoot(hash) + ); continue; } + // Restart the entire getblocks process if (i === hashes.length - 1) peer.loadBlocks(self.chain.locatorHashes(), 0); + // Request block if we don't have it if (!self.chain.hasBlock(hash)) self._request('block', hash); } + self.emit('debug', + 'Requesting %s block packets from %s with getdata', + self.request.active, + peer.address + ); + + // Push our getblocks packet self._scheduleRequests(); + // Store last hash because we can self.block.lastHash = hashes[hashes.length - 1]; + // Reset interval to avoid calling getblocks unnecessarily clearInterval(interval); interval = setInterval(load, self.load.interval); + // Reset timeout to avoid killing the loader clearTimeout(timer); timer = setTimeout(destroy, self.load.timeout); }); @@ -417,7 +435,7 @@ Pool.prototype._load = function _load() { this.load.hiReached = false; - console.log( + this.emit('debug', 'Requesting inv packet from %s with getblocks', this.peers.load.address);