|
| 1 | +use crate::{ |
| 2 | + ConsensusVersion, EthBeaconNodeApiClient, GetGenesisRequest, GetGenesisResponse, |
| 3 | + GetSpecRequest, GetSpecResponse, ValidatorStatus, |
| 4 | +}; |
| 5 | +use chrono::{DateTime, Utc}; |
| 6 | +use std::{collections::HashMap, time}; |
| 7 | + |
| 8 | +/// Error that can occur when using the |
| 9 | +/// [`EthBeaconNodeApiClient`]. |
| 10 | +#[derive(Debug, thiserror::Error)] |
| 11 | +pub enum EthBeaconNodeApiClientError { |
| 12 | + /// Underlying error from [`EthBeaconNodeApiClient`] when |
| 13 | + /// making a request. |
| 14 | + #[error("Request error: {0}")] |
| 15 | + RequestError(#[from] anyhow::Error), |
| 16 | + |
| 17 | + /// Unexpected response, e.g, got an error when an Ok response was expected |
| 18 | + #[error("Unexpected response")] |
| 19 | + UnexpectedResponse, |
| 20 | + |
| 21 | + /// Unexpected type in response |
| 22 | + #[error("Unexpected type in response")] |
| 23 | + UnexpectedType, |
| 24 | + |
| 25 | + /// Zero slot duration or slots per epoch in network spec |
| 26 | + #[error("Zero slot duration or slots per epoch in network spec")] |
| 27 | + ZeroSlotDurationOrSlotsPerEpoch, |
| 28 | +} |
| 29 | + |
| 30 | +/// Type alias for validator index. |
| 31 | +pub type ValidatorIndex = u64; |
| 32 | + |
| 33 | +const FORKS: [ConsensusVersion; 6] = [ |
| 34 | + ConsensusVersion::Altair, |
| 35 | + ConsensusVersion::Bellatrix, |
| 36 | + ConsensusVersion::Capella, |
| 37 | + ConsensusVersion::Deneb, |
| 38 | + ConsensusVersion::Electra, |
| 39 | + ConsensusVersion::Fulu, |
| 40 | +]; |
| 41 | + |
| 42 | +/// The schedule of given fork, containing the fork version and the epoch at |
| 43 | +/// which it activates. |
| 44 | +#[derive(Debug, Clone, PartialEq, Eq)] |
| 45 | +pub struct ForkSchedule { |
| 46 | + /// The fork version, as a 4-byte array. |
| 47 | + pub version: [u8; 4], |
| 48 | + /// The epoch at which the fork activates. |
| 49 | + pub epoch: u64, |
| 50 | +} |
| 51 | + |
| 52 | +impl ValidatorStatus { |
| 53 | + /// Returns true if the validator is in one of the active states. |
| 54 | + pub fn is_active(&self) -> bool { |
| 55 | + matches!( |
| 56 | + self, |
| 57 | + ValidatorStatus::ActiveOngoing |
| 58 | + | ValidatorStatus::ActiveExiting |
| 59 | + | ValidatorStatus::ActiveSlashed |
| 60 | + ) |
| 61 | + } |
| 62 | +} |
| 63 | + |
| 64 | +impl EthBeaconNodeApiClient { |
| 65 | + /// Fetches the genesis time. |
| 66 | + pub async fn fetch_genesis_time(&self) -> Result<DateTime<Utc>, EthBeaconNodeApiClientError> { |
| 67 | + let genesis = match self.get_genesis(GetGenesisRequest {}).await? { |
| 68 | + GetGenesisResponse::Ok(genesis) => genesis, |
| 69 | + _ => return Err(EthBeaconNodeApiClientError::UnexpectedResponse), |
| 70 | + }; |
| 71 | + |
| 72 | + genesis |
| 73 | + .data |
| 74 | + .genesis_time |
| 75 | + .parse() |
| 76 | + .map_err(|_| EthBeaconNodeApiClientError::UnexpectedType) |
| 77 | + .and_then(|timestamp| { |
| 78 | + DateTime::from_timestamp(timestamp, 0) |
| 79 | + .ok_or(EthBeaconNodeApiClientError::UnexpectedType) |
| 80 | + }) |
| 81 | + } |
| 82 | + |
| 83 | + /// Fetches the slot duration and slots per epoch. |
| 84 | + pub async fn fetch_slots_config( |
| 85 | + &self, |
| 86 | + ) -> Result<(time::Duration, u64), EthBeaconNodeApiClientError> { |
| 87 | + let spec = match self.get_spec(GetSpecRequest {}).await? { |
| 88 | + GetSpecResponse::Ok(spec) => spec, |
| 89 | + _ => return Err(EthBeaconNodeApiClientError::UnexpectedResponse), |
| 90 | + }; |
| 91 | + |
| 92 | + let slot_duration = spec |
| 93 | + .data |
| 94 | + .as_object() |
| 95 | + .and_then(|o| o.get("SECONDS_PER_SLOT")) |
| 96 | + .and_then(|v| v.as_str()) |
| 97 | + .and_then(|s| s.parse::<u64>().ok()) |
| 98 | + .ok_or(EthBeaconNodeApiClientError::UnexpectedType) |
| 99 | + .map(time::Duration::from_secs)?; |
| 100 | + |
| 101 | + let slots_per_epoch = spec |
| 102 | + .data |
| 103 | + .as_object() |
| 104 | + .and_then(|o| o.get("SLOTS_PER_EPOCH")) |
| 105 | + .and_then(|v| v.as_str()) |
| 106 | + .and_then(|s| s.parse::<u64>().ok()) |
| 107 | + .ok_or(EthBeaconNodeApiClientError::UnexpectedType)?; |
| 108 | + |
| 109 | + if slot_duration == time::Duration::ZERO || slots_per_epoch == 0 { |
| 110 | + return Err(EthBeaconNodeApiClientError::ZeroSlotDurationOrSlotsPerEpoch); |
| 111 | + } |
| 112 | + |
| 113 | + Ok((slot_duration, slots_per_epoch)) |
| 114 | + } |
| 115 | + |
| 116 | + /// Fetches the fork schedule for all known forks. |
| 117 | + pub async fn fetch_fork_config( |
| 118 | + &self, |
| 119 | + ) -> Result<HashMap<ConsensusVersion, ForkSchedule>, EthBeaconNodeApiClientError> { |
| 120 | + fn fetch_fork( |
| 121 | + fork: &ConsensusVersion, |
| 122 | + spec_data: &serde_json::Value, |
| 123 | + ) -> Result<ForkSchedule, EthBeaconNodeApiClientError> { |
| 124 | + let version_field = format!("{}_FORK_VERSION", fork.to_string().to_uppercase()); |
| 125 | + let version = spec_data |
| 126 | + .as_object() |
| 127 | + .and_then(|o| o.get(&version_field)) |
| 128 | + .and_then(|f| f.as_str()) |
| 129 | + .and_then(|hex| { |
| 130 | + let hex = hex.strip_prefix("0x").unwrap_or(hex); |
| 131 | + hex::decode(hex).ok() |
| 132 | + }) |
| 133 | + .and_then(|bytes| bytes.try_into().ok()) |
| 134 | + .ok_or(EthBeaconNodeApiClientError::UnexpectedType)?; |
| 135 | + |
| 136 | + let epoch_field = format!("{}_FORK_EPOCH", fork.to_string().to_uppercase()); |
| 137 | + let epoch = spec_data |
| 138 | + .as_object() |
| 139 | + .and_then(|o| o.get(&epoch_field)) |
| 140 | + .and_then(|v| v.as_str()) |
| 141 | + .and_then(|s| s.parse::<u64>().ok()) |
| 142 | + .ok_or(EthBeaconNodeApiClientError::UnexpectedType)?; |
| 143 | + |
| 144 | + Ok(ForkSchedule { version, epoch }) |
| 145 | + } |
| 146 | + |
| 147 | + let spec = match self.get_spec(GetSpecRequest {}).await? { |
| 148 | + GetSpecResponse::Ok(spec) => spec, |
| 149 | + _ => return Err(EthBeaconNodeApiClientError::UnexpectedResponse), |
| 150 | + }; |
| 151 | + |
| 152 | + let mut result = HashMap::new(); |
| 153 | + for fork in FORKS.into_iter() { |
| 154 | + let fork_schedule = fetch_fork(&fork, &spec.data)?; |
| 155 | + result.insert(fork, fork_schedule); |
| 156 | + } |
| 157 | + |
| 158 | + Ok(result) |
| 159 | + } |
| 160 | +} |
0 commit comments