diff --git a/Cargo.lock b/Cargo.lock index 35a24d284c..2e211a53d9 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -8550,7 +8550,6 @@ dependencies = [ "katana-provider", "katana-tasks", "katana-trie", - "lazy_static", "metrics", "num-traits 0.2.19", "parking_lot 0.12.3", diff --git a/crates/dojo/test-utils/src/sequencer.rs b/crates/dojo/test-utils/src/sequencer.rs index ef1b023eec..0a9f6a2872 100644 --- a/crates/dojo/test-utils/src/sequencer.rs +++ b/crates/dojo/test-utils/src/sequencer.rs @@ -114,19 +114,15 @@ impl TestSequencer { } pub fn get_default_test_config(sequencing: SequencingConfig) -> Config { - let dev = DevConfig { fee: false, account_validation: true, fixed_gas_prices: None }; - let mut chain = ChainSpec { id: ChainId::SEPOLIA, ..Default::default() }; - chain.genesis.sequencer_address = *DEFAULT_SEQUENCER_ADDRESS; - - let rpc = RpcConfig { - cors_origins: Vec::new(), - port: 0, - addr: DEFAULT_RPC_ADDR, - apis: RpcModulesList::all(), - max_connections: DEFAULT_RPC_MAX_CONNECTIONS, - max_event_page_size: Some(100), - max_proof_keys: Some(100), - }; - - Config { sequencing, rpc, dev, chain: chain.into(), ..Default::default() } + let mut chain_spec = ChainSpec { id: ChainId::SEPOLIA, ..Default::default() }; + chain_spec.genesis.sequencer_address = + katana_primitives::ContractAddress(*DEFAULT_SEQUENCER_ADDRESS); + ConfigBuilder::new() + .dev_fee(false) + .chain(chain_spec) + .rpc_apis(RpcModulesList::all()) + .rpc_max_event_page_size(Some(100)) + .rpc_max_proof_keys(Some(100)) + .sequencing(sequencing) + .build() } diff --git a/crates/katana/cli/src/args.rs b/crates/katana/cli/src/args.rs index cd57acb1b2..1da373bc29 100644 --- a/crates/katana/cli/src/args.rs +++ b/crates/katana/cli/src/args.rs @@ -242,7 +242,8 @@ impl NodeArgs { if let Some(genesis) = self.starknet.genesis.clone() { chain_spec.genesis = genesis; } else { - chain_spec.genesis.sequencer_address = *DEFAULT_SEQUENCER_ADDRESS; + chain_spec.genesis.sequencer_address = + katana_primitives::ContractAddress(*DEFAULT_SEQUENCER_ADDRESS); } // generate dev accounts @@ -423,7 +424,10 @@ mod test { assert_eq!(config.execution.validation_max_steps, DEFAULT_VALIDATION_MAX_STEPS); assert_eq!(config.db.dir, None); assert_eq!(config.chain.id, ChainId::parse("KATANA").unwrap()); - assert_eq!(config.chain.genesis.sequencer_address, *DEFAULT_SEQUENCER_ADDRESS); + assert_eq!( + config.chain.genesis.sequencer_address, + katana_primitives::ContractAddress(*DEFAULT_SEQUENCER_ADDRESS) + ); } #[test] @@ -450,7 +454,10 @@ mod test { assert_eq!(config.execution.validation_max_steps, 100); assert_eq!(config.db.dir, Some(PathBuf::from("/path/to/db"))); assert_eq!(config.chain.id, ChainId::GOERLI); - assert_eq!(config.chain.genesis.sequencer_address, *DEFAULT_SEQUENCER_ADDRESS); + assert_eq!( + config.chain.genesis.sequencer_address, + katana_primitives::ContractAddress(*DEFAULT_SEQUENCER_ADDRESS) + ); } #[test] diff --git a/crates/katana/core/Cargo.toml b/crates/katana/core/Cargo.toml index 896f619a48..9150131c19 100644 --- a/crates/katana/core/Cargo.toml +++ b/crates/katana/core/Cargo.toml @@ -20,7 +20,6 @@ async-trait.workspace = true derive_more.workspace = true dojo-metrics.workspace = true futures.workspace = true -lazy_static.workspace = true metrics.workspace = true num-traits.workspace = true parking_lot.workspace = true diff --git a/crates/katana/core/src/constants.rs b/crates/katana/core/src/constants.rs index 26b2172fce..9ccf14128d 100644 --- a/crates/katana/core/src/constants.rs +++ b/crates/katana/core/src/constants.rs @@ -1,5 +1,4 @@ use katana_primitives::contract::ContractAddress; -use lazy_static::lazy_static; use starknet::macros::felt; // Default gas prices @@ -10,10 +9,4 @@ pub const DEFAULT_STRK_L1_GAS_PRICE: u128 = 20 * u128::pow(10, 9); // Given in u pub const DEFAULT_ETH_L1_DATA_GAS_PRICE: u128 = u128::pow(10, 6); // Given in units of Wei. pub const DEFAULT_STRK_L1_DATA_GAS_PRICE: u128 = u128::pow(10, 6); // Given in units of STRK. -lazy_static! { - - // Predefined contract addresses - - pub static ref DEFAULT_SEQUENCER_ADDRESS: ContractAddress = ContractAddress(felt!("0x1")); - -} +pub const DEFAULT_SEQUENCER_ADDRESS: ContractAddress = ContractAddress(felt!("0x1")); diff --git a/crates/katana/node/src/config/execution.rs b/crates/katana/node/src/config/execution.rs index 69db1edb83..55ee0b4be4 100644 --- a/crates/katana/node/src/config/execution.rs +++ b/crates/katana/node/src/config/execution.rs @@ -13,9 +13,9 @@ pub struct ExecutionConfig { impl std::default::Default for ExecutionConfig { fn default() -> Self { Self { - max_recursion_depth: MAX_RECURSION_DEPTH, invocation_max_steps: DEFAULT_INVOCATION_MAX_STEPS, validation_max_steps: DEFAULT_VALIDATION_MAX_STEPS, + max_recursion_depth: MAX_RECURSION_DEPTH, } } } diff --git a/crates/katana/node/src/config/metrics.rs b/crates/katana/node/src/config/metrics.rs index ddceb84805..8703f913e4 100644 --- a/crates/katana/node/src/config/metrics.rs +++ b/crates/katana/node/src/config/metrics.rs @@ -20,3 +20,9 @@ impl MetricsConfig { SocketAddr::new(self.addr, self.port) } } + +impl Default for MetricsConfig { + fn default() -> Self { + MetricsConfig { addr: DEFAULT_METRICS_ADDR, port: DEFAULT_METRICS_PORT } + } +} diff --git a/crates/katana/node/src/config/mod.rs b/crates/katana/node/src/config/mod.rs index 045e30bdee..dc82c67c89 100644 --- a/crates/katana/node/src/config/mod.rs +++ b/crates/katana/node/src/config/mod.rs @@ -7,15 +7,28 @@ pub mod fork; pub mod metrics; pub mod rpc; +use std::collections::{BTreeMap, HashSet}; +use std::net::{IpAddr, SocketAddr}; +use std::path::PathBuf; +use std::str::FromStr; + use db::DbConfig; -use dev::DevConfig; +use dev::{DevConfig, FixedL1GasPriceConfig}; use execution::ExecutionConfig; use fork::ForkingConfig; use katana_core::service::messaging::MessagingConfig; +use katana_primitives::block::{BlockHash, BlockHashOrNumber, BlockNumber, GasPrices}; +use katana_primitives::chain::ChainId; use katana_primitives::chain_spec::ChainSpec; +use katana_primitives::class::ClassHash; +use katana_primitives::genesis::allocation::GenesisAllocation; +use katana_primitives::genesis::GenesisClass; +use katana_primitives::version::ProtocolVersion; +use katana_primitives::{ContractAddress, Felt}; +use katana_rpc::cors::HeaderValue; use metrics::MetricsConfig; -use rpc::RpcConfig; -use url::Url; +use rpc::{RpcConfig, RpcModulesList}; +use starknet::providers::Url; /// Node configurations. /// @@ -64,3 +77,254 @@ pub struct SequencingConfig { /// Allowing block to only be produced manually. pub no_mining: bool, } + +#[derive(Default, Debug)] +pub struct ConfigBuilder { + config: Config, +} + +impl ConfigBuilder { + pub fn new() -> Self { + ConfigBuilder::default() + } + + pub fn db_dir(&mut self, dir: Option) -> &mut Self { + self.config.db.dir = dir; + self + } + + pub fn fork_url(&mut self, url: Url) -> &mut Self { + self.config.forking.get_or_insert(ForkingConfig { url, block: None }).url = url.clone(); + self + } + + pub fn fork_block(&mut self, block: Option) -> &mut Self { + self.config + .forking + .get_or_insert(ForkingConfig { url: Url::from_str("").unwrap(), block: None }) + .block = block; + self + } + + pub fn rpc_addr(&mut self, addr: IpAddr) -> &mut Self { + self.config.rpc.addr = addr; + self + } + + pub fn rpc_port(&mut self, port: u16) -> &mut Self { + self.config.rpc.port = port; + self + } + + pub fn rpc_max_connections(&mut self, max_connections: u32) -> &mut Self { + self.config.rpc.max_connections = max_connections; + self + } + + pub fn rpc_apis(&mut self, apis: RpcModulesList) -> &mut Self { + self.config.rpc.apis = apis; + self + } + + pub fn rpc_cors_origins(&mut self, cors_origins: Vec) -> &mut Self { + self.config.rpc.cors_origins = cors_origins; + self + } + + pub fn rpc_max_event_page_size(&mut self, max_event_page_size: Option) -> &mut Self { + self.config.rpc.max_event_page_size = max_event_page_size; + self + } + + pub fn rpc_max_proof_keys(&mut self, max_proof_keys: Option) -> &mut Self { + self.config.rpc.max_proof_keys = max_proof_keys; + self + } + + pub fn metrics_addr(&mut self, addr: IpAddr) -> &mut Self { + self.config.metrics.get_or_insert(MetricsConfig { addr, ..Default::default() }).addr = addr; + self + } + + pub fn metrics_port(&mut self, port: u16) -> &mut Self { + self.config.metrics.get_or_insert(MetricsConfig { port, ..Default::default() }).port = port; + self + } + + pub fn execution_invocation_max_steps(&mut self, steps: u32) -> &mut Self { + self.config.execution.invocation_max_steps = steps; + self + } + + pub fn execution_validation_max_steps(&mut self, steps: u32) -> &mut Self { + self.config.execution.validation_max_steps = steps; + self + } + + pub fn execution_max_recursion_depth(&mut self, depth: usize) -> &mut Self { + self.config.execution.max_recursion_depth = depth; + self + } + + pub fn messaging_chain(&mut self, chain: String) -> &mut Self { + self.config + .messaging + .get_or_insert(MessagingConfig { chain, ..Default::default() }) + .chain = chain.clone(); + self + } + + pub fn messaging_rpc_url(&mut self, rpc_url: String) -> &mut Self { + self.config + .messaging + .get_or_insert(MessagingConfig { rpc_url, ..Default::default() }) + .rpc_url = rpc_url.clone(); + self + } + + pub fn messaging_contract_address(&mut self, contract_address: String) -> &mut Self { + self.config + .messaging + .get_or_insert(MessagingConfig { contract_address, ..Default::default() }) + .contract_address = contract_address.clone(); + self + } + + pub fn messaging_sender_address(&mut self, sender_address: String) -> &mut Self { + self.config + .messaging + .get_or_insert(MessagingConfig { sender_address, ..Default::default() }) + .sender_address = sender_address.clone(); + self + } + + pub fn messaging_private_key(&mut self, private_key: String) -> &mut Self { + self.config + .messaging + .get_or_insert(MessagingConfig { private_key, ..Default::default() }) + .private_key = private_key.clone(); + self + } + + pub fn messaging_interval(&mut self, interval: u64) -> &mut Self { + self.config + .messaging + .get_or_insert(MessagingConfig { interval, ..Default::default() }) + .interval = interval; + self + } + + pub fn messaging_from_block(&mut self, from_block: u64) -> &mut Self { + self.config + .messaging + .get_or_insert(MessagingConfig { from_block, ..Default::default() }) + .from_block = from_block; + self + } + + pub fn sequencing_block_time(&mut self, block_time: Option) -> &mut Self { + self.config.sequencing.block_time = block_time; + self + } + + pub fn sequencing_no_mining(&mut self, no_mining: bool) -> &mut Self { + self.config.sequencing.no_mining = no_mining; + self + } + + pub fn dev_fee(&mut self, fee: bool) -> &mut Self { + self.config.dev.fee = fee; + self + } + + pub fn dev_account_validation(&mut self, validation: bool) -> &mut Self { + self.config.dev.account_validation = validation; + self + } + + pub fn dev_fixed_gas_prices(&mut self, gas_prices: Option) -> &mut Self { + self.config.dev.fixed_gas_prices = gas_prices; + self + } + + pub fn dev_fixed_l1_gas_price_config_gas_price(&mut self, gas_price: GasPrices) -> &mut Self { + self.config + .dev + .fixed_gas_prices + .get_or_insert(FixedL1GasPriceConfig { + gas_price: gas_price.clone(), + ..Default::default() + }) + .gas_price = gas_price.clone(); + self + } + + pub fn dev_fixed_l1_gas_price_config_data_gas_price( + &mut self, + gas_price: GasPrices, + ) -> &mut Self { + self.config + .dev + .fixed_gas_prices + .get_or_insert(FixedL1GasPriceConfig { + data_gas_price: gas_price.clone(), + ..Default::default() + }) + .data_gas_price = gas_price.clone(); + self + } + + pub fn l1_provider_url(&mut self, url: Url) -> &mut Self { + self.config.l1_provider_url = Some(url); + self + } + + pub fn chain(&mut self, chain: ChainSpec) -> &mut Self { + self.config.chain = Arc::new(chain); + self + } + + pub fn db(&mut self, db: DbConfig) -> &mut Self { + self.config.db = db; + self + } + + pub fn forking(&mut self, forking: Option) -> &mut Self { + self.config.forking = forking; + self + } + + pub fn rpc(&mut self, rpc: RpcConfig) -> &mut Self { + self.config.rpc = rpc; + self + } + + pub fn metrics(&mut self, metrics: Option) -> &mut Self { + self.config.metrics = metrics; + self + } + + pub fn execution(&mut self, execution: ExecutionConfig) -> &mut Self { + self.config.execution = execution; + self + } + + pub fn messaging(&mut self, messaging: Option) -> &mut Self { + self.config.messaging = messaging; + self + } + + pub fn sequencing(&mut self, sequencing: SequencingConfig) -> &mut Self { + self.config.sequencing = sequencing; + self + } + + pub fn dev(&mut self, dev: DevConfig) -> &mut Self { + self.config.dev = dev; + self + } + + pub fn build(&mut self) -> Config { + self.config.clone() + } +}