Skip to content

fix(wallet): account for output count growth when adding change - #600

Open
artofbitcoin wants to merge 2 commits into
bitcoindevkit:masterfrom
artofbitcoin:fix/change-output-compact-size
Open

artofbitcoin wants to merge 2 commits into
bitcoindevkit:masterfrom
artofbitcoin:fix/change-output-compact-size

Conversation

@artofbitcoin

Copy link
Copy Markdown

Description

Fixes #578.

When create_tx adds the change output, the output count can cross a CompactSize boundary: with 252 recipients, the change output brings it to 253, and the count field grows from 1 to 3 bytes. decide_change accounts for the change output itself but not for those 2 bytes, so the final feerate ends up slightly below the requested one. The TODO at the change-output insertion described this case.

The change output now pays for that growth: its value is reduced by fee_rate * growth. If that leaves it below the dust limit, no change output is added and the remainder goes to the fee, as decide_change already does for dust change. The change keychain index is only revealed when a change output is actually added.

Notes to the reviewers

  • The growth is computed as VarInt(n + 1).size() - VarInt(n).size(), so it is 0 except at the 253, 65,536 and 2^32 boundaries. Every other transaction is built exactly as before.
  • New test test_create_tx_change_output_crossing_compact_size_boundary: 252 recipients at 10 sat/vB, plus change. Without the fix it fails with an effective rate of 2499 sat/kwu instead of 2500; with the fix it passes.
  • I did not add a test for the case where the reduced change falls below the dust limit.
  • cargo test passes for the whole crate, cargo fmt --check is clean and cargo clippy reports no new warnings.

Changelog notice

Fixed: the fee of a transaction whose change output makes the output count cross a CompactSize boundary (e.g. 252 to 253 outputs) now matches the requested fee rate.

Before submitting

Adding the change output can grow the CompactSize encoding of the
output count (e.g. from 252 to 253 outputs), which coin selection does
not account for. The change output now pays for those extra bytes. If
that leaves it below the dust limit, no change output is added and the
remainder goes to the fee.
@artofbitcoin

Copy link
Copy Markdown
Author

Hi, gentle ping on this one. It's still in Triage, and the branch still merges cleanly on master. Could a maintainer approve the three pending workflows and take a look when you have a moment? Happy to adjust anything. Thanks!

@Jean-Regis-M Jean-Regis-M left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

tACK 3e3b783

Ran it on Windows (Rust 1.100). The new test fails on the base e46e668 with Expected fee rate of 2500, the tx has 2499 and passes on this branch. I also printed the numbers, since coin selection picks one or two inputs at random here and I wanted both cases:

inputs fee weight (with fake sigs) effective rate
base 1 79,215 31,694 2499.37 sat/kwu
base 2 79,895 31,966 2499.37 sat/kwu
PR 1 79,235 31,694 2500.00 sat/kwu
PR 2 79,915 31,966 2500.00 sat/kwu

So the 20 sats (2 bytes at 10 sat/vB) are exactly what was missing.

For the dust branch you mentioned not covering, I spent a single UTXO with add_utxo(..).manually_selected_only() and picked the funding so the change before the adjustment is 304 sats (just above the 294 P2WPKH dust limit), so 284 after it:

  • the change output is dropped, the tx has 252 outputs and the remainder goes to the fee (2519 sat/kwu);
  • derivation_index(KeychainKind::Internal) stays None, so the has_change change to the reveal works as intended;
  • with 20 more sats of funding, the change is 304 and the rate is exactly 2500.

manually_selected_only makes it deterministic, so it would make a good second test. Happy to push it as a follow-up if you'd rather keep this PR as is.

The absolute-fee path is unaffected, since fee_rate is FeeRate::ZERO there and the adjustment is 0.

cc @notmandatory since you confirmed #578.

@artofbitcoin

Copy link
Copy Markdown
Author

Thanks @Jean-Regis-M for testing both input counts and the dust branch. Your table matches what I expected: the 2 missing bytes were the change output's varint growth. A follow-up PR with the manually_selected_only dust test sounds good to me. I'll keep this one as is so it stays small for review.

@notmandatory notmandatory added the bug Something isn't working label Nov 25, 2026
@notmandatory notmandatory moved this from Triage to Needs Review in BDK Wallet Nov 25, 2026

This branch has not been deployed

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

Labels

bug Something isn't working

Projects

Status: Needs Review

Development

Successfully merging this pull request may close these issues.

Change output crossing a compactsize boundary leaves the fee slightly below the requested feerate

3 participants