Skip to content

Commit

Permalink
chore: renamings and unifying strategies
Browse files Browse the repository at this point in the history
  • Loading branch information
vvp committed Jul 27, 2023
1 parent fce5f40 commit ad2a77f
Show file tree
Hide file tree
Showing 10 changed files with 62 additions and 63 deletions.
11 changes: 5 additions & 6 deletions node/narwhal/src/event/batch_certified.rs
Original file line number Diff line number Diff line change
Expand Up @@ -74,15 +74,14 @@ mod prop_tests {
}

#[proptest]
fn serialize_deserialize(#[strategy(any_batch_certified())] batch_certified: BatchCertified<CurrentNetwork>) {
fn serialize_deserialize(#[strategy(any_batch_certified())] original: BatchCertified<CurrentNetwork>) {
let mut buf = BytesMut::with_capacity(64).writer();
BatchCertified::serialize(&batch_certified, &mut buf).unwrap();
BatchCertified::serialize(&original, &mut buf).unwrap();

let deserialized_batch_certified: BatchCertified<CurrentNetwork> =
BatchCertified::deserialize(buf.get_ref().clone()).unwrap();
let deserialized: BatchCertified<CurrentNetwork> = BatchCertified::deserialize(buf.get_ref().clone()).unwrap();
assert_eq!(
batch_certified.certificate.deserialize_blocking().unwrap(),
deserialized_batch_certified.certificate.deserialize_blocking().unwrap()
original.certificate.deserialize_blocking().unwrap(),
deserialized.certificate.deserialize_blocking().unwrap()
);
}
}
12 changes: 6 additions & 6 deletions node/narwhal/src/event/batch_propose.rs
Original file line number Diff line number Diff line change
Expand Up @@ -82,17 +82,17 @@ mod prop_tests {
}

#[proptest]
fn serialize_deserialize(#[strategy(any_batch_propose())] propose: BatchPropose<CurrentNetwork>) {
fn serialize_deserialize(#[strategy(any_batch_propose())] original: BatchPropose<CurrentNetwork>) {
let mut buf = BytesMut::with_capacity(64).writer();
BatchPropose::serialize(&propose, &mut buf).unwrap();
BatchPropose::serialize(&original, &mut buf).unwrap();

let deserialized_propose: BatchPropose<CurrentNetwork> =
let deserialized: BatchPropose<CurrentNetwork> =
BatchPropose::deserialize(buf.get_ref().clone()).unwrap();
// because of the Data enum, we cannot compare the structs directly even though it derives PartialEq
assert_eq!(propose.round, deserialized_propose.round);
assert_eq!(original.round, deserialized.round);
assert_eq!(
propose.batch_header.deserialize_blocking().unwrap(),
deserialized_propose.batch_header.deserialize_blocking().unwrap()
original.batch_header.deserialize_blocking().unwrap(),
deserialized.batch_header.deserialize_blocking().unwrap()
);
}
}
8 changes: 4 additions & 4 deletions node/narwhal/src/event/batch_signature.rs
Original file line number Diff line number Diff line change
Expand Up @@ -82,12 +82,12 @@ mod prop_tests {
}

#[proptest]
fn serialize_deserialize(#[strategy(any_batch_signature())] signature: BatchSignature<CurrentNetwork>) {
fn serialize_deserialize(#[strategy(any_batch_signature())] original: BatchSignature<CurrentNetwork>) {
let mut buf = BytesMut::with_capacity(64).writer();
BatchSignature::serialize(&signature, &mut buf).unwrap();
BatchSignature::serialize(&original, &mut buf).unwrap();

let deserialized_signature: BatchSignature<CurrentNetwork> =
let deserialized: BatchSignature<CurrentNetwork> =
BatchSignature::deserialize(buf.get_ref().clone()).unwrap();
assert_eq!(signature, deserialized_signature);
assert_eq!(original, deserialized);
}
}
8 changes: 4 additions & 4 deletions node/narwhal/src/event/certificate_request.rs
Original file line number Diff line number Diff line change
Expand Up @@ -77,12 +77,12 @@ pub mod prop_tests {
}

