Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add test for unknown message fields #1259

Draft
wants to merge 14 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions benches/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,8 @@ async-channel = "1.4.0"
v1 = { path="../protocols/v1", package="sv1_api", version = "^1.0.0" }
serde_json = { version = "1.0.64", default-features = false, features = ["alloc"] }
iai="0.1"
mining_sv2 = { path = "../protocols/v2/subprotocols/mining", version = "^1.0.0" }
roles_logic_sv2 = { path = "../protocols/v2/roles-logic-sv2", version = "^1.0.0" }
mining_sv2 = { path = "../protocols/v2/subprotocols/mining", version = "^2.0.0" }
roles_logic_sv2 = { path = "../protocols/v2/roles-logic-sv2", version = "^2.0.0" }
framing_sv2 = { version = "2.0.0", path = "../protocols/v2/framing-sv2" }
serde = { version = "1.0.89", default-features = false, features = ["derive", "alloc"] }
num-bigint = "0.4.3"
Expand Down
34 changes: 34 additions & 0 deletions examples/ping-pong-with-noise/src/messages.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,21 @@ impl<'decoder> GetSize for Ping<'decoder> {
self.message.get_size() + self.id.get_size()
}
}
#[derive(Debug, Serialize, Deserialize)]
pub struct BiggerPing<'decoder> {
#[cfg_attr(feature = "with_serde", serde(borrow))]
message: Str0255<'decoder>,
id: U24,
#[cfg_attr(feature = "with_serde", serde(borrow))]
additional_data: Str0255<'decoder>,
}

#[cfg(feature = "with_serde")]
impl<'decoder> GetSize for BiggerPing<'decoder> {
fn get_size(&self) -> usize {
self.message.get_size() + self.id.get_size() + self.additional_data.get_size()
}
}

#[derive(Debug, Serialize, Deserialize)]
pub struct Pong<'decoder> {
Expand Down Expand Up @@ -59,6 +74,21 @@ impl<'decoder> Ping<'decoder> {
}
}

impl<'decoder> BiggerPing<'decoder> {
pub fn new(id: u32) -> Self {
let message: String = rand::thread_rng()
.sample_iter(&Alphanumeric)
.take(7)
.map(char::from)
.collect();
Self {
message: message.clone().into_bytes().try_into().unwrap(),
id: id.try_into().unwrap(),
additional_data: message.into_bytes().try_into().unwrap(),
}
}
}

impl<'decoder> Pong<'decoder> {
pub fn new(id: u32, seq: Vec<U256<'decoder>>) -> Self {
Self {
Expand All @@ -77,6 +107,7 @@ impl<'decoder> Pong<'decoder> {
pub enum Message<'decoder> {
Ping(Ping<'decoder>),
Pong(Pong<'decoder>),
BiggerPing(BiggerPing<'decoder>),
}

#[cfg(feature = "with_serde")]
Expand All @@ -88,6 +119,7 @@ impl<'decoder> binary_sv2::Serialize for Message<'decoder> {
match self {
Message::Ping(p) => p.serialize(serializer),
Message::Pong(p) => p.serialize(serializer),
Message::BiggerPing(p) => p.serialize(serializer),
}
}
}
Expand All @@ -108,6 +140,7 @@ impl<'decoder> From<Message<'decoder>> for binary_sv2::encodable::EncodableField
match m {
Message::Ping(p) => p.into(),
Message::Pong(p) => p.into(),
Message::BiggerPing(p) => p.into(),
}
}
}
Expand All @@ -129,6 +162,7 @@ impl GetSize for Message<'_> {
match self {
Self::Ping(ping) => ping.get_size(),
Self::Pong(pong) => pong.get_size(),
Self::BiggerPing(bp) => bp.get_size(),
}
}
}
4 changes: 2 additions & 2 deletions examples/ping-pong-with-noise/src/node.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use crate::messages::{Message, Ping, Pong};
use crate::messages::{Message, Ping, Pong,BiggerPing};
use binary_sv2::{from_bytes, GetSize, U256};
use rand::Rng;

Expand Down Expand Up @@ -127,7 +127,7 @@ impl Node {
pong.get_id(),
pong.get_size()
);
Message::Ping(Ping::new(self.last_id))
Message::BiggerPing(BiggerPing::new(self.last_id))
}
Err(_) => panic!(),
}
Expand Down
33 changes: 32 additions & 1 deletion examples/ping-pong-without-noise/src/messages.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,13 +10,40 @@ pub struct Ping<'decoder> {
message: Str0255<'decoder>,
id: U24,
}

