From 37770aa6df3be727b81a48bccc2a7ba4cea12cbb Mon Sep 17 00:00:00 2001 From: Christopher Jeffrey Date: Sun, 18 Dec 2016 15:50:00 -0800 Subject: [PATCH] mempool: move some methods. --- lib/mempool/mempool.js | 169 +++++++++++++++++++++-------------------- 1 file changed, 85 insertions(+), 84 deletions(-) diff --git a/lib/mempool/mempool.js b/lib/mempool/mempool.js index d9fc5be4..e7129282 100644 --- a/lib/mempool/mempool.js +++ b/lib/mempool/mempool.js @@ -774,10 +774,11 @@ Mempool.prototype._addTX = co(function* _addTX(tx) { if (missing) return this.storeOrphan(tx, missing); - // Create a mempool entry and - // begin contextual verification. + // Create a new mempool entry + // at current chain height. entry = MempoolEntry.fromTX(tx, view, this.chain.height); + // Contextual verification. yield this.verify(entry, view); // Add and index the entry. @@ -792,88 +793,6 @@ Mempool.prototype._addTX = co(function* _addTX(tx) { } }); -/** - * Add a transaction to the mempool without performing any - * validation. Note that this method does not lock the mempool - * and may lend itself to race conditions if used unwisely. - * This function will also resolve orphans if possible (the - * resolved orphans _will_ be validated). - * @param {MempoolEntry} entry - * @param {CoinView} view - * @returns {Promise} - */ - -Mempool.prototype.addEntry = co(function* addEntry(entry, view) { - var i, resolved, tx; - - this.trackEntry(entry, view); - - this.emit('tx', entry.tx, view); - this.emit('add entry', entry); - - if (this.fees) - this.fees.processTX(entry, this.chain.isFull()); - - this.logger.debug('Added tx %s to mempool.', entry.tx.txid()); - - resolved = this.resolveOrphans(entry.tx); - - for (i = 0; i < resolved.length; i++) { - tx = resolved[i]; - - try { - yield this._addTX(tx); - } catch (err) { - if (err.type === 'VerifyError') { - this.logger.debug( - 'Could not resolve orphan %s: %s.', - tx.txid(), err.message); - - if (!tx.hasWitness() && !err.malleated) - this.rejects.add(tx.hash()); - - continue; - } - this.emit('error', err); - continue; - } - - this.logger.debug('Resolved orphan %s in mempool.', tx.txid()); - } -}); - -/** - * Remove a transaction from the mempool. Generally - * only called when a new block is added to the main chain. - * @param {MempoolEntry} entry - * @param {Boolean} limit - */ - -Mempool.prototype.removeEntry = function removeEntry(entry, limit) { - var tx = entry.tx; - var hash = tx.hash('hex'); - - this.removeOrphan(hash); - - // We do not remove spenders if this is - // being removed for a block. The spenders - // are still spending valid coins (which - // now exist on the blockchain). - if (limit) { - this.removeSpenders(entry); - this.logger.debug('Evicting %s from the mempool.', tx.txid()); - } else { - this.logger.spam('Removing block tx %s from mempool.', tx.txid()); - } - - this.untrackEntry(entry); - - if (this.fees) - this.fees.removeTX(hash); - - this.emit('remove entry', entry); -}; - /** * Verify a transaction with mempool standards. * @param {TX} tx @@ -1067,6 +986,88 @@ Mempool.prototype.verifyInputs = co(function* verifyInputs(tx, view, flags) { 100); }); +/** + * Add a transaction to the mempool without performing any + * validation. Note that this method does not lock the mempool + * and may lend itself to race conditions if used unwisely. + * This function will also resolve orphans if possible (the + * resolved orphans _will_ be validated). + * @param {MempoolEntry} entry + * @param {CoinView} view + * @returns {Promise} + */ + +Mempool.prototype.addEntry = co(function* addEntry(entry, view) { + var i, resolved, tx; + + this.trackEntry(entry, view); + + this.emit('tx', entry.tx, view); + this.emit('add entry', entry); + + if (this.fees) + this.fees.processTX(entry, this.chain.isFull()); + + this.logger.debug('Added tx %s to mempool.', entry.tx.txid()); + + resolved = this.resolveOrphans(entry.tx); + + for (i = 0; i < resolved.length; i++) { + tx = resolved[i]; + + try { + yield this._addTX(tx); + } catch (err) { + if (err.type === 'VerifyError') { + this.logger.debug( + 'Could not resolve orphan %s: %s.', + tx.txid(), err.message); + + if (!tx.hasWitness() && !err.malleated) + this.rejects.add(tx.hash()); + + continue; + } + this.emit('error', err); + continue; + } + + this.logger.debug('Resolved orphan %s in mempool.', tx.txid()); + } +}); + +/** + * Remove a transaction from the mempool. Generally + * only called when a new block is added to the main chain. + * @param {MempoolEntry} entry + * @param {Boolean} limit + */ + +Mempool.prototype.removeEntry = function removeEntry(entry, limit) { + var tx = entry.tx; + var hash = tx.hash('hex'); + + this.removeOrphan(hash); + + // We do not remove spenders if this is + // being removed for a block. The spenders + // are still spending valid coins (which + // now exist on the blockchain). + if (limit) { + this.removeSpenders(entry); + this.logger.debug('Evicting %s from the mempool.', tx.txid()); + } else { + this.logger.spam('Removing block tx %s from mempool.', tx.txid()); + } + + this.untrackEntry(entry); + + if (this.fees) + this.fees.removeTX(hash); + + this.emit('remove entry', entry); +}; + /** * Count the highest number of * ancestors a transaction may have.