diff --git a/fuzz/fuzz_targets/common.rs b/fuzz/fuzz_targets/common.rs index c8c8b989ae..fbef6a3dcc 100644 --- a/fuzz/fuzz_targets/common.rs +++ b/fuzz/fuzz_targets/common.rs @@ -124,7 +124,7 @@ macro_rules! test_datatype_roundtrip { // Ensure serialization is canonical: re-encoding must match the consumed input. assert_eq!( encoded, - input[..encoded.get_size()], + input[..encoded.len()], "Serialization is not stable" ); } @@ -162,7 +162,7 @@ macro_rules! test_datatype_roundtrip { // reserialization must match the consumed input bytes. assert_eq!( encoded, - input[..encoded.get_size()], + input[..encoded.len()], "{}: Serialization is not stable", stringify!($datatype) ); diff --git a/sv2/binary-sv2/README.md b/sv2/binary-sv2/README.md index d8e6bcf69a..23c6f60c45 100644 --- a/sv2/binary-sv2/README.md +++ b/sv2/binary-sv2/README.md @@ -27,10 +27,13 @@ The crate supports the following mappings between Rust and SV2 types | `f32` | `F32` | | `Str0255` | `STRO_255` | | `Signature` | `SIGNATURE` | -| `[u8]` | `BYTES` | | `Seq0255` | `SEQ0_255[T]` | | `Seq064K` | `SEQ0_64K[T]` | +`BYTES` is not listed above: it only appears as the length-prefixed frame payload, whose length +comes from the frame header, so it is handled by the framing layer (`framing-sv2`) rather than by +this crate. + ## Features diff --git a/sv2/binary-sv2/src/lib.rs b/sv2/binary-sv2/src/lib.rs index 32dfb55a42..bc8fa12b03 100644 --- a/sv2/binary-sv2/src/lib.rs +++ b/sv2/binary-sv2/src/lib.rs @@ -26,12 +26,15 @@ //! B0255 <-> B0_255 //! B064K <-> B0_64K //! B016M <-> B0_16M -//! [u8] <-> BYTES //! Pubkey <-> PUBKEY //! Seq0255 <-> SEQ0_255[T] //! Seq064K <-> SEQ0_64K[T] //! ``` //! +//! `BYTES` is not in this table: it only appears as the length-prefixed frame payload, whose +//! length comes from the frame header, so it is handled by the framing layer (`framing-sv2`) +//! rather than by this crate. +//! //! # Encoding & Decoding //! //! Enables conversion between various Rust types and SV2-specific data formats for efficient @@ -224,23 +227,3 @@ pub enum Error { /// elements. Sv2OptionHaveMoreThenOneElement(u8), } - -/// Vec is used as the Sv2 type Bytes -impl GetSize for Vec { - fn get_size(&self) -> usize { - self.len() - } -} - -impl From> for EncodableField<'_> { - fn from(v: Vec) -> Self { - EncodableField::Struct(v.into_iter().map(Into::into).collect()) - } -} - -#[cfg(feature = "with_buffer_pool")] -impl From for EncodableField<'_> { - fn from(_v: buffer_sv2::Slice) -> Self { - unreachable!() - } -} diff --git a/sv2/framing-sv2/benches/framing.rs b/sv2/framing-sv2/benches/framing.rs index 02497ad104..5236ad46b6 100644 --- a/sv2/framing-sv2/benches/framing.rs +++ b/sv2/framing-sv2/benches/framing.rs @@ -1,7 +1,7 @@ //! Performance benchmarks for SV2 framing layer operations //! Tests both Vec and buffer_pool backends across different message sizes -use binary_sv2::Serialize; +use binary_sv2::{B016MOwned, Serialize}; use criterion::{black_box, criterion_group, criterion_main, BenchmarkId, Criterion}; use framing_sv2::{framing::Sv2Frame, header::Header}; @@ -42,12 +42,15 @@ fn frame_from_payload_size(size: usize) -> Vec { #[derive(Serialize, Clone)] struct Test { - _a: Vec, + _a: B016MOwned, } impl Test { + // `size` is the total encoded message size: 3-byte B016M length prefix + data fn new(size: usize) -> Self { - Test { _a: vec![2; size] } + Test { + _a: vec![2; size - 3].try_into().unwrap(), + } } } @@ -71,7 +74,7 @@ fn bench_serialize(c: &mut Criterion) { for &size in PAYLOAD_SIZES { let frame = - Sv2Frame::, Slice>::from_bytes(frame_from_payload_size(size).into()).unwrap(); + Sv2Frame::::from_bytes(frame_from_payload_size(size).into()).unwrap(); let mut buf = vec![0u8; frame.encoded_length()]; @@ -92,7 +95,7 @@ fn bench_from_bytes(c: &mut Criterion) { for &size in PAYLOAD_SIZES { let frame = frame_from_payload_size(size); group.bench_with_input(BenchmarkId::from_parameter(size), &size, |b, _| { - b.iter(|| Sv2Frame::, _>::from_bytes(black_box(frame.clone())).unwrap()) + b.iter(|| Sv2Frame::::from_bytes(black_box(frame.clone())).unwrap()) }); } @@ -106,7 +109,7 @@ fn bench_size_hint(c: &mut Criterion) { for &size in PAYLOAD_SIZES { let frame = frame_from_payload_size(size); group.bench_with_input(BenchmarkId::from_parameter(size), &size, |b, _| { - b.iter(|| Sv2Frame::, Slice>::size_hint(black_box(&frame))) + b.iter(|| Sv2Frame::::size_hint(black_box(&frame))) }); } diff --git a/sv2/framing-sv2/src/framing.rs b/sv2/framing-sv2/src/framing.rs index 72507fb1d0..8e9e4948fa 100644 --- a/sv2/framing-sv2/src/framing.rs +++ b/sv2/framing-sv2/src/framing.rs @@ -301,7 +301,7 @@ fn update_extension_type(extension_type: u16, channel_msg: bool) -> u16 { mod tests { use super::*; use alloc::vec; - use binary_sv2::Serialize; + use binary_sv2::{B064KOwned, Serialize}; use quickcheck::{Arbitrary, Gen}; use quickcheck_macros::quickcheck; @@ -325,7 +325,7 @@ mod tests { #[derive(Debug, Clone, PartialEq, Serialize)] struct TestMessage { - data: Vec, + data: B064KOwned, } impl Arbitrary for TestMessage { @@ -394,7 +394,9 @@ mod tests { #[quickcheck] fn prop_sv2frame_serialization_roundtrip_small(data: Vec) { let data: Vec = data.iter().take(1000).copied().collect(); - let msg = TestMessage { data }; + let msg = TestMessage { + data: data.try_into().unwrap(), + }; let msg_type = 0x01u8; let extension_type = 0x0000u16;