diff --git a/src/init.cpp b/src/init.cpp index 549cbfcec80f1..a129db1c226b4 100644 --- a/src/init.cpp +++ b/src/init.cpp @@ -1292,9 +1292,6 @@ bool AppInitMain(NodeContext& node, interfaces::BlockAndHeaderTipInfo* tip_info) validation_signals.RegisterValidationInterface(fee_estimator); } - assert(!node.mempool_fee_estimator); - - node.mempool_fee_estimator = std::make_unique(); // Check port numbers for (const std::string port_option : { @@ -1621,6 +1618,13 @@ bool AppInitMain(NodeContext& node, interfaces::BlockAndHeaderTipInfo* tip_info) ChainstateManager& chainman = *Assert(node.chainman); + assert(!node.mempool_fee_estimator); + + node.mempool_fee_estimator = std::make_unique(); + MemPoolPolicyEstimator* mempool_fee_estimator = node.mempool_fee_estimator.get(); + CBlockPolicyEstimator* fee_estimator = node.fee_estimator.get(); + CTxMemPool& mempool = *(node.mempool.get()); + node.scheduler->scheduleEvery([mempool_fee_estimator, fee_estimator, &mempool, &chainman] { mempool_fee_estimator->EstimateFeeWithMemPool(chainman, mempool, fee_estimator); }, FEE_ESTIMATE_INTERVAL); assert(!node.peerman); node.peerman = PeerManager::make(*node.connman, *node.addrman, node.banman.get(), chainman, diff --git a/src/policy/mempool_fees.cpp b/src/policy/mempool_fees.cpp index ae4d7ad2f8af4..c1b9f6d14e985 100644 --- a/src/policy/mempool_fees.cpp +++ b/src/policy/mempool_fees.cpp @@ -6,9 +6,10 @@ #include #include #include +#include #include #include - +#include using node::GetCustomBlockFeeRateHistogram; @@ -59,10 +60,25 @@ MempoolFeeEstimationResult MemPoolPolicyEstimator::EstimateFeeWithMemPool(Chains if (fee_rate_estimate_result.empty()) { err_message = "Insufficient mempool transactions to perform an estimate."; } - return fee_rate_estimate_result; } +void MemPoolPolicyEstimator::EstimateFeeWithMemPool(const ChainstateManager& chainman, const CTxMemPool& mempool, const CBlockPolicyEstimator* fee_estimator) const +{ + std::string err_message; + LOCK(cs_main); + CBlockIndex* block = chainman.ActiveTip(); + MempoolFeeEstimationResult estimate = EstimateFeeWithMemPool(chainman.ActiveChainstate(), mempool, /*confTarget=*/1, /*force=*/true, err_message); + FeeCalculation feeCalc; + CFeeRate block_estimate = fee_estimator->estimateSmartFee(/*conf_target=*/1, &feeCalc, /*conservative=*/false); + if (estimate.empty()) { + LogInfo("At block %s, height %s, failed to get mempool based fee rate estimate; error: %s \n", block->phashBlock->GetHex(), block->nHeight, err_message); + } else { + LogInfo("At block %s, height %s, mempool based fee rate estimate for next block has a 75th percentile fee rate %s, 50th percentile fee rate %s, 25th percentile fee rate %s, 5th percentile fee rate %s, block estimate for next block is %s \n", + block->phashBlock->GetHex(), block->nHeight, estimate.p75.GetFeePerK(), estimate.p50.GetFeePerK(), estimate.p25.GetFeePerK(), estimate.p5.GetFeePerK(), block_estimate.GetFeePerK()); + } +} + std::map MemPoolPolicyEstimator::EstimateBlockFeeRatesWithMempool( const std::vector>& mempool_fee_stats, unsigned int confTarget) const { diff --git a/src/policy/mempool_fees.h b/src/policy/mempool_fees.h index f024128e7fcd6..13df80057062a 100644 --- a/src/policy/mempool_fees.h +++ b/src/policy/mempool_fees.h @@ -15,13 +15,17 @@ #include #include +class CBlockPolicyEstimator; class Chainstate; +class ChainstateManager; class CTxMemPool; // Fee rate estimates above this confirmation target are not reliable, // mempool condition might likely change. static const unsigned int MAX_CONF_TARGET{1}; +static constexpr std::chrono::minutes FEE_ESTIMATE_INTERVAL{1}; + // Fee estimation result containing percentiles (in sat/kvB). struct MempoolFeeEstimationResult { CFeeRate p5; // 5th percentile @@ -114,6 +118,7 @@ class MemPoolPolicyEstimator * @return The estimated fee rates. */ MempoolFeeEstimationResult EstimateFeeWithMemPool(Chainstate& chainstate, const CTxMemPool& mempool, unsigned int confTarget, const bool force, std::string& err_message) const; + void EstimateFeeWithMemPool(const ChainstateManager& chainstate, const CTxMemPool& mempool, const CBlockPolicyEstimator* fee_estimator) const; private: mutable CachedMempoolEstimates cache;