Skip to content

Fall back to per-target defaults for missing fee estimates - #1042

Open
Jolah1 wants to merge 2 commits into
lightningdevkit:mainfrom
Jolah1:fix-sparse-esplora-fee-estimates
Open

Jolah1 wants to merge 2 commits into
lightningdevkit:mainfrom
Jolah1:fix-sparse-esplora-fee-estimates

Conversation

@Jolah1

@Jolah1 Jolah1 commented Aug 11, 2026 •

Copy link
Copy Markdown
Contributor

Fixes #1028.

When a chain source gives us no usable estimate for a confirmation target, we used to cache 1 sat/vb for it. That is worse than caching nothing: estimate_fee_rate already falls back to get_fallback_rate_for_target on a cache miss, so writing 1 sat/vb in only shadows the value we would have used. UrgentOnChainSweep ends up at 250 sats/kwu where its default is 5000.

We cache the target's own default instead, through one get_fallback_fee_rate_for_network that all three chain sources call. The default is not run through the post-estimation adjustments, so it matches what the cache-miss path returns.

(a) Regtest Esplora returns an empty map, since estimatesmartfee has nothing to go on there, so the fallback decides every rate rather than the odd starved target. Mainnet-sized defaults make no sense without a fee market, so regtest and signet keep the 1 sat/vb they have always had, which is the split bitcoind.rs already made. The stand-in stays adjusted as an estimate would be, as those networks have always cached it that way: MaximumFeeEstimate at 2775 sats/kwu, not the raw 250, which matters because LDK sizes a channel's dust exposure limit off it.

(b) All three sources now follow the same rule: a target without a usable estimate falls back on its own, and on mainnet we only reject the update if we got no usable estimate at all.

  • Esplora: non-positive entries are dropped before an estimate is picked, as convert_fee_rate settles on the closest block count at or below the target and a bogus entry there would otherwise shadow a usable one further down. A map with nothing usable is rejected on mainnet, like an empty one.
  • Electrum: -1 (no estimate for that block count) and results that aren't numbers were floored to 1 sat/vb, so this was reachable there too, mainnet included. They now fall back, and a response with nothing usable is rejected on mainnet.
  • Bitcoin Core: a failed estimatesmartfee call no longer abandons the whole update, on mainnet included. The mempool minimum fee (used for MinAllowedAnchorChannelRemoteFee) doesn't count towards "usable", as Bitcoin Core reports it even with no fee estimates at all, e.g. while syncing, so such a node still fails on mainnet as before.

All three log the targets that fell back in one warning, and stay quiet on regtest/signet, where every target does.

Not addressed: a target that falls back gets its default even if the previous update had a good estimate for it, as each update replaces the whole cache, as it did before.

Testing

