fix(coin_selection): avoid per-utxo fee rounding#479
Open
benthecarman wants to merge 1 commit intobitcoindevkit:masterfrom
Open
fix(coin_selection): avoid per-utxo fee rounding#479benthecarman wants to merge 1 commit intobitcoindevkit:masterfrom
benthecarman wants to merge 1 commit intobitcoindevkit:masterfrom
Conversation
select_sorted_utxos computed fee_rate * weight per utxo and summed the results. Because FeeRate * Weight floors to whole sats, each input dropped up to 0.999 sats of fractional fee. The error grew linearly with the input count: at 1 sat/vb on 271-wu inputs (67.75 sat each), four inputs underpay by 3 sats; ten underpay by 7 sats; large selections drift further. Accumulate input weight during selection and compute the fee once on the total. Flooring now happens at most once for the whole tx, so the result equals the fee a sender would compute by weighing the finished transaction and never falls short of the requested fee rate. This was found when comparing fee calculation differences between LDK and BDK.
Codecov Report✅ All modified and coverable lines are covered by tests. Additional details and impacted files@@ Coverage Diff @@
## master #479 +/- ##
=======================================
Coverage 80.05% 80.05%
=======================================
Files 24 24
Lines 5360 5360
Branches 244 244
=======================================
Hits 4291 4291
Misses 990 990
Partials 79 79
Flags with carried forward coverage won't be shown. Click here to find out more. ☔ View full report in Codecov by Sentry. 🚀 New features to boost your workflow:
|
benthecarman
added a commit
to benthecarman/ldk-node
that referenced
this pull request
May 5, 2026
Update rust-lightning to commit 2313bd5 along with the matching
bitcoin-payment-instructions fork. The new splice API computes its
fee estimate independently of the BDK coin selection it drives, so
any surplus from BDK's higher reservation flows into the new
funding output instead of staying in change.
Bridge the gap in `select_confirmed_utxos`:
* Strip 5 WU per foreign input when calling `add_foreign_utxo`
so BDK doesn't double-count the empty script_sig byte and
witness-count varint that LDK's `satisfaction_weight` already
includes.
* Bump the change output up by the residual rounding surplus
from BDK's per-component fee ceilings — goes away once
bitcoindevkit/bdk_wallet#479 ships.
Generated with the help of Claude Opus 4.7.
benthecarman
added a commit
to benthecarman/ldk-node
that referenced
this pull request
May 5, 2026
Update rust-lightning to commit 2313bd5 along with the matching
bitcoin-payment-instructions fork. The new splice API computes its
fee estimate independently of the BDK coin selection it drives, so
any surplus from BDK's higher reservation flows into the new
funding output instead of staying in change.
Bridge the gap in `select_confirmed_utxos`:
* Strip 5 WU per foreign input when calling `add_foreign_utxo`
so BDK doesn't double-count the empty script_sig byte and
witness-count varint that LDK's `satisfaction_weight` already
includes.
* Bump the change output up by the residual rounding surplus
from BDK's per-component fee ceilings — goes away once
bitcoindevkit/bdk_wallet#479 ships.
Generated with the help of Claude Opus 4.7.
Collaborator
|
I see what you mean about avoiding per-utxo fee rounding, just confused by the "floors to whole sats" comment since bitcoin's |
Contributor
Author
|
Ah yeah, that should be |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
select_sorted_utxos computed fee_rate * weight per utxo and summed the results. Because FeeRate * Weight ceils to whole sats, each input dropped up to 0.999 sats of fractional fee. The error grew linearly with the input count: at 1 sat/vb on 271-wu inputs (67.75 sat each), four inputs underpay by 3 sats; ten underpay by 7 sats; large selections drift further.
Accumulate input weight during selection and compute the fee once on the total. Flooring now happens at most once for the whole tx, so the result equals the fee a sender would compute by weighing the finished transaction and never falls short of the requested fee rate.
This was found when comparing fee calculation differences between LDK and BDK.
Description
Notes to the reviewers
Changelog notice
Checklists
All Submissions:
just pbefore pushingBugfixes: