Skip to content

Commit 5ba62f9

Browse files
authored
feat(eips): allow custom system contracts in EIP-7910 (#3765)
1 parent 2ac99b9 commit 5ba62f9

File tree

2 files changed

+27
-18
lines changed

2 files changed

+27
-18
lines changed

crates/eips/Cargo.toml

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,6 @@ alloy-eip7928.workspace = true
3232
alloy-primitives = { workspace = true, features = ["rlp"] }
3333
alloy-rlp = { workspace = true, features = ["derive"] }
3434
either.workspace = true
35-
thiserror.workspace = true
3635

3736
# serde
3837
alloy-serde = { workspace = true, optional = true }
@@ -91,7 +90,6 @@ std = [
9190
"sha2?/std",
9291
"either/std",
9392
"serde_with?/std",
94-
"thiserror/std",
9593
"borsh?/std"
9694
]
9795
serde = [

crates/eips/src/eip7910.rs

Lines changed: 27 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,12 @@
11
//! Implementation of [`EIP-7910`](https://github.com/ethereum/EIPs/blob/master/EIPS/eip-7910.md).
22
33
use crate::{eip2935, eip4788, eip6110, eip7002, eip7251, eip7840::BlobParams};
4-
use alloc::{borrow::ToOwned, collections::BTreeMap, string::String};
4+
use alloc::{
5+
collections::BTreeMap,
6+
string::{String, ToString},
7+
};
58
use alloy_primitives::{Address, Bytes};
6-
use core::{fmt, str};
9+
use core::{cmp::Ordering, convert::Infallible, fmt, str};
710

811
/// Response type for `eth_config`
912
#[derive(Clone, Debug, PartialEq)]
@@ -75,7 +78,7 @@ pub struct EthForkConfig {
7578
}
7679

7780
/// System-level contracts for [`EthForkConfig`].
78-
#[derive(PartialEq, Eq, PartialOrd, Ord, Clone, Copy, Debug)]
81+
#[derive(PartialEq, Eq, Clone, Debug)]
7982
#[cfg_attr(feature = "serde", derive(serde_with::SerializeDisplay, serde_with::DeserializeFromStr))]
8083
pub enum SystemContract {
8184
/// Beacon roots system contract.
@@ -88,6 +91,20 @@ pub enum SystemContract {
8891
HistoryStorage,
8992
/// Withdrawal requests predeploy system contract.
9093
WithdrawalRequestPredeploy,
94+
/// A custom system contract not defined by a known EIP.
95+
Other(String),
96+
}
97+
98+
impl PartialOrd for SystemContract {
99+
fn partial_cmp(&self, other: &Self) -> Option<Ordering> {
100+
Some(self.cmp(other))
101+
}
102+
}
103+
104+
impl Ord for SystemContract {
105+
fn cmp(&self, other: &Self) -> Ordering {
106+
self.to_string().cmp(&other.to_string())
107+
}
91108
}
92109

93110
impl fmt::Display for SystemContract {
@@ -98,13 +115,14 @@ impl fmt::Display for SystemContract {
98115
Self::DepositContract => "DEPOSIT_CONTRACT",
99116
Self::HistoryStorage => "HISTORY_STORAGE",
100117
Self::WithdrawalRequestPredeploy => "WITHDRAWAL_REQUEST_PREDEPLOY",
118+
Self::Other(name) => return write!(f, "{name}"),
101119
};
102120
write!(f, "{str}_ADDRESS")
103121
}
104122
}
105123

106124
impl str::FromStr for SystemContract {
107-
type Err = ParseSystemContractError;
125+
type Err = Infallible;
108126

109127
fn from_str(s: &str) -> Result<Self, Self::Err> {
110128
let system_contract = match s {
@@ -113,7 +131,7 @@ impl str::FromStr for SystemContract {
113131
"DEPOSIT_CONTRACT_ADDRESS" => Self::DepositContract,
114132
"HISTORY_STORAGE_ADDRESS" => Self::HistoryStorage,
115133
"WITHDRAWAL_REQUEST_PREDEPLOY_ADDRESS" => Self::WithdrawalRequestPredeploy,
116-
_ => return Err(ParseSystemContractError::Unknown(s.to_owned())),
134+
_ => Self::Other(s.into()),
117135
};
118136
Ok(system_contract)
119137
}
@@ -150,14 +168,6 @@ impl SystemContract {
150168
}
151169
}
152170

153-
/// Parse error for [`SystemContract`].
154-
#[derive(Debug, thiserror::Error)]
155-
pub enum ParseSystemContractError {
156-
/// System contract unknown.
157-
#[error("unknown system contract: {0}")]
158-
Unknown(String),
159-
}
160-
161171
#[cfg(test)]
162172
mod tests {
163173
use super::*;
@@ -180,10 +190,11 @@ mod tests {
180190
#[cfg(feature = "serde")]
181191
#[test]
182192
fn system_contract_serde_roundtrip() {
183-
for contract in SystemContract::ALL {
193+
for contract in &SystemContract::ALL {
184194
assert_eq!(
185-
contract,
186-
serde_json::from_value(serde_json::to_value(contract).unwrap()).unwrap()
195+
*contract,
196+
serde_json::from_value::<SystemContract>(serde_json::to_value(contract).unwrap())
197+
.unwrap()
187198
);
188199
}
189200
}

0 commit comments

Comments
 (0)