22//!
33//! This module handles spawning and managing the VRF sidecar process.
44
5- use std:: env;
6- use std:: io;
75use std:: path:: { Path , PathBuf } ;
86use std:: process:: Stdio ;
97use std:: time:: { Duration , Instant } ;
8+ use std:: { env, io} ;
109
1110use katana_primitives:: { ContractAddress , Felt } ;
1211use tokio:: process:: { Child , Command } ;
1312use tokio:: time:: sleep;
1413use tracing:: { debug, info, warn} ;
1514use url:: Url ;
1615
17- use super :: bootstrap:: { bootstrap_vrf, VrfBootstrapConfig , VrfBootstrapResult } ;
18-
1916const LOG_TARGET : & str = "katana::cartridge::vrf::sidecar" ;
2017
2118/// Fixed port used by vrf-server.
2219pub const VRF_SERVER_PORT : u16 = 3000 ;
2320
21+ const DEFAULT_VRF_SERVICE_PATH : & str = "vrf-service" ;
22+
2423/// Default timeout for waiting on sidecar readiness.
2524pub const SIDECAR_TIMEOUT : Duration = Duration :: from_secs ( 10 ) ;
2625
@@ -47,91 +46,51 @@ pub enum Error {
4746 Bootstrap ( #[ source] anyhow:: Error ) ,
4847}
4948
50- /// Result type alias for VRF sidecar operations.
5149pub type Result < T , E = Error > = std:: result:: Result < T , E > ;
5250
5351// ============================================================================
54- // Configuration Types
52+ // VRF Service
5553// ============================================================================
5654
57- /// Configuration for the VRF service.
5855#[ derive( Debug , Clone ) ]
5956pub struct VrfServiceConfig {
60- /// RPC URL of the katana node (for bootstrap).
61- pub rpc_url : Url ,
62- /// Source account address (deploys contracts and funds VRF account).
63- pub source_address : ContractAddress ,
64- /// Source account private key.
65- pub source_private_key : Felt ,
66- /// Path to the vrf-server binary (None = lookup in PATH).
67- pub program_path : Option < PathBuf > ,
57+ pub vrf_account_address : ContractAddress ,
58+ pub vrf_private_key : Felt ,
59+ pub secret_key : u64 ,
6860}
6961
70- // ============================================================================
71- // VRF Service
72- // ============================================================================
73-
74- /// VRF service that handles bootstrapping and spawning the sidecar process.
7562#[ derive( Debug , Clone ) ]
7663pub struct VrfService {
7764 config : VrfServiceConfig ,
78- bootstrap_result : Option < VrfBootstrapResult > ,
65+ path : PathBuf ,
7966}
8067
8168impl VrfService {
82- /// Create a new VRF service with the given configuration.
8369 pub fn new ( config : VrfServiceConfig ) -> Self {
84- Self { config, bootstrap_result : None }
70+ Self { config, path : PathBuf :: from ( DEFAULT_VRF_SERVICE_PATH ) }
8571 }
8672
87- /// Set a pre-existing bootstrap result, skipping the bootstrap step .
73+ /// Sets the path to the vrf service program .
8874 ///
89- /// Use this when the VRF contracts have already been deployed .
90- pub fn bootstrap_result ( mut self , result : VrfBootstrapResult ) -> Self {
91- self . bootstrap_result = Some ( result ) ;
75+ /// If no path is set, the default executable name [`DEFAULT_VRF_SERVICE_PATH`] will be used .
76+ pub fn path < T : Into < PathBuf > > ( mut self , path : T ) -> Self {
77+ self . path = path . into ( ) ;
9278 self
9379 }
9480
95- /// Get the bootstrap result, if set.
96- pub fn get_bootstrap_result ( & self ) -> Option < & VrfBootstrapResult > {
97- self . bootstrap_result . as_ref ( )
98- }
99-
100- /// Bootstrap the VRF service by deploying necessary contracts.
101- ///
102- /// This deploys the VRF account and consumer contracts via RPC,
103- /// sets up the VRF public key, and optionally funds the account.
104- pub async fn bootstrap ( & mut self ) -> Result < & VrfBootstrapResult > {
105- let bootstrap_config = VrfBootstrapConfig {
106- rpc_url : self . config . rpc_url . clone ( ) ,
107- source_address : self . config . source_address ,
108- source_private_key : self . config . source_private_key ,
109- } ;
110-
111- let result = bootstrap_vrf ( & bootstrap_config) . await . map_err ( Error :: Bootstrap ) ?;
112- self . bootstrap_result = Some ( result) ;
113-
114- Ok ( self . bootstrap_result . as_ref ( ) . expect ( "just set" ) )
115- }
116-
117- /// Start the VRF sidecar process.
118- ///
119- /// This spawns the vrf-server binary and waits for it to become ready.
120- /// Returns an error if bootstrap has not been performed.
12181 pub async fn start ( self ) -> Result < VrfServiceProcess > {
122- let bootstrap_result = self . bootstrap_result . ok_or ( Error :: BootstrapResultNotSet ) ?. clone ( ) ;
123-
124- let bin = self . config . program_path . unwrap_or_else ( || "vrf-server" . into ( ) ) ;
125- let bin = resolve_executable ( Path :: new ( & bin) ) ?;
82+ let bin = resolve_executable ( & self . path ) ?;
12683
12784 let mut command = Command :: new ( bin) ;
12885 command
12986 . arg ( "--port" )
13087 . arg ( VRF_SERVER_PORT . to_string ( ) )
13188 . arg ( "--account-address" )
89+ . arg ( self . config . vrf_account_address . to_hex_string ( ) )
13290 . arg ( "--account-private-key" )
91+ . arg ( self . config . vrf_private_key . to_hex_string ( ) )
13392 . arg ( "--secret-key" )
134- . arg ( bootstrap_result . secret_key . to_string ( ) )
93+ . arg ( self . config . secret_key . to_string ( ) )
13594 . stdout ( Stdio :: inherit ( ) )
13695 . stderr ( Stdio :: inherit ( ) )
13796 . kill_on_drop ( true ) ;
@@ -141,33 +100,26 @@ impl VrfService {
141100 let url = format ! ( "http://127.0.0.1:{VRF_SERVER_PORT}/info" , ) ;
142101 wait_for_http_ok ( & url, "vrf info" , SIDECAR_TIMEOUT ) . await ?;
143102
144- Ok ( VrfServiceProcess { process, bootstrap_result } )
103+ Ok ( VrfServiceProcess { process, inner : self } )
145104 }
146105}
147106
148- // ============================================================================
149- // VRF Sidecar Process
150- // ============================================================================
151-
152107/// A running VRF sidecar process.
153108#[ derive( Debug ) ]
154109pub struct VrfServiceProcess {
155110 process : Child ,
156- bootstrap_result : VrfBootstrapResult ,
111+ inner : VrfService ,
157112}
158113
159114impl VrfServiceProcess {
160- /// Get a mutable reference to the underlying child process.
161115 pub fn process ( & mut self ) -> & mut Child {
162116 & mut self . process
163117 }
164118
165- /// Get the bootstrap result containing VRF account information.
166- pub fn bootstrap_result ( & self ) -> & VrfBootstrapResult {
167- & self . bootstrap_result
119+ pub fn config ( & self ) -> & VrfServiceConfig {
120+ & self . inner . config
168121 }
169122
170- /// Gracefully shutdown the sidecar process.
171123 pub async fn shutdown ( & mut self ) -> io:: Result < ( ) > {
172124 self . process . kill ( ) . await
173125 }
0 commit comments