Skip to content

Commit

Permalink
Add test to lighthouse/tests
Browse files Browse the repository at this point in the history
  • Loading branch information
chong-he committed Dec 17, 2024
1 parent 375f8a2 commit d7421b0
Show file tree
Hide file tree
Showing 2 changed files with 85 additions and 44 deletions.
45 changes: 45 additions & 0 deletions lighthouse/tests/validator_manager.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ use types::*;
use validator_manager::{
create_validators::CreateConfig,
delete_validators::DeleteConfig,
exit_validators::ExitConfig,
import_validators::ImportConfig,
list_validators::ListConfig,
move_validators::{MoveConfig, PasswordSource, Validators},
Expand Down Expand Up @@ -119,6 +120,12 @@ impl CommandLineTest<DeleteConfig> {
}
}

impl CommandLineTest<ExitConfig> {
fn validators_exit() -> Self {
Self::default().flag("exit", None)
}
}

#[test]
pub fn validator_create_without_output_path() {
CommandLineTest::validators_create().assert_failed();
Expand Down Expand Up @@ -468,3 +475,41 @@ pub fn validator_delete_defaults() {
assert_eq!(expected, config);
});
}

#[test]
pub fn validator_delete_missing_validator_flag() {
CommandLineTest::validators_delete()
.flag("--vc-token", Some("./token.json"))
.assert_failed();
}

#[test]
pub fn validator_exit_defaults() {
CommandLineTest::validators_exit()
.flag(
"--validators",
Some(&format!("{},{}", EXAMPLE_PUBKEY_0, EXAMPLE_PUBKEY_1)),
)
.flag("--vc-token", Some("./token.json"))
.assert_success(|config| {
let expected = ExitConfig {
vc_url: SensitiveUrl::parse("http://localhost:5062").unwrap(),
vc_token_path: PathBuf::from("./token.json"),
validators_to_exit: vec![
PublicKeyBytes::from_str(EXAMPLE_PUBKEY_0).unwrap(),
PublicKeyBytes::from_str(EXAMPLE_PUBKEY_1).unwrap(),
],
beacon_url: None,
exit_epoch: None,
signature: false,
};
assert_eq!(expected, config);
});
}

#[test]
pub fn validator_exit_missing_validator_flag() {
CommandLineTest::validators_exit()
.flag("--vc-token", Some("./token.json"))
.assert_failed();
}
84 changes: 40 additions & 44 deletions validator_manager/src/exit_validators.rs
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@ pub fn cli_app() -> Command {
.value_name("STRING")
.help("List of validators (pubkey) to exit.")
.action(ArgAction::Set)
.required(true)
.display_order(0),
)
.arg(
Expand Down Expand Up @@ -159,7 +160,7 @@ async fn run<E: EthSpec>(config: ExitConfig) -> Result<(), String> {
if signature {
let exit_message_json = serde_json::to_string(&exit_message.data);
match exit_message_json {
Ok(json) => println!("{}", json),
Ok(json) => println!("Validator {}: {}", validator_to_exit, json),
Err(e) => eprintln!("Failed to serialize voluntary exit message: {}", e),
}
}
Expand Down Expand Up @@ -248,56 +249,51 @@ async fn run<E: EthSpec>(config: ExitConfig) -> Result<(), String> {
"Successfully validated and published voluntary exit for validator {}",
validator_to_exit
);
}
// sleep(Duration::from_secs(spec.seconds_per_slot)).await;

// sleep(Duration::from_secs(spec.seconds_per_slot)).await;

// Check validator status after publishing voluntary exit
match validator_data.status {
ValidatorStatus::ActiveExiting => {
let exit_epoch = validator_data.validator.exit_epoch;
let withdrawal_epoch = validator_data.validator.withdrawable_epoch;
// Check validator status after publishing voluntary exit
match validator_data.status {
ValidatorStatus::ActiveExiting => {
let exit_epoch = validator_data.validator.exit_epoch;
let withdrawal_epoch = validator_data.validator.withdrawable_epoch;

// let slot_clock = SystemTimeSlotClock::new(
// spec.genesis_slot,
// Duration::from_secs(genesis_data.genesis_time),
// Duration::from_secs(spec.config().seconds_per_slot),
// );
// let slot_clock = SystemTimeSlotClock::new(
// spec.genesis_slot,
// Duration::from_secs(genesis_data.genesis_time),
// Duration::from_secs(spec.config().seconds_per_slot),
// );

// let current_epoch = get_current_epoch::<SystemTimeSlotClock, E>(slot_clock)
// .ok_or_else(|| "Unable to determine current epoch".to_string())?;
// let current_epoch = get_current_epoch::<SystemTimeSlotClock, E>(slot_clock)
// .ok_or_else(|| "Unable to determine current epoch".to_string())?;

eprintln!("Voluntary exit has been accepted into the beacon chain, but not yet finalized. \
eprintln!("Voluntary exit has been accepted into the beacon chain, but not yet finalized. \
Finalization may take several minutes or longer. Before finalization there is a low \
probability that the exit may be reverted.");
eprintln!(
"Current epoch: {}, Exit epoch: {}, Withdrawable epoch: {}",
current_epoch, exit_epoch, withdrawal_epoch
);
eprintln!("Please keep your validator running till exit epoch");
eprintln!(
"Exit epoch in approximately {} secs",
(exit_epoch - current_epoch)
* spec.seconds_per_slot
* E::slots_per_epoch()
);
}

_ => {
eprintln!(
"Waiting for voluntary exit to be accepted into the beacon chain..."
)
} // 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()))
// }

// let spec = ChainSpec::mainnet();

// let current_epoch =
// get_current_epoch::<E>(genesis_time, &spec).ok_or("Failed to get current epoch")?;
//let current_epoch = get_current_epoch::<E>(genesis_data.genesis_time, spec);
eprintln!(
"Current epoch: {}, Exit epoch: {}, Withdrawable epoch: {}",
current_epoch, exit_epoch, withdrawal_epoch
);
eprintln!("Please keep your validator running till exit epoch");
eprintln!(
"Exit epoch in approximately {} secs",
(exit_epoch - current_epoch) * spec.seconds_per_slot * E::slots_per_epoch()
);
}

_ => {
eprintln!("Waiting for voluntary exit to be accepted into the beacon chain...")
} // 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()))
// }

// let spec = ChainSpec::mainnet();

// let current_epoch =
// get_current_epoch::<E>(genesis_time, &spec).ok_or("Failed to get current epoch")?;
//let current_epoch = get_current_epoch::<E>(genesis_data.genesis_time, spec);
}
}
}
Expand Down

0 comments on commit d7421b0

Please sign in to comment.