diff --git a/bin/katana/src/cli/node.rs b/bin/katana/src/cli/node.rs index 63055b068b..c2c8482132 100644 --- a/bin/katana/src/cli/node.rs +++ b/bin/katana/src/cli/node.rs @@ -226,18 +226,17 @@ impl NodeArgs { let starknet_config = self.starknet_config()?; // build the node and start it - let (rpc_handle, backend) = - katana_node::start(server_config, sequencer_config, starknet_config).await?; + let node = katana_node::start(server_config, sequencer_config, starknet_config).await?; if !self.silent { #[allow(deprecated)] - let genesis = &backend.config.genesis; - print_intro(&self, genesis, rpc_handle.addr); + let genesis = &node.backend.config.genesis; + print_intro(&self, genesis, node.rpc.addr); } // Wait until Ctrl + C is pressed, then shutdown ctrl_c().await?; - rpc_handle.handle.stop()?; + node.rpc.handle.stop()?; Ok(()) } diff --git a/crates/dojo-test-utils/src/sequencer.rs b/crates/dojo-test-utils/src/sequencer.rs index 534794623d..039b26fab1 100644 --- a/crates/dojo-test-utils/src/sequencer.rs +++ b/crates/dojo-test-utils/src/sequencer.rs @@ -7,7 +7,7 @@ use katana_core::constants::DEFAULT_SEQUENCER_ADDRESS; #[allow(deprecated)] pub use katana_core::sequencer::SequencerConfig; use katana_executor::implementation::blockifier::BlockifierFactory; -use katana_node::NodeHandle; +use katana_node::Handle; use katana_primitives::chain::ChainId; use katana_rpc::config::ServerConfig; use katana_rpc_api::ApiKind; @@ -29,9 +29,8 @@ pub struct TestAccount { #[allow(missing_debug_implementations)] pub struct TestSequencer { url: Url, - handle: NodeHandle, + handle: Handle, account: TestAccount, - backend: Arc>, } impl TestSequencer { @@ -46,19 +45,19 @@ impl TestSequencer { apis: vec![ApiKind::Starknet, ApiKind::Dev, ApiKind::Saya, ApiKind::Torii], }; - let (handle, backend) = katana_node::start(server_config, config, starknet_config) + let node = katana_node::start(server_config, config, starknet_config) .await .expect("Failed to build node components"); - let url = Url::parse(&format!("http://{}", handle.addr)).expect("Failed to parse URL"); + let url = Url::parse(&format!("http://{}", node.rpc.addr)).expect("Failed to parse URL"); - let account = backend.config.genesis.accounts().next().unwrap(); + let account = node.backend.config.genesis.accounts().next().unwrap(); let account = TestAccount { private_key: Felt::from_bytes_be(&account.1.private_key().unwrap().to_bytes_be()), account_address: Felt::from_bytes_be(&account.0.to_bytes_be()), }; - TestSequencer { backend, account, handle, url } + TestSequencer { handle: node, account, url } } pub fn account(&self) -> SingleOwnerAccount, LocalWallet> { @@ -80,7 +79,7 @@ impl TestSequencer { } pub fn backend(&self) -> &Arc> { - &self.backend + &self.handle.backend } pub fn account_at_index( @@ -88,7 +87,7 @@ impl TestSequencer { index: usize, ) -> SingleOwnerAccount, LocalWallet> { #[allow(deprecated)] - let accounts: Vec<_> = self.backend.config.genesis.accounts().collect::<_>(); + let accounts: Vec<_> = self.handle.backend.config.genesis.accounts().collect::<_>(); let account = accounts[index]; let private_key = Felt::from_bytes_be(&account.1.private_key().unwrap().to_bytes_be()); @@ -112,7 +111,7 @@ impl TestSequencer { } pub fn stop(self) -> Result<(), Error> { - self.handle.handle.stop() + self.handle.rpc.handle.stop() } pub fn url(&self) -> Url { diff --git a/crates/katana/node/src/lib.rs b/crates/katana/node/src/lib.rs index 4b2f7c5221..9c62aa0075 100644 --- a/crates/katana/node/src/lib.rs +++ b/crates/katana/node/src/lib.rs @@ -49,6 +49,15 @@ use starknet::providers::{JsonRpcClient, Provider}; use tower_http::cors::{AllowOrigin, CorsLayer}; use tracing::{info, trace}; +/// A handle to the instantiated Katana node. +#[allow(missing_debug_implementations)] +pub struct Handle { + pub pool: TxPool, + pub rpc: RpcServer, + pub backend: Arc>, + pub block_producer: Arc>, +} + /// Build the core Katana components from the given configurations and start running the node. // TODO: placeholder until we implement a dedicated class that encapsulate building the node // components @@ -63,7 +72,7 @@ pub async fn start( server_config: ServerConfig, sequencer_config: SequencerConfig, mut starknet_config: StarknetConfig, -) -> anyhow::Result<(NodeHandle, Arc>)> { +) -> Result { // --- build executor factory let cfg_env = CfgEnv { @@ -211,17 +220,17 @@ pub async fn start( // --- spawn rpc server - let node_components = (pool, backend.clone(), block_producer, validator); - let rpc_handle = spawn(node_components, server_config).await?; + let node_components = (pool.clone(), backend.clone(), block_producer.clone(), validator); + let rpc = spawn(node_components, server_config).await?; - Ok((rpc_handle, backend)) + Ok(Handle { backend, block_producer, pool, rpc }) } // Moved from `katana_rpc` crate pub async fn spawn( node_components: (TxPool, Arc>, Arc>, TxValidator), config: ServerConfig, -) -> Result { +) -> Result { let (pool, backend, block_producer, validator) = node_components; let mut methods = RpcModule::new(()); @@ -291,12 +300,11 @@ pub async fn spawn( let addr = server.local_addr()?; let handle = server.start(methods)?; - Ok(NodeHandle { config, handle, addr }) + Ok(RpcServer { handle, addr }) } -#[derive(Debug, Clone)] -pub struct NodeHandle { +#[derive(Debug)] +pub struct RpcServer { pub addr: SocketAddr, - pub config: ServerConfig, pub handle: ServerHandle, }