11use std:: path:: PathBuf ;
22
3- use anyhow:: { anyhow , Result } ;
3+ use anyhow:: { Context , Result } ;
44pub use clap:: Parser ;
5+ use katana_node:: config:: db:: DbConfig ;
6+ use katana_node:: config:: metrics:: MetricsConfig ;
7+ use katana_node:: config:: rpc:: RpcConfig ;
8+ use katana_node:: full;
59use serde:: { Deserialize , Serialize } ;
10+ use tracing:: info;
611
712use crate :: options:: * ;
813
14+ pub ( crate ) const LOG_TARGET : & str = "katana::cli::full" ;
15+
916#[ derive( Parser , Debug , Serialize , Deserialize , Default , Clone , PartialEq ) ]
1017#[ command( next_help_heading = "Full node options" ) ]
1118pub struct FullNodeArgs {
@@ -21,6 +28,11 @@ pub struct FullNodeArgs {
2128 #[ arg( value_name = "PATH" ) ]
2229 pub db_dir : Option < PathBuf > ,
2330
31+ /// Gateway API key for accessing the sequencer gateway.
32+ #[ arg( long) ]
33+ #[ arg( value_name = "KEY" ) ]
34+ pub gateway_api_key : Option < String > ,
35+
2436 #[ command( flatten) ]
2537 pub logging : LoggingOptions ,
2638
@@ -42,6 +54,95 @@ pub struct FullNodeArgs {
4254
4355impl FullNodeArgs {
4456 pub async fn execute ( & self ) -> Result < ( ) > {
45- Err ( anyhow ! ( "Full node is not implemented yet!" ) )
57+ // Initialize logging with tracer
58+ let tracer_config = self . tracer_config ( ) ;
59+ katana_tracing:: init ( self . logging . log_format , tracer_config) . await ?;
60+ self . start_node ( ) . await
61+ }
62+
63+ async fn start_node ( & self ) -> Result < ( ) > {
64+ // Build the node
65+ let config = self . config ( ) ?;
66+ let node = full:: Node :: build ( config) . context ( "failed to build full node" ) ?;
67+
68+ if !self . silent {
69+ info ! ( target: LOG_TARGET , "Starting full node" ) ;
70+ }
71+
72+ // Launch the node
73+ let handle = node. launch ( ) . await . context ( "failed to launch full node" ) ?;
74+
75+ // Wait until an OS signal (ie SIGINT, SIGTERM) is received or the node is shutdown.
76+ tokio:: select! {
77+ _ = katana_utils:: wait_shutdown_signals( ) => {
78+ // Gracefully shutdown the node before exiting
79+ handle. stop( ) . await ?;
80+ } ,
81+
82+ _ = handle. stopped( ) => { }
83+ }
84+
85+ info ! ( "Shutting down." ) ;
86+
87+ Ok ( ( ) )
88+ }
89+
90+ fn config ( & self ) -> Result < full:: Config > {
91+ let db = self . db_config ( ) ;
92+ let rpc = self . rpc_config ( ) ?;
93+ let metrics = self . metrics_config ( ) ;
94+
95+ Ok ( full:: Config { db, rpc, metrics, gateway_api_key : self . gateway_api_key . clone ( ) } )
96+ }
97+
98+ fn db_config ( & self ) -> DbConfig {
99+ DbConfig { dir : self . db_dir . clone ( ) }
100+ }
101+
102+ fn rpc_config ( & self ) -> Result < RpcConfig > {
103+ #[ cfg( feature = "server" ) ]
104+ {
105+ use std:: time:: Duration ;
106+
107+ let cors_origins = self . server . http_cors_origins . clone ( ) ;
108+
109+ Ok ( RpcConfig {
110+ apis : Default :: default ( ) ,
111+ port : self . server . http_port ,
112+ addr : self . server . http_addr ,
113+ max_connections : self . server . max_connections ,
114+ max_concurrent_estimate_fee_requests : None ,
115+ max_request_body_size : None ,
116+ max_response_body_size : None ,
117+ timeout : self . server . timeout . map ( Duration :: from_secs) ,
118+ cors_origins,
119+ #[ cfg( feature = "explorer" ) ]
120+ explorer : self . explorer . explorer ,
121+ max_event_page_size : Some ( self . server . max_event_page_size ) ,
122+ max_proof_keys : Some ( self . server . max_proof_keys ) ,
123+ max_call_gas : Some ( self . server . max_call_gas ) ,
124+ } )
125+ }
126+
127+ #[ cfg( not( feature = "server" ) ) ]
128+ {
129+ Ok ( RpcConfig :: default ( ) )
130+ }
131+ }
132+
133+ fn metrics_config ( & self ) -> Option < MetricsConfig > {
134+ #[ cfg( feature = "server" ) ]
135+ if self . metrics . metrics {
136+ Some ( MetricsConfig { addr : self . metrics . metrics_addr , port : self . metrics . metrics_port } )
137+ } else {
138+ None
139+ }
140+
141+ #[ cfg( not( feature = "server" ) ) ]
142+ None
143+ }
144+
145+ fn tracer_config ( & self ) -> Option < katana_tracing:: TracerConfig > {
146+ self . tracer . config ( )
46147 }
47148}
0 commit comments