refactor(framing): replace Sv2Frame dual-Option fields with FrameBody enum#2128
Conversation
… enum (fixes stratum-mining#2111) Eliminates the dual-Option pattern (payload + serialized) in Sv2Frame by introducing a type-safe FrameBody enum (Payload(T) | Serialized(B)). This removes 3 'Impossible state' panic branches from serialize(), encoded_length(), and makes the internal state impossible by construction. Also fixes stratum-mining#2112: get_header() now returns Header directly (always present). Fixes stratum-mining#2113: HandShakeFrame::from_bytes() now returns Self directly (always succeeded, Result was unnecessary). All 9 tests pass (3 framing + 6 codec, default + with_buffer_pool).
|
Reviewed the changes and verified them by pulling the branch locally. The refactor to All tests for |
|
Thanks for taking the time to verify locally, @Alkamal01! Appreciate the thorough review — glad the exhaustive matching and API cleanups checked out across all test suites. |
|
Thank you! Happy to help👍 |
Shourya742
left a comment
There was a problem hiding this comment.
Thanks for taking this up. We are yet to triage on how to move forward on protocol crates refactoring, as it has ripple effects on all the downstream crates.
Fixes #2111, #2112, #2113
Summary
Replaces
Sv2Frame<T, B>'s dualOption<T>+Option<B>fields with a type-safeFrameBody<T, B>enum (Payload(T) | Serialized(B)). This makes the invariant that exactly one of payload/serialized is present impossible to violate by construction, eliminating 3panic!("Impossible state")branches.Changes
Core refactor (fixes #2111)
FrameBody<T, B>enum withPayload(T)andSerialized(B)variantsSv2Frame<T, B>now uses a singlebody: FrameBody<T, B>fieldserialize(),encoded_length()are now exhaustive matches (no panic branches)map()updated to match on body variantsAPI soundness (fixes #2112)
Sv2Frame::get_header()now returnsHeaderdirectly (wasOption<Header>— always returnedSome)API soundness (fixes #2113)
HandShakeFrame::from_bytes()now returnsSelfdirectly (wasResult<Self, isize>— always returnedOk)Testing
All 9 tests pass: