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