Skip to content

Commit

Permalink
chore: adjust the tests
Browse files Browse the repository at this point in the history
Signed-off-by: ljedrz <[email protected]>
  • Loading branch information
ljedrz committed Aug 17, 2023
1 parent f471a72 commit 50ac11a
Show file tree
Hide file tree
Showing 11 changed files with 72 additions and 64 deletions.
12 changes: 5 additions & 7 deletions node/narwhal/src/event/batch_certified.rs
Original file line number Diff line number Diff line change
Expand Up @@ -58,12 +58,10 @@ impl<N: Network> FromBytes for BatchCertified<N> {

#[cfg(test)]
pub mod prop_tests {
use crate::{
event::{certificate_response::prop_tests::any_batch_certificate, EventTrait},
BatchCertified,
};
use bytes::{BufMut, BytesMut};
use crate::{event::certificate_response::prop_tests::any_batch_certificate, BatchCertified};
use bytes::{Buf, BufMut, BytesMut};
use proptest::prelude::{BoxedStrategy, Strategy};
use snarkvm::console::prelude::{FromBytes, ToBytes};
use test_strategy::proptest;

type CurrentNetwork = snarkvm::prelude::Testnet3;
Expand All @@ -75,9 +73,9 @@ pub mod prop_tests {
#[proptest]
fn serialize_deserialize(#[strategy(any_batch_certified())] original: BatchCertified<CurrentNetwork>) {
let mut buf = BytesMut::default().writer();
BatchCertified::serialize(&original, &mut buf).unwrap();
BatchCertified::write_le(&original, &mut buf).unwrap();

let deserialized: BatchCertified<CurrentNetwork> = BatchCertified::deserialize(buf.get_ref().clone()).unwrap();
let deserialized: BatchCertified<CurrentNetwork> = BatchCertified::read_le(buf.into_inner().reader()).unwrap();
assert_eq!(
original.certificate.deserialize_blocking().unwrap(),
deserialized.certificate.deserialize_blocking().unwrap()
Expand Down
16 changes: 8 additions & 8 deletions node/narwhal/src/event/batch_propose.rs
Original file line number Diff line number Diff line change
Expand Up @@ -61,14 +61,14 @@ impl<N: Network> FromBytes for BatchPropose<N> {

#[cfg(test)]
pub mod prop_tests {
use crate::{
event::{certificate_response::prop_tests::any_batch_header, EventTrait},
BatchPropose,
};
use bytes::{BufMut, BytesMut};
use crate::{event::certificate_response::prop_tests::any_batch_header, BatchPropose};
use bytes::{Buf, BufMut, BytesMut};
use proptest::prelude::{any, BoxedStrategy, Strategy};
use snarkos_node_narwhal_committee::prop_tests::CommitteeContext;
use snarkvm::prelude::narwhal::Data;
use snarkvm::{
console::prelude::{FromBytes, ToBytes},
prelude::narwhal::Data,
};
use test_strategy::proptest;

type CurrentNetwork = snarkvm::prelude::Testnet3;
Expand All @@ -83,9 +83,9 @@ pub mod prop_tests {
#[proptest]
fn serialize_deserialize(#[strategy(any_batch_propose())] original: BatchPropose<CurrentNetwork>) {
let mut buf = BytesMut::default().writer();
BatchPropose::serialize(&original, &mut buf).unwrap();
BatchPropose::write_le(&original, &mut buf).unwrap();

let deserialized: BatchPropose<CurrentNetwork> = BatchPropose::deserialize(buf.get_ref().clone()).unwrap();
let deserialized: BatchPropose<CurrentNetwork> = BatchPropose::read_le(buf.into_inner().reader()).unwrap();
// because of the Data enum, we cannot compare the structs directly even though it derives PartialEq
assert_eq!(original.round, deserialized.round);
assert_eq!(
Expand Down
13 changes: 5 additions & 8 deletions node/narwhal/src/event/batch_signature.rs
Original file line number Diff line number Diff line change
Expand Up @@ -58,16 +58,13 @@ impl<N: Network> FromBytes for BatchSignature<N> {
#[cfg(test)]
pub mod prop_tests {
use crate::{
event::{
certificate_request::prop_tests::any_field,
challenge_response::prop_tests::any_signature,
EventTrait,
},
event::{certificate_request::prop_tests::any_field, challenge_response::prop_tests::any_signature},
helpers::now,
BatchSignature,
};
use bytes::{BufMut, BytesMut};
use bytes::{Buf, BufMut, BytesMut};
use proptest::prelude::{BoxedStrategy, Just, Strategy};
use snarkvm::console::prelude::{FromBytes, ToBytes};
use test_strategy::proptest;

type CurrentNetwork = snarkvm::prelude::Testnet3;
Expand All @@ -83,9 +80,9 @@ pub mod prop_tests {
#[proptest]
fn serialize_deserialize(#[strategy(any_batch_signature())] original: BatchSignature<CurrentNetwork>) {
let mut buf = BytesMut::default().writer();
BatchSignature::serialize(&original, &mut buf).unwrap();
BatchSignature::write_le(&original, &mut buf).unwrap();

let deserialized: BatchSignature<CurrentNetwork> = BatchSignature::deserialize(buf.get_ref().clone()).unwrap();
let deserialized: BatchSignature<CurrentNetwork> = BatchSignature::read_le(buf.into_inner().reader()).unwrap();
assert_eq!(original, deserialized);
}
}
13 changes: 8 additions & 5 deletions node/narwhal/src/event/certificate_request.rs
Original file line number Diff line number Diff line change
Expand Up @@ -58,10 +58,13 @@ impl<N: Network> FromBytes for CertificateRequest<N> {

#[cfg(test)]
pub mod prop_tests {
use crate::{event::EventTrait, helpers::storage::prop_tests::CryptoTestRng, CertificateRequest};
use bytes::{BufMut, BytesMut};
use crate::{helpers::storage::prop_tests::CryptoTestRng, CertificateRequest};
use bytes::{Buf, BufMut, BytesMut};
use proptest::prelude::{any, BoxedStrategy, Strategy};
use snarkvm::prelude::{Field, Uniform};
use snarkvm::{
console::prelude::{FromBytes, ToBytes},
prelude::{Field, Uniform},
};
use test_strategy::proptest;

type CurrentNetwork = snarkvm::prelude::Testnet3;
Expand All @@ -77,10 +80,10 @@ pub mod prop_tests {
#[proptest]
fn serialize_deserialize(#[strategy(any_certificate_request())] original: CertificateRequest<CurrentNetwork>) {
let mut buf = BytesMut::default().writer();
CertificateRequest::serialize(&original, &mut buf).unwrap();
CertificateRequest::write_le(&original, &mut buf).unwrap();

let deserialized: CertificateRequest<CurrentNetwork> =
CertificateRequest::deserialize(buf.get_ref().clone()).unwrap();
CertificateRequest::read_le(buf.into_inner().reader()).unwrap();
assert_eq!(original, deserialized);
}
}
13 changes: 8 additions & 5 deletions node/narwhal/src/event/certificate_response.rs
Original file line number Diff line number Diff line change
Expand Up @@ -59,21 +59,24 @@ impl<N: Network> FromBytes for CertificateResponse<N> {
#[cfg(test)]
pub mod prop_tests {
use crate::{
event::{transmission_response::prop_tests::any_transmission, EventTrait},
event::transmission_response::prop_tests::any_transmission,
helpers::{
now,
storage::prop_tests::{sign_batch_header, CryptoTestRng},
},
CertificateResponse,
};
use bytes::{BufMut, BytesMut};
use bytes::{Buf, BufMut, BytesMut};
use proptest::{
collection::vec,
prelude::{any, BoxedStrategy, Just, Strategy},
sample::Selector,
};
use snarkos_node_narwhal_committee::prop_tests::{CommitteeContext, ValidatorSet};
use snarkvm::ledger::narwhal::{BatchCertificate, BatchHeader};
use snarkvm::{
console::prelude::{FromBytes, ToBytes},
ledger::narwhal::{BatchCertificate, BatchHeader},
};
use test_strategy::proptest;

type CurrentNetwork = snarkvm::prelude::Testnet3;
Expand Down Expand Up @@ -109,10 +112,10 @@ pub mod prop_tests {
#[proptest]
fn serialize_deserialize(#[strategy(any_certificate_response())] original: CertificateResponse<CurrentNetwork>) {
let mut buf = BytesMut::default().writer();
CertificateResponse::serialize(&original, &mut buf).unwrap();
CertificateResponse::write_le(&original, &mut buf).unwrap();

let deserialized: CertificateResponse<CurrentNetwork> =
CertificateResponse::deserialize(buf.get_ref().clone()).unwrap();
CertificateResponse::read_le(buf.into_inner().reader()).unwrap();
assert_eq!(original, deserialized);
}
}
9 changes: 5 additions & 4 deletions node/narwhal/src/event/challenge_request.rs
Original file line number Diff line number Diff line change
Expand Up @@ -60,10 +60,11 @@ impl<N: Network> FromBytes for ChallengeRequest<N> {

#[cfg(test)]
pub mod prop_tests {
use crate::{event::EventTrait, ChallengeRequest};
use bytes::{BufMut, BytesMut};
use crate::ChallengeRequest;
use bytes::{Buf, BufMut, BytesMut};
use proptest::prelude::{any, BoxedStrategy, Strategy};
use snarkos_node_narwhal_committee::prop_tests::any_valid_account;
use snarkvm::console::prelude::{FromBytes, ToBytes};
use test_strategy::proptest;

type CurrentNetwork = snarkvm::prelude::Testnet3;
Expand All @@ -82,10 +83,10 @@ pub mod prop_tests {
#[proptest]
fn serialize_deserialize(#[strategy(any_challenge_request())] original: ChallengeRequest<CurrentNetwork>) {
let mut buf = BytesMut::default().writer();
ChallengeRequest::serialize(&original, &mut buf).unwrap();
ChallengeRequest::write_le(&original, &mut buf).unwrap();

let deserialized: ChallengeRequest<CurrentNetwork> =
ChallengeRequest::deserialize(buf.get_ref().clone()).unwrap();
ChallengeRequest::read_le(buf.into_inner().reader()).unwrap();
assert_eq!(original, deserialized);
}
}
9 changes: 5 additions & 4 deletions node/narwhal/src/event/challenge_response.rs
Original file line number Diff line number Diff line change
Expand Up @@ -44,10 +44,11 @@ impl<N: Network> FromBytes for ChallengeResponse<N> {

#[cfg(test)]
pub mod prop_tests {
use crate::{event::EventTrait, helpers::storage::prop_tests::CryptoTestRng, ChallengeResponse};
use bytes::{BufMut, BytesMut};
use crate::{helpers::storage::prop_tests::CryptoTestRng, ChallengeResponse};
use bytes::{Buf, BufMut, BytesMut};
use proptest::prelude::{any, BoxedStrategy, Strategy};
use snarkvm::{
console::prelude::{FromBytes, ToBytes},
ledger::narwhal::Data,
prelude::{PrivateKey, Signature},
utilities::rand::Uniform,
Expand All @@ -73,10 +74,10 @@ pub mod prop_tests {
#[proptest]
fn serialize_deserialize(#[strategy(any_challenge_response())] original: ChallengeResponse<CurrentNetwork>) {
let mut buf = BytesMut::default().writer();
ChallengeResponse::serialize(&original, &mut buf).unwrap();
ChallengeResponse::write_le(&original, &mut buf).unwrap();

let deserialized: ChallengeResponse<CurrentNetwork> =
ChallengeResponse::deserialize(buf.get_ref().clone()).unwrap();
ChallengeResponse::read_le(buf.into_inner().reader()).unwrap();
assert_eq!(
original.signature.deserialize_blocking().unwrap(),
deserialized.signature.deserialize_blocking().unwrap()
Expand Down
17 changes: 9 additions & 8 deletions node/narwhal/src/event/disconnect.rs
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ impl FromBytes for Disconnect {
Ok(1) => DisconnectReason::NoReasonGiven,
Ok(2) => DisconnectReason::ProtocolViolation,
Ok(3) => DisconnectReason::OutdatedClientVersion,
_ => return Err(io::Error::new(io::ErrorKind::Other, "Invalid disconnect reason")),
_ => return Err(io::Error::new(io::ErrorKind::Other, "Invalid 'Disconnect' event")),
};

Ok(Self { reason })
Expand All @@ -73,8 +73,9 @@ impl FromBytes for Disconnect {

#[cfg(test)]
mod tests {
use crate::{event::EventTrait, Disconnect, DisconnectReason};
use bytes::{BufMut, BytesMut};
use crate::{Disconnect, DisconnectReason};
use bytes::{Buf, BufMut, Bytes, BytesMut};
use snarkvm::console::prelude::{FromBytes, ToBytes};

#[test]
fn serialize_deserialize() {
Expand All @@ -89,17 +90,17 @@ mod tests {
for reason in all_reasons.iter() {
let disconnect = Disconnect::from(*reason);
let mut buf = BytesMut::default().writer();
Disconnect::serialize(&disconnect, &mut buf).unwrap();
Disconnect::write_le(&disconnect, &mut buf).unwrap();

let disconnect = Disconnect::deserialize(buf.get_ref().clone()).unwrap();
let disconnect = Disconnect::read_le(buf.into_inner().reader()).unwrap();
assert_eq!(reason, &disconnect.reason);
}
}

#[test]
fn deserializing_empty_defaults_no_reason() {
let buf = BytesMut::default().writer();
let disconnect = Disconnect::deserialize(buf.get_ref().clone()).unwrap();
let buf = Bytes::default();
let disconnect = Disconnect::read_le(buf.reader()).unwrap();
assert_eq!(disconnect.reason, DisconnectReason::NoReasonGiven);
}

Expand All @@ -108,6 +109,6 @@ mod tests {
fn deserializing_invalid_data_panics() {
let mut buf = BytesMut::default().writer();
bincode::serialize_into(&mut buf, "not a DisconnectReason-value").unwrap();
let _disconnect = Disconnect::deserialize(buf.get_ref().clone()).unwrap();
let _disconnect = Disconnect::read_le(buf.into_inner().reader()).unwrap();
}
}
12 changes: 7 additions & 5 deletions node/narwhal/src/event/transmission_request.rs
Original file line number Diff line number Diff line change
Expand Up @@ -59,16 +59,18 @@ impl<N: Network> FromBytes for TransmissionRequest<N> {
#[cfg(test)]
pub mod prop_tests {
use crate::{
event::EventTrait,
helpers::storage::prop_tests::{any_puzzle_commitment, any_transaction_id},
TransmissionRequest,
};
use bytes::{BufMut, BytesMut};
use bytes::{Buf, BufMut, BytesMut};
use proptest::{
prelude::{BoxedStrategy, Strategy},
prop_oneof,
};
use snarkvm::ledger::narwhal::TransmissionID;
use snarkvm::{
console::prelude::{FromBytes, ToBytes},
ledger::narwhal::TransmissionID,
};
use test_strategy::proptest;
type CurrentNetwork = snarkvm::prelude::Testnet3;

Expand All @@ -87,9 +89,9 @@ pub mod prop_tests {
#[proptest]
fn serialize_deserialize(#[strategy(any_transmission_request())] original: TransmissionRequest<CurrentNetwork>) {
let mut buf = BytesMut::default().writer();
TransmissionRequest::serialize(&original, &mut buf).unwrap();
TransmissionRequest::write_le(&original, &mut buf).unwrap();

let deserialized = TransmissionRequest::deserialize(buf.get_ref().clone()).unwrap();
let deserialized = TransmissionRequest::read_le(buf.into_inner().reader()).unwrap();
assert_eq!(original, deserialized);
}
}
13 changes: 7 additions & 6 deletions node/narwhal/src/event/transmission_response.rs
Original file line number Diff line number Diff line change
Expand Up @@ -62,18 +62,19 @@ impl<N: Network> FromBytes for TransmissionResponse<N> {
#[cfg(test)]
pub mod prop_tests {
use crate::{
event::EventTrait,
helpers::storage::prop_tests::{any_puzzle_commitment, any_transaction_id},
TransmissionResponse,
};
use ::bytes::Bytes;
use bytes::{BufMut, BytesMut};
use bytes::{Buf, BufMut, Bytes, BytesMut};
use proptest::{
collection,
prelude::{any, BoxedStrategy, Strategy},
prop_oneof,
};
use snarkvm::ledger::narwhal::{Data, Transmission, TransmissionID};
use snarkvm::{
console::prelude::{FromBytes, ToBytes},
ledger::narwhal::{Data, Transmission, TransmissionID},
};
use test_strategy::proptest;

type CurrentNetwork = snarkvm::prelude::Testnet3;
Expand All @@ -99,9 +100,9 @@ pub mod prop_tests {
#[proptest]
fn serialize_deserialize(#[strategy(any_transmission_response())] original: TransmissionResponse<CurrentNetwork>) {
let mut buf = BytesMut::default().writer();
TransmissionResponse::serialize(&original, &mut buf).unwrap();
TransmissionResponse::write_le(&original, &mut buf).unwrap();

let deserialized = TransmissionResponse::deserialize(buf.get_ref().clone()).unwrap();
let deserialized = TransmissionResponse::read_le(buf.into_inner().reader()).unwrap();
assert_eq!(original, deserialized);
}
}
9 changes: 5 additions & 4 deletions node/narwhal/src/event/worker_ping.rs
Original file line number Diff line number Diff line change
Expand Up @@ -65,12 +65,13 @@ impl<N: Network> FromBytes for WorkerPing<N> {

#[cfg(test)]
pub mod prop_tests {
use crate::{event::EventTrait, helpers::storage::prop_tests::any_transmission_id, WorkerPing};
use bytes::{BufMut, BytesMut};
use crate::{helpers::storage::prop_tests::any_transmission_id, WorkerPing};
use bytes::{Buf, BufMut, BytesMut};
use proptest::{
collection::hash_set,
prelude::{BoxedStrategy, Strategy},
};
use snarkvm::console::prelude::{FromBytes, ToBytes};
use test_strategy::proptest;
type CurrentNetwork = snarkvm::prelude::Testnet3;

Expand All @@ -81,9 +82,9 @@ pub mod prop_tests {
#[proptest]
fn serialize_deserialize(#[strategy(any_worker_ping())] original: WorkerPing<CurrentNetwork>) {
let mut buf = BytesMut::default().writer();
WorkerPing::serialize(&original, &mut buf).unwrap();
WorkerPing::write_le(&original, &mut buf).unwrap();

let deserialized = WorkerPing::deserialize(buf.get_ref().clone()).unwrap();
let deserialized = WorkerPing::read_le(buf.into_inner().reader()).unwrap();
assert_eq!(original, deserialized);
}
}

0 comments on commit 50ac11a

Please sign in to comment.