Skip to content

fees: return block_policy fee rate estimate when mempool_policy is not ready - #36182

Closed
ismaelsadeeq wants to merge 5 commits into
bitcoin:masterfrom
ismaelsadeeq:09-2026-fee-estimator-not-ready-fallback
Closed

ismaelsadeeq wants to merge 5 commits into
bitcoin:masterfrom
ismaelsadeeq:09-2026-fee-estimator-not-ready-fallback

Conversation

@ismaelsadeeq

@ismaelsadeeq ismaelsadeeq commented Sep 7, 2026 •

Copy link
Copy Markdown
Member

The first commit of this PR has a minor non breaking API change, it renames the estimatesmartfee default estimator option name from "none" to "auto"


The mempool fee rate estimator can't always return an estimate:

i) right after first startup (or after re-syncing through IBD) it hasn't tracked
enough recent blocks yet;
ii) the mempool is empty or too sparse to fill the requested block percentile,
e.g. after a cold restart with persistence off or a missing/corrupt
mempool.dat.
iii) recent blocks cover too little of the mempool (low coverage); and
iv) the mempool is still loading.

Previously, case (ii) with a healthy mempool will returned the fee rate
floor (the higher of the minimum relay fee and the current mempool minimum fee).
This can be a significant underestimation, since the now-missing mempool may have
held much higher-feerate transactions. The remaining cases surfaced the mempool
error directly from the combined estimate.

This PR makes the combined estimate (estimatesmartfee with
fee_rate_estimator=none, the default) fall back to the block policy estimate
whenever the mempool estimator isn't ready, instead of returning the floor or
surfacing the error:

  • The mempool estimator now returns an error instead of flooring an unfilled
    percentile, so an empty/sparse mempool is treated like any other not-ready
    state rather than potentially silently underestimating.
  • The combined estimate starts from the block policy estimate and only lowers it
    with the mempool estimate when one is available. An error is returned only when
    the block policy estimate itself is unavailable.
  • Calling estimatesmartfee with fee_rate_estimator=mempool_policy explicitly
    still returns the failure.

When the mempool fails to load at startup (persistence off, or a missing/corrupt
mempool.dat), init notifies the estimator, which clears its tracked mined-block
window. Otherwise the estimator would keep a window describing a mempool the node
no longer has; clearing it makes the estimator report insufficient data until the
window refills from the current tip, so the combined estimate falls back to block
policy meanwhile.

This is an alternative to #36095.

@DrahtBot

DrahtBot commented Sep 7, 2026 •

Copy link
Copy Markdown
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/36182.

Reviews

See the guideline and AI policy for information on the review process.

Type Reviewers
Concept ACK sedited, achow101, jeanpablojp, pseudoramdom, willcl-ark
Approach ACK stickies-v
Stale ACK polespinasa

If your review is incorrectly listed, please copy-paste <!--meta-tag:bot-skip--> into the comment that the bot should ignore.

Conflicts

Reviewers, this pull request conflicts with the following ones:

  • #36091 (test: Add debug output to common tested types by rustaceanrob)
  • #33741 (rpc: Optionally print feerates in sat/vb by polespinasa)

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):

  • make_tx(self.wallet, self.wallet.get_utxo(confirmed_only=True), 10, quarter_block_vsize) in test/functional/feature_fee_estimation.py

2026-09-28 09:21:25

@sedited

sedited commented Sep 7, 2026

Copy link
Copy Markdown
Contributor

Concept ACK

@polespinasa

polespinasa commented Sep 7, 2026 •

Copy link
Copy Markdown
Member

Concept ACK

Are we too late to get this for v32? Would be nice to fix this, specially case 1 if #34075 is getting into v32.

@sedited

sedited commented Sep 7, 2026

Copy link
Copy Markdown
Contributor

Are we too late to get this for v32?

Not too late. Fixes can also still be made after branch off, if it not ends up making it in by the 10th.

@sedited sedited added this to the 32.0 milestone Sep 7, 2026

@polespinasa polespinasa left a comment •

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

reviewed b5c1242

Overall the patch looks pretty good to me :)

