Repository navigation
Conversation
|
I've assigned @tnull as a reviewer! |
tnull
left a comment
There was a problem hiding this comment.
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 estimatesmartfee has no data on regtest, so there's nothing for convert_fee_rate What I'd do instead: per-target defaults on mainnet/testnet, keep the 1 sat/vb (b): yes, agreed. On the apply_post_estimation_adjustments question from the description: I don't |
b246cb5 to
6143b78
Compare
|
@tnull (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 (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 |
6143b78 to
260607e
Compare
Camillarhi
left a comment
There was a problem hiding this comment.
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.
|
|
||
| // 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()) |
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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).
| 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(), | ||
| ); |
There was a problem hiding this comment.
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?
There was a problem hiding this comment.
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.
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. A non-positive entry is treated as no entry at all, as it is no more usable than a missing one and must not pin the target to the floor either. 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 maps on and off Mainnet, for non-positive entries, 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 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_016RDZbFbxkpNupzvTFDsL43
A missing estimate should mean the same thing no matter which backend we happen to be on, so have the remaining two chain sources fall back through `get_fallback_fee_rate_for_network` as well. 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 this series removes from the Esplora source was reachable here too, mainnet included. We report the targets we had to fall back for, as the Esplora source now does, and 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 it means we no longer abandon the whole update when a single `estimatesmartfee` call fails off mainnet, which kept none of the estimates we did get, and that the fallback no longer runs through the post-estimation adjustments. The regtest/signet rates are unchanged, and mainnet still fails hard. This change was written with the assistance of Claude Code. Co-Authored-By: Claude Opus 5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_016RDZbFbxkpNupzvTFDsL43
A few follow-ups to the fee estimate fallback handling. For Esplora, we previously filtered non-positive values only after `convert_fee_rate` had picked an entry. As it settles on the closest block count at or below the requested one, a bogus entry there would shadow a usable one further down and send the target to its fallback even though we had an estimate for it. We now drop non-positive entries upfront, which also means a map holding nothing usable is rejected on mainnet just as an empty one is. For Electrum, a server answering `-1` for every block count, or handing us nothing we could parse, left us with no usable estimate at all, yet we'd happily cache the fallback for every target on mainnet. We now reject such an update there, matching what we do for an empty Esplora response. A single usable estimate is still enough to carry on, with the remaining targets falling back individually. To allow testing this without an Electrum server, we move the cache construction to `build_fee_rate_cache`. For Bitcoin Core, we expect `estimatesmartfee` to fail for every target on `regtest`/`signet`, so logging the per-target fallback at error level there is pure noise. We log it at trace level on those two networks instead. This change was written with the assistance of Claude Code. Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>
The Esplora and Electrum sources fall back to a target's default when they have no usable estimate for it, and on mainnet only reject the update if they got no usable estimate at all. Bitcoin Core however still failed the whole update on mainnet as soon as a single `estimatesmartfee` call errored, e.g., as it lacks the data for one of our block counts, discarding the estimates it did give us. Here we bring it in line, moving the cache construction to a `build_fee_rate_cache` that, just as for the other two sources, falls back per target and rejects the update on mainnet only if we didn't get a single usable estimate. Note we don't count the mempool minimum fee towards that, 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. As for the other sources, we now log the targets we fell back for in a single warning, staying quiet on `regtest`/`signet` where that is every target, and log the per-target errors at debug level, or at trace level on `regtest`/`signet`. This change was written with the assistance of Claude Code. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
260607e to
715950e
Compare
Thanks for the review @Camillarhi! Rebased on current main, which resolves the 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- |
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_ratealready falls back toget_fallback_rate_for_targeton a cache miss, so writing 1 sat/vb in only shadows the value we would have used.UrgentOnChainSweepends 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_networkthat 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
estimatesmartfeehas 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 splitbitcoind.rsalready made. The stand-in stays adjusted as an estimate would be, as those networks have always cached it that way:MaximumFeeEstimateat 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.
convert_fee_ratesettles 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.-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.estimatesmartfeecall no longer abandons the whole update, on mainnet included. The mempool minimum fee (used forMinAllowedAnchorChannelRemoteFee) 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_cacheper source (plus Electrum'sfee_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 fullintegration_tests_rustsuite, 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.