Skip to content

Commit

Permalink
feat(katana): add node handle (#2408)
Browse files Browse the repository at this point in the history
  • Loading branch information
kariy authored Sep 10, 2024
1 parent b5403ec commit 1ef88f8
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 24 deletions.
9 changes: 4 additions & 5 deletions bin/katana/src/cli/node.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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(())
}
Expand Down
19 changes: 9 additions & 10 deletions crates/dojo-test-utils/src/sequencer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -29,9 +29,8 @@ pub struct TestAccount {
#[allow(missing_debug_implementations)]
pub struct TestSequencer {
url: Url,
handle: NodeHandle,
handle: Handle,
account: TestAccount,
backend: Arc<Backend<BlockifierFactory>>,
}

impl TestSequencer {
Expand All @@ -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<JsonRpcClient<HttpTransport>, LocalWallet> {
Expand All @@ -80,15 +79,15 @@ impl TestSequencer {
}

pub fn backend(&self) -> &Arc<Backend<BlockifierFactory>> {
&self.backend
&self.handle.backend
}

pub fn account_at_index(
&self,
index: usize,
) -> SingleOwnerAccount<JsonRpcClient<HttpTransport>, 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());
Expand All @@ -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 {
Expand Down
26 changes: 17 additions & 9 deletions crates/katana/node/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<Backend<BlockifierFactory>>,
pub block_producer: Arc<BlockProducer<BlockifierFactory>>,
}

/// 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
Expand All @@ -63,7 +72,7 @@ pub async fn start(
server_config: ServerConfig,
sequencer_config: SequencerConfig,
mut starknet_config: StarknetConfig,
) -> anyhow::Result<(NodeHandle, Arc<Backend<BlockifierFactory>>)> {
) -> Result<Handle> {
// --- build executor factory

let cfg_env = CfgEnv {
Expand Down Expand Up @@ -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<EF: ExecutorFactory>(
node_components: (TxPool, Arc<Backend<EF>>, Arc<BlockProducer<EF>>, TxValidator),
config: ServerConfig,
) -> Result<NodeHandle> {
) -> Result<RpcServer> {
let (pool, backend, block_producer, validator) = node_components;

let mut methods = RpcModule::new(());
Expand Down Expand Up @@ -291,12 +300,11 @@ pub async fn spawn<EF: ExecutorFactory>(
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,
}

0 comments on commit 1ef88f8

Please sign in to comment.