Skip to content

Commit

Permalink
tests: proptest codec roundtrip
Browse files Browse the repository at this point in the history
  • Loading branch information
niklaslong committed Aug 27, 2023
1 parent 44bfd57 commit 6d6b9c0
Show file tree
Hide file tree
Showing 5 changed files with 30 additions and 35 deletions.
2 changes: 1 addition & 1 deletion node/narwhal/src/event/disconnect.rs
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ mod tests {
#[test]
fn serialize_deserialize() {
// TODO switch to an iteration method that doesn't require manually updating this vec if enums are added
let all_reasons = vec![
let all_reasons = [
DisconnectReason::ProtocolViolation,
DisconnectReason::NoReasonGiven,
DisconnectReason::InvalidChallengeResponse,
Expand Down
4 changes: 2 additions & 2 deletions node/narwhal/src/event/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -178,7 +178,7 @@ impl<N: Network> Event<N> {
}

#[cfg(test)]
mod prop_tests {
pub mod prop_tests {
use crate::{
event::{
batch_certified::prop_tests::any_batch_certified,
Expand Down Expand Up @@ -206,7 +206,7 @@ mod prop_tests {

type CurrentNetwork = snarkvm::prelude::Testnet3;

fn any_event() -> BoxedStrategy<Event<CurrentNetwork>> {
pub fn any_event() -> BoxedStrategy<Event<CurrentNetwork>> {
prop_oneof![
any_batch_certified().prop_map(Event::BatchCertified),
any_batch_propose().prop_map(Event::BatchPropose),
Expand Down
37 changes: 20 additions & 17 deletions node/narwhal/src/gateway.rs
Original file line number Diff line number Diff line change
Expand Up @@ -918,15 +918,29 @@ impl<N: Network> Gateway<N> {
}

#[cfg(test)]
pub mod prop_tests {
use crate::{helpers::init_worker_channels, Gateway, Worker, MAX_WORKERS, MEMORY_POOL_PORT};
mod prop_tests {
use crate::{
helpers::{init_worker_channels, Storage},
Gateway,
Worker,
MAX_WORKERS,
MEMORY_POOL_PORT,
};

use snarkos_account::Account;
use snarkos_node_narwhal_committee::{
prop_tests::{CommitteeContext, ValidatorSet},
MAX_COMMITTEE_SIZE,
};
use snarkos_node_narwhal_ledger_service::MockLedgerService;
use snarkos_node_tcp::P2P;
use snarkvm::prelude::Testnet3;

use indexmap::IndexMap;
use proptest::{
prelude::{any, any_with, Arbitrary, BoxedStrategy, Just, Strategy},
sample::Selector,
};
use snarkos_node_tcp::P2P;
use snarkvm::prelude::Testnet3;
use std::{
fmt::{Debug, Formatter},
net::{IpAddr, Ipv4Addr, SocketAddr},
Expand All @@ -936,17 +950,6 @@ pub mod prop_tests {

type CurrentNetwork = Testnet3;

use crate::{
helpers::Storage,
prop_tests::GatewayAddress::{Dev, Prod},
};
use snarkos_account::Account;
use snarkos_node_narwhal_committee::{
prop_tests::{CommitteeContext, ValidatorSet},
MAX_COMMITTEE_SIZE,
};
use snarkos_node_narwhal_ledger_service::MockLedgerService;

impl Debug for Gateway<CurrentNetwork> {
fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
// TODO implement Debug properly and move it over to production code
Expand Down Expand Up @@ -1001,7 +1004,7 @@ pub mod prop_tests {
Just(account_selector.select(validators)),
0u8..,
)
.prop_map(|(a, b, c, d)| (a, b, c.account, Dev(d)))
.prop_map(|(a, b, c, d)| (a, b, c.account, GatewayAddress::Dev(d)))
})
.boxed()
}
Expand All @@ -1016,7 +1019,7 @@ pub mod prop_tests {
Just(account_selector.select(validators)),
any::<Option<SocketAddr>>(),
)
.prop_map(|(a, b, c, d)| (a, b, c.account, Prod(d)))
.prop_map(|(a, b, c, d)| (a, b, c.account, GatewayAddress::Prod(d)))
})
.boxed()
}
Expand Down
18 changes: 5 additions & 13 deletions node/narwhal/src/helpers/codec.rs
Original file line number Diff line number Diff line change
Expand Up @@ -333,9 +333,10 @@ impl<N: Network> Decoder for NoiseCodec<N> {
mod tests {
use super::*;

use crate::{Disconnect, DisconnectReason};
use crate::prop_tests::any_event;

use snow::{params::NoiseParams, Builder};
use test_strategy::proptest;

type CurrentNetwork = snarkvm::prelude::Testnet3;

Expand Down Expand Up @@ -387,17 +388,8 @@ mod tests {
assert_eq!(responder_codec.decode(&mut ciphertext).unwrap().unwrap(), msg);
}

macro_rules! test_disconnect_roundtrip {
($($reason:ident), *) => {
#[test]
fn disconnect_roundtrip() {
$(
let disconnect = EventOrBytes::Event(Event::Disconnect(Disconnect { reason: DisconnectReason::$reason }));
assert_roundtrip(disconnect);
)*
}
};
#[proptest]
fn event_roundtrip(#[strategy(any_event())] event: Event<CurrentNetwork>) {
assert_roundtrip(EventOrBytes::Event(event))
}

test_disconnect_roundtrip!(InvalidChallengeResponse, NoReasonGiven, ProtocolViolation, OutdatedClientVersion);
}
4 changes: 2 additions & 2 deletions node/narwhal/src/helpers/pending.rs
Original file line number Diff line number Diff line change
Expand Up @@ -147,8 +147,8 @@ mod tests {
assert!(!pending.is_empty());

// Check the items.
let ids = vec![commitment_1, commitment_2, commitment_3];
let peers = vec![addr_1, addr_2, addr_3];
let ids = [commitment_1, commitment_2, commitment_3];
let peers = [addr_1, addr_2, addr_3];

for i in 0..3 {
let id = ids[i];
Expand Down

0 comments on commit 6d6b9c0

Please sign in to comment.