-
Notifications
You must be signed in to change notification settings - Fork 11
Expand file tree
/
Copy pathargs.rs
More file actions
937 lines (790 loc) · 33 KB
/
args.rs
File metadata and controls
937 lines (790 loc) · 33 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
//! Katana node CLI options and configuration.
use std::path::PathBuf;
use std::sync::Arc;
use alloy_primitives::U256;
#[cfg(feature = "server")]
use anyhow::bail;
use anyhow::{Context, Result};
pub use clap::Parser;
use katana_chain_spec::rollup::ChainConfigDir;
use katana_chain_spec::ChainSpec;
use katana_core::constants::DEFAULT_SEQUENCER_ADDRESS;
use katana_genesis::allocation::DevAllocationsGenerator;
use katana_genesis::constant::DEFAULT_PREFUNDED_ACCOUNT_BALANCE;
use katana_messaging::MessagingConfig;
use katana_node::config::db::DbConfig;
use katana_node::config::dev::{DevConfig, FixedL1GasPriceConfig};
use katana_node::config::execution::ExecutionConfig;
use katana_node::config::fork::ForkingConfig;
#[cfg(feature = "server")]
use katana_node::config::gateway::GatewayConfig;
#[cfg(all(feature = "server", feature = "grpc"))]
use katana_node::config::grpc::GrpcConfig;
use katana_node::config::metrics::MetricsConfig;
#[cfg(feature = "cartridge")]
use katana_node::config::paymaster::PaymasterConfig;
use katana_node::config::rpc::RpcConfig;
#[cfg(feature = "server")]
use katana_node::config::rpc::{RpcModuleKind, RpcModulesList};
use katana_node::config::sequencing::SequencingConfig;
#[cfg(feature = "tee")]
use katana_node::config::tee::TeeConfig;
use katana_node::config::Config;
use katana_node::Node;
use serde::{Deserialize, Serialize};
use tracing::info;
use url::Url;
use crate::file::NodeArgsConfig;
use crate::options::*;
use crate::utils::{self, parse_chain_config_dir, parse_seed};
pub(crate) const LOG_TARGET: &str = "katana::cli";
#[derive(Parser, Debug, Serialize, Deserialize, Default, Clone, PartialEq)]
#[command(next_help_heading = "Sequencer node options")]
pub struct SequencerNodeArgs {
/// Don't print anything on startup.
#[arg(long)]
pub silent: bool,
/// Path to the chain configuration file.
#[arg(long, hide = true)]
#[arg(value_parser = parse_chain_config_dir)]
pub chain: Option<ChainConfigDir>,
/// Disable auto and interval mining, and mine on demand instead via an endpoint.
#[arg(long)]
#[arg(conflicts_with = "block_time")]
pub no_mining: bool,
/// Block time in milliseconds for interval mining.
#[arg(short, long)]
#[arg(value_name = "MILLISECONDS")]
pub block_time: Option<u64>,
#[arg(long = "sequencing.block-max-cairo-steps")]
#[arg(value_name = "TOTAL")]
pub block_cairo_steps_limit: Option<u64>,
/// Directory path of the database to initialize from.
///
/// The path must either be an empty directory or a directory which already contains a
/// previously initialized Katana database.
#[arg(long, alias = "db-dir")]
#[arg(value_name = "PATH")]
pub data_dir: Option<PathBuf>,
/// Configuration file
#[arg(long)]
pub config: Option<PathBuf>,
/// Configure the messaging with an other chain.
///
/// Configure the messaging to allow Katana listening/sending messages on a
/// settlement chain that can be Ethereum or an other Starknet sequencer.
#[arg(long)]
#[arg(value_name = "PATH")]
#[arg(value_parser = katana_messaging::MessagingConfig::parse)]
#[arg(conflicts_with = "chain")]
pub messaging: Option<MessagingConfig>,
#[arg(long = "l1.provider", value_name = "URL", alias = "l1-provider")]
#[arg(help = "The Ethereum RPC provider to sample the gas prices from to enable the gas \
price oracle.")]
pub l1_provider_url: Option<Url>,
#[command(flatten)]
pub logging: LoggingOptions,
#[command(flatten)]
pub tracer: TracerOptions,
#[cfg(feature = "server")]
#[command(flatten)]
pub metrics: MetricsOptions,
#[cfg(feature = "server")]
#[command(flatten)]
pub gateway: GatewayOptions,
#[cfg(feature = "server")]
#[command(flatten)]
pub server: ServerOptions,
#[command(flatten)]
pub starknet: StarknetOptions,
#[command(flatten)]
pub gpo: GasPriceOracleOptions,
#[command(flatten)]
pub forking: ForkingOptions,
#[command(flatten)]
pub development: DevOptions,
#[cfg(feature = "explorer")]
#[command(flatten)]
pub explorer: ExplorerOptions,
#[cfg(feature = "cartridge")]
#[command(flatten)]
pub cartridge: CartridgeOptions,
#[cfg(feature = "tee")]
#[command(flatten)]
pub tee: TeeOptions,
#[cfg(all(feature = "server", feature = "grpc"))]
#[command(flatten)]
pub grpc: GrpcOptions,
}
impl SequencerNodeArgs {
pub async fn execute(&self) -> Result<()> {
let logging = katana_tracing::LoggingConfig {
stdout_format: self.logging.stdout.stdout_format,
stdout_color: self.logging.stdout.color,
file_enabled: self.logging.file.enabled,
file_format: self.logging.file.file_format,
file_directory: self.logging.file.directory.clone(),
file_max_files: self.logging.file.max_files,
};
katana_tracing::init(logging, self.tracer_config()).await?;
self.start_node().await
}
async fn start_node(&self) -> Result<()> {
// Build the node
let config = self.config()?;
if config.forking.is_some() {
let node = Node::build_forked(config).await.context("failed to build forked node")?;
if !self.silent {
utils::print_intro(self, &node.backend().chain_spec);
}
// Launch the node
let handle = node.launch().await.context("failed to launch forked node")?;
// Wait until an OS signal (ie SIGINT, SIGTERM) is received or the node is shutdown.
tokio::select! {
_ = katana_utils::wait_shutdown_signals() => {
// Gracefully shutdown the node before exiting
handle.stop().await?;
},
_ = handle.stopped() => { }
}
} else {
let node = Node::build(config).context("failed to build node")?;
if !self.silent {
utils::print_intro(self, &node.backend().chain_spec);
}
// Launch the node
let handle = node.launch().await.context("failed to launch node")?;
// Wait until an OS signal (ie SIGINT, SIGTERM) is received or the node is shutdown.
tokio::select! {
_ = katana_utils::wait_shutdown_signals() => {
// Gracefully shutdown the node before exiting
handle.stop().await?;
},
_ = handle.stopped() => { }
}
}
info!("Shutting down.");
Ok(())
}
pub fn config(&self) -> Result<katana_node::config::Config> {
let db = self.db_config();
let rpc = self.rpc_config()?;
let dev = self.dev_config();
let (chain, cs_messaging) = self.chain_spec()?;
let metrics = self.metrics_config();
let gateway = self.gateway_config();
#[cfg(all(feature = "server", feature = "grpc"))]
let grpc = self.grpc_config();
let forking = self.forking_config()?;
let execution = self.execution_config();
let sequencing = self.sequencer_config();
// the `katana init` will automatically generate a messaging config. so if katana is run
// with `--chain` then the `--messaging` flag is not required. this is temporary and
// the messagign config will eventually be removed slowly.
let messaging = if cs_messaging.is_some() { cs_messaging } else { self.messaging.clone() };
Ok(Config {
db,
dev,
rpc,
#[cfg(feature = "grpc")]
grpc,
chain,
metrics,
gateway,
forking,
execution,
messaging,
sequencing,
#[cfg(feature = "cartridge")]
paymaster: self.cartridge_config(),
#[cfg(feature = "tee")]
tee: self.tee_config(),
})
}
fn sequencer_config(&self) -> SequencingConfig {
SequencingConfig {
block_time: self.block_time,
no_mining: self.no_mining,
block_cairo_steps_limit: self.block_cairo_steps_limit,
}
}
pub fn rpc_config(&self) -> Result<RpcConfig> {
#[cfg(feature = "server")]
{
use std::time::Duration;
#[allow(unused_mut)]
let mut modules = if let Some(modules) = &self.server.http_modules {
// TODO: This check should be handled in the `katana-node` level. Right now if you
// instantiate katana programmatically, you can still add the dev module without
// enabling dev mode.
//
// We only allow the `dev` module in dev mode (ie `--dev` flag)
if !self.development.dev && modules.contains(&RpcModuleKind::Dev) {
bail!("The `dev` module can only be enabled in dev mode (ie `--dev` flag)")
}
modules.clone()
} else {
// Expose the default modules if none is specified.
let mut modules = RpcModulesList::default();
// Ensures the `--dev` flag enabled the dev module.
if self.development.dev {
modules.add(RpcModuleKind::Dev);
}
modules
};
// The cartridge rpc must be enabled if the paymaster is enabled.
// We put it here so that even when the individual api are explicitly specified
// (ie `--rpc.api`) we guarantee that the cartridge rpc is enabled.
#[cfg(feature = "cartridge")]
if self.cartridge.paymaster {
modules.add(RpcModuleKind::Cartridge);
}
// The TEE rpc must be enabled if a TEE provider is specified.
// We put it here so that even when the individual api are explicitly specified
// (ie `--rpc.api`) we guarantee that the tee rpc is enabled.
#[cfg(feature = "tee")]
if self.tee.tee_provider.is_some() {
modules.add(RpcModuleKind::Tee);
}
let cors_origins = self.server.http_cors_origins.clone();
Ok(RpcConfig {
apis: modules,
port: self.server.http_port,
addr: self.server.http_addr,
max_connections: self.server.max_connections,
max_concurrent_estimate_fee_requests: None,
max_request_body_size: None,
max_response_body_size: None,
timeout: self.server.timeout.map(Duration::from_secs),
cors_origins,
#[cfg(feature = "explorer")]
explorer: self.explorer.explorer,
max_event_page_size: Some(self.server.max_event_page_size),
max_proof_keys: Some(self.server.max_proof_keys),
max_call_gas: Some(self.server.max_call_gas),
})
}
#[cfg(not(feature = "server"))]
{
Ok(RpcConfig::default())
}
}
fn chain_spec(&self) -> Result<(Arc<ChainSpec>, Option<MessagingConfig>)> {
if let Some(path) = &self.chain {
let mut cs = katana_chain_spec::rollup::read(path)?;
cs.genesis.sequencer_address = *DEFAULT_SEQUENCER_ADDRESS;
let messaging_config = MessagingConfig::from_chain_spec(&cs);
Ok((Arc::new(ChainSpec::Rollup(cs)), Some(messaging_config)))
}
// exclusively for development mode
else {
let mut chain_spec = katana_chain_spec::dev::DEV_UNALLOCATED.clone();
if let Some(id) = self.starknet.environment.chain_id {
chain_spec.id = id;
}
if let Some(genesis) = &self.starknet.genesis {
chain_spec.genesis = genesis.clone();
} else {
chain_spec.genesis.sequencer_address = *DEFAULT_SEQUENCER_ADDRESS;
}
// Generate dev accounts.
// If `cartridge` is enabled, the first account will be the paymaster.
let accounts = DevAllocationsGenerator::new(self.development.total_accounts)
.with_seed(parse_seed(&self.development.seed))
.with_balance(U256::from(DEFAULT_PREFUNDED_ACCOUNT_BALANCE))
.generate();
chain_spec.genesis.extend_allocations(accounts.into_iter().map(|(k, v)| (k, v.into())));
#[cfg(feature = "cartridge")]
if self.cartridge.controllers || self.cartridge.paymaster {
katana_slot_controller::add_controller_classes(&mut chain_spec.genesis);
katana_slot_controller::add_vrf_provider_class(&mut chain_spec.genesis);
}
Ok((Arc::new(ChainSpec::Dev(chain_spec)), None))
}
}
fn dev_config(&self) -> DevConfig {
let mut fixed_gas_prices = None;
if let Some(eth) = self.gpo.l2_eth_gas_price {
let prices = fixed_gas_prices.get_or_insert(FixedL1GasPriceConfig::default());
prices.l2_gas_prices.eth = eth;
}
if let Some(strk) = self.gpo.l2_strk_gas_price {
let prices = fixed_gas_prices.get_or_insert(FixedL1GasPriceConfig::default());
prices.l2_gas_prices.strk = strk;
}
if let Some(eth) = self.gpo.l1_eth_gas_price {
let prices = fixed_gas_prices.get_or_insert(FixedL1GasPriceConfig::default());
prices.l1_gas_prices.eth = eth;
}
if let Some(strk) = self.gpo.l1_strk_gas_price {
let prices = fixed_gas_prices.get_or_insert(FixedL1GasPriceConfig::default());
prices.l1_gas_prices.strk = strk;
}
if let Some(eth) = self.gpo.l1_eth_data_gas_price {
let prices = fixed_gas_prices.get_or_insert(FixedL1GasPriceConfig::default());
prices.l1_data_gas_prices.eth = eth;
}
if let Some(strk) = self.gpo.l1_strk_data_gas_price {
let prices = fixed_gas_prices.get_or_insert(FixedL1GasPriceConfig::default());
prices.l1_data_gas_prices.strk = strk;
}
DevConfig {
fixed_gas_prices,
fee: !self.development.no_fee,
account_validation: !self.development.no_account_validation,
}
}
fn execution_config(&self) -> ExecutionConfig {
ExecutionConfig {
invocation_max_steps: self.starknet.environment.invoke_max_steps,
validation_max_steps: self.starknet.environment.validate_max_steps,
#[cfg(feature = "native")]
compile_native: self.starknet.environment.compile_native,
..Default::default()
}
}
fn forking_config(&self) -> Result<Option<ForkingConfig>> {
if let Some(ref url) = self.forking.fork_provider {
let cfg = ForkingConfig { url: url.clone(), block: self.forking.fork_block };
return Ok(Some(cfg));
}
Ok(None)
}
fn db_config(&self) -> DbConfig {
DbConfig { dir: self.data_dir.clone() }
}
fn metrics_config(&self) -> Option<MetricsConfig> {
#[cfg(feature = "server")]
if self.metrics.metrics {
Some(MetricsConfig { addr: self.metrics.metrics_addr, port: self.metrics.metrics_port })
} else {
None
}
#[cfg(not(feature = "server"))]
None
}
fn gateway_config(&self) -> Option<GatewayConfig> {
#[cfg(feature = "server")]
if self.gateway.gateway_enable {
use std::time::Duration;
Some(GatewayConfig {
addr: self.gateway.gateway_addr,
port: self.gateway.gateway_port,
timeout: Some(Duration::from_secs(self.gateway.gateway_timeout)),
})
} else {
None
}
#[cfg(not(feature = "server"))]
None
}
#[cfg(all(feature = "server", feature = "grpc"))]
fn grpc_config(&self) -> Option<GrpcConfig> {
if self.grpc.grpc_enable {
use std::time::Duration;
Some(GrpcConfig {
addr: self.grpc.grpc_addr,
port: self.grpc.grpc_port,
timeout: self.grpc.grpc_timeout.map(Duration::from_secs),
})
} else {
None
}
}
#[cfg(feature = "cartridge")]
fn cartridge_config(&self) -> Option<PaymasterConfig> {
if self.cartridge.paymaster {
Some(PaymasterConfig { cartridge_api_url: self.cartridge.api.clone() })
} else {
None
}
}
#[cfg(feature = "tee")]
fn tee_config(&self) -> Option<TeeConfig> {
self.tee.tee_provider.map(|provider_type| TeeConfig { provider_type })
}
/// Parse the node config from the command line arguments and the config file,
/// and merge them together prioritizing the command line arguments.
pub fn with_config_file(mut self) -> Result<Self> {
let config = if let Some(path) = &self.config {
NodeArgsConfig::read(path)?
} else {
return Ok(self);
};
// the CLI (self) takes precedence over the config file.
// Currently, the merge is made at the top level of the commands.
// We may add recursive merging in the future.
if !self.no_mining {
self.no_mining = config.no_mining.unwrap_or_default();
}
if self.block_time.is_none() {
self.block_time = config.block_time;
}
if self.data_dir.is_none() {
self.data_dir = config.data_dir;
}
if self.logging == LoggingOptions::default() {
if let Some(logging) = config.logging {
self.logging = logging;
}
}
if self.messaging.is_none() {
self.messaging = config.messaging;
}
#[cfg(feature = "server")]
{
self.server.merge(config.server.as_ref());
if self.metrics == MetricsOptions::default() {
if let Some(metrics) = config.metrics {
self.metrics = metrics;
}
}
}
#[cfg(all(feature = "server", feature = "grpc"))]
{
self.grpc.merge(config.grpc.as_ref());
}
self.starknet.merge(config.starknet.as_ref());
self.development.merge(config.development.as_ref());
if self.gpo == GasPriceOracleOptions::default() {
if let Some(gpo) = config.gpo {
self.gpo = gpo;
}
}
if self.forking == ForkingOptions::default() {
if let Some(forking) = config.forking {
self.forking = forking;
}
}
#[cfg(feature = "cartridge")]
{
self.cartridge.merge(config.cartridge.as_ref());
}
#[cfg(feature = "explorer")]
{
if !self.explorer.explorer {
if let Some(explorer) = &config.explorer {
self.explorer.explorer = explorer.explorer;
}
}
}
Ok(self)
}
fn tracer_config(&self) -> Option<katana_tracing::TracerConfig> {
self.tracer.config()
}
}
#[cfg(test)]
mod test {
use std::str::FromStr;
use assert_matches::assert_matches;
use katana_gas_price_oracle::{
DEFAULT_ETH_L1_DATA_GAS_PRICE, DEFAULT_ETH_L1_GAS_PRICE, DEFAULT_ETH_L2_GAS_PRICE,
DEFAULT_STRK_L1_DATA_GAS_PRICE, DEFAULT_STRK_L1_GAS_PRICE,
};
use katana_node::config::execution::{
DEFAULT_INVOCATION_MAX_STEPS, DEFAULT_VALIDATION_MAX_STEPS,
};
use katana_node::config::rpc::RpcModuleKind;
use katana_primitives::chain::ChainId;
use katana_primitives::{address, felt, Felt};
use super::*;
#[test]
fn test_starknet_config_default() {
let args = SequencerNodeArgs::parse_from(["katana"]);
let config = args.config().unwrap();
assert!(config.dev.fee);
assert!(config.dev.account_validation);
assert!(config.forking.is_none());
assert_eq!(config.execution.invocation_max_steps, DEFAULT_INVOCATION_MAX_STEPS);
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);
}
#[test]
fn test_starknet_config_custom() {
let args = SequencerNodeArgs::parse_from([
"katana",
"--dev",
"--dev.no-fee",
"--dev.no-account-validation",
"--chain-id",
"SN_GOERLI",
"--invoke-max-steps",
"200",
"--validate-max-steps",
"100",
"--data-dir",
"/path/to/db",
]);
let config = args.config().unwrap();
assert!(!config.dev.fee);
assert!(!config.dev.account_validation);
assert_eq!(config.execution.invocation_max_steps, 200);
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);
}
#[test]
fn test_db_dir_alias() {
// --db-dir should work as an alias for --data-dir
let args = SequencerNodeArgs::parse_from(["katana", "--db-dir", "/path/to/db"]);
let config = args.config().unwrap();
assert_eq!(config.db.dir, Some(PathBuf::from("/path/to/db")));
}
#[test]
fn custom_fixed_gas_prices() {
let config = SequencerNodeArgs::parse_from(["katana"]).config().unwrap();
assert!(config.dev.fixed_gas_prices.is_none());
let config = SequencerNodeArgs::parse_from(["katana", "--gpo.l1-eth-gas-price", "10"])
.config()
.unwrap();
assert_matches!(config.dev.fixed_gas_prices, Some(prices) => {
assert_eq!(prices.l1_gas_prices.eth.get(), 10);
assert_eq!(prices.l1_gas_prices.strk, DEFAULT_ETH_L2_GAS_PRICE);
assert_eq!(prices.l1_data_gas_prices.eth, DEFAULT_ETH_L1_DATA_GAS_PRICE);
assert_eq!(prices.l1_data_gas_prices.strk, DEFAULT_STRK_L1_DATA_GAS_PRICE);
});
let config = SequencerNodeArgs::parse_from(["katana", "--gpo.l1-strk-gas-price", "20"])
.config()
.unwrap();
assert_matches!(config.dev.fixed_gas_prices, Some(prices) => {
assert_eq!(prices.l1_gas_prices.eth, DEFAULT_ETH_L1_GAS_PRICE);
assert_eq!(prices.l1_gas_prices.strk.get(), 20);
assert_eq!(prices.l1_data_gas_prices.eth, DEFAULT_ETH_L1_DATA_GAS_PRICE);
assert_eq!(prices.l1_data_gas_prices.strk, DEFAULT_STRK_L1_DATA_GAS_PRICE);
});
let config = SequencerNodeArgs::parse_from(["katana", "--gpo.l1-eth-data-gas-price", "2"])
.config()
.unwrap();
assert_matches!(config.dev.fixed_gas_prices, Some(prices) => {
assert_eq!(prices.l1_gas_prices.eth, DEFAULT_ETH_L1_GAS_PRICE);
assert_eq!(prices.l1_gas_prices.strk, DEFAULT_STRK_L1_GAS_PRICE);
assert_eq!(prices.l1_data_gas_prices.eth.get(), 2);
assert_eq!(prices.l1_data_gas_prices.strk, DEFAULT_STRK_L1_DATA_GAS_PRICE);
});
let config = SequencerNodeArgs::parse_from(["katana", "--gpo.l1-strk-data-gas-price", "2"])
.config()
.unwrap();
assert_matches!(config.dev.fixed_gas_prices, Some(prices) => {
assert_eq!(prices.l1_gas_prices.eth, DEFAULT_ETH_L1_GAS_PRICE);
assert_eq!(prices.l1_gas_prices.strk, DEFAULT_STRK_L1_GAS_PRICE);
assert_eq!(prices.l1_data_gas_prices.eth, DEFAULT_ETH_L1_DATA_GAS_PRICE);
assert_eq!(prices.l1_data_gas_prices.strk.get(), 2);
});
let config = SequencerNodeArgs::parse_from([
"katana",
"--gpo.l1-eth-gas-price",
"10",
"--gpo.l1-strk-data-gas-price",
"2",
])
.config()
.unwrap();
assert_matches!(config.dev.fixed_gas_prices, Some(prices) => {
assert_eq!(prices.l1_gas_prices.eth.get(), 10);
assert_eq!(prices.l1_gas_prices.strk, DEFAULT_STRK_L1_GAS_PRICE);
assert_eq!(prices.l1_data_gas_prices.eth, DEFAULT_ETH_L1_DATA_GAS_PRICE);
assert_eq!(prices.l1_data_gas_prices.strk.get(), 2);
});
// Set all the gas prices options
let config = SequencerNodeArgs::parse_from([
"katana",
"--gpo.l1-eth-gas-price",
"10",
"--gpo.l1-strk-gas-price",
"20",
"--gpo.l1-eth-data-gas-price",
"1",
"--gpo.l1-strk-data-gas-price",
"2",
])
.config()
.unwrap();
assert_matches!(config.dev.fixed_gas_prices, Some(prices) => {
assert_eq!(prices.l1_gas_prices.eth.get(), 10);
assert_eq!(prices.l1_gas_prices.strk.get(), 20);
assert_eq!(prices.l1_data_gas_prices.eth.get(), 1);
assert_eq!(prices.l1_data_gas_prices.strk.get(), 2);
})
}
#[test]
fn genesis_with_fixed_gas_prices() {
let config = SequencerNodeArgs::parse_from([
"katana",
"--genesis",
"./test-data/genesis.json",
"--gpo.l1-eth-gas-price",
"100",
"--gpo.l1-strk-gas-price",
"200",
"--gpo.l1-eth-data-gas-price",
"111",
"--gpo.l1-strk-data-gas-price",
"222",
])
.config()
.unwrap();
assert_eq!(config.chain.genesis().number, 0);
assert_eq!(config.chain.genesis().parent_hash, felt!("0x999"));
assert_eq!(config.chain.genesis().timestamp, 5123512314);
assert_eq!(config.chain.genesis().state_root, felt!("0x99"));
assert_eq!(config.chain.genesis().sequencer_address, address!("0x100"));
assert_eq!(config.chain.genesis().gas_prices.eth.get(), 9999);
assert_eq!(config.chain.genesis().gas_prices.strk.get(), 8888);
assert_matches!(config.dev.fixed_gas_prices, Some(prices) => {
assert_eq!(prices.l1_gas_prices.eth.get(), 100);
assert_eq!(prices.l1_gas_prices.strk.get(), 200);
assert_eq!(prices.l1_data_gas_prices.eth.get(), 111);
assert_eq!(prices.l1_data_gas_prices.strk.get(), 222);
})
}
#[test]
fn config_from_file_and_cli() {
// CLI args must take precedence over the config file.
let content = r#"
[gpo]
l1_eth_gas_price = "0xfe"
l1_strk_gas_price = "200"
l1_eth_data_gas_price = "111"
l1_strk_data_gas_price = "222"
[dev]
total_accounts = 20
[starknet.env]
validate_max_steps = 500
invoke_max_steps = 9988
chain_id.Named = "Mainnet"
[explorer]
explorer = true
"#;
let path = std::env::temp_dir().join("katana-config.json");
std::fs::write(&path, content).unwrap();
let path_str = path.to_string_lossy().to_string();
let args = vec![
"katana",
"--config",
path_str.as_str(),
"--genesis",
"./test-data/genesis.json",
"--validate-max-steps",
"1234",
"--dev",
"--dev.no-fee",
"--chain-id",
"0x123",
];
let config = SequencerNodeArgs::parse_from(args.clone())
.with_config_file()
.unwrap()
.config()
.unwrap();
assert_eq!(config.execution.validation_max_steps, 1234);
assert_eq!(config.execution.invocation_max_steps, 9988);
assert!(!config.dev.fee);
assert_matches!(config.dev.fixed_gas_prices, Some(prices) => {
assert_eq!(prices.l1_gas_prices.eth.get(), 254);
assert_eq!(prices.l1_gas_prices.strk.get(), 200);
assert_eq!(prices.l1_data_gas_prices.eth.get(), 111);
assert_eq!(prices.l1_data_gas_prices.strk.get(), 222);
});
assert_eq!(config.chain.genesis().number, 0);
assert_eq!(config.chain.genesis().parent_hash, felt!("0x999"));
assert_eq!(config.chain.genesis().timestamp, 5123512314);
assert_eq!(config.chain.genesis().state_root, felt!("0x99"));
assert_eq!(config.chain.genesis().sequencer_address, address!("0x100"));
assert_eq!(config.chain.genesis().gas_prices.eth.get(), 9999);
assert_eq!(config.chain.genesis().gas_prices.strk.get(), 8888);
assert_eq!(config.chain.id(), ChainId::Id(Felt::from_str("0x123").unwrap()));
#[cfg(feature = "explorer")]
assert!(config.rpc.explorer);
}
#[test]
#[cfg(feature = "server")]
fn parse_cors_origins() {
use katana_rpc_server::cors::HeaderValue;
let config = SequencerNodeArgs::parse_from([
"katana",
"--http.cors_origins",
"*,http://localhost:3000,https://example.com",
])
.config()
.unwrap();
let cors_origins = config.rpc.cors_origins;
assert_eq!(cors_origins.len(), 3);
assert!(cors_origins.contains(&HeaderValue::from_static("*")));
assert!(cors_origins.contains(&HeaderValue::from_static("http://localhost:3000")));
assert!(cors_origins.contains(&HeaderValue::from_static("https://example.com")));
}
#[test]
fn http_modules() {
// If the `--http.api` isn't specified, only starknet module will be exposed.
let config = SequencerNodeArgs::parse_from(["katana"]).config().unwrap();
let modules = config.rpc.apis;
assert_eq!(modules.len(), 1);
assert!(modules.contains(&RpcModuleKind::Starknet));
// If the `--http.api` is specified, only the ones in the list will be exposed.
let config =
SequencerNodeArgs::parse_from(["katana", "--http.api", "starknet"]).config().unwrap();
let modules = config.rpc.apis;
assert_eq!(modules.len(), 1);
assert!(modules.contains(&RpcModuleKind::Starknet));
// Specifiying the dev module without enabling dev mode is forbidden.
let err = SequencerNodeArgs::parse_from(["katana", "--http.api", "starknet,dev"])
.config()
.unwrap_err();
assert!(err
.to_string()
.contains("The `dev` module can only be enabled in dev mode (ie `--dev` flag)"));
}
#[test]
fn test_dev_api_enabled() {
let args = SequencerNodeArgs::parse_from(["katana", "--dev"]);
let config = args.config().unwrap();
assert!(config.rpc.apis.contains(&RpcModuleKind::Dev));
}
#[cfg(feature = "cartridge")]
#[test]
fn cartridge_paymaster() {
let args = SequencerNodeArgs::parse_from(["katana", "--cartridge.paymaster"]);
let config = args.config().unwrap();
// Verify cartridge module is automatically enabled
assert!(config.rpc.apis.contains(&RpcModuleKind::Cartridge));
// Test with paymaster explicitly specified in RPC modules
let args = SequencerNodeArgs::parse_from([
"katana",
"--cartridge.paymaster",
"--http.api",
"starknet",
]);
let config = args.config().unwrap();
// Verify cartridge module is still enabled even when not in explicit RPC list
assert!(config.rpc.apis.contains(&RpcModuleKind::Cartridge));
assert!(config.rpc.apis.contains(&RpcModuleKind::Starknet));
// Verify that all the Controller classes are added to the genesis
use katana_slot_controller::{
ControllerLatest, ControllerV104, ControllerV105, ControllerV106, ControllerV107,
ControllerV108, ControllerV109,
};
assert!(config.chain.genesis().classes.contains_key(&ControllerV104::HASH));
assert!(config.chain.genesis().classes.contains_key(&ControllerV105::HASH));
assert!(config.chain.genesis().classes.contains_key(&ControllerV106::HASH));
assert!(config.chain.genesis().classes.contains_key(&ControllerV107::HASH));
assert!(config.chain.genesis().classes.contains_key(&ControllerV108::HASH));
assert!(config.chain.genesis().classes.contains_key(&ControllerV109::HASH));
assert!(config.chain.genesis().classes.contains_key(&ControllerLatest::HASH));
// Test without paymaster enabled
let args = SequencerNodeArgs::parse_from(["katana"]);
let config = args.config().unwrap();
// Verify cartridge module is not enabled by default
assert!(!config.rpc.apis.contains(&RpcModuleKind::Cartridge));
assert!(!config.chain.genesis().classes.contains_key(&ControllerV104::HASH));
assert!(!config.chain.genesis().classes.contains_key(&ControllerV105::HASH));
assert!(!config.chain.genesis().classes.contains_key(&ControllerV106::HASH));
assert!(!config.chain.genesis().classes.contains_key(&ControllerV107::HASH));
assert!(!config.chain.genesis().classes.contains_key(&ControllerV108::HASH));
assert!(!config.chain.genesis().classes.contains_key(&ControllerV109::HASH));
assert!(!config.chain.genesis().classes.contains_key(&ControllerLatest::HASH));
}
}