Skip to content

Commit b49845a

Browse files
committed
add builder for DutiesService
1 parent 9b7c160 commit b49845a

File tree

2 files changed

+120
-26
lines changed

2 files changed

+120
-26
lines changed

validator_client/src/lib.rs

Lines changed: 21 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -43,12 +43,11 @@ use validator_http_api::ApiSecret;
4343
use validator_services::{
4444
attestation_service::{AttestationService, AttestationServiceBuilder},
4545
block_service::{BlockService, BlockServiceBuilder},
46-
duties_service::{self, DutiesService},
46+
duties_service::{self, DutiesService, DutiesServiceBuilder},
4747
preparation_service::{PreparationService, PreparationServiceBuilder},
48-
sync::SyncDutiesMap,
4948
sync_committee_service::SyncCommitteeService,
5049
};
51-
use validator_store::ValidatorStore;
50+
use validator_store::ValidatorStore as ValidatorStoreTrait;
5251

5352
/// The interval between attempts to contact the beacon node during startup.
5453
const RETRY_DELAY: Duration = Duration::from_secs(2);
@@ -73,20 +72,18 @@ const HTTP_GET_VALIDATOR_BLOCK_TIMEOUT_QUOTIENT: u32 = 4;
7372

7473
const DOPPELGANGER_SERVICE_NAME: &str = "doppelganger";
7574

