superchain_primitives/
chain_config.rs

1//! Chain Config Types
2
3use crate::AddressList;
4use crate::ChainGenesis;
5use crate::SuperchainLevel;
6use alloc::string::String;
7use alloy_primitives::Address;
8
9/// AltDA configuration.
10#[derive(Debug, Clone, Default, Hash, Eq, PartialEq)]
11#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
12#[cfg_attr(any(test, feature = "arbitrary"), derive(arbitrary::Arbitrary))]
13pub struct AltDAConfig {
14    /// AltDA challenge address
15    pub da_challenge_address: Option<Address>,
16    /// AltDA challenge window time (in seconds)
17    pub da_challenge_window: Option<u64>,
18    /// AltDA resolution window time (in seconds)
19    pub da_resolve_window: Option<u64>,
20}
21
22/// Hardfork configuration.
23#[derive(Debug, Clone, Default, Hash, Eq, PartialEq)]
24#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
25#[cfg_attr(any(test, feature = "arbitrary"), derive(arbitrary::Arbitrary))]
26pub struct HardForkConfiguration {
27    /// Canyon hardfork activation time
28    pub canyon_time: Option<u64>,
29    /// Delta hardfork activation time
30    pub delta_time: Option<u64>,
31    /// Ecotone hardfork activation time
32    pub ecotone_time: Option<u64>,
33    /// Fjord hardfork activation time
34    pub fjord_time: Option<u64>,
35    /// Granite hardfork activation time
36    pub granite_time: Option<u64>,
37    /// Holocene hardfork activation time
38    pub holocene_time: Option<u64>,
39}
40
41/// A chain configuration.
42#[derive(Debug, Clone, Default, Hash, Eq, PartialEq)]
43#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
44#[cfg_attr(any(test, feature = "arbitrary"), derive(arbitrary::Arbitrary))]
45pub struct ChainConfig {
46    /// Chain name (e.g. "Base")
47    #[cfg_attr(feature = "serde", serde(rename = "Name"))]
48    pub name: String,
49    /// Chain ID
50    #[cfg_attr(feature = "serde", serde(rename = "l2_chain_id"))]
51    pub chain_id: u64,
52    /// L1 chain ID
53    #[cfg_attr(feature = "serde", serde(skip))]
54    pub l1_chain_id: u64,
55    /// Chain public RPC endpoint
56    #[cfg_attr(feature = "serde", serde(rename = "PublicRPC"))]
57    pub public_rpc: String,
58    /// Chain sequencer RPC endpoint
59    #[cfg_attr(feature = "serde", serde(rename = "SequencerRPC"))]
60    pub sequencer_rpc: String,
61    /// Chain explorer HTTP endpoint
62    #[cfg_attr(feature = "serde", serde(rename = "Explorer"))]
63    pub explorer: String,
64    /// Level of integration with the superchain.
65    #[cfg_attr(feature = "serde", serde(rename = "SuperchainLevel"))]
66    pub superchain_level: SuperchainLevel,
67    /// Time of opt-in to the Superchain.
68    /// If superchain_time is set, hardforks times after superchain_time
69    /// will be inherited from the superchain-wide config.
70    #[cfg_attr(feature = "serde", serde(rename = "SuperchainTime"))]
71    pub superchain_time: Option<u64>,
72    /// Chain-specific batch inbox address
73    #[cfg_attr(feature = "serde", serde(rename = "batch_inbox_address"))]
74    pub batch_inbox_addr: Address,
75    /// Chain-specific genesis information
76    pub genesis: ChainGenesis,
77    /// Superchain is a simple string to identify the superchain.
78    /// This is implied by directory structure, and not encoded in the config file itself.
79    #[cfg_attr(feature = "serde", serde(rename = "Superchain"))]
80    pub superchain: String,
81    /// Chain is a simple string to identify the chain, within its superchain context.
82    /// This matches the resource filename, it is not encoded in the config file itself.
83    #[cfg_attr(feature = "serde", serde(skip))]
84    pub chain: String,
85    /// Hardfork Configuration. These values may override the superchain-wide defaults.
86    #[cfg_attr(feature = "serde", serde(flatten))]
87    pub hardfork_configuration: HardForkConfiguration,
88    /// Optional AltDA feature
89    pub alt_da: Option<AltDAConfig>,
90    /// Addresses
91    #[cfg_attr(feature = "serde", serde(rename = "Addresses"))]
92    pub addresses: Option<AddressList>,
93}
94
95impl ChainConfig {
96    /// Set missing hardfork configurations to the defaults, if the chain has
97    /// a superchain_time set. Defaults are only used if the chain's hardfork
98    /// activated after the superchain_time.
99    pub fn set_missing_fork_configs(&mut self, defaults: &HardForkConfiguration) {
100        let Some(super_time) = self.superchain_time else {
101            return;
102        };
103        let cfg = &mut self.hardfork_configuration;
104
105        if cfg.canyon_time.is_none() && defaults.canyon_time.is_some_and(|t| t > super_time) {
106            cfg.canyon_time = defaults.canyon_time;
107        }
108        if cfg.delta_time.is_none() && defaults.delta_time.is_some_and(|t| t > super_time) {
109            cfg.delta_time = defaults.delta_time;
110        }
111        if cfg.ecotone_time.is_none() && defaults.ecotone_time.is_some_and(|t| t > super_time) {
112            cfg.ecotone_time = defaults.ecotone_time;
113        }
114        if cfg.fjord_time.is_none() && defaults.fjord_time.is_some_and(|t| t > super_time) {
115            cfg.fjord_time = defaults.fjord_time;
116        }
117        if cfg.granite_time.is_none() && defaults.granite_time.is_some_and(|t| t > super_time) {
118            cfg.granite_time = defaults.granite_time;
119        }
120        if cfg.holocene_time.is_none() && defaults.holocene_time.is_some_and(|t| t > super_time) {
121            cfg.holocene_time = defaults.holocene_time;
122        }
123    }
124}