From 3ba53c8a8264a37812b5fa2634441867d83bed60 Mon Sep 17 00:00:00 2001
From: Ammar Arif <evergreenkary@gmail.com>
Date: Sat, 30 Nov 2024 17:33:51 -0500
Subject: [PATCH] refactor(katana): include config in the node struct (#2742)

---
 crates/dojo/test-utils/src/sequencer.rs |  2 +-
 crates/katana/cli/src/args.rs           |  5 +++--
 crates/katana/core/src/backend/mod.rs   |  2 +-
 crates/katana/node/src/config/mod.rs    |  4 +++-
 crates/katana/node/src/lib.rs           | 29 ++++++++++---------------
 5 files changed, 19 insertions(+), 23 deletions(-)

diff --git a/crates/dojo/test-utils/src/sequencer.rs b/crates/dojo/test-utils/src/sequencer.rs
index 6d876b3f44..43e154e072 100644
--- a/crates/dojo/test-utils/src/sequencer.rs
+++ b/crates/dojo/test-utils/src/sequencer.rs
@@ -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() }
 }
diff --git a/crates/katana/cli/src/args.rs b/crates/katana/cli/src/args.rs
index 0976880839..0474007377 100644
--- a/crates/katana/cli/src/args.rs
+++ b/crates/katana/cli/src/args.rs
@@ -2,6 +2,7 @@
 
 use std::collections::HashSet;
 use std::path::PathBuf;
+use std::sync::Arc;
 
 use alloy_primitives::U256;
 use anyhow::{Context, Result};
@@ -203,7 +204,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 {
@@ -229,7 +230,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 {
diff --git a/crates/katana/core/src/backend/mod.rs b/crates/katana/core/src/backend/mod.rs
index 7d8d74a142..47908816bc 100644
--- a/crates/katana/core/src/backend/mod.rs
+++ b/crates/katana/core/src/backend/mod.rs
@@ -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.
diff --git a/crates/katana/node/src/config/mod.rs b/crates/katana/node/src/config/mod.rs
index b79ae25a97..61bf2eeea1 100644
--- a/crates/katana/node/src/config/mod.rs
+++ b/crates/katana/node/src/config/mod.rs
@@ -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;
@@ -20,7 +22,7 @@ use rpc::RpcConfig;
 #[derive(Debug, Clone, Default)]
 pub struct Config {
     /// The chain specification.
-    pub chain: ChainSpec,
+    pub chain: Arc<ChainSpec>,
 
     /// Database options.
     pub db: DbConfig,
diff --git a/crates/katana/node/src/lib.rs b/crates/katana/node/src/lib.rs
index 4675e1acbe..31003668ee 100644
--- a/crates/katana/node/src/lib.rs
+++ b/crates/katana/node/src/lib.rs
@@ -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};
@@ -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};
@@ -90,10 +88,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 config: Arc<Config>,
     forked_client: Option<ForkedClient>,
 }
 
@@ -106,7 +101,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();
 
@@ -133,7 +128,7 @@ impl Node {
             backend.clone(),
             self.task_manager.task_spawner(),
             block_producer.clone(),
-            self.messaging_config.clone(),
+            self.config.messaging.clone(),
         );
 
         self.task_manager
@@ -144,7 +139,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?;
 
         Ok(LaunchedNode { node: self, rpc })
     }
@@ -183,8 +178,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);
@@ -201,8 +197,8 @@ 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.
-    let gas_oracle = if let Some(fixed_prices) = config.dev.fixed_gas_prices {
-        L1GasOracle::fixed(fixed_prices.gas_price, fixed_prices.data_gas_price)
+    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.
@@ -219,7 +215,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
@@ -245,10 +241,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(),
     };