Limit mempool size based on chunk feerate

Rather than evicting the transactions with the lowest descendant feerate,
instead evict transactions that have the lowest chunk feerate.

Once mining is implemented based on choosing transactions with highest chunk
feerate (see next commit), mining and eviction will be opposites, so that we
will evict the transactions that would be mined last.
This commit is contained in:
Suhas Daftuar
2023-09-20 20:55:50 -04:00
parent b11c89cab2
commit 1ad4590f63
2 changed files with 32 additions and 13 deletions

View File

@@ -533,27 +533,37 @@ BOOST_AUTO_TEST_CASE(MempoolSizeLimitTest)
tx7.vout[1].nValue = 10 * COIN;
AddToMempool(pool, entry.Fee(700LL).FromTx(tx4));
auto usage_with_tx4_only = pool.DynamicMemoryUsage();
AddToMempool(pool, entry.Fee(100LL).FromTx(tx5));
AddToMempool(pool, entry.Fee(110LL).FromTx(tx6));
AddToMempool(pool, entry.Fee(900LL).FromTx(tx7));
// we only require this to remove, at max, 2 txn, because it's not clear what we're really optimizing for aside from that
// From the topology above, tx7 must be sorted last, so it should
// definitely evicted first if we must trim. tx4 should definitely remain
// in the mempool since it has a higher feerate than its descendants and
// should be in its own chunk.
pool.TrimToSize(pool.DynamicMemoryUsage() - 1);
BOOST_CHECK(pool.exists(tx4.GetHash()));
BOOST_CHECK(pool.exists(tx6.GetHash()));
BOOST_CHECK(!pool.exists(tx7.GetHash()));
// Tx5 and Tx6 may be removed as well because they're in the same chunk as
// tx7, but this behavior need not be guaranteed.
if (!pool.exists(tx5.GetHash()))
AddToMempool(pool, entry.Fee(100LL).FromTx(tx5));
if (!pool.exists(tx6.GetHash()))
AddToMempool(pool, entry.Fee(110LL).FromTx(tx6));
AddToMempool(pool, entry.Fee(900LL).FromTx(tx7));
pool.TrimToSize(pool.DynamicMemoryUsage() * 0.75); // should maximize mempool size by only removing 5/7
// If we trim sufficiently, everything but tx4 should be removed.
pool.TrimToSize(usage_with_tx4_only + 1);
BOOST_CHECK(pool.exists(tx4.GetHash()));
BOOST_CHECK(!pool.exists(tx5.GetHash()));
BOOST_CHECK(pool.exists(tx6.GetHash()));
BOOST_CHECK(!pool.exists(tx6.GetHash()));
BOOST_CHECK(!pool.exists(tx7.GetHash()));
AddToMempool(pool, entry.Fee(100LL).FromTx(tx5));
AddToMempool(pool, entry.Fee(110LL).FromTx(tx6));
AddToMempool(pool, entry.Fee(900LL).FromTx(tx7));
std::vector<CTransactionRef> vtx;