Skip to content

Commit

Permalink
f fixes in roles
Browse files Browse the repository at this point in the history
  • Loading branch information
jbesraa committed Jun 13, 2024
1 parent 579bef9 commit f9a97f9
Show file tree
Hide file tree
Showing 37 changed files with 434 additions and 301 deletions.
24 changes: 15 additions & 9 deletions 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::{StandardFrame, StandardSv2Frame};
use criterion::{black_box, Criterion};
use roles_logic_sv2::{
handlers::{common::ParseUpstreamCommonMessages, mining::ParseUpstreamMiningMessages},
Expand All @@ -20,7 +20,7 @@ use crate::client::{

pub type Message = MiningDeviceMessages<'static>;
pub type StdFrame = StandardSv2Frame<Message>;
pub type EitherFrame = StandardEitherFrame<Message>;
pub type EitherFrame = StandardFrame<Message>;

fn client_sv2_setup_connection(c: &mut Criterion) {
c.bench_function("client_sv2_setup_connection", |b| {
Expand Down Expand Up @@ -53,9 +53,11 @@ fn client_sv2_setup_connection_serialize_deserialize(c: &mut Criterion) {
let mut dst = vec![0; size];
let _serialized = frame.serialize(&mut dst);
b.iter(|| {
let mut frame = StdFrame::from_bytes(black_box(dst.clone().into())).unwrap();
let type_ = frame.get_header().unwrap().msg_type().clone();
let payload = frame.payload();
let frame = StdFrame::from_bytes(black_box(dst.clone().into())).unwrap();
let type_ = frame.header().msg_type();
let payload = frame.payload().unwrap();
let mut payload = payload.to_owned();
let payload = payload.as_mut();
let _ = AnyMessage::try_from((type_, payload)).unwrap();
});
});
Expand Down Expand Up @@ -95,8 +97,10 @@ fn client_sv2_open_channel_serialize_deserialize(c: &mut Criterion) {
frame.serialize(&mut dst);
b.iter(|| {
let mut frame = StdFrame::from_bytes(black_box(dst.clone().into())).unwrap();
let type_ = frame.get_header().unwrap().msg_type().clone();
let payload = frame.payload();
let type_ = frame.header().msg_type();
let payload = frame.payload().unwrap();
let mut payload = payload.to_owned();
let payload = payload.as_mut();
black_box(AnyMessage::try_from((type_, payload)).unwrap());
});
});
Expand Down Expand Up @@ -151,8 +155,10 @@ fn client_sv2_mining_message_submit_standard_serialize_deserialize(c: &mut Crite
|b| {
b.iter(|| {
let mut frame = StdFrame::from_bytes(black_box(dst.clone().into())).unwrap();
let type_ = frame.get_header().unwrap().msg_type().clone();
let payload = frame.payload();
let type_ = frame.header().msg_type();
let payload = frame.payload().unwrap();
let mut payload = payload.to_owned();
let payload = payload.as_mut();
black_box(AnyMessage::try_from((type_, payload)).unwrap());
});
},
Expand Down
22 changes: 14 additions & 8 deletions 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::{StandardFrame, StandardSv2Frame};
use iai::{black_box, main};
use roles_logic_sv2::{
handlers::{common::ParseUpstreamCommonMessages, mining::ParseUpstreamMiningMessages, SendTo_},
Expand All @@ -18,7 +18,7 @@ use crate::client::{create_client, open_channel, Device, SetupConnectionHandler}

pub type Message = MiningDeviceMessages<'static>;
pub type StdFrame = StandardSv2Frame<Message>;
pub type EitherFrame = StandardEitherFrame<Message>;
pub type EitherFrame = StandardFrame<Message>;

fn client_sv2_setup_connection() {
let address: SocketAddr = SocketAddr::new(IpAddr::V4(Ipv4Addr::new(0, 0, 0, 0)), 34254);
Expand Down Expand Up @@ -46,8 +46,10 @@ fn client_sv2_setup_connection_serialize_deserialize() {
let mut dst = vec![0; size];
frame.serialize(&mut dst);
let mut frame = StdFrame::from_bytes(black_box(dst.clone().into())).unwrap();
let type_ = frame.get_header().unwrap().msg_type().clone();
let payload = frame.payload();
let type_ = frame.header().msg_type();
let payload = frame.payload().unwrap();
let mut payload = payload.to_owned();
let payload = payload.as_mut();
black_box(AnyMessage::try_from((type_, payload)));
}

Expand Down Expand Up @@ -77,8 +79,10 @@ fn client_sv2_open_channel_serialize_deserialize() {
let mut dst = vec![0; size];
frame.serialize(&mut dst);
let mut frame = StdFrame::from_bytes(black_box(dst.clone().into())).unwrap();
let type_ = frame.get_header().unwrap().msg_type().clone();
let payload = frame.payload();
let type_ = frame.header().msg_type();
let payload = frame.payload().unwrap();
let mut payload = payload.to_owned();
let payload = payload.as_mut();
black_box(AnyMessage::try_from((type_, payload)));
}

Expand Down Expand Up @@ -127,8 +131,10 @@ fn client_sv2_mining_message_submit_standard_serialize_deserialize() {
let mut dst = vec![0; size];
frame.serialize(&mut dst);
let mut frame = StdFrame::from_bytes(black_box(dst.clone().into())).unwrap();
let type_ = frame.get_header().unwrap().msg_type().clone();
let payload = frame.payload();
let type_ = frame.header().msg_type();
let payload = frame.payload().unwrap();
let mut payload = payload.to_owned();
let payload = payload.as_mut();
black_box(AnyMessage::try_from((type_, payload)));
}

Expand Down
4 changes: 2 additions & 2 deletions benches/benches/src/sv2/lib/client.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ use bitcoin::{
use async_channel::{Receiver, Sender};
use async_std::channel::unbounded;
use binary_sv2::u256_from_int;
use codec_sv2::{buffer_sv2::Slice, StandardEitherFrame, StandardSv2Frame};
use codec_sv2::{buffer_sv2::Slice, StandardFrame, StandardSv2Frame};
use roles_logic_sv2::{
common_messages_sv2::{Protocol, SetupConnection, SetupConnectionSuccess},
common_properties::{IsMiningUpstream, IsUpstream},
Expand All @@ -23,7 +23,7 @@ use roles_logic_sv2::{
use std::{net::SocketAddr, sync::Arc};
pub type Message = MiningDeviceMessages<'static>;
pub type StdFrame = StandardSv2Frame<Message>;
pub type EitherFrame = StandardEitherFrame<Message>;
pub type EitherFrame = StandardFrame<Message>;

pub fn create_client() -> Device {
let (sender, receiver) = unbounded();
Expand Down
18 changes: 12 additions & 6 deletions 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, StandardFrame, StandardSv2Frame};

use std::time;

Expand All @@ -28,8 +28,8 @@ pub struct Node {
name: String,
last_id: u32,
expected: Expected,
receiver: Receiver<StandardEitherFrame<Message<'static>>>,
sender: Sender<StandardEitherFrame<Message<'static>>>,
receiver: Receiver<StandardFrame<Message<'static>>>,
sender: Sender<StandardFrame<Message<'static>>>,
}

impl Node {
Expand Down Expand Up @@ -95,11 +95,14 @@ impl Node {

fn handle_message(
&mut self,
mut frame: StandardSv2Frame<Message<'static>>,
frame: StandardSv2Frame<Message<'static>>,
) -> Message<'static> {
match self.expected {
Expected::Ping => {
let ping: Result<Ping, _> = from_bytes(frame.payload());
let payload = frame.payload().unwrap();
let mut payload = payload.to_owned();
let payload = payload.as_mut();
let ping: Result<Ping, _> = from_bytes(payload);
match ping {
Ok(ping) => {
println!("Node {} received:", self.name);
Expand All @@ -118,7 +121,10 @@ impl Node {
}
}
Expected::Pong => {
let pong: Result<Pong, _> = from_bytes(frame.payload());
let payload = frame.payload().unwrap();
let mut payload = payload.to_owned();
let payload = payload.as_mut();
let pong: Result<Pong, _> = from_bytes(payload);
match pong {
Ok(pong) => {
println!("Node {} received:", self.name);
Expand Down
14 changes: 10 additions & 4 deletions 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 Expand Up @@ -83,11 +83,14 @@ impl Node {

fn handle_message(
&mut self,
mut frame: StandardSv2Frame<Message<'static>>,
frame: StandardSv2Frame<Message<'static>>,
) -> Message<'static> {
match self.expected {
Expected::Ping => {
let ping: Result<Ping, _> = from_bytes(frame.payload());
let payload = frame.payload().unwrap();
let mut payload = payload.to_owned();
let payload = payload.as_mut();
let ping: Result<Ping, _> = from_bytes(payload);
match ping {
Ok(ping) => {
println!("Node {} received:", self.name);
Expand All @@ -107,7 +110,10 @@ impl Node {
}
}
Expected::Pong => {
let pong: Result<Pong, _> = from_bytes(frame.payload());
let payload = frame.payload().unwrap();
let mut payload = payload.to_owned();
let payload = payload.as_mut();
let pong: Result<Pong, _> = from_bytes(payload);
match pong {
Ok(pong) => {
println!("Node {} received:", self.name);
Expand Down
4 changes: 2 additions & 2 deletions 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::{StandardFrame, StandardSv2Frame, Sv2Frame};
use network_helpers::PlainConnection;
use roles_logic_sv2::{
parsers::{IsSv2Message, TemplateDistribution},
Expand All @@ -13,7 +13,7 @@ use std::{

pub type Message = TemplateDistribution<'static>;
pub type StdFrame = StandardSv2Frame<Message>;
pub type EitherFrame = StandardEitherFrame<Message>;
pub type EitherFrame = StandardFrame<Message>;

#[async_std::main]
async fn main() {
Expand Down
66 changes: 44 additions & 22 deletions protocols/v2/sv2-ffi/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -760,8 +760,10 @@ mod tests {

let mut decoded = decoder.next_frame().unwrap();

let msg_type = decoded.get_header().unwrap().msg_type();
let payload = decoded.payload();
let msg_type = decoded.header().msg_type();
let payload = decoded.payload().unwrap();
let mut payload = payload.to_owned();
let payload = payload.as_mut();
let decoded_message: Sv2Message = (msg_type, payload).try_into().unwrap();
let decoded_message = match decoded_message {
Sv2Message::CoinbaseOutputDataSize(m) => m,
Expand Down Expand Up @@ -812,8 +814,10 @@ mod tests {
let mut decoded = decoder.next_frame().unwrap();

// Extract payload of the frame which is the NewTemplate message
let msg_type = decoded.get_header().unwrap().msg_type();
let payload = decoded.payload();
let msg_type = decoded.header().msg_type();
let payload = decoded.payload().unwrap();
let mut payload = payload.to_owned();
let payload = payload.as_mut();
let decoded_message: Sv2Message = (msg_type, payload).try_into().unwrap();
let decoded_message = match decoded_message {
Sv2Message::NewTemplate(m) => m,
Expand Down Expand Up @@ -860,8 +864,10 @@ mod tests {

let mut decoded = decoder.next_frame().unwrap();

let msg_type = decoded.get_header().unwrap().msg_type();
let payload = decoded.payload();
let msg_type = decoded.header().msg_type();
let payload = decoded.payload().unwrap();
let mut payload = payload.to_owned();
let payload = payload.as_mut();
let decoded_message: Sv2Message = (msg_type, payload).try_into().unwrap();
let decoded_message = match decoded_message {
Sv2Message::RequestTransactionData(m) => m,
Expand Down Expand Up @@ -910,8 +916,10 @@ mod tests {

let mut decoded = decoder.next_frame().unwrap();

let msg_type = decoded.get_header().unwrap().msg_type();
let payload = decoded.payload();
let msg_type = decoded.header().msg_type();
let payload = decoded.payload().unwrap();
let mut payload = payload.to_owned();
let payload = payload.as_mut();
let decoded_message: Sv2Message = (msg_type, payload).try_into().unwrap();
let decoded_message = match decoded_message {
Sv2Message::RequestTransactionDataError(m) => m,
Expand Down Expand Up @@ -960,8 +968,10 @@ mod tests {

let mut decoded = decoder.next_frame().unwrap();

let msg_type = decoded.get_header().unwrap().msg_type();
let payload = decoded.payload();
let msg_type = decoded.header().msg_type();
let payload = decoded.payload().unwrap();
let mut payload = payload.to_owned();
let payload = payload.as_mut();
let decoded_message: Sv2Message = (msg_type, payload).try_into().unwrap();
let decoded_message = match decoded_message {
Sv2Message::RequestTransactionDataSuccess(m) => m,
Expand Down Expand Up @@ -1005,8 +1015,10 @@ mod tests {

let mut decoded = decoder.next_frame().unwrap();

let msg_type = decoded.get_header().unwrap().msg_type();
let payload = decoded.payload();
let msg_type = decoded.header().msg_type();
let payload = decoded.payload().unwrap();
let mut payload = payload.to_owned();
let payload = payload.as_mut();
let decoded_message: Sv2Message = (msg_type, payload).try_into().unwrap();
let decoded_message = match decoded_message {
Sv2Message::SetNewPrevHash(m) => m,
Expand Down Expand Up @@ -1050,8 +1062,10 @@ mod tests {

let mut decoded = decoder.next_frame().unwrap();

let msg_type = decoded.get_header().unwrap().msg_type();
let payload = decoded.payload();
let msg_type = decoded.header().msg_type();
let payload = decoded.payload().unwrap();
let mut payload = payload.to_owned();
let payload = payload.as_mut();
let decoded_message: Sv2Message = (msg_type, payload).try_into().unwrap();
let decoded_message = match decoded_message {
Sv2Message::SubmitSolution(m) => m,
Expand Down Expand Up @@ -1108,8 +1122,10 @@ mod tests {

let mut decoded = decoder.next_frame().unwrap();

let msg_type = decoded.get_header().unwrap().msg_type();
let payload = decoded.payload();
let msg_type = decoded.header().msg_type();
let payload = decoded.payload().unwrap();
let mut payload = payload.to_owned();
let payload = payload.as_mut();
let decoded_message: Sv2Message = (msg_type, payload).try_into().unwrap();
let decoded_message = match decoded_message {
Sv2Message::ChannelEndpointChanged(m) => m,
Expand Down Expand Up @@ -1144,8 +1160,10 @@ mod tests {

let mut decoded = decoder.next_frame().unwrap();

let msg_type = decoded.get_header().unwrap().msg_type();
let payload = decoded.payload();
let msg_type = decoded.header().msg_type();
let payload = decoded.payload().unwrap();
let mut payload = payload.to_owned();
let payload = payload.as_mut();
let decoded_message: Sv2Message = (msg_type, payload).try_into().unwrap();
let decoded_message = match decoded_message {
Sv2Message::SetupConnection(m) => m,
Expand Down Expand Up @@ -1193,8 +1211,10 @@ mod tests {

let mut decoded = decoder.next_frame().unwrap();

let msg_type = decoded.get_header().unwrap().msg_type();
let payload = decoded.payload();
let msg_type = decoded.header().msg_type();
let payload = decoded.payload().unwrap();
let mut payload = payload.to_owned();
let payload = payload.as_mut();
let decoded_message: Sv2Message = (msg_type, payload).try_into().unwrap();
let decoded_message = match decoded_message {
Sv2Message::SetupConnectionError(m) => m,
Expand Down Expand Up @@ -1242,8 +1262,10 @@ mod tests {

let mut decoded = decoder.next_frame().unwrap();

let msg_type = decoded.get_header().unwrap().msg_type();
let payload = decoded.payload();
let msg_type = decoded.header().msg_type();
let payload = decoded.payload().unwrap();
let mut payload = payload.to_owned();
let payload = payload.as_mut();
let decoded_message: Sv2Message = (msg_type, payload).try_into().unwrap();
let decoded_message = match decoded_message {
Sv2Message::SetupConnectionSuccess(m) => m,
Expand Down
Loading

0 comments on commit f9a97f9

Please sign in to comment.