fix: define Candidate::weight as segwit-serialized, fixing multi-input candidates - #61
Open
evanlinjin wants to merge 1 commit into
Open
Conversation
…oups In a segwit transaction every legacy input still serializes an empty witness. `input_weight` accounted for that byte by adding it once per non-segwit *candidate*, which is only correct when a candidate represents a single input. A candidate grouping N legacy inputs was undercounted by N-1 weight units, and one mixing legacy and segwit spends by its full legacy count -- `is_segwit` had to be true for the marker and flag, which suppressed the adjustment its legacy inputs still needed. Fix it at the definition instead of the use. `Candidate::weight` now means the input weight as serialized in a segwit transaction, so a legacy input counts its empty witness up front, where each input is still visible individually. `is_segwit` is then only the transaction-level marker and flag question, and grouping inputs of either script type into one candidate works. `input_weight` correspondingly stops adjusting per candidate: if nothing selected is segwit the transaction has no witness section at all, so every input takes its byte back. Existing tests only used single-input candidates, which is why this went unnoticed. The new test weighs one transaction under three groupings of the same three inputs -- one per candidate, the two legacy inputs shared, and all three shared -- and asserts all agree with the real serialized weight. Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
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.
Bug:
CoinSelector::input_weightundercounts the transaction weight when aCandidaterepresents more than one input.In a segwit transaction, every input serializes a witness — a legacy input serializes an empty one, costing 1 WU. A segwit input already counts its witness in
Candidate::weight; a legacy input doesn't, soinput_weightcompensated:But the cost is 1 WU per legacy input, not per candidate:
is_segwitistrue, so the branch is skipped)Fix: put the byte into
weightwhen the candidate is built, while each input is still individually visible.Candidate::weightnow means the weight of the inputs as serialized in a segwit transaction: legacy inputs include their empty-witness byte.is_segwitonly decides whether the tx pays the 2 WU witness marker + flag.input_weightgives 1 WU per input back.⚠ Silent breaking change: hand-built candidates that use
txin.legacy_weight().to_wu()still compile but are now 1 WU short per legacy input — usetxin.legacy_weight().to_wu() + 1.Candidate::newalready does this for you.Test: one real tx (2 legacy + 1 segwit inputs) weighed under three groupings — one candidate per input, the legacy pair grouped, all three in one candidate. Each must equal
tx.weight(); all three fail on unfixedmaster.🤖 Generated with Claude Code