Skip to content

Commit

Permalink
chore: add arbitrary for blockbody (#1867)
Browse files Browse the repository at this point in the history
  • Loading branch information
mattsse authored Dec 30, 2024
1 parent 28e9c84 commit 7fe5ea0
Showing 1 changed file with 24 additions and 15 deletions.
39 changes: 24 additions & 15 deletions crates/consensus/src/block/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -107,25 +107,13 @@ impl<T, H> From<Block<T, H>> for BlockBody<T> {
}

#[cfg(any(test, feature = "arbitrary"))]
impl<'a, T> arbitrary::Arbitrary<'a> for Block<T>
impl<'a, T, H> arbitrary::Arbitrary<'a> for Block<T, H>
where
T: arbitrary::Arbitrary<'a>,
H: 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()? },
})
Ok(Self { header: u.arbitrary()?, body: u.arbitrary()? })
}
}

Expand Down Expand Up @@ -253,3 +241,24 @@ mod block_rlp {
}
}
}

#[cfg(any(test, feature = "arbitrary"))]
impl<'a, T> arbitrary::Arbitrary<'a> for BlockBody<T>
where
T: arbitrary::Arbitrary<'a>,
{
fn arbitrary(u: &mut arbitrary::Unstructured<'a>) -> arbitrary::Result<Self> {
// 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::<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 { transactions, ommers, withdrawals: u.arbitrary()? })
}
}

0 comments on commit 7fe5ea0

Please sign in to comment.