@@ -6,9 +6,9 @@ use ethers::prelude::*;
6
6
use ethers:: providers:: { Http , Provider } ;
7
7
use ethers:: types:: Address ;
8
8
use k256:: ecdsa:: SigningKey ;
9
- use rand:: Rng ;
10
9
use reqwest:: StatusCode ;
11
10
11
+ use super :: background_server:: get_acquired_port;
12
12
use super :: constants:: { DEFAULT_ETH_ACCOUNT_PRIVATE_KEY , HOST } ;
13
13
use super :: errors:: TestError ;
14
14
use super :: safe_child:: SafeChild ;
@@ -30,31 +30,29 @@ mod abigen {
30
30
}
31
31
32
32
impl BackgroundAnvil {
33
- /// To avoid TOCTOU or binding issues, we try random ports and try to start
34
- /// Anvil on this port (as Anvil will actually open the socket right after binding).
35
- #[ allow( dead_code) ] // dead_code needed to pass clippy
36
33
pub ( crate ) async fn spawn ( ) -> Result < Self , TestError > {
37
34
BackgroundAnvil :: spawn_with_additional_args ( & [ ] ) . await
38
35
}
39
36
37
+ /// Spawns an instance at random port. Assumes CLI args in `args` don't contain `--port`.
40
38
pub ( crate ) async fn spawn_with_additional_args ( args : & [ & str ] ) -> Result < Self , TestError > {
41
- // Relies on `background_devnet::BackgroundDevnet` starting its check from smaller values
42
- // (1025). Relies on the probability of M simultaneously spawned Anvils occupying
43
- // different ports being fairly big (N*(N-1)*...*(N-M+1) / N**M; N=65_000-20_000+1)
44
- let port = rand:: thread_rng ( ) . gen_range ( 20_000 ..=65_000 ) ;
45
-
46
39
let process = Command :: new ( "anvil" )
47
40
. arg ( "--port" )
48
- . arg ( port . to_string ( ) )
41
+ . arg ( "0" )
49
42
. arg ( "--silent" )
50
43
. args ( args)
51
44
. spawn ( )
52
45
. expect ( "Could not start background Anvil" ) ;
53
- let safe_process = SafeChild { process } ;
46
+ let mut safe_process = SafeChild { process } ;
47
+
48
+ let sleep_time = time:: Duration :: from_millis ( 500 ) ;
49
+ let max_retries = 30 ;
50
+ let port = get_acquired_port ( & mut safe_process, sleep_time, max_retries)
51
+ . await
52
+ . map_err ( |e| TestError :: AnvilNotStartable ( format ! ( "Cannot determine port: {e:?}" ) ) ) ?;
54
53
55
54
let url = format ! ( "http://{HOST}:{port}" ) ;
56
55
let client = reqwest:: Client :: new ( ) ;
57
- let max_retries = 30 ;
58
56
for _ in 0 ..max_retries {
59
57
if let Ok ( anvil_block_rsp) = send_dummy_request ( & client, & url) . await {
60
58
assert_eq ! ( anvil_block_rsp. status( ) , StatusCode :: OK ) ;
@@ -65,34 +63,30 @@ impl BackgroundAnvil {
65
63
return Ok ( Self { process : safe_process, url, provider, provider_signer } ) ;
66
64
}
67
65
68
- tokio:: time:: sleep ( time :: Duration :: from_millis ( 500 ) ) . await ;
66
+ tokio:: time:: sleep ( sleep_time ) . await ;
69
67
}
70
68
71
- Err ( TestError :: AnvilNotStartable )
69
+ Err ( TestError :: AnvilNotStartable ( "Not responsive for too long" . into ( ) ) )
72
70
}
73
71
74
72
pub async fn deploy_l1l2_contract (
75
73
& self ,
76
74
messaging_address : Address ,
77
75
) -> Result < Address , TestError > {
78
- let contract = abigen :: L1L2Example :: deploy (
79
- self . provider_signer . clone ( ) ,
80
- messaging_address ,
81
- )
76
+ // Required by the new version of anvil, as default is no longer accepted.
77
+ // We use here the default value from anvil and hardhat multiplied by 2.
78
+ let gas_price = 2_000_000_000 ;
79
+ let contract = abigen :: L1L2Example :: deploy ( self . provider_signer . clone ( ) , messaging_address )
82
80
. map_err ( |e| {
83
81
TestError :: EthersError ( format ! (
84
82
"Error formatting messaging contract deploy request: {e}"
85
83
) )
86
84
} ) ?
87
- // Required by the new version of anvil, as default is no longer accepted.
88
- // We use here the default value from anvil and hardat multiplied by 2.
89
- . gas_price ( 2000000000 )
85
+ . gas_price ( gas_price)
90
86
. send ( )
91
87
. await
92
88
. map_err ( |e| {
93
- TestError :: EthersError ( format ! (
94
- "Error deploying messaging contract: {e}"
95
- ) )
89
+ TestError :: EthersError ( format ! ( "Error deploying messaging contract: {e}" ) )
96
90
} ) ?;
97
91
98
92
Ok ( contract. address ( ) )
0 commit comments