mempool: move some methods.

This commit is contained in:
Christopher Jeffrey 2016-12-18 15:50:00 -08:00
parent 53e30fcc08
commit 37770aa6df
No known key found for this signature in database
GPG Key ID: 8962AB9DE6666BBD

View File

@ -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.