From ed3e9d522756e4e2d002fbe1f6545cb4b495aa46 Mon Sep 17 00:00:00 2001 From: Matthias Seitz Date: Mon, 30 Dec 2024 22:50:56 +0100 Subject: [PATCH] chore: add arbitrary for blockbody --- crates/consensus/src/block/mod.rs | 39 +++++++++++++++++++------------ 1 file changed, 24 insertions(+), 15 deletions(-) diff --git a/crates/consensus/src/block/mod.rs b/crates/consensus/src/block/mod.rs index 5e54f9e2d2e..995cc7c4861 100644 --- a/crates/consensus/src/block/mod.rs +++ b/crates/consensus/src/block/mod.rs @@ -107,25 +107,13 @@ impl From> for BlockBody { } #[cfg(any(test, feature = "arbitrary"))] -impl<'a, T> arbitrary::Arbitrary<'a> for Block +impl<'a, T, H> arbitrary::Arbitrary<'a> for Block where T: arbitrary::Arbitrary<'a>, + H: arbitrary::Arbitrary<'a>, { fn arbitrary(u: &mut arbitrary::Unstructured<'a>) -> arbitrary::Result { - // first generate a reasonable amount of txs - let transactions = (0..u.int_in_range(0..=100)?) - .map(|_| T::arbitrary(u)) - .collect::>>()?; - - // then generate up to 2 ommers - let ommers = (0..u.int_in_range(0..=1)?) - .map(|_| Header::arbitrary(u)) - .collect::>>()?; - - Ok(Self { - header: u.arbitrary()?, - body: BlockBody { transactions, ommers, withdrawals: u.arbitrary()? }, - }) + Ok(Self { header: u.arbitrary()?, body: u.arbitrary()? }) } } @@ -253,3 +241,24 @@ mod block_rlp { } } } + +#[cfg(any(test, feature = "arbitrary"))] +impl<'a, T> arbitrary::Arbitrary<'a> for BlockBody +where + T: arbitrary::Arbitrary<'a>, +{ + fn arbitrary(u: &mut arbitrary::Unstructured<'a>) -> arbitrary::Result { + // first generate up to 100 txs + // first generate a reasonable amount of txs + let transactions = (0..u.int_in_range(0..=100)?) + .map(|_| T::arbitrary(u)) + .collect::>>()?; + + // then generate up to 2 ommers + let ommers = (0..u.int_in_range(0..=1)?) + .map(|_| Header::arbitrary(u)) + .collect::>>()?; + + Ok(Self { transactions, ommers, withdrawals: u.arbitrary()? }) + } +}