Register vals with doppelganger earlier (#2494)

## Issue Addressed

NA

## Proposed Changes

Registers validators with the doppelganger service at the earliest possible point.

This avoids the following (non-harmful, but scary) log when pruning the slashing DB on startup:

```
CRIT Validator unknown to doppelganger service, pubkey: 0xabc..., msg: preventing validator from performing duties, service: doppelganger
```

## Additional Info

NA
This commit is contained in:
Paul Hauner 2021-08-06 02:13:15 +00:00
parent 17a2c778e3
commit 71ab16e404
2 changed files with 30 additions and 6 deletions

View File

@ -371,7 +371,8 @@ impl DoppelgangerService {
slot_clock: &T, slot_clock: &T,
) -> Result<(), String> { ) -> Result<(), String> {
let current_epoch = slot_clock let current_epoch = slot_clock
.now() // If registering before genesis, use the genesis slot.
.now_or_genesis()
.ok_or_else(|| "Unable to read slot clock when registering validator".to_string())? .ok_or_else(|| "Unable to read slot clock when registering validator".to_string())?
.epoch(E::slots_per_epoch()); .epoch(E::slots_per_epoch());
let genesis_epoch = slot_clock.genesis_slot().epoch(E::slots_per_epoch()); let genesis_epoch = slot_clock.genesis_slot().epoch(E::slots_per_epoch());
@ -674,6 +675,9 @@ mod test {
const DEFAULT_VALIDATORS: usize = 8; const DEFAULT_VALIDATORS: usize = 8;
const GENESIS_TIME: Duration = Duration::from_secs(42);
const SLOT_DURATION: Duration = Duration::from_secs(1);
type E = MainnetEthSpec; type E = MainnetEthSpec;
fn genesis_epoch() -> Epoch { fn genesis_epoch() -> Epoch {
@ -703,8 +707,7 @@ mod test {
impl TestBuilder { impl TestBuilder {
fn build(self) -> TestScenario { fn build(self) -> TestScenario {
let mut rng = XorShiftRng::from_seed([42; 16]); let mut rng = XorShiftRng::from_seed([42; 16]);
let slot_clock = let slot_clock = TestingSlotClock::new(Slot::new(0), GENESIS_TIME, SLOT_DURATION);
TestingSlotClock::new(Slot::new(0), Duration::from_secs(0), Duration::from_secs(1));
let log = null_logger().unwrap(); let log = null_logger().unwrap();
TestScenario { TestScenario {
@ -737,6 +740,16 @@ mod test {
self self
} }
pub fn set_current_time(self, time: Duration) -> Self {
self.slot_clock.set_current_time(time);
self
}
pub fn assert_prior_to_genesis(self) -> Self {
assert!(self.slot_clock.is_prior_to_genesis().unwrap());
self
}
pub fn register_all_in_doppelganger_protection_if_enabled(self) -> Self { pub fn register_all_in_doppelganger_protection_if_enabled(self) -> Self {
let mut this = self; let mut this = self;
for i in 0..this.validators.len() { for i in 0..this.validators.len() {
@ -1099,6 +1112,17 @@ mod test {
}) })
} }
#[test]
fn register_prior_to_genesis() {
let prior_to_genesis = GENESIS_TIME.checked_sub(SLOT_DURATION).unwrap();
TestBuilder::default()
.build()
.set_current_time(prior_to_genesis)
.assert_prior_to_genesis()
.register_all_in_doppelganger_protection_if_enabled();
}
#[test] #[test]
fn detect_doppelganger_in_starting_epoch() { fn detect_doppelganger_in_starting_epoch() {
let epoch = genesis_epoch() + 1; let epoch = genesis_epoch() + 1;

View File

@ -335,6 +335,9 @@ impl<T: EthSpec> ProductionValidatorClient<T> {
log.clone(), log.clone(),
)); ));
// Ensure all validators are registered in doppelganger protection.
validator_store.register_all_in_doppelganger_protection_if_enabled()?;
info!( info!(
log, log,
"Loaded validator keypair store"; "Loaded validator keypair store";
@ -402,9 +405,6 @@ impl<T: EthSpec> ProductionValidatorClient<T> {
// of making too many changes this close to genesis (<1 week). // of making too many changes this close to genesis (<1 week).
wait_for_genesis(&beacon_nodes, genesis_time, &context).await?; wait_for_genesis(&beacon_nodes, genesis_time, &context).await?;
// Ensure all validators are registered in doppelganger protection.
validator_store.register_all_in_doppelganger_protection_if_enabled()?;
Ok(Self { Ok(Self {
context, context,
duties_service, duties_service,