diff --git a/zebra-network/Cargo.toml b/zebra-network/Cargo.toml index fa9438415..9ce0d1cf0 100644 --- a/zebra-network/Cargo.toml +++ b/zebra-network/Cargo.toml @@ -17,6 +17,8 @@ hex = "0.4" # which we don't use, so disable it to drop the dependencies. indexmap = { version = "1.2", default-features = false } pin-project = "0.4" +proptest = "0.9" +proptest-derive = "0.1.0" rand = "0.7" serde = { version = "1", features = ["serde_derive"] } thiserror = "1" diff --git a/zebra-network/proptest-regressions/protocol/internal.txt b/zebra-network/proptest-regressions/protocol/internal.txt new file mode 100644 index 000000000..3f172403d --- /dev/null +++ b/zebra-network/proptest-regressions/protocol/internal.txt @@ -0,0 +1,8 @@ +# Seeds for failure cases proptest has generated in the past. It is +# automatically read and these particular cases re-run before any +# novel cases are generated. +# +# It is recommended to check this file in to source control so that +# everyone who runs the test benefits from these saved cases. +cc fb728d0d2a16976de785ed8525cc74c4d3f7826f8840ebe5a4fee6b5ab0edad8 # shrinks to req = GetMempool +cc 3ed95f3a436a15e33b27f4bf1105629860b8e6a895165eb83befcb625e951b1d # shrinks to req = GetPeers diff --git a/zebra-network/src/protocol/internal.rs b/zebra-network/src/protocol/internal.rs index 186d2fb4f..a22450452 100644 --- a/zebra-network/src/protocol/internal.rs +++ b/zebra-network/src/protocol/internal.rs @@ -6,6 +6,9 @@ use std::error::Error; +#[cfg(test)] +use proptest_derive::Arbitrary; + use zebra_chain::transaction::Transaction; use crate::meta_addr::MetaAddr; @@ -14,15 +17,18 @@ use super::types::Nonce; /// A network request, represented in internal format. #[derive(Clone, Debug, Eq, PartialEq)] +#[cfg_attr(test, derive(Arbitrary))] pub enum Request { /// Requests additional peers from the server. GetPeers, /// Advertises peers to the remote server. + #[cfg_attr(test, proptest(skip))] PushPeers(Vec), /// Heartbeats triggered on peer connection start. // This is included as a bit of a hack, it should only be used // internally for connection management. You should not expect to // be firing or handling `Ping` requests or `Pong` responses. + #[cfg_attr(test, proptest(skip))] Ping(Nonce), /// Requests the transactions the remote server has verified but /// not yet confirmed. @@ -31,14 +37,17 @@ pub enum Request { /// A response to a network request, represented in internal format. #[derive(Clone, Debug, Eq, PartialEq)] +#[cfg_attr(test, derive(Arbitrary))] pub enum Response { /// Generic success. Ok, /// Generic error. Error, /// A list of peers, used to respond to `GetPeers`. + #[cfg_attr(test, proptest(skip))] Peers(Vec), /// A list of transactions, such as in response to `GetMempool`. + #[cfg_attr(test, proptest(skip))] Transactions(Vec), } @@ -50,3 +59,23 @@ where Self::Error } } + +#[cfg(test)] +mod test { + use proptest::prelude::*; + + use super::{Request, Response}; + + proptest! { + + #[test] + fn proptest_request(req in any::()) { + println!("{:?}", req); + } + + #[test] + fn proptest_response(res in any::()) { + println!("{:?}", res); + } + } +}