@@ -18,7 +18,7 @@ use url::Url;
1818
1919use super :: { load_optional_env_var, PbsConfig , RelayConfig , MUX_PATH_ENV } ;
2020use crate :: {
21- config:: { safe_read_http_response, MUXER_HTTP_TIMEOUT_DEFAULT } ,
21+ config:: { safe_read_http_response, HTTP_TIMEOUT_SECONDS_DEFAULT , HTTP_TIMEOUT_SECONDS_ENV } ,
2222 pbs:: RelayClient ,
2323 types:: Chain ,
2424} ;
@@ -43,13 +43,19 @@ impl PbsMuxes {
4343 chain : Chain ,
4444 default_pbs : & PbsConfig ,
4545 ) -> eyre:: Result < HashMap < BlsPublicKey , RuntimeMuxConfig > > {
46+ let http_timeout = match load_optional_env_var ( HTTP_TIMEOUT_SECONDS_ENV ) {
47+ Some ( timeout_str) => Duration :: from_secs ( timeout_str. parse :: < u64 > ( ) ?) ,
48+ None => Duration :: from_secs ( default_pbs. http_timeout_seconds ) ,
49+ } ;
50+
4651 let mut muxes = self . muxes ;
4752
4853 for mux in muxes. iter_mut ( ) {
4954 ensure ! ( !mux. relays. is_empty( ) , "mux config {} must have at least one relay" , mux. id) ;
5055
5156 if let Some ( loader) = & mux. loader {
52- let extra_keys = loader. load ( & mux. id , chain, default_pbs. rpc_url . clone ( ) ) . await ?;
57+ let extra_keys =
58+ loader. load ( & mux. id , chain, default_pbs. rpc_url . clone ( ) , http_timeout) . await ?;
5359 mux. validator_pubkeys . extend ( extra_keys) ;
5460 }
5561
@@ -147,12 +153,10 @@ pub enum MuxKeysLoader {
147153 File ( PathBuf ) ,
148154 HTTP {
149155 url : String ,
150- timeout : Option < u64 > ,
151156 } ,
152157 Registry {
153158 registry : NORegistry ,
154159 node_operator_id : u64 ,
155- timeout : Option < u64 > ,
156160 } ,
157161}
158162
@@ -170,6 +174,7 @@ impl MuxKeysLoader {
170174 mux_id : & str ,
171175 chain : Chain ,
172176 rpc_url : Option < Url > ,
177+ http_timeout : Duration ,
173178 ) -> eyre:: Result < Vec < BlsPublicKey > > {
174179 match self {
175180 Self :: File ( config_path) => {
@@ -181,21 +186,19 @@ impl MuxKeysLoader {
181186 serde_json:: from_str ( & file) . wrap_err ( "failed to parse mux keys file" )
182187 }
183188
184- Self :: HTTP { url, timeout } => {
189+ Self :: HTTP { url } => {
185190 let url = Url :: parse ( url) . wrap_err ( "failed to parse mux keys URL" ) ?;
186191 if url. scheme ( ) != "https" {
187192 bail ! ( "mux keys URL must use HTTPS" ) ;
188193 }
189- let client = reqwest:: ClientBuilder :: new ( )
190- . timeout ( Duration :: from_secs ( timeout. unwrap_or ( MUXER_HTTP_TIMEOUT_DEFAULT ) ) )
191- . build ( ) ?;
194+ let client = reqwest:: ClientBuilder :: new ( ) . timeout ( http_timeout) . build ( ) ?;
192195 let response = client. get ( url) . send ( ) . await ?;
193196 let pubkeys = safe_read_http_response ( response) . await ?;
194197 serde_json:: from_str ( & pubkeys)
195198 . wrap_err ( "failed to fetch mux keys from HTTP endpoint" )
196199 }
197200
198- Self :: Registry { registry, node_operator_id, timeout } => match registry {
201+ Self :: Registry { registry, node_operator_id } => match registry {
199202 NORegistry :: Lido => {
200203 let Some ( rpc_url) = rpc_url else {
201204 bail ! ( "Lido registry requires RPC URL to be set in the PBS config" ) ;
@@ -204,7 +207,7 @@ impl MuxKeysLoader {
204207 fetch_lido_registry_keys ( rpc_url, chain, U256 :: from ( * node_operator_id) ) . await
205208 }
206209 NORegistry :: SSV => {
207- fetch_ssv_pubkeys ( chain, U256 :: from ( * node_operator_id) , timeout ) . await
210+ fetch_ssv_pubkeys ( chain, U256 :: from ( * node_operator_id) , http_timeout ) . await
208211 }
209212 } ,
210213 }
@@ -305,7 +308,7 @@ async fn fetch_lido_registry_keys(
305308async fn fetch_ssv_pubkeys (
306309 chain : Chain ,
307310 node_operator_id : U256 ,
308- timeout : & Option < u64 > ,
311+ http_timeout : Duration ,
309312) -> eyre:: Result < Vec < BlsPublicKey > > {
310313 const MAX_PER_PAGE : usize = 100 ;
311314
@@ -325,7 +328,7 @@ async fn fetch_ssv_pubkeys(
325328 chain_name, node_operator_id, MAX_PER_PAGE , page
326329 ) ;
327330
328- let response = fetch_ssv_pubkeys_from_url ( & url, timeout ) . await ?;
331+ let response = fetch_ssv_pubkeys_from_url ( & url, http_timeout ) . await ?;
329332 pubkeys. extend ( response. validators . iter ( ) . map ( |v| v. pubkey ) . collect :: < Vec < BlsPublicKey > > ( ) ) ;
330333 page += 1 ;
331334
@@ -346,10 +349,11 @@ async fn fetch_ssv_pubkeys(
346349 Ok ( pubkeys)
347350}
348351
349- async fn fetch_ssv_pubkeys_from_url ( url : & str , timeout : & Option < u64 > ) -> eyre:: Result < SSVResponse > {
350- let client = reqwest:: ClientBuilder :: new ( )
351- . timeout ( Duration :: from_secs ( timeout. unwrap_or ( MUXER_HTTP_TIMEOUT_DEFAULT ) ) )
352- . build ( ) ?;
352+ async fn fetch_ssv_pubkeys_from_url (
353+ url : & str ,
354+ http_timeout : Duration ,
355+ ) -> eyre:: Result < SSVResponse > {
356+ let client = reqwest:: ClientBuilder :: new ( ) . timeout ( http_timeout) . build ( ) ?;
353357 let response = client. get ( url) . send ( ) . await . map_err ( |e| {
354358 if e. is_timeout ( ) {
355359 eyre:: eyre!( "Request to SSV network API timed out: {e}" )
@@ -436,7 +440,9 @@ mod tests {
436440 let port = 30100 ;
437441 let _server_handle = create_mock_server ( port) . await ?;
438442 let url = format ! ( "http://localhost:{port}/ssv" ) ;
439- let response = fetch_ssv_pubkeys_from_url ( & url, & None ) . await ?;
443+ let response =
444+ fetch_ssv_pubkeys_from_url ( & url, Duration :: from_secs ( HTTP_TIMEOUT_SECONDS_DEFAULT ) )
445+ . await ?;
440446
441447 // Make sure the response is correct
442448 // NOTE: requires that ssv_data.json dpesn't change
@@ -472,7 +478,7 @@ mod tests {
472478 env:: remove_var ( CB_TEST_HTTP_DISABLE_CONTENT_LENGTH_ENV ) ;
473479 let _server_handle = create_mock_server ( port) . await ?;
474480 let url = format ! ( "http://localhost:{port}/big_data" ) ;
475- let response = fetch_ssv_pubkeys_from_url ( & url, & Some ( 120 ) ) . await ;
481+ let response = fetch_ssv_pubkeys_from_url ( & url, Duration :: from_secs ( 120 ) ) . await ;
476482
477483 // The response should fail due to content length being too big
478484 assert ! ( response. is_err( ) , "Expected error due to big content length, but got success" ) ;
@@ -498,7 +504,8 @@ mod tests {
498504 let port = 30102 ;
499505 let _server_handle = create_mock_server ( port) . await ?;
500506 let url = format ! ( "http://localhost:{port}/timeout" ) ;
501- let response = fetch_ssv_pubkeys_from_url ( & url, & Some ( TEST_HTTP_TIMEOUT ) ) . await ;
507+ let response =
508+ fetch_ssv_pubkeys_from_url ( & url, Duration :: from_secs ( TEST_HTTP_TIMEOUT ) ) . await ;
502509
503510 // The response should fail due to timeout
504511 assert ! ( response. is_err( ) , "Expected timeout error, but got success" ) ;
@@ -523,7 +530,7 @@ mod tests {
523530 defer ! { env:: remove_var( CB_TEST_HTTP_DISABLE_CONTENT_LENGTH_ENV ) ; }
524531 let _server_handle = create_mock_server ( port) . await ?;
525532 let url = format ! ( "http://localhost:{port}/big_data" ) ;
526- let response = fetch_ssv_pubkeys_from_url ( & url, & Some ( 120 ) ) . await ;
533+ let response = fetch_ssv_pubkeys_from_url ( & url, Duration :: from_secs ( 120 ) ) . await ;
527534
528535 // The response should fail due to timeout
529536 assert ! ( response. is_err( ) , "Expected error due to body size, but got success" ) ;
0 commit comments