Skip to content

Commit

Permalink
feat(net): Arbitrary Prop Testing (#66)
Browse files Browse the repository at this point in the history
### Description

Adds arbitrary property-based testing to `net` crate objects.
  • Loading branch information
refcell authored Aug 30, 2024
1 parent f729cde commit ab14a33
Show file tree
Hide file tree
Showing 4 changed files with 60 additions and 2 deletions.
31 changes: 31 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 4 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -166,6 +166,10 @@ openssl = { version = "0.10.66", features = ["vendored"] }
libp2p-identity = { version = "0.2.9", features = [ "secp256k1" ] }
libp2p = { version = "0.54.0", features = ["macros", "tokio", "tcp", "noise", "gossipsub", "ping", "yamux"] }

# Testing
arbtest = "0.3"
arbitrary = { version = "1", features = ["derive"] }

# Misc
tracing = "0.1.0"
tracing-subscriber = "0.3.18"
Expand Down
11 changes: 11 additions & 0 deletions crates/net/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -33,3 +33,14 @@ tokio.workspace = true
tracing.workspace = true
lazy_static.workspace = true
unsigned-varint.workspace = true

# `arbitrary` feature dependencies
arbitrary = { workspace = true, optional = true }

[dev-dependencies]
arbtest.workspace = true
arbitrary.workspace = true

[features]
default = []
arbitrary = ["dep:arbitrary"]
16 changes: 14 additions & 2 deletions crates/net/src/types/enr.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ pub const OP_CL_KEY: &str = "opstack";

/// The unique L2 network identifier
#[derive(Debug, Clone, Copy, Default, PartialEq, Eq)]
#[cfg_attr(any(test, feature = "arbitrary"), derive(arbitrary::Arbitrary))]
pub struct OpStackEnr {
/// Chain ID
pub chain_id: u64,
Expand Down Expand Up @@ -64,7 +65,18 @@ mod tests {
use alloy::primitives::{bytes, Bytes};

#[test]
fn test_encode_decode_op_enr() {
fn roundtrip_op_stack_enr() {
arbtest::arbtest(|u| {
let op_stack_enr = OpStackEnr::new(u.arbitrary()?, 0);
let bytes = alloy_rlp::encode(op_stack_enr).to_vec();
let decoded = OpStackEnr::decode(&mut &bytes[..]).unwrap();
assert_eq!(decoded, op_stack_enr);
Ok(())
});
}

#[test]
fn test_op_mainnet_enr() {
let op_enr = OpStackEnr::new(10, 0);
let bytes = alloy_rlp::encode(op_enr).to_vec();
assert_eq!(Bytes::from(bytes.clone()), bytes!("820A00"));
Expand All @@ -73,7 +85,7 @@ mod tests {
}

#[test]
fn test_encode_decode_base_enr() {
fn test_base_mainnet_enr() {
let base_enr = OpStackEnr::new(8453, 0);
let bytes = alloy_rlp::encode(base_enr).to_vec();
assert_eq!(Bytes::from(bytes.clone()), bytes!("83854200"));
Expand Down

0 comments on commit ab14a33

Please sign in to comment.