Skip to content

Commit

Permalink
policy, fees: get mempool based fee estimates periodically
Browse files Browse the repository at this point in the history
  • Loading branch information
ismaelsadeeq committed Feb 28, 2024
1 parent 100a02f commit 02bd0ff
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 4 deletions.
10 changes: 7 additions & 3 deletions src/init.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1269,9 +1269,6 @@ bool AppInitMain(NodeContext& node, interfaces::BlockAndHeaderTipInfo* tip_info)
RegisterValidationInterface(fee_estimator);
}

assert(!node.mempool_fee_estimator);

node.mempool_fee_estimator = std::make_unique<MemPoolPolicyEstimator>();

// Check port numbers
for (const std::string port_option : {
Expand Down Expand Up @@ -1596,6 +1593,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>();
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,
Expand Down
18 changes: 17 additions & 1 deletion src/policy/mempool_fees.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,10 @@
#include <consensus/consensus.h>
#include <logging.h>
#include <node/miner.h>
#include <policy/fees.h>
#include <policy/mempool_fees.h>
#include <policy/policy.h>

#include <validation.h>

using node::GetCustomBlockFeeRateHistogram;

Expand Down Expand Up @@ -58,6 +59,21 @@ CFeeRate MemPoolPolicyEstimator::EstimateFeeWithMemPool(Chainstate& chainstate,
return block_fee_rate;
}

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();
CFeeRate 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 == CFeeRate(0)) {
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 is %s, block estimate for next block is %s \n", block->phashBlock->GetHex(), block->nHeight, estimate.GetFeePerK(), block_estimate.GetFeePerK());
}
}

std::map<uint64_t, CFeeRate> MemPoolPolicyEstimator::EstimateBlockFeeRatesWithMempool(
const std::vector<std::tuple<CFeeRate, uint64_t>>& mempool_fee_stats, unsigned int confTarget) const
{
Expand Down
5 changes: 5 additions & 0 deletions src/policy/mempool_fees.h
Original file line number Diff line number Diff line change
Expand Up @@ -15,13 +15,17 @@
#include <logging.h>
#include <policy/feerate.h>

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{3};

static constexpr std::chrono::minutes FEE_ESTIMATE_INTERVAL{1};

/**
* CachedMempoolEstimates holds a cache of recent mempool-based fee estimates.
* Running the block-building algorithm multiple times is undesriable due to
Expand Down Expand Up @@ -96,6 +100,7 @@ class MemPoolPolicyEstimator
* @return The estimated fee rate.
*/
CFeeRate 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;
Expand Down

0 comments on commit 02bd0ff

Please sign in to comment.