Skip to main content

alloy_rpc_types_beacon/
genesis.rs

1//! Types for the beacon genesis endpoint.
2
3use alloy_primitives::{FixedBytes, B256};
4use serde::{Deserialize, Serialize};
5use serde_with::{serde_as, DisplayFromStr};
6
7/// Response from the [`/eth/v1/beacon/genesis`](https://ethereum.github.io/beacon-APIs/#/Beacon/getGenesis) endpoint.
8#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
9pub struct GenesisResponse {
10    /// Container for the genesis data.
11    pub data: GenesisData,
12}
13
14/// Genesis information for the beacon chain.
15#[serde_as]
16#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
17pub struct GenesisData {
18    /// Unix timestamp when the beacon chain started.
19    #[serde_as(as = "DisplayFromStr")]
20    pub genesis_time: u64,
21    /// Root hash of the genesis validator set.
22    pub genesis_validators_root: B256,
23    /// Fork version at genesis.
24    pub genesis_fork_version: FixedBytes<4>,
25}
26
27#[cfg(test)]
28mod tests {
29    use super::*;
30
31    #[test]
32    fn serde_genesis_response() {
33        let s = r#"{
34            "data": {
35                "genesis_time": "1742213400",
36                "genesis_validators_root": "0x212f13fc4df078b6cb7db228f1c8307566dcecf900867401a92023d7ba99cb5f",
37                "genesis_fork_version": "0x10000910"
38            }
39        }"#;
40
41        let genesis_response: GenesisResponse = serde_json::from_str(s).unwrap();
42
43        assert_eq!(genesis_response.data.genesis_time, 1742213400);
44        assert_eq!(
45            genesis_response.data.genesis_validators_root,
46            "0x212f13fc4df078b6cb7db228f1c8307566dcecf900867401a92023d7ba99cb5f"
47                .parse::<B256>()
48                .unwrap()
49        );
50        assert_eq!(
51            genesis_response.data.genesis_fork_version,
52            FixedBytes::from([0x10, 0x00, 0x09, 0x10])
53        );
54    }
55}