From 41516b48665872a8b5a8023fe90822ab6503f206 Mon Sep 17 00:00:00 2001 From: Christopher Jeffrey Date: Fri, 29 Jan 2016 16:08:33 -0800 Subject: [PATCH] better system for avoiding duplicate txs. --- lib/bcoin/pool.js | 24 +++++++++++++----------- 1 file changed, 13 insertions(+), 11 deletions(-) diff --git a/lib/bcoin/pool.js b/lib/bcoin/pool.js index 224d1449..d8aea5af 100644 --- a/lib/bcoin/pool.js +++ b/lib/bcoin/pool.js @@ -134,7 +134,7 @@ function Pool(options) { }; this.tx = { - have: {}, + state: {}, count: 0 }; @@ -815,12 +815,12 @@ Pool.prototype._createPeer = function _createPeer(backoff, priority) { }); peer.on('tx', function(tx) { - var requested, added; + var state = self.tx.state[tx.hash('hex')]; - requested = self._response(tx); - added = self._addTX(tx); + self._response(tx); + self._addTX(tx, 1); - if (added || tx.block) + if (state !== 1 || tx.block) self.emit('tx', tx, peer); if (!self.options.fullNode && tx.block) @@ -840,7 +840,7 @@ Pool.prototype._createPeer = function _createPeer(backoff, priority) { self.emit('txs', txs, peer); txs.forEach(function(hash) { hash = utils.toHex(hash); - if (self._addTX(hash)) + if (self._addTX(hash, 0)) self._request('tx', hash, peer); }); self._scheduleRequests(); @@ -935,19 +935,21 @@ Pool.prototype._addPeer = function _addPeer(backoff) { }); }; -Pool.prototype._addTX = function(hash) { +Pool.prototype._addTX = function(hash, state) { if (utils.isBuffer(hash)) hash = utils.toHex(hash); else if (hash.hash) hash = hash.hash('hex'); - if (this.tx.count >= 10000) { - this.tx.have = {}; + if (this.tx.count >= 5000) { + this.tx.state = {}; this.tx.count = 0; } - if (!this.tx.have[hash]) { - this.tx.have[hash] = true; + if (this.tx.state[hash] == null) { + if (state == null) + state = 1; + this.tx.state[hash] = state; this.tx.count++; return true; }