#[cfg(feature = "with_serde")]
impl<'decoder> GetSize for Ping<'decoder> {
fn get_size(&self) -> usize {
self.message.get_size() + self.id.get_size()
}
}
#[derive(Debug, Serialize, Deserialize)]
pub struct BiggerPing<'decoder> {
#[cfg_attr(feature = "with_serde", serde(borrow))]
message: Str0255<'decoder>,
id: U24,
add_data: Str0255<'decoder>,
}

#[cfg(feature = "with_serde")]
impl<'decoder> GetSize for BiggerPing<'decoder> {
fn get_size(&self) -> usize {
self.message.get_size() + self.id.get_size() + self.add_data.get_size()
}
}
impl<'decoder> BiggerPing<'decoder> {
pub fn new(id: u32) -> Self {
let message: String = rand::thread_rng()
.sample_iter(&Alphanumeric)
.take(7)
.map(char::from)
.collect();
Self {
message: message.clone().into_bytes().try_into().unwrap(),
id: id.try_into().unwrap(),
add_data: message.into_bytes().try_into().unwrap(),
}
}
}

#[derive(Debug, Serialize, Deserialize)]
pub struct Pong<'decoder> {
Expand Down Expand Up @@ -77,6 +104,7 @@ impl<'decoder> Pong<'decoder> {
pub enum Message<'decoder> {
Ping(Ping<'decoder>),
Pong(Pong<'decoder>),
BiggerPing(BiggerPing<'decoder>),
}

#[cfg(feature = "with_serde")]
Expand All @@ -88,6 +116,7 @@ impl<'decoder> binary_sv2::Serialize for Message<'decoder> {
match self {
Message::Ping(p) => p.serialize(serializer),
Message::Pong(p) => p.serialize(serializer),
Message::BiggerPing(p) => p.serialize(serializer),
}
}
}
Expand All @@ -108,6 +137,7 @@ impl<'decoder> From<Message<'decoder>> for binary_sv2::encodable::EncodableField
match m {
Message::Ping(p) => p.into(),
Message::Pong(p) => p.into(),
Message::BiggerPing(p) => p.into(),
}
}
}
Expand All @@ -129,6 +159,7 @@ impl GetSize for Message<'_> {
match self {
Self::Ping(ping) => ping.get_size(),
Self::Pong(pong) => pong.get_size(),
Self::BiggerPing(bp) => bp.get_size(),
}
}
}
33 changes: 26 additions & 7 deletions examples/ping-pong-without-noise/src/node.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use crate::messages::{Message, Ping, Pong};
use crate::messages::{Message, Ping, Pong,BiggerPing};
use binary_sv2::{from_bytes, U256};
use rand::Rng;

