diff --git a/common/eth2/src/lib.rs b/common/eth2/src/lib.rs index 522c6414ea..01c61826ab 100644 --- a/common/eth2/src/lib.rs +++ b/common/eth2/src/lib.rs @@ -48,6 +48,21 @@ pub const CONSENSUS_BLOCK_VALUE_HEADER: &str = "Eth-Consensus-Block-Value"; pub const CONTENT_TYPE_HEADER: &str = "Content-Type"; pub const SSZ_CONTENT_TYPE_HEADER: &str = "application/octet-stream"; +/// Specific optimized timeout constants for HTTP requests involved in different validator duties. +/// This can help ensure that proper endpoint fallback occurs. +const HTTP_ATTESTATION_TIMEOUT_QUOTIENT: u32 = 4; +const HTTP_ATTESTER_DUTIES_TIMEOUT_QUOTIENT: u32 = 4; +const HTTP_ATTESTATION_SUBSCRIPTIONS_TIMEOUT_QUOTIENT: u32 = 24; +const HTTP_LIVENESS_TIMEOUT_QUOTIENT: u32 = 4; +const HTTP_PROPOSAL_TIMEOUT_QUOTIENT: u32 = 2; +const HTTP_PROPOSER_DUTIES_TIMEOUT_QUOTIENT: u32 = 4; +const HTTP_SYNC_COMMITTEE_CONTRIBUTION_TIMEOUT_QUOTIENT: u32 = 4; +const HTTP_SYNC_DUTIES_TIMEOUT_QUOTIENT: u32 = 4; +const HTTP_GET_BEACON_BLOCK_SSZ_TIMEOUT_QUOTIENT: u32 = 4; +const HTTP_GET_DEBUG_BEACON_STATE_QUOTIENT: u32 = 4; +const HTTP_GET_DEPOSIT_SNAPSHOT_QUOTIENT: u32 = 4; +const HTTP_GET_VALIDATOR_BLOCK_TIMEOUT_QUOTIENT: u32 = 4; + #[derive(Debug)] pub enum Error { /// The `reqwest` client raised an error. @@ -151,6 +166,25 @@ impl Timeouts { get_validator_block: timeout, } } + + pub fn use_optimized_timeouts(base_timeout: Duration) -> Self { + Timeouts { + attestation: base_timeout / HTTP_ATTESTATION_TIMEOUT_QUOTIENT, + attester_duties: base_timeout / HTTP_ATTESTER_DUTIES_TIMEOUT_QUOTIENT, + attestation_subscriptions: base_timeout + / HTTP_ATTESTATION_SUBSCRIPTIONS_TIMEOUT_QUOTIENT, + liveness: base_timeout / HTTP_LIVENESS_TIMEOUT_QUOTIENT, + proposal: base_timeout / HTTP_PROPOSAL_TIMEOUT_QUOTIENT, + proposer_duties: base_timeout / HTTP_PROPOSER_DUTIES_TIMEOUT_QUOTIENT, + sync_committee_contribution: base_timeout + / HTTP_SYNC_COMMITTEE_CONTRIBUTION_TIMEOUT_QUOTIENT, + sync_duties: base_timeout / HTTP_SYNC_DUTIES_TIMEOUT_QUOTIENT, + get_beacon_blocks_ssz: base_timeout / HTTP_GET_BEACON_BLOCK_SSZ_TIMEOUT_QUOTIENT, + get_debug_beacon_states: base_timeout / HTTP_GET_DEBUG_BEACON_STATE_QUOTIENT, + get_deposit_snapshot: base_timeout / HTTP_GET_DEPOSIT_SNAPSHOT_QUOTIENT, + get_validator_block: base_timeout / HTTP_GET_VALIDATOR_BLOCK_TIMEOUT_QUOTIENT, + } + } } /// A wrapper around `reqwest::Client` which provides convenience methods for interfacing with a diff --git a/validator_client/src/beacon_node_fallback.rs b/validator_client/src/beacon_node_fallback.rs index e5fe419983..58e76a8015 100644 --- a/validator_client/src/beacon_node_fallback.rs +++ b/validator_client/src/beacon_node_fallback.rs @@ -9,8 +9,9 @@ use crate::beacon_node_health::{ use crate::check_synced::check_node_health; use crate::http_metrics::metrics::{inc_counter_vec, ENDPOINT_ERRORS, ENDPOINT_REQUESTS}; use environment::RuntimeContext; -use eth2::BeaconNodeHttpClient; +use eth2::{BeaconNodeHttpClient, Timeouts}; use futures::future; +use sensitive_url::SensitiveUrl; use serde::{ser::SerializeStruct, Deserialize, Serialize, Serializer}; use slog::{debug, error, warn, Logger}; use slot_clock::SlotClock; @@ -461,6 +462,42 @@ impl BeaconNodeFallback { (candidate_info, num_available, num_synced) } + /// Update the list of candidates with a new list. + /// Returns `Ok(new_list)` if the update was successful. + /// Returns `Err(some_err)` if the list is empty. + pub async fn replace_candidates( + &self, + new_list: Vec, + use_long_timeouts: bool, + ) -> Result, String> { + if new_list.is_empty() { + return Err("Beacon Node list cannot be empty".to_string()); + } + + let timeouts: Timeouts = if new_list.len() == 1 || use_long_timeouts { + Timeouts::set_all(Duration::from_secs(self.spec.seconds_per_slot)) + } else { + Timeouts::use_optimized_timeouts(Duration::from_secs(self.spec.seconds_per_slot)) + }; + + let new_candidates: Vec> = new_list + .clone() + .into_iter() + .enumerate() + .map(|(index, url)| { + CandidateBeaconNode::::new( + BeaconNodeHttpClient::new(url, timeouts.clone()), + index, + ) + }) + .collect(); + + let mut candidates = self.candidates.write().await; + *candidates = new_candidates; + + Ok(new_list) + } + /// Loop through ALL candidates in `self.candidates` and update their sync status. /// /// It is possible for a node to return an unsynced status while continuing to serve diff --git a/validator_client/src/lib.rs b/validator_client/src/lib.rs index 9a02ffdefb..979e8bc374 100644 --- a/validator_client/src/lib.rs +++ b/validator_client/src/lib.rs @@ -72,21 +72,6 @@ const RETRY_DELAY: Duration = Duration::from_secs(2); /// The time between polls when waiting for genesis. const WAITING_FOR_GENESIS_POLL_TIME: Duration = Duration::from_secs(12); -/// Specific timeout constants for HTTP requests involved in different validator duties. -/// This can help ensure that proper endpoint fallback occurs. -const HTTP_ATTESTATION_TIMEOUT_QUOTIENT: u32 = 4; -const HTTP_ATTESTER_DUTIES_TIMEOUT_QUOTIENT: u32 = 4; -const HTTP_ATTESTATION_SUBSCRIPTIONS_TIMEOUT_QUOTIENT: u32 = 24; -const HTTP_LIVENESS_TIMEOUT_QUOTIENT: u32 = 4; -const HTTP_PROPOSAL_TIMEOUT_QUOTIENT: u32 = 2; -const HTTP_PROPOSER_DUTIES_TIMEOUT_QUOTIENT: u32 = 4; -const HTTP_SYNC_COMMITTEE_CONTRIBUTION_TIMEOUT_QUOTIENT: u32 = 4; -const HTTP_SYNC_DUTIES_TIMEOUT_QUOTIENT: u32 = 4; -const HTTP_GET_BEACON_BLOCK_SSZ_TIMEOUT_QUOTIENT: u32 = 4; -const HTTP_GET_DEBUG_BEACON_STATE_QUOTIENT: u32 = 4; -const HTTP_GET_DEPOSIT_SNAPSHOT_QUOTIENT: u32 = 4; -const HTTP_GET_VALIDATOR_BLOCK_TIMEOUT_QUOTIENT: u32 = 4; - const DOPPELGANGER_SERVICE_NAME: &str = "doppelganger"; #[derive(Clone)] @@ -322,23 +307,7 @@ impl ProductionValidatorClient { log, "Fallback endpoints are available, using optimized timeouts."; ); - Timeouts { - attestation: slot_duration / HTTP_ATTESTATION_TIMEOUT_QUOTIENT, - attester_duties: slot_duration / HTTP_ATTESTER_DUTIES_TIMEOUT_QUOTIENT, - attestation_subscriptions: slot_duration - / HTTP_ATTESTATION_SUBSCRIPTIONS_TIMEOUT_QUOTIENT, - liveness: slot_duration / HTTP_LIVENESS_TIMEOUT_QUOTIENT, - proposal: slot_duration / HTTP_PROPOSAL_TIMEOUT_QUOTIENT, - proposer_duties: slot_duration / HTTP_PROPOSER_DUTIES_TIMEOUT_QUOTIENT, - sync_committee_contribution: slot_duration - / HTTP_SYNC_COMMITTEE_CONTRIBUTION_TIMEOUT_QUOTIENT, - sync_duties: slot_duration / HTTP_SYNC_DUTIES_TIMEOUT_QUOTIENT, - get_beacon_blocks_ssz: slot_duration - / HTTP_GET_BEACON_BLOCK_SSZ_TIMEOUT_QUOTIENT, - get_debug_beacon_states: slot_duration / HTTP_GET_DEBUG_BEACON_STATE_QUOTIENT, - get_deposit_snapshot: slot_duration / HTTP_GET_DEPOSIT_SNAPSHOT_QUOTIENT, - get_validator_block: slot_duration / HTTP_GET_VALIDATOR_BLOCK_TIMEOUT_QUOTIENT, - } + Timeouts::use_optimized_timeouts(slot_duration) } else { Timeouts::set_all(slot_duration) };