-
Notifications
You must be signed in to change notification settings - Fork 139
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #1231 from Shourya742/doc-binary-sv2
doc binary-sv2
- Loading branch information
Showing
16 changed files
with
1,533 additions
and
151 deletions.
There are no files selected for viewing
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,28 @@ | ||
# binary_sv2 | ||
# binary-sv2 | ||
|
||
`binary_sv2` is a Rust `no_std` crate | ||
[![crates.io](https://img.shields.io/crates/v/binary-sv2.svg)](https://crates.io/crates/binary-sv2) | ||
[![docs.rs](https://docs.rs/binary-sv2/badge.svg)](https://docs.rs/binary-sv2) | ||
[![rustc+](https://img.shields.io/badge/rustc-1.75.0%2B-lightgrey.svg)](https://blog.rust-lang.org/2023/12/28/Rust-1.75.0.html) | ||
[![license](https://img.shields.io/badge/license-MIT%2FApache--2.0-blue.svg)](https://github.com/stratum-mining/stratum/blob/main/LICENSE.md) | ||
|
||
`binary-sv2` is a Rust `no-std` crate that helps encode and decode binary data into Stratum V2 messages — either through `serde` or custom trait-based setup. Allowing it to be used in environment(s) where std or/and serde are not available. | ||
|
||
## Key Capabilities | ||
|
||
- **Protocol-Specific Types**: Supports fixed and dynamically-sized SV2 types. | ||
- **Optimized Memory Use**: Supports buffer pooling to enhance memory efficiency. | ||
|
||
## Features | ||
|
||
- **with_serde**: Enables `serde`-based encoding and decoding. | ||
- **core**: Activates non-`serde` implementations via `binary_codec_sv2` and `derive_codec_sv2`.(default) | ||
- **prop_test**: Adds property testing support. | ||
- **with_buffer_pool**: Optimizes memory usage during encoding. | ||
|
||
## Usage | ||
|
||
To include this crate in your project, run: | ||
|
||
```sh | ||
cargo add binary-sv2 | ||
``` |
118 changes: 118 additions & 0 deletions
118
protocols/v2/binary-sv2/binary-sv2/examples/encode_decode.rs
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,118 @@ | ||
pub use binary_codec_sv2::{self, Decodable as Deserialize, Encodable as Serialize, *}; | ||
use core::convert::TryInto; | ||
pub use derive_codec_sv2::{Decodable as Deserialize, Encodable as Serialize}; | ||
|
||
// The `Test` struct is expanded using the `Deserialize` and `Serialize` procedural macros. | ||
// These macros provide the necessary methods for serializing and deserializing the struct. | ||
// | ||
// mod impl_parse_decodable_test { | ||
// use super::binary_codec_sv2::{ | ||
// decodable::DecodableField, decodable::FieldMarker, Decodable, Error, SizeHint, | ||
// }; | ||
// use super::*; | ||
// impl<'decoder> Decodable<'decoder> for Test { | ||
// fn get_structure(data: &[u8]) -> Result<Vec<FieldMarker>, Error> { | ||
// let mut fields = Vec::new(); | ||
// let mut offset = 0; | ||
// let a: Vec<FieldMarker> = u32::get_structure(&data[offset..])?; | ||
// offset += a.size_hint_(&data, offset)?; | ||
// let a = a.try_into()?; | ||
// fields.push(a); | ||
// let b: Vec<FieldMarker> = u8::get_structure(&data[offset..])?; | ||
// offset += b.size_hint_(&data, offset)?; | ||
// let b = b.try_into()?; | ||
// fields.push(b); | ||
// let c: Vec<FieldMarker> = U24::get_structure(&data[offset..])?; | ||
// offset += c.size_hint_(&data, offset)?; | ||
// let c = c.try_into()?; | ||
// fields.push(c); | ||
// Ok(fields) | ||
// } | ||
// fn from_decoded_fields( | ||
// mut data: Vec<DecodableField<'decoder>>, | ||
// ) -> Result<Self, Error> { | ||
// Ok(Self { | ||
// c: U24::from_decoded_fields( | ||
// data.pop().ok_or(Error::NoDecodableFieldPassed)?.into(), | ||
// )?, | ||
// b: u8::from_decoded_fields( | ||
// data.pop().ok_or(Error::NoDecodableFieldPassed)?.into(), | ||
// )?, | ||
// a: u32::from_decoded_fields( | ||
// data.pop().ok_or(Error::NoDecodableFieldPassed)?.into(), | ||
// )?, | ||
// }) | ||
// } | ||
// } | ||
// impl<'decoder> Test { | ||
// pub fn into_static(self) -> Test { | ||
// Test { | ||
// a: self.a.clone(), | ||
// b: self.b.clone(), | ||
// c: self.c.clone(), | ||
// } | ||
// } | ||
// } | ||
// impl<'decoder> Test { | ||
// pub fn as_static(&self) -> Test { | ||
// Test { | ||
// a: self.a.clone(), | ||
// b: self.b.clone(), | ||
// c: self.c.clone(), | ||
// } | ||
// } | ||
// } | ||
// } | ||
// mod impl_parse_encodable_test { | ||
// use super::binary_codec_sv2::{encodable::EncodableField, GetSize}; | ||
// use super::Test; | ||
// extern crate alloc; | ||
// use alloc::vec::Vec; | ||
// impl<'decoder> From<Test> for EncodableField<'decoder> { | ||
// fn from(v: Test) -> Self { | ||
// let mut fields: Vec<EncodableField> = Vec::new(); | ||
// let val = v.a; | ||
// fields.push(val.into()); | ||
// let val = v.b; | ||
// fields.push(val.into()); | ||
// let val = v.c; | ||
// fields.push(val.into()); | ||
// Self::Struct(fields) | ||
// } | ||
// } | ||
// impl<'decoder> GetSize for Test { | ||
// fn get_size(&self) -> usize { | ||
// let mut size = 0; | ||
// size += self.a.get_size(); | ||
// size += self.b.get_size(); | ||
// size += self.c.get_size(); | ||
// size | ||
// } | ||
// } | ||
// } | ||
// | ||
|
||
#[derive(Clone, Deserialize, Serialize, PartialEq, Debug)] | ||
struct Test { | ||
a: u32, | ||
b: u8, | ||
c: U24, | ||
} | ||
|
||
fn main() { | ||
let expected = Test { | ||
a: 456, | ||
b: 9, | ||
c: 67_u32.try_into().unwrap(), | ||
}; | ||
|
||
// `to_bytes` serves as the entry point to the `binary_sv2` crate. It acts as a serializer that | ||
// converts the struct into bytes. | ||
let mut bytes = to_bytes(expected.clone()).unwrap(); | ||
|
||
// `from_bytes` is a deserializer that interprets the bytes and reconstructs the original | ||
// struct. | ||
let deserialized: Test = from_bytes(&mut bytes[..]).unwrap(); | ||
|
||
assert_eq!(deserialized, expected); | ||
} |
This file contains 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
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,44 @@ | ||
# binary_codec_sv2 | ||
|
||
`binary_codec_sv2` is a Rust `no_std` crate | ||
[![crates.io](https://img.shields.io/crates/v/binary_codec_sv2.svg)](https://crates.io/crates/binary_codec_sv2) | ||
[![docs.rs](https://docs.rs/binary_codec_sv2/badge.svg)](https://docs.rs/binary_codec_sv2) | ||
[![rustc+](https://img.shields.io/badge/rustc-1.75.0%2B-lightgrey.svg)](https://blog.rust-lang.org/2023/12/28/Rust-1.75.0.html) | ||
[![license](https://img.shields.io/badge/license-MIT%2FApache--2.0-blue.svg)](https://github.com/stratum-mining/stratum/blob/main/LICENSE.md) | ||
[![codecov](https://codecov.io/gh/stratum-mining/stratum/branch/main/graph/badge.svg?flag=binary_codec_sv2-coverage)](https://codecov.io/gh/stratum-mining/stratum) | ||
|
||
`binary_codec_sv2` is a `no_std` Rust crate that helps serialize and de-serialize binary data into and from Stratum V2 types. | ||
|
||
## Key Features | ||
|
||
- **Comprehensive Encoding and Decoding**: Provides traits (`Encodable`, `Decodable`) for converting between Rust and SV2 data types/structures. | ||
- **Support for Complex Data Structures**: Handles primitives, nested structures, and protocol-specific types like `U24`, `U256`,`Str0255` and rest. | ||
- **Error Handling**: Robust mechanisms for managing encoding/decoding failures, including size mismatches and invalid data. | ||
- **Cross-Language Compatibility**: Utilities like `CVec` and `CError` ensure smooth integration with other programming languages. | ||
- **`no_std` Compatibility**: Fully supports constrained environments without the Rust standard library. | ||
|
||
## Sv2 Type Mapping | ||
|
||
The crate supports the following mappings between Rust and SV2 types | ||
|
||
| Rust Type | Sv2 Type | | ||
|-------------|----------------| | ||
| `bool` | `BOOL` | | ||
| `u8` | `U8` | | ||
| `u16` | `U16` | | ||
| `U24` | `U24` | | ||
| `u32` | `U32` | | ||
| `u64` | `U64` | | ||
| `f32` | `F32` | | ||
| `Str0255` | `STRO_255` | | ||
| `Signature` | `SIGNATURE` | | ||
| `[u8]` | `BYTES` | | ||
| `Seq0255` | `SEQ0_255[T]` | | ||
| `Seq064K` | `SEQ0_64K[T]` | | ||
|
||
## Installation | ||
|
||
Add `binary_codec_sv2` to your project by running: | ||
|
||
```sh | ||
cargo add binary_codec_sv2 | ||
``` |
This file contains 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
Oops, something went wrong.