Skip to content

Commit

Permalink
Merge branch 'dev' into johnny/issue/484
Browse files Browse the repository at this point in the history
  • Loading branch information
johnnyasantoss committed Jul 8, 2024
2 parents e017235 + 5a2b01b commit e431367
Show file tree
Hide file tree
Showing 37 changed files with 248 additions and 331 deletions.
2 changes: 1 addition & 1 deletion benches/benches/src/sv2/criterion_sv2_benchmark.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use codec_sv2::{Frame, StandardEitherFrame, StandardSv2Frame};
use codec_sv2::{StandardEitherFrame, StandardSv2Frame};
use criterion::{black_box, Criterion};
use roles_logic_sv2::{
handlers::{common::ParseUpstreamCommonMessages, mining::ParseUpstreamMiningMessages},
Expand Down
2 changes: 1 addition & 1 deletion benches/benches/src/sv2/iai_sv2_benchmark.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use codec_sv2::{Frame, StandardEitherFrame, StandardSv2Frame};
use codec_sv2::{StandardEitherFrame, StandardSv2Frame};
use iai::{black_box, main};
use roles_logic_sv2::{
handlers::{common::ParseUpstreamCommonMessages, mining::ParseUpstreamMiningMessages, SendTo_},
Expand Down
2 changes: 1 addition & 1 deletion examples/interop-cpp/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ mod main_ {

#[cfg(not(feature = "with_serde"))]
mod main_ {
use codec_sv2::{Encoder, Frame, StandardDecoder, StandardSv2Frame};
use codec_sv2::{Encoder, StandardDecoder, StandardSv2Frame};
use common_messages_sv2::{Protocol, SetupConnection, SetupConnectionError};
use const_sv2::{
CHANNEL_BIT_SETUP_CONNECTION, MESSAGE_TYPE_SETUP_CONNECTION,
Expand Down
2 changes: 1 addition & 1 deletion examples/ping-pong-with-noise/src/node.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ use async_std::{
};
use core::convert::TryInto;

use codec_sv2::{Frame, HandshakeRole, StandardEitherFrame, StandardSv2Frame};
use codec_sv2::{HandshakeRole, StandardEitherFrame, StandardSv2Frame};

use std::time;

Expand Down
2 changes: 1 addition & 1 deletion examples/ping-pong-without-noise/src/node.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ use async_std::{
task,
};

use codec_sv2::{Frame, StandardDecoder, StandardSv2Frame};
use codec_sv2::{StandardDecoder, StandardSv2Frame};

#[derive(Debug)]
enum Expected {
Expand Down
2 changes: 1 addition & 1 deletion examples/template-provider-test/src/main.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use async_channel::{Receiver, Sender};
use async_std::net::TcpStream;
use codec_sv2::{Frame, StandardEitherFrame, StandardSv2Frame, Sv2Frame};
use codec_sv2::{StandardEitherFrame, StandardSv2Frame, Sv2Frame};
use network_helpers::PlainConnection;
use roles_logic_sv2::{
parsers::{IsSv2Message, TemplateDistribution},
Expand Down
2 changes: 1 addition & 1 deletion protocols/fuzz-tests/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
use libfuzzer_sys::fuzz_target;
use binary_codec_sv2::{Seq064K,U256,B0255,Seq0255};
use binary_codec_sv2::from_bytes;
use codec_sv2::{StandardDecoder,Sv2Frame,Frame};
use codec_sv2::{StandardDecoder,Sv2Frame};
use roles_logic_sv2::parsers::PoolMessages;

type F = Sv2Frame<PoolMessages<'static>,Vec<u8>>;
Expand Down
33 changes: 15 additions & 18 deletions protocols/v2/codec-sv2/src/decoder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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::NoiseHeader;
use framing_sv2::header::{NOISE_HEADER_ENCRYPTED_SIZE, NOISE_HEADER_SIZE};
use framing_sv2::{
framing2::{EitherFrame, Frame as F_, Sv2Frame},
framing::{Frame, Sv2Frame},
header::Header,
};
#[cfg(feature = "noise_sv2")]
Expand All @@ -36,7 +36,7 @@ use crate::State;

#[cfg(feature = "noise_sv2")]
pub type StandardNoiseDecoder<T> = WithNoise<Buffer, T>;
pub type StandardEitherFrame<T> = EitherFrame<T, <Buffer as IsBuffer>::Slice>;
pub type StandardEitherFrame<T> = Frame<T, <Buffer as IsBuffer>::Slice>;
pub type StandardSv2Frame<T> = Sv2Frame<T, <Buffer as IsBuffer>::Slice>;
pub type StandardDecoder<T> = WithoutNoise<Buffer, T>;

Expand All @@ -51,14 +51,14 @@ pub struct WithNoise<B: IsBuffer, T: Serialize + binary_sv2::GetSize> {
#[cfg(feature = "noise_sv2")]
impl<'a, T: Serialize + GetSize + Deserialize<'a>, B: IsBuffer + AeadBuffer> WithNoise<B, T> {
#[inline]
pub fn next_frame(&mut self, state: &mut State) -> Result<EitherFrame<T, B::Slice>> {
pub fn next_frame(&mut self, state: &mut State) -> Result<Frame<T, B::Slice>> {
match state {
State::HandShake(_) => unreachable!(),
State::NotInitialized(msg_len) => {
let hint = *msg_len - self.noise_buffer.as_ref().len();
match hint {
0 => {
self.missing_noise_b = NoiseHeader::HEADER_SIZE;
self.missing_noise_b = NOISE_HEADER_SIZE;
Ok(self.while_handshaking())
}
_ => {
Expand All @@ -71,20 +71,20 @@ impl<'a, T: Serialize + GetSize + Deserialize<'a>, B: IsBuffer + AeadBuffer> Wit
let hint = if IsBuffer::len(&self.sv2_buffer) < SV2_FRAME_HEADER_SIZE {
let len = IsBuffer::len(&self.noise_buffer);
let src = self.noise_buffer.get_data_by_ref(len);
if src.len() < NoiseHeader::SIZE {
NoiseHeader::SIZE - src.len()
if src.len() < NOISE_HEADER_ENCRYPTED_SIZE {
NOISE_HEADER_ENCRYPTED_SIZE - src.len()
} else {
0
}
} else {
let src = self.sv2_buffer.get_data_by_ref_(SV2_FRAME_HEADER_SIZE);
let src = self.sv2_buffer.get_data_by_ref(SV2_FRAME_HEADER_SIZE);
let header = Header::from_bytes(src)?;
header.encrypted_len() - IsBuffer::len(&self.noise_buffer)
};

match hint {
0 => {
self.missing_noise_b = NoiseHeader::SIZE;
self.missing_noise_b = NOISE_HEADER_ENCRYPTED_SIZE;
self.decode_noise_frame(noise_codec)
}
_ => {
Expand All @@ -97,23 +97,20 @@ impl<'a, T: Serialize + GetSize + Deserialize<'a>, B: IsBuffer + AeadBuffer> Wit
}

#[inline]
fn decode_noise_frame(
&mut self,
noise_codec: &mut NoiseCodec,
) -> Result<EitherFrame<T, B::Slice>> {
fn decode_noise_frame(&mut self, noise_codec: &mut NoiseCodec) -> Result<Frame<T, B::Slice>> {
match (
IsBuffer::len(&self.noise_buffer),
IsBuffer::len(&self.sv2_buffer),
) {
// HERE THE SV2 HEADER IS READY TO BE DECRYPTED
(NoiseHeader::SIZE, 0) => {
(NOISE_HEADER_ENCRYPTED_SIZE, 0) => {
let src = self.noise_buffer.get_data_owned();
let decrypted_header = self.sv2_buffer.get_writable(NoiseHeader::SIZE);
let decrypted_header = self.sv2_buffer.get_writable(NOISE_HEADER_ENCRYPTED_SIZE);
decrypted_header.copy_from_slice(src.as_ref());
self.sv2_buffer.as_ref();
noise_codec.decrypt(&mut self.sv2_buffer)?;
let header =
Header::from_bytes(self.sv2_buffer.get_data_by_ref_(SV2_FRAME_HEADER_SIZE))?;
Header::from_bytes(self.sv2_buffer.get_data_by_ref(SV2_FRAME_HEADER_SIZE))?;
self.missing_noise_b = header.encrypted_len();
Err(Error::MissingBytes(header.encrypted_len()))
}
Expand Down Expand Up @@ -148,7 +145,7 @@ impl<'a, T: Serialize + GetSize + Deserialize<'a>, B: IsBuffer + AeadBuffer> Wit
}
}

fn while_handshaking(&mut self) -> EitherFrame<T, B::Slice> {
fn while_handshaking(&mut self) -> Frame<T, B::Slice> {
let src = self.noise_buffer.get_data_owned().as_mut().to_vec();

// below is inffalible as noise frame length has been already checked
Expand Down
10 changes: 5 additions & 5 deletions protocols/v2/codec-sv2/src/encoder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,11 @@ 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::framing::Sv2Frame;
#[cfg(feature = "noise_sv2")]
use framing_sv2::framing2::{EitherFrame, HandShakeFrame};
use framing_sv2::framing2::{Frame as F_, Sv2Frame};
use framing_sv2::framing::{Frame, HandShakeFrame};
#[allow(unused_imports)]
pub use framing_sv2::header::NoiseHeader;
pub use framing_sv2::header::NOISE_HEADER_ENCRYPTED_SIZE;

#[cfg(feature = "noise_sv2")]
use tracing::error;
Expand Down Expand Up @@ -43,7 +43,7 @@ pub struct NoiseEncoder<T: Serialize + binary_sv2::GetSize> {
}

#[cfg(feature = "noise_sv2")]
type Item<T> = EitherFrame<T, Slice>;
type Item<T> = Frame<T, Slice>;

#[cfg(feature = "noise_sv2")]
impl<T: Serialize + GetSize> NoiseEncoder<T> {
Expand Down Expand Up @@ -76,7 +76,7 @@ impl<T: Serialize + GetSize> NoiseEncoder<T> {
} else {
SV2_FRAME_CHUNK_SIZE + start - AEAD_MAC_LEN
};
let mut encrypted_len = NoiseHeader::SIZE;
let mut encrypted_len = NOISE_HEADER_ENCRYPTED_SIZE;

while start < sv2.len() {
let to_encrypt = self.noise_buffer.get_writable(end - start);
Expand Down
6 changes: 3 additions & 3 deletions protocols/v2/codec-sv2/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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::{Frame, 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)]
Expand Down
9 changes: 7 additions & 2 deletions protocols/v2/framing-sv2/src/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,13 @@ impl fmt::Display for Error {
ExpectedSv2Frame => {
write!(f, "Expected `Sv2Frame`, received `HandshakeFrame`")
}
UnexpectedHeaderLength(i) => {
write!(f, "Unexpected `Header` length: `{}`", i)
UnexpectedHeaderLength(actual_size) => {
write!(
f,
"Unexpected `Header` length: `{}`, should be equal or more to {}",
actual_size,
const_sv2::SV2_FRAME_HEADER_SIZE
)
}
}
}
Expand Down
Loading

0 comments on commit e431367

Please sign in to comment.