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

@@ -1167,29 +1167,38 @@ void CTxMemPool::TrimToSize(size_t sizelimit, std::vector<COutPoint>* pvNoSpends
unsigned nTxnRemoved = 0;
CFeeRate maxFeeRateRemoved(0);
while (!mapTx.empty() && DynamicMemoryUsage() > sizelimit) {
indexed_transaction_set::index<descendant_score>::type::iterator it = mapTx.get<descendant_score>().begin();
const auto &[worst_chunk, feeperweight] = m_txgraph->GetWorstMainChunk();
FeePerVSize feerate = ToFeePerVSize(feeperweight);
CFeeRate removed{feerate.fee, feerate.size};
// We set the new mempool min fee to the feerate of the removed set, plus the
// "minimum reasonable fee rate" (ie some value under which we consider txn
// to have 0 fee). This way, we don't allow txn to enter mempool with feerate
// equal to txn which were removed with no block in between.
CFeeRate removed(it->GetModFeesWithDescendants(), it->GetSizeWithDescendants());
removed += m_opts.incremental_relay_feerate;
trackPackageRemoved(removed);
maxFeeRateRemoved = std::max(maxFeeRateRemoved, removed);
setEntries stage;
CalculateDescendants(mapTx.project<0>(it), stage);
nTxnRemoved += stage.size();
nTxnRemoved += worst_chunk.size();
std::vector<CTransaction> txn;
if (pvNoSpendsRemaining) {
txn.reserve(stage.size());
for (txiter iter : stage)
txn.push_back(iter->GetTx());
txn.reserve(worst_chunk.size());
for (auto ref : worst_chunk) {
txn.emplace_back(static_cast<const CTxMemPoolEntry&>(*ref).GetTx());
}
}
setEntries stage;
for (auto ref : worst_chunk) {
stage.insert(mapTx.iterator_to(static_cast<const CTxMemPoolEntry&>(*ref)));
}
UpdateForRemoveFromMempool(stage, false);
for (auto e : stage) {
removeUnchecked(e, MemPoolRemovalReason::SIZELIMIT);
}
RemoveStaged(stage, false, MemPoolRemovalReason::SIZELIMIT);
if (pvNoSpendsRemaining) {
for (const CTransaction& tx : txn) {
for (const CTxIn& txin : tx.vin) {