From 5fb64e7bac6b999f46adae05dbac84d7dbd54d0c Mon Sep 17 00:00:00 2001 From: Christopher Jeffrey Date: Thu, 5 Jan 2017 04:47:33 -0800 Subject: [PATCH] pool: refactor reject handling. --- lib/net/pool.js | 52 +++++++++++++++++-------------------------------- 1 file changed, 18 insertions(+), 34 deletions(-) diff --git a/lib/net/pool.js b/lib/net/pool.js index 72bd01f8..6bb4a13c 100644 --- a/lib/net/pool.js +++ b/lib/net/pool.js @@ -1022,15 +1022,22 @@ Pool.prototype.handleTX = co(function* handleTX(peer, tx) { var missing; if (!requested) { - peer.invFilter.add(tx.hash()); - - if (!this.mempool) - this.txFilter.add(tx.hash()); - this.logger.warning('Peer sent unrequested tx: %s (%s).', tx.txid(), peer.hostname); - if (this.hasReject(tx.hash())) { + peer.invFilter.add(tx.hash()); + } + + if (!this.mempool) { + if (!requested) + this.txFilter.add(tx.hash()); + this.emit('tx', tx, peer); + this.scheduleRequests(peer); + return; + } + + if (!requested) { + if (this.mempool.hasReject(tx.hash())) { throw new VerifyError(tx, 'alreadyknown', 'txn-already-in-mempool', @@ -1038,12 +1045,6 @@ Pool.prototype.handleTX = co(function* handleTX(peer, tx) { } } - if (!this.mempool) { - this.emit('tx', tx, peer); - this.scheduleRequests(peer); - return; - } - try { missing = yield this.mempool.addTX(tx); } catch (err) { @@ -1356,19 +1357,6 @@ Pool.prototype.handleAlert = function handleAlert(peer, alert) { this.emit('alert', alert, peer); }; -/** - * Test the mempool to see if it - * contains a recent reject. - * @param {Hash} hash - * @returns {Boolean} - */ - -Pool.prototype.hasReject = function hasReject(hash) { - if (!this.mempool) - return false; - return this.mempool.hasReject(hash); -}; - /** * Create an inbound peer from an existing socket. * @private @@ -1650,8 +1638,6 @@ Pool.prototype.getBlock = function getBlock(peer, hash) { /** * Test whether the chain has or has seen an item. - * @param {Peer} peer - * @param {InvType} type * @param {Hash} hash * @returns {Promise} - Returns Boolean. */ @@ -1710,8 +1696,6 @@ Pool.prototype.getTX = function getTX(peer, hashes) { /** * Test whether the mempool has or has seen an item. - * @param {Peer} peer - * @param {InvType} type * @param {Hash} hash * @returns {Promise} - Returns Boolean. */ @@ -1726,12 +1710,12 @@ Pool.prototype.hasTX = function hasTX(hash) { // Check the mempool. if (this.mempool.has(hash)) return true; - } - // If we recently rejected this item. Ignore. - if (this.hasReject(hash)) { - this.logger.spam('Saw known reject of %s.', util.revHex(hash)); - return true; + // If we recently rejected this item. Ignore. + if (this.mempool.hasReject(hash)) { + this.logger.spam('Saw known reject of %s.', util.revHex(hash)); + return true; + } } // Check the pending requests.