Skip to content

Commit

Permalink
Add merge messages to one file
Browse files Browse the repository at this point in the history
  • Loading branch information
chong-he committed Dec 20, 2024
1 parent 5a4f928 commit 6cdc908
Show file tree
Hide file tree
Showing 2 changed files with 49 additions and 7 deletions.
1 change: 1 addition & 0 deletions lighthouse/tests/validator_manager.rs
Original file line number Diff line number Diff line change
Expand Up @@ -503,6 +503,7 @@ pub fn validator_exit_defaults() {
beacon_url: Some(SensitiveUrl::parse("http://localhost:5052").unwrap()),
exit_epoch: None,
signature: false,
merge: false,
exit_status: false,
};
assert_eq!(expected, config);
Expand Down
55 changes: 48 additions & 7 deletions validator_manager/src/exit_validators.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ pub const VALIDATOR_FLAG: &str = "validators";
pub const EXIT_EPOCH_FLAG: &str = "exit-epoch";
pub const SIGNATURE_FLAG: &str = "signature";
pub const EXIT_STATUS_FLAG: &str = "status";
pub const MERGE_FLAG: &str = "merge";

pub fn cli_app() -> Command {
Command::new(CMD)
Expand Down Expand Up @@ -68,16 +69,37 @@ pub fn cli_app() -> Command {
Arg::new(EXIT_EPOCH_FLAG)
.long(EXIT_EPOCH_FLAG)
.value_name("EPOCH")
.help("Provide the minimum epoch for processing voluntary exit.")
.help(
"Provide the minimum epoch for processing voluntary exit. \
This flag is typically used in combination with `--signature` to \
save the voluntary exit signature to a file for future use.",
)
.action(ArgAction::Set)
.display_order(0),
)
.arg(
Arg::new(SIGNATURE_FLAG)
.long(SIGNATURE_FLAG)
.help("Display the signature of the voluntary exit.")
.help(
"Generate the voluntary exit signature and save it to a file \
named {validator_pubkey}.json. Note: Using this without the \
`--beacon-node` flag will not publish the voluntary exit to the network.",
)
.help_heading(FLAG_HEADER)
.action(ArgAction::SetTrue)
.display_order(0),
)
.arg(
Arg::new(MERGE_FLAG)
.long(MERGE_FLAG)
.help(
"Merge the generated voluntary exit signatures into a single file named \
`all_validators.json`. This flag has to be used together with \
the `--signature` flag.",
)
.help_heading(FLAG_HEADER)
.action(ArgAction::SetTrue)
.requires(SIGNATURE_FLAG)
.display_order(0),
)
.arg(
Expand All @@ -99,6 +121,7 @@ pub struct ExitConfig {
pub beacon_url: Option<SensitiveUrl>,
pub exit_epoch: Option<Epoch>,
pub signature: bool,
pub merge: bool,
pub exit_status: bool,
}

Expand All @@ -123,6 +146,7 @@ impl ExitConfig {
beacon_url: clap_utils::parse_optional(matches, BEACON_URL_FLAG)?,
exit_epoch: clap_utils::parse_optional(matches, EXIT_EPOCH_FLAG)?,
signature: matches.get_flag(SIGNATURE_FLAG),
merge: matches.get_flag(MERGE_FLAG),
exit_status: matches.get_flag(EXIT_STATUS_FLAG),
})
}
Expand All @@ -149,6 +173,7 @@ async fn run<E: EthSpec>(config: ExitConfig) -> Result<(), String> {
beacon_url,
exit_epoch,
signature,
merge,
exit_status,
} = config;

Expand All @@ -158,6 +183,8 @@ async fn run<E: EthSpec>(config: ExitConfig) -> Result<(), String> {
validators_to_exit = validators.iter().map(|v| v.validating_pubkey).collect();
}

let mut exit_message_all = Vec::new();

for validator_to_exit in validators_to_exit {
// Check that the validators_to_exit is in the validator client
if !validators
Expand All @@ -174,13 +201,18 @@ 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) => {
// Save the exit message to a JSON file
let file_path = format!("validator_{}.json", validator_to_exit);
std::fs::write(&file_path, json)
.map_err(|e| format!("Failed to write exit message to file: {}", e))?;
println!("Exit message saved to {}", file_path);
// Save the exit message to JSON file(s)
if merge {
exit_message_all.push(json.clone());
} else {
let file_path = format!("{}.json", validator_to_exit);
std::fs::write(&file_path, json)
.map_err(|e| format!("Failed to write voluntary exit message to file: {}", e))?;
println!("Voluntary exit message saved to {}", file_path);
}
}
Err(e) => eprintln!("Failed to serialize voluntary exit message: {}", e),
}
Expand Down Expand Up @@ -324,6 +356,14 @@ async fn run<E: EthSpec>(config: ExitConfig) -> Result<(), String> {
}
}
}

if merge {
let all_json = serde_json::to_string(&exit_message_all)
.map_err(|e| format!("Failed to serialize voluntary exit message: {}", e))?;
std::fs::write("all_validators.json", all_json)
.map_err(|e| format!("Failed to write all voluntary exit messages to file: {}", e))?;
println!("All voluntary exit messages save to all_validators.json.")
}
Ok(())
}

Expand Down Expand Up @@ -484,6 +524,7 @@ mod test {
beacon_url: Some(beacon_url),
exit_epoch: None,
signature: false,
merge: false,
exit_status: false,
});

Expand Down

0 comments on commit 6cdc908

Please sign in to comment.