Skip to content

Commit

Permalink
dev mode (#1067)
Browse files Browse the repository at this point in the history
  • Loading branch information
kariy authored Oct 19, 2023
1 parent 3101ce8 commit 3610243
Show file tree
Hide file tree
Showing 6 changed files with 56 additions and 26 deletions.
20 changes: 12 additions & 8 deletions crates/dojo-test-utils/src/sequencer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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");

Expand Down
7 changes: 7 additions & 0 deletions crates/katana/rpc/src/api/mod.rs
Original file line number Diff line number Diff line change
@@ -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,
}
3 changes: 3 additions & 0 deletions crates/katana/rpc/src/config.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
use crate::api::ApiKind;

#[derive(Debug, Clone)]
pub struct ServerConfig {
pub port: u16,
pub host: String,
pub apis: Vec<ApiKind>,
}

impl ServerConfig {
Expand Down
34 changes: 21 additions & 13 deletions crates/katana/rpc/src/lib.rs
Original file line number Diff line number Diff line change
@@ -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};
Expand All @@ -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<NodeHandle> {
use crate::katana::KatanaApi;
use crate::starknet::StarknetApi;

pub async fn spawn(sequencer: Arc<KatanaSequencer>, config: ServerConfig) -> Result<NodeHandle> {
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])
Expand Down
11 changes: 11 additions & 0 deletions crates/katana/src/args.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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};
Expand Down Expand Up @@ -43,6 +44,9 @@ pub struct KatanaArgs {
#[arg(help = "The Starknet RPC provider to fork the network from.")]
pub rpc_url: Option<Url>,

#[arg(long)]
pub dev: bool,

#[arg(long)]
#[arg(help = "Output logs in JSON format.")]
pub json_log: bool,
Expand Down Expand Up @@ -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()),
}
Expand Down
7 changes: 2 additions & 5 deletions crates/katana/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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};

Expand Down Expand Up @@ -33,10 +33,7 @@ async fn main() -> Result<(), Box<dyn std::error::Error>> {
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();
Expand Down

0 comments on commit 3610243

Please sign in to comment.