Skip to content

Commit

Permalink
fixup! [tx fees, policy]: create a mempool fee estimator
Browse files Browse the repository at this point in the history
* Always calculate fee rates for MAX_CONF_TARGET number of blocks, as
  it's not that expensive to do so, and we cache them all.
  • Loading branch information
willcl-ark committed Feb 20, 2024
1 parent bdc1a0c commit 4972738
Showing 1 changed file with 4 additions and 7 deletions.
11 changes: 4 additions & 7 deletions src/policy/mempool_fees.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -28,25 +28,23 @@ CFeeRate MemPoolPolicyEstimator::EstimateFeeWithMemPool(Chainstate& chainstate,
err_message = "Mempool did not finish loading, can't get accurate fee rate estimate.";
return CFeeRate(0);
}
// Try the cache if not forced
if (!force) {
cached_fee = cache.get(confTarget);
}

if (!cached_fee) {
// Run block builder and update cache
std::map<CFeeRate, uint64_t> mempool_fee_stats;
size_t target_weight = confTarget * DEFAULT_BLOCK_MAX_WEIGHT;
mempool_fee_stats = GetCustomBlockFeeRateHistogram(chainstate, &mempool, target_weight);
// Always get stats for MAX_CONF_TARGET blocks because current algo
// fast enough while we're here
mempool_fee_stats = GetCustomBlockFeeRateHistogram(chainstate, &mempool, DEFAULT_BLOCK_MAX_WEIGHT * MAX_CONF_TARGET);
if (mempool_fee_stats.empty()) {
err_message = "No transactions available in the mempool yet.";
return CFeeRate(0);
}
fee_rates = EstimateBlockFeeRatesWithMempool(mempool_fee_stats, confTarget);
fee_rates = EstimateBlockFeeRatesWithMempool(mempool_fee_stats, MAX_CONF_TARGET);
cache.update(fee_rates);
block_fee_rate = fee_rates[confTarget];
} else {
// Use the cached value
block_fee_rate = *cached_fee;
}

Expand All @@ -60,7 +58,6 @@ std::map<uint64_t, CFeeRate> MemPoolPolicyEstimator::EstimateBlockFeeRatesWithMe
const std::map<CFeeRate, uint64_t>& mempool_fee_stats, unsigned int confTarget) const
{
std::map<uint64_t, CFeeRate> fee_rates;
// Return empty if no stats
if (mempool_fee_stats.empty()) return fee_rates;

auto start = mempool_fee_stats.begin();
Expand Down

0 comments on commit 4972738

Please sign in to comment.