Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Improve re-insertion of TXs from disconnected blocks into mempool #917

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
39 changes: 22 additions & 17 deletions lib/mempool/mempool.js
Original file line number Diff line number Diff line change
Expand Up @@ -244,11 +244,6 @@ class Mempool extends EventEmitter {
*/

async _removeBlock(block, txs) {
if (this.map.size === 0) {
this.tip = block.prevBlock;
return;
}

let total = 0;

for (let i = 1; i < txs.length; i++) {
Expand Down Expand Up @@ -295,6 +290,7 @@ class Mempool extends EventEmitter {
const height = this.chain.height + 1;
const mtp = await this.chain.getMedianTime(this.chain.tip);
const remove = [];
const lockFlags = common.lockFlags.STANDARD_LOCKTIME_FLAGS;

for (const [hash, entry] of this.map) {
const {tx} = entry;
Expand All @@ -304,24 +300,32 @@ class Mempool extends EventEmitter {
continue;
}

if (tx.version > 1) {
let hasLocks = false;

for (const {sequence} of tx.inputs) {
if (!(sequence & consensus.SEQUENCE_DISABLE_FLAG)) {
hasLocks = true;
break;
}
}
// We get SpentView here instead of CoinView because for this
// check we are only concerned with sequence locks relative to
// the height of the input coins. Since this TX is already in
// the mempool, all its input coins are "spent" anyway.
const view = await this._getSpentView(tx);

if (hasLocks) {
// Verify Sequence locks.
if (tx.version > 1) {
if (!await this.verifyLocks(tx, view, lockFlags)) {
remove.push(hash);
continue;
}
}

if (entry.coinbase)
remove.push(hash);
// In this context "coinbase" means the TX spends from a coinbase output
// and so the maturity must be checked. Actual coinbase TXs are never
// allowed in the mempool and should not be inserted in the first place.
// This exact same check is performed in TX.checkInputs() along with
// several other tests we don't need here.
if (entry.coinbase) {
for (const {prevout} of tx.inputs) {
const inputEntry = view.getEntry(prevout);
if (height - inputEntry.height < consensus.COINBASE_MATURITY)
remove.push(hash);
}
}
}

for (const hash of remove) {
Expand Down Expand Up @@ -1111,6 +1115,7 @@ class Mempool extends EventEmitter {
*/

evictEntry(entry) {
this.logger.debug('Evicting entry %h', entry.tx.hash());
this.removeSpenders(entry);
this.updateAncestors(entry, removeFee);
this.removeEntry(entry);
Expand Down
Loading