From e880dc5a5cbae2c707f903264fa56c43bcabad2e Mon Sep 17 00:00:00 2001 From: Christopher Jeffrey Date: Fri, 29 Jan 2016 14:51:36 -0800 Subject: [PATCH] pool._addTX to prevent duplicate requests. --- lib/bcoin/pool.js | 33 ++++++++++++++++++++++++--------- 1 file changed, 24 insertions(+), 9 deletions(-) diff --git a/lib/bcoin/pool.js b/lib/bcoin/pool.js index 11c6cec8..d10694f6 100644 --- a/lib/bcoin/pool.js +++ b/lib/bcoin/pool.js @@ -820,6 +820,8 @@ Pool.prototype._createPeer = function _createPeer(backoff, priority) { else utils.debug('Received unrequested TX: %s', tx.rhash); + self._addTX(tx); + self.emit('tx', tx, peer); if (!self.options.fullNode && tx.block) @@ -839,16 +841,9 @@ Pool.prototype._createPeer = function _createPeer(backoff, priority) { self.emit('txs', txs, peer); txs.forEach(function(hash) { hash = utils.toHex(hash); - if (self.tx.have[hash]) - return; - self.tx.have[hash] = true; - self.tx.count++; - self._request('tx', hash, peer); + if (self._addTX(hash)) + self._request('tx', hash, peer); }); - if (self.tx.count > 10000) { - self.tx.have = {}; - self.tx.count = 0; - } self._scheduleRequests(); }); @@ -941,6 +936,26 @@ Pool.prototype._addPeer = function _addPeer(backoff) { }); }; +Pool.prototype._addTX = function(hash) { + if (utils.isBuffer(hash)) + hash = utils.toHex(hash); + else if (hash.hash) + hash = hash.hash('hex'); + + if (this.tx.count >= 10000) { + this.tx.have = {}; + this.tx.count = 0; + } + + if (!this.tx.have[hash]) { + this.tx.have[hash] = true; + this.tx.count++; + return true; + } + + return false; +}; + Pool.prototype.bestPeer = function bestPeer() { return this.peers.block.reduce(function(best, peer) { if (!peer.version || !peer.socket)