mirror of
https://github.com/hl-archive-node/nanoreth.git
synced 2025-12-06 19:09:54 +00:00
refactor(provider): random_block and random_block_range functions (#10563)
This commit is contained in:
@ -20,6 +20,36 @@ use std::{
|
||||
ops::{Range, RangeInclusive},
|
||||
};
|
||||
|
||||
/// Used to pass arguments for random block generation function in tests
|
||||
#[derive(Debug, Default)]
|
||||
pub struct BlockParams {
|
||||
/// The parent hash of the block.
|
||||
pub parent: Option<B256>,
|
||||
/// The number of transactions in the block.
|
||||
pub tx_count: Option<u8>,
|
||||
/// The number of ommers (uncles) in the block.
|
||||
pub ommers_count: Option<u8>,
|
||||
/// The number of requests in the block.
|
||||
pub requests_count: Option<u8>,
|
||||
/// The number of withdrawals in the block.
|
||||
pub withdrawals_count: Option<u8>,
|
||||
}
|
||||
|
||||
/// Used to pass arguments for random block generation function in tests
|
||||
#[derive(Debug, Default)]
|
||||
pub struct BlockRangeParams {
|
||||
/// The parent hash of the block.
|
||||
pub parent: Option<B256>,
|
||||
/// The range of transactions in the block.
|
||||
/// If set, a random count between the range will be used.
|
||||
/// If not set, a random number of transactions will be used.
|
||||
pub tx_count: Range<u8>,
|
||||
/// The number of requests in the block.
|
||||
pub requests_count: Option<Range<u8>>,
|
||||
/// The number of withdrawals in the block.
|
||||
pub withdrawals_count: Option<Range<u8>>,
|
||||
}
|
||||
|
||||
/// Returns a random number generator that can be seeded using the `SEED` environment variable.
|
||||
///
|
||||
/// If `SEED` is not set, a random seed is used.
|
||||
@ -131,35 +161,29 @@ pub fn generate_keys<R: Rng>(rng: &mut R, count: usize) -> Vec<Keypair> {
|
||||
/// transactions in the block.
|
||||
///
|
||||
/// The ommer headers are not assumed to be valid.
|
||||
pub fn random_block<R: Rng>(
|
||||
rng: &mut R,
|
||||
number: u64,
|
||||
parent: Option<B256>,
|
||||
tx_count: Option<u8>,
|
||||
ommers_count: Option<u8>,
|
||||
requests_count: Option<u8>,
|
||||
withdrawals_count: Option<u8>,
|
||||
) -> SealedBlock {
|
||||
pub fn random_block<R: Rng>(rng: &mut R, number: u64, block_params: BlockParams) -> SealedBlock {
|
||||
// Generate transactions
|
||||
let tx_count = tx_count.unwrap_or_else(|| rng.gen::<u8>());
|
||||
let tx_count = block_params.tx_count.unwrap_or_else(|| rng.gen::<u8>());
|
||||
let transactions: Vec<TransactionSigned> =
|
||||
(0..tx_count).map(|_| random_signed_tx(rng)).collect();
|
||||
let total_gas = transactions.iter().fold(0, |sum, tx| sum + tx.transaction.gas_limit());
|
||||
|
||||
// Generate ommers
|
||||
let ommers_count = ommers_count.unwrap_or_else(|| rng.gen_range(0..2));
|
||||
let ommers =
|
||||
(0..ommers_count).map(|_| random_header(rng, number, parent).unseal()).collect::<Vec<_>>();
|
||||
let ommers_count = block_params.ommers_count.unwrap_or_else(|| rng.gen_range(0..2));
|
||||
let ommers = (0..ommers_count)
|
||||
.map(|_| random_header(rng, number, block_params.parent).unseal())
|
||||
.collect::<Vec<_>>();
|
||||
|
||||
// Calculate roots
|
||||
let transactions_root = proofs::calculate_transaction_root(&transactions);
|
||||
let ommers_hash = proofs::calculate_ommers_root(&ommers);
|
||||
|
||||
let requests =
|
||||
requests_count.map(|count| (0..count).map(|_| random_request(rng)).collect::<Vec<_>>());
|
||||
let requests = block_params
|
||||
.requests_count
|
||||
.map(|count| (0..count).map(|_| random_request(rng)).collect::<Vec<_>>());
|
||||
let requests_root = requests.as_ref().map(|requests| proofs::calculate_requests_root(requests));
|
||||
|
||||
let withdrawals = withdrawals_count.map(|count| {
|
||||
let withdrawals = block_params.withdrawals_count.map(|count| {
|
||||
(0..count)
|
||||
.map(|i| Withdrawal {
|
||||
amount: rng.gen(),
|
||||
@ -173,7 +197,7 @@ pub fn random_block<R: Rng>(
|
||||
|
||||
SealedBlock {
|
||||
header: Header {
|
||||
parent_hash: parent.unwrap_or_default(),
|
||||
parent_hash: block_params.parent.unwrap_or_default(),
|
||||
number,
|
||||
gas_used: total_gas,
|
||||
gas_limit: total_gas,
|
||||
@ -201,25 +225,29 @@ pub fn random_block<R: Rng>(
|
||||
pub fn random_block_range<R: Rng>(
|
||||
rng: &mut R,
|
||||
block_numbers: RangeInclusive<BlockNumber>,
|
||||
head: B256,
|
||||
tx_count: Range<u8>,
|
||||
requests_count: Option<Range<u8>>,
|
||||
withdrawals_count: Option<Range<u8>>,
|
||||
block_range_params: BlockRangeParams,
|
||||
) -> Vec<SealedBlock> {
|
||||
let mut blocks =
|
||||
Vec::with_capacity(block_numbers.end().saturating_sub(*block_numbers.start()) as usize);
|
||||
for idx in block_numbers {
|
||||
let tx_count = tx_count.clone().sample_single(rng);
|
||||
let requests_count = requests_count.clone().map(|r| r.sample_single(rng));
|
||||
let withdrawals_count = withdrawals_count.clone().map(|r| r.sample_single(rng));
|
||||
let tx_count = block_range_params.tx_count.clone().sample_single(rng);
|
||||
let requests_count =
|
||||
block_range_params.requests_count.clone().map(|r| r.sample_single(rng));
|
||||
let withdrawals_count =
|
||||
block_range_params.withdrawals_count.clone().map(|r| r.sample_single(rng));
|
||||
let parent = block_range_params.parent.unwrap_or_default();
|
||||
blocks.push(random_block(
|
||||
rng,
|
||||
idx,
|
||||
Some(blocks.last().map(|block: &SealedBlock| block.header.hash()).unwrap_or(head)),
|
||||
Some(tx_count),
|
||||
None,
|
||||
requests_count,
|
||||
withdrawals_count,
|
||||
BlockParams {
|
||||
parent: Some(
|
||||
blocks.last().map(|block: &SealedBlock| block.header.hash()).unwrap_or(parent),
|
||||
),
|
||||
tx_count: Some(tx_count),
|
||||
ommers_count: None,
|
||||
requests_count,
|
||||
withdrawals_count,
|
||||
},
|
||||
));
|
||||
}
|
||||
blocks
|
||||
|
||||
Reference in New Issue
Block a user