fees: Introduce Mempool Based Fee Estimation to reduce overestimation #34075

pull ismaelsadeeq wants to merge 27 commits into bitcoin:master from ismaelsadeeq:12-2025-fee-estimation-improvement changing 59 files +1620 −427
  1. ismaelsadeeq commented at 11:53 am on December 15, 2025: member

    This PR is another attempt to fix #27995 using a better approach.

    For background and motivation, see #27995 and the discussion in the Delving Bitcoin post Mempool Based Fee Estimation on Bitcoin Core This PR is currently limited to using the mempool only to lower what is recommended by the Block Policy Estimator. Accurate and safe fee estimation using the mempool is challenging. There are open questions about how to prevent mempool games that are theoretically possible for miners (a variant of the Finney attack)

    This is one of the reasons I opted to use the mempool only to lower the Block Policy Estimator, which itself is not gameable, and therefore this mechanism is not susceptible to this attack.

    The underlying assumption here is that, with the current tools and work done to make RBF and CPFP feasible and guarantee (TRUC transaction relay, ephemeral anchors, cluster size 2 package rbf), it is safer to underestimate rather than overestimate. We now assume it is relatively easy to fee-bump later if a transaction does not confirm, whereas once a fee is overestimated there is no way to recover from that.

    Another open question when using the mempool for fee estimation is how to account for incoming transaction inflow. Bitcoin Augur does this by using past inflow plus a constant expected inflow to predict future inflow. I find this unconvincing for fee estimation and potentially prone to more overestimation, as past conditions are not always representative of the future. See my review of the Augur fee rate estimator and open questions.

    This PR uses a much simpler approach based on current user behavior, similar to the widely used method employed by mempool.space: looking at the top block of the mempool and selecting a percentile feerate depending on whether the user is economical or conservative.

    Empirical data from both myself and Clara Shikhelman shows that the 75th percentile feerate for economical users and the 50th percentile feerate for conservative users provide positive confirmation guarantees, hence this is what is used in this PR.

    Parallel research by René Pickhardt and his student suggests that using the average fee per byte of the block template performs well.

    All of these are constants that can be adjusted, there is parallel work exploring these constants and running benchmarks across fee estimators to find a sweet spot.

    See also work in LND, the LND Budget Sweeper, which applies this idea successfully. Their approach is to estimate fees initially with bitcoind, then increment gradually as the confirmation deadline approaches, using a fixed fee budget.

    Historical data indicates that by doing only the approach in this PR, we are able to reduce overestimation quite significantly (~29%).

    This is particularly useful in scenarios where the Block Policy Estimator recommends a high feerate while the mempool is empty.

    As seen in the image above, there is only one remaining unfixed case: when there is a sudden inflow of transactions and the feerate rises, the Block Policy Estimator takes time to reflect this. In that case, users will continue to see a low feerate estimate until it slowly updates. From the historical data linked above, this occurs about ~26% of the time).

    Overall, we observe a 73% success rate with 0% overestimation, and 26% underestimation with this approach.

    This PR also includes refactors that enable this work. Rather than splitting the PR and implementing changes incrementally, I opted for an end-to-end implementation:

    1. Refactors

    • Separation of feerate format from the fee estimate mode enumerator (separation of concerns, as they are semantically distinct)
    • Move logging after fee estimation estimateSmartFee from the wallet to the Block Policy Estimator internal method (also separation of concerns and avoiding leaking fee estimator internals). This change also comes with updating createTransactionInternal to log only the fee and its source (fee estimator, fallback fee, etc.), which is more appropriate.
    • Separate the fee estimate mode enum from the fee source enumerator (separation of concerns, as they are semantically distinct). This introduces a minor breaking change: the wallet RPCs now return fee source instead of fee reason, which is more accurate. Let me know if we prefer to keep the field name unchanged while returning the fee source.
    • Move the StringForReason enum from common to the Block Policy Estimator (also separation of concerns)
    • Various test file updates and renames for improved accuracy
    • Update Block Policy Estimator unit tests to be independent of the mempool and the validation interface

    2. Introduce Mempool-Based Fee Estimator and Fee Estimator Manager

    • Introduce a new abstract fee estimator base class that all fee estimators must implement

    • Introduce FeeRateEstimateResult as the response type, containing metadata and avoiding the use of out-parameters. This is easily extensible and makes future changes less invasive, as method signatures do not need to change.

    • Add FeeEstimatorType enum to identify different fee estimators

    • Make CBlockPolicyEstimator derive from the new fee estimator base class

    • Add FeeRateEstimatorManager, responsible for managing all fee estimators

    • Update the node context to store a unique_ptr to FeeRateEstimatorManager instead of CBlockPolicyEstimator

    • Update CBlockPolicyEstimator to no longer subscribe directly to the validation interface; instead, FeeRateEstimatorManager subscribes and forwards relevant notifications

    • Add a percentile calculation function that takes the chunk feerates of a block template and returns feerate percentiles; this can also be reused by the getblockstats RPC

    • Add a mempool fee estimator that generates a block template when called, calculates a percentile feerate, and returns the 75th percentile for economical mode or the 50th percentile for conservative mode

    • Add caching to the mempool estimator so that new estimates are generated only every 7 seconds, assuming enough transactions have propagated to make a meaningful difference. This heuristic will likely be replaced by requesting block templates via the general-purpose block template cache proposed here: https://github.com/bitcoin/bitcoin/issues/33389

    • Update MempoolTransactionRemovedForBlock to return the actual block transactions as well

    • Track the weight of block transactions and evicted transactions after each block connection This data is tracked for the last 6 mined blocks. A mempool feerate estimate is returned only when the average ratio of mempool transaction weight evicted due to BLOCK reasons to block transaction weight is greater than 75%. This heuristic provides rough confidence that the node’s mempool matches that of the majority of the hashrate. The 75% threshold is arbitrary and can be adjusted.

    There is a caveat when transactions in the local mempool are consistently not mined by the network, as described in #27995 (e.g., due to filtering). Accounting for these transactions during fee estimation is not necessary, as they should be evicted from the mempool itself (see #33510). Handling this again within fee estimation would be redundant.

    • Persist statistics for the 6 most recent blocks to disk during periodic flushes and shutdown, so this data is available after restarts
    • Expose this data to users when running estimatesmartfee with verbosity > 2
    0bitcoin-cli estimatesmartfee 1 "conservative" 2
    
     0{
     1  "feerate": 0.00002133,
     2  "errors": [],
     3  "blocks": 2,
     4  "mempool_health_statistics": [
     5    {
     6      "block_height": 927953,
     7      "block_weight": 3991729,
     8      "mempool_txs_weight": 3942409,
     9      "ratio": 0.987644451815241
    10    },
    11    ...
    12  ]
    13}
    
  2. DrahtBot added the label TX fees and policy on Dec 15, 2025
  3. DrahtBot commented at 11:53 am on December 15, 2025: contributor

    The following sections might be updated with supplementary metadata relevant to reviewers and maintainers.

    Code Coverage & Benchmarks

    For details see: https://corecheck.dev/bitcoin/bitcoin/pulls/34075.

    Reviews

    See the guideline for information on the review process. A summary of reviews will appear here.

    Conflicts

    Reviewers, this pull request conflicts with the following ones:

    • #33922 (mining: add getMemoryLoad() and track template non-mempool memory footprint by Sjors)
    • #33421 (node: add BlockTemplateCache by ismaelsadeeq)
    • #33199 (fees: enable CBlockPolicyEstimator return sub 1 sat/vb fee rate estimates by ismaelsadeeq)
    • #32427 ((RFC) kernel: Replace leveldb-based BlockTreeDB with flat-file based store by sedited)
    • #32138 (wallet, rpc: remove settxfee and paytxfee by polespinasa)
    • #31382 (kernel: Flush in ChainstateManager destructor by sedited)
    • #28690 (build: Introduce internal kernel library by sedited)

    If you consider this pull request important, please also help to review the conflicting pull requests. Ideally, start with the one that should be merged first.

    LLM Linter (✨ experimental)

    Possible places where named args for integral literals may be used (e.g. func(x, /*named_arg=*/0) in C++, and func(x, named_arg=0) in Python):

    • ParseVerbosity(request.params[2], /default_verbosity=/1, /allow_bool=/true) in src/rpc/fees.cpp
    • node0.estimatesmartfee(1, “economical”, 2) in test/functional/feature_fee_estimation.py
    • assert_raises_rpc_error(-1, “estimatesmartfee”, self.nodes[0].estimatesmartfee, 1, ‘ECONOMICAL’, 1, 1) in test/functional/rpc_estimatefee.py

    Possible places where equality-specific test macros should replace generic comparisons:

    • src/test/blockpolicyestimator_tests.cpp BOOST_CHECK(feeEst.estimateFee(1) == CFeeRate(0)); -> Use BOOST_CHECK_EQUAL(feeEst.estimateFee(1), CFeeRate(0)) (or BOOST_REQUIRE_EQUAL as appropriate) for clearer diagnostics.
    • src/test/blockpolicyestimator_tests.cpp BOOST_CHECK(feeEst.estimateFee(2) … ) (and other BOOST_CHECK(…) expressions that use == to compare CFeeRate results) -> Use BOOST_CHECK_EQUAL / BOOST_REQUIRE_EQUAL for those feerate equality comparisons.
    • src/test/blockpolicyestimator_tests.cpp BOOST_CHECK(feeEst.estimateFee(i) == CFeeRate(0) || …) -> For the simple equality parts prefer BOOST_CHECK_EQUAL(feeEst.estimateFee(i), CFeeRate(0)) when possible; keep complex compound checks but consider splitting into separate checks so equality uses BOOST_CHECK_EQUAL.
    • src/test/fees_util_tests.cpp BOOST_CHECK(percentiles.p25 == super_high_fee_rate); -> Use BOOST_CHECK_EQUAL(percentiles.p25, super_high_fee_rate) (and similarly for p50/p75/p95 comparisons) for better failure messages.
    • src/test/mempool_fee_estimator_tests.cpp BOOST_CHECK(result.feerate.IsEmpty()); BOOST_CHECK(result.errors.back() == strprintf("%s: Mempool is unreliable for fee rate estimation", FeeRateEstimatorTypeToString(FeeRateEstimatorType::MEMPOOL_POLICY))); -> Replace string equality checks with BOOST_CHECK_EQUAL(result.errors.back(), strprintf(…)). Also use BOOST_CHECK_EQUAL / BOOST_REQUIRE_EQUAL for other equality comparisons of feerates or integers in this test file (e.g. checks comparing returned feerates, heights, boolean expectations where equality is asserted).

    Notes:

    • I did not flag assert(…) usages inside fuzz targets or non-Boost test code per your instruction.
    • Also did not flag non-equality checks (e.g., BOOST_CHECK(!something)) or checks that are appropriately expressed as boolean checks.
    • If you want, I can produce a precise list of every added BOOST_CHECK/BOOST_REQUIRE that uses == and a suggested replacement line for each.

    2025-12-22

  4. DrahtBot added the label CI failed on Dec 15, 2025
  5. ismaelsadeeq force-pushed on Dec 15, 2025
  6. DrahtBot removed the label CI failed on Dec 15, 2025
  7. gmaxwell commented at 11:01 pm on December 15, 2025: contributor

    I think this is a good idea, especially if it there was a ‘very conservative’ that just didn’t use this.

    Accounting for these transactions during fee estimation is not necessary, as they should be evicted from the mempool itself

    I don’t agree here– rather time based expiration should probably be eliminated over time. If there really is a censored transaction then we want nodes to work on getting it mined, and not just give up if the censors manage to control the blocks for a long time.

    Also two weeks is already long time to allow a non-minable transaction to distort fee estimates. And it’s a short time for low rate confirmation– there have been times in history when transactions weren’t confirmed for months after their initial announcement (though those were also at time so far where size based expiration would have eliminated them).

    So I suggest instead that future work (not this PR) could improve the estimator by excluding from considerations transactions that should have been mined according to our template but weren’t for more than a threshold number of blocks, perhaps 6 or something like that.

    Such code could also be used to log/warn the user that there is either ongoing censorship or that their node’s policy appears to be less restrictive than the network.

    But in any case it doesn’t need to be done immediate to make use of this kind of estimator.

    Overall, we observe a 73% success rate with 0% overestimation, and 26% underestimation with this approach.

    One thing to keep in mind is that if this approach is widely used the success rate will probably change. It might make sense to initially deploy with more conservative settings (/defaults) and slowly increase them to avoid causing an abrupt change.

  8. DrahtBot added the label Needs rebase on Dec 16, 2025
  9. ismaelsadeeq force-pushed on Dec 16, 2025
  10. ismaelsadeeq commented at 3:39 pm on December 16, 2025: member

    I don’t agree here; rather, time-based expiration should probably be eliminated over time. If there really is a censored transaction, then we want nodes to continue working to get it mined, not just give up if censors manage to control blocks for a long time.

    Indeed. In the context of censorship, we would want to keep such transactions if they are incentive compatible. However, there may also be legitimate reasons miners deliberately exclude them for example, a legacy node that is unaware of a soft fork that invalidates a certain spend (e.g., post-quantum).

    Hence as you also mentioned, I would like to see the direction of #33510, leaning toward deferring this improvement to future work.

    Especially if there were a “very conservative” option that simply didn’t use this. One thing to keep in mind is that if this approach is widely used, the success rate will probably change. It might make sense to initially deploy with more conservative settings (or defaults) and slowly increase them to avoid causing an abrupt change.

    I considered this and even had a branch that added an option to estimatesmartfee to force block-policy-only behavior.

    However, the users this targets are those running bitcoind with default settings and trusting developers to make sane default choices. I’ve seen this scenario before with previous conservative defaults: complaints and issues accumulated over time, eventually leading us to switch block policy the default to economical, which provided a significant reduction in overestimation and better accuracy.

    FWIW, I also did some research over a short block interval to get a sense of what Bitcoin core wallet users uses for feerate estimation, and I find that the majority of Bitcoin Core wallet users (that I was able to fingerprints) are not using the block-policy estimator, we’ve seen self-hosted users 1 2 switch approaches in the past, I’ve also personally spoken to large Bitcoin services that rely on third party but want this feature.
    It seems to me enabling this by default feels like the better direction, IMHO.

  11. DrahtBot removed the label Needs rebase on Dec 16, 2025
  12. gmaxwell commented at 2:57 pm on December 17, 2025: contributor

    I think enabling it by default probably makes sense, that particular sub comment was more about having a ready way to choose not to use it when transacting.

    As far as the initial defaults I think there I meant that that it shouldn’t target too close to the bottom of the mempool as even if that currently gets high success that may not be true after wide deployment. Though on reflection I’m not sure that thats true as this only lowers feerates, so other people using this should only increase your odds of success while using it.

  13. DrahtBot added the label Needs rebase on Dec 18, 2025
  14. refactor: separate feerate format from fee estimate mode
        - This commit introduces a separate enumerator for feerate format.
          This makes feerate format independent of the fee estimation mode enum.
          Feerate format is logically independent, hence should be separated.
          Additionally, the fee estimation mode enum is also moved to a fees
          util file.
    e003e10cc8
  15. refactor: make estimateSmartFee log internal 7252022947
  16. refactor: seperate fee estimate mode from fee source
    - Fee Estimation mode and fee estimate source are different
      semantically hence each should have an independent enum.
    084146446d
  17. refactor: move StringForReason to block policy estimator
    - This commit also added the fee reason to the final log
      of estimateSmartFee method.
    56e7117bf2
  18. refactor: log transaction fee in `CreateTransactionInternal`
    - The transaction size in bytes and the transaction fee source
      are also logged.
    a1b07154bc
  19. refactor: rename policyestimator_tests to blockpolicyestimator_tests 9dd4f1fd2c
  20. refactor: rename policy estimator to block policy estimator
    - This also rename the fuzz target to be specific as well.
    35fb8f81ad
  21. test: make blockpolicyestimator_tests independent of mempool 70e51eca68
  22. fees: add abstract fee estimator
    i  - FeeRateEstimator abstract class is the base class of fee rate estimators
         Derived classes must provide concrete implementation
         of the virtual methods.
    
    ii - FeeRateEstimatorResult struct represent the response returned by
         a fee rate estimator.
    
    iii - FeeRateEstimatorType will be used to identify fee estimators.
          Each time a new fee estimator is added, a corresponding
          enum value should be added.
    
    Co-authored-by: willcl-ark <will@256k1.dev>
    5844aba3ea
  23. fees: make block_policy_estimator a FeeRateEstimator
    - This commit updates CBlockPolicyEstimator class to implement
      the FeeRateEstimator interface.
    
    Co-authored-by: willcl-ark <will@256k1.dev>
    61be2183ae
  24. fees: add FeeRateEstimatorManager class
    - Introduce FeeRateEstimatorManager, a module for managing
      multiple fee rate estimators. This manager allows registration
      of multiple FeeRateEstimator instances, enabling having more fee rate
      estimation strategies to be used in the bitcoin core node and wallet.
    
    Co-authored-by: willcl-ark <will@256k1.dev>
    2100684aea
  25. fees: add typed accessor for FeeRateEstimator
    - Add GetFeeRateEstimator(), a templated method that returns a specific
      estimator instance by type.
    dc9b342ae4
  26. fees: replace `CBlockPolicyEstimator` with `FeeEstimatorManager`
    - Initialize FeeEstimatorManager in node instead of BlockPolicyEstimator.
    - Forward fee rate estimation requests through FeeEstimatorManager.
    - Update Chain interface to return FeeRateEstimatorResult, removing the out parameter requirement.
    - Retain selected BlockPolicyEstimator methods for test-only estimaterawfee RPC compatibility.
    cfcf01bd87
  27. fees: add `CalculatePercentiles` function
    - Given a vector of chunk feerates in the order they were added
      to the block, CalculatePercentiles function will return the 25th,
      50th, 75th and 95th percentile feerates.
    
    - This commit also add a unit test for the CalculatePercentile function.
    c480e8094e
  28. fees: add MemPoolFeeRateEstimator class
    - The mempool fee rate estimator uses the unconfirmed transactions in the mempool
      to generate a fee rate that a package should have for it to confirm as soon as possible.
    
    Co-authored-by: willcl-ark <will@256k1.dev>
    7b345746c3
  29. test: add mempool fee estimator unit test 1d713c6988
  30. fees: add caching to MemPoolFeeRateEstimator
    - Only refresh the cached fee rate estimate if it is older
          than 7 seconds.
    
    - This avoids generating block templates too often.
    
    Co-authored-by: willcl-ark <will@256k1.dev>
    5742c3f39e
  31. fees: return mempool estimates when it's lower than block policy 5be5ea073e
  32. rpc:fees: update estimatesmartfee to return errors whether it succeeds or not 9f0c97b39d
  33. test: ensure estimatesmartfee returns mempool fee rate estimate f2c4fb4b5f
  34. validation: add block transactions to `MempoolTransactionsRemovedForBlock` bf048fbf4b
  35. miner: remove unused node/context.h import 680dbed0f0
  36. fees: only return mempool fee rate estimate when mempool is healthy
    - This ensures that the users mempool has seen more than 75% of the
      last 6 blocks transactions.
    694b3ef34d
  37. fees: return mempool health stats from feerate estimator manager 34d50beca8
  38. fees: rename and move fee_estimates.dat to fees/block_policy_estimates.dat e193fc0605
  39. fees: add EncodedBlockDataFormatter struct 3de5535ce0
  40. fees: persist mempool estimator statistics
    - This commit also enable estimatesmartfee to return
      the stats when verbosity level is greater than 1.
    fee261249f
  41. ismaelsadeeq force-pushed on Dec 22, 2025
  42. DrahtBot removed the label Needs rebase on Dec 22, 2025
  43. ismaelsadeeq commented at 4:40 pm on January 8, 2026: member

    @brunoerg friendly ping for a mutation score and un-killed mutants 🔪


    I will fix the issues brought up by @DrahtBot soon.

  44. brunoerg commented at 5:00 pm on January 8, 2026: contributor

    @brunoerg friendly ping for a mutation score and un-killed mutants 🔪


    I will fix the issues brought up by @DrahtBot soon.

    I’m running it.


github-metadata-mirror

This is a metadata mirror of the GitHub repository bitcoin/bitcoin. This site is not affiliated with GitHub. Content is generated from a GitHub metadata backup.
generated: 2026-01-09 06:13 UTC

This site is hosted by @0xB10C
More mirrored repositories can be found on mirror.b10c.me