Expand Down Expand Up @@ -66,7 +66,7 @@ impl Node {

pub async fn send_ping(&mut self) {
self.expected = Expected::Pong;
let message = Message::Ping(Ping::new(self.last_id));
let message = Message::BiggerPing(BiggerPing::new(self.last_id));
let frame =
StandardSv2Frame::<Message<'static>>::from_message(message, 0, 0, false).unwrap();
self.sender.send(frame).await.unwrap();
Expand All @@ -87,8 +87,8 @@ impl Node {
) -> Message<'static> {
match self.expected {
Expected::Ping => {
let ping: Result<Ping, _> = from_bytes(frame.payload());
match ping {
let ping: Result<BiggerPing, _> = from_bytes(frame.payload());
match ping.as_ref() {
Ok(ping) => {
println!("Node {} received:", self.name);
println!("{:#?}\n", ping);
Expand All @@ -100,9 +100,28 @@ impl Node {
}
Message::Pong(Pong::new(self.last_id, seq))
}
Err(e) => {
println!("{:#?}", e);
todo!()
Err(error) => {
let error = error.clone();
drop(ping);
let ping: Result<Ping, _> = from_bytes(frame.payload());
match ping {
Ok(ping) => {
println!("Node {} received:", self.name);
println!("{:#?}\n", ping);
let mut seq: Vec<U256> = vec![];
for _ in 0..100 {
let random_bytes = rand::thread_rng().gen::<[u8; 32]>();
let u256: U256 = random_bytes.into();
seq.push(u256);
}
Message::Pong(Pong::new(self.last_id, seq))
}
Err(e) => {
println!("Error parsing BiggerPing {:#?}", error);
println!("Error parsing Ping {:#?}", e);
todo!()
}
}
}
}
}
Expand Down
2 changes: 1 addition & 1 deletion protocols/fuzz-tests/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ arbitrary = { version = "1", features = ["derive"] }
rand = "0.8.3"
binary_codec_sv2 = { version = "1.0.0", path = "../v2/binary-sv2/no-serde-sv2/codec"}
codec_sv2 = { version = "1.0.0", path = "../v2/codec-sv2", features = ["noise_sv2"]}
roles_logic_sv2 = { version = "1.0.0", path = "../v2/roles-logic-sv2"}
roles_logic_sv2 = { version = "2.0.0", path = "../v2/roles-logic-sv2"}
affinity = "0.1.1"
threadpool = "1.8.1"
lazy_static = "1.4.0"
Expand Down
33 changes: 32 additions & 1 deletion protocols/v2/binary-sv2/binary-sv2/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
// TODO unify errors from serde_sv2 and no-serde-sv2

#![no_std]
//#![no_std]

#[macro_use]
extern crate alloc;
Expand Down Expand Up @@ -783,4 +783,35 @@ mod test {
assert_eq!(bytes, bytes_2);
}
}
mod test_decode_when_too_much_data {
use super::*;
use core::convert::TryInto;

#[derive(Clone, Deserialize, Serialize, PartialEq, Debug)]
struct Test {
a: u32,
b: u8,
c: U24,
}

#[test]
fn test_decode_when_too_much_data() {
let expected = Test {
a: 456,
b: 9,
c: 67_u32.try_into().unwrap(),
};

#[cfg(not(feature = "with_serde"))]
let mut bytes = to_bytes(expected.clone()).unwrap();
#[cfg(feature = "with_serde")]
let mut bytes = to_bytes(&expected.clone()).unwrap();

bytes.extend_from_slice(&[0, 0, 0, 0, 0, 0, 0, 0]);

let deserialized: Test = from_bytes(&mut bytes[..]).unwrap();

assert_eq!(deserialized, expected);
}
}
}
6 changes: 3 additions & 3 deletions protocols/v2/roles-logic-sv2/Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "roles_logic_sv2"
version = "1.2.1"
version = "2.0.0"
authors = ["The Stratum V2 Developers"]
edition = "2018"
readme = "README.md"
Expand All @@ -18,7 +18,7 @@ stratum-common = { version="1.0.0", path = "../../../common", features=["bitcoin
serde = { version = "1.0.89", features = ["derive", "alloc"], default-features = false, optional = true}
binary_sv2 = {version = "^1.0.0", path = "../../../protocols/v2/binary-sv2/binary-sv2", default-features = true }
common_messages_sv2 = { path = "../../../protocols/v2/subprotocols/common-messages", version = "^2.0.0" }
mining_sv2 = { path = "../../../protocols/v2/subprotocols/mining", version = "^1.0.0" }
mining_sv2 = { path = "../../../protocols/v2/subprotocols/mining", version = "^2.0.0" }
template_distribution_sv2 = { path = "../../../protocols/v2/subprotocols/template-distribution", version = "^1.0.1" }
job_declaration_sv2 = { path = "../../../protocols/v2/subprotocols/job-declaration", version = "^1.0.0" }
const_sv2 = { version = "^2.0.0", path = "../../../protocols/v2/const-sv2"}
Expand Down Expand Up @@ -46,4 +46,4 @@ prop_test = ["template_distribution_sv2/prop_test"]
disable_nopanic = []

[package.metadata.docs.rs]
all-features = true
all-features = true
Loading
Loading