Skip to content

Commit

Permalink
chore: add arbitrary for block (#1797)
Browse files Browse the repository at this point in the history
  • Loading branch information
mattsse authored Dec 14, 2024
1 parent 5d5031c commit 28ad3f7
Showing 1 changed file with 23 additions and 0 deletions.
23 changes: 23 additions & 0 deletions crates/consensus/src/block/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,29 @@ impl<T, H> Block<T, H> {
}
}

#[cfg(any(test, feature = "arbitrary"))]
impl<'a, T> arbitrary::Arbitrary<'a> for Block<T>
where
T: arbitrary::Arbitrary<'a>,
{
fn arbitrary(u: &mut arbitrary::Unstructured<'a>) -> arbitrary::Result<Self> {
// first generate a reasonable amount of txs
let transactions = (0..u.int_in_range(0..=100)?)
.map(|_| T::arbitrary(u))
.collect::<arbitrary::Result<Vec<_>>>()?;

// then generate up to 2 ommers
let ommers = (0..u.int_in_range(0..=1)?)
.map(|_| Header::arbitrary(u))
.collect::<arbitrary::Result<Vec<_>>>()?;

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.
Expand Down

0 comments on commit 28ad3f7

Please sign in to comment.