Migrate validator client to clap derive (#6300)

Partially #5900


  Migrate the validator client cli to clap derive
This commit is contained in:
Eitan Seri-Levi
2025-02-03 23:08:31 +03:00
committed by GitHub
parent 95cec45c38
commit 7e4b27c922
18 changed files with 653 additions and 773 deletions

View File

@@ -9,6 +9,7 @@ name = "beacon_node_fallback"
path = "src/lib.rs"
[dependencies]
clap = { workspace = true }
environment = { workspace = true }
eth2 = { workspace = true }
futures = { workspace = true }

View File

@@ -1,11 +1,9 @@
use super::CandidateError;
use eth2::BeaconNodeHttpClient;
use itertools::Itertools;
use serde::{Deserialize, Serialize};
use slog::{warn, Logger};
use std::cmp::Ordering;
use std::fmt::{Debug, Display, Formatter};
use std::str::FromStr;
use types::Slot;
/// Sync distances between 0 and DEFAULT_SYNC_TOLERANCE are considered `synced`.
@@ -53,29 +51,6 @@ impl Default for BeaconNodeSyncDistanceTiers {
}
}
impl FromStr for BeaconNodeSyncDistanceTiers {
type Err = String;
fn from_str(s: &str) -> Result<Self, String> {
let values: (u64, u64, u64) = s
.split(',')
.map(|s| {
s.parse()
.map_err(|e| format!("Invalid sync distance modifier: {e:?}"))
})
.collect::<Result<Vec<_>, _>>()?
.into_iter()
.collect_tuple()
.ok_or("Invalid number of sync distance modifiers".to_string())?;
Ok(BeaconNodeSyncDistanceTiers {
synced: Slot::new(values.0),
small: Slot::new(values.0 + values.1),
medium: Slot::new(values.0 + values.1 + values.2),
})
}
}
impl BeaconNodeSyncDistanceTiers {
/// Takes a given sync distance and determines its tier based on the `sync_tolerance` defined by
/// the CLI.
@@ -90,6 +65,17 @@ impl BeaconNodeSyncDistanceTiers {
SyncDistanceTier::Large
}
}
pub fn from_vec(tiers: &[u64]) -> Result<Self, String> {
if tiers.len() != 3 {
return Err("Invalid number of sync distance modifiers".to_string());
}
Ok(BeaconNodeSyncDistanceTiers {
synced: Slot::new(tiers[0]),
small: Slot::new(tiers[0] + tiers[1]),
medium: Slot::new(tiers[0] + tiers[1] + tiers[2]),
})
}
}
/// Execution Node health metrics.
@@ -320,7 +306,6 @@ mod tests {
SyncDistanceTier,
};
use crate::Config;
use std::str::FromStr;
use types::Slot;
#[test]
@@ -423,7 +408,7 @@ mod tests {
// medium 9..=12
// large: 13..
let distance_tiers = BeaconNodeSyncDistanceTiers::from_str("4,4,4").unwrap();
let distance_tiers = BeaconNodeSyncDistanceTiers::from_vec(&[4, 4, 4]).unwrap();
let synced_low = new_distance_tier(0, &distance_tiers);
let synced_high = new_distance_tier(4, &distance_tiers);

View File

@@ -7,6 +7,7 @@ use beacon_node_health::{
check_node_health, BeaconNodeHealth, BeaconNodeSyncDistanceTiers, ExecutionEngineHealth,
IsOptimistic, SyncDistanceTier,
};
use clap::ValueEnum;
use environment::RuntimeContext;
use eth2::BeaconNodeHttpClient;
use futures::future;
@@ -20,7 +21,8 @@ use std::future::Future;
use std::marker::PhantomData;
use std::sync::Arc;
use std::time::{Duration, Instant};
use strum::{EnumString, EnumVariantNames};
use std::vec::Vec;
use strum::EnumVariantNames;
use tokio::{sync::RwLock, time::sleep};
use types::{ChainSpec, Config as ConfigSpec, EthSpec, Slot};
use validator_metrics::{inc_counter_vec, ENDPOINT_ERRORS, ENDPOINT_REQUESTS};
@@ -727,9 +729,10 @@ async fn sort_nodes_by_health<E: EthSpec>(nodes: &mut Vec<CandidateBeaconNode<E>
}
/// Serves as a cue for `BeaconNodeFallback` to tell which requests need to be broadcasted.
#[derive(Clone, Copy, Debug, PartialEq, Deserialize, Serialize, EnumString, EnumVariantNames)]
#[derive(Clone, Copy, Debug, PartialEq, Deserialize, Serialize, EnumVariantNames, ValueEnum)]
#[strum(serialize_all = "kebab-case")]
pub enum ApiTopic {
None,
Attestations,
Blocks,
Subscriptions,
@@ -749,7 +752,6 @@ mod tests {
use crate::beacon_node_health::BeaconNodeHealthTier;
use eth2::SensitiveUrl;
use eth2::Timeouts;
use std::str::FromStr;
use strum::VariantNames;
use types::{MainnetEthSpec, Slot};
@@ -758,10 +760,13 @@ mod tests {
#[test]
fn api_topic_all() {
let all = ApiTopic::all();
assert_eq!(all.len(), ApiTopic::VARIANTS.len());
assert!(ApiTopic::VARIANTS
// ignore NONE variant
let mut variants = ApiTopic::VARIANTS.to_vec();
variants.retain(|s| *s != "none");
assert_eq!(all.len(), variants.len());
assert!(variants
.iter()
.map(|topic| ApiTopic::from_str(topic).unwrap())
.map(|topic| ApiTopic::from_str(topic, true).unwrap())
.eq(all.into_iter()));
}