Skip to content

Commit cea215c

Browse files
committed
Modify StageTrimToSize logic
Always be willing to try at least 5 different starting transactions so hitting a long chain you can't evict on your first try doesn't eliminate any chance of success.
1 parent 56b67ba commit cea215c

File tree

1 file changed

+10
-8
lines changed

1 file changed

+10
-8
lines changed

src/txmempool.cpp

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -455,7 +455,8 @@ bool CTxMemPool::StageTrimToSize(size_t sizelimit, const CTxMemPoolEntry& toadd,
455455
}
456456
size_t expsize = DynamicMemoryUsage() + GuessDynamicMemoryUsage(toadd); // Track the expected resulting memory usage of the mempool.
457457
indexed_transaction_set::nth_index<1>::type::reverse_iterator it = mapTx.get<1>().rbegin();
458-
int fails = 0; // Number of mempool transactions iterated over that were not included in the stage.
458+
int fails = 0; // Number of initial mempool transactions iterated over that were not included in the stage.
459+
int itertotal = 0; // Total number of transactions inspected so far
459460
// Iterate from lowest feerate to highest feerate in the mempool:
460461
while (expsize > sizelimit && it != mapTx.get<1>().rend()) {
461462
const uint256& hash = it->GetTx().GetHash();
@@ -479,7 +480,6 @@ bool CTxMemPool::StageTrimToSize(size_t sizelimit, const CTxMemPoolEntry& toadd,
479480
CAmount nowfee = 0; // Sum of the fees in 'now'.
480481
size_t nowsize = 0; // Sum of the tx sizes in 'now'.
481482
size_t nowusage = 0; // Sum of the memory usages of transactions in 'now'.
482-
int iternow = 0; // Transactions we've inspected so far while determining whether 'hash' is acceptable.
483483
todo.push_back(it->GetTx().GetHash()); // Add 'hash' to the todo list, to initiate processing its children.
484484
bool good = true; // Whether including 'hash' (and all its descendants) is a good idea.
485485
// Iterate breadth-first over all descendants of transaction with hash 'hash'.
@@ -490,9 +490,11 @@ bool CTxMemPool::StageTrimToSize(size_t sizelimit, const CTxMemPoolEntry& toadd,
490490
good = false;
491491
break;
492492
}
493-
iternow++; // We only count transactions we actually had to go find in the mempool.
494-
if (iternow + fails > 20) {
495-
return false;
493+
itertotal++; // We only count transactions we actually had to go find in the mempool.
494+
//Don't want to iterate more than 50 transactions, saving at least 5 to try on each fail attempt
495+
if (itertotal + 5*(4-fails) > 50) {
496+
good = false;
497+
break;
496498
}
497499
const CTxMemPoolEntry* origTx = &*mapTx.find(hashnow);
498500
nowfee += origTx->GetFee();
@@ -526,9 +528,9 @@ bool CTxMemPool::StageTrimToSize(size_t sizelimit, const CTxMemPoolEntry& toadd,
526528
nSizeRemoved += nowsize;
527529
expsize -= nowusage;
528530
} else {
529-
fails += iternow;
530-
if (fails > 10) {
531-
// Bail out after traversing 32 transactions that are not acceptable.
531+
fails++;
532+
if (fails >= 5) {
533+
// Bail out after trying to add 5 different failing transaction chains.
532534
return false;
533535
}
534536
}

0 commit comments

Comments
 (0)