Cache construction moves into a pure build_fee_rate_cache per source (plus Electrum's fee_rate_from_estimate), so all of it is testable without a server. Unit tests cover sparse, empty and all-unusable responses on and off mainnet, non-positive Esplora entries (incl. one shadowing a usable estimate), partial and total Bitcoin Core failures (incl. the mempool-min-fee-only case), and the network-dependent fallback including the 2775. The sparse-map test fails against the old behaviour, and reverting the Esplora pre-filter, the Electrum mainnet rejection or the Bitcoin Core mempool-min-fee exclusion makes their tests fail. The full integration_tests_rust suite, plus the on-chain tests pinned to each of Esplora, Electrum, Bitcoind RPC and Bitcoind REST, ran locally against bitcoind 31.1 (as in CI) and electrs.

@ldk-reviews-bot

ldk-reviews-bot commented Aug 11, 2026 •

Copy link
Copy Markdown

I've assigned @tnull as a reviewer!
I'll wait for their review and will help manage the review process.
Once they submit their review, I'll check if a second reviewer would be helpful.

@ldk-reviews-bot
ldk-reviews-bot requested a review from tnull August 11, 2026 15:36

@tnull tnull left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

Please excuse the delay here!

Yes, we're currently considering Esplora/Electrum servers to be somewhat trusted (for fee rates, but also privacy-wise). It could make sense to apply additional checks to the returned fee rates (i.e., also somewhat bound them), but this is generally pretty delicate as we'd still need to make sure that we can handle legitimate fee rate spikes.

As for falling back to default values - yes, that's somewhat legitimate also but a) different networks (e.g., regtest) probably need to be handled differently and b) if we do this, we'd probably at the very least implement the same behavior across all three chain sources?

@Jolah1

Jolah1 commented Sep 21, 2026

Copy link
Copy Markdown
Contributor Author

Please excuse the delay here!

Yes, we're currently considering Esplora/Electrum servers to be somewhat trusted (for fee rates, but also privacy-wise). It could make sense to apply additional checks to the returned fee rates (i.e., also somewhat bound them), but this is generally pretty delicate as we'd still need to make sure that we can handle legitimate fee rate spikes.

As for falling back to default values - yes, that's somewhat legitimate also but a) different networks (e.g., regtest) probably need to be handled differently and b) if we do this, we'd probably at the very least implement the same behavior across all three chain sources?

Thanks for the review @tnull , and no worries about the delay!

(a): turns out regtest doesn't serve estimates at all. I ran the electrs build
the tests use (esplora_a33e97e1) against bitcoind 29.0:

  $ curl localhost:3002/fee-estimates
  {}

estimatesmartfee has no data on regtest, so there's nothing for convert_fee_rate
to pick up. That means the fallback decides every fee rate on regtest/signet, not
just a rare sparse-map case, and as written this PR moves all of them onto
mainnet-sized defaults: UrgentOnChainSweep 253 -> 5000 sat/kwu, ChannelFunding
253 -> 1000. That seems clearly wrong for a chain with no fee market.

What I'd do instead: per-target defaults on mainnet/testnet, keep the 1 sat/vb
behavior on regtest/signet, i.e. the same split bitcoind.rs:612-621 already does.

(b): yes, agreed.

On the apply_post_estimation_adjustments question from the description: I don't
think the fallback should go through it. 8000 has been the no-estimate ceiling
since 2024, and the adjustment was added later (02208ab) to relax the
estimate-derived values, without touching the fallbacks. Sending the default
through it would also mean the cache no longer matches what estimate_fee_rate
returns on a cache miss. If 8000 is too low nowadays that feels like a separate
change to get_fallback_rate_for_ldk_target.

@Jolah1
Jolah1 force-pushed the fix-sparse-esplora-fee-estimates branch from b246cb5 to 6143b78 Compare October 14, 2026 09:50
@Jolah1 Jolah1 changed the title Fall back to per-target defaults for sparse Esplora fee estimates Fall back to per-target defaults for missing fee estimates Oct 14, 2026
@Jolah1

Jolah1 commented Oct 14, 2026 •

Copy link
Copy Markdown
Contributor Author

@tnull
Pushed, rebased on current main. Both points are in.

(a) regtest/signet keep their 1 sat/vb, mainnet/testnet get the per-target defaults. The regtest/signet value stays adjusted as an estimate would be, so MaximumFeeEstimate is still 2775 sats/kwu rather than the raw 250, which
LDK's dust exposure limit is sized off.

(b) All three sources share one get_fallback_fee_rate_for_network. Electrum's -1 and results that aren't numbers were floored to 1 sat/vb too, so this was reachable there on mainnet, and Bitcoin Core no longer discards the
estimates it already collected when one estimatesmartfee call fails off mainnet. (Scratch the short batch response I mentioned: batch_call returns one result per request or an error.)

@Jolah1
Jolah1 force-pushed the fix-sparse-esplora-fee-estimates branch from 6143b78 to 260607e Compare October 14, 2026 14:41

@Camillarhi Camillarhi 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.

Thanks for this!

Needs a rebase, there is conflict on electrum.rs after #1164.

On "same behavior across sources": mainnet still differs. Bitcoin Core fails the whole update on one error, Esplora only rejects an empty map, and Electrum accepts all--1 answers by falling back on every target.

Comment thread src/chain/esplora.rs Outdated

// Note a non-positive value is no more usable than a missing one, so we treat it as
// missing rather than letting it pin the target to 1 sat/vb.
let converted = esplora_client::convert_fee_rate(num_blocks, estimates.clone())

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.

convert_fee_rate picks the closest key before the > 0.0 filter, so a 0.0 there falls back even when a smaller key has a valid estimate. Filtering the map once before the loop would fix this.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Good catch, thanks. The map is now filtered once before picking any estimate, so a 0.0/negative entry can't shadow a usable one at a smaller block count. There's a test for exactly that case (non_positive_estimates_dont_shadow_usable_ones).

Comment thread src/chain/bitcoind.rs Outdated
Comment on lines 621 to 627
let fallback_fee_rate = get_fallback_fee_rate_for_network(target, network);
log_error!(
self.logger,
"Failed to retrieve fee rate estimates: {}. Falling back to defaults.",
"Failed to retrieve fee rate estimates: {}. Falling back to {} sats/kwu.",
e,
fallback_fee_rate.to_sat_per_kwu(),
);

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.

Nit: This logs an error for every target on regtest/signet, while Esplora and Electrum stay quiet there. Maybe log on those networks as well?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Done. Bitcoin Core now logs like the other two: a single warning listing the targets that fell back, which stays quiet on regtest/signet. The per-target error is at debug level, or trace on regtest/signet.

@Jolah1
Jolah1 force-pushed the fix-sparse-esplora-fee-estimates branch from 260607e to 715950e Compare November 24, 2026 04:41
@Jolah1

Jolah1 commented Nov 24, 2026

Copy link
Copy Markdown
Contributor Author

Thanks for this!

Needs a rebase, there is conflict on electrum.rs after #1164.

On "same behavior across sources": mainnet still differs. Bitcoin Core fails the whole update on one error, Esplora only rejects an empty map, and Electrum accepts all--1 answers by falling back on every target.

Thanks for the review @Camillarhi! Rebased on current main, which resolves the electrum.rs conflict with #1164.

On mainnet consistency: all three now share one rule. Each target falls back on its own when it has no usable estimate, and on mainnet the update is rejected only if nothing usable came back at all. Concretely: Electrum now rejects an all--1 response on mainnet, and Bitcoin Core no longer fails the whole update on a single estimatesmartfee error. For Bitcoin Core, the mempool minimum fee doesn't count as usable, since it's reported even with no estimates at all. So a node with no fee data still fails on mainnet, as an empty Esplora map does.
@tnull

@Camillarhi

Copy link
Copy Markdown
Contributor

Could you squash the follow-up commits into the ones they fix, so we end up with two?

Jolah1 and others added 2 commits December 3, 2026 06:07
Esplora may return a non-empty estimate map that still has no usable entry
for some of our confirmation targets, as `convert_fee_rate` only yields a
value if the map holds a block count at or below the requested one. We'd then
substitute 1 sat/vb, which is well below the default we'd otherwise use for
urgent targets: `UrgentOnChainSweep` would end up at 250 sats/kwu instead of
5000.

Note this isn't a case of a missing fallback, but of an actively harmful one:
`OnchainFeeEstimator::estimate_fee_rate` already falls back to
`get_fallback_rate_for_target` whenever it misses the cache, so inserting
1 sat/vb only serves to shadow the value we'd have used anyway.

Here we therefore fall back to the target's own default. We deliberately skip
the post-estimation adjustments for it, as there is no estimate to adjust, and
so that we land on the same rate the cache-miss path would have given us.

We also drop non-positive entries before picking an estimate, as they are no
more usable than a missing one. As `convert_fee_rate` settles on the closest
block count at or below the requested one, a bogus entry there would
otherwise shadow a usable one further down. A map left with nothing usable is
rejected on mainnet, just as an empty one is.

The defaults are sized for a chain that has a fee market, though, and
`regtest`/`signet` don't have one: regtest Esplora serves an empty map, as
`estimatesmartfee` has nothing to go on there, so the fallback would decide
every rate rather than the odd sparse-map target. We therefore stand in the
1 sat/vb we have always used on those two networks, which is the same split
`BitcoindChainSource` already makes, and adjust that stand-in exactly as we
would an estimate, because that is what those networks have always cached:
`MaximumFeeEstimate` stays at 2775 sats/kwu rather than dropping to the raw
250, which matters as LDK sizes a channel's dust exposure limit off it.
`get_fallback_fee_rate_for_network` holds that decision so the remaining chain
sources can share it.

Finally, we log the targets we had to fall back to a default for. On
`regtest`/`signet` that is every target on every update, so we keep quiet
there, but anywhere else it means we're pricing transactions off defaults
rather than off the chain, which shouldn't pass silently.

To allow testing this without an Esplora server, we move the cache
construction to `build_fee_rate_cache` and add coverage for sparse maps, for
empty and all-unusable maps on and off mainnet, for non-positive entries
including one that would shadow a usable estimate, for the network-dependent
fallback, and for the unchanged complete-map behavior.

This change was written with the assistance of Claude Code.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
A missing estimate should mean the same thing no matter which backend we
happen to be on, so have the remaining two chain sources follow the rule the
Esplora source now does: a target without a usable estimate falls back
through `get_fallback_fee_rate_for_network`, and on mainnet we only reject
the update if we didn't get a single usable estimate.

For Electrum that covers the results we can't use: a server that has no
estimate for the requested block count answers `-1`, and a result may not be
a number at all. Both used to be floored to 1 sat/vb, so the silent downgrade
of urgent targets was reachable here too, mainnet included. A response
holding nothing usable is now rejected on mainnet, matching an empty Esplora
response. We also index into the batch results rather than zipping them with
the targets, so that a short response could never silently drop the trailing
targets from the cache. Note `batch_call` returns one result per request or
an error, so that isn't a case we can hit today.

For Bitcoin Core, a single failing `estimatesmartfee` call, e.g., as it lacks
the data for one of our block counts, used to fail the whole update on
mainnet, and to abandon it on testnet, in both cases discarding the estimates
it did give us. It now falls back per target as well. Note we don't count the
mempool minimum fee towards a usable estimate, as Bitcoin Core reports it
even when it has no fee estimates at all, e.g., while still syncing, which is
exactly when we want to keep failing. The `regtest`/`signet` rates are
unchanged.

As for Esplora, we log the targets we fell back for in a single warning,
staying quiet on `regtest`/`signet` where that is every target, and log
Bitcoin Core's per-target errors at debug level, or at trace level on
`regtest`/`signet`.

To allow testing this without a server, we move the cache construction for
both sources to a `build_fee_rate_cache`, and add coverage for missing and
unusable estimates on and off mainnet, including Bitcoin Core reporting
nothing but the mempool minimum fee.

This change was written with the assistance of Claude Code.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@Jolah1
Jolah1 force-pushed the fix-sparse-esplora-fee-estimates branch from 715950e to 4fc3d29 Compare December 3, 2026 05:11
@Jolah1

Jolah1 commented Dec 3, 2026

Copy link
Copy Markdown
Contributor Author

Could you squash the follow-up commits into the ones they fix, so we end up with two?

Done, squashed into two: one for the shared fallback and Esplora, one for Electrum and Bitcoin Core. Also rebased on current main.

@Camillarhi

Copy link
Copy Markdown
Contributor

Claude has to add:

Looks good overall, and it fixes #1028 as filed. One concern with the Bitcoin Core part: every failure while fetching an estimate now becomes a per-target fallback, including connection failures (RpcClientError::Http) and RPC errors such as warm-up -28 (RpcClientError::Rpc). Before this PR, a failed update on mainnet kept the previous chain-derived cache. Now a node whose connection flaps can overwrite those targets with static defaults; during a fee spike, UrgentOnChainSweep could drop to 5000 sat/kwu until the next update. This can also happen under load, as Core answers "Work queue depth exceeded" with an HTTP 503 that can hit some of the ~10 calls in an update but not others. On testnet, an error used to leave the cache alone and now writes defaults.

The "no data" case is distinguishable: when Core lacks data, estimatesmartfee answers with {"errors": [...]}, which FeeResponse surfaces as RpcClientError::InvalidData (or HttpClientError::Parse for the REST mempool-info call). Could we fall back only on those, and keep the old handling for everything else (fail on mainnet, return Ok(()) on testnet, stand-in on regtest/signet)? Moving that matching into a small helper would make it testable without a node. One thing to note: InvalidData also covers malformed responses, which would then count as "no data" too.

Nit: since the mainnet "no usable estimates" error then only fires when every target lacked data, it would help to include the first per-target error in that log line.

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

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Sparse Esplora fee estimates may fall back to 1 sat/vB for urgent targets

4 participants