18#include <validation.h>
20#include <boost/test/unit_test.hpp>
33 tx.vin[0].prevout.n = 0;
36 tx.vout[0].nValue =
COIN;
41 int32_t removed_txs_weight,
42 int32_t block_txs_weight,
45 auto block = std::make_shared<CBlock>();
46 std::vector<RemovedMempoolTransactionInfo> removed_txs;
48 Assert(block_txs_weight >= removed_txs_weight);
50 while (block_txs_weight > 0) {
53 if (block_txs_weight - tx_weight < 0)
break;
54 block->vtx.emplace_back(tx);
55 block_txs_weight -= tx_weight;
56 if (removed_txs_weight - tx_weight >= 0) {
57 removed_txs.emplace_back(entry.
FromTx(tx));
58 removed_txs_weight -= tx_weight;
71 const int32_t chunk_size{10};
73 const FeePerVSize super_high_fee_rate{500 * individual_tx_vsize, individual_tx_vsize};
74 const FeePerVSize high_fee_rate{100 * individual_tx_vsize, individual_tx_vsize};
75 const FeePerVSize medium_fee_rate{50 * individual_tx_vsize, individual_tx_vsize};
76 const FeePerVSize low_fee_rate{10 * individual_tx_vsize, individual_tx_vsize};
77 std::vector<FeePerVSize> chunk_feerates;
78 chunk_feerates.reserve(chunk_size);
79 for (
int i = 0; i < chunk_size; ++i) {
81 chunk_feerates.emplace_back(super_high_fee_rate);
83 chunk_feerates.emplace_back(high_fee_rate);
85 chunk_feerates.emplace_back(medium_fee_rate);
95 chunk_feerates.emplace_back(low_fee_rate);
118 cache.
Update(conservative, economical, tip_hash);
121 BOOST_REQUIRE(cached);
122 BOOST_CHECK(cached->m_conservative == conservative);
126 clock +=
CACHE_LIFE + std::chrono::seconds{1};
137 const std::string unloaded_err =
strprintf(
"%s: Mempool not loaded yet, no fee rate estimate available",
139 const auto result = mempool_estimator.EstimateFeeRate(
true);
145 BOOST_CHECK(!mempool_estimator.IsMempoolHealthy());
148 const auto result = mempool_estimator.EstimateFeeRate(
true);
149 const std::string insufficient_err{
strprintf(
"%s: Not enough recent block data for fee rate estimation",
157 unsigned int custom_height{100};
163 BOOST_CHECK(!custom_mempool_estimator.IsMempoolHealthy());
166 const int64_t low_activity_weight{1000};
167 AddRemovedBlock(custom_mempool_estimator, low_activity_weight / 2, low_activity_weight, custom_height);
172 BOOST_CHECK(custom_mempool_estimator.IsMempoolHealthy());
174 size_t block_count = 1;
176 unsigned int height = 100;
181 BOOST_CHECK(!mempool_estimator.IsMempoolHealthy());
201 BOOST_CHECK(!mempool_estimator.IsMempoolHealthy());
203 while (block_count <= 3) {
205 if (block_count < 3) {
206 BOOST_CHECK(!mempool_estimator.IsMempoolHealthy());
224 BOOST_CHECK(!mempool_estimator.IsMempoolHealthy());
233 BOOST_CHECK(!mempool_estimator.IsMempoolHealthy());
237 BOOST_CHECK(!mempool_estimator.IsMempoolHealthy());
250 const auto result = mempool_estimator.EstimateFeeRate(
true);
251 BOOST_REQUIRE(result.has_value());
257 const auto cached_result = mempool_estimator.EstimateFeeRate(
true);
258 BOOST_REQUIRE(cached_result.has_value());
279 const auto result = mempool_estimator.EstimateFeeRate(
true);
280 BOOST_REQUIRE(result.has_value());
294 const auto conservative = mempool_estimator.EstimateFeeRate(
true);
295 const auto economical = mempool_estimator.EstimateFeeRate(
false);
296 BOOST_REQUIRE(conservative.has_value());
297 BOOST_REQUIRE(economical.has_value());
313 const auto result_conservative = mempool_estimator.EstimateFeeRate(
true);
314 const auto result_economical = mempool_estimator.EstimateFeeRate(
false);
int64_t CAmount
Amount in satoshis (Can be negative)
constexpr CAmount COIN
The amount of satoshis in one BTC.
TryAddToMempool(pool, CTxMemPoolEntry(tx, fee, 0, 1, 0, false, 4, lp))
#define Assert(val)
Identity function.
Wrapper around FeeFrac & derived types, which adds a feerate-based ordering which treats equal-feerat...
Serialized script, used inside transaction inputs and outputs.
int32_t GetTxSize() const
Helper to initialize the global NodeClock, let a duration elapse, and reset it after use in a test.
MemPoolFeeRateEstimatorCache holds a cache of recent fee rate estimates.
std::optional< FeeRateEstimate > GetCachedEstimate(const uint256 &tip_hash) const
Returns cached estimates if not stale and computed on tip_hash, nullopt otherwise.
bool IsStale() const
Returns true if the cache is empty or older than CACHE_LIFE.
void Update(FeePerVSize conservative, FeePerVSize economical, const uint256 &tip_hash)
Update the cache with new estimates computed on tip_hash.
Estimate the fee rate required for a transaction to be included in the next block.
void MempoolTxsRemovedForBlock(const std::shared_ptr< const CBlock > &block, const std::vector< RemovedMempoolTransactionInfo > &txs_removed_for_block, unsigned int block_height) EXCLUSIVE_LOCKS_REQUIRED(!cs)
static Percentiles CalculateMaxWeightPercentiles(std::span< const FeePerVSize > chunk_feerates)
Calculate the 50th and 75th percentile fee rates from block template chunks, sorted in descending min...
@ INSUFFICIENT_DATA
Too few recent mined blocks to estimate a fee rate.
uint256 rand256() noexcept
generate a random uint256.
static transaction_identifier FromUint256(const uint256 &id)
static int32_t GetTransactionWeight(const CTransaction &tx)
constexpr int WITNESS_SCALE_FACTOR
RecursiveMutex cs_main
Mutex to guard access to validation specific variables, such as reading or changing the chainstate.
BOOST_FIXTURE_TEST_SUITE(cuckoocache_tests, BasicTestingSetup)
Test Suite for CuckooCache.
BOOST_AUTO_TEST_SUITE_END()
fs::path MempoolPolicyEstimatorPath(const ArgsManager &argsman)
BOOST_CHECK_EQUAL(headers.FindFirst("key"), "value")
constexpr int MEMPOOL_FEE_ESTIMATOR_MAX_TARGET
constexpr std::chrono::seconds CACHE_LIFE
constexpr size_t MEMPOOL_HEALTH_WINDOW_BLOCKS
void AddRemovedBlock(MemPoolFeeRateEstimator &fee_est, int32_t removed_txs_weight, int32_t block_txs_weight, unsigned int &height)
BOOST_AUTO_TEST_CASE(calculate_max_weight_percentiles)
static CTransactionRef MakeRandomTx()
#define BOOST_CHECK(expr)
constexpr unsigned int DEFAULT_BLOCK_MAX_WEIGHT
Default for -blockmaxweight, which controls the range of block weights the mining code will create.
static CTransactionRef MakeTransactionRef(Tx &&txIn)
std::shared_ptr< const CTransaction > CTransactionRef
A mutable version of CTransaction.
Data structure storing a fee and size.
CTxMemPoolEntry FromTx(const CMutableTransaction &tx) const
TestMemPoolEntryHelper & Fee(CAmount _fee)
Testing setup that configures a complete environment.
std::unique_ptr< CTxMemPool > mempool
std::unique_ptr< ChainstateManager > chainman
std::string_view FeeRateEstimatorTypeToString(FeeRateEstimatorType feerate_estimator_type)
void SetMockTime(std::chrono::time_point< NodeClock, std::chrono::seconds > mock)