Comment thread src/policy/fees/mempool_estimator.h Resolved
Comment thread src/policy/fees/mempool_estimator.cpp Resolved
Comment thread src/policy/fees/estimator_man.cpp Resolved
Comment thread src/rpc/fees.cpp Resolved
Comment thread src/policy/fees/mempool_estimator.cpp Resolved
Comment thread src/rpc/fees.cpp Resolved
Comment thread src/policy/fees/mempool_estimator.h Resolved
Comment thread src/policy/fees/mempool_estimator.cpp Resolved
@ismaelsadeeq
ismaelsadeeq force-pushed the 09-2026-fee-estimator-not-ready-fallback branch from b5c1242 to a089204 Compare September 7, 2026 20:57
@achow101

achow101 commented Sep 7, 2026

Copy link
Copy Markdown
Member

Concept ACK

I'm not sure how useful it is to fallback to the block policy estimator for fresh starts as it also requires seeing a number of blocks before it works. However, I think it makes sense to fallback to block policy if it is available and mempool policy is not.

@jeanpablojp jeanpablojp left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Concept ACK

A question on the warm-up. The window keeps three of the six pre-restart blocks, and GetMempoolHealthCheck sums across the whole window, so those three still carry the ratio. Sweeping the three fresh blocks that follow, the check turns healthy once they reach 50% coverage, with the retained blocks fully covered and of the same weight, against 75% for a window of six fresh blocks.

So how far the bar drops is set by the blocks on their way out, not by the fresh ones being measured against it. Is that intended?

doc/release-notes-34075.md and one test comment still describe the old behaviour. Worth updating them here?

the two places

The release note still says the combined estimate returns an error when too few recent blocks have been observed, and the comment above the restart in test_estimatesmartfee_return_mempool_estimates says the same.

@ismaelsadeeq
ismaelsadeeq force-pushed the 09-2026-fee-estimator-not-ready-fallback branch from a089204 to abd2982 Compare September 8, 2026 12:23
@ismaelsadeeq

Copy link
Copy Markdown
Member Author

re: #36182 (review)

AFAICT, it's the same moving mempool representation ratio, now used as a proxy for mempool inflow. The same assumption holds even before a restart: if the first 3 blocks have 100% coverage, we only need 50% from the next 3 to stay healthy.

In the case you mentioned, 100% coverage before and then 50% after is a good sign your peers are relaying with sane policy rules, and the drop in coverage is most likely just your own lost mempool so using it for fee estimation won't be wildly off.

I fixed the docs as you suggested. The CI failure seems unrelated.

Thanks for the review.

Comment thread src/policy/fees/mempool_estimator.h Resolved
Comment thread src/policy/fees/mempool_estimator.h Resolved
Comment thread src/policy/fees/mempool_estimator.h Resolved

@pseudoramdom pseudoramdom left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Concept ACK. Left a few comments.

Comment thread src/policy/fees/mempool_estimator.cpp Resolved
Comment thread src/policy/fees/mempool_estimator.cpp Resolved
Comment thread src/policy/fees/mempool_estimator.cpp Resolved
Comment thread src/rpc/fees.cpp Resolved
Comment thread src/rpc/fees.cpp Outdated
"\"none\" returns the lower of the block policy and mempool estimates. If the mempool\n"
"estimate is unavailable, it returns that error instead of falling back to the block\n"
"policy estimate; use \"block_policy\" in that case to get the block policy estimate.\n"
"\"none\" (the default) combines both estimators, returning the lower of the block policy and\n"

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Unrelated to this PR -
It's weird that setting none uses all available estimators. This should probably have been called auto or combined and the enum case to be FeeRateEstimatorType::AUTOMATIC

Comment thread src/policy/fees/mempool_estimator.h Resolved
Comment thread src/policy/fees/mempool_estimator.cpp Resolved
@ismaelsadeeq

Copy link
Copy Markdown
Member Author

Forced pushed 651b0a9db877..86ef5cc277

  • The main change is that I simplified the fix: The mempool estimator now returns an error instead of the relay floor when it can't fill a percentile, and the combined estimate falls back to block_policy; hence, an explicit mempool_policy query still returns an error in that case. This should handle the most likely case of underpaying because nodes do frequently restart. But having a completely empty mempool on mainnet is rare, it usually occurs in test networks. And returning a block policy in this case won't hurt because it will also be on the low end or an insufficient data error, so users can explicitly pass the relay floor or use the fallbackfee feature in the wallet.

