diff --git a/src/init.cpp b/src/init.cpp index 7b82067c8c761..3b5d4b930b659 100644 --- a/src/init.cpp +++ b/src/init.cpp @@ -57,8 +57,9 @@ #include #include #include -#include #include +#include +#include #include #include #include @@ -1429,24 +1430,6 @@ bool AppInitMain(NodeContext& node, interfaces::BlockAndHeaderTipInfo* tip_info) rng.rand64(), *node.addrman, *node.netgroupman, chainparams, args.GetBoolArg("-networkactive", true)); - assert(!node.forecasterman); - // Don't initialize fee estimation with old data if we don't relay transactions, - // as they would never get updated. - if (!peerman_opts.ignore_incoming_txs) { - bool read_stale_estimates = args.GetBoolArg("-acceptstalefeeestimates", DEFAULT_ACCEPT_STALE_FEE_ESTIMATES); - if (read_stale_estimates && (chainparams.GetChainType() != ChainType::REGTEST)) { - return InitError(strprintf(_("acceptstalefeeestimates is not supported on %s chain."), chainparams.GetChainTypeString())); - } - node.forecasterman = std::make_unique(); - auto block_policy_estimator = std::make_shared(FeeestPath(args), read_stale_estimates); - validation_signals.RegisterSharedValidationInterface(block_policy_estimator); - // Flush block policy estimates to disk periodically - scheduler.scheduleEvery([block_policy_estimator] { block_policy_estimator->FlushFeeEstimates(); }, FEE_FLUSH_INTERVAL); - - // Register block policy estimator to forecaster manager - node.forecasterman->RegisterForecaster(block_policy_estimator); - } - for (const std::string& socket_addr : args.GetArgs("-bind")) { std::string host_out; uint16_t port_out{0}; @@ -1660,6 +1643,26 @@ bool AppInitMain(NodeContext& node, interfaces::BlockAndHeaderTipInfo* tip_info) ChainstateManager& chainman = *Assert(node.chainman); + assert(!node.forecasterman); + // Don't initialize fee estimation with old data if we don't relay transactions, + // as they would never get updated. + if (!peerman_opts.ignore_incoming_txs) { + bool read_stale_estimates = args.GetBoolArg("-acceptstalefeeestimates", DEFAULT_ACCEPT_STALE_FEE_ESTIMATES); + if (read_stale_estimates && (chainparams.GetChainType() != ChainType::REGTEST)) { + return InitError(strprintf(_("acceptstalefeeestimates is not supported on %s chain."), chainparams.GetChainTypeString())); + } + node.forecasterman = std::make_unique(); + auto mempool_forecaster = std::make_shared(node.mempool.get(), &(chainman.ActiveChainstate())); + node.forecasterman->RegisterForecaster(mempool_forecaster); + auto block_policy_estimator = std::make_shared(FeeestPath(args), read_stale_estimates); + validation_signals.RegisterSharedValidationInterface(block_policy_estimator); + // Flush block policy estimates to disk periodically + scheduler.scheduleEvery([block_policy_estimator] { block_policy_estimator->FlushFeeEstimates(); }, FEE_FLUSH_INTERVAL); + + // Register block policy estimator to forecaster manager + node.forecasterman->RegisterForecaster(block_policy_estimator); + } + assert(!node.peerman); node.peerman = PeerManager::make(*node.connman, *node.addrman, node.banman.get(), chainman, diff --git a/src/policy/fees/forecaster_man.cpp b/src/policy/fees/forecaster_man.cpp index 38f2f65bbbc61..ee5d3560a570e 100644 --- a/src/policy/fees/forecaster_man.cpp +++ b/src/policy/fees/forecaster_man.cpp @@ -2,11 +2,13 @@ // Distributed under the MIT software license. See the accompanying // file COPYING or http://www.opensource.org/licenses/mit-license.php. +#include #include #include #include #include +#include #include void FeeRateForecasterManager::RegisterForecaster(std::shared_ptr forecaster) @@ -20,3 +22,46 @@ CBlockPolicyEstimator* FeeRateForecasterManager::GetBlockPolicyEstimator() Forecaster* block_policy_estimator = forecasters.find(ForecastType::BLOCK_POLICY)->second.get(); return dynamic_cast(block_policy_estimator); } + +std::pair, std::vector> FeeRateForecasterManager::GetFeeRateForecast(ConfirmationTarget& target) +{ + std::vector err_messages; + ForecastResult selected_forecast; + + for (const auto& forecaster : forecasters) { + auto curr_forecast = forecaster.second->EstimateFee(target); + + if (curr_forecast.GetError().has_value()) { + err_messages.emplace_back( + strprintf("%s: %s", forecastTypeToString(forecaster.first), curr_forecast.GetError().value_or(""))); + } + + // Handle case where the block policy estimator does not have enough data for fee estimates. + if (curr_forecast.Empty() && forecaster.first == ForecastType::BLOCK_POLICY) { + return {std::nullopt, err_messages}; + } + + if (!curr_forecast.Empty()) { + if (selected_forecast.Empty()) { + // If there's no selected forecast, choose curr_forecast as selected_forecast. + selected_forecast = curr_forecast; + } else { + // Otherwise, choose the smaller as selected_forecast. + selected_forecast = std::min(selected_forecast, curr_forecast); + } + } + } + + if (!selected_forecast.Empty()) { + LogDebug(BCLog::ESTIMATEFEE, + "FeeEst %s: Block height %s, low priority fee rate %s %s/kvB, high priority fee rate %s %s/kvB.\n", + forecastTypeToString(selected_forecast.GetResponse().forecaster), + selected_forecast.GetResponse().current_block_height, + CFeeRate(selected_forecast.GetResponse().low_priority.fee, selected_forecast.GetResponse().low_priority.size).GetFeePerK(), + CURRENCY_ATOM, + CFeeRate(selected_forecast.GetResponse().high_priority.fee, selected_forecast.GetResponse().high_priority.size).GetFeePerK(), + CURRENCY_ATOM); + } + + return {selected_forecast, err_messages}; +} diff --git a/src/policy/fees/forecaster_man.h b/src/policy/fees/forecaster_man.h index 3e352fc481721..fb30166e5ae77 100644 --- a/src/policy/fees/forecaster_man.h +++ b/src/policy/fees/forecaster_man.h @@ -6,12 +6,15 @@ #define BITCOIN_POLICY_FEES_FORECASTER_MAN_H #include +#include #include class CBlockPolicyEstimator; class Forecaster; class ForecastResult; +struct ConfirmationTarget; + enum class ForecastType; /** \class FeeRateForecasterManager @@ -38,6 +41,17 @@ class FeeRateForecasterManager * Return the pointer to block policy estimator. */ CBlockPolicyEstimator* GetBlockPolicyEstimator(); + + /** + * Get a fee rate estimate from all registered forecasters for a given confirmation target. + * + * Polls all registered forecasters and selects the lowest fee rate + * estimate with acceptable confidence. + * + * @param[in] target The target within which the transaction should be confirmed. + * @return A pair consisting of the forecast result and a vector of forecaster names. + */ + std::pair, std::vector> GetFeeRateForecast(ConfirmationTarget& target); }; #endif // BITCOIN_POLICY_FEES_FORECASTER_MAN_H