diff --git a/protocols/v2/codec-sv2/src/decoder.rs b/protocols/v2/codec-sv2/src/decoder.rs index 4a946a1e1..4d1440018 100644 --- a/protocols/v2/codec-sv2/src/decoder.rs +++ b/protocols/v2/codec-sv2/src/decoder.rs @@ -8,11 +8,11 @@ pub use buffer_sv2::AeadBuffer; pub use const_sv2::{SV2_FRAME_CHUNK_SIZE, SV2_FRAME_HEADER_SIZE}; use core::marker::PhantomData; #[cfg(feature = "noise_sv2")] -use framing_sv2::framing2::HandShakeFrame; +use framing_sv2::framing::HandShakeFrame; #[cfg(feature = "noise_sv2")] use framing_sv2::header::{NOISE_HEADER_ENCRYPTED_SIZE, NOISE_HEADER_SIZE}; use framing_sv2::{ - framing2::{EitherFrame, Sv2Frame}, + framing::{Frame, Sv2Frame}, header::Header, }; #[cfg(feature = "noise_sv2")] @@ -36,7 +36,7 @@ use crate::State; #[cfg(feature = "noise_sv2")] pub type StandardNoiseDecoder = WithNoise; -pub type StandardEitherFrame = EitherFrame::Slice>; +pub type StandardEitherFrame = Frame::Slice>; pub type StandardSv2Frame = Sv2Frame::Slice>; pub type StandardDecoder = WithoutNoise; @@ -51,7 +51,7 @@ pub struct WithNoise { #[cfg(feature = "noise_sv2")] impl<'a, T: Serialize + GetSize + Deserialize<'a>, B: IsBuffer + AeadBuffer> WithNoise { #[inline] - pub fn next_frame(&mut self, state: &mut State) -> Result> { + pub fn next_frame(&mut self, state: &mut State) -> Result> { match state { State::HandShake(_) => unreachable!(), State::NotInitialized(msg_len) => { @@ -97,10 +97,7 @@ impl<'a, T: Serialize + GetSize + Deserialize<'a>, B: IsBuffer + AeadBuffer> Wit } #[inline] - fn decode_noise_frame( - &mut self, - noise_codec: &mut NoiseCodec, - ) -> Result> { + fn decode_noise_frame(&mut self, noise_codec: &mut NoiseCodec) -> Result> { match ( IsBuffer::len(&self.noise_buffer), IsBuffer::len(&self.sv2_buffer), @@ -148,7 +145,7 @@ impl<'a, T: Serialize + GetSize + Deserialize<'a>, B: IsBuffer + AeadBuffer> Wit } } - fn while_handshaking(&mut self) -> EitherFrame { + fn while_handshaking(&mut self) -> Frame { let src = self.noise_buffer.get_data_owned().as_mut().to_vec(); // below is inffalible as noise frame length has been already checked diff --git a/protocols/v2/codec-sv2/src/encoder.rs b/protocols/v2/codec-sv2/src/encoder.rs index 9ecc37c3e..21618fda5 100644 --- a/protocols/v2/codec-sv2/src/encoder.rs +++ b/protocols/v2/codec-sv2/src/encoder.rs @@ -5,9 +5,9 @@ pub use const_sv2::{AEAD_MAC_LEN, SV2_FRAME_CHUNK_SIZE, SV2_FRAME_HEADER_SIZE}; #[cfg(feature = "noise_sv2")] use core::convert::TryInto; use core::marker::PhantomData; -use framing_sv2::framing2::Sv2Frame; +use framing_sv2::framing::Sv2Frame; #[cfg(feature = "noise_sv2")] -use framing_sv2::framing2::{EitherFrame, HandShakeFrame}; +use framing_sv2::framing::{Frame, HandShakeFrame}; #[allow(unused_imports)] pub use framing_sv2::header::NOISE_HEADER_ENCRYPTED_SIZE; @@ -43,7 +43,7 @@ pub struct NoiseEncoder { } #[cfg(feature = "noise_sv2")] -type Item = EitherFrame; +type Item = Frame; #[cfg(feature = "noise_sv2")] impl NoiseEncoder { diff --git a/protocols/v2/codec-sv2/src/lib.rs b/protocols/v2/codec-sv2/src/lib.rs index 184c39fda..0a2492890 100644 --- a/protocols/v2/codec-sv2/src/lib.rs +++ b/protocols/v2/codec-sv2/src/lib.rs @@ -22,15 +22,15 @@ pub use encoder::Encoder; pub use encoder::NoiseEncoder; #[cfg(feature = "noise_sv2")] -pub use framing_sv2::framing2::HandShakeFrame; -pub use framing_sv2::framing2::Sv2Frame; +pub use framing_sv2::framing::HandShakeFrame; +pub use framing_sv2::framing::Sv2Frame; #[cfg(feature = "noise_sv2")] pub use noise_sv2::{self, Initiator, NoiseCodec, Responder}; pub use buffer_sv2; -pub use framing_sv2::{self, framing2::handshake_message_to_frame as h2f}; +pub use framing_sv2::{self, framing::handshake_message_to_frame as h2f}; #[cfg(feature = "noise_sv2")] #[derive(Debug)] diff --git a/protocols/v2/framing-sv2/src/framing2.rs b/protocols/v2/framing-sv2/src/framing.rs similarity index 74% rename from protocols/v2/framing-sv2/src/framing2.rs rename to protocols/v2/framing-sv2/src/framing.rs index eec461fa9..616d53354 100644 --- a/protocols/v2/framing-sv2/src/framing2.rs +++ b/protocols/v2/framing-sv2/src/framing.rs @@ -1,35 +1,43 @@ -#![allow(dead_code)] -use crate::{ - header::{Header, NOISE_HEADER_LEN_OFFSET, NOISE_HEADER_SIZE}, - Error, -}; +use crate::{header::Header, Error}; use alloc::vec::Vec; use binary_sv2::{to_writer, GetSize, Serialize}; use core::convert::TryFrom; -const NOISE_MAX_LEN: usize = const_sv2::NOISE_FRAME_MAX_SIZE; - #[cfg(not(feature = "with_buffer_pool"))] type Slice = Vec; #[cfg(feature = "with_buffer_pool")] type Slice = buffer_sv2::Slice; -impl Sv2Frame { - /// Maps a `Sv2Frame` to `Sv2Frame` by applying `fun`, - /// which is assumed to be a closure that converts `A` to `C` - pub fn map(self, fun: fn(A) -> C) -> Sv2Frame { - let serialized = self.serialized; - let header = self.header; - let payload = self.payload.map(fun); - Sv2Frame { - header, - payload, - serialized, +/// A wrapper to be used in a context we need a generic reference to a frame +/// but it doesn't matter which kind of frame it is (`Sv2Frame` or `HandShakeFrame`) +#[derive(Debug)] +pub enum Frame { + HandShake(HandShakeFrame), + Sv2(Sv2Frame), +} + +impl + AsRef<[u8]>> Frame { + pub fn encoded_length(&self) -> usize { + match &self { + Self::HandShake(frame) => frame.encoded_length(), + Self::Sv2(frame) => frame.encoded_length(), } } } +impl From for Frame { + fn from(v: HandShakeFrame) -> Self { + Self::HandShake(v) + } +} + +impl From> for Frame { + fn from(v: Sv2Frame) -> Self { + Self::Sv2(v) + } +} + /// Abstraction for a SV2 Frame. #[derive(Debug, Clone)] pub struct Sv2Frame { @@ -39,21 +47,6 @@ pub struct Sv2Frame { serialized: Option, } -/// Abstraction for a Noise Handshake Frame -/// Contains only a `Slice` payload with a fixed length -/// Only used during Noise Handshake process -#[derive(Debug)] -pub struct HandShakeFrame { - payload: Slice, -} - -impl HandShakeFrame { - /// Returns payload of `HandShakeFrame` as a `Vec` - pub fn get_payload_when_handshaking(&self) -> Vec { - self.payload[0..].to_vec() - } -} - impl + AsRef<[u8]>> Sv2Frame { /// Write the serialized `Sv2Frame` into `dst`. /// This operation when called on an already serialized frame is very cheap. @@ -182,23 +175,44 @@ impl + AsRef<[u8]>> Sv2Frame { } } -impl HandShakeFrame { - /// Put the Noise Frame payload into `dst` - #[inline] - fn serialize(mut self, dst: &mut [u8]) -> Result<(), Error> { - dst.swap_with_slice(self.payload.as_mut()); - Ok(()) +impl Sv2Frame { + /// Maps a `Sv2Frame` to `Sv2Frame` by applying `fun`, + /// which is assumed to be a closure that converts `A` to `C` + pub fn map(self, fun: fn(A) -> C) -> Sv2Frame { + let serialized = self.serialized; + let header = self.header; + let payload = self.payload.map(fun); + Sv2Frame { + header, + payload, + serialized, + } } +} - /// Get the Noise Frame payload - #[inline] - fn payload(&mut self) -> &mut [u8] { - &mut self.payload[NOISE_HEADER_SIZE..] +impl TryFrom> for Sv2Frame { + type Error = Error; + + fn try_from(v: Frame) -> Result { + match v { + Frame::Sv2(frame) => Ok(frame), + Frame::HandShake(_) => Err(Error::ExpectedSv2Frame), + } } +} - /// `HandShakeFrame` always returns `None`. - fn get_header(&self) -> Option { - None +/// Abstraction for a Noise Handshake Frame +/// Contains only a `Slice` payload with a fixed length +/// Only used during Noise Handshake process +#[derive(Debug)] +pub struct HandShakeFrame { + payload: Slice, +} + +impl HandShakeFrame { + /// Returns payload of `HandShakeFrame` as a `Vec` + pub fn get_payload_when_handshaking(&self) -> Vec { + self.payload[0..].to_vec() } /// Builds a `HandShakeFrame` from raw bytes. Nothing is assumed or checked about the correctness of the payload. @@ -211,52 +225,20 @@ impl HandShakeFrame { Self { payload: bytes } } - /// After parsing the expected `HandShakeFrame` size from `bytes`, this function helps to determine if this value - /// correctly representing the size of the frame. - /// - Returns `0` if the byte slice is of the expected size according to the header. - /// - Returns a negative value if the byte slice is smaller than a Noise Frame header; this value - /// represents how many bytes are missing. - /// - Returns a positive value if the byte slice is longer than expected; this value - /// indicates the surplus of bytes beyond the expected size. - #[inline] - fn size_hint(bytes: &[u8]) -> isize { - if bytes.len() < NOISE_HEADER_SIZE { - return (NOISE_HEADER_SIZE - bytes.len()) as isize; - }; - - let len_b = &bytes[NOISE_HEADER_LEN_OFFSET..NOISE_HEADER_SIZE]; - let expected_len = u16::from_le_bytes([len_b[0], len_b[1]]) as usize; - - if bytes.len() - NOISE_HEADER_SIZE == expected_len { - 0 - } else { - expected_len as isize - (bytes.len() - NOISE_HEADER_SIZE) as isize - } - } - /// Returns the size of the `HandShakeFrame` payload. #[inline] fn encoded_length(&self) -> usize { self.payload.len() } +} - /// Tries to build a `HandShakeFrame` frame from a byte slice. - /// Returns a `HandShakeFrame` if the size of the payload fits in the frame, `None` otherwise. - /// This is quite inefficient, and should be used only to build `HandShakeFrames` - // TODO check if is used only to build `HandShakeFrames` - #[allow(clippy::useless_conversion)] - fn from_message( - message: Slice, - _message_type: u8, - _extension_type: u16, - _channel_msg: bool, - ) -> Option { - if message.len() <= NOISE_MAX_LEN { - Some(Self { - payload: message.into(), - }) - } else { - None +impl TryFrom> for HandShakeFrame { + type Error = Error; + + fn try_from(v: Frame) -> Result { + match v { + Frame::HandShake(frame) => Ok(frame), + Frame::Sv2(_) => Err(Error::ExpectedHandshakeFrame), } } } @@ -285,57 +267,6 @@ fn update_extension_type(extension_type: u16, channel_msg: bool) -> u16 { } } -/// A wrapper to be used in a context we need a generic reference to a frame -/// but it doesn't matter which kind of frame it is (`Sv2Frame` or `HandShakeFrame`) -#[derive(Debug)] -pub enum EitherFrame { - HandShake(HandShakeFrame), - Sv2(Sv2Frame), -} - -impl + AsRef<[u8]>> EitherFrame { - pub fn encoded_length(&self) -> usize { - match &self { - Self::HandShake(frame) => frame.encoded_length(), - Self::Sv2(frame) => frame.encoded_length(), - } - } -} - -impl TryFrom> for HandShakeFrame { - type Error = Error; - - fn try_from(v: EitherFrame) -> Result { - match v { - EitherFrame::HandShake(frame) => Ok(frame), - EitherFrame::Sv2(_) => Err(Error::ExpectedHandshakeFrame), - } - } -} - -impl TryFrom> for Sv2Frame { - type Error = Error; - - fn try_from(v: EitherFrame) -> Result { - match v { - EitherFrame::Sv2(frame) => Ok(frame), - EitherFrame::HandShake(_) => Err(Error::ExpectedSv2Frame), - } - } -} - -impl From for EitherFrame { - fn from(v: HandShakeFrame) -> Self { - Self::HandShake(v) - } -} - -impl From> for EitherFrame { - fn from(v: Sv2Frame) -> Self { - Self::Sv2(v) - } -} - #[cfg(test)] use binary_sv2::binary_codec_sv2; diff --git a/protocols/v2/framing-sv2/src/lib.rs b/protocols/v2/framing-sv2/src/lib.rs index 34fe8708b..33dd11fe2 100644 --- a/protocols/v2/framing-sv2/src/lib.rs +++ b/protocols/v2/framing-sv2/src/lib.rs @@ -23,7 +23,7 @@ extern crate alloc; /// SV2 framing types -pub mod framing2; +pub mod framing; /// SV2 framing errors pub mod error; diff --git a/protocols/v2/roles-logic-sv2/src/parsers.rs b/protocols/v2/roles-logic-sv2/src/parsers.rs index 4d80387c9..0274ce785 100644 --- a/protocols/v2/roles-logic-sv2/src/parsers.rs +++ b/protocols/v2/roles-logic-sv2/src/parsers.rs @@ -13,7 +13,7 @@ use binary_sv2::GetSize; use binary_sv2::{from_bytes, Deserialize}; -use framing_sv2::framing2::Sv2Frame; +use framing_sv2::framing::Sv2Frame; use const_sv2::{ CHANNEL_BIT_ALLOCATE_MINING_JOB_TOKEN, CHANNEL_BIT_ALLOCATE_MINING_JOB_TOKEN_SUCCESS,