Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Merged by Bors] - Fix timing issue in obtaining the Fork #2158

Closed
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 10 additions & 5 deletions validator_client/src/fork_service.rs
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,11 @@ impl<T: SlotClock + 'static, E: EthSpec> ForkServiceBuilder<T, E> {
}
}

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

pub fn slot_clock(mut self, slot_clock: T) -> Self {
self.slot_clock = Some(slot_clock);
self
Expand All @@ -52,7 +57,7 @@ impl<T: SlotClock + 'static, E: EthSpec> ForkServiceBuilder<T, E> {
pub fn build(self) -> Result<ForkService<T, E>, String> {
Ok(ForkService {
inner: Arc::new(Inner {
fork: RwLock::new(self.fork),
fork: RwLock::new(self.fork.ok_or("Cannot build ForkService without fork")?),
slot_clock: self
.slot_clock
.ok_or("Cannot build ForkService without slot_clock")?,
Expand Down Expand Up @@ -100,7 +105,7 @@ impl<E: EthSpec> ForkServiceBuilder<slot_clock::TestingSlotClock, E> {

/// Helper to minimise `Arc` usage.
pub struct Inner<T, E: EthSpec> {
fork: RwLock<Option<Fork>>,
fork: RwLock<Fork>,
beacon_nodes: Arc<BeaconNodeFallback<T, E>>,
log: Logger,
slot_clock: T,
Expand Down Expand Up @@ -129,7 +134,7 @@ impl<T, E: EthSpec> Deref for ForkService<T, E> {

impl<T: SlotClock + 'static, E: EthSpec> ForkService<T, E> {
/// Returns the last fork downloaded from the beacon node, if any.
pub fn fork(&self) -> Option<Fork> {
pub fn fork(&self) -> Fork {
*self.fork.read()
}

Expand Down Expand Up @@ -201,8 +206,8 @@ impl<T: SlotClock + 'static, E: EthSpec> ForkService<T, E> {
.await
.map_err(|_| ())?;

if self.fork.read().as_ref() != Some(&fork) {
*(self.fork.write()) = Some(fork);
if *(self.fork.read()) != fork {
*(self.fork.write()) = fork;
}

debug!(self.log, "Fork update success");
Expand Down
36 changes: 32 additions & 4 deletions validator_client/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ use block_service::{BlockService, BlockServiceBuilder};
use clap::ArgMatches;
use duties_service::{DutiesService, DutiesServiceBuilder};
use environment::RuntimeContext;
use eth2::types::StateId;
use eth2::{reqwest::ClientBuilder, BeaconNodeHttpClient, StatusCode, Url};
use fork_service::{ForkService, ForkServiceBuilder};
use futures::channel::mpsc;
Expand All @@ -43,7 +44,7 @@ use std::net::SocketAddr;
use std::sync::Arc;
use std::time::{SystemTime, UNIX_EPOCH};
use tokio::time::{sleep, Duration};
use types::{EthSpec, Hash256};
use types::{EthSpec, Fork, Hash256};
use validator_store::ValidatorStore;

/// The interval between attempts to contact the beacon node during startup.
Expand Down Expand Up @@ -234,7 +235,7 @@ impl<T: EthSpec> ProductionValidatorClient<T> {
BeaconNodeFallback::new(candidates, context.eth2_config.spec.clone(), log.clone());

// Perform some potentially long-running initialization tasks.
let (genesis_time, genesis_validators_root) = tokio::select! {
let (genesis_time, genesis_validators_root, fork) = tokio::select! {
tuple = init_from_beacon_node(&beacon_nodes, &context) => tuple?,
() = context.executor.exit() => return Err("Shutting down".to_string())
};
Expand All @@ -255,6 +256,7 @@ impl<T: EthSpec> ProductionValidatorClient<T> {
start_fallback_updater_service(context.clone(), beacon_nodes.clone())?;

let fork_service = ForkServiceBuilder::new()
.fork(fork)
.slot_clock(slot_clock.clone())
.beacon_nodes(beacon_nodes.clone())
.log(log.clone())
Expand Down Expand Up @@ -394,7 +396,7 @@ impl<T: EthSpec> ProductionValidatorClient<T> {
async fn init_from_beacon_node<E: EthSpec>(
beacon_nodes: &BeaconNodeFallback<SystemTimeSlotClock, E>,
context: &RuntimeContext<E>,
) -> Result<(u64, Hash256), String> {
) -> Result<(u64, Hash256, Fork), String> {
loop {
beacon_nodes.update_unready_candidates().await;
let num_available = beacon_nodes.num_available().await;
Expand Down Expand Up @@ -453,7 +455,33 @@ async fn init_from_beacon_node<E: EthSpec>(
sleep(RETRY_DELAY).await;
};

Ok((genesis.genesis_time, genesis.genesis_validators_root))
let fork = loop {
match beacon_nodes
.first_success(RequireSynced::No, |node| async move {
node.get_beacon_states_fork(StateId::Head).await
})
.await
{
Ok(Some(fork)) => break fork.data,
Ok(None) => {
info!(
context.log(),
"Failed to get fork, state not found";
);
}
Err(errors) => {
error!(
context.log(),
"Failed to get fork";
"error" => %errors
);
}
}

sleep(RETRY_DELAY).await;
};

Ok((genesis.genesis_time, genesis.genesis_validators_root, fork))
}

async fn wait_for_genesis<E: EthSpec>(
Expand Down
22 changes: 8 additions & 14 deletions validator_client/src/validator_store.rs
Original file line number Diff line number Diff line change
Expand Up @@ -120,30 +120,24 @@ impl<T: SlotClock + 'static, E: EthSpec> ValidatorStore<T, E> {
self.validators.read().num_enabled()
}

fn fork(&self) -> Option<Fork> {
if self.fork_service.fork().is_none() {
error!(
self.log,
"Unable to get Fork for signing";
);
}
fn fork(&self) -> Fork {
self.fork_service.fork()
}

pub fn randao_reveal(&self, validator_pubkey: &PublicKey, epoch: Epoch) -> Option<Signature> {
self.validators
.read()
.voting_keypair(validator_pubkey)
.and_then(|voting_keypair| {
.map(|voting_keypair| {
let domain = self.spec.get_domain(
epoch,
Domain::Randao,
&self.fork()?,
&self.fork(),
self.genesis_validators_root,
);
let message = epoch.signing_root(domain);

Some(voting_keypair.sk.sign(message))
voting_keypair.sk.sign(message)
})
}

Expand All @@ -165,7 +159,7 @@ impl<T: SlotClock + 'static, E: EthSpec> ValidatorStore<T, E> {
}

// Check for slashing conditions.
let fork = self.fork()?;
let fork = self.fork();
let domain = self.spec.get_domain(
block.epoch(),
Domain::BeaconProposer,
Expand Down Expand Up @@ -237,7 +231,7 @@ impl<T: SlotClock + 'static, E: EthSpec> ValidatorStore<T, E> {
}

// Checking for slashing conditions.
let fork = self.fork()?;
let fork = self.fork();

let domain = self.spec.get_domain(
attestation.data.target.epoch,
Expand Down Expand Up @@ -339,7 +333,7 @@ impl<T: SlotClock + 'static, E: EthSpec> ValidatorStore<T, E> {
aggregate,
Some(selection_proof),
&voting_keypair.sk,
&self.fork()?,
&self.fork(),
self.genesis_validators_root,
&self.spec,
))
Expand All @@ -360,7 +354,7 @@ impl<T: SlotClock + 'static, E: EthSpec> ValidatorStore<T, E> {
Some(SelectionProof::new::<E>(
slot,
&voting_keypair.sk,
&self.fork()?,
&self.fork(),
self.genesis_validators_root,
&self.spec,
))
Expand Down