Repository navigation
fix(wallet): account for output count growth when adding change - #600
artofbitcoin wants to merge 2 commits into
Conversation
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.
|
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
left a comment
There was a problem hiding this comment.
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)staysNone, so thehas_changechange 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.
|
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 |
Description
Fixes #578.
When
create_txadds 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_changeaccounts 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, asdecide_changealready does for dust change. The change keychain index is only revealed when a change output is actually added.Notes to the reviewers
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.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.cargo testpasses for the whole crate,cargo fmt --checkis clean andcargo clippyreports 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