espresso_types/v0/v0_1/
chain_config.rs1use 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#[cfg_attr(
12 feature = "rlp",
13 derive(alloy_rlp::RlpEncodableWrapper, alloy_rlp::RlpDecodableWrapper)
14)]
15#[display("{_0}")]
16pub struct ChainId(pub U256);
17
18pub const DECAF_CHAIN_ID: ChainId = ChainId(U256::from_limbs([0xdecaf, 0, 0, 0]));
20
21#[derive(Hash, Copy, Clone, Debug, Default, Display, PartialEq, Eq, From, Into, Deref)]
22#[display("{_0}")]
23pub struct BlockSize(pub(crate) u64);
24
25#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Serialize, Deserialize)]
27pub struct ChainConfig {
28 pub chain_id: ChainId,
30
31 pub max_block_size: BlockSize,
33
34 pub base_fee: FeeAmount,
36
37 #[serde(with = "ethers_serde::option_address")]
43 pub fee_contract: Option<Address>,
44
45 pub fee_recipient: FeeAccount,
51}
52
53#[derive(Clone, Debug, Copy, PartialEq, Deserialize, Serialize, Eq, Hash)]
54pub struct ResolvableChainConfig {
55 pub(crate) chain_config: Either<ChainConfig, Commitment<ChainConfig>>,
56}
57
58impl Committable for ChainConfig {
59 fn tag() -> String {
60 "CHAIN_CONFIG".to_string()
61 }
62
63 fn commit(&self) -> Commitment<Self> {
64 let comm = committable::RawCommitmentBuilder::new(&Self::tag())
65 .fixed_size_field("chain_id", &self.chain_id.to_fixed_bytes())
66 .u64_field("max_block_size", *self.max_block_size)
67 .fixed_size_field("base_fee", &self.base_fee.to_fixed_bytes())
68 .fixed_size_field("fee_recipient", &self.fee_recipient.to_fixed_bytes());
69 let comm = if let Some(addr) = self.fee_contract {
70 comm.u64_field("fee_contract", 1).fixed_size_bytes(&addr.0)
71 } else {
72 comm.u64_field("fee_contract", 0)
73 };
74 comm.finalize()
75 }
76}
77
78impl ResolvableChainConfig {
79 pub fn commit(&self) -> Commitment<ChainConfig> {
80 match self.chain_config {
81 Either::Left(config) => config.commit(),
82 Either::Right(commitment) => commitment,
83 }
84 }
85 pub fn resolve(self) -> Option<ChainConfig> {
86 match self.chain_config {
87 Either::Left(config) => Some(config),
88 Either::Right(_) => None,
89 }
90 }
91}
92
93impl From<Commitment<ChainConfig>> for ResolvableChainConfig {
94 fn from(value: Commitment<ChainConfig>) -> Self {
95 Self {
96 chain_config: Either::Right(value),
97 }
98 }
99}
100
101impl From<ChainConfig> for ResolvableChainConfig {
102 fn from(value: ChainConfig) -> Self {
103 Self {
104 chain_config: Either::Left(value),
105 }
106 }
107}
108
109impl Default for ChainConfig {
110 fn default() -> Self {
111 Self {
112 chain_id: U256::from(35353).into(), max_block_size: 30720.into(),
114 base_fee: 0.into(),
115 fee_contract: None,
116 fee_recipient: Default::default(),
117 }
118 }
119}