Skip to content

Commit

Permalink
Add http api endpoint
Browse files Browse the repository at this point in the history
  • Loading branch information
macladson committed Aug 1, 2023
1 parent 9b5bf12 commit 0bdb482
Show file tree
Hide file tree
Showing 5 changed files with 55 additions and 15 deletions.
10 changes: 5 additions & 5 deletions validator_client/src/beacon_node_fallback.rs
Original file line number Diff line number Diff line change
Expand Up @@ -132,7 +132,7 @@ impl<E: Debug> fmt::Display for Errors<E> {
}

/// Reasons why a candidate might not be ready.
#[derive(Debug, Clone, Copy)]
#[derive(Debug, Clone, Copy, Deserialize, Serialize)]
pub enum CandidateError {
Uninitialized,
Offline,
Expand All @@ -151,9 +151,9 @@ pub struct CandidateInfo {
/// for a query.
#[derive(Debug)]
pub struct CandidateBeaconNode<E> {
id: usize,
beacon_node: BeaconNodeHttpClient,
health: PLRwLock<Result<BeaconNodeHealth, CandidateError>>,
pub id: usize,
pub beacon_node: BeaconNodeHttpClient,
pub health: PLRwLock<Result<BeaconNodeHealth, CandidateError>>,
_phantom: PhantomData<E>,
}

Expand Down Expand Up @@ -324,7 +324,7 @@ impl<E: EthSpec> CandidateBeaconNode<E> {
/// identical query.
#[derive(Clone, Debug)]
pub struct BeaconNodeFallback<T, E> {
candidates: Arc<RwLock<Vec<CandidateBeaconNode<E>>>>,
pub candidates: Arc<RwLock<Vec<CandidateBeaconNode<E>>>>,
disable_run_on_all: bool,
distance_tiers: BeaconNodeSyncDistanceTiers,
slot_clock: Option<T>,
Expand Down
11 changes: 6 additions & 5 deletions validator_client/src/beacon_node_health.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
use crate::beacon_node_fallback::Config;
use serde_derive::{Deserialize, Serialize};
use slot_clock::SlotClock;
use std::cmp::Ordering;
use std::fmt::{Debug, Display, Formatter};
Expand All @@ -20,7 +21,7 @@ type HealthTier = u8;
type SyncDistance = Slot;

/// Helpful enum which is used when pattern matching to determine health tier.
#[derive(Copy, Clone, Debug, PartialEq, Eq)]
#[derive(Copy, Clone, Debug, PartialEq, Eq, Deserialize, Serialize)]
pub enum SyncDistanceTier {
Synced,
Small,
Expand Down Expand Up @@ -95,19 +96,19 @@ impl Default for BeaconNodeSyncDistanceTiers {
/// Execution Node health metrics.
///
/// Currently only considers `el_offline`.
#[derive(Copy, Clone, Debug, PartialEq, Eq)]
#[derive(Copy, Clone, Debug, PartialEq, Eq, Deserialize, Serialize)]
pub enum ExecutionEngineHealth {
Healthy,
Unhealthy,
}

#[derive(Copy, Clone, Debug, PartialEq, Eq)]
#[derive(Copy, Clone, Debug, PartialEq, Eq, Deserialize, Serialize)]
pub enum IsOptimistic {
Yes,
No,
}

#[derive(Copy, Clone, Debug, PartialEq, Eq)]
#[derive(Copy, Clone, Debug, PartialEq, Eq, Deserialize, Serialize)]
pub struct BeaconNodeHealthTier {
pub tier: HealthTier,
pub sync_distance: SyncDistance,
Expand Down Expand Up @@ -158,7 +159,7 @@ impl BeaconNodeHealthTier {
}

/// Beacon Node Health metrics.
#[derive(Copy, Clone, Debug, PartialEq, Eq)]
#[derive(Copy, Clone, Debug, PartialEq, Eq, Deserialize, Serialize)]
pub struct BeaconNodeHealth {
// The ID of the Beacon Node. This should correspond with its position in the `--beacon-nodes`
// list. Note that the ID field is used to tie-break nodes with the same health so that nodes
Expand Down
8 changes: 4 additions & 4 deletions validator_client/src/block_service.rs
Original file line number Diff line number Diff line change
Expand Up @@ -44,8 +44,8 @@ impl From<Errors<BlockError>> for BlockError {
pub struct BlockServiceBuilder<T, E: EthSpec> {
validator_store: Option<Arc<ValidatorStore<T, E>>>,
slot_clock: Option<Arc<T>>,
beacon_nodes: Option<Arc<BeaconNodeFallback<T, E>>>,
proposer_nodes: Option<Arc<BeaconNodeFallback<T, E>>>,
pub beacon_nodes: Option<Arc<BeaconNodeFallback<T, E>>>,
pub proposer_nodes: Option<Arc<BeaconNodeFallback<T, E>>>,
context: Option<RuntimeContext<E>>,
graffiti: Option<Graffiti>,
graffiti_file: Option<GraffitiFile>,
Expand Down Expand Up @@ -187,8 +187,8 @@ impl<T: SlotClock, E: EthSpec> ProposerFallback<T, E> {
pub struct Inner<T, E: EthSpec> {
validator_store: Arc<ValidatorStore<T, E>>,
slot_clock: Arc<T>,
beacon_nodes: Arc<BeaconNodeFallback<T, E>>,
proposer_nodes: Option<Arc<BeaconNodeFallback<T, E>>>,
pub beacon_nodes: Arc<BeaconNodeFallback<T, E>>,
pub proposer_nodes: Option<Arc<BeaconNodeFallback<T, E>>>,
context: RuntimeContext<E>,
graffiti: Option<Graffiti>,
graffiti_file: Option<GraffitiFile>,
Expand Down
40 changes: 39 additions & 1 deletion validator_client/src/http_api/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,10 @@ mod keystores;
mod remotekeys;
mod tests;

use crate::beacon_node_fallback::CandidateError;
use crate::beacon_node_health::BeaconNodeHealth;
use crate::http_api::create_signed_voluntary_exit::create_signed_voluntary_exit;
use crate::{determine_graffiti, GraffitiFile, ValidatorStore};
use crate::{determine_graffiti, BlockService, GraffitiFile, ValidatorStore};
use account_utils::{
mnemonic_from_phrase,
validator_definitions::{SigningDefinition, ValidatorDefinition, Web3SignerDefinition},
Expand Down Expand Up @@ -69,6 +71,7 @@ impl From<String> for Error {
pub struct Context<T: SlotClock, E: EthSpec> {
pub task_executor: TaskExecutor,
pub api_secret: ApiSecret,
pub block_service: Option<BlockService<T, E>>,
pub validator_store: Option<Arc<ValidatorStore<T, E>>>,
pub validator_dir: Option<PathBuf>,
pub graffiti_file: Option<GraffitiFile>,
Expand Down Expand Up @@ -162,6 +165,17 @@ pub fn serve<T: 'static + SlotClock + Clone, E: EthSpec>(
let signer = ctx.api_secret.signer();
let signer = warp::any().map(move || signer.clone());

let inner_block_service = ctx.block_service.clone();
let block_service_filter = warp::any()
.map(move || inner_block_service.clone())
.and_then(|block_service: Option<_>| async move {
block_service.ok_or_else(|| {
warp_utils::reject::custom_not_found(
"block service is not initialized.".to_string(),
)
})
});

let inner_validator_store = ctx.validator_store.clone();
let validator_store_filter = warp::any()
.map(move || inner_validator_store.clone())
Expand Down Expand Up @@ -388,6 +402,29 @@ pub fn serve<T: 'static + SlotClock + Clone, E: EthSpec>(
},
);

// GET lighthouse/ui/fallback_health
let get_lighthouse_ui_fallback_health = warp::path("lighthouse")
.and(warp::path("ui"))
.and(warp::path("fallback_health"))
.and(warp::path::end())
.and(signer.clone())
.and(block_service_filter.clone())
.and_then(|signer, block_filter: BlockService<T, E>| async move {
let mut result: HashMap<String, Result<BeaconNodeHealth, CandidateError>> =
HashMap::new();
for node in &*block_filter.beacon_nodes.candidates.read().await {
result.insert(node.beacon_node.to_string(), *node.health.read());
}
if let Some(proposer_nodes) = &block_filter.proposer_nodes {
for node in &*proposer_nodes.candidates.read().await {
result.insert(node.beacon_node.to_string(), *node.health.read());
}
}

blocking_signed_json_task(signer, move || Ok(api_types::GenericResponse::from(result)))
.await
});

// POST lighthouse/validators/
let post_validators = warp::path("lighthouse")
.and(warp::path("validators"))
Expand Down Expand Up @@ -1099,6 +1136,7 @@ pub fn serve<T: 'static + SlotClock + Clone, E: EthSpec>(
.or(get_lighthouse_validators_pubkey)
.or(get_lighthouse_ui_health)
.or(get_lighthouse_ui_graffiti)
.or(get_lighthouse_ui_fallback_health)
.or(get_fee_recipient)
.or(get_gas_limit)
.or(get_std_keystores)
Expand Down
1 change: 1 addition & 0 deletions validator_client/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -574,6 +574,7 @@ impl<T: EthSpec> ProductionValidatorClient<T> {
let ctx = Arc::new(http_api::Context {
task_executor: self.context.executor.clone(),
api_secret,
block_service: Some(self.block_service.clone()),
validator_store: Some(self.validator_store.clone()),
validator_dir: Some(self.config.validator_dir.clone()),
graffiti_file: self.config.graffiti_file.clone(),
Expand Down

0 comments on commit 0bdb482

Please sign in to comment.