lighthouse/lcli/src/state_root.rs
Michael Sproul 420e9c490e Add state-root command and network support to lcli (#4492)
## Proposed Changes

* Add `lcli state-root` command for computing the hash tree root of a `BeaconState`.
* Add a `--network` flag which can be used instead of `--testnet-dir` to set the network, e.g. Mainnet, Goerli, Gnosis.
* Use the new network flag in `transition-blocks`, `skip-slots`, and `block-root`, which previously only supported mainnet.
* **BREAKING CHANGE** Remove the default value of `~/.lighthouse/testnet` from `--testnet-dir`. This may have made sense in previous versions where `lcli` was more testnet focussed, but IMO it is an unnecessary complication and foot-gun today.
2023-07-12 07:05:58 +00:00

77 lines
2.6 KiB
Rust

use crate::transition_blocks::load_from_ssz_with;
use clap::ArgMatches;
use clap_utils::{parse_optional, parse_required};
use environment::Environment;
use eth2::{types::StateId, BeaconNodeHttpClient, SensitiveUrl, Timeouts};
use eth2_network_config::Eth2NetworkConfig;
use std::path::PathBuf;
use std::time::{Duration, Instant};
use types::{BeaconState, EthSpec};
const HTTP_TIMEOUT: Duration = Duration::from_secs(10);
pub fn run<T: EthSpec>(
env: Environment<T>,
network_config: Eth2NetworkConfig,
matches: &ArgMatches,
) -> Result<(), String> {
let executor = env.core_context().executor;
let spec = &network_config.chain_spec::<T>()?;
let state_path: Option<PathBuf> = parse_optional(matches, "state-path")?;
let beacon_url: Option<SensitiveUrl> = parse_optional(matches, "beacon-url")?;
let runs: usize = parse_required(matches, "runs")?;
info!(
"Using {} network ({} spec)",
spec.config_name.as_deref().unwrap_or("unknown"),
T::spec_name()
);
info!("Doing {} runs", runs);
let state = match (state_path, beacon_url) {
(Some(state_path), None) => {
info!("State path: {:?}", state_path);
load_from_ssz_with(&state_path, spec, BeaconState::from_ssz_bytes)?
}
(None, Some(beacon_url)) => {
let state_id: StateId = parse_required(matches, "state-id")?;
let client = BeaconNodeHttpClient::new(beacon_url, Timeouts::set_all(HTTP_TIMEOUT));
executor
.handle()
.ok_or("shutdown in progress")?
.block_on(async move {
client
.get_debug_beacon_states::<T>(state_id)
.await
.map_err(|e| format!("Failed to download state: {:?}", e))
})
.map_err(|e| format!("Failed to complete task: {:?}", e))?
.ok_or_else(|| format!("Unable to locate state at {:?}", state_id))?
.data
}
_ => return Err("must supply either --state-path or --beacon-url".into()),
};
/*
* Perform the core "runs".
*/
let mut state_root = None;
for i in 0..runs {
let mut state = state.clone();
let timer = Instant::now();
state_root = Some(
state
.update_tree_hash_cache()
.map_err(|e| format!("error computing state root: {e:?}"))?,
);
info!("Run {}: {:?}", i, timer.elapsed());
}
if let Some(state_root) = state_root {
info!("State root is {:?}", state_root);
}
Ok(())
}