Skip to main content

espresso_types/v0/v0_1/
chain_config.rs

1use alloy::primitives::{Address, U256};
2use alloy_compat::ethers_serde;
3use committable::{Commitment, Committable};
4use derive_more::{Deref, Display, From, Into};
5use itertools::Either;
6use serde::{Deserialize, Serialize};
7
8use crate::{FeeAccount, FeeAmount};
9
10#[derive(Default, Hash, Copy, Clone, Debug, Display, PartialEq, Eq, From, Into)]
11#[display("{_0}")]
12pub struct ChainId(pub U256);
13
14#[derive(Hash, Copy, Clone, Debug, Default, Display, PartialEq, Eq, From, Into, Deref)]
15#[display("{_0}")]
16pub struct BlockSize(pub(crate) u64);
17
18/// Global variables for an Espresso blockchain.
19#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Serialize, Deserialize)]
20pub struct ChainConfig {
21    /// Espresso chain ID
22    pub chain_id: ChainId,
23
24    /// Maximum size in bytes of a block
25    pub max_block_size: BlockSize,
26
27    /// Minimum fee in WEI per byte of payload
28    pub base_fee: FeeAmount,
29
30    /// Fee contract address on L1.
31    ///
32    /// This is optional so that fees can easily be toggled on/off, with no need to deploy a
33    /// contract when they are off. In a future release, after fees are switched on and thoroughly
34    /// tested, this may be made mandatory.
35    #[serde(with = "ethers_serde::option_address")]
36    pub fee_contract: Option<Address>,
37
38    /// Account that receives sequencing fees.
39    ///
40    /// This account in the Espresso fee ledger will always receive every fee paid in Espresso,
41    /// regardless of whether or not their is a `fee_contract` deployed. Once deployed, the fee
42    /// contract can decide what to do with tokens locked in this account in Espresso.
43    pub fee_recipient: FeeAccount,
44}
45
46#[derive(Clone, Debug, Copy, PartialEq, Deserialize, Serialize, Eq, Hash)]
47pub struct ResolvableChainConfig {
48    pub(crate) chain_config: Either<ChainConfig, Commitment<ChainConfig>>,
49}
50
51impl Committable for ChainConfig {
52    fn tag() -> String {
53        "CHAIN_CONFIG".to_string()
54    }
55
56    fn commit(&self) -> Commitment<Self> {
57        let comm = committable::RawCommitmentBuilder::new(&Self::tag())
58            .fixed_size_field("chain_id", &self.chain_id.to_fixed_bytes())
59            .u64_field("max_block_size", *self.max_block_size)
60            .fixed_size_field("base_fee", &self.base_fee.to_fixed_bytes())
61            .fixed_size_field("fee_recipient", &self.fee_recipient.to_fixed_bytes());
62        let comm = if let Some(addr) = self.fee_contract {
63            comm.u64_field("fee_contract", 1).fixed_size_bytes(&addr.0)
64        } else {
65            comm.u64_field("fee_contract", 0)
66        };
67        comm.finalize()
68    }
69}
70
71impl ResolvableChainConfig {
72    pub fn commit(&self) -> Commitment<ChainConfig> {
73        match self.chain_config {
74            Either::Left(config) => config.commit(),
75            Either::Right(commitment) => commitment,
76        }
77    }
78    pub fn resolve(self) -> Option<ChainConfig> {
79        match self.chain_config {
80            Either::Left(config) => Some(config),
81            Either::Right(_) => None,
82        }
83    }
84}
85
86impl From<Commitment<ChainConfig>> for ResolvableChainConfig {
87    fn from(value: Commitment<ChainConfig>) -> Self {
88        Self {
89            chain_config: Either::Right(value),
90        }
91    }
92}
93
94impl From<ChainConfig> for ResolvableChainConfig {
95    fn from(value: ChainConfig) -> Self {
96        Self {
97            chain_config: Either::Left(value),
98        }
99    }
100}
101
102impl Default for ChainConfig {
103    fn default() -> Self {
104        Self {
105            chain_id: U256::from(35353).into(), // arbitrarily chosen chain ID
106            max_block_size: 30720.into(),
107            base_fee: 0.into(),
108            fee_contract: None,
109            fee_recipient: Default::default(),
110        }
111    }
112}