From d605af7ed8930e6afda4afca90ec0d741d203970 Mon Sep 17 00:00:00 2001 From: Christopher Jeffrey Date: Sun, 22 May 2016 07:22:55 -0700 Subject: [PATCH] fix race condition. --- lib/bcoin/pool.js | 59 +++++++++++++++++++++++++++-------------------- 1 file changed, 34 insertions(+), 25 deletions(-) diff --git a/lib/bcoin/pool.js b/lib/bcoin/pool.js index 75ba5f80..26bebf47 100644 --- a/lib/bcoin/pool.js +++ b/lib/bcoin/pool.js @@ -1545,52 +1545,61 @@ Pool.prototype.getData = function getData(peer, type, hash, options, callback) { */ Pool.prototype.has = function has(type, hash, force, callback) { + var self = this; + if (!callback) { callback = force; force = false; } - if (force) { - callback = utils.asyncify(callback); + function check(err, exists) { + if (err) + return callback(err); + + if (exists) + return callback(null, true); + + // Check the pending requests. + if (self.request.map[hash]) + return callback(null, true); + + // We need to reset the rejects filter periodically. + // There may be a locktime in a TX that is now valid. + if (self.rejects.tip !== self.chain.tip.hash) { + self.rejects.tip = self.chain.tip.hash; + self.rejects.reset(); + } else { + // If we recently rejected this item. Ignore. + if (self.rejects.test(hash, 'hex')) { + bcoin.debug('Peer sent a known reject: %s.', hash); + return callback(null, true); + } + } + return callback(null, false); } - // Check the pending requests. - if (this.request.map[hash]) { - callback = utils.asyncify(callback); - return callback(null, true); - } - - // We need to reset the rejects filter periodically. - // There may be a locktime in a TX that is now valid. - if (this.rejects.tip !== this.chain.tip.hash) { - this.rejects.tip = this.chain.tip.hash; - this.rejects.reset(); - } else { - // If we recently rejected this item. Ignore. - if (this.rejects.test(hash, 'hex')) { - callback = utils.asyncify(callback); - bcoin.debug('Peer sent a known reject: %s.', hash); - return callback(null, true); - } + if (force) { + check = utils.asyncify(check); + return check(null, false); } if (type === this.tx.type) { // Check the TX filter if // we don't have a mempool. if (!this.mempool) { - callback = utils.asyncify(callback); + check = utils.asyncify(check); if (this.tx.filter.added(hash, 'hex')) - return callback(null, false); - return callback(null, true); + return check(null, false); + return check(null, true); } // Check the mempool. - return this.mempool.has(hash, callback); + return this.mempool.has(hash, check); } // Check the chain. - return this.chain.has(hash, callback); + return this.chain.has(hash, check); }; /**