From 28ad3f778d44c743408963b244e04b458591b160 Mon Sep 17 00:00:00 2001 From: Matthias Seitz Date: Sat, 14 Dec 2024 14:41:21 +0100 Subject: [PATCH] chore: add arbitrary for block (#1797) --- crates/consensus/src/block/mod.rs | 23 +++++++++++++++++++++++ 1 file changed, 23 insertions(+) diff --git a/crates/consensus/src/block/mod.rs b/crates/consensus/src/block/mod.rs index 7fe4ccf90a4..898eda39cf6 100644 --- a/crates/consensus/src/block/mod.rs +++ b/crates/consensus/src/block/mod.rs @@ -34,6 +34,29 @@ impl Block { } } +#[cfg(any(test, feature = "arbitrary"))] +impl<'a, T> arbitrary::Arbitrary<'a> for Block +where + T: 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()? }, + }) + } +} + /// A response to `GetBlockBodies`, containing bodies if any bodies were found. /// /// Withdrawals can be optionally included at the end of the RLP encoded message.