Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

chore: add arbitrary for blockbody #1867

Merged
merged 1 commit into from
Dec 30, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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()? })
}
}
Loading