1+ use std:: net:: { IpAddr , Ipv4Addr , SocketAddr } ;
12use std:: path:: { Path , PathBuf } ;
23use std:: process:: Stdio ;
34use std:: time:: { Duration , Instant } ;
@@ -7,6 +8,9 @@ use katana_primitives::{ContractAddress, Felt};
78use tokio:: process:: { Child , Command } ;
89use tokio:: time:: sleep;
910use tracing:: { debug, info, warn} ;
11+ use url:: Url ;
12+
13+ use super :: client:: VrfClient ;
1014
1115const LOG_TARGET : & str = "katana::cartridge::vrf::sidecar" ;
1216
@@ -78,11 +82,15 @@ impl VrfService {
7882 . kill_on_drop ( true ) ;
7983
8084 let process = command. spawn ( ) . map_err ( Error :: Spawn ) ?;
85+ let addr = SocketAddr :: new ( IpAddr :: V4 ( Ipv4Addr :: LOCALHOST ) , VRF_SERVER_PORT ) ;
86+
87+ let url = Url :: parse ( & format ! ( "http://{addr}" ) ) . expect ( "valid url" ) ;
88+ let client = VrfClient :: new ( url) ;
89+ wait_for_http_ok ( & client, "vrf info" , SIDECAR_TIMEOUT ) . await ?;
8190
82- let url = format ! ( "http://127.0.0.1:{VRF_SERVER_PORT}/info" , ) ;
83- wait_for_http_ok ( & url, "vrf info" , SIDECAR_TIMEOUT ) . await ?;
91+ info ! ( %addr, "VRF service started." ) ;
8492
85- Ok ( VrfServiceProcess { process, inner : self } )
93+ Ok ( VrfServiceProcess { process, addr , inner : self } )
8694 }
8795}
8896
@@ -91,9 +99,15 @@ impl VrfService {
9199pub struct VrfServiceProcess {
92100 process : Child ,
93101 inner : VrfService ,
102+ addr : SocketAddr ,
94103}
95104
96105impl VrfServiceProcess {
106+ /// Get the address of the VRF service.
107+ pub fn addr ( & self ) -> & SocketAddr {
108+ & self . addr
109+ }
110+
97111 pub fn process ( & mut self ) -> & mut Child {
98112 & mut self . process
99113 }
@@ -128,20 +142,16 @@ pub fn resolve_executable(path: &Path) -> Result<PathBuf> {
128142 Err ( Error :: BinaryNotInPath ( path. to_path_buf ( ) ) )
129143}
130144
131- /// Wait for an HTTP endpoint to return a successful response.
132- pub async fn wait_for_http_ok ( url : & str , name : & str , timeout : Duration ) -> Result < ( ) > {
133- let client = reqwest:: Client :: new ( ) ;
145+ /// Wait for the VRF sidecar to become ready by polling its `/info` endpoint.
146+ pub async fn wait_for_http_ok ( client : & VrfClient , name : & str , timeout : Duration ) -> Result < ( ) > {
134147 let start = Instant :: now ( ) ;
135148
136149 loop {
137- match client. get ( url ) . send ( ) . await {
138- Ok ( resp ) if resp . status ( ) . is_success ( ) => {
150+ match client. info ( ) . await {
151+ Ok ( _ ) => {
139152 info ! ( target: LOG_TARGET , %name, "sidecar ready" ) ;
140153 return Ok ( ( ) ) ;
141154 }
142- Ok ( resp) => {
143- debug ! ( target: LOG_TARGET , %name, status = %resp. status( ) , "waiting for sidecar" ) ;
144- }
145155 Err ( err) => {
146156 debug ! ( target: LOG_TARGET , %name, error = %err, "waiting for sidecar" ) ;
147157 }
0 commit comments