Files
lighthouse/lcli/src/skip_slots.rs
Michael Sproul 61962898e2 In-memory tree states (#5533)
* Consensus changes

* EF tests

* lcli

* common and watch

* account manager

* cargo

* fork choice

* promise cache

* beacon chain

* interop genesis

* http api

* lighthouse

* op pool

* beacon chain misc

* parallel state cache

* store

* fix issues in store

* IT COMPILES

* Remove some unnecessary module qualification

* Revert Arced pubkey optimization (#5536)

* Merge remote-tracking branch 'origin/unstable' into tree-states-memory

* Fix caching, rebasing and some tests

* Remove unused deps

* Merge remote-tracking branch 'origin/unstable' into tree-states-memory

* Small cleanups

* Revert shuffling cache/promise cache changes

* Fix state advance bugs

* Fix shuffling tests

* Remove some resolved FIXMEs

* Remove StateProcessingStrategy

* Optimise withdrawals calculation

* Don't reorg if state cache is missed

* Remove inconsistent state func

* Fix beta compiler

* Rebase early, rebase often

* Fix state caching behaviour

* Update to milhouse release

* Fix on-disk consensus context format

* Merge remote-tracking branch 'origin/unstable' into tree-states-memory

* Squashed commit of the following:

commit 3a16649023
Author: Michael Sproul <michael@sigmaprime.io>
Date:   Thu Apr 18 14:26:09 2024 +1000

    Fix on-disk consensus context format

* Keep indexed attestations, thanks Sean

* Merge branch 'on-disk-consensus-context' into tree-states-memory

* Merge branch 'unstable' into tree-states-memory

* Address half of Sean's review

* More simplifications from Sean's review

* Cache state after get_advanced_hot_state
2024-04-24 01:22:36 +00:00

159 lines
5.5 KiB
Rust

//! # Skip-Slots
//!
//! Use this tool to process a `BeaconState` through empty slots. Useful for benchmarking or
//! troubleshooting consensus failures.
//!
//! It can load states from file or pull them from a beaconAPI. States pulled from a beaconAPI can
//! be saved to disk to reduce future calls to that server.
//!
//! ## Examples
//!
//! ### Example 1.
//!
//! Download a state from a HTTP endpoint and skip forward an epoch, twice (the initial state is
//! advanced 32 slots twice, rather than it being advanced 64 slots):
//!
//! ```ignore
//! lcli skip-slots \
//! --beacon-url http://localhost:5052 \
//! --state-id 0x3cdc33cd02713d8d6cc33a6dbe2d3a5bf9af1d357de0d175a403496486ff845e \\
//! --slots 32 \
//! --runs 2
//! ```
//!
//! ### Example 2.
//!
//! Download a state to a SSZ file (without modifying it):
//!
//! ```ignore
//! lcli skip-slots \
//! --beacon-url http://localhost:5052 \
//! --state-id 0x3cdc33cd02713d8d6cc33a6dbe2d3a5bf9af1d357de0d175a403496486ff845e \
//! --slots 0 \
//! --runs 0 \
//! --output-path /tmp/state-0x3cdc.ssz
//! ```
//!
//! ### Example 3.
//!
//! Do two runs over the state that was downloaded in the previous example:
//!
//! ```ignore
//! lcli skip-slots \
//! --pre-state-path /tmp/state-0x3cdc.ssz \
//! --slots 32 \
//! --runs 2
//! ```
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 ssz::Encode;
use state_processing::state_advance::{complete_state_advance, partial_state_advance};
use state_processing::AllCaches;
use std::fs::File;
use std::io::prelude::*;
use std::path::PathBuf;
use std::time::{Duration, Instant};
use types::{BeaconState, EthSpec, Hash256};
const HTTP_TIMEOUT: Duration = Duration::from_secs(10);
pub fn run<E: EthSpec>(
env: Environment<E>,
network_config: Eth2NetworkConfig,
matches: &ArgMatches,
) -> Result<(), String> {
let spec = &network_config.chain_spec::<E>()?;
let executor = env.core_context().executor;
let output_path: Option<PathBuf> = parse_optional(matches, "output-path")?;
let state_path: Option<PathBuf> = parse_optional(matches, "pre-state-path")?;
let beacon_url: Option<SensitiveUrl> = parse_optional(matches, "beacon-url")?;
let runs: usize = parse_required(matches, "runs")?;
let slots: u64 = parse_required(matches, "slots")?;
let cli_state_root: Option<Hash256> = parse_optional(matches, "state-root")?;
let partial: bool = matches.is_present("partial-state-advance");
info!("Using {} spec", E::spec_name());
info!("Advancing {} slots", slots);
info!("Doing {} runs", runs);
let (mut state, state_root) = match (state_path, beacon_url) {
(Some(state_path), None) => {
info!("State path: {:?}", state_path);
let state = load_from_ssz_with(&state_path, spec, BeaconState::from_ssz_bytes)?;
(state, None)
}
(None, Some(beacon_url)) => {
let state_id: StateId = parse_required(matches, "state-id")?;
let client = BeaconNodeHttpClient::new(beacon_url, Timeouts::set_all(HTTP_TIMEOUT));
let state = executor
.handle()
.ok_or("shutdown in progress")?
.block_on(async move {
client
.get_debug_beacon_states::<E>(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;
let state_root = match state_id {
StateId::Root(root) => Some(root),
_ => None,
};
(state, state_root)
}
_ => return Err("must supply either --state-path or --beacon-url".into()),
};
let mut post_state = None;
let initial_slot = state.slot();
let target_slot = initial_slot + slots;
state
.build_all_caches(spec)
.map_err(|e| format!("Unable to build caches: {:?}", e))?;
let state_root = if let Some(root) = cli_state_root.or(state_root) {
root
} else {
state
.update_tree_hash_cache()
.map_err(|e| format!("Unable to build THC: {:?}", e))?
};
for i in 0..runs {
let mut state = state.clone();
let start = Instant::now();
if partial {
partial_state_advance(&mut state, Some(state_root), target_slot, spec)
.map_err(|e| format!("Unable to perform partial advance: {:?}", e))?;
} else {
complete_state_advance(&mut state, Some(state_root), target_slot, spec)
.map_err(|e| format!("Unable to perform complete advance: {:?}", e))?;
}
let duration = Instant::now().duration_since(start);
info!("Run {}: {:?}", i, duration);
post_state = Some(state);
}
if let (Some(post_state), Some(output_path)) = (post_state, output_path) {
let mut output_file = File::create(output_path)
.map_err(|e| format!("Unable to create output file: {:?}", e))?;
output_file
.write_all(&post_state.as_ssz_bytes())
.map_err(|e| format!("Unable to write to output file: {:?}", e))?;
}
Ok(())
}