test(provider): RequestsProvider of BlockchainProvider2 (#10356)

This commit is contained in:
Alexey Shekhirin
2024-08-21 21:58:49 +01:00
committed by GitHub
parent 302ce7f482
commit 3da119b1ac
30 changed files with 236 additions and 133 deletions

View File

@ -14,7 +14,8 @@ workspace = true
[dependencies]
reth-primitives = { workspace = true, features = ["secp256k1"] }
alloy-eips.workspace = true
alloy-genesis.workspace = true
secp256k1 = { workspace = true, features = ["rand"] }
rand.workspace = true
secp256k1 = { workspace = true, features = ["rand"] }

View File

@ -1,12 +1,16 @@
//! Generators for different data structures like block headers, block bodies and ranges of those.
use alloy_eips::{
eip6110::DepositRequest, eip7002::WithdrawalRequest, eip7251::ConsolidationRequest,
};
pub use rand::Rng;
use rand::{
distributions::uniform::SampleRange, rngs::StdRng, seq::SliceRandom, thread_rng, SeedableRng,
};
use reth_primitives::{
proofs, sign_message, Account, Address, BlockNumber, Bytes, Header, Log, Receipt, SealedBlock,
SealedHeader, StorageEntry, Transaction, TransactionSigned, TxKind, TxLegacy, B256, U256,
proofs, sign_message, Account, Address, BlockNumber, Bytes, Header, Log, Receipt, Request,
Requests, SealedBlock, SealedHeader, StorageEntry, Transaction, TransactionSigned, TxKind,
TxLegacy, B256, U256,
};
use secp256k1::{Keypair, Secp256k1};
use std::{
@ -133,6 +137,7 @@ pub fn random_block<R: Rng>(
parent: Option<B256>,
tx_count: Option<u8>,
ommers_count: Option<u8>,
requests_count: Option<u8>,
) -> SealedBlock {
// Generate transactions
let tx_count = tx_count.unwrap_or_else(|| rng.gen::<u8>());
@ -149,6 +154,10 @@ pub fn random_block<R: Rng>(
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_root = requests.as_ref().map(|requests| proofs::calculate_requests_root(requests));
SealedBlock {
header: Header {
parent_hash: parent.unwrap_or_default(),
@ -158,13 +167,14 @@ pub fn random_block<R: Rng>(
transactions_root,
ommers_hash,
base_fee_per_gas: Some(rng.gen()),
requests_root,
..Default::default()
}
.seal_slow(),
body: transactions,
ommers,
withdrawals: None,
requests: None,
requests: requests.map(Requests),
}
}
@ -179,17 +189,20 @@ pub fn random_block_range<R: Rng>(
block_numbers: RangeInclusive<BlockNumber>,
head: B256,
tx_count: Range<u8>,
requests_count: Option<Range<u8>>,
) -> 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));
blocks.push(random_block(
rng,
idx,
Some(blocks.last().map(|block: &SealedBlock| block.header.hash()).unwrap_or(head)),
Some(tx_count),
None,
requests_count,
));
}
blocks
@ -383,6 +396,31 @@ pub fn random_log<R: Rng>(rng: &mut R, address: Option<Address>, topics_count: O
)
}
/// Generate random request
pub fn random_request<R: Rng>(rng: &mut R) -> Request {
let request_type = rng.gen_range(0..3);
match request_type {
0 => Request::DepositRequest(DepositRequest {
pubkey: rng.gen(),
withdrawal_credentials: rng.gen(),
amount: rng.gen(),
signature: rng.gen(),
index: rng.gen(),
}),
1 => Request::WithdrawalRequest(WithdrawalRequest {
source_address: rng.gen(),
validator_pubkey: rng.gen(),
amount: rng.gen(),
}),
2 => Request::ConsolidationRequest(ConsolidationRequest {
source_address: rng.gen(),
source_pubkey: rng.gen(),
target_pubkey: rng.gen(),
}),
_ => panic!("invalid request type"),
}
}
#[cfg(test)]
mod tests {
use super::*;