[test util] mock mempool minimum feerate

This commit is contained in:
glozow 2023-01-17 11:00:49 +00:00
parent 6b9fedd221
commit ac463e87df
2 changed files with 39 additions and 0 deletions

View File

@ -432,6 +432,33 @@ std::vector<CTransactionRef> TestChain100Setup::PopulateMempool(FastRandomContex
return mempool_transactions;
}
void TestChain100Setup::MockMempoolMinFee(const CFeeRate& target_feerate)
{
LOCK2(cs_main, m_node.mempool->cs);
// Transactions in the mempool will affect the new minimum feerate.
assert(m_node.mempool->size() == 0);
// The target feerate cannot be too low...
// ...otherwise the transaction's feerate will need to be negative.
assert(target_feerate > m_node.mempool->m_incremental_relay_feerate);
// ...otherwise this is not meaningful. The feerate policy uses the maximum of both feerates.
assert(target_feerate > m_node.mempool->m_min_relay_feerate);
// Manually create an invalid transaction. Manually set the fee in the CTxMemPoolEntry to
// achieve the exact target feerate.
CMutableTransaction mtx = CMutableTransaction();
mtx.vin.push_back(CTxIn{COutPoint{g_insecure_rand_ctx.rand256(), 0}});
mtx.vout.push_back(CTxOut(1 * COIN, GetScriptForDestination(WitnessV0ScriptHash(CScript() << OP_TRUE))));
const auto tx{MakeTransactionRef(mtx)};
LockPoints lp;
// The new mempool min feerate is equal to the removed package's feerate + incremental feerate.
const auto tx_fee = target_feerate.GetFee(GetVirtualTransactionSize(*tx)) -
m_node.mempool->m_incremental_relay_feerate.GetFee(GetVirtualTransactionSize(*tx));
m_node.mempool->addUnchecked(CTxMemPoolEntry(tx, /*fee=*/tx_fee,
/*time=*/0, /*entry_height=*/1,
/*spends_coinbase=*/true, /*sigops_cost=*/1, lp));
m_node.mempool->TrimToSize(0);
assert(m_node.mempool->GetMinFee() == target_feerate);
}
/**
* @returns a real block (0000000000013b8ab2cd513b0261a14096412195a72a0c4827d229dcc7e0f7af)
* with 9 txs.

View File

@ -23,6 +23,7 @@
#include <type_traits>
#include <vector>
class CFeeRate;
class Chainstate;
/** This is connected to the logger. Can be used to redirect logs to any other log */
@ -185,6 +186,17 @@ struct TestChain100Setup : public TestingSetup {
*/
std::vector<CTransactionRef> PopulateMempool(FastRandomContext& det_rand, size_t num_transactions, bool submit);
/** Mock the mempool minimum feerate by adding a transaction and calling TrimToSize(0),
* simulating the mempool "reaching capacity" and evicting by descendant feerate. Note that
* this clears the mempool, and the new minimum feerate will depend on the maximum feerate of
* transactions removed, so this must be called while the mempool is empty.
*
* @param target_feerate The new mempool minimum feerate after this function returns.
* Must be above max(incremental feerate, min relay feerate),
* or 1sat/vB with default settings.
*/
void MockMempoolMinFee(const CFeeRate& target_feerate);
std::vector<CTransactionRef> m_coinbase_txns; // For convenience, coinbase transactions
CKey coinbaseKey; // private/public key needed to spend coinbase transactions
};