Skip to content

Commit 7fe5ea0

Browse files
authored
chore: add arbitrary for blockbody (#1867)
1 parent 28e9c84 commit 7fe5ea0

File tree

1 file changed

+24
-15
lines changed
  • crates/consensus/src/block

1 file changed

+24
-15
lines changed

crates/consensus/src/block/mod.rs

Lines changed: 24 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -107,25 +107,13 @@ impl<T, H> From<Block<T, H>> for BlockBody<T> {
107107
}
108108

109109
#[cfg(any(test, feature = "arbitrary"))]
110-
impl<'a, T> arbitrary::Arbitrary<'a> for Block<T>
110+
impl<'a, T, H> arbitrary::Arbitrary<'a> for Block<T, H>
111111
where
112112
T: arbitrary::Arbitrary<'a>,
113+
H: arbitrary::Arbitrary<'a>,
113114
{
114115
fn arbitrary(u: &mut arbitrary::Unstructured<'a>) -> arbitrary::Result<Self> {
115-
// first generate a reasonable amount of txs
116-
let transactions = (0..u.int_in_range(0..=100)?)
117-
.map(|_| T::arbitrary(u))
118-
.collect::<arbitrary::Result<Vec<_>>>()?;
119-
120-
// then generate up to 2 ommers
121-
let ommers = (0..u.int_in_range(0..=1)?)
122-
.map(|_| Header::arbitrary(u))
123-
.collect::<arbitrary::Result<Vec<_>>>()?;
124-
125-
Ok(Self {
126-
header: u.arbitrary()?,
127-
body: BlockBody { transactions, ommers, withdrawals: u.arbitrary()? },
128-
})
116+
Ok(Self { header: u.arbitrary()?, body: u.arbitrary()? })
129117
}
130118
}
131119

@@ -253,3 +241,24 @@ mod block_rlp {
253241
}
254242
}
255243
}
244+
245+
#[cfg(any(test, feature = "arbitrary"))]
246+
impl<'a, T> arbitrary::Arbitrary<'a> for BlockBody<T>
247+
where
248+
T: arbitrary::Arbitrary<'a>,
249+
{
250+
fn arbitrary(u: &mut arbitrary::Unstructured<'a>) -> arbitrary::Result<Self> {
251+
// first generate up to 100 txs
252+
// first generate a reasonable amount of txs
253+
let transactions = (0..u.int_in_range(0..=100)?)
254+
.map(|_| T::arbitrary(u))
255+
.collect::<arbitrary::Result<Vec<_>>>()?;
256+
257+
// then generate up to 2 ommers
258+
let ommers = (0..u.int_in_range(0..=1)?)
259+
.map(|_| Header::arbitrary(u))
260+
.collect::<arbitrary::Result<Vec<_>>>()?;
261+
262+
Ok(Self { transactions, ommers, withdrawals: u.arbitrary()? })
263+
}
264+
}

0 commit comments

Comments
 (0)