Skip to main content

alloy_rpc_types_beacon/
config.rs

1//! Types for the beacon config endpoints.
2//!
3//! See <https://ethereum.github.io/beacon-APIs/#/Config>
4
5use alloy_primitives::Address;
6use serde::{Deserialize, Serialize};
7use serde_with::{serde_as, DisplayFromStr};
8use std::collections::BTreeMap;
9
10/// Response from the [`/eth/v1/config/deposit_contract`](https://ethereum.github.io/beacon-APIs/#/Config/getDepositContract) endpoint.
11#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
12pub struct DepositContractResponse {
13    /// The deposit contract data.
14    pub data: DepositContract,
15}
16
17/// Deposit contract information.
18#[serde_as]
19#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
20pub struct DepositContract {
21    /// The chain ID of the network the deposit contract is deployed on.
22    #[serde_as(as = "DisplayFromStr")]
23    pub chain_id: u64,
24    /// The address of the deposit contract.
25    pub address: Address,
26}
27
28/// Response from the [`/eth/v1/config/fork_schedule`](https://ethereum.github.io/beacon-APIs/#/Config/getForkSchedule) endpoint.
29#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
30pub struct ForkScheduleResponse {
31    /// The list of forks in the fork schedule.
32    pub data: Vec<crate::fork::Fork>,
33}
34
35/// Response from the [`/eth/v1/config/spec`](https://ethereum.github.io/beacon-APIs/#/Config/getSpec) endpoint.
36#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
37pub struct SpecResponse {
38    /// The spec configuration as key-value pairs.
39    pub data: BTreeMap<String, String>,
40}
41
42#[cfg(test)]
43mod tests {
44    use super::*;
45    use alloy_primitives::FixedBytes;
46
47    #[test]
48    fn serde_deposit_contract_response() {
49        let s = r#"{
50            "data": {
51                "chain_id": "1",
52                "address": "0x00000000219ab540356cbb839cbe05303d7705fa"
53            }
54        }"#;
55
56        let resp: DepositContractResponse = serde_json::from_str(s).unwrap();
57        assert_eq!(resp.data.chain_id, 1);
58        assert_eq!(
59            resp.data.address,
60            "0x00000000219ab540356cBB839Cbe05303d7705Fa".parse::<Address>().unwrap()
61        );
62
63        let serialized = serde_json::to_string(&resp).unwrap();
64        let deserialized: DepositContractResponse = serde_json::from_str(&serialized).unwrap();
65        assert_eq!(resp, deserialized);
66    }
67
68    #[test]
69    fn serde_fork_schedule_response() {
70        let s = r#"{
71            "data": [
72                {
73                    "previous_version": "0x00000000",
74                    "current_version": "0x01000000",
75                    "epoch": "0"
76                },
77                {
78                    "previous_version": "0x01000000",
79                    "current_version": "0x02000000",
80                    "epoch": "74240"
81                }
82            ]
83        }"#;
84
85        let resp: ForkScheduleResponse = serde_json::from_str(s).unwrap();
86        assert_eq!(resp.data.len(), 2);
87        assert_eq!(resp.data[0].epoch, 0);
88        assert_eq!(resp.data[0].current_version, FixedBytes::from([0x01, 0x00, 0x00, 0x00]));
89        assert_eq!(resp.data[1].epoch, 74240);
90
91        let serialized = serde_json::to_string(&resp).unwrap();
92        let deserialized: ForkScheduleResponse = serde_json::from_str(&serialized).unwrap();
93        assert_eq!(resp, deserialized);
94    }
95
96    #[test]
97    fn serde_spec_response() {
98        let s = r#"{
99            "data": {
100                "MAX_VALIDATORS_PER_COMMITTEE": "2048",
101                "SECONDS_PER_SLOT": "12",
102                "DEPOSIT_CONTRACT_ADDRESS": "0x00000000219ab540356cbb839cbe05303d7705fa"
103            }
104        }"#;
105
106        let resp: SpecResponse = serde_json::from_str(s).unwrap();
107        assert_eq!(resp.data.len(), 3);
108        assert_eq!(resp.data.get("SECONDS_PER_SLOT").unwrap(), "12");
109        assert_eq!(resp.data.get("MAX_VALIDATORS_PER_COMMITTEE").unwrap(), "2048");
110
111        let serialized = serde_json::to_string(&resp).unwrap();
112        let deserialized: SpecResponse = serde_json::from_str(&serialized).unwrap();
113        assert_eq!(resp, deserialized);
114    }
115}