Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(katana): add node handle #2408

Merged
merged 3 commits into from
Sep 10, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 @@
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?;

Check warning on line 229 in bin/katana/src/cli/node.rs

View check run for this annotation

Codecov / codecov/patch

bin/katana/src/cli/node.rs#L229

Added line #L229 was not covered by tests

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);

Check warning on line 234 in bin/katana/src/cli/node.rs

View check run for this annotation

Codecov / codecov/patch

bin/katana/src/cli/node.rs#L233-L234

Added lines #L233 - L234 were not covered by tests
}

// Wait until Ctrl + C is pressed, then shutdown
ctrl_c().await?;
rpc_handle.handle.stop()?;
node.rpc.handle.stop()?;

Check warning on line 239 in bin/katana/src/cli/node.rs

View check run for this annotation

Codecov / codecov/patch

bin/katana/src/cli/node.rs#L239

Added line #L239 was not covered by tests

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 @@
#[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 @@
#[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 @@
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");

Check warning on line 52 in crates/dojo-test-utils/src/sequencer.rs

View check run for this annotation

Codecov / codecov/patch

crates/dojo-test-utils/src/sequencer.rs#L52

Added line #L52 was not covered by tests

let account = backend.config.genesis.accounts().next().unwrap();
let account = node.backend.config.genesis.accounts().next().unwrap();

Check warning on line 54 in crates/dojo-test-utils/src/sequencer.rs

View check run for this annotation

Codecov / codecov/patch

crates/dojo-test-utils/src/sequencer.rs#L54

Added line #L54 was not covered by tests
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 }

Check warning on line 60 in crates/dojo-test-utils/src/sequencer.rs

View check run for this annotation

Codecov / codecov/patch

crates/dojo-test-utils/src/sequencer.rs#L60

Added line #L60 was not covered by tests
}

pub fn account(&self) -> SingleOwnerAccount<JsonRpcClient<HttpTransport>, LocalWallet> {
Expand All @@ -80,15 +79,15 @@
}

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::<_>();

Check warning on line 90 in crates/dojo-test-utils/src/sequencer.rs

View check run for this annotation

Codecov / codecov/patch

crates/dojo-test-utils/src/sequencer.rs#L90

Added line #L90 was not covered by tests

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 @@
}

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 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 @@
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 @@

// --- 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);

Check warning on line 223 in crates/katana/node/src/lib.rs

View check run for this annotation

Codecov / codecov/patch

crates/katana/node/src/lib.rs#L223

Added line #L223 was not covered by tests
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 @@
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,
}
Loading