improve documentation and refactor transaction-pool gen test utils (#6142)

This commit is contained in:
Thomas Coratger
2024-01-21 18:07:05 +01:00
committed by GitHub
parent 90e1c432de
commit 734109e968

View File

@ -1,5 +1,3 @@
#![allow(missing_docs)]
use crate::EthPooledTransaction;
use rand::Rng;
use reth_primitives::{
@ -8,12 +6,16 @@ use reth_primitives::{
TxValue, B256, MAINNET,
};
/// A generator for transactions for testing purposes
/// A generator for transactions for testing purposes.
pub struct TransactionGenerator<R> {
rng: R,
signer_keys: Vec<B256>,
base_fee: u128,
gas_limit: u64,
/// The random number generator used for generating keys and selecting signers.
pub rng: R,
/// The set of signer keys available for transaction generation.
pub signer_keys: Vec<B256>,
/// The base fee for transactions.
pub base_fee: u128,
/// The gas limit for transactions.
pub gas_limit: u64,
}
impl<R: Rng> TransactionGenerator<R> {
@ -24,11 +26,12 @@ impl<R: Rng> TransactionGenerator<R> {
/// Generates random random signers
pub fn with_num_signers(rng: R, num_signers: usize) -> Self {
let mut signer_keys = Vec::with_capacity(num_signers);
for _ in 0..num_signers {
signer_keys.push(B256::random());
Self {
rng,
signer_keys: (0..num_signers).map(|_| B256::random()).collect(),
base_fee: MIN_PROTOCOL_BASE_FEE as u128,
gas_limit: 300_000,
}
Self { rng, signer_keys, base_fee: MIN_PROTOCOL_BASE_FEE as u128, gas_limit: 300_000 }
}
/// Adds a new signer to the set
@ -87,191 +90,210 @@ impl<R: Rng> TransactionGenerator<R> {
self.transaction().into_eip1559()
}
/// Generates and returns a pooled EIP-1559 transaction with a random signer.
pub fn gen_eip1559_pooled(&mut self) -> EthPooledTransaction {
let tx = self.gen_eip1559().into_ecrecovered().unwrap();
EthPooledTransaction::from_recovered_transaction(tx)
EthPooledTransaction::from_recovered_transaction(
self.gen_eip1559().into_ecrecovered().unwrap(),
)
}
}
/// A Builder type to configure and create a transaction.
pub struct TransactionBuilder {
signer: B256,
chain_id: u64,
nonce: u64,
gas_limit: u64,
max_fee_per_gas: u128,
max_priority_fee_per_gas: u128,
to: TransactionKind,
value: TxValue,
access_list: AccessList,
input: Bytes,
/// The signer used to sign the transaction.
pub signer: B256,
/// The chain ID on which the transaction will be executed.
pub chain_id: u64,
/// The nonce value for the transaction to prevent replay attacks.
pub nonce: u64,
/// The maximum amount of gas units that the transaction can consume.
pub gas_limit: u64,
/// The maximum fee per gas unit that the sender is willing to pay.
pub max_fee_per_gas: u128,
/// The maximum priority fee per gas unit that the sender is willing to pay for faster
/// processing.
pub max_priority_fee_per_gas: u128,
/// The recipient or contract address of the transaction.
pub to: TransactionKind,
/// The value to be transferred in the transaction.
pub value: TxValue,
/// The list of addresses and storage keys that the transaction can access.
pub access_list: AccessList,
/// The input data for the transaction, typically containing function parameters for contract
/// calls.
pub input: Bytes,
}
impl TransactionBuilder {
/// Converts the transaction builder into a legacy transaction format.
pub fn into_legacy(self) -> TransactionSigned {
let Self {
signer,
chain_id,
nonce,
gas_limit,
max_fee_per_gas,
max_priority_fee_per_gas: _,
to,
value,
access_list: _,
input,
} = self;
let tx: Transaction = TxLegacy {
chain_id: Some(chain_id),
nonce,
gas_limit,
gas_price: max_fee_per_gas,
to,
value,
input,
TransactionBuilder::signed(
TxLegacy {
chain_id: Some(self.chain_id),
nonce: self.nonce,
gas_limit: self.gas_limit,
gas_price: self.max_fee_per_gas,
to: self.to,
value: self.value,
input: self.input,
}
.into();
TransactionBuilder::signed(tx, signer)
.into(),
self.signer,
)
}
/// Converts the transaction builder into a transaction format using EIP-1559.
pub fn into_eip1559(self) -> TransactionSigned {
let Self {
signer,
chain_id,
nonce,
gas_limit,
max_fee_per_gas,
max_priority_fee_per_gas,
to,
value,
access_list,
input,
} = self;
let tx: Transaction = TxEip1559 {
chain_id,
nonce,
gas_limit,
max_fee_per_gas,
max_priority_fee_per_gas,
to,
value,
access_list,
input,
TransactionBuilder::signed(
TxEip1559 {
chain_id: self.chain_id,
nonce: self.nonce,
gas_limit: self.gas_limit,
max_fee_per_gas: self.max_fee_per_gas,
max_priority_fee_per_gas: self.max_priority_fee_per_gas,
to: self.to,
value: self.value,
access_list: self.access_list,
input: self.input,
}
.into();
TransactionBuilder::signed(tx, signer)
.into(),
self.signer,
)
}
/// Signs the provided transaction using the specified signer and returns a signed transaction.
fn signed(transaction: Transaction, signer: B256) -> TransactionSigned {
let signature = sign_message(signer, transaction.signature_hash()).unwrap();
TransactionSigned::from_transaction_and_signature(transaction, signature)
}
/// Sets the signer for the transaction builder.
pub fn signer(mut self, signer: B256) -> Self {
self.signer = signer;
self
}
/// Sets the gas limit for the transaction builder.
pub fn gas_limit(mut self, gas_limit: u64) -> Self {
self.gas_limit = gas_limit;
self
}
/// Sets the nonce for the transaction builder.
pub fn nonce(mut self, nonce: u64) -> Self {
self.nonce = nonce;
self
}
/// Increments the nonce value of the transaction builder by 1.
pub fn inc_nonce(mut self) -> Self {
self.nonce += 1;
self
}
/// Decrements the nonce value of the transaction builder by 1, avoiding underflow.
pub fn decr_nonce(mut self) -> Self {
self.nonce = self.nonce.saturating_sub(1);
self
}
/// Sets the maximum fee per gas for the transaction builder.
pub fn max_fee_per_gas(mut self, max_fee_per_gas: u128) -> Self {
self.max_fee_per_gas = max_fee_per_gas;
self
}
/// Sets the maximum priority fee per gas for the transaction builder.
pub fn max_priority_fee_per_gas(mut self, max_priority_fee_per_gas: u128) -> Self {
self.max_priority_fee_per_gas = max_priority_fee_per_gas;
self
}
/// Sets the recipient or contract address for the transaction builder.
pub fn to(mut self, to: Address) -> Self {
self.to = TransactionKind::Call(to);
self
}
/// Sets the value to be transferred in the transaction.
pub fn value(mut self, value: u128) -> Self {
self.value = value.into();
self
}
/// Sets the access list for the transaction builder.
pub fn access_list(mut self, access_list: AccessList) -> Self {
self.access_list = access_list;
self
}
/// Sets the transaction input data.
pub fn input(mut self, input: impl Into<Bytes>) -> Self {
self.input = input.into();
self
}
/// Sets the chain ID for the transaction.
pub fn chain_id(mut self, chain_id: u64) -> Self {
self.chain_id = chain_id;
self
}
/// Sets the chain ID for the transaction, mutable reference version.
pub fn set_chain_id(&mut self, chain_id: u64) -> &mut Self {
self.chain_id = chain_id;
self
}
/// Sets the nonce for the transaction, mutable reference version.
pub fn set_nonce(&mut self, nonce: u64) -> &mut Self {
self.nonce = nonce;
self
}
/// Sets the gas limit for the transaction, mutable reference version.
pub fn set_gas_limit(&mut self, gas_limit: u64) -> &mut Self {
self.gas_limit = gas_limit;
self
}
/// Sets the maximum fee per gas for the transaction, mutable reference version.
pub fn set_max_fee_per_gas(&mut self, max_fee_per_gas: u128) -> &mut Self {
self.max_fee_per_gas = max_fee_per_gas;
self
}
/// Sets the maximum priority fee per gas for the transaction, mutable reference version.
pub fn set_max_priority_fee_per_gas(&mut self, max_priority_fee_per_gas: u128) -> &mut Self {
self.max_priority_fee_per_gas = max_priority_fee_per_gas;
self
}
/// Sets the recipient or contract address for the transaction, mutable reference version.
pub fn set_to(&mut self, to: Address) -> &mut Self {
self.to = TransactionKind::Call(to);
self
}
/// Sets the value to be transferred in the transaction, mutable reference version.
pub fn set_value(&mut self, value: u128) -> &mut Self {
self.value = value.into();
self
}
/// Sets the access list for the transaction, mutable reference version.
pub fn set_access_list(&mut self, access_list: AccessList) -> &mut Self {
self.access_list = access_list;
self
}
/// Sets the signer for the transaction, mutable reference version.
pub fn set_signer(&mut self, signer: B256) -> &mut Self {
self.signer = signer;
self
}
/// Sets the transaction input data, mutable reference version.
pub fn set_input(&mut self, input: impl Into<Bytes>) -> &mut Self {
self.input = input.into();
self