Inside Bitcoin
Opening the selected file…
Inside Bitcoin
Opening the selected file…
The code behind the network, with room to understand it.
Release published 2026-10-28 · Snapshot fetched 2026-11-01. Latest stable release at fetch time; development branches and Git history are excluded. Only the selected code window and visible folders load here.
1// Copyright (c) 2020-present The Bitcoin Core developers2// Distributed under the MIT software license, see the accompanying3// file COPYING or http://www.opensource.org/licenses/mit-license.php.5#include <policy/fees/block_policy_estimator.h>7#include <kernel/mempool_entry.h>8#include <policy/fees/estimator_args.h>9#include <primitives/transaction.h>10#include <streams.h>11#include <test/fuzz/FuzzedDataProvider.h>12#include <test/fuzz/fuzz.h>13#include <test/fuzz/util.h>14#include <test/fuzz/util/mempool.h>15#include <test/util/setup_common.h>17#include <memory>18#include <optional>19#include <vector>21namespace {22const BasicTestingSetup* g_setup;23} // namespace25void initialize_block_policy_estimator()26{27static const auto testing_setup = MakeNoLogFileContext<>();28g_setup = testing_setup.get();29}31FUZZ_TARGET(block_policy_estimator, .init = initialize_block_policy_estimator)32{33FuzzedDataProvider fuzzed_data_provider(buffer.data(), buffer.size());34bool good_data{true};36CBlockPolicyEstimator block_policy_estimator{BlockPolicyFeeEstPath(*g_setup->m_node.args), DEFAULT_ACCEPT_STALE_FEE_ESTIMATES};38uint32_t current_height{0};39const auto advance_height{40[&] { current_height = fuzzed_data_provider.ConsumeIntegralInRange<decltype(current_height)>(current_height, 1 << 30); },41};42advance_height();43LIMITED_WHILE (good_data && fuzzed_data_provider.ConsumeBool(), 10'000) {44CallOneOf(45fuzzed_data_provider,46[&] {47const std::optional<CMutableTransaction> mtx = ConsumeDeserializable<CMutableTransaction>(fuzzed_data_provider, TX_WITH_WITNESS);48if (!mtx) {49good_data = false;50return;51}52const CTransaction tx{*mtx};53const auto entry{ConsumeTxMemPoolEntry(fuzzed_data_provider, tx, current_height)};54const auto tx_submitted_in_package = fuzzed_data_provider.ConsumeBool();55const auto tx_has_mempool_parents = fuzzed_data_provider.ConsumeBool();56const auto tx_info = NewMempoolTransactionInfo(entry.GetSharedTx(), entry.GetFee(),57entry.GetTxSize(), entry.GetHeight(),58/*mempool_limit_bypassed=*/false,59tx_submitted_in_package,60/*chainstate_is_current=*/true,61tx_has_mempool_parents);62block_policy_estimator.processTransaction(tx_info);63if (fuzzed_data_provider.ConsumeBool()) {64(void)block_policy_estimator.removeTx(tx.GetHash());65}66},67[&] {68std::list<CTxMemPoolEntry> mempool_entries;69LIMITED_WHILE (fuzzed_data_provider.ConsumeBool(), 10000) {70const std::optional<CMutableTransaction> mtx = ConsumeDeserializable<CMutableTransaction>(fuzzed_data_provider, TX_WITH_WITNESS);71if (!mtx) {72good_data = false;73break;74}75const CTransaction tx{*mtx};76mempool_entries.push_back(ConsumeTxMemPoolEntry(fuzzed_data_provider, tx, current_height));77}78std::vector<RemovedMempoolTransactionInfo> txs;79txs.reserve(mempool_entries.size());80for (const CTxMemPoolEntry& mempool_entry : mempool_entries) {81txs.emplace_back(mempool_entry);82}83advance_height();84block_policy_estimator.processBlock(txs, current_height);85},86[&] {87(void)block_policy_estimator.removeTx(Txid::FromUint256(ConsumeUInt256(fuzzed_data_provider)));88},89[&] {90block_policy_estimator.FlushUnconfirmed();91});92(void)block_policy_estimator.estimateFee(fuzzed_data_provider.ConsumeIntegral<int>());93EstimationResult result;94auto conf_target = fuzzed_data_provider.ConsumeIntegral<int>();95auto success_threshold = fuzzed_data_provider.ConsumeFloatingPoint<double>();96auto horizon = fuzzed_data_provider.PickValueInArray(ALL_FEE_ESTIMATE_HORIZONS);97auto* result_ptr = fuzzed_data_provider.ConsumeBool() ? &result : nullptr;98(void)block_policy_estimator.estimateRawFee(conf_target, success_threshold, horizon, result_ptr);100FeeCalculation fee_calculation;101conf_target = fuzzed_data_provider.ConsumeIntegral<int>();102auto* fee_calc_ptr = fuzzed_data_provider.ConsumeBool() ? &fee_calculation : nullptr;103auto conservative = fuzzed_data_provider.ConsumeBool();104(void)block_policy_estimator.estimateSmartFee(conf_target, fee_calc_ptr, conservative);106(void)block_policy_estimator.HighestTargetTracked(fuzzed_data_provider.PickValueInArray(ALL_FEE_ESTIMATE_HORIZONS));107}108{109FuzzedFileProvider fuzzed_file_provider{fuzzed_data_provider};110AutoFile fuzzed_auto_file{fuzzed_file_provider.open()};111block_policy_estimator.Write(fuzzed_auto_file);112block_policy_estimator.Read(fuzzed_auto_file);113(void)fuzzed_auto_file.fclose();114}115}
Independent explanations by MyBitcoinExplorer. Not affiliated with or endorsed by Bitcoin Core. Guides are educational and do not replace the source or its tests.
Bitcoin Core source © its contributors, distributed under the MIT license. Original notices and bundled third-party licenses are retained in the source tree.