Skip to content

Commit

Permalink
feat(net): Arbitrary Payload Hash (#75)
Browse files Browse the repository at this point in the history
### Description

Adds an `Arbitrary` impl for the `PayloadHash` type.
  • Loading branch information
refcell authored Sep 2, 2024
1 parent 07d3775 commit 7dd71d8
Show file tree
Hide file tree
Showing 4 changed files with 99 additions and 1 deletion.
85 changes: 85 additions & 0 deletions Cargo.lock

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

1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -135,6 +135,7 @@ alloy = { version = "0.2", features = [
"signers",
"consensus",
] }
alloy-primitives = { version = "0.8", features = ["serde"] }
alloy-rlp = "0.3.4"

# Tokio
Expand Down
3 changes: 2 additions & 1 deletion crates/net/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,8 @@ arbitrary = { workspace = true, optional = true }
[dev-dependencies]
arbtest.workspace = true
arbitrary.workspace = true
alloy = { workspace = true, features = ["arbitrary"] }

[features]
default = []
arbitrary = ["dep:arbitrary"]
arbitrary = ["dep:arbitrary", "alloy/arbitrary"]
11 changes: 11 additions & 0 deletions crates/net/src/types/payload.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ type Transaction = List<u8, 1073741824>;

/// Represents the Keccak256 hash of the block
#[derive(Debug, Clone, Copy, Default, PartialEq, Eq)]
#[cfg_attr(any(test, feature = "arbitrary"), derive(arbitrary::Arbitrary))]
pub struct PayloadHash(B256);

impl From<&[u8]> for PayloadHash {
Expand Down Expand Up @@ -281,4 +282,14 @@ mod tests {
let expected = b256!("44a0e2b1aba1aae1771eddae1dcd2ad18a8cdac8891517153f03253e49d3f206");
assert_eq!(hash.signature_message(chain_id), expected);
}

#[test]
fn test_inner_payload_hash() {
arbtest::arbtest(|u| {
let inner = B256::from(u.arbitrary::<[u8; 32]>()?);
let hash = PayloadHash::from(inner.as_slice());
assert_eq!(hash.0, keccak256(inner.as_slice()));
Ok(())
});
}
}

0 comments on commit 7dd71d8

Please sign in to comment.