mirror of
https://github.com/sigp/lighthouse.git
synced 2026-04-17 12:58:31 +00:00
add random blobs
This commit is contained in:
@@ -255,6 +255,11 @@ pub trait EthSpec:
|
||||
fn max_blobs_per_block() -> usize {
|
||||
Self::MaxBlobsPerBlock::to_usize()
|
||||
}
|
||||
|
||||
/// Returns the `BYTES_PER_BLOB` constant for this specification.
|
||||
fn bytes_per_blob() -> usize {
|
||||
Self::BytesPerBlob::to_usize()
|
||||
}
|
||||
}
|
||||
|
||||
/// Macro to inherit some type values from another EthSpec.
|
||||
|
||||
@@ -4,8 +4,24 @@ use smallvec::smallvec;
|
||||
|
||||
impl<N: Unsigned + Clone> TestRandom for BitList<N> {
|
||||
fn random_for_test(rng: &mut impl RngCore) -> Self {
|
||||
let mut raw_bytes = smallvec![0; std::cmp::max(1, (N::to_usize() + 7) / 8)];
|
||||
let initial_len = std::cmp::max(1, (N::to_usize() + 7) / 8);
|
||||
let mut raw_bytes = smallvec![0; initial_len];
|
||||
rng.fill_bytes(&mut raw_bytes);
|
||||
|
||||
let highest_set_bit = raw_bytes
|
||||
.iter()
|
||||
.enumerate()
|
||||
.rev()
|
||||
.find(|(_, byte)| **byte > 0)
|
||||
.map(|(i, byte)| i * 8 + 7 - byte.leading_zeros() as usize)
|
||||
.unwrap_or(0);
|
||||
|
||||
let actual_len = highest_set_bit / 8 + 1;
|
||||
|
||||
if actual_len < initial_len {
|
||||
raw_bytes.truncate(actual_len);
|
||||
}
|
||||
|
||||
Self::from_bytes(raw_bytes).expect("we generate a valid BitList")
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user