Branch data Line data Source code
1 : : // Copyright (c) The Bitcoin Core developers
2 : : // Distributed under the MIT software license. See the accompanying
3 : : // file COPYING or http://www.opensource.org/licenses/mit-license.php.
4 : :
5 : : #include <kernel/mempool_entry.h>
6 : : #include <policy/fees/estimator_args.h>
7 : : #include <policy/fees/mempool_estimator.h>
8 : : #include <policy/policy.h>
9 : : #include <primitives/block.h>
10 : : #include <random.h>
11 : : #include <test/util/common.h>
12 : : #include <test/util/setup_common.h>
13 : : #include <test/util/txmempool.h>
14 : : #include <txmempool.h>
15 : : #include <uint256.h>
16 : : #include <util/feefrac.h>
17 : : #include <util/fees.h>
18 : : #include <util/time.h>
19 : : #include <validation.h>
20 : :
21 : : #include <boost/test/unit_test.hpp>
22 : :
23 : : #include <string>
24 : :
25 : : BOOST_FIXTURE_TEST_SUITE(mempool_fee_estimator_tests, TestingSetup)
26 : :
27 : 186335 : static inline CTransactionRef MakeRandomTx()
28 : : {
29 : 186335 : auto rng = FastRandomContext();
30 [ + - ]: 186335 : auto tx = CMutableTransaction();
31 [ + - ]: 186335 : tx.vin.resize(1);
32 [ + - ]: 186335 : tx.vout.resize(1);
33 [ + - ]: 186335 : tx.vin[0].prevout.hash = Txid::FromUint256(rng.rand256());
34 : 186335 : tx.vin[0].prevout.n = 0;
35 [ + - ]: 186335 : tx.vin[0].scriptSig << OP_TRUE;
36 [ + - ]: 186335 : tx.vout[0].scriptPubKey = CScript() << OP_TRUE;
37 [ + - ]: 186335 : tx.vout[0].nValue = COIN;
38 [ + - ]: 372670 : return MakeTransactionRef(tx);
39 : 186335 : }
40 : :
41 : 28 : void AddRemovedBlock(MemPoolFeeRateEstimator& fee_est,
42 : : int32_t removed_txs_weight,
43 : : int32_t block_txs_weight,
44 : : unsigned int& height)
45 : : {
46 : 28 : auto block = std::make_shared<CBlock>();
47 : 28 : std::vector<RemovedMempoolTransactionInfo> removed_txs;
48 : 28 : TestMemPoolEntryHelper entry;
49 [ + - ]: 28 : Assert(block_txs_weight >= removed_txs_weight);
50 [ + - + - ]: 28 : block->vtx.emplace_back(MakeRandomTx()); // Add a coinbase tx
51 [ + + ]: 169376 : while (block_txs_weight > 0) {
52 [ + - ]: 169370 : auto tx = MakeRandomTx();
53 : 169370 : auto tx_weight = GetTransactionWeight(*tx);
54 [ + + ]: 169370 : if (block_txs_weight - tx_weight < 0) break;
55 [ + - ]: 169348 : block->vtx.emplace_back(tx);
56 : 169348 : block_txs_weight -= tx_weight;
57 [ + + ]: 169348 : if (removed_txs_weight - tx_weight >= 0) {
58 [ + - + - ]: 157250 : removed_txs.emplace_back(entry.FromTx(tx));
59 : 157250 : removed_txs_weight -= tx_weight;
60 : : }
61 : 169370 : }
62 [ + - + - ]: 56 : fee_est.MempoolTxsRemovedForBlock(block, removed_txs, height);
63 : 28 : height += 1;
64 [ + - ]: 56 : }
65 : :
66 [ + - + - : 7 : BOOST_AUTO_TEST_CASE(calculate_max_weight_percentiles)
+ - + - -
+ + - + -
+ - + - +
- + - - +
+ - + - +
- + - + -
+ - - + +
- + - + -
+ - + - +
- + - - +
+ - + - +
- + - + -
+ - - + +
- ]
67 : : {
68 : : // With no chunks neither percentile can be populated.
69 : 1 : const auto empty = MemPoolFeeRateEstimator::CalculateMaxWeightPercentiles({});
70 [ + - ]: 2 : BOOST_CHECK(empty.p50.IsEmpty());
71 [ + - + - ]: 2 : BOOST_CHECK(empty.p75.IsEmpty());
72 : 1 : const int32_t chunk_size{10};
73 : 1 : const int32_t individual_tx_vsize = static_cast<int32_t>(DEFAULT_BLOCK_MAX_WEIGHT / WITNESS_SCALE_FACTOR) / chunk_size;
74 : 1 : const FeePerVSize super_high_fee_rate{500 * individual_tx_vsize, individual_tx_vsize};
75 : 1 : const FeePerVSize high_fee_rate{100 * individual_tx_vsize, individual_tx_vsize};
76 : 1 : const FeePerVSize medium_fee_rate{50 * individual_tx_vsize, individual_tx_vsize};
77 : 1 : const FeePerVSize low_fee_rate{10 * individual_tx_vsize, individual_tx_vsize};
78 : 1 : std::vector<FeePerVSize> chunk_feerates;
79 [ + - ]: 1 : chunk_feerates.reserve(chunk_size);
80 [ + + ]: 11 : for (int i = 0; i < chunk_size; ++i) {
81 [ + + ]: 10 : if (i < 3) {
82 [ + - ]: 3 : chunk_feerates.emplace_back(super_high_fee_rate);
83 [ + + ]: 7 : } else if (i < 5) {
84 [ + - ]: 2 : chunk_feerates.emplace_back(high_fee_rate);
85 [ + + ]: 5 : } else if (i < 8) {
86 [ + - ]: 3 : chunk_feerates.emplace_back(medium_fee_rate);
87 : : // Once 50% coverage is reached but 75% is not, only the p50 (conservative)
88 : : // percentile is populated; p75 (economical) is left empty for the caller to floor.
89 [ + + ]: 3 : if (i < 7) {
90 [ - + + - ]: 2 : const auto partial = MemPoolFeeRateEstimator::CalculateMaxWeightPercentiles(chunk_feerates);
91 [ + - + - ]: 2 : BOOST_CHECK_EQUAL(partial.p50.fee, high_fee_rate.fee);
92 [ + - + - ]: 2 : BOOST_CHECK_EQUAL(partial.p50.size, high_fee_rate.size);
93 [ + - + - ]: 4 : BOOST_CHECK(partial.p75.IsEmpty());
94 : : }
95 : : } else {
96 [ + - ]: 2 : chunk_feerates.emplace_back(low_fee_rate);
97 : : }
98 : : }
99 [ - + + - ]: 1 : const auto percentiles = MemPoolFeeRateEstimator::CalculateMaxWeightPercentiles(chunk_feerates);
100 [ + - + - ]: 1 : BOOST_CHECK_EQUAL(percentiles.p50.fee, high_fee_rate.fee);
101 [ + - + - ]: 1 : BOOST_CHECK_EQUAL(percentiles.p50.size, high_fee_rate.size);
102 [ + - + - ]: 1 : BOOST_CHECK_EQUAL(percentiles.p75.fee, medium_fee_rate.fee);
103 [ + - + - ]: 1 : BOOST_CHECK_EQUAL(percentiles.p75.size, medium_fee_rate.size);
104 [ + - + - ]: 2 : BOOST_CHECK(ByRatio{percentiles.p50} > ByRatio{percentiles.p75});
105 : 1 : }
106 : :
107 [ + - + - : 7 : BOOST_AUTO_TEST_CASE(mempool_fee_rate_estimator_cache)
+ - + - -
+ + - + -
+ - + - +
- + - - +
+ - + - +
- + - + -
+ - - + +
- + - + -
+ - + - +
- + - - +
+ - + - +
- + - + -
+ - - + +
- ]
108 : : {
109 : 1 : FakeNodeClock clock{};
110 : 1 : MemPoolFeeRateEstimatorCache cache;
111 : 1 : const uint256 tip_hash{uint256::ONE};
112 : 1 : const uint256 next_tip_hash{uint256{2}};
113 : 1 : const FeePerVSize conservative{2, 1};
114 : 1 : const FeePerVSize economical{1, 1};
115 : :
116 [ + - + - : 2 : BOOST_CHECK(cache.IsStale());
+ - + - ]
117 [ + - + - : 2 : BOOST_CHECK(!cache.GetCachedEstimate(tip_hash));
+ - + - ]
118 : :
119 [ + - ]: 1 : cache.Update(conservative, economical, tip_hash);
120 [ + - + - : 2 : BOOST_CHECK(!cache.IsStale());
+ - + - ]
121 [ + - ]: 1 : const auto cached{cache.GetCachedEstimate(tip_hash)};
122 [ + - + - : 2 : BOOST_REQUIRE(cached);
+ - ]
123 [ + - + - ]: 1 : BOOST_CHECK_EQUAL(cached->m_conservative, conservative);
124 [ + - + - ]: 1 : BOOST_CHECK_EQUAL(cached->m_economical, economical);
125 [ + - + - : 2 : BOOST_CHECK(!cache.GetCachedEstimate(next_tip_hash));
+ - + - ]
126 : :
127 [ + - ]: 1 : clock += CACHE_LIFE + std::chrono::seconds{1};
128 [ + - + - : 2 : BOOST_CHECK(cache.IsStale());
+ - + - ]
129 [ + - + - : 2 : BOOST_CHECK(!cache.GetCachedEstimate(tip_hash));
+ - ]
130 : 1 : }
131 : :
132 [ + - + - : 7 : BOOST_AUTO_TEST_CASE(MempoolFeeRateEstimator)
+ - + - -
+ + - + -
+ - + - +
- + - - +
+ - + - +
- + - + -
+ - - + +
- + - + -
+ - + - +
- + - - +
+ - + - +
- + - + -
+ - - + +
- ]
133 : : {
134 [ + - ]: 1 : auto mempool_estimator = MemPoolFeeRateEstimator(MempoolPolicyEstimatorPath(*m_node.args), *m_node.mempool, *m_node.chainman);
135 [ + - + - ]: 1 : BOOST_CHECK_EQUAL(mempool_estimator.MaximumTarget(), MEMPOOL_FEE_ESTIMATOR_MAX_TARGET);
136 : : // Before the mempool has finished loading, no estimate is available.
137 : 1 : {
138 : 1 : const std::string unloaded_err = strprintf("%s: Mempool not loaded yet, no fee rate estimate available",
139 [ + - + - ]: 1 : FeeRateEstimatorTypeToString(FeeRateEstimatorType::MEMPOOL_POLICY));
140 [ + - ]: 1 : const auto result = mempool_estimator.EstimateFeeRate(/*conservative=*/true);
141 [ + - + - : 2 : BOOST_CHECK(!result);
+ - ]
142 [ + - + - ]: 1 : BOOST_CHECK_EQUAL(result.error().reason, unloaded_err);
143 : 1 : }
144 [ + - ]: 1 : m_node.mempool->SetLoadTried(true);
145 : :
146 [ + - + - : 2 : BOOST_CHECK(!mempool_estimator.IsMempoolHealthy());
+ - + - ]
147 [ + - + - : 2 : BOOST_CHECK(mempool_estimator.GetMempoolHealth() == MemPoolFeeRateEstimator::MempoolHealth::INSUFFICIENT_DATA);
+ - + - ]
148 : 1 : {
149 [ + - ]: 1 : const auto result = mempool_estimator.EstimateFeeRate(/*conservative=*/true);
150 : 1 : const std::string insufficient_err{strprintf("%s: Not enough recent block data for fee rate estimation",
151 [ + - + - ]: 1 : FeeRateEstimatorTypeToString(FeeRateEstimatorType::MEMPOOL_POLICY))};
152 [ + - + - : 2 : BOOST_CHECK(!result);
+ - ]
153 [ + - + - ]: 1 : BOOST_CHECK_EQUAL(result.error().reason, insufficient_err);
154 : 1 : }
155 : 1 : {
156 : 1 : MemPoolFeeRateEstimator custom_mempool_estimator{
157 [ + - + - ]: 1 : MempoolPolicyEstimatorPath(*m_node.args), *m_node.mempool, *m_node.chainman};
158 : 1 : unsigned int custom_height{100};
159 [ + + ]: 6 : for (size_t block_count{1}; block_count < MEMPOOL_HEALTH_WINDOW_BLOCKS; ++block_count) {
160 [ + - ]: 5 : AddRemovedBlock(custom_mempool_estimator,
161 : : /*removed_txs_weight=*/0,
162 : : /*block_txs_weight=*/0,
163 : : custom_height);
164 [ + - + - : 10 : BOOST_CHECK(!custom_mempool_estimator.IsMempoolHealthy());
+ - ]
165 : : }
166 : 1 : {
167 : 1 : const int64_t low_activity_weight{1000};
168 [ + - ]: 1 : AddRemovedBlock(custom_mempool_estimator, low_activity_weight / 2, low_activity_weight, custom_height);
169 : : }
170 : : // Below one block worth of total activity across the full window, even
171 : : // poor coverage in the only non-empty block is too noisy to reject the
172 : : // mempool as unhealthy.
173 [ + - + - : 2 : BOOST_CHECK(custom_mempool_estimator.IsMempoolHealthy());
+ - ]
174 : 1 : }
175 : 1 : size_t block_count = 1;
176 : 1 : const int64_t weight{DEFAULT_BLOCK_MAX_WEIGHT / 2};
177 : 1 : unsigned int height = 100;
178 : : // Equal weight
179 [ + + ]: 7 : while (block_count <= MEMPOOL_HEALTH_WINDOW_BLOCKS) {
180 [ + - ]: 6 : AddRemovedBlock(mempool_estimator, weight, weight, height);
181 [ + + ]: 6 : if (block_count < MEMPOOL_HEALTH_WINDOW_BLOCKS) {
182 [ + - + - : 10 : BOOST_CHECK(!mempool_estimator.IsMempoolHealthy());
+ - ]
183 : : }
184 : 6 : block_count += 1;
185 : : }
186 : : // Total txs weight ~11999k WU (~3.0 blocks), removed txs ~11999k WU (~3.0 blocks); coverage = 100%.
187 [ + - + - : 2 : BOOST_CHECK(mempool_estimator.IsMempoolHealthy());
+ - + - ]
188 : : // Adding a single underrepresented block will not make the mempool unhealthy
189 : : // while the window coverage remains above the threshold.
190 [ + - ]: 1 : AddRemovedBlock(mempool_estimator, weight / 2, weight, height);
191 : : // Total txs weight ~11999k WU (~3.0 blocks), removed txs ~10999k WU (~2.75 blocks); coverage = ~92%.
192 [ + - + - : 2 : BOOST_CHECK(mempool_estimator.IsMempoolHealthy());
+ - + - ]
193 : : // Empty block
194 : : // Total txs weight ~9999k WU (~2.5 blocks), removed txs ~8999k WU (~2.25 blocks); coverage = 90%.
195 [ + - ]: 1 : AddRemovedBlock(mempool_estimator, 0, 0, height);
196 [ + - + - : 2 : BOOST_CHECK(mempool_estimator.IsMempoolHealthy());
+ - + - ]
197 : : // Total txs weight ~9999k WU (~2.5 blocks), removed txs ~7999k WU (~2.0 blocks); coverage = 80%.
198 [ + - ]: 1 : AddRemovedBlock(mempool_estimator, weight / 2, weight, height);
199 [ + - + - : 2 : BOOST_CHECK(mempool_estimator.IsMempoolHealthy());
+ - + - ]
200 : : // Total txs weight ~9999k WU (~2.5 blocks), removed txs ~7000k WU (~1.75 blocks); coverage = 70%.
201 [ + - ]: 1 : AddRemovedBlock(mempool_estimator, weight / 2, weight, height);
202 [ + - + - : 2 : BOOST_CHECK(!mempool_estimator.IsMempoolHealthy());
+ - ]
203 : 1 : block_count = 1;
204 [ + + ]: 4 : while (block_count <= 3) {
205 [ + - ]: 3 : AddRemovedBlock(mempool_estimator, weight, weight, height);
206 [ + + ]: 3 : if (block_count < 3) {
207 [ + - + - : 4 : BOOST_CHECK(!mempool_estimator.IsMempoolHealthy());
+ - ]
208 : : }
209 : 3 : block_count += 1;
210 : : }
211 : : // Total txs weight ~9999k WU (~2.5 blocks), removed txs ~7999k WU (~2.0 blocks); coverage = 80%.
212 [ + - + - : 2 : BOOST_CHECK(mempool_estimator.IsMempoolHealthy());
+ - + - ]
213 : :
214 : : // Reorg out and replace the last block. Replacing the tip block should keep a full
215 : : // healthy window when the replacement block has good mempool representation.
216 : 1 : height -= 1;
217 [ + - ]: 1 : AddRemovedBlock(mempool_estimator, weight, weight, height);
218 [ + - + - : 2 : BOOST_CHECK(mempool_estimator.IsMempoolHealthy());
+ - + - ]
219 : :
220 : : // Reorg out the last two blocks. The estimator should discard the stale suffix,
221 : : // become temporarily unhealthy due to having fewer than MEMPOOL_HEALTH_WINDOW_BLOCKS stats,
222 : : // then recover after the replacement chain catches up.
223 : 1 : height -= 2;
224 [ + - ]: 1 : AddRemovedBlock(mempool_estimator, weight, weight, height);
225 [ + - + - : 2 : BOOST_CHECK(!mempool_estimator.IsMempoolHealthy());
+ - + - ]
226 [ + - ]: 1 : AddRemovedBlock(mempool_estimator, weight, weight, height);
227 [ + - + - : 2 : BOOST_CHECK(mempool_estimator.IsMempoolHealthy());
+ - + - ]
228 : :
229 : : // A forward height gap (e.g. stale persisted stats after an unclean shutdown
230 : : // while the chain advanced) resets the tracked window entirely; the estimator
231 : : // stays unhealthy until a full window of contiguous blocks is seen again.
232 : 1 : height += 3;
233 [ + - ]: 1 : AddRemovedBlock(mempool_estimator, weight, weight, height);
234 [ + - + - : 2 : BOOST_CHECK(!mempool_estimator.IsMempoolHealthy());
+ - ]
235 [ + + ]: 6 : for (size_t i = 1; i < MEMPOOL_HEALTH_WINDOW_BLOCKS; ++i) {
236 [ + - ]: 5 : AddRemovedBlock(mempool_estimator, weight, weight, height);
237 [ + + ]: 5 : if (i < MEMPOOL_HEALTH_WINDOW_BLOCKS - 1) {
238 [ + - + - : 8 : BOOST_CHECK(!mempool_estimator.IsMempoolHealthy());
+ - ]
239 : : }
240 : : }
241 [ + - + - : 2 : BOOST_CHECK(mempool_estimator.IsMempoolHealthy());
+ - + - ]
242 : 1 : {
243 [ + - ]: 1 : LOCK(m_node.mempool->cs);
244 [ + - + - : 1 : BOOST_CHECK_EQUAL(m_node.mempool->GetTotalTxSize(), 0);
+ - ]
245 : 0 : }
246 : : // With an empty mempool there is nothing to build a feerate estimate from, so both
247 : : // estimates fall back to the floor fee rate: the higher of the minimum relay fee rate
248 : : // and the current mempool minimum fee rate.
249 [ + - + - ]: 1 : const FeePerVSize floor{std::max(m_node.mempool->m_opts.min_relay_feerate, m_node.mempool->GetMinFee()).GetFeePerVSize()};
250 : 1 : {
251 [ + - ]: 1 : const auto result = mempool_estimator.EstimateFeeRate(/*conservative=*/true);
252 [ + - + - : 2 : BOOST_REQUIRE(result.has_value());
+ - ]
253 [ + - + - ]: 1 : BOOST_CHECK_EQUAL(result->feerate, floor);
254 [ + - + - : 2 : BOOST_CHECK(result->feerate_estimator == FeeRateEstimatorType::MEMPOOL_POLICY);
+ - ]
255 [ + - + - ]: 1 : BOOST_CHECK_EQUAL(result->returned_target, MEMPOOL_FEE_ESTIMATOR_MAX_TARGET);
256 : :
257 : : // The floor estimate is cached like any other; a second call returns the same value.
258 [ + - ]: 1 : const auto cached_result = mempool_estimator.EstimateFeeRate(/*conservative=*/true);
259 [ + - + - : 2 : BOOST_REQUIRE(cached_result.has_value());
+ - ]
260 [ + - + - ]: 1 : BOOST_CHECK_EQUAL(cached_result->feerate, floor);
261 : 1 : }
262 : 1 : TestMemPoolEntryHelper entry;
263 [ + - + - : 1 : const auto tx_vsize = entry.FromTx(MakeRandomTx()).GetTxSize();
+ - + - ]
264 : 1 : const CAmount low_fee{CENT / 3000};
265 : 1 : const CAmount med_fee{CENT / 100};
266 : 1 : const CAmount high_fee{CENT / 10};
267 : 1 : const CAmount very_high_fee{CENT};
268 : : // A mempool that cannot fill 50% of a block leaves both percentiles empty,
269 : : // so both estimate still fall back to the floor.
270 : 1 : {
271 : : // Add high_fee transactions until mempool weight exceeds 25% of DEFAULT_BLOCK_MAX_WEIGHT.
272 : 1 : {
273 [ + - + - ]: 1 : LOCK2(cs_main, m_node.mempool->cs);
274 [ + + ]: 4034 : while ((m_node.mempool->GetTotalTxSize() * WITNESS_SCALE_FACTOR) <= (DEFAULT_BLOCK_MAX_WEIGHT * 25 / 100)) {
275 [ + - + - : 8066 : TryAddToMempool(*m_node.mempool, entry.Fee(high_fee).FromTx(MakeRandomTx()));
+ - + - ]
276 : : }
277 [ + - ]: 1 : }
278 : : // Expire the cached floor estimate so the denser mempool is observed.
279 [ + - ]: 1 : SetMockTime(GetTime<std::chrono::seconds>() + CACHE_LIFE + std::chrono::seconds{1});
280 [ + - ]: 1 : const auto result = mempool_estimator.EstimateFeeRate(/*conservative=*/true);
281 [ + - + - : 2 : BOOST_REQUIRE(result.has_value());
+ - ]
282 [ + - + - ]: 1 : BOOST_CHECK_EQUAL(result->feerate, floor);
283 : 0 : }
284 : : // A mempool that fills 50% of a block but not 75% has a conservative (p50)
285 : : // estimate, while the economical (p75) estimate falls back to the floor.
286 : 1 : {
287 : : // Add med_fee transactions until mempool weight exceeds 50% of DEFAULT_BLOCK_MAX_WEIGHT.
288 : 1 : {
289 [ + - + - ]: 1 : LOCK2(cs_main, m_node.mempool->cs);
290 [ + + ]: 4033 : while ((m_node.mempool->GetTotalTxSize() * WITNESS_SCALE_FACTOR) <= (DEFAULT_BLOCK_MAX_WEIGHT * 50 / 100)) {
291 [ + - + - : 8064 : TryAddToMempool(*m_node.mempool, entry.Fee(med_fee).FromTx(MakeRandomTx()));
+ - + - ]
292 : : }
293 [ + - ]: 1 : }
294 [ + - ]: 1 : SetMockTime(GetTime<std::chrono::seconds>() + CACHE_LIFE + std::chrono::seconds{1});
295 [ + - ]: 1 : const auto conservative = mempool_estimator.EstimateFeeRate(/*conservative=*/true);
296 [ + - ]: 1 : const auto economical = mempool_estimator.EstimateFeeRate(/*conservative=*/false);
297 [ + - + - : 2 : BOOST_REQUIRE(conservative.has_value());
+ - ]
298 [ + - + - : 2 : BOOST_REQUIRE(economical.has_value());
+ - ]
299 [ + - + - ]: 1 : BOOST_CHECK_EQUAL(conservative->feerate, FeeFrac(med_fee, tx_vsize));
300 [ + - + - ]: 1 : BOOST_CHECK_EQUAL(economical->feerate, floor);
301 : 1 : }
302 : : // Mempool transactions are enough to provide both feerate estimates.
303 : 1 : {
304 : : // Add low_fee transactions until mempool transactions weight
305 : : // is enough to reach the 75% coverage requirement
306 : 1 : {
307 [ + - + - ]: 1 : LOCK2(cs_main, m_node.mempool->cs);
308 [ + + ]: 4033 : while ((m_node.mempool->GetTotalTxSize() * WITNESS_SCALE_FACTOR) <= (DEFAULT_BLOCK_MAX_WEIGHT * 75 / 100)) {
309 [ + - + - : 8064 : TryAddToMempool(*m_node.mempool, entry.Fee(low_fee).FromTx(MakeRandomTx()));
+ - + - ]
310 : : }
311 [ + - ]: 1 : }
312 : : // Expire the sparse-result cache before expecting the estimator to observe the denser mempool.
313 [ + - ]: 1 : SetMockTime(GetTime<std::chrono::seconds>() + CACHE_LIFE + std::chrono::seconds{1});
314 [ + - ]: 1 : const auto result_conservative = mempool_estimator.EstimateFeeRate(/*conservative=*/true);
315 [ + - ]: 1 : const auto result_economical = mempool_estimator.EstimateFeeRate(/*conservative=*/false);
316 [ + - + - : 2 : BOOST_CHECK(result_conservative.has_value());
+ - ]
317 [ + - + - : 2 : BOOST_CHECK(result_economical.has_value());
+ - ]
318 [ + - + - ]: 1 : BOOST_CHECK_EQUAL(result_economical->feerate, FeeFrac(low_fee, tx_vsize));
319 [ + - + - ]: 1 : BOOST_CHECK_EQUAL(result_conservative->feerate, FeeFrac(med_fee, tx_vsize));
320 [ + - + - : 2 : BOOST_CHECK(ByRatio{result_conservative->feerate} > ByRatio{result_economical->feerate});
+ - ]
321 [ + - + - : 2 : BOOST_CHECK(result_conservative->feerate_estimator == FeeRateEstimatorType::MEMPOOL_POLICY);
+ - ]
322 [ + - + - : 2 : BOOST_CHECK(result_economical->feerate_estimator == FeeRateEstimatorType::MEMPOOL_POLICY);
+ - ]
323 [ + - + - ]: 1 : BOOST_CHECK_EQUAL(result_conservative->returned_target, MEMPOOL_FEE_ESTIMATOR_MAX_TARGET);
324 [ + - + - ]: 1 : BOOST_CHECK_EQUAL(result_economical->returned_target, MEMPOOL_FEE_ESTIMATOR_MAX_TARGET);
325 : :
326 : : // Adding another 30% of very-high-fee transactions should change the
327 : : // estimates after recomputation, but not while the cached estimate is fresh.
328 : 1 : {
329 [ + - + - ]: 1 : LOCK2(cs_main, m_node.mempool->cs);
330 [ + + ]: 4840 : while ((m_node.mempool->GetTotalTxSize() * WITNESS_SCALE_FACTOR) <=
331 : : (DEFAULT_BLOCK_MAX_WEIGHT * 105 / 100)) {
332 [ + - + - : 9678 : TryAddToMempool(*m_node.mempool, entry.Fee(very_high_fee).FromTx(MakeRandomTx()));
+ - + - ]
333 : : }
334 [ + - ]: 1 : }
335 [ + - + - : 2 : BOOST_CHECK_EQUAL(mempool_estimator.EstimateFeeRate(/*conservative=*/false).value().feerate, FeeFrac(low_fee, tx_vsize));
+ - ]
336 [ + - + - : 2 : BOOST_CHECK_EQUAL(mempool_estimator.EstimateFeeRate(/*conservative=*/true).value().feerate, FeeFrac(med_fee, tx_vsize));
+ - ]
337 : : // Expire the cache by advancing mock time past CACHE_LIFE so the next call recomputes.
338 [ + - ]: 1 : SetMockTime(GetTime<std::chrono::seconds>() + CACHE_LIFE + std::chrono::seconds{1});
339 [ + - + - : 2 : BOOST_CHECK_EQUAL(mempool_estimator.EstimateFeeRate(/*conservative=*/false).value().feerate, FeeFrac(med_fee, tx_vsize));
+ - ]
340 [ + - + - : 2 : BOOST_CHECK_EQUAL(mempool_estimator.EstimateFeeRate(/*conservative=*/true).value().feerate, FeeFrac(high_fee, tx_vsize));
+ - ]
341 : 1 : }
342 : 1 : }
343 : :
344 : : BOOST_AUTO_TEST_SUITE_END()
|