75+
type ValidatorStore = LighthouseValidatorStore<SystemTimeSlotClock>;
76+
7677
#[derive(Clone)]
7778
pub struct ProductionValidatorClient<E: EthSpec> {
7879
context: RuntimeContext<E>,
79-
duties_service:
80-
Arc<DutiesService<LighthouseValidatorStore<SystemTimeSlotClock>, SystemTimeSlotClock, E>>,
81-
block_service: BlockService<LighthouseValidatorStore<SystemTimeSlotClock>, SystemTimeSlotClock>,
82-
attestation_service:
83-
AttestationService<LighthouseValidatorStore<SystemTimeSlotClock>, SystemTimeSlotClock, E>,
84-
sync_committee_service:
85-
SyncCommitteeService<LighthouseValidatorStore<SystemTimeSlotClock>, SystemTimeSlotClock, E>,
80+
duties_service: Arc<DutiesService<ValidatorStore, SystemTimeSlotClock, E>>,
81+
block_service: BlockService<ValidatorStore, SystemTimeSlotClock>,
82+
attestation_service: AttestationService<ValidatorStore, SystemTimeSlotClock, E>,
83+
sync_committee_service: SyncCommitteeService<ValidatorStore, SystemTimeSlotClock, E>,
8684
doppelganger_service: Option<Arc<DoppelgangerService>>,
87-
preparation_service:
88-
PreparationService<LighthouseValidatorStore<SystemTimeSlotClock>, SystemTimeSlotClock>,
89-
validator_store: Arc<LighthouseValidatorStore<SystemTimeSlotClock>>,
85+
preparation_service: PreparationService<ValidatorStore, SystemTimeSlotClock>,
86+
validator_store: Arc<ValidatorStore>,
9087
slot_clock: SystemTimeSlotClock,
9188
http_api_listen_addr: Option<SocketAddr>,
9289
config: Config,
@@ -470,19 +467,17 @@ impl<E: EthSpec> ProductionValidatorClient<E> {
470467
validator_store.prune_slashing_protection_db(slot.epoch(E::slots_per_epoch()), true);
471468
}
472469

473-
let duties_service = Arc::new(DutiesService {
474-
attesters: <_>::default(),
475-
proposers: <_>::default(),
476-
sync_duties: SyncDutiesMap::new(config.distributed),
477-
slot_clock: slot_clock.clone(),
478-
beacon_nodes: beacon_nodes.clone(),
479-
validator_store: validator_store.clone(),
480-
unknown_validator_next_poll_slots: <_>::default(),
481-
spec: context.eth2_config.spec.clone(),
482-
executor: context.executor.clone(),
483-
enable_high_validator_count_metrics: config.enable_high_validator_count_metrics,
484-
distributed: config.distributed,
485-
});
470+
let duties_service = Arc::new(
471+
DutiesServiceBuilder::new()
472+
.slot_clock(slot_clock.clone())
473+
.beacon_nodes(beacon_nodes.clone())
474+
.validator_store(validator_store.clone())
475+
.spec(context.eth2_config.spec.clone())
476+
.executor(context.executor.clone())
477+
.enable_high_validator_count_metrics(config.enable_high_validator_count_metrics)
478+
.distributed(config.distributed)
479+
.build()?,
480+
);
486481

487482
// Update the metrics server.
488483
if let Some(ctx) = &validator_metrics_ctx {

validator_client/validator_services/src/duties_service.rs

Lines changed: 99 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -204,6 +204,105 @@ type DependentRoot = Hash256;
204204
type AttesterMap = HashMap<PublicKeyBytes, HashMap<Epoch, (DependentRoot, DutyAndProof)>>;
205205
type ProposerMap = HashMap<Epoch, (DependentRoot, Vec<ProposerData>)>;
206206

207+
pub struct DutiesServiceBuilder<S, T> {
208+
/// Provides the canonical list of locally-managed validators.
209+
validator_store: Option<Arc<S>>,
210+
/// Tracks the current slot.
211+
slot_clock: Option<T>,
212+
/// Provides HTTP access to remote beacon nodes.
213+
beacon_nodes: Option<Arc<BeaconNodeFallback<T>>>,
214+
/// The runtime for spawning tasks.
215+
executor: Option<TaskExecutor>,
216+
/// The current chain spec.
217+
spec: Option<Arc<ChainSpec>>,
218+
//// Whether we permit large validator counts in the metrics.
219+
enable_high_validator_count_metrics: bool,
220+
/// If this validator is running in distributed mode.
221+
distributed: bool,
222+
}
223+
224+
impl<S, T> Default for DutiesServiceBuilder<S, T> {
225+
fn default() -> Self {
226+
Self::new()
227+
}
228+
}
229+
230+
impl<S, T> DutiesServiceBuilder<S, T> {
231+
pub fn new() -> Self {
232+
Self {
233+
validator_store: None,
234+
slot_clock: None,
235+
beacon_nodes: None,
236+
executor: None,
237+
spec: None,
238+
enable_high_validator_count_metrics: false,
239+
distributed: false,
240+
}
241+
}
242+
243+
pub fn validator_store(mut self, validator_store: Arc<S>) -> Self {
244+
self.validator_store = Some(validator_store);
245+
self
246+
}
247+
248+
pub fn slot_clock(mut self, slot_clock: T) -> Self {
249+
self.slot_clock = Some(slot_clock);
250+
self
251+
}
252+
253+
pub fn beacon_nodes(mut self, beacon_nodes: Arc<BeaconNodeFallback<T>>) -> Self {
254+
self.beacon_nodes = Some(beacon_nodes);
255+
self
256+
}
257+
258+
pub fn executor(mut self, executor: TaskExecutor) -> Self {
259+
self.executor = Some(executor);
260+
self
261+
}
262+
263+
pub fn spec(mut self, spec: Arc<ChainSpec>) -> Self {
264+
self.spec = Some(spec);
265+
self
266+
}
267+
268+
pub fn enable_high_validator_count_metrics(
269+
mut self,
270+
enable_high_validator_count_metrics: bool,
271+
) -> Self {
272+
self.enable_high_validator_count_metrics = enable_high_validator_count_metrics;
273+
self
274+
}
275+
276+
pub fn distributed(mut self, distributed: bool) -> Self {
277+
self.distributed = distributed;
278+
self
279+
}
280+
281+
pub fn build<E: EthSpec>(self) -> Result<DutiesService<S, T, E>, String> {
282+
Ok(DutiesService {
283+
attesters: Default::default(),
284+
proposers: Default::default(),
285+
sync_duties: SyncDutiesMap::new(self.distributed),
286+
validator_store: self
287+
.validator_store
288+
.ok_or("Cannot build DutiesService without validator_store")?,
289+
unknown_validator_next_poll_slots: Default::default(),
290+
slot_clock: self
291+
.slot_clock
292+
.ok_or("Cannot build DutiesService without slot_clock")?,
293+
beacon_nodes: self
294+
.beacon_nodes
295+
.ok_or("Cannot build DutiesService without beacon_nodes")?,
296+
executor: self
297+
.executor
298+
.ok_or("Cannot build DutiesService without executor")?,
299+
spec: self.spec.ok_or("Cannot build DutiesService without spec")?,
300+
enable_high_validator_count_metrics: self.enable_high_validator_count_metrics,
301+
distributed: self.distributed,
302+
})
303+
}
304+
}
305+
207306
/// See the module-level documentation.
208307
pub struct DutiesService<S, T, E: EthSpec> {
209308
/// Maps a validator public key to their duties for each epoch.

0 commit comments

Comments
 (0)