Comment thread src/policy/fees/mempool_estimator.cpp Resolved
@stickies-v

Copy link
Copy Markdown
Contributor

Approach ACK. I was feeling very uneasy about the amount of complexity (process flow more so than code) being tacked on in the state prior to the latest force-push (352283b..86ef5cc). I think the current approach of simply erroring (and falling back to block policy) whenever the mempool seems like it can't produce an estimate is the best choice for this backport. Solving the biggest problem with the least amount of complexity.

There are now a few quirks with empty percentiles never resulting in a mempool estimate, but I think their impact is limited enough that this remains a good approach. Some quirks:

  • economical estimate can end up a higher feerate than conservative (if p50 has a result, but p75 doesn't, the mempool estimate is used for conservative and the block policy estimate for economical)
  • as the mempool continually clears until it is almost fully empty, estimated fees will suddenly jump as p50/p75 become empty, the severity depending on how quickly the mempool clears/fees have changed.

I would prefer addressing these in a follow-up where we have less time pressure to find a good solution.

nit: the message of 247d470 still says the change is necessary for "the warmup fallback", which is no longer part of this PR.

const FeePerVSize feerate_estimate{conservative ? percentiles.p50 : percentiles.p75};
// An empty percentile means the mempool was too sparse to fill that fraction of a block.
if (feerate_estimate.IsEmpty()) {
return EstimationError(strprintf("%s: Insufficient mempool transaction data", FeeRateEstimatorTypeToString(estimator_type)));

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

86ef5cc (fees: return an error instead of a floored mempool fee rate estimate)

In the case of a node with a healthy view of the mempool, but a mempool that has less than a p50 or p75 of transactions, don't we actually want to return the floored estimate instead of falling back to the block policy estimator. Not frequent but from looking at the mempool.space chart it has happened as recently as February of this year on mainnet:

Image

And I also imagine this will in practice make the mempool estimator never engage on testnet/signet.

Question: why can't we just rely on the mempool health metrics here?

What are the odds that someone loads a mempool.dat that happens to do a good job matching the catch-up blocks during loading and that someone asks for a fee estimate during that time? And the current approach will only stop the fee estimate if that happens and at the same time the mempool happens to be kind of empty around the time the fee is being requested which isn't necessarily the case.

I wonder if maybe a check should be added to GetMempoolHealth() that the current tip height is close to the best known header or something like that but I'll have to look closer tomorrow.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Valid observation, we do want to return the floor in that case, but as it is now, we do not have a correct and final approach mechanism that differentiates whether we are indeed empty, have just connected, have lots of our mempool, on testnets etc. So a valid e2e fix can be pursued in a follow-up, given that it is very infrequent, this minimal fix is okay, I think.

@davidgumberg davidgumberg Sep 24, 2026 •

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm not sure I agree, I think it would be better to drop the last two commits entirely from this PR, since it only sometimes fixes a problem that likely no one will ever run into:

  • Fee misestimation because a user starts their node after a day off and their mempool.dat happened to be healthy for 6 blocks in a row (I think this is the thing that's most unlikely) and they request a feerate at that time and it happens that at that time their mempool is nearly empty [if not nearly empty the mempool estimator will still return a stale estimate]

And creates a problem that all users will run into infrequently:

  • Overestimating fees whenever the mempool is starved.

The actual fix would be to use a heuristic to decide whether or not we are at or close to the tip, and only return a block estimate then. I think using mempool is empty as a proxy for mempool is stale both produces false positives e.g. when the mempool is actually <3MvB and I suspect produces lots of false negatives, i.e. I suspect that if you restart a node after a few hours or a day of being offline that it will happen quite often that the mempool does not drain but I would have to check that it's just an assumption.

@ismaelsadeeq
ismaelsadeeq force-pushed the 09-2026-fee-estimator-not-ready-fallback branch from 86ef5cc to 3f68d2c Compare September 24, 2026 09:02
@ismaelsadeeq

Copy link
Copy Markdown
Member Author

Forced pushed 86ef5cc...3f68d2c to address feedback by @stickies-v and @davidgumberg

  • I added a check that prevents the economical estimates from being lower than the estimates, we only return an estimate when both have non-empty estimates.
  • I prevent logging that we clear the mined block stats when the vector is empty.
  • Removed stale mention of warmup in commit message.

@willcl-ark

Copy link
Copy Markdown
Member

I’m generally happy with the PR now.

One API naming question before 32.0: should we rename fee_rate_estimator=none to auto, as suggested above? I agree that none reads as though fee estimation is disabled, whereas it selects the default combined behavior.

I know it's not been introduced here, but this PR might be our best chance to fix the public name before 32.0 is released...

As we coerce all unknown values to none, anyone who has been testing and has none in a script/application already, would not see a breaking change, which makes it feel even more appealing to me.

It could also be it's own tiny "bugfix" PR perhaps, too.

Happy to ACK this with or without that change, but wanted to see what folks reviewing this thought.

@ismaelsadeeq
ismaelsadeeq force-pushed the 09-2026-fee-estimator-not-ready-fallback branch from 3f68d2c to dd85315 Compare September 25, 2026 11:19
@ismaelsadeeq

Copy link
Copy Markdown
Member Author

Forced pushed 3f68d2c...dd85315

I added a commit here to fix #36182 (comment)

Comment thread src/policy/fees/estimator_man.h Resolved
Comment thread src/policy/fees/mempool_estimator.cpp Resolved
Comment thread src/policy/fees/mempool_estimator.h Resolved
Comment thread src/policy/fees/estimator_man.cpp Resolved
"none" read as though fee estimation was disabled, when it actually
selects the default combined behavior: the lower of the block policy
and mempool fee rate estimates, falling back to the block policy
estimate when the mempool estimator can't produce one.

Rename the public value (and the FeeRateEstimatorType::NONE enumerator)
to "auto", which describes what it does. Unknown values are still
treated as to auto, so the previous "none" spelling works.
…mate

When the mempool policy estimator cannot produce an estimate, GetFeeRateEstimate()
now returns the block policy estimate rather than an error. The combined estimate
starts from the block policy estimate and lowers it with the mempool estimate only
when present, so the fallback path shares the log that reports the selected fee
rate. An error is returned only when the block policy estimate itself is unavailable.
When the mempool fails to load at startup (persistence disabled, or a
missing or corrupt mempool.dat), init notifies the fee_rate_estimator_man,
which clears its tracked mined-block window. Otherwise the estimator
would keep a window describing a mempool the node no longer has and,
once the mempool refilled, serve an estimate built on it.

Clearing makes the estimator report insufficient data until the window refills from the
current tip, so the combined estimate falls back to the block policy
estimate meanwhile.
Hoist the Percentiles struct to file scope and cache the raw p50/p75
percentiles, applying the relay floor on each call rather than caching the
already-floored conservative/economical fee rates. The template build moves
into GetOrBuildPercentiles().

Flooring per call lets a later commit tell an unfilled percentile apart from a
floored one, so it can return an error instead of the floor and let the
combined estimate fall back to the block policy estimate.
When the block template does not fill both the 50th and 75th percentiles of a
block, EstimateFeeRate() returns an error rather than flooring the estimate to
the higher of the minimum relay fee and the mempool minimum fee, so the
combined estimator falls back to the block policy estimate.

Both percentiles are required together so the economical (p75) estimate cannot
end up above the conservative (p50) one, which could happen if only p50 were
served while p75 fell back to the block policy estimate.
@ismaelsadeeq
ismaelsadeeq force-pushed the 09-2026-fee-estimator-not-ready-fallback branch from dd85315 to eb28c15 Compare September 28, 2026 09:21
@ismaelsadeeq
ismaelsadeeq marked this pull request as draft September 28, 2026 10:15
@maflcko maflcko removed this from the 32.0 milestone Sep 28, 2026
@ismaelsadeeq

Copy link
Copy Markdown
Member Author

@davidgumberg raised a concern that, with the last two commits, we may overestimate for everyone when the mempool is empty (see thread: #36182 (comment)).

My rationale for including those two commits before 32.0 is that they preserve the status quo: an empty mempool occurs rarely, whereas users restarting after a while and losing their transaction backlog happens more often. However, an empty mempool is also a valid state and with the last two commits when it occur we overestimate for everyone else.

I discussed this offline with @davidgumberg, and we concluded that the core issue is being able to distinguish a genuinely empty mempool from a missing backlog. We decided to move forward with the first three commits and discuss the issue further and also these are very edge cases that may not warrant the energy.

I split the PR and converted this one into a draft. The first three commits are, IMO, what should be in the 32.x milestone.

Thanks for the review and feedback, all.

achow101 added a commit that referenced this pull request Sep 29, 2026
5b77288 fees: clear mined-block stats when the mempool load fails (ismaelsadeeq)
c0b7ca3 fees: fall back to block_policy when the mempool estimator can't estimate (ismaelsadeeq)
4056908 fees: rename the default fee_rate_estimator value to "auto" (ismaelsadeeq)

Pull request description:

  This is split from #36182 to allow review of the first three commits that we want to backport to 32.x.

  This is a simple PR that does three things:

  1. A minor, non-breaking `estimatesmartfee` API update: it renames the default estimator option from "none" to "auto". See the rationale here: #36182 (comment)

  2. Falls back to the `block_policy` fee rate estimator when `mempool_policy` is not available. This preserves the previous behavior and lets users who update their node keep using `block_policy` estimates until the mempool policy has finished gathering stats and is ready to serve estimates.

  3. After a restart in which mempool loading fails, we clear the data gathered by the mempool fee rate estimator, to prevent it from returning the fee rate floor just because the node lost its mempool transactions.

ACKs for top commit:
  davidgumberg:
    re-crACK 5b77288
  achow101:
    ACK 5b77288
  willcl-ark:
    reACK 5b77288
  w0xlt:
    ACK 5b77288

Tree-SHA512: 4e5742899992ae6d0f062e436ed4da159896b02b7c60ce6aec0d2e4beb635e922d34154995256d33972e77269f5e60141432d6276df58e2e8b75300d3043de83
@DrahtBot

Copy link
Copy Markdown
Contributor

🐙 This pull request conflicts with the target branch and needs rebase.

Kino1994 pushed a commit to Kino1994/bitcoin-full-history that referenced this pull request Oct 5, 2026
…nor api change

5b77288673e2b74de1512180a8911f435311ca7a fees: clear mined-block stats when the mempool load fails (ismaelsadeeq)
c0b7ca3dbdbaecdd045c8a36d9758470358880a0 fees: fall back to block_policy when the mempool estimator can't estimate (ismaelsadeeq)
4056908f0feae2a1caf9b53cfbe77bb8091246c9 fees: rename the default fee_rate_estimator value to "auto" (ismaelsadeeq)

Pull request description:

  This is split from #36182 to allow review of the first three commits that we want to backport to 32.x.

  This is a simple PR that does three things:

  1. A minor, non-breaking `estimatesmartfee` API update: it renames the default estimator option from "none" to "auto". See the rationale here: bitcoin/bitcoin#36182 (comment)

  2. Falls back to the `block_policy` fee rate estimator when `mempool_policy` is not available. This preserves the previous behavior and lets users who update their node keep using `block_policy` estimates until the mempool policy has finished gathering stats and is ready to serve estimates.

  3. After a restart in which mempool loading fails, we clear the data gathered by the mempool fee rate estimator, to prevent it from returning the fee rate floor just because the node lost its mempool transactions.

ACKs for top commit:
  davidgumberg:
    re-crACK bitcoin/bitcoin@5b77288
  achow101:
    ACK 5b77288673e2b74de1512180a8911f435311ca7a
  willcl-ark:
    reACK 5b77288673e2b74de1512180a8911f435311ca7a
  w0xlt:
    ACK 5b77288673e2b74de1512180a8911f435311ca7a

Tree-SHA512: 4e5742899992ae6d0f062e436ed4da159896b02b7c60ce6aec0d2e4beb635e922d34154995256d33972e77269f5e60141432d6276df58e2e8b75300d3043de83
@ismaelsadeeq

Copy link
Copy Markdown
Member Author

Closing this due to the merge of the minimal approach in #36365, mempool backlog concerns and recovery discussed here can be revisited. I am not working on it at the moment, but I will review any related work or may pick it up again.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.