22
33use std:: sync:: Arc ;
44
5- use alloy_consensus:: crypto:: secp256k1:: public_key_to_address;
65use alloy_eips:: Encodable2718 ;
7- use alloy_genesis:: GenesisAccount ;
8- use alloy_primitives:: { Address , B256 , Bytes , U256 , keccak256} ;
6+ use alloy_primitives:: { Address , B256 , Bytes , U256 , hex:: FromHex , keccak256} ;
97use base_bundles:: { Bundle , ParsedBundle } ;
108use base_reth_metering:: meter_bundle;
11- use base_reth_test_utils:: create_provider_factory;
9+ use base_reth_test_utils:: { TestAccounts , create_provider_factory, load_genesis } ;
1210use eyre:: Context ;
1311use op_alloy_consensus:: OpTxEnvelope ;
14- use rand:: { SeedableRng , rngs:: StdRng } ;
1512use reth:: { api:: NodeTypesWithDBAdapter , chainspec:: EthChainSpec } ;
1613use reth_db:: { DatabaseEnv , test_utils:: TempDatabase } ;
17- use reth_optimism_chainspec:: { BASE_MAINNET , OpChainSpec , OpChainSpecBuilder } ;
14+ use reth_optimism_chainspec:: { OpChainSpec , OpChainSpecBuilder } ;
1815use reth_optimism_node:: OpNode ;
1916use reth_optimism_primitives:: OpTransactionSigned ;
2017use reth_primitives_traits:: SealedHeader ;
2118use reth_provider:: { HeaderProvider , StateProviderFactory , providers:: BlockchainProvider } ;
22- use reth_testing_utils:: generators:: generate_keys;
2319use reth_transaction_pool:: test_utils:: TransactionBuilder ;
2420
2521type NodeTypes = NodeTypesWithDBAdapter < OpNode , Arc < TempDatabase < DatabaseEnv > > > ;
2622
27- #[ derive( Eq , PartialEq , Debug , Hash , Clone , Copy ) ]
28- enum User {
29- Alice ,
30- Bob ,
31- }
32-
3323#[ derive( Debug , Clone ) ]
3424struct TestHarness {
3525 provider : BlockchainProvider < NodeTypes > ,
3626 header : SealedHeader ,
3727 chain_spec : Arc < OpChainSpec > ,
38- user_to_address : std:: collections:: HashMap < User , Address > ,
39- user_to_private_key : std:: collections:: HashMap < User , B256 > ,
28+ accounts : TestAccounts ,
4029}
4130
4231impl TestHarness {
43- fn address ( & self , u : User ) -> Address {
44- self . user_to_address [ & u ]
32+ fn alice_signer ( & self ) -> B256 {
33+ B256 :: from_hex ( self . accounts . alice . private_key ) . expect ( "valid hex-encoded key" )
4534 }
4635
47- fn signer ( & self , u : User ) -> B256 {
48- self . user_to_private_key [ & u ]
36+ fn bob_signer ( & self ) -> B256 {
37+ B256 :: from_hex ( self . accounts . bob . private_key ) . expect ( "valid hex-encoded key" )
4938 }
5039}
5140
52- fn create_chain_spec (
53- seed : u64 ,
54- ) -> (
55- Arc < OpChainSpec > ,
56- std:: collections:: HashMap < User , Address > ,
57- std:: collections:: HashMap < User , B256 > ,
58- ) {
59- let keys = generate_keys ( & mut StdRng :: seed_from_u64 ( seed) , 2 ) ;
60-
61- let mut addresses = std:: collections:: HashMap :: new ( ) ;
62- let mut private_keys = std:: collections:: HashMap :: new ( ) ;
63-
64- let alice_key = keys[ 0 ] ;
65- let alice_address = public_key_to_address ( alice_key. public_key ( ) ) ;
66- let alice_secret = B256 :: from ( alice_key. secret_bytes ( ) ) ;
67- addresses. insert ( User :: Alice , alice_address) ;
68- private_keys. insert ( User :: Alice , alice_secret) ;
69-
70- let bob_key = keys[ 1 ] ;
71- let bob_address = public_key_to_address ( bob_key. public_key ( ) ) ;
72- let bob_secret = B256 :: from ( bob_key. secret_bytes ( ) ) ;
73- addresses. insert ( User :: Bob , bob_address) ;
74- private_keys. insert ( User :: Bob , bob_secret) ;
75-
76- let genesis = BASE_MAINNET
77- . genesis
78- . clone ( )
79- . extend_accounts ( vec ! [
80- ( alice_address, GenesisAccount :: default ( ) . with_balance( U256 :: from( 1_000_000_000_u64 ) ) ) ,
81- ( bob_address, GenesisAccount :: default ( ) . with_balance( U256 :: from( 1_000_000_000_u64 ) ) ) ,
82- ] )
83- . with_gas_limit ( 100_000_000 ) ;
84-
85- let spec =
41+ fn setup_harness ( ) -> eyre:: Result < TestHarness > {
42+ let accounts = TestAccounts :: new ( ) ;
43+ let genesis = load_genesis ( ) ;
44+ let chain_spec =
8645 Arc :: new ( OpChainSpecBuilder :: base_mainnet ( ) . genesis ( genesis) . isthmus_activated ( ) . build ( ) ) ;
8746
88- ( spec, addresses, private_keys)
89- }
90-
91- fn setup_harness ( ) -> eyre:: Result < TestHarness > {
92- let ( chain_spec, user_to_address, user_to_private_key) = create_chain_spec ( 1337 ) ;
9347 let factory = create_provider_factory :: < OpNode > ( chain_spec. clone ( ) ) ;
9448
9549 reth_db_common:: init:: init_genesis ( & factory) . context ( "initializing genesis state" ) ?;
@@ -100,7 +54,7 @@ fn setup_harness() -> eyre::Result<TestHarness> {
10054 . context ( "fetching genesis header" ) ?
10155 . expect ( "genesis header exists" ) ;
10256
103- Ok ( TestHarness { provider, header, chain_spec, user_to_address , user_to_private_key } )
57+ Ok ( TestHarness { provider, header, chain_spec, accounts } )
10458}
10559
10660fn envelope_from_signed ( tx : & OpTransactionSigned ) -> eyre:: Result < OpTxEnvelope > {
@@ -155,7 +109,7 @@ fn meter_bundle_single_transaction() -> eyre::Result<()> {
155109
156110 let to = Address :: random ( ) ;
157111 let signed_tx = TransactionBuilder :: default ( )
158- . signer ( harness. signer ( User :: Alice ) )
112+ . signer ( harness. alice_signer ( ) )
159113 . chain_id ( harness. chain_spec . chain_id ( ) )
160114 . nonce ( 0 )
161115 . to ( to)
@@ -185,7 +139,7 @@ fn meter_bundle_single_transaction() -> eyre::Result<()> {
185139 let result = & results[ 0 ] ;
186140 assert ! ( total_execution_time > 0 ) ;
187141
188- assert_eq ! ( result. from_address, harness. address( User :: Alice ) ) ;
142+ assert_eq ! ( result. from_address, harness. accounts . alice . address) ;
189143 assert_eq ! ( result. to_address, Some ( to) ) ;
190144 assert_eq ! ( result. tx_hash, tx_hash) ;
191145 assert_eq ! ( result. gas_price, U256 :: from( 10 ) ) ;
@@ -213,7 +167,7 @@ fn meter_bundle_multiple_transactions() -> eyre::Result<()> {
213167
214168 // Create first transaction
215169 let signed_tx_1 = TransactionBuilder :: default ( )
216- . signer ( harness. signer ( User :: Alice ) )
170+ . signer ( harness. alice_signer ( ) )
217171 . chain_id ( harness. chain_spec . chain_id ( ) )
218172 . nonce ( 0 )
219173 . to ( to_1)
@@ -229,7 +183,7 @@ fn meter_bundle_multiple_transactions() -> eyre::Result<()> {
229183
230184 // Create second transaction
231185 let signed_tx_2 = TransactionBuilder :: default ( )
232- . signer ( harness. signer ( User :: Bob ) )
186+ . signer ( harness. bob_signer ( ) )
233187 . chain_id ( harness. chain_spec . chain_id ( ) )
234188 . nonce ( 0 )
235189 . to ( to_2)
@@ -263,7 +217,7 @@ fn meter_bundle_multiple_transactions() -> eyre::Result<()> {
263217
264218 // Check first transaction
265219 let result_1 = & results[ 0 ] ;
266- assert_eq ! ( result_1. from_address, harness. address( User :: Alice ) ) ;
220+ assert_eq ! ( result_1. from_address, harness. accounts . alice . address) ;
267221 assert_eq ! ( result_1. to_address, Some ( to_1) ) ;
268222 assert_eq ! ( result_1. tx_hash, tx_hash_1) ;
269223 assert_eq ! ( result_1. gas_price, U256 :: from( 10 ) ) ;
@@ -272,7 +226,7 @@ fn meter_bundle_multiple_transactions() -> eyre::Result<()> {
272226
273227 // Check second transaction
274228 let result_2 = & results[ 1 ] ;
275- assert_eq ! ( result_2. from_address, harness. address( User :: Bob ) ) ;
229+ assert_eq ! ( result_2. from_address, harness. accounts . bob . address) ;
276230 assert_eq ! ( result_2. to_address, Some ( to_2) ) ;
277231 assert_eq ! ( result_2. tx_hash, tx_hash_2) ;
278232 assert_eq ! ( result_2. gas_price, U256 :: from( 15 ) ) ;
0 commit comments