Skip to content

Commit e29b607

Browse files
authored
Move notifier and latency service to validator_services (#7427)
We would like to reuse the `notifier` and `latency_service` in Anchor. To make this possible, this PR moves these from `validator_client` to `validator_services` and makes them use the new `ValidatorStore` trait is used so that the code can be reused in Anchor.
1 parent 0688932 commit e29b607

File tree

5 files changed

+35
-29
lines changed

5 files changed

+35
-29
lines changed

validator_client/src/lib.rs

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,5 @@
11
pub mod cli;
22
pub mod config;
3-
mod latency;
4-
mod notifier;
53

64
use crate::cli::ValidatorClient;
75
pub use config::Config;
@@ -21,7 +19,6 @@ use environment::RuntimeContext;
2119
use eth2::{reqwest::ClientBuilder, BeaconNodeHttpClient, StatusCode, Timeouts};
2220
use initialized_validators::Error::UnableToOpenVotingKeystore;
2321
use lighthouse_validator_store::LighthouseValidatorStore;
24-
use notifier::spawn_notifier;
2522
use parking_lot::RwLock;
2623
use reqwest::Certificate;
2724
use slot_clock::SlotClock;
@@ -39,10 +36,12 @@ use tokio::{
3936
use tracing::{debug, error, info, warn};
4037
use types::{EthSpec, Hash256};
4138
use validator_http_api::ApiSecret;
39+
use validator_services::notifier_service::spawn_notifier;
4240
use validator_services::{
4341
attestation_service::{AttestationService, AttestationServiceBuilder},
4442
block_service::{BlockService, BlockServiceBuilder},
4543
duties_service::{self, DutiesService, DutiesServiceBuilder},
44+
latency_service,
4645
preparation_service::{PreparationService, PreparationServiceBuilder},
4746
sync_committee_service::SyncCommitteeService,
4847
};
@@ -601,11 +600,17 @@ impl<E: EthSpec> ProductionValidatorClient<E> {
601600
info!("Doppelganger protection disabled.")
602601
}
603602

604-
spawn_notifier(self).map_err(|e| format!("Failed to start notifier: {}", e))?;
603+
let context = self.context.service_context("notifier".into());
604+
spawn_notifier(
605+
self.duties_service.clone(),
606+
context.executor,
607+
&self.context.eth2_config.spec,
608+
)
609+
.map_err(|e| format!("Failed to start notifier: {}", e))?;
605610

606611
if self.config.enable_latency_measurement_service {
607-
latency::start_latency_service(
608-
self.context.clone(),
612+
latency_service::start_latency_service(
613+
self.context.executor.clone(),
609614
self.duties_service.slot_clock.clone(),
610615
self.duties_service.beacon_nodes.clone(),
611616
);

validator_client/validator_services/Cargo.toml

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -14,11 +14,11 @@ graffiti_file = { workspace = true }
1414
logging = { workspace = true }
1515
parking_lot = { workspace = true }
1616
safe_arith = { workspace = true }
17-
slot_clock = { workspace = true }
17+
slot_clock = { workspace = true }
1818
task_executor = { workspace = true }
19-
tokio = { workspace = true }
19+
tokio = { workspace = true }
2020
tracing = { workspace = true }
21-
tree_hash = { workspace = true }
22-
types = { workspace = true }
21+
tree_hash = { workspace = true }
22+
types = { workspace = true }
2323
validator_metrics = { workspace = true }
2424
validator_store = { workspace = true }

validator_client/src/latency.rs renamed to validator_client/validator_services/src/latency_service.rs

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,18 @@
11
use beacon_node_fallback::BeaconNodeFallback;
2-
use environment::RuntimeContext;
32
use slot_clock::SlotClock;
43
use std::sync::Arc;
4+
use task_executor::TaskExecutor;
55
use tokio::time::sleep;
66
use tracing::debug;
7-
use types::EthSpec;
87

98
/// The latency service will run 11/12ths of the way through the slot.
109
pub const SLOT_DELAY_MULTIPLIER: u32 = 11;
1110
pub const SLOT_DELAY_DENOMINATOR: u32 = 12;
1211

1312
/// Starts a service that periodically checks the latency between the VC and the
1413
/// candidate BNs.
15-
pub fn start_latency_service<T: SlotClock + 'static, E: EthSpec>(
16-
context: RuntimeContext<E>,
14+
pub fn start_latency_service<T: SlotClock + 'static>(
15+
executor: TaskExecutor,
1716
slot_clock: T,
1817
beacon_nodes: Arc<BeaconNodeFallback<T>>,
1918
) {
@@ -57,5 +56,5 @@ pub fn start_latency_service<T: SlotClock + 'static, E: EthSpec>(
5756
}
5857
};
5958

60-
context.executor.spawn(future, "latency");
59+
executor.spawn(future, "latency");
6160
}
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
pub mod attestation_service;
22
pub mod block_service;
33
pub mod duties_service;
4+
pub mod latency_service;
5+
pub mod notifier_service;
46
pub mod preparation_service;
57
pub mod sync;
68
pub mod sync_committee_service;

validator_client/src/notifier.rs renamed to validator_client/validator_services/src/notifier_service.rs

Lines changed: 14 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,20 @@
1-
use crate::{DutiesService, ProductionValidatorClient};
2-
use lighthouse_validator_store::LighthouseValidatorStore;
3-
use metrics::set_gauge;
1+
use crate::duties_service::DutiesService;
42
use slot_clock::SlotClock;
3+
use std::sync::Arc;
4+
use task_executor::TaskExecutor;
55
use tokio::time::{sleep, Duration};
66
use tracing::{debug, error, info};
7-
use types::EthSpec;
7+
use types::{ChainSpec, EthSpec};
8+
use validator_metrics::set_gauge;
9+
use validator_store::ValidatorStore;
810

911
/// Spawns a notifier service which periodically logs information about the node.
10-
pub fn spawn_notifier<E: EthSpec>(client: &ProductionValidatorClient<E>) -> Result<(), String> {
11-
let context = client.context.service_context("notifier".into());
12-
let executor = context.executor.clone();
13-
let duties_service = client.duties_service.clone();
14-
15-
let slot_duration = Duration::from_secs(context.eth2_config.spec.seconds_per_slot);
12+
pub fn spawn_notifier<S: ValidatorStore + 'static, T: SlotClock + 'static>(
13+
duties_service: Arc<DutiesService<S, T>>,
14+
executor: TaskExecutor,
15+
spec: &ChainSpec,
16+
) -> Result<(), String> {
17+
let slot_duration = Duration::from_secs(spec.seconds_per_slot);
1618

1719
let interval_fut = async move {
1820
loop {
@@ -33,9 +35,7 @@ pub fn spawn_notifier<E: EthSpec>(client: &ProductionValidatorClient<E>) -> Resu
3335
}
3436

3537
/// Performs a single notification routine.
36-
async fn notify<T: SlotClock + 'static, E: EthSpec>(
37-
duties_service: &DutiesService<LighthouseValidatorStore<T, E>, T>,
38-
) {
38+
async fn notify<S: ValidatorStore, T: SlotClock + 'static>(duties_service: &DutiesService<S, T>) {
3939
let (candidate_info, num_available, num_synced) =
4040
duties_service.beacon_nodes.get_notifier_info().await;
4141
let num_total = candidate_info.len();
@@ -102,7 +102,7 @@ async fn notify<T: SlotClock + 'static, E: EthSpec>(
102102
}
103103

104104
if let Some(slot) = duties_service.slot_clock.now() {
105-
let epoch = slot.epoch(E::slots_per_epoch());
105+
let epoch = slot.epoch(S::E::slots_per_epoch());
106106

107107
let total_validators = duties_service.total_validator_count();
108108
let proposing_validators = duties_service.proposer_count(epoch);

0 commit comments

Comments
 (0)