Skip to content

Commit

Permalink
add builder for DutiesService
Browse files Browse the repository at this point in the history
  • Loading branch information
dknopik committed Dec 19, 2024
1 parent e0c6fda commit c26126f
Show file tree
Hide file tree
Showing 2 changed files with 120 additions and 26 deletions.
47 changes: 21 additions & 26 deletions validator_client/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -43,12 +43,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);
Expand All @@ -73,20 +72,18 @@ const HTTP_GET_VALIDATOR_BLOCK_TIMEOUT_QUOTIENT: u32 = 4;

const DOPPELGANGER_SERVICE_NAME: &str = "doppelganger";

type ValidatorStore = LighthouseValidatorStore<SystemTimeSlotClock>;

#[derive(Clone)]
pub struct ProductionValidatorClient<E: EthSpec> {
context: RuntimeContext<E>,
duties_service:
Arc<DutiesService<LighthouseValidatorStore<SystemTimeSlotClock>, SystemTimeSlotClock, E>>,
block_service: BlockService<LighthouseValidatorStore<SystemTimeSlotClock>, SystemTimeSlotClock>,
attestation_service:
AttestationService<LighthouseValidatorStore<SystemTimeSlotClock>, SystemTimeSlotClock, E>,
sync_committee_service:
SyncCommitteeService<LighthouseValidatorStore<SystemTimeSlotClock>, SystemTimeSlotClock, E>,
duties_service: Arc<DutiesService<ValidatorStore, SystemTimeSlotClock, E>>,
block_service: BlockService<ValidatorStore, SystemTimeSlotClock>,
attestation_service: AttestationService<ValidatorStore, SystemTimeSlotClock, E>,
sync_committee_service: SyncCommitteeService<ValidatorStore, SystemTimeSlotClock, E>,
doppelganger_service: Option<Arc<DoppelgangerService>>,
preparation_service:
PreparationService<LighthouseValidatorStore<SystemTimeSlotClock>, SystemTimeSlotClock>,
validator_store: Arc<LighthouseValidatorStore<SystemTimeSlotClock>>,
preparation_service: PreparationService<ValidatorStore, SystemTimeSlotClock>,
validator_store: Arc<ValidatorStore>,
slot_clock: SystemTimeSlotClock,
http_api_listen_addr: Option<SocketAddr>,
config: Config,
Expand Down Expand Up @@ -470,19 +467,17 @@ impl<E: EthSpec> ProductionValidatorClient<E> {
validator_store.prune_slashing_protection_db(slot.epoch(E::slots_per_epoch()), true);
}

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(),
executor: context.executor.clone(),
enable_high_validator_count_metrics: config.enable_high_validator_count_metrics,
distributed: config.distributed,
});
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)
.build()?,
);

// Update the metrics server.
if let Some(ctx) = &validator_metrics_ctx {
Expand Down
99 changes: 99 additions & 0 deletions validator_client/validator_services/src/duties_service.rs
Original file line number Diff line number Diff line change
Expand Up @@ -204,6 +204,105 @@ type DependentRoot = Hash256;
type AttesterMap = HashMap<PublicKeyBytes, HashMap<Epoch, (DependentRoot, DutyAndProof)>>;
type ProposerMap = HashMap<Epoch, (DependentRoot, Vec<ProposerData>)>;

pub struct DutiesServiceBuilder<S, T> {
/// Provides the canonical list of locally-managed validators.
validator_store: Option<Arc<S>>,
/// Tracks the current slot.
slot_clock: Option<T>,
/// Provides HTTP access to remote beacon nodes.
beacon_nodes: Option<Arc<BeaconNodeFallback<T>>>,
/// The runtime for spawning tasks.
executor: Option<TaskExecutor>,
/// The current chain spec.
spec: Option<Arc<ChainSpec>>,
//// Whether we permit large validator counts in the metrics.
enable_high_validator_count_metrics: bool,
/// If this validator is running in distributed mode.
distributed: bool,
}

impl<S, T> Default for DutiesServiceBuilder<S, T> {
fn default() -> Self {
Self::new()
}
}

impl<S, T> DutiesServiceBuilder<S, T> {
pub fn new() -> Self {
Self {
validator_store: None,
slot_clock: None,
beacon_nodes: None,
executor: None,
spec: None,
enable_high_validator_count_metrics: false,
distributed: false,
}
}

pub fn validator_store(mut self, validator_store: Arc<S>) -> Self {
self.validator_store = Some(validator_store);
self
}

pub fn slot_clock(mut self, slot_clock: T) -> Self {
self.slot_clock = Some(slot_clock);
self
}

pub fn beacon_nodes(mut self, beacon_nodes: Arc<BeaconNodeFallback<T>>) -> Self {
self.beacon_nodes = Some(beacon_nodes);
self
}

pub fn executor(mut self, executor: TaskExecutor) -> Self {
self.executor = Some(executor);
self
}

pub fn spec(mut self, spec: Arc<ChainSpec>) -> Self {
self.spec = Some(spec);
self
}

pub fn enable_high_validator_count_metrics(
mut self,
enable_high_validator_count_metrics: bool,
) -> Self {
self.enable_high_validator_count_metrics = enable_high_validator_count_metrics;
self
}

pub fn distributed(mut self, distributed: bool) -> Self {
self.distributed = distributed;
self
}

pub fn build<E: EthSpec>(self) -> Result<DutiesService<S, T, E>, String> {
Ok(DutiesService {
attesters: Default::default(),
proposers: Default::default(),
sync_duties: SyncDutiesMap::new(self.distributed),
validator_store: self
.validator_store
.ok_or("Cannot build DutiesService without validator_store")?,
unknown_validator_next_poll_slots: Default::default(),
slot_clock: self
.slot_clock
.ok_or("Cannot build DutiesService without slot_clock")?,
beacon_nodes: self
.beacon_nodes
.ok_or("Cannot build DutiesService without beacon_nodes")?,
executor: self
.executor
.ok_or("Cannot build DutiesService without executor")?,
spec: self.spec.ok_or("Cannot build DutiesService without spec")?,
enable_high_validator_count_metrics: self.enable_high_validator_count_metrics,
distributed: self.distributed,
})
}
}

/// See the module-level documentation.
pub struct DutiesService<S, T, E: EthSpec> {
/// Maps a validator public key to their duties for each epoch.
Expand Down

0 comments on commit c26126f

Please sign in to comment.