mirror of
https://github.com/bitcoin/bitcoin.git
synced 2025-03-17 21:32:00 +01:00
Merge c6b94403bf9d5bf4fe9d5e5360b3d2c30a430d69 into 5f4422d68dc3530c353af1f87499de1c864b60ad
This commit is contained in:
commit
e4aead69cc
@ -163,6 +163,7 @@ add_library(bitcoin_common STATIC EXCLUDE_FROM_ALL
|
|||||||
netbase.cpp
|
netbase.cpp
|
||||||
outputtype.cpp
|
outputtype.cpp
|
||||||
policy/feerate.cpp
|
policy/feerate.cpp
|
||||||
|
policy/fees/forecaster_util.cpp
|
||||||
policy/policy.cpp
|
policy/policy.cpp
|
||||||
pow.cpp
|
pow.cpp
|
||||||
protocol.cpp
|
protocol.cpp
|
||||||
@ -277,8 +278,10 @@ add_library(bitcoin_node STATIC EXCLUDE_FROM_ALL
|
|||||||
node/warnings.cpp
|
node/warnings.cpp
|
||||||
noui.cpp
|
noui.cpp
|
||||||
policy/ephemeral_policy.cpp
|
policy/ephemeral_policy.cpp
|
||||||
policy/fees.cpp
|
policy/fees/block_policy_estimator.cpp
|
||||||
policy/fees_args.cpp
|
policy/fees/block_policy_estimator_args.cpp
|
||||||
|
policy/fees/forecaster_man.cpp
|
||||||
|
policy/fees/mempool_forecaster.cpp
|
||||||
policy/packages.cpp
|
policy/packages.cpp
|
||||||
policy/rbf.cpp
|
policy/rbf.cpp
|
||||||
policy/settings.cpp
|
policy/settings.cpp
|
||||||
|
@ -6,7 +6,7 @@
|
|||||||
#include <common/messages.h>
|
#include <common/messages.h>
|
||||||
|
|
||||||
#include <common/types.h>
|
#include <common/types.h>
|
||||||
#include <policy/fees.h>
|
#include <policy/fees/block_policy_estimator.h>
|
||||||
#include <node/types.h>
|
#include <node/types.h>
|
||||||
#include <tinyformat.h>
|
#include <tinyformat.h>
|
||||||
#include <util/strencodings.h>
|
#include <util/strencodings.h>
|
||||||
|
54
src/init.cpp
54
src/init.cpp
@ -57,8 +57,10 @@
|
|||||||
#include <node/miner.h>
|
#include <node/miner.h>
|
||||||
#include <node/peerman_args.h>
|
#include <node/peerman_args.h>
|
||||||
#include <policy/feerate.h>
|
#include <policy/feerate.h>
|
||||||
#include <policy/fees.h>
|
#include <policy/fees/block_policy_estimator.h>
|
||||||
#include <policy/fees_args.h>
|
#include <policy/fees/block_policy_estimator_args.h>
|
||||||
|
#include <policy/fees/forecaster_man.h>
|
||||||
|
#include <policy/fees/mempool_forecaster.h>
|
||||||
#include <policy/policy.h>
|
#include <policy/policy.h>
|
||||||
#include <policy/settings.h>
|
#include <policy/settings.h>
|
||||||
#include <protocol.h>
|
#include <protocol.h>
|
||||||
@ -328,13 +330,9 @@ void Shutdown(NodeContext& node)
|
|||||||
DumpMempool(*node.mempool, MempoolPath(*node.args));
|
DumpMempool(*node.mempool, MempoolPath(*node.args));
|
||||||
}
|
}
|
||||||
|
|
||||||
// Drop transactions we were still watching, record fee estimations and unregister
|
// Drop transactions we were still watching, record fee estimations.
|
||||||
// fee estimator from validation interface.
|
if (node.forecasterman) {
|
||||||
if (node.fee_estimator) {
|
node.forecasterman->GetBlockPolicyEstimator()->Flush();
|
||||||
node.fee_estimator->Flush();
|
|
||||||
if (node.validation_signals) {
|
|
||||||
node.validation_signals->UnregisterValidationInterface(node.fee_estimator.get());
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// FlushStateToDisk generates a ChainStateFlushed callback, which we should avoid missing
|
// FlushStateToDisk generates a ChainStateFlushed callback, which we should avoid missing
|
||||||
@ -389,7 +387,7 @@ void Shutdown(NodeContext& node)
|
|||||||
node.validation_signals->UnregisterAllValidationInterfaces();
|
node.validation_signals->UnregisterAllValidationInterfaces();
|
||||||
}
|
}
|
||||||
node.mempool.reset();
|
node.mempool.reset();
|
||||||
node.fee_estimator.reset();
|
node.forecasterman.reset();
|
||||||
node.chainman.reset();
|
node.chainman.reset();
|
||||||
node.validation_signals.reset();
|
node.validation_signals.reset();
|
||||||
node.scheduler.reset();
|
node.scheduler.reset();
|
||||||
@ -1500,22 +1498,6 @@ bool AppInitMain(NodeContext& node, interfaces::BlockAndHeaderTipInfo* tip_info)
|
|||||||
rng.rand64(),
|
rng.rand64(),
|
||||||
*node.addrman, *node.netgroupman, chainparams, args.GetBoolArg("-networkactive", true));
|
*node.addrman, *node.netgroupman, chainparams, args.GetBoolArg("-networkactive", true));
|
||||||
|
|
||||||
assert(!node.fee_estimator);
|
|
||||||
// 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.fee_estimator = std::make_unique<CBlockPolicyEstimator>(FeeestPath(args), read_stale_estimates);
|
|
||||||
|
|
||||||
// Flush estimates to disk periodically
|
|
||||||
CBlockPolicyEstimator* fee_estimator = node.fee_estimator.get();
|
|
||||||
scheduler.scheduleEvery([fee_estimator] { fee_estimator->FlushFeeEstimates(); }, FEE_FLUSH_INTERVAL);
|
|
||||||
validation_signals.RegisterValidationInterface(fee_estimator);
|
|
||||||
}
|
|
||||||
|
|
||||||
for (const std::string& socket_addr : args.GetArgs("-bind")) {
|
for (const std::string& socket_addr : args.GetArgs("-bind")) {
|
||||||
std::string host_out;
|
std::string host_out;
|
||||||
uint16_t port_out{0};
|
uint16_t port_out{0};
|
||||||
@ -1730,6 +1712,26 @@ bool AppInitMain(NodeContext& node, interfaces::BlockAndHeaderTipInfo* tip_info)
|
|||||||
|
|
||||||
ChainstateManager& chainman = *Assert(node.chainman);
|
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<FeeRateForecasterManager>();
|
||||||
|
auto mempool_forecaster = std::make_shared<MemPoolForecaster>(node.mempool.get(), &(chainman.ActiveChainstate()));
|
||||||
|
node.forecasterman->RegisterForecaster(mempool_forecaster);
|
||||||
|
auto block_policy_estimator = std::make_shared<CBlockPolicyEstimator>(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);
|
assert(!node.peerman);
|
||||||
node.peerman = PeerManager::make(*node.connman, *node.addrman,
|
node.peerman = PeerManager::make(*node.connman, *node.addrman,
|
||||||
node.banman.get(), chainman,
|
node.banman.get(), chainman,
|
||||||
|
@ -27,7 +27,7 @@
|
|||||||
#include <node/txdownloadman.h>
|
#include <node/txdownloadman.h>
|
||||||
#include <node/txreconciliation.h>
|
#include <node/txreconciliation.h>
|
||||||
#include <node/warnings.h>
|
#include <node/warnings.h>
|
||||||
#include <policy/fees.h>
|
#include <policy/fees/block_policy_estimator.h>
|
||||||
#include <policy/policy.h>
|
#include <policy/policy.h>
|
||||||
#include <policy/settings.h>
|
#include <policy/settings.h>
|
||||||
#include <primitives/block.h>
|
#include <primitives/block.h>
|
||||||
|
@ -15,7 +15,7 @@
|
|||||||
#include <netgroup.h>
|
#include <netgroup.h>
|
||||||
#include <node/kernel_notifications.h>
|
#include <node/kernel_notifications.h>
|
||||||
#include <node/warnings.h>
|
#include <node/warnings.h>
|
||||||
#include <policy/fees.h>
|
#include <policy/fees/forecaster_man.h>
|
||||||
#include <scheduler.h>
|
#include <scheduler.h>
|
||||||
#include <txmempool.h>
|
#include <txmempool.h>
|
||||||
#include <validation.h>
|
#include <validation.h>
|
||||||
|
@ -16,7 +16,7 @@ class ArgsManager;
|
|||||||
class AddrMan;
|
class AddrMan;
|
||||||
class BanMan;
|
class BanMan;
|
||||||
class BaseIndex;
|
class BaseIndex;
|
||||||
class CBlockPolicyEstimator;
|
class FeeRateForecasterManager;
|
||||||
class CConnman;
|
class CConnman;
|
||||||
class ValidationSignals;
|
class ValidationSignals;
|
||||||
class CScheduler;
|
class CScheduler;
|
||||||
@ -67,7 +67,7 @@ struct NodeContext {
|
|||||||
std::unique_ptr<CConnman> connman;
|
std::unique_ptr<CConnman> connman;
|
||||||
std::unique_ptr<CTxMemPool> mempool;
|
std::unique_ptr<CTxMemPool> mempool;
|
||||||
std::unique_ptr<const NetGroupManager> netgroupman;
|
std::unique_ptr<const NetGroupManager> netgroupman;
|
||||||
std::unique_ptr<CBlockPolicyEstimator> fee_estimator;
|
std::unique_ptr<FeeRateForecasterManager> forecasterman;
|
||||||
std::unique_ptr<PeerManager> peerman;
|
std::unique_ptr<PeerManager> peerman;
|
||||||
std::unique_ptr<ChainstateManager> chainman;
|
std::unique_ptr<ChainstateManager> chainman;
|
||||||
std::unique_ptr<BanMan> banman;
|
std::unique_ptr<BanMan> banman;
|
||||||
|
@ -40,7 +40,8 @@
|
|||||||
#include <node/types.h>
|
#include <node/types.h>
|
||||||
#include <node/warnings.h>
|
#include <node/warnings.h>
|
||||||
#include <policy/feerate.h>
|
#include <policy/feerate.h>
|
||||||
#include <policy/fees.h>
|
#include <policy/fees/block_policy_estimator.h>
|
||||||
|
#include <policy/fees/forecaster_man.h>
|
||||||
#include <policy/policy.h>
|
#include <policy/policy.h>
|
||||||
#include <policy/rbf.h>
|
#include <policy/rbf.h>
|
||||||
#include <policy/settings.h>
|
#include <policy/settings.h>
|
||||||
@ -739,13 +740,13 @@ public:
|
|||||||
}
|
}
|
||||||
CFeeRate estimateSmartFee(int num_blocks, bool conservative, FeeCalculation* calc) override
|
CFeeRate estimateSmartFee(int num_blocks, bool conservative, FeeCalculation* calc) override
|
||||||
{
|
{
|
||||||
if (!m_node.fee_estimator) return {};
|
if (!m_node.forecasterman) return {};
|
||||||
return m_node.fee_estimator->estimateSmartFee(num_blocks, calc, conservative);
|
return m_node.forecasterman->GetBlockPolicyEstimator()->estimateSmartFee(num_blocks, calc, conservative);
|
||||||
}
|
}
|
||||||
unsigned int estimateMaxBlocks() override
|
unsigned int estimateMaxBlocks() override
|
||||||
{
|
{
|
||||||
if (!m_node.fee_estimator) return 0;
|
if (!m_node.forecasterman) return 0;
|
||||||
return m_node.fee_estimator->HighestTargetTracked(FeeEstimateHorizon::LONG_HALFLIFE);
|
return m_node.forecasterman->GetBlockPolicyEstimator()->HighestTargetTracked(FeeEstimateHorizon::LONG_HALFLIFE);
|
||||||
}
|
}
|
||||||
CFeeRate mempoolMinFee() override
|
CFeeRate mempoolMinFee() override
|
||||||
{
|
{
|
||||||
|
@ -3,13 +3,14 @@
|
|||||||
// Distributed under the MIT software license, see the accompanying
|
// Distributed under the MIT software license, see the accompanying
|
||||||
// file COPYING or http://www.opensource.org/licenses/mit-license.php.
|
// file COPYING or http://www.opensource.org/licenses/mit-license.php.
|
||||||
|
|
||||||
#include <policy/fees.h>
|
#include <policy/fees/block_policy_estimator.h>
|
||||||
|
|
||||||
#include <common/system.h>
|
#include <common/system.h>
|
||||||
#include <consensus/amount.h>
|
#include <consensus/amount.h>
|
||||||
#include <kernel/mempool_entry.h>
|
#include <kernel/mempool_entry.h>
|
||||||
#include <logging.h>
|
#include <logging.h>
|
||||||
#include <policy/feerate.h>
|
#include <policy/feerate.h>
|
||||||
|
#include <policy/fees/forecaster_util.h>
|
||||||
#include <primitives/transaction.h>
|
#include <primitives/transaction.h>
|
||||||
#include <random.h>
|
#include <random.h>
|
||||||
#include <serialize.h>
|
#include <serialize.h>
|
||||||
@ -540,7 +541,7 @@ bool CBlockPolicyEstimator::_removeTx(const uint256& hash, bool inBlock)
|
|||||||
}
|
}
|
||||||
|
|
||||||
CBlockPolicyEstimator::CBlockPolicyEstimator(const fs::path& estimation_filepath, const bool read_stale_estimates)
|
CBlockPolicyEstimator::CBlockPolicyEstimator(const fs::path& estimation_filepath, const bool read_stale_estimates)
|
||||||
: m_estimation_filepath{estimation_filepath}
|
: Forecaster(ForecastType::BLOCK_POLICY), m_estimation_filepath{estimation_filepath}
|
||||||
{
|
{
|
||||||
static_assert(MIN_BUCKET_FEERATE > 0, "Min feerate must be nonzero");
|
static_assert(MIN_BUCKET_FEERATE > 0, "Min feerate must be nonzero");
|
||||||
size_t bucketIndex = 0;
|
size_t bucketIndex = 0;
|
||||||
@ -723,6 +724,29 @@ CFeeRate CBlockPolicyEstimator::estimateFee(int confTarget) const
|
|||||||
return estimateRawFee(confTarget, DOUBLE_SUCCESS_PCT, FeeEstimateHorizon::MED_HALFLIFE);
|
return estimateRawFee(confTarget, DOUBLE_SUCCESS_PCT, FeeEstimateHorizon::MED_HALFLIFE);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
ForecastResult CBlockPolicyEstimator::EstimateFee(ConfirmationTarget& target)
|
||||||
|
{
|
||||||
|
ForecastResult::ForecastResponse response;
|
||||||
|
response.forecaster = ForecastType::BLOCK_POLICY;
|
||||||
|
if (target.type != ConfirmationTargetType::BLOCKS) {
|
||||||
|
return ForecastResult(response, "Incorrect Confirmation target, expecting blocks");
|
||||||
|
}
|
||||||
|
FeeCalculation feeCalcConservative;
|
||||||
|
CFeeRate feerate_conservative{estimateSmartFee(target.value, &feeCalcConservative, /**conservative**/ true)};
|
||||||
|
FeeCalculation feeCalcEconomical;
|
||||||
|
CFeeRate feerate_economical{estimateSmartFee(target.value, &feeCalcEconomical, /**conservative**/ false)};
|
||||||
|
response.current_block_height = feeCalcEconomical.bestheight;
|
||||||
|
if (feerate_conservative == CFeeRate(0) || feerate_economical == CFeeRate(0)) {
|
||||||
|
return ForecastResult(response, "Insufficient data or no feerate found");
|
||||||
|
}
|
||||||
|
// Note: size can be any positive non-zero integer; the evaluated fee/size will result in the same fee rate,
|
||||||
|
// and we only care that the fee rate remains consistent.
|
||||||
|
int32_t size = 1000;
|
||||||
|
response.low_priority = FeeFrac(feerate_economical.GetFee(size), size);
|
||||||
|
response.high_priority = FeeFrac(feerate_conservative.GetFee(size), size);
|
||||||
|
return ForecastResult(response);
|
||||||
|
}
|
||||||
|
|
||||||
CFeeRate CBlockPolicyEstimator::estimateRawFee(int confTarget, double successThreshold, FeeEstimateHorizon horizon, EstimationResult* result) const
|
CFeeRate CBlockPolicyEstimator::estimateRawFee(int confTarget, double successThreshold, FeeEstimateHorizon horizon, EstimationResult* result) const
|
||||||
{
|
{
|
||||||
TxConfirmStats* stats = nullptr;
|
TxConfirmStats* stats = nullptr;
|
||||||
@ -874,6 +898,7 @@ CFeeRate CBlockPolicyEstimator::estimateSmartFee(int confTarget, FeeCalculation
|
|||||||
if (feeCalc) {
|
if (feeCalc) {
|
||||||
feeCalc->desiredTarget = confTarget;
|
feeCalc->desiredTarget = confTarget;
|
||||||
feeCalc->returnedTarget = confTarget;
|
feeCalc->returnedTarget = confTarget;
|
||||||
|
feeCalc->bestheight = nBestSeenHeight;
|
||||||
}
|
}
|
||||||
|
|
||||||
double median = -1;
|
double median = -1;
|
@ -2,11 +2,12 @@
|
|||||||
// Copyright (c) 2009-2022 The Bitcoin Core developers
|
// Copyright (c) 2009-2022 The Bitcoin Core developers
|
||||||
// Distributed under the MIT software license, see the accompanying
|
// Distributed under the MIT software license, see the accompanying
|
||||||
// file COPYING or http://www.opensource.org/licenses/mit-license.php.
|
// file COPYING or http://www.opensource.org/licenses/mit-license.php.
|
||||||
#ifndef BITCOIN_POLICY_FEES_H
|
#ifndef BITCOIN_POLICY_FEES_BLOCK_POLICY_ESTIMATOR_H
|
||||||
#define BITCOIN_POLICY_FEES_H
|
#define BITCOIN_POLICY_FEES_BLOCK_POLICY_ESTIMATOR_H
|
||||||
|
|
||||||
#include <consensus/amount.h>
|
#include <consensus/amount.h>
|
||||||
#include <policy/feerate.h>
|
#include <policy/feerate.h>
|
||||||
|
#include <policy/fees/forecaster.h>
|
||||||
#include <random.h>
|
#include <random.h>
|
||||||
#include <sync.h>
|
#include <sync.h>
|
||||||
#include <threadsafety.h>
|
#include <threadsafety.h>
|
||||||
@ -36,10 +37,14 @@ static constexpr std::chrono::hours MAX_FILE_AGE{60};
|
|||||||
static constexpr bool DEFAULT_ACCEPT_STALE_FEE_ESTIMATES{false};
|
static constexpr bool DEFAULT_ACCEPT_STALE_FEE_ESTIMATES{false};
|
||||||
|
|
||||||
class AutoFile;
|
class AutoFile;
|
||||||
|
class ForecastResult;
|
||||||
class TxConfirmStats;
|
class TxConfirmStats;
|
||||||
|
|
||||||
|
struct ConfirmationTarget;
|
||||||
struct RemovedMempoolTransactionInfo;
|
struct RemovedMempoolTransactionInfo;
|
||||||
struct NewMempoolTransactionInfo;
|
struct NewMempoolTransactionInfo;
|
||||||
|
|
||||||
|
|
||||||
/* Identifier for each of the 3 different TxConfirmStats which will track
|
/* Identifier for each of the 3 different TxConfirmStats which will track
|
||||||
* history over different time horizons. */
|
* history over different time horizons. */
|
||||||
enum class FeeEstimateHorizon {
|
enum class FeeEstimateHorizon {
|
||||||
@ -95,6 +100,7 @@ struct FeeCalculation
|
|||||||
FeeReason reason = FeeReason::NONE;
|
FeeReason reason = FeeReason::NONE;
|
||||||
int desiredTarget = 0;
|
int desiredTarget = 0;
|
||||||
int returnedTarget = 0;
|
int returnedTarget = 0;
|
||||||
|
unsigned int bestheight{0};
|
||||||
};
|
};
|
||||||
|
|
||||||
/** \class CBlockPolicyEstimator
|
/** \class CBlockPolicyEstimator
|
||||||
@ -145,7 +151,7 @@ struct FeeCalculation
|
|||||||
* a certain number of blocks. Every time a block is added to the best chain, this class records
|
* a certain number of blocks. Every time a block is added to the best chain, this class records
|
||||||
* stats on the transactions included in that block
|
* stats on the transactions included in that block
|
||||||
*/
|
*/
|
||||||
class CBlockPolicyEstimator : public CValidationInterface
|
class CBlockPolicyEstimator : public Forecaster, public CValidationInterface
|
||||||
{
|
{
|
||||||
private:
|
private:
|
||||||
/** Track confirm delays up to 12 blocks for short horizon */
|
/** Track confirm delays up to 12 blocks for short horizon */
|
||||||
@ -271,6 +277,10 @@ protected:
|
|||||||
void MempoolTransactionsRemovedForBlock(const std::vector<RemovedMempoolTransactionInfo>& txs_removed_for_block, unsigned int nBlockHeight) override
|
void MempoolTransactionsRemovedForBlock(const std::vector<RemovedMempoolTransactionInfo>& txs_removed_for_block, unsigned int nBlockHeight) override
|
||||||
EXCLUSIVE_LOCKS_REQUIRED(!m_cs_fee_estimator);
|
EXCLUSIVE_LOCKS_REQUIRED(!m_cs_fee_estimator);
|
||||||
|
|
||||||
|
/** Overridden from Forecaster. */
|
||||||
|
ForecastResult EstimateFee(ConfirmationTarget& target) override
|
||||||
|
EXCLUSIVE_LOCKS_REQUIRED(!m_cs_fee_estimator);
|
||||||
|
|
||||||
private:
|
private:
|
||||||
mutable Mutex m_cs_fee_estimator;
|
mutable Mutex m_cs_fee_estimator;
|
||||||
|
|
||||||
@ -342,4 +352,4 @@ private:
|
|||||||
FastRandomContext& insecure_rand GUARDED_BY(m_insecure_rand_mutex);
|
FastRandomContext& insecure_rand GUARDED_BY(m_insecure_rand_mutex);
|
||||||
};
|
};
|
||||||
|
|
||||||
#endif // BITCOIN_POLICY_FEES_H
|
#endif // BITCOIN_POLICY_FEES_BLOCK_POLICY_ESTIMATOR_H
|
@ -2,7 +2,7 @@
|
|||||||
// Distributed under the MIT software license, see the accompanying
|
// Distributed under the MIT software license, see the accompanying
|
||||||
// file COPYING or http://www.opensource.org/licenses/mit-license.php.
|
// file COPYING or http://www.opensource.org/licenses/mit-license.php.
|
||||||
|
|
||||||
#include <policy/fees_args.h>
|
#include <policy/fees/block_policy_estimator_args.h>
|
||||||
|
|
||||||
#include <common/args.h>
|
#include <common/args.h>
|
||||||
|
|
@ -2,8 +2,8 @@
|
|||||||
// Distributed under the MIT software license, see the accompanying
|
// Distributed under the MIT software license, see the accompanying
|
||||||
// file COPYING or http://www.opensource.org/licenses/mit-license.php.
|
// file COPYING or http://www.opensource.org/licenses/mit-license.php.
|
||||||
|
|
||||||
#ifndef BITCOIN_POLICY_FEES_ARGS_H
|
#ifndef BITCOIN_POLICY_FEES_BLOCK_POLICY_ESTIMATOR_ARGS_H
|
||||||
#define BITCOIN_POLICY_FEES_ARGS_H
|
#define BITCOIN_POLICY_FEES_BLOCK_POLICY_ESTIMATOR_ARGS_H
|
||||||
|
|
||||||
#include <util/fs.h>
|
#include <util/fs.h>
|
||||||
|
|
||||||
@ -12,4 +12,4 @@ class ArgsManager;
|
|||||||
/** @return The fee estimates data file path. */
|
/** @return The fee estimates data file path. */
|
||||||
fs::path FeeestPath(const ArgsManager& argsman);
|
fs::path FeeestPath(const ArgsManager& argsman);
|
||||||
|
|
||||||
#endif // BITCOIN_POLICY_FEES_ARGS_H
|
#endif // BITCOIN_POLICY_FEES_BLOCK_POLICY_ESTIMATOR_ARGS_H
|
36
src/policy/fees/forecaster.h
Normal file
36
src/policy/fees/forecaster.h
Normal file
@ -0,0 +1,36 @@
|
|||||||
|
// Copyright (c) 2025 The Bitcoin Core developers
|
||||||
|
// Distributed under the MIT software license. See the accompanying
|
||||||
|
// file COPYING or http://www.opensource.org/licenses/mit-license.php.
|
||||||
|
|
||||||
|
#ifndef BITCOIN_POLICY_FEES_FORECASTER_H
|
||||||
|
#define BITCOIN_POLICY_FEES_FORECASTER_H
|
||||||
|
|
||||||
|
#include <policy/fees/forecaster_util.h>
|
||||||
|
|
||||||
|
/** \class Forecaster
|
||||||
|
* Abstract base class for fee rate forecasters.
|
||||||
|
*/
|
||||||
|
class Forecaster
|
||||||
|
{
|
||||||
|
protected:
|
||||||
|
const ForecastType m_forecastType;
|
||||||
|
|
||||||
|
public:
|
||||||
|
Forecaster(ForecastType forecastType) : m_forecastType(forecastType) {}
|
||||||
|
|
||||||
|
ForecastType GetForecastType() const { return m_forecastType; }
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Forecast the fee rate required for package to confirm within
|
||||||
|
* a specified target.
|
||||||
|
*
|
||||||
|
* This pure virtual function must be overridden by derived classes.
|
||||||
|
* @param target The confirmation target for which to forecast the feerate for.
|
||||||
|
* @return ForecastResult containing the forecasted fee rate.
|
||||||
|
*/
|
||||||
|
virtual ForecastResult EstimateFee(ConfirmationTarget& target) = 0;
|
||||||
|
|
||||||
|
virtual ~Forecaster() = default;
|
||||||
|
};
|
||||||
|
|
||||||
|
#endif // BITCOIN_POLICY_FEES_FORECASTER_H
|
67
src/policy/fees/forecaster_man.cpp
Normal file
67
src/policy/fees/forecaster_man.cpp
Normal file
@ -0,0 +1,67 @@
|
|||||||
|
// Copyright (c) 2025 The Bitcoin Core developers
|
||||||
|
// Distributed under the MIT software license. See the accompanying
|
||||||
|
// file COPYING or http://www.opensource.org/licenses/mit-license.php.
|
||||||
|
|
||||||
|
#include <logging.h>
|
||||||
|
#include <policy/fees/block_policy_estimator.h>
|
||||||
|
#include <policy/fees/forecaster.h>
|
||||||
|
#include <policy/fees/forecaster_man.h>
|
||||||
|
#include <policy/fees/forecaster_util.h>
|
||||||
|
|
||||||
|
#include <algorithm>
|
||||||
|
#include <utility>
|
||||||
|
|
||||||
|
void FeeRateForecasterManager::RegisterForecaster(std::shared_ptr<Forecaster> forecaster)
|
||||||
|
{
|
||||||
|
forecasters.emplace(forecaster->GetForecastType(), forecaster);
|
||||||
|
}
|
||||||
|
|
||||||
|
CBlockPolicyEstimator* FeeRateForecasterManager::GetBlockPolicyEstimator()
|
||||||
|
{
|
||||||
|
Assume(forecasters.contains(ForecastType::BLOCK_POLICY));
|
||||||
|
Forecaster* block_policy_estimator = forecasters.find(ForecastType::BLOCK_POLICY)->second.get();
|
||||||
|
return dynamic_cast<CBlockPolicyEstimator*>(block_policy_estimator);
|
||||||
|
}
|
||||||
|
|
||||||
|
std::pair<std::optional<ForecastResult>, std::vector<std::string>> FeeRateForecasterManager::GetFeeRateForecast(ConfirmationTarget& target)
|
||||||
|
{
|
||||||
|
std::vector<std::string> 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};
|
||||||
|
}
|
57
src/policy/fees/forecaster_man.h
Normal file
57
src/policy/fees/forecaster_man.h
Normal file
@ -0,0 +1,57 @@
|
|||||||
|
// Copyright (c) 2025 The Bitcoin Core developers
|
||||||
|
// Distributed under the MIT software license. See the accompanying
|
||||||
|
// file COPYING or http://www.opensource.org/licenses/mit-license.php.
|
||||||
|
|
||||||
|
#ifndef BITCOIN_POLICY_FEES_FORECASTER_MAN_H
|
||||||
|
#define BITCOIN_POLICY_FEES_FORECASTER_MAN_H
|
||||||
|
|
||||||
|
#include <memory>
|
||||||
|
#include <optional>
|
||||||
|
#include <unordered_map>
|
||||||
|
|
||||||
|
class CBlockPolicyEstimator;
|
||||||
|
class Forecaster;
|
||||||
|
class ForecastResult;
|
||||||
|
|
||||||
|
struct ConfirmationTarget;
|
||||||
|
|
||||||
|
enum class ForecastType;
|
||||||
|
|
||||||
|
/** \class FeeRateForecasterManager
|
||||||
|
* Module for managing and utilising multiple fee rate forecasters to provide a forecast for a target.
|
||||||
|
*
|
||||||
|
* The FeeRateForecasterManager class allows for the registration of multiple fee rate
|
||||||
|
* forecasters.
|
||||||
|
*/
|
||||||
|
class FeeRateForecasterManager
|
||||||
|
{
|
||||||
|
private:
|
||||||
|
//! Map of all registered forecasters to their shared pointers.
|
||||||
|
std::unordered_map<ForecastType, std::shared_ptr<Forecaster>> forecasters;
|
||||||
|
|
||||||
|
public:
|
||||||
|
/**
|
||||||
|
* Register a forecaster to provide fee rate estimates.
|
||||||
|
*
|
||||||
|
* @param[in] forecaster shared pointer to a Forecaster instance.
|
||||||
|
*/
|
||||||
|
void RegisterForecaster(std::shared_ptr<Forecaster> forecaster);
|
||||||
|
|
||||||
|
/*
|
||||||
|
* 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::optional<ForecastResult>, std::vector<std::string>> GetFeeRateForecast(ConfirmationTarget& target);
|
||||||
|
};
|
||||||
|
|
||||||
|
#endif // BITCOIN_POLICY_FEES_FORECASTER_MAN_H
|
57
src/policy/fees/forecaster_util.cpp
Normal file
57
src/policy/fees/forecaster_util.cpp
Normal file
@ -0,0 +1,57 @@
|
|||||||
|
// Copyright (c) 2025 The Bitcoin Core developers
|
||||||
|
// Distributed under the MIT software license. See the accompanying
|
||||||
|
// file COPYING or http://www.opensource.org/licenses/mit-license.php.
|
||||||
|
|
||||||
|
#include <policy/fees/forecaster_util.h>
|
||||||
|
#include <policy/policy.h>
|
||||||
|
|
||||||
|
#include <algorithm>
|
||||||
|
|
||||||
|
Percentiles CalculatePercentiles(const std::vector<FeeFrac>& package_feerates, const int32_t total_weight)
|
||||||
|
{
|
||||||
|
if (package_feerates.empty()) return Percentiles{};
|
||||||
|
int32_t accumulated_weight{0};
|
||||||
|
const int32_t p25_weight = 0.25 * total_weight;
|
||||||
|
const int32_t p50_weight = 0.50 * total_weight;
|
||||||
|
const int32_t p75_weight = 0.75 * total_weight;
|
||||||
|
const int32_t p95_weight = 0.95 * total_weight;
|
||||||
|
auto last_tracked_feerate = package_feerates.front();
|
||||||
|
auto percentiles = Percentiles{};
|
||||||
|
|
||||||
|
// Process histogram entries while maintaining monotonicity
|
||||||
|
for (const auto& curr_feerate : package_feerates) {
|
||||||
|
accumulated_weight += curr_feerate.size * WITNESS_SCALE_FACTOR;
|
||||||
|
// Maintain monotonicity by taking the minimum between the current and last tracked fee rate
|
||||||
|
last_tracked_feerate = std::min(last_tracked_feerate, curr_feerate, [](const FeeFrac& a, const FeeFrac& b) {
|
||||||
|
return std::is_lt(FeeRateCompare(a, b));
|
||||||
|
});
|
||||||
|
if (accumulated_weight >= p25_weight && percentiles.p25.IsEmpty()) {
|
||||||
|
percentiles.p25 = last_tracked_feerate;
|
||||||
|
}
|
||||||
|
if (accumulated_weight >= p50_weight && percentiles.p50.IsEmpty()) {
|
||||||
|
percentiles.p50 = last_tracked_feerate;
|
||||||
|
}
|
||||||
|
if (accumulated_weight >= p75_weight && percentiles.p75.IsEmpty()) {
|
||||||
|
percentiles.p75 = last_tracked_feerate;
|
||||||
|
}
|
||||||
|
if (accumulated_weight >= p95_weight && percentiles.p95.IsEmpty()) {
|
||||||
|
percentiles.p95 = last_tracked_feerate;
|
||||||
|
break; // Early exit once all percentiles are calculated
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// Return empty percentiles if we couldn't calculate the 95th percentile.
|
||||||
|
return percentiles.p95.IsEmpty() ? Percentiles{} : percentiles;
|
||||||
|
}
|
||||||
|
|
||||||
|
std::string forecastTypeToString(ForecastType forecastType)
|
||||||
|
{
|
||||||
|
switch (forecastType) {
|
||||||
|
case ForecastType::MEMPOOL_FORECAST:
|
||||||
|
return std::string("Mempool Forecast");
|
||||||
|
case ForecastType::BLOCK_POLICY:
|
||||||
|
return std::string("Block Policy Estimator");
|
||||||
|
}
|
||||||
|
// no default case, so the compiler can warn about missing cases
|
||||||
|
assert(false);
|
||||||
|
}
|
161
src/policy/fees/forecaster_util.h
Normal file
161
src/policy/fees/forecaster_util.h
Normal file
@ -0,0 +1,161 @@
|
|||||||
|
// Copyright (c) 2025 The Bitcoin Core developers
|
||||||
|
// Distributed under the MIT software license, see the accompanying
|
||||||
|
// file COPYING or http://www.opensource.org/licenses/mit-license.php.
|
||||||
|
|
||||||
|
#ifndef BITCOIN_POLICY_FEES_FORECASTER_UTIL_H
|
||||||
|
#define BITCOIN_POLICY_FEES_FORECASTER_UTIL_H
|
||||||
|
|
||||||
|
#include <util/feefrac.h>
|
||||||
|
|
||||||
|
#include <optional>
|
||||||
|
#include <string>
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @enum ForecastType
|
||||||
|
* Identifier for fee rate forecasters.
|
||||||
|
*/
|
||||||
|
enum class ForecastType {
|
||||||
|
BLOCK_POLICY,
|
||||||
|
MEMPOOL_FORECAST,
|
||||||
|
};
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @class ForecastResult
|
||||||
|
* Represents the response returned by a fee rate forecaster.
|
||||||
|
*/
|
||||||
|
class ForecastResult
|
||||||
|
{
|
||||||
|
public:
|
||||||
|
/**
|
||||||
|
* @struct ForecastResponse
|
||||||
|
* Contains fee rate forecast metadata.
|
||||||
|
*/
|
||||||
|
struct ForecastResponse {
|
||||||
|
/**
|
||||||
|
* An estimated fee rate as a result of a forecast for economical transactions.
|
||||||
|
* This value is sufficient to confirm a package within the specified target.
|
||||||
|
*/
|
||||||
|
FeeFrac low_priority;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* An estimated fee rate as a result of forecast for high-priority transactions.
|
||||||
|
* This value is for users willing to pay more to increase the likelihood of
|
||||||
|
* confirmation within the specified target.
|
||||||
|
*/
|
||||||
|
FeeFrac high_priority;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* The chain tip at which the forecast was made.
|
||||||
|
*/
|
||||||
|
unsigned int current_block_height{0};
|
||||||
|
/* This identifies which forecaster is providing this feerate forecast */
|
||||||
|
ForecastType forecaster;
|
||||||
|
};
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Default ForecastResult constructor.
|
||||||
|
*/
|
||||||
|
ForecastResult() {}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Constructs a ForecastResult object.
|
||||||
|
* @param response The forecast response data.
|
||||||
|
* @param error An optional error message (default: nullopt).
|
||||||
|
*/
|
||||||
|
ForecastResult(ForecastResponse response, std::optional<std::string> error = std::nullopt)
|
||||||
|
: m_response(std::move(response)), m_error(std::move(error))
|
||||||
|
{
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Checks if the forecast response is empty.
|
||||||
|
* @return true if both low and high priority forecast estimates are empty, false otherwise.
|
||||||
|
*/
|
||||||
|
bool Empty() const
|
||||||
|
{
|
||||||
|
return m_response.low_priority.IsEmpty() && m_response.high_priority.IsEmpty();
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Overloaded less than operator which compares two ForecastResult objects based on
|
||||||
|
* their high priority estimates.
|
||||||
|
* @param other The other ForecastResult object to compare with.
|
||||||
|
* @return true if the current object's high priority estimate is less than the other, false otherwise.
|
||||||
|
*/
|
||||||
|
bool operator<(const ForecastResult& other) const
|
||||||
|
{
|
||||||
|
return m_response.high_priority << other.m_response.high_priority;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Retrieves the forecast response.
|
||||||
|
* @return A reference to the forecast response metadata.
|
||||||
|
*/
|
||||||
|
const ForecastResponse& GetResponse() const
|
||||||
|
{
|
||||||
|
return m_response;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Retrieves the error message, if any.
|
||||||
|
* @return An optional string containing the error message.
|
||||||
|
*/
|
||||||
|
const std::optional<std::string>& GetError() const
|
||||||
|
{
|
||||||
|
return m_error;
|
||||||
|
}
|
||||||
|
|
||||||
|
private:
|
||||||
|
ForecastResponse m_response; ///< The forecast response data.
|
||||||
|
std::optional<std::string> m_error; ///< Optional error message.
|
||||||
|
};
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @enum ConfirmationTargetType
|
||||||
|
* Defines the types of confirmation targets for fee rate forecasters.
|
||||||
|
*/
|
||||||
|
enum class ConfirmationTargetType {
|
||||||
|
BLOCKS, /**< Forecasters providing estimates for a specific number of blocks use this type. */
|
||||||
|
};
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @struct ConfirmationTarget
|
||||||
|
* Represents the input for a parameter of fee rate forecaster.
|
||||||
|
*/
|
||||||
|
struct ConfirmationTarget {
|
||||||
|
unsigned int value;
|
||||||
|
ConfirmationTargetType type;
|
||||||
|
};
|
||||||
|
|
||||||
|
// Block percentiles fee rate (in sat/kvB).
|
||||||
|
struct Percentiles {
|
||||||
|
FeeFrac p25; // 25th percentile
|
||||||
|
FeeFrac p50; // 50th percentile
|
||||||
|
FeeFrac p75; // 75th percentile
|
||||||
|
FeeFrac p95; // 5th percentile
|
||||||
|
|
||||||
|
Percentiles() = default;
|
||||||
|
|
||||||
|
bool empty() const
|
||||||
|
{
|
||||||
|
return p25.IsEmpty() && p50.IsEmpty() && p75.IsEmpty() && p95.IsEmpty();
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Calculates the percentile fee rates from a given vector of fee rates.
|
||||||
|
*
|
||||||
|
* This function assumes that the fee rates in the input vector are sorted in descending order
|
||||||
|
* based on mining score priority. It ensures that the calculated percentile fee rates
|
||||||
|
* are monotonically decreasing by filtering out outliers. Outliers can occur when
|
||||||
|
* the mining score of a transaction increases due to the inclusion of its ancestors
|
||||||
|
* in different transaction packages.
|
||||||
|
*
|
||||||
|
* @param[in] package_feerates A vector containing fee rates and their corresponding virtual sizes.
|
||||||
|
* @return Percentiles object containing the calculated percentile fee rates.
|
||||||
|
*/
|
||||||
|
Percentiles CalculatePercentiles(const std::vector<FeeFrac>& package_feerates, const int32_t total_weight);
|
||||||
|
|
||||||
|
std::string forecastTypeToString(ForecastType forecastType);
|
||||||
|
|
||||||
|
#endif // BITCOIN_POLICY_FEES_FORECASTER_UTIL_H
|
71
src/policy/fees/mempool_forecaster.cpp
Normal file
71
src/policy/fees/mempool_forecaster.cpp
Normal file
@ -0,0 +1,71 @@
|
|||||||
|
// Copyright (c) 2024 The Bitcoin Core developers
|
||||||
|
// Distributed under the MIT software license. See the accompanying
|
||||||
|
// file COPYING or http://www.opensource.org/licenses/mit-license.php.
|
||||||
|
|
||||||
|
#include <logging.h>
|
||||||
|
#include <node/miner.h>
|
||||||
|
#include <policy/fees/forecaster.h>
|
||||||
|
#include <policy/fees/forecaster_util.h>
|
||||||
|
#include <policy/fees/mempool_forecaster.h>
|
||||||
|
#include <policy/policy.h>
|
||||||
|
#include <validation.h>
|
||||||
|
|
||||||
|
ForecastResult MemPoolForecaster::EstimateFee(ConfirmationTarget& target)
|
||||||
|
{
|
||||||
|
ForecastResult::ForecastResponse response;
|
||||||
|
response.forecaster = m_forecastType;
|
||||||
|
LOCK2(cs_main, m_mempool->cs);
|
||||||
|
auto activeTip = m_chainstate->m_chainman.ActiveTip();
|
||||||
|
if (!activeTip) {
|
||||||
|
return ForecastResult(response, "No active chainstate available");
|
||||||
|
}
|
||||||
|
response.current_block_height = static_cast<unsigned int>(activeTip->nHeight);
|
||||||
|
|
||||||
|
if (target.type != ConfirmationTargetType::BLOCKS) {
|
||||||
|
return ForecastResult(response, "Forecaster can only provide an estimate for block targets");
|
||||||
|
}
|
||||||
|
if (target.value > MEMPOOL_FORECAST_MAX_TARGET) {
|
||||||
|
return ForecastResult(response, strprintf(
|
||||||
|
"Confirmation target %s exceeds the maximum limit of %s. mempool conditions might change, "
|
||||||
|
"making forecasts above %s block may be unreliable",
|
||||||
|
target.value, MEMPOOL_FORECAST_MAX_TARGET, MEMPOOL_FORECAST_MAX_TARGET));
|
||||||
|
}
|
||||||
|
|
||||||
|
const auto cached_estimate = cache.get();
|
||||||
|
if (cached_estimate) {
|
||||||
|
response.low_priority = cached_estimate->p75;
|
||||||
|
response.high_priority = cached_estimate->p50;
|
||||||
|
return ForecastResult(response);
|
||||||
|
}
|
||||||
|
|
||||||
|
node::BlockAssembler::Options options;
|
||||||
|
options.test_block_validity = false;
|
||||||
|
node::BlockAssembler assembler(*m_chainstate, m_mempool, options);
|
||||||
|
|
||||||
|
const auto pblocktemplate = assembler.CreateNewBlock();
|
||||||
|
const auto& m_package_feerates = pblocktemplate->m_package_feerates;
|
||||||
|
if (m_package_feerates.empty()) {
|
||||||
|
return ForecastResult(response, "No enough transactions in the mempool to provide a fee rate forecast");
|
||||||
|
}
|
||||||
|
|
||||||
|
const auto percentiles = CalculatePercentiles(m_package_feerates, DEFAULT_BLOCK_MAX_WEIGHT);
|
||||||
|
if (percentiles.empty()) {
|
||||||
|
return ForecastResult(response, "Forecaster unable to provide an estimate due to insufficient data");
|
||||||
|
}
|
||||||
|
|
||||||
|
LogDebug(BCLog::ESTIMATEFEE,
|
||||||
|
"FeeEstimation: %s: Block height %s, 25th percentile fee rate: %s %s/kvB, "
|
||||||
|
"50th percentile fee rate: %s %s/kvB, 75th percentile fee rate: %s %s/kvB, "
|
||||||
|
"95th percentile fee rate: %s %s/kvB\n",
|
||||||
|
forecastTypeToString(m_forecastType), response.current_block_height,
|
||||||
|
CFeeRate(percentiles.p25.fee, percentiles.p25.size).GetFeePerK(), CURRENCY_ATOM,
|
||||||
|
CFeeRate(percentiles.p50.fee, percentiles.p50.size).GetFeePerK(), CURRENCY_ATOM,
|
||||||
|
CFeeRate(percentiles.p75.fee, percentiles.p75.size).GetFeePerK(), CURRENCY_ATOM,
|
||||||
|
CFeeRate(percentiles.p95.fee, percentiles.p95.size).GetFeePerK(), CURRENCY_ATOM);
|
||||||
|
|
||||||
|
cache.update(percentiles);
|
||||||
|
response.low_priority = percentiles.p75;
|
||||||
|
response.high_priority = percentiles.p50;
|
||||||
|
|
||||||
|
return ForecastResult(response);
|
||||||
|
}
|
92
src/policy/fees/mempool_forecaster.h
Normal file
92
src/policy/fees/mempool_forecaster.h
Normal file
@ -0,0 +1,92 @@
|
|||||||
|
// Copyright (c) 2024 The Bitcoin Core developers
|
||||||
|
// Distributed under the MIT software license. See the accompanying
|
||||||
|
// file COPYING or http://www.opensource.org/licenses/mit-license.php.
|
||||||
|
|
||||||
|
#ifndef BITCOIN_POLICY_FEES_MEMPOOL_FORECASTER_H
|
||||||
|
#define BITCOIN_POLICY_FEES_MEMPOOL_FORECASTER_H
|
||||||
|
|
||||||
|
|
||||||
|
#include <logging.h>
|
||||||
|
#include <policy/fees/forecaster.h>
|
||||||
|
#include <policy/fees/forecaster_util.h>
|
||||||
|
#include <sync.h>
|
||||||
|
#include <util/time.h>
|
||||||
|
|
||||||
|
#include <chrono>
|
||||||
|
|
||||||
|
class Chainstate;
|
||||||
|
class CTxMemPool;
|
||||||
|
|
||||||
|
struct ConfirmationTarget;
|
||||||
|
|
||||||
|
// Fee rate estimates above this confirmation target are not reliable,
|
||||||
|
// mempool condition might likely change.
|
||||||
|
constexpr int MEMPOOL_FORECAST_MAX_TARGET{2};
|
||||||
|
constexpr std::chrono::seconds CACHE_LIFE{30};
|
||||||
|
|
||||||
|
/**
|
||||||
|
* CachedMempoolEstimates holds a cache of recent forecast.
|
||||||
|
* We only provide fresh estimates if the last cached fee rate
|
||||||
|
* forecast ages more than CACHE_LIFE.
|
||||||
|
*/
|
||||||
|
struct CachedMempoolEstimates {
|
||||||
|
private:
|
||||||
|
mutable Mutex cache_mutex;
|
||||||
|
Percentiles fee_estimate GUARDED_BY(cache_mutex);
|
||||||
|
NodeClock::time_point last_updated GUARDED_BY(cache_mutex){NodeClock::now() - CACHE_LIFE - std::chrono::seconds(1)};
|
||||||
|
|
||||||
|
bool isStale() const EXCLUSIVE_LOCKS_REQUIRED(cache_mutex)
|
||||||
|
{
|
||||||
|
AssertLockHeld(cache_mutex);
|
||||||
|
return (last_updated + CACHE_LIFE) < NodeClock::now();
|
||||||
|
}
|
||||||
|
|
||||||
|
public:
|
||||||
|
CachedMempoolEstimates() = default;
|
||||||
|
CachedMempoolEstimates(const CachedMempoolEstimates&) = delete;
|
||||||
|
CachedMempoolEstimates& operator=(const CachedMempoolEstimates&) = delete;
|
||||||
|
|
||||||
|
std::optional<Percentiles> get() const EXCLUSIVE_LOCKS_REQUIRED(!cache_mutex)
|
||||||
|
{
|
||||||
|
LOCK(cache_mutex);
|
||||||
|
if (isStale()) return std::nullopt;
|
||||||
|
LogDebug(BCLog::ESTIMATEFEE, "%s: cache is not stale, using cached value\n", forecastTypeToString(ForecastType::MEMPOOL_FORECAST));
|
||||||
|
return fee_estimate;
|
||||||
|
}
|
||||||
|
|
||||||
|
void update(const Percentiles& new_fee_estimate) EXCLUSIVE_LOCKS_REQUIRED(!cache_mutex)
|
||||||
|
{
|
||||||
|
LOCK(cache_mutex);
|
||||||
|
fee_estimate = new_fee_estimate;
|
||||||
|
last_updated = NodeClock::now();
|
||||||
|
LogDebug(BCLog::ESTIMATEFEE, "%s: updated cache\n", forecastTypeToString(ForecastType::MEMPOOL_FORECAST));
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
/** \class MemPoolForecaster
|
||||||
|
* This fee estimate forecaster estimates the fee rate that a transaction will pay
|
||||||
|
* to be included in a block as soon as possible.
|
||||||
|
* It uses the unconfirmed transactions in the mempool to generate the next block template
|
||||||
|
* that will likely be mined.
|
||||||
|
* The percentile fee rate's are computed, and the bottom 25th percentile and 50th percentile fee rate's are returned.
|
||||||
|
*/
|
||||||
|
class MemPoolForecaster : public Forecaster
|
||||||
|
{
|
||||||
|
public:
|
||||||
|
MemPoolForecaster(const CTxMemPool* mempool, Chainstate* chainstate)
|
||||||
|
: Forecaster(ForecastType::MEMPOOL_FORECAST), m_mempool(mempool), m_chainstate(chainstate) {};
|
||||||
|
~MemPoolForecaster() = default;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Estimate the fee rate from mempool transactions given a confirmation target.
|
||||||
|
* @param[in] target The confirmation target to provide estimate for.
|
||||||
|
* @return The forecasted fee rates.
|
||||||
|
*/
|
||||||
|
ForecastResult EstimateFee(ConfirmationTarget& target) override;
|
||||||
|
|
||||||
|
private:
|
||||||
|
const CTxMemPool* m_mempool;
|
||||||
|
Chainstate* m_chainstate;
|
||||||
|
mutable CachedMempoolEstimates cache;
|
||||||
|
};
|
||||||
|
#endif // BITCOIN_POLICY_FEES_MEMPOOL_FORECASTER_H
|
@ -21,7 +21,7 @@
|
|||||||
#include <key_io.h>
|
#include <key_io.h>
|
||||||
#include <node/interface_ui.h>
|
#include <node/interface_ui.h>
|
||||||
#include <node/types.h>
|
#include <node/types.h>
|
||||||
#include <policy/fees.h>
|
#include <policy/fees/block_policy_estimator.h>
|
||||||
#include <txmempool.h>
|
#include <txmempool.h>
|
||||||
#include <validation.h>
|
#include <validation.h>
|
||||||
#include <wallet/coincontrol.h>
|
#include <wallet/coincontrol.h>
|
||||||
|
@ -7,7 +7,8 @@
|
|||||||
#include <core_io.h>
|
#include <core_io.h>
|
||||||
#include <node/context.h>
|
#include <node/context.h>
|
||||||
#include <policy/feerate.h>
|
#include <policy/feerate.h>
|
||||||
#include <policy/fees.h>
|
#include <policy/fees/block_policy_estimator.h>
|
||||||
|
#include <policy/fees/forecaster_man.h>
|
||||||
#include <rpc/protocol.h>
|
#include <rpc/protocol.h>
|
||||||
#include <rpc/request.h>
|
#include <rpc/request.h>
|
||||||
#include <rpc/server.h>
|
#include <rpc/server.h>
|
||||||
@ -59,12 +60,12 @@ static RPCHelpMan estimatesmartfee()
|
|||||||
},
|
},
|
||||||
[&](const RPCHelpMan& self, const JSONRPCRequest& request) -> UniValue
|
[&](const RPCHelpMan& self, const JSONRPCRequest& request) -> UniValue
|
||||||
{
|
{
|
||||||
CBlockPolicyEstimator& fee_estimator = EnsureAnyFeeEstimator(request.context);
|
CBlockPolicyEstimator& block_policy_fee_estimator = *(EnsureAnyForecasterMan(request.context).GetBlockPolicyEstimator());
|
||||||
const NodeContext& node = EnsureAnyNodeContext(request.context);
|
const NodeContext& node = EnsureAnyNodeContext(request.context);
|
||||||
const CTxMemPool& mempool = EnsureMemPool(node);
|
const CTxMemPool& mempool = EnsureMemPool(node);
|
||||||
|
|
||||||
CHECK_NONFATAL(mempool.m_opts.signals)->SyncWithValidationInterfaceQueue();
|
CHECK_NONFATAL(mempool.m_opts.signals)->SyncWithValidationInterfaceQueue();
|
||||||
unsigned int max_target = fee_estimator.HighestTargetTracked(FeeEstimateHorizon::LONG_HALFLIFE);
|
unsigned int max_target = block_policy_fee_estimator.HighestTargetTracked(FeeEstimateHorizon::LONG_HALFLIFE);
|
||||||
unsigned int conf_target = ParseConfirmTarget(request.params[0], max_target);
|
unsigned int conf_target = ParseConfirmTarget(request.params[0], max_target);
|
||||||
bool conservative = false;
|
bool conservative = false;
|
||||||
if (!request.params[1].isNull()) {
|
if (!request.params[1].isNull()) {
|
||||||
@ -78,7 +79,7 @@ static RPCHelpMan estimatesmartfee()
|
|||||||
UniValue result(UniValue::VOBJ);
|
UniValue result(UniValue::VOBJ);
|
||||||
UniValue errors(UniValue::VARR);
|
UniValue errors(UniValue::VARR);
|
||||||
FeeCalculation feeCalc;
|
FeeCalculation feeCalc;
|
||||||
CFeeRate feeRate{fee_estimator.estimateSmartFee(conf_target, &feeCalc, conservative)};
|
CFeeRate feeRate{block_policy_fee_estimator.estimateSmartFee(conf_target, &feeCalc, conservative)};
|
||||||
if (feeRate != CFeeRate(0)) {
|
if (feeRate != CFeeRate(0)) {
|
||||||
CFeeRate min_mempool_feerate{mempool.GetMinFee()};
|
CFeeRate min_mempool_feerate{mempool.GetMinFee()};
|
||||||
CFeeRate min_relay_feerate{mempool.m_opts.min_relay_feerate};
|
CFeeRate min_relay_feerate{mempool.m_opts.min_relay_feerate};
|
||||||
@ -150,11 +151,11 @@ static RPCHelpMan estimaterawfee()
|
|||||||
},
|
},
|
||||||
[&](const RPCHelpMan& self, const JSONRPCRequest& request) -> UniValue
|
[&](const RPCHelpMan& self, const JSONRPCRequest& request) -> UniValue
|
||||||
{
|
{
|
||||||
CBlockPolicyEstimator& fee_estimator = EnsureAnyFeeEstimator(request.context);
|
CBlockPolicyEstimator& block_policy_fee_estimator = *(EnsureAnyForecasterMan(request.context).GetBlockPolicyEstimator());
|
||||||
const NodeContext& node = EnsureAnyNodeContext(request.context);
|
const NodeContext& node = EnsureAnyNodeContext(request.context);
|
||||||
|
|
||||||
CHECK_NONFATAL(node.validation_signals)->SyncWithValidationInterfaceQueue();
|
CHECK_NONFATAL(node.validation_signals)->SyncWithValidationInterfaceQueue();
|
||||||
unsigned int max_target = fee_estimator.HighestTargetTracked(FeeEstimateHorizon::LONG_HALFLIFE);
|
unsigned int max_target = block_policy_fee_estimator.HighestTargetTracked(FeeEstimateHorizon::LONG_HALFLIFE);
|
||||||
unsigned int conf_target = ParseConfirmTarget(request.params[0], max_target);
|
unsigned int conf_target = ParseConfirmTarget(request.params[0], max_target);
|
||||||
double threshold = 0.95;
|
double threshold = 0.95;
|
||||||
if (!request.params[1].isNull()) {
|
if (!request.params[1].isNull()) {
|
||||||
@ -171,9 +172,9 @@ static RPCHelpMan estimaterawfee()
|
|||||||
EstimationResult buckets;
|
EstimationResult buckets;
|
||||||
|
|
||||||
// Only output results for horizons which track the target
|
// Only output results for horizons which track the target
|
||||||
if (conf_target > fee_estimator.HighestTargetTracked(horizon)) continue;
|
if (conf_target > block_policy_fee_estimator.HighestTargetTracked(horizon)) continue;
|
||||||
|
|
||||||
feeRate = fee_estimator.estimateRawFee(conf_target, threshold, horizon, &buckets);
|
feeRate = block_policy_fee_estimator.estimateRawFee(conf_target, threshold, horizon, &buckets);
|
||||||
UniValue horizon_result(UniValue::VOBJ);
|
UniValue horizon_result(UniValue::VOBJ);
|
||||||
UniValue errors(UniValue::VARR);
|
UniValue errors(UniValue::VARR);
|
||||||
UniValue passbucket(UniValue::VOBJ);
|
UniValue passbucket(UniValue::VOBJ);
|
||||||
|
@ -9,7 +9,8 @@
|
|||||||
#include <net_processing.h>
|
#include <net_processing.h>
|
||||||
#include <node/context.h>
|
#include <node/context.h>
|
||||||
#include <node/miner.h>
|
#include <node/miner.h>
|
||||||
#include <policy/fees.h>
|
#include <policy/fees/block_policy_estimator.h>
|
||||||
|
#include <policy/fees/forecaster_man.h>
|
||||||
#include <pow.h>
|
#include <pow.h>
|
||||||
#include <rpc/protocol.h>
|
#include <rpc/protocol.h>
|
||||||
#include <rpc/request.h>
|
#include <rpc/request.h>
|
||||||
@ -84,19 +85,20 @@ ChainstateManager& EnsureAnyChainman(const std::any& context)
|
|||||||
return EnsureChainman(EnsureAnyNodeContext(context));
|
return EnsureChainman(EnsureAnyNodeContext(context));
|
||||||
}
|
}
|
||||||
|
|
||||||
CBlockPolicyEstimator& EnsureFeeEstimator(const NodeContext& node)
|
FeeRateForecasterManager& EnsureForecasterMan(const NodeContext& node)
|
||||||
{
|
{
|
||||||
if (!node.fee_estimator) {
|
if (!node.forecasterman) {
|
||||||
throw JSONRPCError(RPC_INTERNAL_ERROR, "Fee estimation disabled");
|
throw JSONRPCError(RPC_INTERNAL_ERROR, "Fee estimation disabled");
|
||||||
}
|
}
|
||||||
return *node.fee_estimator;
|
return *node.forecasterman.get();
|
||||||
}
|
}
|
||||||
|
|
||||||
CBlockPolicyEstimator& EnsureAnyFeeEstimator(const std::any& context)
|
FeeRateForecasterManager& EnsureAnyForecasterMan(const std::any& context)
|
||||||
{
|
{
|
||||||
return EnsureFeeEstimator(EnsureAnyNodeContext(context));
|
return EnsureForecasterMan(EnsureAnyNodeContext(context));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
CConnman& EnsureConnman(const NodeContext& node)
|
CConnman& EnsureConnman(const NodeContext& node)
|
||||||
{
|
{
|
||||||
if (!node.connman) {
|
if (!node.connman) {
|
||||||
|
@ -12,7 +12,7 @@
|
|||||||
class AddrMan;
|
class AddrMan;
|
||||||
class ArgsManager;
|
class ArgsManager;
|
||||||
class CBlockIndex;
|
class CBlockIndex;
|
||||||
class CBlockPolicyEstimator;
|
class FeeRateForecasterManager;
|
||||||
class CConnman;
|
class CConnman;
|
||||||
class CTxMemPool;
|
class CTxMemPool;
|
||||||
class ChainstateManager;
|
class ChainstateManager;
|
||||||
@ -34,8 +34,8 @@ ArgsManager& EnsureArgsman(const node::NodeContext& node);
|
|||||||
ArgsManager& EnsureAnyArgsman(const std::any& context);
|
ArgsManager& EnsureAnyArgsman(const std::any& context);
|
||||||
ChainstateManager& EnsureChainman(const node::NodeContext& node);
|
ChainstateManager& EnsureChainman(const node::NodeContext& node);
|
||||||
ChainstateManager& EnsureAnyChainman(const std::any& context);
|
ChainstateManager& EnsureAnyChainman(const std::any& context);
|
||||||
CBlockPolicyEstimator& EnsureFeeEstimator(const node::NodeContext& node);
|
FeeRateForecasterManager& EnsureForecasterMan(const node::NodeContext& node);
|
||||||
CBlockPolicyEstimator& EnsureAnyFeeEstimator(const std::any& context);
|
FeeRateForecasterManager& EnsureAnyForecasterMan(const std::any& context);
|
||||||
CConnman& EnsureConnman(const node::NodeContext& node);
|
CConnman& EnsureConnman(const node::NodeContext& node);
|
||||||
interfaces::Mining& EnsureMining(const node::NodeContext& node);
|
interfaces::Mining& EnsureMining(const node::NodeContext& node);
|
||||||
PeerManager& EnsurePeerman(const node::NodeContext& node);
|
PeerManager& EnsurePeerman(const node::NodeContext& node);
|
||||||
|
@ -40,6 +40,8 @@ add_executable(test_bitcoin
|
|||||||
descriptor_tests.cpp
|
descriptor_tests.cpp
|
||||||
disconnected_transactions.cpp
|
disconnected_transactions.cpp
|
||||||
feefrac_tests.cpp
|
feefrac_tests.cpp
|
||||||
|
feerounder_tests.cpp
|
||||||
|
forecaster_util_tests.cpp
|
||||||
flatfile_tests.cpp
|
flatfile_tests.cpp
|
||||||
fs_tests.cpp
|
fs_tests.cpp
|
||||||
getarg_tests.cpp
|
getarg_tests.cpp
|
||||||
@ -52,6 +54,7 @@ add_executable(test_bitcoin
|
|||||||
key_tests.cpp
|
key_tests.cpp
|
||||||
logging_tests.cpp
|
logging_tests.cpp
|
||||||
mempool_tests.cpp
|
mempool_tests.cpp
|
||||||
|
mempoolforecaster_tests.cpp
|
||||||
merkle_tests.cpp
|
merkle_tests.cpp
|
||||||
merkleblock_tests.cpp
|
merkleblock_tests.cpp
|
||||||
miner_tests.cpp
|
miner_tests.cpp
|
||||||
@ -68,7 +71,6 @@ add_executable(test_bitcoin
|
|||||||
pcp_tests.cpp
|
pcp_tests.cpp
|
||||||
peerman_tests.cpp
|
peerman_tests.cpp
|
||||||
pmt_tests.cpp
|
pmt_tests.cpp
|
||||||
policy_fee_tests.cpp
|
|
||||||
policyestimator_tests.cpp
|
policyestimator_tests.cpp
|
||||||
pool_tests.cpp
|
pool_tests.cpp
|
||||||
pow_tests.cpp
|
pow_tests.cpp
|
||||||
|
@ -3,13 +3,13 @@
|
|||||||
// file COPYING or http://www.opensource.org/licenses/mit-license.php.
|
// file COPYING or http://www.opensource.org/licenses/mit-license.php.
|
||||||
|
|
||||||
#include <consensus/amount.h>
|
#include <consensus/amount.h>
|
||||||
#include <policy/fees.h>
|
#include <policy/fees/block_policy_estimator.h>
|
||||||
|
|
||||||
#include <boost/test/unit_test.hpp>
|
#include <boost/test/unit_test.hpp>
|
||||||
|
|
||||||
#include <set>
|
#include <set>
|
||||||
|
|
||||||
BOOST_AUTO_TEST_SUITE(policy_fee_tests)
|
BOOST_AUTO_TEST_SUITE(fee_rounder_tests)
|
||||||
|
|
||||||
BOOST_AUTO_TEST_CASE(FeeRounder)
|
BOOST_AUTO_TEST_CASE(FeeRounder)
|
||||||
{
|
{
|
65
src/test/forecaster_util_tests.cpp
Normal file
65
src/test/forecaster_util_tests.cpp
Normal file
@ -0,0 +1,65 @@
|
|||||||
|
// Copyright (c) 2024 The Bitcoin Core developers
|
||||||
|
// Distributed under the MIT software license, see the accompanying
|
||||||
|
// file COPYING or http://www.opensource.org/licenses/mit-license.php.
|
||||||
|
|
||||||
|
|
||||||
|
#include <policy/fees/forecaster_util.h>
|
||||||
|
#include <policy/policy.h>
|
||||||
|
|
||||||
|
#include <boost/test/unit_test.hpp>
|
||||||
|
#include <vector>
|
||||||
|
|
||||||
|
BOOST_AUTO_TEST_SUITE(forecaster_util_tests)
|
||||||
|
|
||||||
|
BOOST_AUTO_TEST_CASE(calculate_percentile_test)
|
||||||
|
{
|
||||||
|
// Test that CalculatePercentiles returns empty when vector is empty
|
||||||
|
BOOST_CHECK(CalculatePercentiles({}, DEFAULT_BLOCK_MAX_WEIGHT).empty());
|
||||||
|
|
||||||
|
const int32_t package_size{10};
|
||||||
|
const int32_t individual_tx_vsize = static_cast<int32_t>(DEFAULT_BLOCK_MAX_WEIGHT / WITNESS_SCALE_FACTOR) / package_size;
|
||||||
|
|
||||||
|
// Define fee rate categories with corresponding fee rates in satoshis per kvB
|
||||||
|
const FeeFrac superHighFeerate{500 * individual_tx_vsize, individual_tx_vsize}; // Super High fee: 500 sat/kvB
|
||||||
|
const FeeFrac highFeerate{100 * individual_tx_vsize, individual_tx_vsize}; // High fee: 100 sat/kvB
|
||||||
|
const FeeFrac mediumFeerate{50 * individual_tx_vsize, individual_tx_vsize}; // Medium fee: 50 sat/kvB
|
||||||
|
const FeeFrac lowFeerate{10 * individual_tx_vsize, individual_tx_vsize}; // Low fee: 10 sat/kvB
|
||||||
|
|
||||||
|
std::vector<FeeFrac> package_feerates;
|
||||||
|
package_feerates.reserve(package_size);
|
||||||
|
|
||||||
|
// Populate the feerate histogram based on specified index ranges.
|
||||||
|
for (int i = 0; i < package_size; ++i) {
|
||||||
|
if (i < 3) {
|
||||||
|
package_feerates.emplace_back(superHighFeerate); // Super High fee rate for top 3
|
||||||
|
} else if (i < 5) {
|
||||||
|
package_feerates.emplace_back(highFeerate); // High fee rate for next 2
|
||||||
|
} else if (i < 8) {
|
||||||
|
package_feerates.emplace_back(mediumFeerate); // Medium fee rate for next 3
|
||||||
|
BOOST_CHECK(CalculatePercentiles(package_feerates, DEFAULT_BLOCK_MAX_WEIGHT).empty()); // CalculatePercentiles should return empty until reaching the 95th percentile
|
||||||
|
} else {
|
||||||
|
package_feerates.emplace_back(lowFeerate); // Low fee rate for remaining 2
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// Test percentile calculation on a complete histogram
|
||||||
|
{
|
||||||
|
const auto percentiles = CalculatePercentiles(package_feerates, DEFAULT_BLOCK_MAX_WEIGHT);
|
||||||
|
BOOST_CHECK(percentiles.p25 == superHighFeerate);
|
||||||
|
BOOST_CHECK(percentiles.p50 == highFeerate);
|
||||||
|
BOOST_CHECK(percentiles.p75 == mediumFeerate);
|
||||||
|
BOOST_CHECK(percentiles.p95 == lowFeerate);
|
||||||
|
}
|
||||||
|
|
||||||
|
// Test that CalculatePercentiles maintains monotonicity across all percentiles
|
||||||
|
{
|
||||||
|
package_feerates[7] = superHighFeerate; // Increase 8th index to a high fee rate
|
||||||
|
const auto percentiles = CalculatePercentiles(package_feerates, DEFAULT_BLOCK_MAX_WEIGHT);
|
||||||
|
BOOST_CHECK(percentiles.p25 == superHighFeerate);
|
||||||
|
BOOST_CHECK(percentiles.p50 == highFeerate);
|
||||||
|
BOOST_CHECK(percentiles.p75 == mediumFeerate); // Should still reflect the previous medium rate
|
||||||
|
BOOST_CHECK(percentiles.p95 == lowFeerate);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
BOOST_AUTO_TEST_SUITE_END()
|
@ -4,7 +4,7 @@
|
|||||||
|
|
||||||
#include <common/messages.h>
|
#include <common/messages.h>
|
||||||
#include <consensus/amount.h>
|
#include <consensus/amount.h>
|
||||||
#include <policy/fees.h>
|
#include <policy/fees/block_policy_estimator.h>
|
||||||
#include <test/fuzz/FuzzedDataProvider.h>
|
#include <test/fuzz/FuzzedDataProvider.h>
|
||||||
#include <test/fuzz/fuzz.h>
|
#include <test/fuzz/fuzz.h>
|
||||||
#include <test/fuzz/util.h>
|
#include <test/fuzz/util.h>
|
||||||
|
@ -5,7 +5,7 @@
|
|||||||
#include <common/messages.h>
|
#include <common/messages.h>
|
||||||
#include <merkleblock.h>
|
#include <merkleblock.h>
|
||||||
#include <node/types.h>
|
#include <node/types.h>
|
||||||
#include <policy/fees.h>
|
#include <policy/fees/block_policy_estimator.h>
|
||||||
#include <rpc/util.h>
|
#include <rpc/util.h>
|
||||||
#include <test/fuzz/FuzzedDataProvider.h>
|
#include <test/fuzz/FuzzedDataProvider.h>
|
||||||
#include <test/fuzz/fuzz.h>
|
#include <test/fuzz/fuzz.h>
|
||||||
|
@ -3,8 +3,8 @@
|
|||||||
// file COPYING or http://www.opensource.org/licenses/mit-license.php.
|
// file COPYING or http://www.opensource.org/licenses/mit-license.php.
|
||||||
|
|
||||||
#include <kernel/mempool_entry.h>
|
#include <kernel/mempool_entry.h>
|
||||||
#include <policy/fees.h>
|
#include <policy/fees/block_policy_estimator.h>
|
||||||
#include <policy/fees_args.h>
|
#include <policy/fees/block_policy_estimator_args.h>
|
||||||
#include <primitives/transaction.h>
|
#include <primitives/transaction.h>
|
||||||
#include <streams.h>
|
#include <streams.h>
|
||||||
#include <test/fuzz/FuzzedDataProvider.h>
|
#include <test/fuzz/FuzzedDataProvider.h>
|
||||||
|
@ -2,8 +2,8 @@
|
|||||||
// Distributed under the MIT software license, see the accompanying
|
// Distributed under the MIT software license, see the accompanying
|
||||||
// file COPYING or http://www.opensource.org/licenses/mit-license.php.
|
// file COPYING or http://www.opensource.org/licenses/mit-license.php.
|
||||||
|
|
||||||
#include <policy/fees.h>
|
#include <policy/fees/block_policy_estimator.h>
|
||||||
#include <policy/fees_args.h>
|
#include <policy/fees/block_policy_estimator_args.h>
|
||||||
#include <streams.h>
|
#include <streams.h>
|
||||||
#include <test/fuzz/FuzzedDataProvider.h>
|
#include <test/fuzz/FuzzedDataProvider.h>
|
||||||
#include <test/fuzz/fuzz.h>
|
#include <test/fuzz/fuzz.h>
|
||||||
|
99
src/test/mempoolforecaster_tests.cpp
Normal file
99
src/test/mempoolforecaster_tests.cpp
Normal file
@ -0,0 +1,99 @@
|
|||||||
|
// Copyright (c) 2024 The Bitcoin Core developers
|
||||||
|
// Distributed under the MIT software license. See the accompanying
|
||||||
|
// file COPYING or http://www.opensource.org/licenses/mit-license.php.
|
||||||
|
|
||||||
|
#include <policy/fees/forecaster.h>
|
||||||
|
#include <policy/fees/forecaster_util.h>
|
||||||
|
#include <policy/fees/mempool_forecaster.h>
|
||||||
|
#include <random.h>
|
||||||
|
#include <test/util/txmempool.h>
|
||||||
|
#include <txmempool.h>
|
||||||
|
#include <uint256.h>
|
||||||
|
#include <util/feefrac.h>
|
||||||
|
#include <util/strencodings.h>
|
||||||
|
#include <validation.h>
|
||||||
|
|
||||||
|
|
||||||
|
#include <test/util/setup_common.h>
|
||||||
|
|
||||||
|
#include <memory>
|
||||||
|
#include <string>
|
||||||
|
|
||||||
|
#include <boost/test/unit_test.hpp>
|
||||||
|
|
||||||
|
BOOST_FIXTURE_TEST_SUITE(mempoolforecaster_tests, TestChain100Setup)
|
||||||
|
|
||||||
|
static inline CTransactionRef make_random_tx()
|
||||||
|
{
|
||||||
|
auto rng = FastRandomContext();
|
||||||
|
auto tx = CMutableTransaction();
|
||||||
|
tx.vin.resize(1);
|
||||||
|
tx.vout.resize(1);
|
||||||
|
tx.vin[0].prevout.hash = Txid::FromUint256(rng.rand256());
|
||||||
|
tx.vin[0].prevout.n = 0;
|
||||||
|
tx.vin[0].scriptSig << OP_TRUE;
|
||||||
|
tx.vout[0].scriptPubKey = CScript() << OP_TRUE;
|
||||||
|
tx.vout[0].nValue = COIN;
|
||||||
|
return MakeTransactionRef(tx);
|
||||||
|
}
|
||||||
|
|
||||||
|
BOOST_AUTO_TEST_CASE(MempoolEstimator)
|
||||||
|
{
|
||||||
|
auto mempool_fee_estimator = std::make_unique<MemPoolForecaster>(m_node.mempool.get(), &(m_node.chainman->ActiveChainstate()));
|
||||||
|
ConfirmationTarget conf_target = {MEMPOOL_FORECAST_MAX_TARGET + 1, ConfirmationTargetType::BLOCKS};
|
||||||
|
LOCK2(cs_main, m_node.mempool->cs);
|
||||||
|
{
|
||||||
|
// Test when targetBlocks > MEMPOOL_FORECAST_MAX_TARGET
|
||||||
|
const auto fee_estimate = mempool_fee_estimator->EstimateFee(conf_target);
|
||||||
|
BOOST_CHECK(fee_estimate.Empty());
|
||||||
|
BOOST_CHECK(*fee_estimate.GetError() == strprintf("Confirmation target %s exceeds the maximum limit of %s. mempool conditions might change, "
|
||||||
|
"making forecasts above %s block may be unreliable",
|
||||||
|
MEMPOOL_FORECAST_MAX_TARGET + 1, MEMPOOL_FORECAST_MAX_TARGET, MEMPOOL_FORECAST_MAX_TARGET));
|
||||||
|
}
|
||||||
|
|
||||||
|
BOOST_CHECK(m_node.mempool->GetTotalTxSize() == 0);
|
||||||
|
TestMemPoolEntryHelper entry;
|
||||||
|
|
||||||
|
const CAmount low_fee{CENT / 3000};
|
||||||
|
const CAmount med_fee{CENT / 100};
|
||||||
|
const CAmount high_fee{CENT / 10};
|
||||||
|
|
||||||
|
conf_target.value = MEMPOOL_FORECAST_MAX_TARGET;
|
||||||
|
// Test when there are not enough mempool transactions to get an accurate estimate
|
||||||
|
{
|
||||||
|
// Add transactions with high_fee fee until mempool transactions weight is more than 25th percent of DEFAULT_BLOCK_MAX_WEIGHT
|
||||||
|
while (static_cast<int>(m_node.mempool->GetTotalTxSize() * WITNESS_SCALE_FACTOR) <= static_cast<int>(0.25 * DEFAULT_BLOCK_MAX_WEIGHT)) {
|
||||||
|
AddToMempool(*m_node.mempool, entry.Fee(high_fee).FromTx(make_random_tx()));
|
||||||
|
}
|
||||||
|
const auto fee_estimate = mempool_fee_estimator->EstimateFee(conf_target);
|
||||||
|
BOOST_CHECK(fee_estimate.Empty());
|
||||||
|
BOOST_CHECK(*fee_estimate.GetError() == "Forecaster unable to provide an estimate due to insufficient data");
|
||||||
|
}
|
||||||
|
|
||||||
|
{
|
||||||
|
// Add transactions with med_fee fee until mempool transactions weight is more than 50th percent of DEFAULT_BLOCK_MAX_WEIGHT
|
||||||
|
while (static_cast<int>(m_node.mempool->GetTotalTxSize() * WITNESS_SCALE_FACTOR) <= static_cast<int>(0.5 * DEFAULT_BLOCK_MAX_WEIGHT)) {
|
||||||
|
AddToMempool(*m_node.mempool, entry.Fee(med_fee).FromTx(make_random_tx()));
|
||||||
|
}
|
||||||
|
const auto fee_estimate = mempool_fee_estimator->EstimateFee(conf_target);
|
||||||
|
BOOST_CHECK(fee_estimate.Empty());
|
||||||
|
BOOST_CHECK(*fee_estimate.GetError() == "Forecaster unable to provide an estimate due to insufficient data");
|
||||||
|
}
|
||||||
|
|
||||||
|
// Mempool transactions are enough to provide feerate estimate
|
||||||
|
{
|
||||||
|
// Add low_fee transactions until mempool transactions weight is more than 95th percent of DEFAULT_BLOCK_MAX_WEIGHT
|
||||||
|
while (static_cast<int>(m_node.mempool->GetTotalTxSize() * WITNESS_SCALE_FACTOR) <= static_cast<int>(0.95 * DEFAULT_BLOCK_MAX_WEIGHT)) {
|
||||||
|
const auto txref = make_random_tx();
|
||||||
|
AddToMempool(*m_node.mempool, entry.Fee(low_fee).FromTx(make_random_tx()));
|
||||||
|
}
|
||||||
|
|
||||||
|
const auto fee_estimate = mempool_fee_estimator->EstimateFee(conf_target);
|
||||||
|
BOOST_CHECK(!fee_estimate.Empty());
|
||||||
|
const auto tx_vsize = entry.FromTx(make_random_tx()).GetTxSize();
|
||||||
|
BOOST_CHECK(fee_estimate.GetResponse().low_priority == FeeFrac(low_fee, tx_vsize));
|
||||||
|
BOOST_CHECK(fee_estimate.GetResponse().high_priority == FeeFrac(med_fee, tx_vsize));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
BOOST_AUTO_TEST_SUITE_END()
|
@ -2,8 +2,8 @@
|
|||||||
// Distributed under the MIT software license, see the accompanying
|
// Distributed under the MIT software license, see the accompanying
|
||||||
// file COPYING or http://www.opensource.org/licenses/mit-license.php.
|
// file COPYING or http://www.opensource.org/licenses/mit-license.php.
|
||||||
|
|
||||||
#include <policy/fees.h>
|
#include <policy/fees/block_policy_estimator.h>
|
||||||
#include <policy/fees_args.h>
|
#include <policy/fees/block_policy_estimator_args.h>
|
||||||
#include <policy/policy.h>
|
#include <policy/policy.h>
|
||||||
#include <test/util/txmempool.h>
|
#include <test/util/txmempool.h>
|
||||||
#include <txmempool.h>
|
#include <txmempool.h>
|
||||||
|
@ -28,7 +28,7 @@
|
|||||||
#include <node/peerman_args.h>
|
#include <node/peerman_args.h>
|
||||||
#include <node/warnings.h>
|
#include <node/warnings.h>
|
||||||
#include <noui.h>
|
#include <noui.h>
|
||||||
#include <policy/fees.h>
|
#include <policy/fees/block_policy_estimator.h>
|
||||||
#include <pow.h>
|
#include <pow.h>
|
||||||
#include <random.h>
|
#include <random.h>
|
||||||
#include <rpc/blockchain.h>
|
#include <rpc/blockchain.h>
|
||||||
@ -275,7 +275,7 @@ ChainTestingSetup::~ChainTestingSetup()
|
|||||||
m_node.netgroupman.reset();
|
m_node.netgroupman.reset();
|
||||||
m_node.args = nullptr;
|
m_node.args = nullptr;
|
||||||
m_node.mempool.reset();
|
m_node.mempool.reset();
|
||||||
Assert(!m_node.fee_estimator); // Each test must create a local object, if they wish to use the fee_estimator
|
Assert(!m_node.forecasterman); // Each test must create a local object, if they wish to use the forecasterman
|
||||||
m_node.chainman.reset();
|
m_node.chainman.reset();
|
||||||
m_node.validation_signals.reset();
|
m_node.validation_signals.reset();
|
||||||
m_node.scheduler.reset();
|
m_node.scheduler.reset();
|
||||||
|
@ -7,7 +7,7 @@
|
|||||||
|
|
||||||
#include <outputtype.h>
|
#include <outputtype.h>
|
||||||
#include <policy/feerate.h>
|
#include <policy/feerate.h>
|
||||||
#include <policy/fees.h>
|
#include <policy/fees/block_policy_estimator.h>
|
||||||
#include <primitives/transaction.h>
|
#include <primitives/transaction.h>
|
||||||
#include <script/keyorigin.h>
|
#include <script/keyorigin.h>
|
||||||
#include <script/signingprovider.h>
|
#include <script/signingprovider.h>
|
||||||
|
@ -6,7 +6,7 @@
|
|||||||
#include <consensus/validation.h>
|
#include <consensus/validation.h>
|
||||||
#include <interfaces/chain.h>
|
#include <interfaces/chain.h>
|
||||||
#include <node/types.h>
|
#include <node/types.h>
|
||||||
#include <policy/fees.h>
|
#include <policy/fees/block_policy_estimator.h>
|
||||||
#include <policy/policy.h>
|
#include <policy/policy.h>
|
||||||
#include <util/moneystr.h>
|
#include <util/moneystr.h>
|
||||||
#include <util/rbf.h>
|
#include <util/rbf.h>
|
||||||
|
@ -9,7 +9,7 @@
|
|||||||
#include <interfaces/chain.h>
|
#include <interfaces/chain.h>
|
||||||
#include <interfaces/handler.h>
|
#include <interfaces/handler.h>
|
||||||
#include <node/types.h>
|
#include <node/types.h>
|
||||||
#include <policy/fees.h>
|
#include <policy/fees/block_policy_estimator.h>
|
||||||
#include <primitives/transaction.h>
|
#include <primitives/transaction.h>
|
||||||
#include <rpc/server.h>
|
#include <rpc/server.h>
|
||||||
#include <scheduler.h>
|
#include <scheduler.h>
|
||||||
|
@ -6,7 +6,7 @@
|
|||||||
#define BITCOIN_WALLET_SPEND_H
|
#define BITCOIN_WALLET_SPEND_H
|
||||||
|
|
||||||
#include <consensus/amount.h>
|
#include <consensus/amount.h>
|
||||||
#include <policy/fees.h> // for FeeCalculation
|
#include <policy/fees/block_policy_estimator.h> // for FeeCalculation
|
||||||
#include <util/result.h>
|
#include <util/result.h>
|
||||||
#include <wallet/coinselection.h>
|
#include <wallet/coinselection.h>
|
||||||
#include <wallet/transaction.h>
|
#include <wallet/transaction.h>
|
||||||
|
@ -3,7 +3,7 @@
|
|||||||
// file COPYING or http://www.opensource.org/licenses/mit-license.php.
|
// file COPYING or http://www.opensource.org/licenses/mit-license.php.
|
||||||
|
|
||||||
#include <consensus/amount.h>
|
#include <consensus/amount.h>
|
||||||
#include <policy/fees.h>
|
#include <policy/fees/block_policy_estimator.h>
|
||||||
#include <script/solver.h>
|
#include <script/solver.h>
|
||||||
#include <validation.h>
|
#include <validation.h>
|
||||||
#include <wallet/coincontrol.h>
|
#include <wallet/coincontrol.h>
|
||||||
|
Loading…
x
Reference in New Issue
Block a user