Followups to #4998 (fix TransactionDecoder)#5129
Merged
apoelstra merged 4 commits intorust-bitcoin:masterfrom Oct 14, 2025
Merged
Followups to #4998 (fix TransactionDecoder)#5129apoelstra merged 4 commits intorust-bitcoin:masterfrom
apoelstra merged 4 commits intorust-bitcoin:masterfrom
Conversation
The state machine logic in TransactionDecoder is a bit frustrating. We need to obtain a mutable reference to the current sub-decoder to attempt to feed data to it. If this fails, we then need to move the sub-decoder, attempt to end() it, then replace it if this succeeds. (And the replacement uses the return value from end() so we can't reorder this.) Having the end() between the move and replacement means we need a dummy Transitioning state, and having a mutable borrow before the move means we need some borrowck trickery. This commit separates the mutable-borrow logic from the move logic to avoid the trickery. It retains the Transitioning dummy state which I can't find a way to avoid. Refactor only; no observable behavior changes.
Search-and-replace the error variant name, and update all the panic conditions that are triggered by it. From a user point of view this state does not represent "transitioning". It represents that we called end() on a sub-decoder and that call yielded an error, leaving us with no meaningful state. The docs for Decoder::end and Decoder::current_chunk say that after returning the initial error we are allowed to panic; update the panic message to say that this is what we're doing. (We also have a panic in read_limit, which we are not allowed to do, so replace that with a dummy value return.) No observable behavior changes except that some panic messages change.
…o end If the data stream ends early we shouldn't panic. This is not a programmer error. It indicates that we ran out of data. This one has observable changes. Unit test in next commit.
nyonson
reviewed
Oct 13, 2025
| } | ||
|
|
||
| let decoder = self.state.inputs_transition(); | ||
| // If the above failed, end the current decoder and go to the next state. |
Contributor
There was a problem hiding this comment.
nit: the "failed" in this comment and "success" up on line 391 had me a little confused thinking it was an error. FWIW I thought of the success as "more bytes required" and failure as "sufficient bytes for next state".
Member
Author
There was a problem hiding this comment.
Hmm, yeah, that's a good way of looking at it. I was thinking of it like "succeeded means we're done for now".
tcharding
approved these changes
Oct 13, 2025
apoelstra
added a commit
that referenced
this pull request
Oct 14, 2025
144adfc consensus_encoding: remove Transitioning state (Nick Johnson) Pull request description: Inspired by cleanup in #5129, simplify the `Decoder2State` by dropping the unnecessary Transitioning variant. We can just use the `Errored` variant and not change the state of the `Decoder2` if the first decoder fails while transitioning to the second. ACKs for top commit: apoelstra: ACK 144adfc; successfully ran local tests tcharding: ACK 144adfc Tree-SHA512: 23af024d28fdc8d9f8859c9aaf7ab68cffabb948581dce80ae9d4d0bc66adf01f7455804f6ca67209696a2d74b6857a93208e1595e46c8bef9f7f0a603c77d35
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.
Refactors the
TransactionDecoderstate machine in a couple ways, eliminating some unreachable panic paths and also eliminating most of the reachable panic paths inend()(which should have been error returns).Fixes two of the four followup issues to #4998 -- the other two are optimizing
OutpointDecoderwhich is non-essential/low-priority, and eliminating theFromimpls required by the compositeDecodertypes (which is a somewhat-hard API problem and I don't think we want to hold up the rc on it).Fixes #5125
Fixes #5122