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) 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/estimator_man.h>7#include <logging.h>8#include <policy/feerate.h>9#include <policy/fees/block_policy_estimator.h>10#include <policy/fees/mempool_estimator.h>11#include <util/fees.h>13FeeRateEstimatorManager::~FeeRateEstimatorManager() = default;15FeeRateEstimatorManager::FeeRateEstimatorManager(const fs::path& block_policy_path,16bool read_stale_estimates,17const fs::path& mempool_estimator_path,18const CTxMemPool& mempool,19ChainstateManager& chainman)20: m_block_policy_estimator(std::make_unique<CBlockPolicyEstimator>(block_policy_path, read_stale_estimates)),21m_mempool_estimator(std::make_unique<MemPoolFeeRateEstimator>(mempool_estimator_path, mempool, chainman))22{23}25util::Expected<FeeRateEstimation, FeeRateEstimationError> FeeRateEstimatorManager::GetFeeRateEstimate(int target, bool conservative) const26{27auto selected_estimate = m_block_policy_estimator->EstimateFeeRate(target, conservative);28if (!selected_estimate) {29LogDebug(BCLog::ESTIMATEFEE, "%s", selected_estimate.error().reason);30return selected_estimate;31}32auto mempool_estimate = m_mempool_estimator->EstimateFeeRate(conservative);33if (mempool_estimate) {34selected_estimate = std::min(*selected_estimate, *mempool_estimate);35} else {36LogDebug(BCLog::ESTIMATEFEE, "%s", mempool_estimate.error().reason);37}38LogDebug(BCLog::ESTIMATEFEE, "Fee rate estimated using %s: target=%s feerate=%s %s/kvB.",39FeeRateEstimatorTypeToString(selected_estimate->feerate_estimator),40selected_estimate->returned_target, CFeeRate(selected_estimate->feerate).GetFeePerK(), CURRENCY_ATOM);41return selected_estimate;42}44util::Expected<FeeRateEstimation, FeeRateEstimationError> FeeRateEstimatorManager::GetFeeRateEstimate(FeeRateEstimatorType type, int target, bool conservative) const45{46switch (type) {47case FeeRateEstimatorType::AUTO:48return GetFeeRateEstimate(target, conservative);49case FeeRateEstimatorType::BLOCK_POLICY:50return m_block_policy_estimator->EstimateFeeRate(target, conservative);51case FeeRateEstimatorType::MEMPOOL_POLICY:52return m_mempool_estimator->EstimateFeeRate(conservative);53} // no default case, so the compiler can warn about missing cases54assert(false);55}57void FeeRateEstimatorManager::IntervalFlush()58{59m_block_policy_estimator->FlushFeeEstimates();60m_mempool_estimator->FlushMinedBlockStats();61}63void FeeRateEstimatorManager::ShutdownFlush()64{65m_block_policy_estimator->Flush();66m_mempool_estimator->FlushMinedBlockStats();67}69void FeeRateEstimatorManager::MempoolLoadFailed()70{71m_mempool_estimator->MempoolLoadFailed();72}74std::vector<MinedBlockStats> FeeRateEstimatorManager::MempoolPolicyEstimatorBlocksStats() const75{76return m_mempool_estimator->GetPrevBlockData();77}79void FeeRateEstimatorManager::TransactionAddedToMempool(const NewMempoolTransactionInfo& tx, uint64_t /*unused*/)80{81m_block_policy_estimator->processTransaction(tx);82}84void FeeRateEstimatorManager::TransactionRemovedFromMempool(const CTransactionRef& tx, MemPoolRemovalReason /*unused*/, uint64_t /*unused*/)85{86m_block_policy_estimator->removeTx(tx->GetHash());87}89void FeeRateEstimatorManager::MempoolTransactionsRemovedForBlock(const std::shared_ptr<const CBlock>& block, const std::vector<RemovedMempoolTransactionInfo>& txs_removed_for_block, unsigned int block_height)90{91m_block_policy_estimator->processBlock(txs_removed_for_block, block_height);92m_mempool_estimator->MempoolTxsRemovedForBlock(block, txs_removed_for_block, block_height);93}95CFeeRate FeeRateEstimatorManager::BlockPolicyEstimateRawFee(unsigned int target, double threshold, FeeEstimateHorizon horizon, EstimationResult* buckets) const96{97return m_block_policy_estimator->estimateRawFee(target, threshold, horizon, buckets);98}100unsigned int FeeRateEstimatorManager::BlockPolicyHighestTargetTracked(FeeEstimateHorizon horizon) const101{102return m_block_policy_estimator->HighestTargetTracked(horizon);103}105unsigned int FeeRateEstimatorManager::MaximumTarget() const106{107return std::max(m_block_policy_estimator->MaximumTarget(), m_mempool_estimator->MaximumTarget());108}