From 136a48b4b4801cff4d8661d8b2e67286f3c2583b Mon Sep 17 00:00:00 2001 From: Christopher Jeffrey Date: Tue, 6 Dec 2016 17:12:54 -0800 Subject: [PATCH] pool: refactor requests. --- lib/net/pool.js | 45 +++++++++++++++++++++++---------------------- 1 file changed, 23 insertions(+), 22 deletions(-) diff --git a/lib/net/pool.js b/lib/net/pool.js index 2d73262f..39f7fd61 100644 --- a/lib/net/pool.js +++ b/lib/net/pool.js @@ -793,7 +793,8 @@ Pool.prototype.__handleHeaders = co(function* _handleHeaders(headers, peer) { last = hash; - yield this.getBlock(peer, hash); + if (!(yield this.chain.has(hash))) + this.getBlock(peer, hash); } // Schedule the getdata's we just added. @@ -853,7 +854,11 @@ Pool.prototype._handleBlocks = co(function* _handleBlocks(hashes, peer) { continue; } - exists = yield this.getBlock(peer, hash); + // Request the block if we don't have it. + if (!(yield this.chain.has(hash))) { + this.getBlock(peer, hash); + continue; + } // Normally we request the hashContinue. // In the odd case where we already have @@ -862,15 +867,9 @@ Pool.prototype._handleBlocks = co(function* _handleBlocks(hashes, peer) { // continue the sync, or do a getblocks // from the last hash (this will reset // the hashContinue on the remote node). - if (exists && i === hashes.length - 1) { - // Make sure we _actually_ have this block. - if (!this.requestMap[hash]) { - this.logger.debug('Received existing hash (%s).', peer.hostname); - yield peer.getBlocks(hash, null); - continue; - } - // Otherwise, we're still requesting it. Ignore. - this.logger.debug('Received requested hash (%s).', peer.hostname); + if (i === hashes.length - 1) { + this.logger.debug('Received existing hash (%s).', peer.hostname); + yield peer.getBlocks(hash, null); } } @@ -1223,7 +1222,7 @@ Pool.prototype.createPeer = function createPeer(addr, socket) { for (i = 0; i < txs.length; i++) { hash = txs[i]; try { - yield self.getTX(peer, hash); + self.getTX(peer, hash); } catch (e) { self.emit('error', e); } @@ -1389,8 +1388,12 @@ Pool.prototype._handleTX = co(function* _handleTX(tx, peer) { } if (this.options.requestMissing && missing) { - for (i = 0; i < missing.length; i++) - yield this.getTX(peer, missing[i]); + try { + for (i = 0; i < missing.length; i++) + this.getTX(peer, missing[i]); + } catch (e) { + this.emit('error', e); + } } this.emit('tx', tx, peer); @@ -1572,7 +1575,7 @@ Pool.prototype.watchAddress = function watchAddress(address) { * @returns {Promise} */ -Pool.prototype.getBlock = co(function* getBlock(peer, hash) { +Pool.prototype.getBlock = function getBlock(peer, hash) { var item; if (!this.loaded) @@ -1581,15 +1584,13 @@ Pool.prototype.getBlock = co(function* getBlock(peer, hash) { if (peer.destroyed) throw new Error('Peer is already destroyed (getdata).'); - if (yield this.hasBlock(hash)) - return true; + if (this.requestMap[hash]) + return; item = new LoadRequest(this, peer, this.blockType, hash); peer.queueBlock.push(item); - - return false; -}); +}; /** * Test whether the chain has or has seen an item. @@ -1619,7 +1620,7 @@ Pool.prototype.hasBlock = co(function* hasBlock(hash) { * @returns {Promise} */ -Pool.prototype.getTX = co(function* getTX(peer, hash) { +Pool.prototype.getTX = function getTX(peer, hash) { var self = this; var item; @@ -1650,7 +1651,7 @@ Pool.prototype.getTX = co(function* getTX(peer, hash) { peer.queueTX.push(item.start()); return false; -}); +}; /** * Test whether the mempool has or has seen an item.