#[proptest]
fn serialize_deserialize(#[strategy(any_challenge_request())] request: CertificateRequest<CurrentNetwork>) {
fn serialize_deserialize(#[strategy(any_challenge_request())] original: CertificateRequest<CurrentNetwork>) {
let mut buf = BytesMut::with_capacity(64).writer();
CertificateRequest::serialize(&request, &mut buf).unwrap();
CertificateRequest::serialize(&original, &mut buf).unwrap();

let deserialized_request: CertificateRequest<CurrentNetwork> =
let deserialized: CertificateRequest<CurrentNetwork> =
CertificateRequest::deserialize(buf.get_ref().clone()).unwrap();
assert_eq!(request, deserialized_request);
assert_eq!(original, deserialized);
}
}
8 changes: 4 additions & 4 deletions node/narwhal/src/event/certificate_response.rs
Original file line number Diff line number Diff line change
Expand Up @@ -117,12 +117,12 @@ pub mod prop_tests {
}

#[proptest]
fn serialize_deserialize(#[strategy(any_certificate_response())] response: CertificateResponse<CurrentNetwork>) {
fn serialize_deserialize(#[strategy(any_certificate_response())] original: CertificateResponse<CurrentNetwork>) {
let mut buf = BytesMut::with_capacity(64).writer();
CertificateResponse::serialize(&response, &mut buf).unwrap();
CertificateResponse::serialize(&original, &mut buf).unwrap();

let deserialized_response: CertificateResponse<CurrentNetwork> =
let deserialized: CertificateResponse<CurrentNetwork> =
CertificateResponse::deserialize(buf.get_ref().clone()).unwrap();
assert_eq!(response, deserialized_response);
assert_eq!(original, deserialized);
}
}
8 changes: 4 additions & 4 deletions node/narwhal/src/event/challenge_request.rs
Original file line number Diff line number Diff line change
Expand Up @@ -72,12 +72,12 @@ mod prop_tests {
}

#[proptest]
fn serialize_deserialize(#[strategy(any_challenge_request())] request: ChallengeRequest<CurrentNetwork>) {
fn serialize_deserialize(#[strategy(any_challenge_request())] original: ChallengeRequest<CurrentNetwork>) {
let mut buf = BytesMut::with_capacity(64).writer();
ChallengeRequest::serialize(&request, &mut buf).unwrap();
ChallengeRequest::serialize(&original, &mut buf).unwrap();

let deserialized_request: ChallengeRequest<CurrentNetwork> =
let deserialized: ChallengeRequest<CurrentNetwork> =
ChallengeRequest::deserialize(buf.get_ref().clone()).unwrap();
assert_eq!(request, deserialized_request);
assert_eq!(original, deserialized);
}
}
17 changes: 11 additions & 6 deletions node/narwhal/src/event/challenge_response.rs
Original file line number Diff line number Diff line change
Expand Up @@ -64,15 +64,20 @@ pub mod prop_tests {
.boxed()
}

