From 361024339dfefeee6a66768ad0517947958fd4a5 Mon Sep 17 00:00:00 2001 From: Ammar Arif Date: Thu, 19 Oct 2023 09:05:15 +0900 Subject: [PATCH] dev mode (#1067) --- crates/dojo-test-utils/src/sequencer.rs | 20 +++++++++------ crates/katana/rpc/src/api/mod.rs | 7 +++++ crates/katana/rpc/src/config.rs | 3 +++ crates/katana/rpc/src/lib.rs | 34 +++++++++++++++---------- crates/katana/src/args.rs | 11 ++++++++ crates/katana/src/main.rs | 7 ++--- 6 files changed, 56 insertions(+), 26 deletions(-) diff --git a/crates/dojo-test-utils/src/sequencer.rs b/crates/dojo-test-utils/src/sequencer.rs index abd214478c..2bc3ae2282 100644 --- a/crates/dojo-test-utils/src/sequencer.rs +++ b/crates/dojo-test-utils/src/sequencer.rs @@ -4,8 +4,9 @@ use jsonrpsee::core::Error; pub use katana_core::backend::config::{Environment, StarknetConfig}; use katana_core::sequencer::KatanaSequencer; pub use katana_core::sequencer::SequencerConfig; +use katana_rpc::api::ApiKind; use katana_rpc::config::ServerConfig; -use katana_rpc::{spawn, KatanaApi, NodeHandle, StarknetApi}; +use katana_rpc::{spawn, NodeHandle}; use starknet::accounts::{ExecutionEncoding, SingleOwnerAccount}; use starknet::core::chain_id; use starknet::core::types::FieldElement; @@ -31,13 +32,16 @@ impl TestSequencer { pub async fn start(config: SequencerConfig, starknet_config: StarknetConfig) -> Self { let sequencer = Arc::new(KatanaSequencer::new(config, starknet_config).await); - let starknet_api = StarknetApi::new(sequencer.clone()); - let katana_api = KatanaApi::new(sequencer.clone()); - - let handle = - spawn(katana_api, starknet_api, ServerConfig { port: 0, host: "localhost".into() }) - .await - .expect("Unable to spawn server"); + let handle = spawn( + Arc::clone(&sequencer), + ServerConfig { + port: 0, + host: "0.0.0.0".into(), + apis: vec![ApiKind::Starknet, ApiKind::Katana], + }, + ) + .await + .expect("Unable to spawn server"); let url = Url::parse(&format!("http://{}", handle.addr)).expect("Failed to parse URL"); diff --git a/crates/katana/rpc/src/api/mod.rs b/crates/katana/rpc/src/api/mod.rs index 608feeefd5..67dd0c7d90 100644 --- a/crates/katana/rpc/src/api/mod.rs +++ b/crates/katana/rpc/src/api/mod.rs @@ -1,2 +1,9 @@ pub mod katana; pub mod starknet; + +/// List of APIs supported by Katana. +#[derive(Debug, Copy, Clone)] +pub enum ApiKind { + Starknet, + Katana, +} diff --git a/crates/katana/rpc/src/config.rs b/crates/katana/rpc/src/config.rs index d2413a5059..adb8dac1b2 100644 --- a/crates/katana/rpc/src/config.rs +++ b/crates/katana/rpc/src/config.rs @@ -1,7 +1,10 @@ +use crate::api::ApiKind; + #[derive(Debug, Clone)] pub struct ServerConfig { pub port: u16, pub host: String, + pub apis: Vec, } impl ServerConfig { diff --git a/crates/katana/rpc/src/lib.rs b/crates/katana/rpc/src/lib.rs index 1ab9673782..29615d658d 100644 --- a/crates/katana/rpc/src/lib.rs +++ b/crates/katana/rpc/src/lib.rs @@ -1,12 +1,14 @@ -mod api; +pub mod api; pub mod config; -mod katana; -mod starknet; +pub mod katana; +pub mod starknet; use std::net::SocketAddr; +use std::sync::Arc; use std::time::{Duration, Instant}; use anyhow::Result; +use api::ApiKind; use config::ServerConfig; use hyper::Method; use jsonrpsee::server::logger::{Logger, MethodKind, TransportProtocol}; @@ -15,23 +17,29 @@ use jsonrpsee::server::{AllowHosts, ServerBuilder, ServerHandle}; use jsonrpsee::tracing::debug; use jsonrpsee::types::Params; use jsonrpsee::RpcModule; +use katana_core::sequencer::KatanaSequencer; use tower_http::cors::{Any, CorsLayer}; use crate::api::katana::KatanaApiServer; use crate::api::starknet::StarknetApiServer; -pub use crate::katana::KatanaApi; -pub use crate::starknet::StarknetApi; - -pub async fn spawn( - katana_api: KatanaApi, - starknet_api: StarknetApi, - config: ServerConfig, -) -> Result { +use crate::katana::KatanaApi; +use crate::starknet::StarknetApi; + +pub async fn spawn(sequencer: Arc, config: ServerConfig) -> Result { let mut methods = RpcModule::new(()); - methods.merge(starknet_api.into_rpc())?; - methods.merge(katana_api.into_rpc())?; methods.register_method("health", |_, _| Ok(serde_json::json!({ "health": true })))?; + for api in &config.apis { + match api { + ApiKind::Starknet => { + methods.merge(StarknetApi::new(sequencer.clone()).into_rpc())?; + } + ApiKind::Katana => { + methods.merge(KatanaApi::new(sequencer.clone()).into_rpc())?; + } + } + } + let cors = CorsLayer::new() // Allow `POST` when accessing the resource .allow_methods([Method::POST, Method::GET]) diff --git a/crates/katana/src/args.rs b/crates/katana/src/args.rs index 26af32c05e..abd284a0a1 100644 --- a/crates/katana/src/args.rs +++ b/crates/katana/src/args.rs @@ -8,6 +8,7 @@ use katana_core::constants::{ }; use katana_core::db::serde::state::SerializableState; use katana_core::sequencer::SequencerConfig; +use katana_rpc::api::ApiKind; use katana_rpc::config::ServerConfig; use tracing::Subscriber; use tracing_subscriber::{fmt, EnvFilter}; @@ -43,6 +44,9 @@ pub struct KatanaArgs { #[arg(help = "The Starknet RPC provider to fork the network from.")] pub rpc_url: Option, + #[arg(long)] + pub dev: bool, + #[arg(long)] #[arg(help = "Output logs in JSON format.")] pub json_log: bool, @@ -170,7 +174,14 @@ impl KatanaArgs { } pub fn server_config(&self) -> ServerConfig { + let mut apis = vec![ApiKind::Starknet]; + // only enable `katana` API in dev mode + if self.dev { + apis.push(ApiKind::Katana); + } + ServerConfig { + apis, port: self.server.port, host: self.server.host.clone().unwrap_or("0.0.0.0".into()), } diff --git a/crates/katana/src/main.rs b/crates/katana/src/main.rs index 6db80605dd..72e76e1e92 100644 --- a/crates/katana/src/main.rs +++ b/crates/katana/src/main.rs @@ -5,7 +5,7 @@ use clap::{CommandFactory, Parser}; use clap_complete::{generate, Shell}; use console::Style; use katana_core::sequencer::KatanaSequencer; -use katana_rpc::{spawn, KatanaApi, NodeHandle, StarknetApi}; +use katana_rpc::{spawn, NodeHandle}; use tokio::signal::ctrl_c; use tracing::{error, info}; @@ -33,10 +33,7 @@ async fn main() -> Result<(), Box> { let starknet_config = config.starknet_config(); let sequencer = Arc::new(KatanaSequencer::new(sequencer_config, starknet_config).await); - let starknet_api = StarknetApi::new(sequencer.clone()); - let katana_api = KatanaApi::new(sequencer.clone()); - - let NodeHandle { addr, handle, .. } = spawn(katana_api, starknet_api, server_config).await?; + let NodeHandle { addr, handle, .. } = spawn(Arc::clone(&sequencer), server_config).await?; if !config.silent { let accounts = sequencer.backend.accounts.iter();