Skip to content

Commit

Permalink
Improve validator exit test
Browse files Browse the repository at this point in the history
  • Loading branch information
jimmygchen committed Mar 29, 2023
1 parent abf58bb commit 709131e
Show file tree
Hide file tree
Showing 4 changed files with 35 additions and 31 deletions.
17 changes: 5 additions & 12 deletions validator_client/src/http_api/create_signed_voluntary_exit.rs
Original file line number Diff line number Diff line change
@@ -1,22 +1,20 @@
use crate::validator_store::ValidatorStore;
use bls::{PublicKey, PublicKeyBytes};
use slog::{info, Logger};
use slot_clock::{SlotClock, SystemTimeSlotClock};
use slot_clock::SlotClock;
use std::sync::Arc;
use std::time::Duration;
use types::{ChainSpec, Epoch, EthSpec, SignedVoluntaryExit, VoluntaryExit};
use types::{Epoch, EthSpec, SignedVoluntaryExit, VoluntaryExit};

pub async fn create_signed_voluntary_exit<T: 'static + SlotClock + Clone, E: EthSpec>(
pubkey: PublicKey,
maybe_epoch: Option<Epoch>,
validator_store: Arc<ValidatorStore<T, E>>,
spec: Arc<ChainSpec>,
genesis_time: u64,
slot_clock: T,
log: Logger,
) -> Result<SignedVoluntaryExit, warp::Rejection> {
let epoch = match maybe_epoch {
Some(epoch) => epoch,
None => get_current_epoch::<E>(genesis_time, spec).ok_or_else(|| {
None => get_current_epoch::<T, E>(slot_clock).ok_or_else(|| {
warp_utils::reject::custom_server_error("Unable to determine current epoch".to_string())
})?,
};
Expand Down Expand Up @@ -52,11 +50,6 @@ pub async fn create_signed_voluntary_exit<T: 'static + SlotClock + Clone, E: Eth
}

/// Calculates the current epoch from the genesis time and current time.
fn get_current_epoch<E: EthSpec>(genesis_time: u64, spec: Arc<ChainSpec>) -> Option<Epoch> {
let slot_clock = SystemTimeSlotClock::new(
spec.genesis_slot,
Duration::from_secs(genesis_time),
Duration::from_secs(spec.seconds_per_slot),
);
fn get_current_epoch<T: 'static + SlotClock + Clone, E: EthSpec>(slot_clock: T) -> Option<Epoch> {
slot_clock.now().map(|s| s.epoch(E::slots_per_epoch()))
}
17 changes: 7 additions & 10 deletions validator_client/src/http_api/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ pub struct Context<T: SlotClock, E: EthSpec> {
pub spec: ChainSpec,
pub config: Config,
pub log: Logger,
pub genesis_time: u64,
pub slot_clock: T,
pub _phantom: PhantomData<E>,
}

Expand Down Expand Up @@ -192,8 +192,8 @@ pub fn serve<T: 'static + SlotClock + Clone, E: EthSpec>(
let inner_ctx = ctx.clone();
let log_filter = warp::any().map(move || inner_ctx.log.clone());

let inner_genesis_time = ctx.genesis_time;
let genesis_time_filter = warp::any().map(move || inner_genesis_time);
let inner_slot_clock = ctx.slot_clock.clone();
let slot_clock_filter = warp::any().map(move || inner_slot_clock.clone());

let inner_spec = Arc::new(ctx.spec.clone());
let spec_filter = warp::any().map(move || inner_spec.clone());
Expand Down Expand Up @@ -430,7 +430,7 @@ pub fn serve<T: 'static + SlotClock + Clone, E: EthSpec>(
.and(warp::body::json())
.and(validator_dir_filter.clone())
.and(validator_store_filter.clone())
.and(spec_filter.clone())
.and(spec_filter)
.and(signer.clone())
.and(task_executor_filter.clone())
.and_then(
Expand Down Expand Up @@ -918,17 +918,15 @@ pub fn serve<T: 'static + SlotClock + Clone, E: EthSpec>(
.and(warp::query::<api_types::VoluntaryExitQuery>())
.and(warp::path::end())
.and(validator_store_filter.clone())
.and(spec_filter)
.and(genesis_time_filter)
.and(slot_clock_filter)
.and(log_filter.clone())
.and(signer.clone())
.and(task_executor_filter.clone())
.and_then(
|pubkey: PublicKey,
query: api_types::VoluntaryExitQuery,
validator_store: Arc<ValidatorStore<T, E>>,
spec: Arc<ChainSpec>,
genesis_time: u64,
slot_clock: T,
log,
signer,
task_executor: TaskExecutor| {
Expand All @@ -939,8 +937,7 @@ pub fn serve<T: 'static + SlotClock + Clone, E: EthSpec>(
pubkey,
query.epoch,
validator_store,
spec,
genesis_time,
slot_clock,
log,
))?;
Ok(signed_voluntary_exit)
Expand Down
24 changes: 19 additions & 5 deletions validator_client/src/http_api/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ struct ApiTester {
initialized_validators: Arc<RwLock<InitializedValidators>>,
validator_store: Arc<ValidatorStore<TestingSlotClock, E>>,
url: SensitiveUrl,
slot_clock: TestingSlotClock,
_server_shutdown: oneshot::Sender<()>,
_validator_dir: TempDir,
_runtime_shutdown: exit_future::Signal,
Expand Down Expand Up @@ -107,7 +108,7 @@ impl ApiTester {
Hash256::repeat_byte(42),
spec.clone(),
Some(Arc::new(DoppelgangerService::new(log.clone()))),
slot_clock,
slot_clock.clone(),
&config,
executor.clone(),
log.clone(),
Expand All @@ -134,7 +135,7 @@ impl ApiTester {
allow_origin: None,
},
log: log.clone(),
genesis_time,
slot_clock: slot_clock.clone(),
_phantom: PhantomData,
});
let ctx = context.clone();
Expand All @@ -161,6 +162,7 @@ impl ApiTester {
initialized_validators,
validator_store,
url,
slot_clock,
_server_shutdown: shutdown_tx,
_validator_dir: validator_dir,
_runtime_shutdown: runtime_shutdown,
Expand Down Expand Up @@ -499,23 +501,33 @@ impl ApiTester {
self
}

pub async fn test_sign_voluntary_exits(self, index: usize) -> Self {
pub async fn test_sign_voluntary_exits(self, index: usize, maybe_epoch: Option<Epoch>) -> Self {
let validator = &self.client.get_lighthouse_validators().await.unwrap().data[index];
// manually setting validator index in `ValidatorStore`
self.initialized_validators
.write()
.set_index(&validator.voting_pubkey, 0);

let expected_exit_epoch = maybe_epoch.unwrap_or_else(|| self.get_current_epoch());

let resp = self
.client
.post_validator_voluntary_exit(&validator.voting_pubkey, None)
.post_validator_voluntary_exit(&validator.voting_pubkey, maybe_epoch)
.await;

assert!(resp.is_ok());
assert_eq!(resp.unwrap().message.epoch, expected_exit_epoch);

self
}

fn get_current_epoch(&self) -> Epoch {
self.slot_clock
.now()
.map(|s| s.epoch(E::slots_per_epoch()))
.unwrap()
}

pub async fn set_validator_enabled(self, index: usize, enabled: bool) -> Self {
let validator = &self.client.get_lighthouse_validators().await.unwrap().data[index];

Expand Down Expand Up @@ -816,7 +828,9 @@ fn validator_exit() {
.await
.assert_enabled_validators_count(2)
.assert_validators_count(2)
.test_sign_voluntary_exits(0)
.test_sign_voluntary_exits(0, None)
.await
.test_sign_voluntary_exits(0, Some(Epoch::new(256)))
.await;
});
}
Expand Down
8 changes: 4 additions & 4 deletions validator_client/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,7 @@ pub struct ProductionValidatorClient<T: EthSpec> {
doppelganger_service: Option<Arc<DoppelgangerService>>,
preparation_service: PreparationService<SystemTimeSlotClock, T>,
validator_store: Arc<ValidatorStore<SystemTimeSlotClock, T>>,
genesis_time: u64,
slot_clock: SystemTimeSlotClock,
http_api_listen_addr: Option<SocketAddr>,
config: Config,
}
Expand Down Expand Up @@ -462,7 +462,7 @@ impl<T: EthSpec> ProductionValidatorClient<T> {
let sync_committee_service = SyncCommitteeService::new(
duties_service.clone(),
validator_store.clone(),
slot_clock,
slot_clock.clone(),
beacon_nodes.clone(),
context.service_context("sync_committee".into()),
);
Expand All @@ -483,7 +483,7 @@ impl<T: EthSpec> ProductionValidatorClient<T> {
preparation_service,
validator_store,
config,
genesis_time,
slot_clock,
http_api_listen_addr: None,
})
}
Expand Down Expand Up @@ -546,7 +546,7 @@ impl<T: EthSpec> ProductionValidatorClient<T> {
graffiti_flag: self.config.graffiti,
spec: self.context.eth2_config.spec.clone(),
config: self.config.http_api.clone(),
genesis_time: self.genesis_time,
slot_clock: self.slot_clock.clone(),
log: log.clone(),
_phantom: PhantomData,
});
Expand Down

0 comments on commit 709131e

Please sign in to comment.