Skip to content

Commit

Permalink
Add replace_candidates function
Browse files Browse the repository at this point in the history
  • Loading branch information
macladson committed Oct 29, 2024
1 parent 3c6d294 commit 02eb997
Showing 1 changed file with 38 additions and 1 deletion.
39 changes: 38 additions & 1 deletion validator_client/src/beacon_node_fallback.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -461,6 +462,42 @@ impl<T: SlotClock, E: EthSpec> BeaconNodeFallback<T, E> {
(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<SensitiveUrl>,
use_long_timeouts: bool,
) -> Result<Vec<SensitiveUrl>, 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<CandidateBeaconNode<E>> = new_list
.clone()
.into_iter()
.enumerate()
.map(|(index, url)| {
CandidateBeaconNode::<E>::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
Expand Down

0 comments on commit 02eb997

Please sign in to comment.