Skip to content

refactor(framing): replace Sv2Frame dual-Option fields with FrameBody enum#2128

Open
ThomsenDrake wants to merge 1 commit intostratum-mining:mainfrom
ThomsenDrake:refactor-sv2frame-enum-fix-2111-2112-2113
Open

refactor(framing): replace Sv2Frame dual-Option fields with FrameBody enum#2128
ThomsenDrake wants to merge 1 commit intostratum-mining:mainfrom
ThomsenDrake:refactor-sv2frame-enum-fix-2111-2112-2113

Conversation

@ThomsenDrake
Copy link
Copy Markdown

Fixes #2111, #2112, #2113

Summary

Replaces Sv2Frame<T, B>'s dual Option<T> + Option<B> fields with a type-safe FrameBody<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 3 panic!("Impossible state") branches.

Changes

Core refactor (fixes #2111)

  • Added FrameBody<T, B> enum with Payload(T) and Serialized(B) variants
  • Sv2Frame<T, B> now uses a single body: FrameBody<T, B> field
  • serialize(), encoded_length() are now exhaustive matches (no panic branches)
  • map() updated to match on body variants

API soundness (fixes #2112)

  • Sv2Frame::get_header() now returns Header directly (was Option<Header> — always returned Some)
  • Updated all call sites (codec-sv2 decoder tests, examples)

API soundness (fixes #2113)

  • HandShakeFrame::from_bytes() now returns Self directly (was Result<Self, isize> — always returned Ok)

Testing

All 9 tests pass:

  • framing-sv2: 3/3 (default)
  • codec-sv2: 6/6 (default + with_buffer_pool features)

… 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).
@Alkamal01
Copy link
Copy Markdown

Reviewed the changes and verified them by pulling the branch locally.

The refactor to FrameBody is a great improvement — it eliminates the dual-Option field logic and correctly removes the panic! branches in favors of exhaustive matching. The return type refinements for get_header and HandShakeFrame::from_bytes also help clean up the public API.

All tests for framing-sv2 and codec-sv2 (unencrypted, noise-sv2, and with_buffer_pool) passed on my side. Nice work!

@ThomsenDrake
Copy link
Copy Markdown
Author

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.

@Alkamal01
Copy link
Copy Markdown

Thank you! Happy to help👍

Copy link
Copy Markdown
Collaborator

@Shourya742 Shourya742 left a comment

Choose a reason for hiding this comment

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

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.

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

Labels

None yet

Projects

None yet

3 participants