@@ -57,7 +57,8 @@ const BS: &str = "https://blockstream.info";
5757const LOCAL : & str = "http://127.0.0.1" ;
5858
5959impl Client {
60- pub fn new ( args : & Arguments ) -> Client {
60+ pub fn new ( args : & Arguments ) -> Result < Client > {
61+ args. is_valid ( ) ?;
6162 let esplora_url = match args. network {
6263 Network :: Liquid => args
6364 . esplora_url
@@ -89,13 +90,19 @@ impl Client {
8990 node_url. unwrap_or ( format ! ( "{LOCAL}:{port}" ) )
9091 } ;
9192 log:: info!( "connecting to {base_url}" ) ;
92- Client {
93- client : reqwest:: Client :: new ( ) ,
93+ let client = reqwest:: Client :: builder ( )
94+ . timeout ( Duration :: from_secs ( args. request_timeout_seconds ) )
95+ . connect_timeout ( Duration :: from_secs ( args. request_timeout_seconds ) ) // Connection establishment timeout
96+ . build ( )
97+ . with_context ( || "Failed to create HTTP client with timeout" ) ?;
98+
99+ Ok ( Client {
100+ client,
94101 use_esplora,
95102 base_url,
96103 esplora_url,
97104 rpc_user_password : args. rpc_user_password . clone ( ) ,
98- }
105+ } )
99106 }
100107
101108 // `curl http://127.0.0.1:7041/rest/blockhashbyheight/0.hex`
@@ -488,9 +495,10 @@ mod test {
488495 let _ = env_logger:: try_init ( ) ;
489496 let mut args = Arguments :: default ( ) ;
490497 args. use_esplora = true ;
498+ args. request_timeout_seconds = 30 ;
491499 for network in [ Network :: Bitcoin , Network :: Liquid , Network :: LiquidTestnet ] {
492500 args. network = network;
493- let client = Client :: new ( & args) ;
501+ let client = Client :: new ( & args) . unwrap ( ) ;
494502 tokio:: time:: sleep ( std:: time:: Duration :: from_secs ( 1 ) ) . await ;
495503 test ( client, network) . await ;
496504 }
@@ -505,7 +513,9 @@ mod test {
505513 args. use_esplora = false ;
506514 args. network = Network :: ElementsRegtest ;
507515 args. node_url = Some ( elementsd. rpc_url ( ) ) ;
508- let client = Client :: new ( & args) ;
516+ args. request_timeout_seconds = 10 ;
517+ args. rpc_user_password = Some ( elementsd. params . cookie_file . to_string_lossy ( ) . to_string ( ) ) ;
518+ let client = Client :: new ( & args) . unwrap ( ) ;
509519 test ( client, args. network ) . await ;
510520 }
511521
@@ -535,7 +545,7 @@ mod test {
535545 let mut args = Arguments :: default ( ) ;
536546 args. use_esplora = false ;
537547 args. network = network;
538- Client :: new ( & args)
548+ Client :: new ( & args) . unwrap ( )
539549 }
540550
541551 #[ tokio:: test]
@@ -548,7 +558,9 @@ mod test {
548558 args. use_esplora = false ;
549559 args. network = Network :: BitcoinRegtest ;
550560 args. node_url = Some ( bitcoind. rpc_url ( ) ) ;
551- let client = Client :: new ( & args) ;
561+ args. request_timeout_seconds = 10 ;
562+ args. rpc_user_password = Some ( bitcoind. params . cookie_file . to_string_lossy ( ) . to_string ( ) ) ;
563+ let client = Client :: new ( & args) . unwrap ( ) ;
552564 test ( client, args. network ) . await ;
553565 }
554566
0 commit comments