mirror of
https://github.com/sigp/lighthouse.git
synced 2026-03-15 19:02:42 +00:00
* upgrade clap to v4.5 * cli fixes * Merge branch 'unstable' of https://github.com/sigp/lighthouse into upgrade-clap-cli * value parser for mnemonic * Merge branch 'unstable' of https://github.com/sigp/lighthouse into upgrade-clap-cli * merge unstable * default --format val * fix eth sim * fix eth sim * merge conflicts * resolve beta compiler issue * add num args, version * add custom flag parser, make rate limiter flags clap friendly * remove unneeded check * fmt * update * alphabetic order * resolve merge conflict * fix test * resolve conflicts * fix test * revert removed if statement * fmt got me again * fix broken flag * make cli * make cli * update * remove -e files * update * cli help updates * Merge branch 'unstable' of https://github.com/sigp/lighthouse into upgrade-clap-cli * cli help updates * md files * merge conflict * merge conflicts * md * help text, text width, and a few flag fixes * fmt * merge * revert * revert * resolve merge conflicts * merge conflicts * revert simulator changes * require at least one arg * fix eth sim cli * resolve merge conflicts * book changes * md changes * cli check * cli check * retry cli check * retry cli check * Merge branch 'unstable' of https://github.com/sigp/lighthouse into upgrade-clap-cli * cli * Merge remote-tracking branch 'origin/unstable' into upgrade-clap-cli * Update CLI docs for Goerli removal * Fix cargo lock
87 lines
3.3 KiB
Rust
87 lines
3.3 KiB
Rust
use account_utils::{eth2_keystore::keypair_from_secret, mnemonic_from_phrase};
|
|
use clap::ArgMatches;
|
|
use eth2_network_config::Eth2NetworkConfig;
|
|
use eth2_wallet::bip39::Seed;
|
|
use eth2_wallet::{recover_validator_secret_from_mnemonic, KeyType};
|
|
use ssz::Encode;
|
|
use state_processing::common::DepositDataTree;
|
|
use std::fs::File;
|
|
use std::io::{Read, Write};
|
|
use std::path::PathBuf;
|
|
use tree_hash::TreeHash;
|
|
use types::{BeaconState, DepositData, EthSpec, Hash256, SignatureBytes, DEPOSIT_TREE_DEPTH};
|
|
|
|
pub fn run<E: EthSpec>(testnet_dir: PathBuf, matches: &ArgMatches) -> Result<(), String> {
|
|
let path = matches
|
|
.get_one::<String>("ssz-state")
|
|
.ok_or("ssz-state not specified")?
|
|
.parse::<PathBuf>()
|
|
.map_err(|e| format!("Unable to parse ssz-state: {}", e))?;
|
|
|
|
let mnemonic_phrase = matches
|
|
.get_one::<String>("mnemonic")
|
|
.ok_or("mnemonic not specified")?;
|
|
|
|
let eth2_network_config = Eth2NetworkConfig::load(testnet_dir)?;
|
|
let spec = ð2_network_config.chain_spec::<E>()?;
|
|
|
|
let mut state: BeaconState<E> = {
|
|
let mut file = File::open(&path).map_err(|e| format!("Unable to open file: {}", e))?;
|
|
|
|
let mut ssz = vec![];
|
|
|
|
file.read_to_end(&mut ssz)
|
|
.map_err(|e| format!("Unable to read file: {}", e))?;
|
|
|
|
BeaconState::from_ssz_bytes(&ssz, spec)
|
|
.map_err(|e| format!("Unable to decode SSZ: {:?}", e))?
|
|
};
|
|
|
|
let mnemonic = mnemonic_from_phrase(mnemonic_phrase)?;
|
|
let seed = Seed::new(&mnemonic, "");
|
|
|
|
let mut deposit_tree = DepositDataTree::create(&[], 0, DEPOSIT_TREE_DEPTH);
|
|
let mut deposit_root = Hash256::zero();
|
|
let validators = state.validators_mut();
|
|
for index in 0..validators.len() {
|
|
let (secret, _) =
|
|
recover_validator_secret_from_mnemonic(seed.as_bytes(), index as u32, KeyType::Voting)
|
|
.map_err(|e| format!("Unable to generate validator key: {:?}", e))?;
|
|
|
|
let keypair = keypair_from_secret(secret.as_bytes())
|
|
.map_err(|e| format!("Unable build keystore: {:?}", e))?;
|
|
|
|
eprintln!("{}: {}", index, keypair.pk);
|
|
|
|
validators.get_mut(index).unwrap().pubkey = keypair.pk.into();
|
|
|
|
// Update the deposit tree.
|
|
let mut deposit_data = DepositData {
|
|
pubkey: validators.get(index).unwrap().pubkey,
|
|
// Set this to a junk value since it's very time consuming to generate the withdrawal
|
|
// keys and it's not useful for the time being.
|
|
withdrawal_credentials: Hash256::zero(),
|
|
amount: spec.min_deposit_amount,
|
|
signature: SignatureBytes::empty(),
|
|
};
|
|
deposit_data.signature = deposit_data.create_signature(&keypair.sk, spec);
|
|
deposit_tree
|
|
.push_leaf(deposit_data.tree_hash_root())
|
|
.map_err(|e| format!("failed to create deposit tree: {:?}", e))?;
|
|
deposit_root = deposit_tree.root();
|
|
}
|
|
|
|
// Update the genesis validators root since we changed the validators.
|
|
*state.genesis_validators_root_mut() = state.validators().tree_hash_root();
|
|
|
|
// Update the deposit root with our simulated deposits.
|
|
state.eth1_data_mut().deposit_root = deposit_root;
|
|
|
|
let mut file = File::create(path).map_err(|e| format!("Unable to create file: {}", e))?;
|
|
|
|
file.write_all(&state.as_ssz_bytes())
|
|
.map_err(|e| format!("Unable to write to file: {}", e))?;
|
|
|
|
Ok(())
|
|
}
|