#[proptest]
fn serialize_deserialize(#[strategy(any_signature())] signature: Signature<CurrentNetwork>) {
let response = ChallengeResponse::<CurrentNetwork> { signature: Data::Object(signature) };
fn any_challenge_response() -> BoxedStrategy<ChallengeResponse<CurrentNetwork>> {
any_signature().prop_map(|sig| ChallengeResponse { signature: Data::Object(sig) }).boxed()
}

#[proptest]
fn serialize_deserialize(#[strategy(any_challenge_response())] original: ChallengeResponse<CurrentNetwork>) {
let mut buf = BytesMut::with_capacity(64).writer();
ChallengeResponse::serialize(&response, &mut buf).unwrap();
ChallengeResponse::serialize(&original, &mut buf).unwrap();

let response: ChallengeResponse<CurrentNetwork> =
let deserialized: ChallengeResponse<CurrentNetwork> =
ChallengeResponse::deserialize(buf.get_ref().clone()).unwrap();
assert_eq!(signature, response.signature.deserialize_blocking().unwrap());
assert_eq!(
original.signature.deserialize_blocking().unwrap(),
deserialized.signature.deserialize_blocking().unwrap()
);
}
}
16 changes: 9 additions & 7 deletions node/narwhal/src/event/transmission_request.rs
Original file line number Diff line number Diff line change
Expand Up @@ -74,22 +74,24 @@ mod prop_tests {
use test_strategy::proptest;
type CurrentNetwork = snarkvm::prelude::Testnet3;

fn any_transmission() -> BoxedStrategy<TransmissionID<CurrentNetwork>> {
fn any_transmission_id() -> BoxedStrategy<TransmissionID<CurrentNetwork>> {
prop_oneof![
any_puzzle_commitment().prop_map(TransmissionID::Solution),
any_transaction_id().prop_map(TransmissionID::Transaction),
]
.boxed()
}

#[proptest]
fn serialize_deserialize(#[strategy(any_transmission())] input: TransmissionID<CurrentNetwork>) {
let request = TransmissionRequest::new(input);
fn any_transmission_request() -> BoxedStrategy<TransmissionRequest<CurrentNetwork>> {
any_transmission_id().prop_map(TransmissionRequest::new).boxed()
}

#[proptest]
fn serialize_deserialize(#[strategy(any_transmission_request())] original: TransmissionRequest<CurrentNetwork>) {
let mut buf = BytesMut::with_capacity(64).writer();
TransmissionRequest::serialize(&request, &mut buf).unwrap();
TransmissionRequest::serialize(&original, &mut buf).unwrap();

let request = TransmissionRequest::deserialize(buf.get_ref().clone()).unwrap();
assert_eq!(input, request.transmission_id);
let deserialized = TransmissionRequest::deserialize(buf.get_ref().clone()).unwrap();
assert_eq!(original, deserialized);
}
}
19 changes: 9 additions & 10 deletions node/narwhal/src/event/transmission_response.rs
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,7 @@ pub mod prop_tests {
};
use snarkvm::ledger::narwhal::{Data, Transmission, TransmissionID};
use test_strategy::proptest;

type CurrentNetwork = snarkvm::prelude::Testnet3;

pub fn any_transmission() -> BoxedStrategy<(TransmissionID<CurrentNetwork>, Transmission<CurrentNetwork>)> {
Expand All @@ -93,18 +94,16 @@ pub mod prop_tests {
.boxed()
}

#[proptest]
fn serialize_deserialize(
#[strategy(any_transmission())] input: (TransmissionID<CurrentNetwork>, Transmission<CurrentNetwork>),
) {
let (id, transmission) = input;
let response = TransmissionResponse::new(id, transmission.clone());
pub fn any_transmission_response() -> BoxedStrategy<TransmissionResponse<CurrentNetwork>> {
any_transmission().prop_map(|(id, t)| TransmissionResponse::new(id, t)).boxed()
}

#[proptest]
fn serialize_deserialize(#[strategy(any_transmission_response())] original: TransmissionResponse<CurrentNetwork>) {
let mut buf = BytesMut::with_capacity(64).writer();
TransmissionResponse::serialize(&response, &mut buf).unwrap();
TransmissionResponse::serialize(&original, &mut buf).unwrap();

let response = TransmissionResponse::deserialize(buf.get_ref().clone()).unwrap();
assert_eq!(id, response.transmission_id);
assert_eq!(transmission, response.transmission);
let deserialized = TransmissionResponse::deserialize(buf.get_ref().clone()).unwrap();
assert_eq!(original, deserialized);
}
}
18 changes: 6 additions & 12 deletions node/narwhal/src/event/worker_ping.rs
Original file line number Diff line number Diff line change
Expand Up @@ -69,29 +69,23 @@ impl<N: Network> EventTrait for WorkerPing<N> {
mod prop_tests {
use crate::{event::EventTrait, helpers::storage::prop_tests::any_transmission_id, WorkerPing};
use bytes::{BufMut, BytesMut};
use indexmap::IndexSet;
use proptest::{
collection::hash_set,
prelude::{BoxedStrategy, Strategy},
};
use snarkvm::ledger::narwhal::TransmissionID;
use std::collections::HashSet;
use test_strategy::proptest;
type CurrentNetwork = snarkvm::prelude::Testnet3;

fn any_ids() -> BoxedStrategy<HashSet<TransmissionID<CurrentNetwork>>> {
hash_set(any_transmission_id(), 1..16).boxed()
fn any_worker_ping() -> BoxedStrategy<WorkerPing<CurrentNetwork>> {
hash_set(any_transmission_id(), 1..16).prop_map(|ids| WorkerPing::new(ids.into_iter().collect())).boxed()
}

#[proptest]
fn serialize_deserialize(#[strategy(any_ids())] ids: HashSet<TransmissionID<CurrentNetwork>>) {
let input: IndexSet<TransmissionID<CurrentNetwork>> = ids.into_iter().collect();
let worker_ping = WorkerPing::new(input.clone());

fn serialize_deserialize(#[strategy(any_worker_ping())] original: WorkerPing<CurrentNetwork>) {
let mut buf = BytesMut::with_capacity(64).writer();
WorkerPing::serialize(&worker_ping, &mut buf).unwrap();
WorkerPing::serialize(&original, &mut buf).unwrap();

let output = WorkerPing::deserialize(buf.get_ref().clone()).unwrap().transmission_ids;
assert_eq!(input, output);
let deserialized = WorkerPing::deserialize(buf.get_ref().clone()).unwrap();
assert_eq!(original, deserialized);
}
}

0 comments on commit ad2a77f

Please sign in to comment.