aboutsummaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorglozow <gloriajzhao@gmail.com>2023-01-17 11:00:49 +0000
committerglozow <gloriajzhao@gmail.com>2023-04-17 09:52:25 +0100
commitac463e87df728689701810c3961155c49fdc5b31 (patch)
treec19a3b7f7d97a31b222e6af277df13c3e6527973 /src
parent6b9fedd2211851a70d2d82dea04420e9b87bfff9 (diff)
[test util] mock mempool minimum feerate
Diffstat (limited to 'src')
-rw-r--r--src/test/util/setup_common.cpp27
-rw-r--r--src/test/util/setup_common.h12
2 files changed, 39 insertions, 0 deletions
diff --git a/src/test/util/setup_common.cpp b/src/test/util/setup_common.cpp
index 58593c9d5b..4992dbd956 100644
--- a/src/test/util/setup_common.cpp
+++ b/src/test/util/setup_common.cpp
@@ -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.
diff --git a/src/test/util/setup_common.h b/src/test/util/setup_common.h
index 8874db7e75..10a6b19d03 100644
--- a/src/test/util/setup_common.h
+++ b/src/test/util/setup_common.h
@@ -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
};