Skip to content

Commit

Permalink
refactor(katana): include config in the node struct (dojoengine#2742)
Browse files Browse the repository at this point in the history
  • Loading branch information
kariy authored and augustin-v committed Dec 4, 2024
1 parent 7c4467a commit 9604d5a
Show file tree
Hide file tree
Showing 5 changed files with 23 additions and 31 deletions.
2 changes: 1 addition & 1 deletion crates/dojo/test-utils/src/sequencer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -126,5 +126,5 @@ pub fn get_default_test_config(sequencing: SequencingConfig) -> Config {
max_event_page_size: Some(100),
};

Config { sequencing, rpc, dev, chain, ..Default::default() }
Config { sequencing, rpc, dev, chain: chain.into(), ..Default::default() }
}
5 changes: 3 additions & 2 deletions crates/katana/cli/src/args.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
use std::collections::HashSet;
use std::path::PathBuf;
use std::sync::Arc;

use alloy_primitives::U256;
use anyhow::{Context, Ok, Result};
Expand Down Expand Up @@ -220,7 +221,7 @@ impl NodeArgs {
}
}

fn chain_spec(&self) -> Result<ChainSpec> {
fn chain_spec(&self) -> Result<Arc<ChainSpec>> {
let mut chain_spec = chain_spec::DEV_UNALLOCATED.clone();

if let Some(id) = self.starknet.environment.chain_id {
Expand All @@ -246,7 +247,7 @@ impl NodeArgs {
katana_slot_controller::add_controller_account(&mut chain_spec.genesis)?;
}

Ok(chain_spec)
Ok(Arc::new(chain_spec))
}

fn dev_config(&self) -> DevConfig {
Expand Down
2 changes: 1 addition & 1 deletion crates/katana/core/src/backend/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ pub(crate) const LOG_TARGET: &str = "katana::core::backend";

#[derive(Debug)]
pub struct Backend<EF: ExecutorFactory> {
pub chain_spec: ChainSpec,
pub chain_spec: Arc<ChainSpec>,
/// stores all block related data in memory
pub blockchain: Blockchain,
/// The block context generator.
Expand Down
4 changes: 3 additions & 1 deletion crates/katana/node/src/config/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ pub mod fork;
pub mod metrics;
pub mod rpc;

use std::sync::Arc;

use db::DbConfig;
use dev::DevConfig;
use execution::ExecutionConfig;
Expand All @@ -21,7 +23,7 @@ use url::Url;
#[derive(Debug, Clone, Default)]
pub struct Config {
/// The chain specification.
pub chain: ChainSpec,
pub chain: Arc<ChainSpec>,

/// Database options.
pub db: DbConfig,
Expand Down
41 changes: 15 additions & 26 deletions crates/katana/node/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,8 @@ use std::sync::Arc;
use std::time::Duration;

use anyhow::Result;
use config::metrics::MetricsConfig;
use config::rpc::{ApiKind, RpcConfig};
use config::{Config, SequencingConfig};
use config::Config;
use dojo_metrics::exporters::prometheus::PrometheusRecorder;
use dojo_metrics::{Report, Server as MetricsServer};
use hyper::{Method, Uri};
Expand All @@ -28,7 +27,6 @@ use katana_core::constants::{
};
use katana_core::env::BlockContextGenerator;
use katana_core::service::block_producer::BlockProducer;
use katana_core::service::messaging::MessagingConfig;
use katana_db::mdbx::DbEnv;
use katana_executor::implementation::blockifier::BlockifierFactory;
use katana_executor::{ExecutionFlags, ExecutorFactory};
Expand Down Expand Up @@ -91,11 +89,7 @@ pub struct Node {
pub task_manager: TaskManager,
pub backend: Arc<Backend<BlockifierFactory>>,
pub block_producer: BlockProducer<BlockifierFactory>,
pub rpc_config: RpcConfig,
pub metrics_config: Option<MetricsConfig>,
pub sequencing_config: SequencingConfig,
pub messaging_config: Option<MessagingConfig>,
pub l1_provider_url: Option<Url>,
pub config: Arc<Config>,
forked_client: Option<ForkedClient>,
}

Expand All @@ -108,7 +102,7 @@ impl Node {
info!(%chain, "Starting node.");

// TODO: maybe move this to the build stage
if let Some(ref cfg) = self.metrics_config {
if let Some(ref cfg) = self.config.metrics {
let addr = cfg.socket_addr();
let mut reports: Vec<Box<dyn Report>> = Vec::new();

Expand All @@ -135,7 +129,7 @@ impl Node {
backend.clone(),
self.task_manager.task_spawner(),
block_producer.clone(),
self.messaging_config.clone(),
self.config.messaging.clone(),
);

self.task_manager
Expand All @@ -146,7 +140,7 @@ impl Node {
.spawn(sequencing.into_future());

let node_components = (pool, backend, block_producer, validator, self.forked_client.take());
let rpc = spawn(node_components, self.rpc_config.clone()).await?;
let rpc = spawn(node_components, self.config.rpc.clone()).await?;

// --- build and start the gas oracle worker task

Expand Down Expand Up @@ -196,8 +190,9 @@ pub async fn build(mut config: Config) -> Result<Node> {
// --- build backend

let (blockchain, db, forked_client) = if let Some(cfg) = &config.forking {
let chain_spec = Arc::get_mut(&mut config.chain).expect("get mut Arc");
let (bc, block_num) =
Blockchain::new_from_forked(cfg.url.clone(), cfg.block, &mut config.chain).await?;
Blockchain::new_from_forked(cfg.url.clone(), cfg.block, chain_spec).await?;

// TODO: it'd bee nice if the client can be shared on both the rpc and forked backend side
let forked_client = ForkedClient::new_http(cfg.url.clone(), block_num);
Expand All @@ -214,15 +209,12 @@ pub async fn build(mut config: Config) -> Result<Node> {
// --- build l1 gas oracle

// Check if the user specify a fixed gas price in the dev config.
// cases to cover:
// 1. Fixed price by user
// 2. No fixed price by user and no sampling
// 3. Sampling with user input provider url
let gas_oracle = if let Some(fixed_prices) = config.dev.fixed_gas_prices {
// Use fixed gas prices if provided in the configuration
L1GasOracle::fixed(fixed_prices.gas_price, fixed_prices.data_gas_price)
} else if config.l1_provider_url.as_ref().is_none() {
// Use default fixed gas prices if `gpo` is `None`
let gas_oracle = if let Some(fixed_prices) = &config.dev.fixed_gas_prices {
L1GasOracle::fixed(fixed_prices.gas_price.clone(), fixed_prices.data_gas_price.clone())
}
// TODO: for now we just use the default gas prices, but this should be a proper oracle in the
// future that can perform actual sampling.
else {
L1GasOracle::fixed(
GasPrices { eth: DEFAULT_ETH_L1_GAS_PRICE, strk: DEFAULT_STRK_L1_GAS_PRICE },
GasPrices { eth: DEFAULT_ETH_L1_DATA_GAS_PRICE, strk: DEFAULT_STRK_L1_DATA_GAS_PRICE },
Expand All @@ -238,7 +230,7 @@ pub async fn build(mut config: Config) -> Result<Node> {
blockchain,
executor_factory,
block_context_generator,
chain_spec: config.chain,
chain_spec: config.chain.clone(),
});

// --- build block producer
Expand All @@ -264,10 +256,7 @@ pub async fn build(mut config: Config) -> Result<Node> {
backend,
forked_client,
block_producer,
rpc_config: config.rpc,
metrics_config: config.metrics,
messaging_config: config.messaging,
sequencing_config: config.sequencing,
config: Arc::new(config),
task_manager: TaskManager::current(),
l1_provider_url: config.l1_provider_url,
};
Expand Down

0 comments on commit 9604d5a

Please sign in to comment.