reth_transaction_pool/test_utils/
gen.rs

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
use crate::EthPooledTransaction;
use alloy_consensus::{TxEip1559, TxEip4844, TxLegacy};
use alloy_eips::{eip1559::MIN_PROTOCOL_BASE_FEE, eip2718::Encodable2718, eip2930::AccessList};
use alloy_primitives::{Address, Bytes, TxKind, B256, U256};
use rand::Rng;
use reth_chainspec::MAINNET;
use reth_primitives::{sign_message, Transaction, TransactionSigned};

/// A generator for transactions for testing purposes.
#[derive(Debug)]
pub struct TransactionGenerator<R> {
    /// 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> {
    /// Initializes the generator with 10 random signers
    pub fn new(rng: R) -> Self {
        Self::with_num_signers(rng, 10)
    }

    /// Generates random random signers
    pub fn with_num_signers(rng: R, num_signers: usize) -> Self {
        Self {
            rng,
            signer_keys: (0..num_signers).map(|_| B256::random()).collect(),
            base_fee: MIN_PROTOCOL_BASE_FEE as u128,
            gas_limit: 300_000,
        }
    }

    /// Adds a new signer to the set
    pub fn push_signer(&mut self, signer: B256) -> &mut Self {
        self.signer_keys.push(signer);
        self
    }

    /// Sets the default gas limit for all generated transactions
    pub fn set_gas_limit(&mut self, gas_limit: u64) -> &mut Self {
        self.gas_limit = gas_limit;
        self
    }

    /// Sets the default gas limit for all generated transactions
    pub const fn with_gas_limit(mut self, gas_limit: u64) -> Self {
        self.gas_limit = gas_limit;
        self
    }

    /// Sets the base fee for the generated transactions
    pub fn set_base_fee(&mut self, base_fee: u64) -> &mut Self {
        self.base_fee = base_fee as u128;
        self
    }

    /// Sets the base fee for the generated transactions
    pub const fn with_base_fee(mut self, base_fee: u64) -> Self {
        self.base_fee = base_fee as u128;
        self
    }

    /// Adds the given signers to the set.
    pub fn extend_signers(&mut self, signers: impl IntoIterator<Item = B256>) -> &mut Self {
        self.signer_keys.extend(signers);
        self
    }

    /// Returns a random signer from the set
    fn rng_signer(&mut self) -> B256 {
        let idx = self.rng.gen_range(0..self.signer_keys.len());
        self.signer_keys[idx]
    }

    /// Creates a new transaction with a random signer
    pub fn transaction(&mut self) -> TransactionBuilder {
        TransactionBuilder::default()
            .signer(self.rng_signer())
            .max_fee_per_gas(self.base_fee)
            .max_priority_fee_per_gas(self.base_fee)
            .gas_limit(self.gas_limit)
    }

    /// Creates a new transaction with a random signer
    pub fn gen_eip1559(&mut self) -> TransactionSigned {
        self.transaction().into_eip1559()
    }

    /// Creates a new transaction with a random signer
    pub fn gen_eip4844(&mut self) -> TransactionSigned {
        self.transaction().into_eip4844()
    }

    /// Generates and returns a pooled EIP-1559 transaction with a random signer.
    pub fn gen_eip1559_pooled(&mut self) -> EthPooledTransaction {
        self.gen_eip1559().into_ecrecovered().unwrap().try_into().unwrap()
    }

    /// Generates and returns a pooled EIP-4844 transaction with a random signer.
    pub fn gen_eip4844_pooled(&mut self) -> EthPooledTransaction {
        let tx = self.gen_eip4844().into_ecrecovered().unwrap();
        let encoded_length = tx.encode_2718_len();
        EthPooledTransaction::new(tx, encoded_length)
    }
}

/// A Builder type to configure and create a transaction.
#[derive(Debug)]
pub struct TransactionBuilder {
    /// 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: TxKind,
    /// The value to be transferred in the transaction.
    pub value: U256,
    /// 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 {
        Self::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(),
            self.signer,
        )
    }

    /// Converts the transaction builder into a transaction format using EIP-1559.
    pub fn into_eip1559(self) -> TransactionSigned {
        Self::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(),
            self.signer,
        )
    }
    /// Converts the transaction builder into a transaction format using EIP-4844.
    pub fn into_eip4844(self) -> TransactionSigned {
        Self::signed(
            TxEip4844 {
                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: match self.to {
                    TxKind::Call(to) => to,
                    TxKind::Create => Address::default(),
                },
                value: self.value,
                access_list: self.access_list,
                input: self.input,
                blob_versioned_hashes: Default::default(),
                max_fee_per_blob_gas: Default::default(),
            }
            .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::new_unhashed(transaction, signature)
    }

    /// Sets the signer for the transaction builder.
    pub const fn signer(mut self, signer: B256) -> Self {
        self.signer = signer;
        self
    }

    /// Sets the gas limit for the transaction builder.
    pub const fn gas_limit(mut self, gas_limit: u64) -> Self {
        self.gas_limit = gas_limit;
        self
    }

    /// Sets the nonce for the transaction builder.
    pub const fn nonce(mut self, nonce: u64) -> Self {
        self.nonce = nonce;
        self
    }

    /// Increments the nonce value of the transaction builder by 1.
    pub const fn inc_nonce(mut self) -> Self {
        self.nonce += 1;
        self
    }

    /// Decrements the nonce value of the transaction builder by 1, avoiding underflow.
    pub const 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 const 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 const 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 const fn to(mut self, to: Address) -> Self {
        self.to = TxKind::Call(to);
        self
    }

    /// Sets the value to be transferred in the transaction.
    pub fn value(mut self, value: u128) -> Self {
        self.value = U256::from(value);
        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 const 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 = to.into();
        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 = U256::from(value);
        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
    }
}

impl Default for TransactionBuilder {
    fn default() -> Self {
        Self {
            signer: B256::random(),
            chain_id: MAINNET.chain.id(),
            nonce: 0,
            gas_limit: 0,
            max_fee_per_gas: 0,
            max_priority_fee_per_gas: 0,
            to: Default::default(),
            value: Default::default(),
            access_list: Default::default(),
            input: Default::default(),
        }
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use rand::thread_rng;

    #[test]
    fn test_generate_transaction() {
        let rng = thread_rng();
        let mut gen = TransactionGenerator::new(rng);
        let _tx = gen.transaction().into_legacy();
        let _tx = gen.transaction().into_eip1559();
    }
}