diff --git a/lib/bcoin/mempool.js b/lib/bcoin/mempool.js index 9214f2ab..3e7fbb7f 100644 --- a/lib/bcoin/mempool.js +++ b/lib/bcoin/mempool.js @@ -366,7 +366,7 @@ Mempool.prototype.limitMempoolSize = function limitMempoolSize(callback) { if (this.getSize() <= this.maxSize) return callback(null, true); - this.tx.getRange({ + this.getRange({ start: 0, end: utils.now() - constants.mempool.MEMPOOL_EXPIRY }, function(err, txs) { @@ -405,13 +405,23 @@ Mempool.prototype.limitMempoolSize = function limitMempoolSize(callback) { * @param {Function} callback */ -Mempool.prototype.pruneOrphans = function pruneOrphans(callback) { +Mempool.prototype.limitOrphans = function limitOrphans(callback) { var self = this; - utils.forEachSerial(Object.keys(this.orphans), function(hash, next) { + var orphans = Object.keys(this.orphans); + var i, hash; + + (function next() { if (self.totalOrphans <= constants.mempool.MAX_ORPHAN_TX) return callback(); + + i = Math.random() * orphans.length | 0; + hash = orphans[i]; + orphans.splice(i, 1); + + bcoin.debug('Removing orphan %s from mempool.', utils.revHex(hash)); + self.removeOrphan(hash, next); - }, callback); + })(); }; /** @@ -422,8 +432,6 @@ Mempool.prototype.pruneOrphans = function pruneOrphans(callback) { */ Mempool.prototype.getTX = function getTX(hash, callback) { - if (hash instanceof bcoin.tx) - hash = hash.hash('hex'); return this.tx.getTX(hash, callback); }; @@ -506,6 +514,16 @@ Mempool.prototype.hasTX = function hasTX(hash, callback) { return this.tx.hasTX(hash, callback); }; +/** + * Find transactions within a range. + * @param {Object} range + * @param {Function} callback - Returns [Error, {@link TX}[]]. + */ + +Mempool.prototype.getRange = function getRange(options, callback) { + return this.tx.getRange(options, callback); +}; + /** * Test the mempool to see if it contains a transaction or an orphan. * @param {Hash} hash @@ -775,13 +793,10 @@ Mempool.prototype.getMinRate = function getMinRate() { if (now > this.lastFeeUpdate + 10) { halflife = constants.mempool.FEE_HALFLIFE; - if (this.size < this.maxSize / 4) { - halflife /= 4; - halflife |= 0; - } else if (this.size < this.maxSize / 2) { - halflife /= 2; - halflife |= 0; - } + if (this.size < this.maxSize / 4) + halflife >>>= 2; + else if (this.size < this.maxSize / 2) + halflife >>>= 1; this.minFeeRate /= Math.pow(2.0, (now - this.lastFeeUpdate) / halflife | 0); this.minFeeRate |= 0; @@ -1020,8 +1035,10 @@ Mempool.prototype.storeOrphan = function storeOrphan(tx, callback, force) { this.orphans[hash] = tx.toExtended(true); this.totalOrphans++; + bcoin.debug('Added orphan %s to mempool.', tx.rhash); + if (this.totalOrphans > constants.mempool.MAX_ORPHAN_TX) - return this.pruneOrphans(callback); + return this.limitOrphans(callback); return callback(); };