Skip to content

Commit ac463e8

Browse files
committed
[test util] mock mempool minimum feerate
1 parent 6b9fedd commit ac463e8

File tree

2 files changed

+39
-0
lines changed

2 files changed

+39
-0
lines changed

src/test/util/setup_common.cpp

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -432,6 +432,33 @@ std::vector<CTransactionRef> TestChain100Setup::PopulateMempool(FastRandomContex
432432
return mempool_transactions;
433433
}
434434

435+
void TestChain100Setup::MockMempoolMinFee(const CFeeRate& target_feerate)
436+
{
437+
LOCK2(cs_main, m_node.mempool->cs);
438+
// Transactions in the mempool will affect the new minimum feerate.
439+
assert(m_node.mempool->size() == 0);
440+
// The target feerate cannot be too low...
441+
// ...otherwise the transaction's feerate will need to be negative.
442+
assert(target_feerate > m_node.mempool->m_incremental_relay_feerate);
443+
// ...otherwise this is not meaningful. The feerate policy uses the maximum of both feerates.
444+
assert(target_feerate > m_node.mempool->m_min_relay_feerate);
445+
446+
// Manually create an invalid transaction. Manually set the fee in the CTxMemPoolEntry to
447+
// achieve the exact target feerate.
448+
CMutableTransaction mtx = CMutableTransaction();
449+
mtx.vin.push_back(CTxIn{COutPoint{g_insecure_rand_ctx.rand256(), 0}});
450+
mtx.vout.push_back(CTxOut(1 * COIN, GetScriptForDestination(WitnessV0ScriptHash(CScript() << OP_TRUE))));
451+
const auto tx{MakeTransactionRef(mtx)};
452+
LockPoints lp;
453+
// The new mempool min feerate is equal to the removed package's feerate + incremental feerate.
454+
const auto tx_fee = target_feerate.GetFee(GetVirtualTransactionSize(*tx)) -
455+
m_node.mempool->m_incremental_relay_feerate.GetFee(GetVirtualTransactionSize(*tx));
456+
m_node.mempool->addUnchecked(CTxMemPoolEntry(tx, /*fee=*/tx_fee,
457+
/*time=*/0, /*entry_height=*/1,
458+
/*spends_coinbase=*/true, /*sigops_cost=*/1, lp));
459+
m_node.mempool->TrimToSize(0);
460+
assert(m_node.mempool->GetMinFee() == target_feerate);
461+
}
435462
/**
436463
* @returns a real block (0000000000013b8ab2cd513b0261a14096412195a72a0c4827d229dcc7e0f7af)
437464
* with 9 txs.

src/test/util/setup_common.h

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323
#include <type_traits>
2424
#include <vector>
2525

26+
class CFeeRate;
2627
class Chainstate;
2728

2829
/** This is connected to the logger. Can be used to redirect logs to any other log */
@@ -185,6 +186,17 @@ struct TestChain100Setup : public TestingSetup {
185186
*/
186187
std::vector<CTransactionRef> PopulateMempool(FastRandomContext& det_rand, size_t num_transactions, bool submit);
187188

189+
/** Mock the mempool minimum feerate by adding a transaction and calling TrimToSize(0),
190+
* simulating the mempool "reaching capacity" and evicting by descendant feerate. Note that
191+
* this clears the mempool, and the new minimum feerate will depend on the maximum feerate of
192+
* transactions removed, so this must be called while the mempool is empty.
193+
*
194+
* @param target_feerate The new mempool minimum feerate after this function returns.
195+
* Must be above max(incremental feerate, min relay feerate),
196+
* or 1sat/vB with default settings.
197+
*/
198+
void MockMempoolMinFee(const CFeeRate& target_feerate);
199+
188200
std::vector<CTransactionRef> m_coinbase_txns; // For convenience, coinbase transactions
189201
CKey coinbaseKey; // private/public key needed to spend coinbase transactions
190202
};

0 commit comments

Comments
 (0)