|
| 1 | +//! Provides tools for checking if a node is ready for the Electra upgrade and following merge |
| 2 | +//! transition. |
| 3 | +
|
| 4 | +use crate::{BeaconChain, BeaconChainTypes}; |
| 5 | +use execution_layer::http::{ |
| 6 | + ENGINE_FORKCHOICE_UPDATED_V3, ENGINE_GET_PAYLOAD_V3, ENGINE_NEW_PAYLOAD_V3, |
| 7 | +}; |
| 8 | +use serde::{Deserialize, Serialize}; |
| 9 | +use std::fmt; |
| 10 | +use std::time::Duration; |
| 11 | +use types::*; |
| 12 | + |
| 13 | +/// The time before the Electra fork when we will start issuing warnings about preparation. |
| 14 | +use super::merge_readiness::SECONDS_IN_A_WEEK; |
| 15 | +pub const ELECTRA_READINESS_PREPARATION_SECONDS: u64 = SECONDS_IN_A_WEEK * 2; |
| 16 | +pub const ENGINE_CAPABILITIES_REFRESH_INTERVAL: u64 = 300; |
| 17 | + |
| 18 | +#[derive(Debug, Serialize, Deserialize)] |
| 19 | +#[serde(rename_all = "snake_case")] |
| 20 | +#[serde(tag = "type")] |
| 21 | +pub enum ElectraReadiness { |
| 22 | + /// The execution engine is electra-enabled (as far as we can tell) |
| 23 | + Ready, |
| 24 | + /// We are connected to an execution engine which doesn't support the V3 engine api methods |
| 25 | + V3MethodsNotSupported { error: String }, |
| 26 | + /// The transition configuration with the EL failed, there might be a problem with |
| 27 | + /// connectivity, authentication or a difference in configuration. |
| 28 | + ExchangeCapabilitiesFailed { error: String }, |
| 29 | + /// The user has not configured an execution endpoint |
| 30 | + NoExecutionEndpoint, |
| 31 | +} |
| 32 | + |
| 33 | +impl fmt::Display for ElectraReadiness { |
| 34 | + fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { |
| 35 | + match self { |
| 36 | + ElectraReadiness::Ready => { |
| 37 | + write!(f, "This node appears ready for Electra.") |
| 38 | + } |
| 39 | + ElectraReadiness::ExchangeCapabilitiesFailed { error } => write!( |
| 40 | + f, |
| 41 | + "Could not exchange capabilities with the \ |
| 42 | + execution endpoint: {}", |
| 43 | + error |
| 44 | + ), |
| 45 | + ElectraReadiness::NoExecutionEndpoint => write!( |
| 46 | + f, |
| 47 | + "The --execution-endpoint flag is not specified, this is a \ |
| 48 | + requirement post-merge" |
| 49 | + ), |
| 50 | + ElectraReadiness::V3MethodsNotSupported { error } => write!( |
| 51 | + f, |
| 52 | + "Execution endpoint does not support Electra methods: {}", |
| 53 | + error |
| 54 | + ), |
| 55 | + } |
| 56 | + } |
| 57 | +} |
| 58 | + |
| 59 | +impl<T: BeaconChainTypes> BeaconChain<T> { |
| 60 | + /// Returns `true` if electra epoch is set and Electra fork has occurred or will |
| 61 | + /// occur within `ELECTRA_READINESS_PREPARATION_SECONDS` |
| 62 | + pub fn is_time_to_prepare_for_electra(&self, current_slot: Slot) -> bool { |
| 63 | + if let Some(electra_epoch) = self.spec.electra_fork_epoch { |
| 64 | + let electra_slot = electra_epoch.start_slot(T::EthSpec::slots_per_epoch()); |
| 65 | + let electra_readiness_preparation_slots = |
| 66 | + ELECTRA_READINESS_PREPARATION_SECONDS / self.spec.seconds_per_slot; |
| 67 | + // Return `true` if Electra has happened or is within the preparation time. |
| 68 | + current_slot + electra_readiness_preparation_slots > electra_slot |
| 69 | + } else { |
| 70 | + // The Electra fork epoch has not been defined yet, no need to prepare. |
| 71 | + false |
| 72 | + } |
| 73 | + } |
| 74 | + |
| 75 | + /// Attempts to connect to the EL and confirm that it is ready for electra. |
| 76 | + pub async fn check_electra_readiness(&self) -> ElectraReadiness { |
| 77 | + if let Some(el) = self.execution_layer.as_ref() { |
| 78 | + match el |
| 79 | + .get_engine_capabilities(Some(Duration::from_secs( |
| 80 | + ENGINE_CAPABILITIES_REFRESH_INTERVAL, |
| 81 | + ))) |
| 82 | + .await |
| 83 | + { |
| 84 | + Err(e) => { |
| 85 | + // The EL was either unreachable or responded with an error |
| 86 | + ElectraReadiness::ExchangeCapabilitiesFailed { |
| 87 | + error: format!("{:?}", e), |
| 88 | + } |
| 89 | + } |
| 90 | + Ok(capabilities) => { |
| 91 | + // TODO(electra): Update in the event we get V4s. |
| 92 | + let mut missing_methods = String::from("Required Methods Unsupported:"); |
| 93 | + let mut all_good = true; |
| 94 | + if !capabilities.get_payload_v3 { |
| 95 | + missing_methods.push(' '); |
| 96 | + missing_methods.push_str(ENGINE_GET_PAYLOAD_V3); |
| 97 | + all_good = false; |
| 98 | + } |
| 99 | + if !capabilities.forkchoice_updated_v3 { |
| 100 | + missing_methods.push(' '); |
| 101 | + missing_methods.push_str(ENGINE_FORKCHOICE_UPDATED_V3); |
| 102 | + all_good = false; |
| 103 | + } |
| 104 | + if !capabilities.new_payload_v3 { |
| 105 | + missing_methods.push(' '); |
| 106 | + missing_methods.push_str(ENGINE_NEW_PAYLOAD_V3); |
| 107 | + all_good = false; |
| 108 | + } |
| 109 | + |
| 110 | + if all_good { |
| 111 | + ElectraReadiness::Ready |
| 112 | + } else { |
| 113 | + ElectraReadiness::V3MethodsNotSupported { |
| 114 | + error: missing_methods, |
| 115 | + } |
| 116 | + } |
| 117 | + } |
| 118 | + } |
| 119 | + } else { |
| 120 | + ElectraReadiness::NoExecutionEndpoint |
| 121 | + } |
| 122 | + } |
| 123 | +} |
0 commit comments