8 releases (4 stable)

new 3.0.0 Dec 27, 2024
2.0.0 Aug 9, 2024
1.1.0 May 23, 2024
1.0.0 Mar 22, 2024
0.1.0 Jun 14, 2021

#122 in Magic Beans

Download history 50/week @ 2024-09-11 93/week @ 2024-09-18 86/week @ 2024-09-25 43/week @ 2024-10-02 31/week @ 2024-10-09 100/week @ 2024-10-16 43/week @ 2024-10-23 55/week @ 2024-10-30 37/week @ 2024-11-06 24/week @ 2024-11-13 30/week @ 2024-11-20 40/week @ 2024-11-27 68/week @ 2024-12-04 68/week @ 2024-12-11 23/week @ 2024-12-18 132/week @ 2024-12-25

293 downloads per month
Used in 12 crates (5 directly)

MIT/Apache

105KB
1.5K SLoC

framing_sv2

crates.io docs.rs rustc+ license codecov

framing_sv2 provides utilities for framing messages sent between Sv2 roles, handling both Sv2 message and Noise handshake frames.

The Sv2 protocol is binary, with fixed message framing. Each message begins with the extension type, message type, and message length (six bytes in total), followed by a variable length message. The message framing is outlined below (according to Sv2 specs ):

Field Name Byte Length Description
extension_type U16 Unique identifier of the extension associated with this protocol message.
msg_type U8 Unique identifier of this protocol message.
msg_length U24 Length of the protocol message, not including this header.
payload BYTES Message-specific payload of length msg_length. If the MSB in extension_type (the channel_msg bit) is set the first four bytes are defined as a U32 "channel_id", though this definition is repeated in the message definitions below and these 4 bytes are included in msg_length.

Some bits of the extension_type field can also be repurposed for signaling on how the frame should be handled across channels.

The least significant bit of extension_type (i.e.bit 15, 0-indexed, aka channel_msg) indicates a message which is specific to a channel, whereas if the most significant bit is unset, the message is to be interpreted by the immediate receiving device.

Note that the channel_msg bit is ignored in the extension lookup, i.e.an extension_type of 0x8ABC is for the same "extension" as 0x0ABC.

If the channel_msg bit is set, the first four bytes of the payload field is a U32 representing the channel_id this message is destined for (these bytes are repeated in the message framing descriptions below).

Note that for the Job Declaration and Template Distribution Protocols the channel_msg bit is always unset.

Main Components

  • Header: Defines the 6-byte Sv2 message header with information about the message payload, including its extension type, if it is associated with a specific mining channel, the type of message (e.g. SetupConnection, NewMiningJob, etc.) and the payload length.
  • Sv2 Framing: Use for serializing Sv2 messages.
  • Noise Handshake Framing: Use for serializing Noise protocol handshake messages.

Usage

To include this crate in your project, run:

cargo add framing_sv2

This crate can be built with the following feature flags:

  • with_buffer_pool: Enables buffer pooling for more efficient memory management.
  • with_serde: builds binary_sv2 and buffer_sv2 crates with serde-based encoding and decoding. Note that this feature flag is only used for the Message Generator, and deprecated for any other kind of usage. It will likely be fully deprecated in the future.

Examples

This crate provides an example demonstrating how to serialize and deserialize Sv2 message frames:

  1. Sv2 Frame: Constructs, serializes, and deserialize a regular Sv2 message frame (Sv2Frame).

Dependencies

~305–750KB
~13K SLoC