mirror of
https://github.com/sigp/lighthouse.git
synced 2026-03-20 13:24:44 +00:00
Modularize validator store (#6705)
- Create trait `ValidatorStore` with all functions used by the `validator_services` - Make `validator_services` generic on `S: ValidatorStore` - Introduce `LighthouseValidatorStore`, which has identical functionality to the old `ValidatorStore` - Remove dependencies (especially `environment`) from `validator_services` and `beacon_node_fallback` in order to be able to cleanly use them in Anchor
This commit is contained in:
@@ -10,6 +10,7 @@ use directory::{
|
||||
use eth2::types::Graffiti;
|
||||
use graffiti_file::GraffitiFile;
|
||||
use initialized_validators::Config as InitializedValidatorsConfig;
|
||||
use lighthouse_validator_store::Config as ValidatorStoreConfig;
|
||||
use sensitive_url::SensitiveUrl;
|
||||
use serde::{Deserialize, Serialize};
|
||||
use std::fs;
|
||||
@@ -20,7 +21,6 @@ use tracing::{info, warn};
|
||||
use types::GRAFFITI_BYTES_LEN;
|
||||
use validator_http_api::{self, PK_FILENAME};
|
||||
use validator_http_metrics;
|
||||
use validator_store::Config as ValidatorStoreConfig;
|
||||
|
||||
pub const DEFAULT_BEACON_NODE: &str = "http://localhost:5052/";
|
||||
|
||||
|
||||
@@ -15,7 +15,7 @@ pub const SLOT_DELAY_DENOMINATOR: u32 = 12;
|
||||
pub fn start_latency_service<T: SlotClock + 'static, E: EthSpec>(
|
||||
context: RuntimeContext<E>,
|
||||
slot_clock: T,
|
||||
beacon_nodes: Arc<BeaconNodeFallback<T, E>>,
|
||||
beacon_nodes: Arc<BeaconNodeFallback<T>>,
|
||||
) {
|
||||
let future = async move {
|
||||
loop {
|
||||
|
||||
@@ -20,6 +20,7 @@ use doppelganger_service::DoppelgangerService;
|
||||
use environment::RuntimeContext;
|
||||
use eth2::{reqwest::ClientBuilder, BeaconNodeHttpClient, StatusCode, Timeouts};
|
||||
use initialized_validators::Error::UnableToOpenVotingKeystore;
|
||||
use lighthouse_validator_store::LighthouseValidatorStore;
|
||||
use notifier::spawn_notifier;
|
||||
use parking_lot::RwLock;
|
||||
use reqwest::Certificate;
|
||||
@@ -27,7 +28,6 @@ use slot_clock::SlotClock;
|
||||
use slot_clock::SystemTimeSlotClock;
|
||||
use std::fs::File;
|
||||
use std::io::Read;
|
||||
use std::marker::PhantomData;
|
||||
use std::net::SocketAddr;
|
||||
use std::path::Path;
|
||||
use std::sync::Arc;
|
||||
@@ -42,12 +42,11 @@ use validator_http_api::ApiSecret;
|
||||
use validator_services::{
|
||||
attestation_service::{AttestationService, AttestationServiceBuilder},
|
||||
block_service::{BlockService, BlockServiceBuilder},
|
||||
duties_service::{self, DutiesService},
|
||||
duties_service::{self, DutiesService, DutiesServiceBuilder},
|
||||
preparation_service::{PreparationService, PreparationServiceBuilder},
|
||||
sync::SyncDutiesMap,
|
||||
sync_committee_service::SyncCommitteeService,
|
||||
};
|
||||
use validator_store::ValidatorStore;
|
||||
use validator_store::ValidatorStore as ValidatorStoreTrait;
|
||||
|
||||
/// The interval between attempts to contact the beacon node during startup.
|
||||
const RETRY_DELAY: Duration = Duration::from_secs(2);
|
||||
@@ -72,20 +71,22 @@ const HTTP_GET_VALIDATOR_BLOCK_TIMEOUT_QUOTIENT: u32 = 4;
|
||||
|
||||
const DOPPELGANGER_SERVICE_NAME: &str = "doppelganger";
|
||||
|
||||
type ValidatorStore<E> = LighthouseValidatorStore<SystemTimeSlotClock, E>;
|
||||
|
||||
#[derive(Clone)]
|
||||
pub struct ProductionValidatorClient<E: EthSpec> {
|
||||
context: RuntimeContext<E>,
|
||||
duties_service: Arc<DutiesService<SystemTimeSlotClock, E>>,
|
||||
block_service: BlockService<SystemTimeSlotClock, E>,
|
||||
attestation_service: AttestationService<SystemTimeSlotClock, E>,
|
||||
sync_committee_service: SyncCommitteeService<SystemTimeSlotClock, E>,
|
||||
duties_service: Arc<DutiesService<ValidatorStore<E>, SystemTimeSlotClock>>,
|
||||
block_service: BlockService<ValidatorStore<E>, SystemTimeSlotClock>,
|
||||
attestation_service: AttestationService<ValidatorStore<E>, SystemTimeSlotClock>,
|
||||
sync_committee_service: SyncCommitteeService<ValidatorStore<E>, SystemTimeSlotClock>,
|
||||
doppelganger_service: Option<Arc<DoppelgangerService>>,
|
||||
preparation_service: PreparationService<SystemTimeSlotClock, E>,
|
||||
validator_store: Arc<ValidatorStore<SystemTimeSlotClock, E>>,
|
||||
preparation_service: PreparationService<ValidatorStore<E>, SystemTimeSlotClock>,
|
||||
validator_store: Arc<ValidatorStore<E>>,
|
||||
slot_clock: SystemTimeSlotClock,
|
||||
http_api_listen_addr: Option<SocketAddr>,
|
||||
config: Config,
|
||||
beacon_nodes: Arc<BeaconNodeFallback<SystemTimeSlotClock, E>>,
|
||||
beacon_nodes: Arc<BeaconNodeFallback<SystemTimeSlotClock>>,
|
||||
genesis_time: u64,
|
||||
}
|
||||
|
||||
@@ -367,14 +368,14 @@ impl<E: EthSpec> ProductionValidatorClient<E> {
|
||||
// Initialize the number of connected, avaliable beacon nodes to 0.
|
||||
set_gauge(&validator_metrics::AVAILABLE_BEACON_NODES_COUNT, 0);
|
||||
|
||||
let mut beacon_nodes: BeaconNodeFallback<_, E> = BeaconNodeFallback::new(
|
||||
let mut beacon_nodes: BeaconNodeFallback<_> = BeaconNodeFallback::new(
|
||||
candidates,
|
||||
config.beacon_node_fallback,
|
||||
config.broadcast_topics.clone(),
|
||||
context.eth2_config.spec.clone(),
|
||||
);
|
||||
|
||||
let mut proposer_nodes: BeaconNodeFallback<_, E> = BeaconNodeFallback::new(
|
||||
let mut proposer_nodes: BeaconNodeFallback<_> = BeaconNodeFallback::new(
|
||||
proposer_candidates,
|
||||
config.beacon_node_fallback,
|
||||
config.broadcast_topics.clone(),
|
||||
@@ -383,7 +384,7 @@ impl<E: EthSpec> ProductionValidatorClient<E> {
|
||||
|
||||
// Perform some potentially long-running initialization tasks.
|
||||
let (genesis_time, genesis_validators_root) = tokio::select! {
|
||||
tuple = init_from_beacon_node(&beacon_nodes, &proposer_nodes) => tuple?,
|
||||
tuple = init_from_beacon_node::<E>(&beacon_nodes, &proposer_nodes) => tuple?,
|
||||
() = context.executor.exit() => return Err("Shutting down".to_string())
|
||||
};
|
||||
|
||||
@@ -402,10 +403,10 @@ impl<E: EthSpec> ProductionValidatorClient<E> {
|
||||
proposer_nodes.set_slot_clock(slot_clock.clone());
|
||||
|
||||
let beacon_nodes = Arc::new(beacon_nodes);
|
||||
start_fallback_updater_service(context.clone(), beacon_nodes.clone())?;
|
||||
start_fallback_updater_service::<_, E>(context.executor.clone(), beacon_nodes.clone())?;
|
||||
|
||||
let proposer_nodes = Arc::new(proposer_nodes);
|
||||
start_fallback_updater_service(context.clone(), proposer_nodes.clone())?;
|
||||
start_fallback_updater_service::<_, E>(context.executor.clone(), proposer_nodes.clone())?;
|
||||
|
||||
let doppelganger_service = if config.enable_doppelganger_protection {
|
||||
Some(Arc::new(DoppelgangerService::default()))
|
||||
@@ -413,7 +414,7 @@ impl<E: EthSpec> ProductionValidatorClient<E> {
|
||||
None
|
||||
};
|
||||
|
||||
let validator_store = Arc::new(ValidatorStore::new(
|
||||
let validator_store = Arc::new(LighthouseValidatorStore::new(
|
||||
validators,
|
||||
slashing_protection,
|
||||
genesis_validators_root,
|
||||
@@ -439,21 +440,18 @@ impl<E: EthSpec> ProductionValidatorClient<E> {
|
||||
validator_store.prune_slashing_protection_db(slot.epoch(E::slots_per_epoch()), true);
|
||||
}
|
||||
|
||||
let duties_context = context.service_context("duties".into());
|
||||
let duties_service = Arc::new(DutiesService {
|
||||
attesters: <_>::default(),
|
||||
proposers: <_>::default(),
|
||||
sync_duties: SyncDutiesMap::new(config.distributed),
|
||||
slot_clock: slot_clock.clone(),
|
||||
beacon_nodes: beacon_nodes.clone(),
|
||||
validator_store: validator_store.clone(),
|
||||
unknown_validator_next_poll_slots: <_>::default(),
|
||||
spec: context.eth2_config.spec.clone(),
|
||||
context: duties_context,
|
||||
enable_high_validator_count_metrics: config.enable_high_validator_count_metrics,
|
||||
distributed: config.distributed,
|
||||
disable_attesting: config.disable_attesting,
|
||||
});
|
||||
let duties_service = Arc::new(
|
||||
DutiesServiceBuilder::new()
|
||||
.slot_clock(slot_clock.clone())
|
||||
.beacon_nodes(beacon_nodes.clone())
|
||||
.validator_store(validator_store.clone())
|
||||
.spec(context.eth2_config.spec.clone())
|
||||
.executor(context.executor.clone())
|
||||
.enable_high_validator_count_metrics(config.enable_high_validator_count_metrics)
|
||||
.distributed(config.distributed)
|
||||
.disable_attesting(config.disable_attesting)
|
||||
.build()?,
|
||||
);
|
||||
|
||||
// Update the metrics server.
|
||||
if let Some(ctx) = &validator_metrics_ctx {
|
||||
@@ -465,7 +463,8 @@ impl<E: EthSpec> ProductionValidatorClient<E> {
|
||||
.slot_clock(slot_clock.clone())
|
||||
.validator_store(validator_store.clone())
|
||||
.beacon_nodes(beacon_nodes.clone())
|
||||
.runtime_context(context.service_context("block".into()))
|
||||
.executor(context.executor.clone())
|
||||
.chain_spec(context.eth2_config.spec.clone())
|
||||
.graffiti(config.graffiti)
|
||||
.graffiti_file(config.graffiti_file.clone());
|
||||
|
||||
@@ -481,7 +480,8 @@ impl<E: EthSpec> ProductionValidatorClient<E> {
|
||||
.slot_clock(slot_clock.clone())
|
||||
.validator_store(validator_store.clone())
|
||||
.beacon_nodes(beacon_nodes.clone())
|
||||
.runtime_context(context.service_context("attestation".into()))
|
||||
.executor(context.executor.clone())
|
||||
.chain_spec(context.eth2_config.spec.clone())
|
||||
.disable(config.disable_attesting)
|
||||
.build()?;
|
||||
|
||||
@@ -489,7 +489,7 @@ impl<E: EthSpec> ProductionValidatorClient<E> {
|
||||
.slot_clock(slot_clock.clone())
|
||||
.validator_store(validator_store.clone())
|
||||
.beacon_nodes(beacon_nodes.clone())
|
||||
.runtime_context(context.service_context("preparation".into()))
|
||||
.executor(context.executor.clone())
|
||||
.builder_registration_timestamp_override(config.builder_registration_timestamp_override)
|
||||
.validator_registration_batch_size(config.validator_registration_batch_size)
|
||||
.build()?;
|
||||
@@ -499,7 +499,7 @@ impl<E: EthSpec> ProductionValidatorClient<E> {
|
||||
validator_store.clone(),
|
||||
slot_clock.clone(),
|
||||
beacon_nodes.clone(),
|
||||
context.service_context("sync_committee".into()),
|
||||
context.executor.clone(),
|
||||
);
|
||||
|
||||
Ok(Self {
|
||||
@@ -542,12 +542,11 @@ impl<E: EthSpec> ProductionValidatorClient<E> {
|
||||
config: self.config.http_api.clone(),
|
||||
sse_logging_components: self.context.sse_logging_components.clone(),
|
||||
slot_clock: self.slot_clock.clone(),
|
||||
_phantom: PhantomData,
|
||||
});
|
||||
|
||||
let exit = self.context.executor.exit();
|
||||
|
||||
let (listen_addr, server) = validator_http_api::serve(ctx, exit)
|
||||
let (listen_addr, server) = validator_http_api::serve::<_, E>(ctx, exit)
|
||||
.map_err(|e| format!("Unable to start HTTP API server: {:?}", e))?;
|
||||
|
||||
self.context
|
||||
@@ -615,12 +614,12 @@ impl<E: EthSpec> ProductionValidatorClient<E> {
|
||||
}
|
||||
|
||||
async fn init_from_beacon_node<E: EthSpec>(
|
||||
beacon_nodes: &BeaconNodeFallback<SystemTimeSlotClock, E>,
|
||||
proposer_nodes: &BeaconNodeFallback<SystemTimeSlotClock, E>,
|
||||
beacon_nodes: &BeaconNodeFallback<SystemTimeSlotClock>,
|
||||
proposer_nodes: &BeaconNodeFallback<SystemTimeSlotClock>,
|
||||
) -> Result<(u64, Hash256), String> {
|
||||
loop {
|
||||
beacon_nodes.update_all_candidates().await;
|
||||
proposer_nodes.update_all_candidates().await;
|
||||
beacon_nodes.update_all_candidates::<E>().await;
|
||||
proposer_nodes.update_all_candidates::<E>().await;
|
||||
|
||||
let num_available = beacon_nodes.num_available().await;
|
||||
let num_total = beacon_nodes.num_total().await;
|
||||
@@ -697,8 +696,8 @@ async fn init_from_beacon_node<E: EthSpec>(
|
||||
Ok((genesis.genesis_time, genesis.genesis_validators_root))
|
||||
}
|
||||
|
||||
async fn wait_for_genesis<E: EthSpec>(
|
||||
beacon_nodes: &BeaconNodeFallback<SystemTimeSlotClock, E>,
|
||||
async fn wait_for_genesis(
|
||||
beacon_nodes: &BeaconNodeFallback<SystemTimeSlotClock>,
|
||||
genesis_time: u64,
|
||||
) -> Result<(), String> {
|
||||
let now = SystemTime::now()
|
||||
@@ -740,8 +739,8 @@ async fn wait_for_genesis<E: EthSpec>(
|
||||
|
||||
/// Request the version from the node, looping back and trying again on failure. Exit once the node
|
||||
/// has been contacted.
|
||||
async fn poll_whilst_waiting_for_genesis<E: EthSpec>(
|
||||
beacon_nodes: &BeaconNodeFallback<SystemTimeSlotClock, E>,
|
||||
async fn poll_whilst_waiting_for_genesis(
|
||||
beacon_nodes: &BeaconNodeFallback<SystemTimeSlotClock>,
|
||||
genesis_time: Duration,
|
||||
) -> Result<(), String> {
|
||||
loop {
|
||||
|
||||
@@ -1,4 +1,5 @@
|
||||
use crate::{DutiesService, ProductionValidatorClient};
|
||||
use lighthouse_validator_store::LighthouseValidatorStore;
|
||||
use metrics::set_gauge;
|
||||
use slot_clock::SlotClock;
|
||||
use tokio::time::{sleep, Duration};
|
||||
@@ -32,7 +33,9 @@ pub fn spawn_notifier<E: EthSpec>(client: &ProductionValidatorClient<E>) -> Resu
|
||||
}
|
||||
|
||||
/// Performs a single notification routine.
|
||||
async fn notify<T: SlotClock + 'static, E: EthSpec>(duties_service: &DutiesService<T, E>) {
|
||||
async fn notify<T: SlotClock + 'static, E: EthSpec>(
|
||||
duties_service: &DutiesService<LighthouseValidatorStore<T, E>, T>,
|
||||
) {
|
||||
let (candidate_info, num_available, num_synced) =
|
||||
duties_service.beacon_nodes.get_notifier_info().await;
|
||||
let num_total = candidate_info.len();
|
||||
|
||||
Reference in New Issue
Block a user