Skip to content

Commit

Permalink
feat: proptest ChallengeRequest (de)serialization
Browse files Browse the repository at this point in the history
  • Loading branch information
vvp committed Jul 27, 2023
1 parent f46ccd0 commit 9a917b3
Showing 1 changed file with 32 additions and 0 deletions.
32 changes: 32 additions & 0 deletions node/narwhal/src/event/challenge_request.rs
Original file line number Diff line number Diff line change
Expand Up @@ -49,3 +49,35 @@ impl<N: Network> EventTrait for ChallengeRequest<N> {
Ok(Self { version, listener_port, address, nonce })
}
}

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

type CurrentNetwork = snarkvm::prelude::Testnet3;

fn any_challenge_request() -> BoxedStrategy<ChallengeRequest<CurrentNetwork>> {
(any_valid_account(), any::<u64>(), any::<u32>(), any::<u16>())
.prop_map(|(account, nonce, version, listener_port)| ChallengeRequest {
address: account.address(),
nonce,
version,
listener_port,
})
.boxed()
}

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

let deserialized_request: ChallengeRequest<CurrentNetwork> =
ChallengeRequest::deserialize(buf.get_ref().clone()).unwrap();
assert_eq!(request, deserialized_request);
}
}

0 comments on commit 9a917b3

Please sign in to comment.