mirror of
https://github.com/hl-archive-node/nanoreth.git
synced 2025-12-06 10:59:55 +00:00
feat: log & receipt test generators (#3357)
This commit is contained in:
@ -3,9 +3,9 @@ use rand::{
|
||||
distributions::uniform::SampleRange, rngs::StdRng, seq::SliceRandom, thread_rng, SeedableRng,
|
||||
};
|
||||
use reth_primitives::{
|
||||
proofs, sign_message, Account, Address, BlockNumber, Bytes, Header, SealedBlock, SealedHeader,
|
||||
Signature, StorageEntry, Transaction, TransactionKind, TransactionSigned, TxLegacy, H160, H256,
|
||||
U256,
|
||||
proofs, sign_message, Account, Address, BlockNumber, Bytes, Header, Log, Receipt, SealedBlock,
|
||||
SealedHeader, Signature, StorageEntry, Transaction, TransactionKind, TransactionSigned,
|
||||
TxLegacy, H160, H256, U256,
|
||||
};
|
||||
use secp256k1::{KeyPair, Message as SecpMessage, Secp256k1, SecretKey, SECP256K1};
|
||||
use std::{
|
||||
@ -326,6 +326,37 @@ pub fn random_contract_account_range<R: Rng>(
|
||||
accounts
|
||||
}
|
||||
|
||||
/// Generate random receipt for transaction
|
||||
pub fn random_receipt<R: Rng>(
|
||||
rng: &mut R,
|
||||
transaction: &TransactionSigned,
|
||||
logs_count: Option<u8>,
|
||||
) -> Receipt {
|
||||
let success = rng.gen::<bool>();
|
||||
let logs_count = logs_count.unwrap_or_else(|| rng.gen::<u8>());
|
||||
Receipt {
|
||||
tx_type: transaction.tx_type(),
|
||||
success,
|
||||
cumulative_gas_used: rng.gen_range(0..=transaction.gas_limit()),
|
||||
logs: if success {
|
||||
(0..logs_count).map(|_| random_log(rng, None, None)).collect()
|
||||
} else {
|
||||
vec![]
|
||||
},
|
||||
}
|
||||
}
|
||||
|
||||
/// Generate random log
|
||||
pub fn random_log<R: Rng>(rng: &mut R, address: Option<Address>, topics_count: Option<u8>) -> Log {
|
||||
let data_byte_count = rng.gen::<u8>();
|
||||
let topics_count = topics_count.unwrap_or_else(|| rng.gen::<u8>());
|
||||
Log {
|
||||
address: address.unwrap_or_else(|| rng.gen()),
|
||||
topics: (0..topics_count).map(|_| rng.gen()).collect(),
|
||||
data: Bytes::from((0..data_byte_count).map(|_| rng.gen::<u8>()).collect::<Vec<_>>()),
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod test {
|
||||
use std::str::FromStr;
|
||||
|
||||
Reference in New Issue
Block a user