mirror of
https://github.com/sigp/lighthouse.git
synced 2026-04-17 04:48:21 +00:00
Merge remote-tracking branch 'origin/unstable' into tree-states
This commit is contained in:
@@ -1,7 +1,7 @@
|
||||
use clap::ArgMatches;
|
||||
use environment::Environment;
|
||||
use eth2_network_config::Eth2NetworkConfig;
|
||||
use genesis::{Eth1Config, Eth1GenesisService};
|
||||
use genesis::{Eth1Config, Eth1Endpoint, Eth1GenesisService};
|
||||
use sensitive_url::SensitiveUrl;
|
||||
use ssz::Encode;
|
||||
use std::cmp::max;
|
||||
@@ -35,11 +35,12 @@ pub fn run<T: EthSpec>(
|
||||
|
||||
let mut config = Eth1Config::default();
|
||||
if let Some(v) = endpoints.clone() {
|
||||
config.endpoints = v
|
||||
let endpoints = v
|
||||
.iter()
|
||||
.map(|s| SensitiveUrl::parse(s))
|
||||
.collect::<Result<_, _>>()
|
||||
.map_err(|e| format!("Unable to parse eth1 endpoint URL: {:?}", e))?;
|
||||
config.endpoints = Eth1Endpoint::NoAuth(endpoints);
|
||||
}
|
||||
config.deposit_contract_address = format!("{:?}", spec.deposit_contract_address);
|
||||
config.deposit_contract_deploy_block = eth2_network_config.deposit_contract_deploy_block;
|
||||
|
||||
48
lcli/src/indexed_attestations.rs
Normal file
48
lcli/src/indexed_attestations.rs
Normal file
@@ -0,0 +1,48 @@
|
||||
use clap::ArgMatches;
|
||||
use clap_utils::parse_required;
|
||||
use state_processing::common::get_indexed_attestation;
|
||||
use std::fs::File;
|
||||
use std::io::Read;
|
||||
use std::path::{Path, PathBuf};
|
||||
use types::*;
|
||||
|
||||
fn read_file_bytes(filename: &Path) -> Result<Vec<u8>, String> {
|
||||
let mut bytes = vec![];
|
||||
let mut file = File::open(filename)
|
||||
.map_err(|e| format!("Unable to open {}: {}", filename.display(), e))?;
|
||||
file.read_to_end(&mut bytes)
|
||||
.map_err(|e| format!("Unable to read {}: {}", filename.display(), e))?;
|
||||
Ok(bytes)
|
||||
}
|
||||
|
||||
pub fn run<T: EthSpec>(matches: &ArgMatches) -> Result<(), String> {
|
||||
let spec = &T::default_spec();
|
||||
|
||||
let state_file: PathBuf = parse_required(matches, "state")?;
|
||||
let attestations_file: PathBuf = parse_required(matches, "attestations")?;
|
||||
|
||||
let mut state = BeaconState::<T>::from_ssz_bytes(&read_file_bytes(&state_file)?, spec)
|
||||
.map_err(|e| format!("Invalid state: {:?}", e))?;
|
||||
state
|
||||
.build_all_committee_caches(spec)
|
||||
.map_err(|e| format!("{:?}", e))?;
|
||||
|
||||
let attestations: Vec<Attestation<T>> =
|
||||
serde_json::from_slice(&read_file_bytes(&attestations_file)?)
|
||||
.map_err(|e| format!("Invalid attestation list: {:?}", e))?;
|
||||
|
||||
let indexed_attestations = attestations
|
||||
.into_iter()
|
||||
.map(|att| {
|
||||
let committee = state.get_beacon_committee(att.data.slot, att.data.index)?;
|
||||
get_indexed_attestation(committee.committee, &att)
|
||||
})
|
||||
.collect::<Result<Vec<_>, _>>()
|
||||
.map_err(|e| format!("Error constructing indexed attestation: {:?}", e))?;
|
||||
|
||||
let string_output = serde_json::to_string_pretty(&indexed_attestations)
|
||||
.map_err(|e| format!("Unable to convert to JSON: {:?}", e))?;
|
||||
println!("{}", string_output);
|
||||
|
||||
Ok(())
|
||||
}
|
||||
182
lcli/src/main.rs
182
lcli/src/main.rs
@@ -6,6 +6,7 @@ mod create_payload_header;
|
||||
mod deploy_deposit_contract;
|
||||
mod eth1_genesis;
|
||||
mod generate_bootnode_enr;
|
||||
mod indexed_attestations;
|
||||
mod insecure_validators;
|
||||
mod interop_genesis;
|
||||
mod new_testnet;
|
||||
@@ -21,7 +22,6 @@ use parse_ssz::run_parse_ssz;
|
||||
use std::path::PathBuf;
|
||||
use std::process;
|
||||
use std::str::FromStr;
|
||||
use transition_blocks::run_transition_blocks;
|
||||
use types::{EthSpec, EthSpecId};
|
||||
|
||||
fn main() {
|
||||
@@ -56,53 +56,149 @@ fn main() {
|
||||
"Performs a state transition from some state across some number of skip slots",
|
||||
)
|
||||
.arg(
|
||||
Arg::with_name("pre-state")
|
||||
.value_name("BEACON_STATE")
|
||||
Arg::with_name("output-path")
|
||||
.long("output-path")
|
||||
.value_name("PATH")
|
||||
.takes_value(true)
|
||||
.required(true)
|
||||
.help("Path to output a SSZ file."),
|
||||
)
|
||||
.arg(
|
||||
Arg::with_name("pre-state-path")
|
||||
.long("pre-state-path")
|
||||
.value_name("PATH")
|
||||
.takes_value(true)
|
||||
.conflicts_with("beacon-url")
|
||||
.help("Path to a SSZ file of the pre-state."),
|
||||
)
|
||||
.arg(
|
||||
Arg::with_name("slots")
|
||||
.value_name("SLOT_COUNT")
|
||||
Arg::with_name("beacon-url")
|
||||
.long("beacon-url")
|
||||
.value_name("URL")
|
||||
.takes_value(true)
|
||||
.required(true)
|
||||
.help("Number of slots to skip before outputting a state.."),
|
||||
.help("URL to a beacon-API provider."),
|
||||
)
|
||||
.arg(
|
||||
Arg::with_name("output")
|
||||
.value_name("SSZ_FILE")
|
||||
Arg::with_name("state-id")
|
||||
.long("state-id")
|
||||
.value_name("STATE_ID")
|
||||
.takes_value(true)
|
||||
.required(true)
|
||||
.default_value("./output.ssz")
|
||||
.help("Path to output a SSZ file."),
|
||||
),
|
||||
.requires("beacon-url")
|
||||
.help("Identifier for a state as per beacon-API standards (slot, root, etc.)"),
|
||||
)
|
||||
.arg(
|
||||
Arg::with_name("runs")
|
||||
.long("runs")
|
||||
.value_name("INTEGER")
|
||||
.takes_value(true)
|
||||
.default_value("1")
|
||||
.help("Number of repeat runs, useful for benchmarking."),
|
||||
)
|
||||
.arg(
|
||||
Arg::with_name("state-root")
|
||||
.long("state-root")
|
||||
.value_name("HASH256")
|
||||
.takes_value(true)
|
||||
.help("Tree hash root of the provided state, to avoid computing it."),
|
||||
)
|
||||
.arg(
|
||||
Arg::with_name("slots")
|
||||
.long("slots")
|
||||
.value_name("INTEGER")
|
||||
.takes_value(true)
|
||||
.help("Number of slots to skip forward."),
|
||||
)
|
||||
.arg(
|
||||
Arg::with_name("partial-state-advance")
|
||||
.long("partial-state-advance")
|
||||
.takes_value(false)
|
||||
.help("If present, don't compute state roots when skipping forward."),
|
||||
)
|
||||
)
|
||||
.subcommand(
|
||||
SubCommand::with_name("transition-blocks")
|
||||
.about("Performs a state transition given a pre-state and block")
|
||||
.arg(
|
||||
Arg::with_name("pre-state")
|
||||
.value_name("BEACON_STATE")
|
||||
Arg::with_name("pre-state-path")
|
||||
.long("pre-state-path")
|
||||
.value_name("PATH")
|
||||
.takes_value(true)
|
||||
.required(true)
|
||||
.help("Path to a SSZ file of the pre-state."),
|
||||
.conflicts_with("beacon-url")
|
||||
.requires("block-path")
|
||||
.help("Path to load a BeaconState from file as SSZ."),
|
||||
)
|
||||
.arg(
|
||||
Arg::with_name("block")
|
||||
.value_name("BEACON_BLOCK")
|
||||
Arg::with_name("block-path")
|
||||
.long("block-path")
|
||||
.value_name("PATH")
|
||||
.takes_value(true)
|
||||
.required(true)
|
||||
.help("Path to a SSZ file of the block to apply to pre-state."),
|
||||
.conflicts_with("beacon-url")
|
||||
.requires("pre-state-path")
|
||||
.help("Path to load a SignedBeaconBlock from file as SSZ."),
|
||||
)
|
||||
.arg(
|
||||
Arg::with_name("output")
|
||||
.value_name("SSZ_FILE")
|
||||
Arg::with_name("post-state-output-path")
|
||||
.long("post-state-output-path")
|
||||
.value_name("PATH")
|
||||
.takes_value(true)
|
||||
.required(true)
|
||||
.default_value("./output.ssz")
|
||||
.help("Path to output a SSZ file."),
|
||||
),
|
||||
.help("Path to output the post-state."),
|
||||
)
|
||||
.arg(
|
||||
Arg::with_name("pre-state-output-path")
|
||||
.long("pre-state-output-path")
|
||||
.value_name("PATH")
|
||||
.takes_value(true)
|
||||
.help("Path to output the pre-state, useful when used with --beacon-url."),
|
||||
)
|
||||
.arg(
|
||||
Arg::with_name("block-output-path")
|
||||
.long("block-output-path")
|
||||
.value_name("PATH")
|
||||
.takes_value(true)
|
||||
.help("Path to output the block, useful when used with --beacon-url."),
|
||||
)
|
||||
.arg(
|
||||
Arg::with_name("beacon-url")
|
||||
.long("beacon-url")
|
||||
.value_name("URL")
|
||||
.takes_value(true)
|
||||
.help("URL to a beacon-API provider."),
|
||||
)
|
||||
.arg(
|
||||
Arg::with_name("block-id")
|
||||
.long("block-id")
|
||||
.value_name("BLOCK_ID")
|
||||
.takes_value(true)
|
||||
.requires("beacon-url")
|
||||
.help("Identifier for a block as per beacon-API standards (slot, root, etc.)"),
|
||||
)
|
||||
.arg(
|
||||
Arg::with_name("runs")
|
||||
.long("runs")
|
||||
.value_name("INTEGER")
|
||||
.takes_value(true)
|
||||
.default_value("1")
|
||||
.help("Number of repeat runs, useful for benchmarking."),
|
||||
)
|
||||
.arg(
|
||||
Arg::with_name("no-signature-verification")
|
||||
.long("no-signature-verification")
|
||||
.takes_value(false)
|
||||
.help("Disable signature verification.")
|
||||
)
|
||||
.arg(
|
||||
Arg::with_name("exclude-cache-builds")
|
||||
.long("exclude-cache-builds")
|
||||
.takes_value(false)
|
||||
.help("If present, pre-build the committee and tree-hash caches without \
|
||||
including them in the timings."),
|
||||
)
|
||||
.arg(
|
||||
Arg::with_name("exclude-post-block-thc")
|
||||
.long("exclude-post-block-thc")
|
||||
.takes_value(false)
|
||||
.help("If present, don't rebuild the tree-hash-cache after applying \
|
||||
the block."),
|
||||
)
|
||||
)
|
||||
.subcommand(
|
||||
SubCommand::with_name("pretty-ssz")
|
||||
@@ -598,6 +694,26 @@ fn main() {
|
||||
.help("The number of nodes to divide the validator keys to"),
|
||||
)
|
||||
)
|
||||
.subcommand(
|
||||
SubCommand::with_name("indexed-attestations")
|
||||
.about("Convert attestations to indexed form, using the committees from a state.")
|
||||
.arg(
|
||||
Arg::with_name("state")
|
||||
.long("state")
|
||||
.value_name("SSZ_STATE")
|
||||
.takes_value(true)
|
||||
.required(true)
|
||||
.help("BeaconState to generate committees from (SSZ)"),
|
||||
)
|
||||
.arg(
|
||||
Arg::with_name("attestations")
|
||||
.long("attestations")
|
||||
.value_name("JSON_ATTESTATIONS")
|
||||
.takes_value(true)
|
||||
.required(true)
|
||||
.help("List of Attestations to convert to indexed form (JSON)"),
|
||||
)
|
||||
)
|
||||
.get_matches();
|
||||
|
||||
let result = matches
|
||||
@@ -631,6 +747,7 @@ fn run<T: EthSpec>(
|
||||
debug_level: "trace",
|
||||
logfile_debug_level: "trace",
|
||||
log_format: None,
|
||||
log_color: false,
|
||||
max_log_size: 0,
|
||||
max_log_number: 0,
|
||||
compression: false,
|
||||
@@ -646,10 +763,11 @@ fn run<T: EthSpec>(
|
||||
)?;
|
||||
|
||||
match matches.subcommand() {
|
||||
("transition-blocks", Some(matches)) => run_transition_blocks::<T>(testnet_dir, matches)
|
||||
("transition-blocks", Some(matches)) => transition_blocks::run::<T>(env, matches)
|
||||
.map_err(|e| format!("Failed to transition blocks: {}", e)),
|
||||
("skip-slots", Some(matches)) => skip_slots::run::<T>(testnet_dir, matches)
|
||||
.map_err(|e| format!("Failed to skip slots: {}", e)),
|
||||
("skip-slots", Some(matches)) => {
|
||||
skip_slots::run::<T>(env, matches).map_err(|e| format!("Failed to skip slots: {}", e))
|
||||
}
|
||||
("pretty-ssz", Some(matches)) => {
|
||||
run_parse_ssz::<T>(matches).map_err(|e| format!("Failed to pretty print hex: {}", e))
|
||||
}
|
||||
@@ -679,6 +797,8 @@ fn run<T: EthSpec>(
|
||||
.map_err(|e| format!("Failed to run generate-bootnode-enr command: {}", e)),
|
||||
("insecure-validators", Some(matches)) => insecure_validators::run(matches)
|
||||
.map_err(|e| format!("Failed to run insecure-validators command: {}", e)),
|
||||
("indexed-attestations", Some(matches)) => indexed_attestations::run::<T>(matches)
|
||||
.map_err(|e| format!("Failed to run indexed-attestations command: {}", e)),
|
||||
(other, _) => Err(format!("Unknown subcommand {}. See --help.", other)),
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,7 +1,9 @@
|
||||
use clap::ArgMatches;
|
||||
use clap_utils::parse_required;
|
||||
use serde::Serialize;
|
||||
use snap::raw::Decoder;
|
||||
use ssz::Decode;
|
||||
use std::fs;
|
||||
use std::fs::File;
|
||||
use std::io::Read;
|
||||
use std::str::FromStr;
|
||||
@@ -29,11 +31,18 @@ pub fn run_parse_ssz<T: EthSpec>(matches: &ArgMatches) -> Result<(), String> {
|
||||
let filename = matches.value_of("ssz-file").ok_or("No file supplied")?;
|
||||
let format = parse_required(matches, "format")?;
|
||||
|
||||
let mut bytes = vec![];
|
||||
let mut file =
|
||||
File::open(filename).map_err(|e| format!("Unable to open {}: {}", filename, e))?;
|
||||
file.read_to_end(&mut bytes)
|
||||
.map_err(|e| format!("Unable to read {}: {}", filename, e))?;
|
||||
let bytes = if filename.ends_with("ssz_snappy") {
|
||||
let bytes = fs::read(filename).unwrap();
|
||||
let mut decoder = Decoder::new();
|
||||
decoder.decompress_vec(&bytes).unwrap()
|
||||
} else {
|
||||
let mut bytes = vec![];
|
||||
let mut file =
|
||||
File::open(filename).map_err(|e| format!("Unable to open {}: {}", filename, e))?;
|
||||
file.read_to_end(&mut bytes)
|
||||
.map_err(|e| format!("Unable to read {}: {}", filename, e))?;
|
||||
bytes
|
||||
};
|
||||
|
||||
info!("Using {} spec", T::spec_name());
|
||||
info!("Type: {:?}", type_str);
|
||||
|
||||
@@ -1,58 +1,150 @@
|
||||
//! # 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 eth2_network_config::Eth2NetworkConfig;
|
||||
use clap_utils::{parse_optional, parse_required};
|
||||
use environment::Environment;
|
||||
use eth2::{types::StateId, BeaconNodeHttpClient, SensitiveUrl, Timeouts};
|
||||
use ssz::Encode;
|
||||
use state_processing::per_slot_processing;
|
||||
use state_processing::state_advance::{complete_state_advance, partial_state_advance};
|
||||
use std::fs::File;
|
||||
use std::io::prelude::*;
|
||||
use std::path::PathBuf;
|
||||
use types::{BeaconState, EthSpec};
|
||||
use std::time::{Duration, Instant};
|
||||
use types::{BeaconState, CloneConfig, EthSpec, Hash256};
|
||||
|
||||
pub fn run<T: EthSpec>(testnet_dir: PathBuf, matches: &ArgMatches) -> Result<(), String> {
|
||||
let pre_state_path = matches
|
||||
.value_of("pre-state")
|
||||
.ok_or("No pre-state file supplied")?
|
||||
.parse::<PathBuf>()
|
||||
.map_err(|e| format!("Failed to parse pre-state path: {}", e))?;
|
||||
const HTTP_TIMEOUT: Duration = Duration::from_secs(10);
|
||||
|
||||
let slots = matches
|
||||
.value_of("slots")
|
||||
.ok_or("No slots supplied")?
|
||||
.parse::<usize>()
|
||||
.map_err(|e| format!("Failed to parse slots: {}", e))?;
|
||||
pub fn run<T: EthSpec>(mut env: Environment<T>, matches: &ArgMatches) -> Result<(), String> {
|
||||
let spec = &T::default_spec();
|
||||
let executor = env.core_context().executor;
|
||||
|
||||
let output_path = matches
|
||||
.value_of("output")
|
||||
.ok_or("No output file supplied")?
|
||||
.parse::<PathBuf>()
|
||||
.map_err(|e| format!("Failed to parse output path: {}", e))?;
|
||||
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", T::spec_name());
|
||||
info!("Pre-state path: {:?}", pre_state_path);
|
||||
info!("Slots: {:?}", slots);
|
||||
info!("Advancing {} slots", slots);
|
||||
info!("Doing {} runs", runs);
|
||||
|
||||
let eth2_network_config = Eth2NetworkConfig::load(testnet_dir)?;
|
||||
let spec = ð2_network_config.chain_spec::<T>()?;
|
||||
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::<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;
|
||||
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 state: BeaconState<T> =
|
||||
load_from_ssz_with(&pre_state_path, spec, BeaconState::from_ssz_bytes)?;
|
||||
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))?;
|
||||
|
||||
// Transition the parent state to the block slot.
|
||||
for i in 0..slots {
|
||||
per_slot_processing(&mut state, None, spec)
|
||||
.map_err(|e| format!("Failed to advance slot on iteration {}: {:?}", i, 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_with(CloneConfig::committee_caches_only());
|
||||
|
||||
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);
|
||||
}
|
||||
|
||||
let mut output_file =
|
||||
File::create(output_path).map_err(|e| format!("Unable to create output file: {:?}", e))?;
|
||||
if let Some(output_path) = output_path {
|
||||
let mut output_file = File::create(output_path)
|
||||
.map_err(|e| format!("Unable to create output file: {:?}", e))?;
|
||||
|
||||
output_file
|
||||
.write_all(&state.as_ssz_bytes())
|
||||
.map_err(|e| format!("Unable to write to output file: {:?}", e))?;
|
||||
output_file
|
||||
.write_all(&state.as_ssz_bytes())
|
||||
.map_err(|e| format!("Unable to write to output file: {:?}", e))?;
|
||||
}
|
||||
|
||||
Ok(())
|
||||
}
|
||||
|
||||
@@ -1,63 +1,289 @@
|
||||
//! # Transition Blocks
|
||||
//!
|
||||
//! Use this tool to apply a `SignedBeaconBlock` to a `BeaconState`. Useful for benchmarking or
|
||||
//! troubleshooting consensus failures.
|
||||
//!
|
||||
//! It can load states and blocks from file or pull them from a beaconAPI. Objects pulled from a
|
||||
//! beaconAPI can be saved to disk to reduce future calls to that server.
|
||||
//!
|
||||
//! ## Examples
|
||||
//!
|
||||
//! ### Run using a block from a beaconAPI
|
||||
//!
|
||||
//! Download the 0x6c69 block and its pre-state (the state from its parent block) from the
|
||||
//! beaconAPI. Advance the pre-state to the slot of the 0x6c69 block and apply that block to the
|
||||
//! pre-state.
|
||||
//!
|
||||
//! ```ignore
|
||||
//! lcli transition-blocks \
|
||||
//! --beacon-url http://localhost:5052 \
|
||||
//! --block-id 0x6c69cf50a451f1ec905e954bf1fa22970f371a72a5aa9f8e3a43a18fdd980bec \
|
||||
//! --runs 10
|
||||
//! ```
|
||||
//!
|
||||
//! ### Download a block and pre-state from a beaconAPI to the filesystem
|
||||
//!
|
||||
//! Download a block and pre-state to the filesystem, without performing any transitions:
|
||||
//!
|
||||
//! ```ignore
|
||||
//! lcli transition-blocks \
|
||||
//! --beacon-url http://localhost:5052 \
|
||||
//! --block-id 0x6c69cf50a451f1ec905e954bf1fa22970f371a72a5aa9f8e3a43a18fdd980bec \
|
||||
//! --runs 0 \
|
||||
//! --block-output-path /tmp/block-0x6c69.ssz \
|
||||
//! --pre-state-output-path /tmp/pre-state-0x6c69.ssz
|
||||
//! ```
|
||||
//!
|
||||
//! ### Use a block and pre-state from the filesystem
|
||||
//!
|
||||
//! Do one run over the block and pre-state downloaded in the previous example and save the post
|
||||
//! state to file:
|
||||
//!
|
||||
//! ```ignore
|
||||
//! lcli transition-blocks \
|
||||
//! --block-path /tmp/block-0x6c69.ssz \
|
||||
//! --pre-state-path /tmp/pre-state-0x6c69.ssz
|
||||
//! --post-state-output-path /tmp/post-state-0x6c69.ssz
|
||||
//! ```
|
||||
//!
|
||||
//! ### Isolate block processing for benchmarking
|
||||
//!
|
||||
//! Try to isolate block processing as much as possible for benchmarking:
|
||||
//!
|
||||
//! ```ignore
|
||||
//! lcli transition-blocks \
|
||||
//! --block-path /tmp/block-0x6c69.ssz \
|
||||
//! --pre-state-path /tmp/pre-state-0x6c69.ssz \
|
||||
//! --runs 10 \
|
||||
//! --exclude-cache-builds \
|
||||
//! --exclude-post-block-thc
|
||||
//! ```
|
||||
use beacon_chain::{
|
||||
test_utils::EphemeralHarnessType, validator_pubkey_cache::ValidatorPubkeyCache,
|
||||
};
|
||||
use clap::ArgMatches;
|
||||
use eth2_network_config::Eth2NetworkConfig;
|
||||
use clap_utils::{parse_optional, parse_required};
|
||||
use environment::{null_logger, Environment};
|
||||
use eth2::{
|
||||
types::{BlockId, StateId},
|
||||
BeaconNodeHttpClient, SensitiveUrl, Timeouts,
|
||||
};
|
||||
use ssz::Encode;
|
||||
use state_processing::{
|
||||
per_block_processing, per_slot_processing, BlockSignatureStrategy, ConsensusContext,
|
||||
VerifyBlockRoot,
|
||||
block_signature_verifier::BlockSignatureVerifier, per_block_processing, per_slot_processing,
|
||||
BlockSignatureStrategy, ConsensusContext, VerifyBlockRoot,
|
||||
};
|
||||
use std::borrow::Cow;
|
||||
use std::fs::File;
|
||||
use std::io::prelude::*;
|
||||
use std::path::{Path, PathBuf};
|
||||
use types::{BeaconState, ChainSpec, EthSpec, SignedBeaconBlock};
|
||||
use std::sync::Arc;
|
||||
use std::time::{Duration, Instant};
|
||||
use store::HotColdDB;
|
||||
use types::{BeaconState, ChainSpec, CloneConfig, EthSpec, Hash256, SignedBeaconBlock};
|
||||
|
||||
pub fn run_transition_blocks<T: EthSpec>(
|
||||
testnet_dir: PathBuf,
|
||||
matches: &ArgMatches,
|
||||
) -> Result<(), String> {
|
||||
let pre_state_path = matches
|
||||
.value_of("pre-state")
|
||||
.ok_or("No pre-state file supplied")?
|
||||
.parse::<PathBuf>()
|
||||
.map_err(|e| format!("Failed to parse pre-state path: {}", e))?;
|
||||
const HTTP_TIMEOUT: Duration = Duration::from_secs(10);
|
||||
|
||||
let block_path = matches
|
||||
.value_of("block")
|
||||
.ok_or("No block file supplied")?
|
||||
.parse::<PathBuf>()
|
||||
.map_err(|e| format!("Failed to parse block path: {}", e))?;
|
||||
#[derive(Debug)]
|
||||
struct Config {
|
||||
no_signature_verification: bool,
|
||||
exclude_cache_builds: bool,
|
||||
exclude_post_block_thc: bool,
|
||||
}
|
||||
|
||||
let output_path = matches
|
||||
.value_of("output")
|
||||
.ok_or("No output file supplied")?
|
||||
.parse::<PathBuf>()
|
||||
.map_err(|e| format!("Failed to parse output path: {}", e))?;
|
||||
pub fn run<T: EthSpec>(mut env: Environment<T>, matches: &ArgMatches) -> Result<(), String> {
|
||||
let spec = &T::default_spec();
|
||||
let executor = env.core_context().executor;
|
||||
|
||||
/*
|
||||
* Parse (most) CLI arguments.
|
||||
*/
|
||||
|
||||
let pre_state_path: Option<PathBuf> = parse_optional(matches, "pre-state-path")?;
|
||||
let block_path: Option<PathBuf> = parse_optional(matches, "block-path")?;
|
||||
let post_state_output_path: Option<PathBuf> =
|
||||
parse_optional(matches, "post-state-output-path")?;
|
||||
let pre_state_output_path: Option<PathBuf> = parse_optional(matches, "pre-state-output-path")?;
|
||||
let block_output_path: Option<PathBuf> = parse_optional(matches, "block-output-path")?;
|
||||
let beacon_url: Option<SensitiveUrl> = parse_optional(matches, "beacon-url")?;
|
||||
let runs: usize = parse_required(matches, "runs")?;
|
||||
let config = Config {
|
||||
no_signature_verification: matches.is_present("no-signature-verification"),
|
||||
exclude_cache_builds: matches.is_present("exclude-cache-builds"),
|
||||
exclude_post_block_thc: matches.is_present("exclude-post-block-thc"),
|
||||
};
|
||||
|
||||
info!("Using {} spec", T::spec_name());
|
||||
info!("Pre-state path: {:?}", pre_state_path);
|
||||
info!("Block path: {:?}", block_path);
|
||||
info!("Doing {} runs", runs);
|
||||
info!("{:?}", &config);
|
||||
|
||||
let eth2_network_config = Eth2NetworkConfig::load(testnet_dir)?;
|
||||
let spec = ð2_network_config.chain_spec::<T>()?;
|
||||
/*
|
||||
* Load the block and pre-state from disk or beaconAPI URL.
|
||||
*/
|
||||
|
||||
let pre_state: BeaconState<T> =
|
||||
load_from_ssz_with(&pre_state_path, spec, BeaconState::from_ssz_bytes)?;
|
||||
let block: SignedBeaconBlock<T> =
|
||||
load_from_ssz_with(&block_path, spec, SignedBeaconBlock::from_ssz_bytes)?;
|
||||
let (mut pre_state, mut state_root_opt, block) = match (pre_state_path, block_path, beacon_url)
|
||||
{
|
||||
(Some(pre_state_path), Some(block_path), None) => {
|
||||
info!("Block path: {:?}", pre_state_path);
|
||||
info!("Pre-state path: {:?}", block_path);
|
||||
let pre_state = load_from_ssz_with(&pre_state_path, spec, BeaconState::from_ssz_bytes)?;
|
||||
let block = load_from_ssz_with(&block_path, spec, SignedBeaconBlock::from_ssz_bytes)?;
|
||||
(pre_state, None, block)
|
||||
}
|
||||
(None, None, Some(beacon_url)) => {
|
||||
let block_id: BlockId = parse_required(matches, "block-id")?;
|
||||
let client = BeaconNodeHttpClient::new(beacon_url, Timeouts::set_all(HTTP_TIMEOUT));
|
||||
executor
|
||||
.handle()
|
||||
.ok_or("shutdown in progress")?
|
||||
.block_on(async move {
|
||||
let block = client
|
||||
.get_beacon_blocks(block_id)
|
||||
.await
|
||||
.map_err(|e| format!("Failed to download block: {:?}", e))?
|
||||
.ok_or_else(|| format!("Unable to locate block at {:?}", block_id))?
|
||||
.data;
|
||||
|
||||
let t = std::time::Instant::now();
|
||||
let mut post_state = do_transition(pre_state.clone(), block.clone(), spec)?;
|
||||
println!("Total transition time: {}ms", t.elapsed().as_millis());
|
||||
if block.slot() == spec.genesis_slot {
|
||||
return Err("Cannot run on the genesis block".to_string());
|
||||
}
|
||||
|
||||
if post_state.update_tree_hash_cache().unwrap() != block.state_root() {
|
||||
return Err("state root mismatch".into());
|
||||
let parent_block: SignedBeaconBlock<T> = client
|
||||
.get_beacon_blocks(BlockId::Root(block.parent_root()))
|
||||
.await
|
||||
.map_err(|e| format!("Failed to download parent block: {:?}", e))?
|
||||
.ok_or_else(|| format!("Unable to locate parent block at {:?}", block_id))?
|
||||
.data;
|
||||
|
||||
let state_root = parent_block.state_root();
|
||||
let state_id = StateId::Root(state_root);
|
||||
let pre_state = client
|
||||
.get_debug_beacon_states::<T>(state_id)
|
||||
.await
|
||||
.map_err(|e| format!("Failed to download state: {:?}", e))?
|
||||
.ok_or_else(|| format!("Unable to locate state at {:?}", state_id))?
|
||||
.data;
|
||||
|
||||
Ok((pre_state, Some(state_root), block))
|
||||
})
|
||||
.map_err(|e| format!("Failed to complete task: {:?}", e))?
|
||||
}
|
||||
_ => {
|
||||
return Err(
|
||||
"must supply *both* --pre-state-path and --block-path *or* only --beacon-url"
|
||||
.into(),
|
||||
)
|
||||
}
|
||||
};
|
||||
|
||||
// Compute the block root.
|
||||
let block_root = block.canonical_root();
|
||||
|
||||
/*
|
||||
* Create a `BeaconStore` and `ValidatorPubkeyCache` for block signature verification.
|
||||
*/
|
||||
|
||||
let store = HotColdDB::open_ephemeral(
|
||||
<_>::default(),
|
||||
spec.clone(),
|
||||
null_logger().map_err(|e| format!("Failed to create null_logger: {:?}", e))?,
|
||||
)
|
||||
.map_err(|e| format!("Failed to create ephemeral store: {:?}", e))?;
|
||||
let store = Arc::new(store);
|
||||
|
||||
debug!("Building pubkey cache (might take some time)");
|
||||
let validator_pubkey_cache = ValidatorPubkeyCache::new(&pre_state, store)
|
||||
.map_err(|e| format!("Failed to create pubkey cache: {:?}", e))?;
|
||||
|
||||
/*
|
||||
* If cache builds are excluded from the timings, build them early so they are available for
|
||||
* each run.
|
||||
*/
|
||||
|
||||
if config.exclude_cache_builds {
|
||||
pre_state
|
||||
.build_all_caches(spec)
|
||||
.map_err(|e| format!("Unable to build caches: {:?}", e))?;
|
||||
let state_root = pre_state
|
||||
.update_tree_hash_cache()
|
||||
.map_err(|e| format!("Unable to build THC: {:?}", e))?;
|
||||
|
||||
if state_root_opt.map_or(false, |expected| expected != state_root) {
|
||||
return Err(format!(
|
||||
"State root mismatch! Expected {}, computed {}",
|
||||
state_root_opt.unwrap(),
|
||||
state_root
|
||||
));
|
||||
}
|
||||
state_root_opt = Some(state_root);
|
||||
}
|
||||
|
||||
let mut output_file =
|
||||
File::create(output_path).map_err(|e| format!("Unable to create output file: {:?}", e))?;
|
||||
/*
|
||||
* Perform the core "runs".
|
||||
*/
|
||||
|
||||
output_file
|
||||
.write_all(&post_state.as_ssz_bytes())
|
||||
.map_err(|e| format!("Unable to write to output file: {:?}", e))?;
|
||||
let mut output_post_state = None;
|
||||
for i in 0..runs {
|
||||
let pre_state = pre_state.clone_with(CloneConfig::all());
|
||||
let block = block.clone();
|
||||
|
||||
let start = Instant::now();
|
||||
|
||||
let post_state = do_transition(
|
||||
pre_state,
|
||||
block_root,
|
||||
block,
|
||||
state_root_opt,
|
||||
&config,
|
||||
&validator_pubkey_cache,
|
||||
spec,
|
||||
)?;
|
||||
|
||||
let duration = Instant::now().duration_since(start);
|
||||
info!("Run {}: {:?}", i, duration);
|
||||
|
||||
if output_post_state.is_none() {
|
||||
output_post_state = Some(post_state)
|
||||
}
|
||||
}
|
||||
|
||||
/*
|
||||
* Write artifacts to disk, if required.
|
||||
*/
|
||||
|
||||
if let Some(path) = post_state_output_path {
|
||||
let output_post_state = output_post_state.ok_or_else(|| {
|
||||
format!(
|
||||
"Post state was not computed, cannot save to disk (runs = {})",
|
||||
runs
|
||||
)
|
||||
})?;
|
||||
|
||||
let mut output_file =
|
||||
File::create(path).map_err(|e| format!("Unable to create output file: {:?}", e))?;
|
||||
|
||||
output_file
|
||||
.write_all(&output_post_state.as_ssz_bytes())
|
||||
.map_err(|e| format!("Unable to write to output file: {:?}", e))?;
|
||||
}
|
||||
|
||||
if let Some(path) = pre_state_output_path {
|
||||
let mut output_file =
|
||||
File::create(path).map_err(|e| format!("Unable to create output file: {:?}", e))?;
|
||||
|
||||
output_file
|
||||
.write_all(&pre_state.as_ssz_bytes())
|
||||
.map_err(|e| format!("Unable to write to output file: {:?}", e))?;
|
||||
}
|
||||
|
||||
if let Some(path) = block_output_path {
|
||||
let mut output_file =
|
||||
File::create(path).map_err(|e| format!("Unable to create output file: {:?}", e))?;
|
||||
|
||||
output_file
|
||||
.write_all(&block.as_ssz_bytes())
|
||||
.map_err(|e| format!("Unable to write to output file: {:?}", e))?;
|
||||
}
|
||||
|
||||
drop(pre_state);
|
||||
drop(post_state);
|
||||
@@ -67,60 +293,100 @@ pub fn run_transition_blocks<T: EthSpec>(
|
||||
|
||||
fn do_transition<T: EthSpec>(
|
||||
mut pre_state: BeaconState<T>,
|
||||
block_root: Hash256,
|
||||
block: SignedBeaconBlock<T>,
|
||||
mut state_root_opt: Option<Hash256>,
|
||||
config: &Config,
|
||||
validator_pubkey_cache: &ValidatorPubkeyCache<EphemeralHarnessType<T>>,
|
||||
spec: &ChainSpec,
|
||||
) -> Result<BeaconState<T>, String> {
|
||||
let t = std::time::Instant::now();
|
||||
pre_state
|
||||
.build_all_caches(spec)
|
||||
.map_err(|e| format!("Unable to build caches: {:?}", e))?;
|
||||
println!("Build all caches: {}ms", t.elapsed().as_millis());
|
||||
if !config.exclude_cache_builds {
|
||||
let t = Instant::now();
|
||||
pre_state
|
||||
.build_all_caches(spec)
|
||||
.map_err(|e| format!("Unable to build caches: {:?}", e))?;
|
||||
debug!("Build caches: {:?}", t.elapsed());
|
||||
|
||||
let t = std::time::Instant::now();
|
||||
pre_state
|
||||
.update_tree_hash_cache()
|
||||
.map_err(|e| format!("Unable to build tree hash cache: {:?}", e))?;
|
||||
println!("Initial tree hash: {}ms", t.elapsed().as_millis());
|
||||
let t = Instant::now();
|
||||
let state_root = pre_state
|
||||
.update_tree_hash_cache()
|
||||
.map_err(|e| format!("Unable to build tree hash cache: {:?}", e))?;
|
||||
debug!("Initial tree hash: {:?}", t.elapsed());
|
||||
|
||||
// Transition the parent state to the block slot.
|
||||
let t = std::time::Instant::now();
|
||||
for i in pre_state.slot().as_u64()..block.slot().as_u64() {
|
||||
per_slot_processing(&mut pre_state, None, spec)
|
||||
.map_err(|e| format!("Failed to advance slot on iteration {}: {:?}", i, e))?;
|
||||
if state_root_opt.map_or(false, |expected| expected != state_root) {
|
||||
return Err(format!(
|
||||
"State root mismatch! Expected {}, computed {}",
|
||||
state_root_opt.unwrap(),
|
||||
state_root
|
||||
));
|
||||
}
|
||||
state_root_opt = Some(state_root);
|
||||
}
|
||||
println!("Slot processing: {}ms", t.elapsed().as_millis());
|
||||
|
||||
let t = std::time::Instant::now();
|
||||
pre_state
|
||||
.update_tree_hash_cache()
|
||||
.map_err(|e| format!("Unable to build tree hash cache: {:?}", e))?;
|
||||
println!("Pre-block tree hash: {}ms", t.elapsed().as_millis());
|
||||
let state_root = state_root_opt.ok_or("Failed to compute state root, internal error")?;
|
||||
|
||||
let t = std::time::Instant::now();
|
||||
// Transition the parent state to the block slot.
|
||||
let t = Instant::now();
|
||||
for i in pre_state.slot().as_u64()..block.slot().as_u64() {
|
||||
per_slot_processing(&mut pre_state, Some(state_root), spec)
|
||||
.map_err(|e| format!("Failed to advance slot on iteration {}: {:?}", i, e))?;
|
||||
}
|
||||
debug!("Slot processing: {:?}", t.elapsed());
|
||||
|
||||
let t = Instant::now();
|
||||
pre_state
|
||||
.build_all_caches(spec)
|
||||
.map_err(|e| format!("Unable to build caches: {:?}", e))?;
|
||||
println!("Build all caches (again): {}ms", t.elapsed().as_millis());
|
||||
debug!("Build all caches (again): {:?}", t.elapsed());
|
||||
|
||||
let t = std::time::Instant::now();
|
||||
let mut ctxt =
|
||||
ConsensusContext::new(block.slot()).set_proposer_index(block.message().proposer_index());
|
||||
if !config.no_signature_verification {
|
||||
let get_pubkey = move |validator_index| {
|
||||
validator_pubkey_cache
|
||||
.get(validator_index)
|
||||
.map(Cow::Borrowed)
|
||||
};
|
||||
|
||||
let decompressor = move |pk_bytes| {
|
||||
// Map compressed pubkey to validator index.
|
||||
let validator_index = validator_pubkey_cache.get_index(pk_bytes)?;
|
||||
// Map validator index to pubkey (respecting guard on unknown validators).
|
||||
get_pubkey(validator_index)
|
||||
};
|
||||
|
||||
let t = Instant::now();
|
||||
BlockSignatureVerifier::verify_entire_block(
|
||||
&pre_state,
|
||||
get_pubkey,
|
||||
decompressor,
|
||||
&block,
|
||||
Some(block_root),
|
||||
spec,
|
||||
)
|
||||
.map_err(|e| format!("Invalid block signature: {:?}", e))?;
|
||||
debug!("Batch verify block signatures: {:?}", t.elapsed());
|
||||
}
|
||||
|
||||
let t = Instant::now();
|
||||
per_block_processing(
|
||||
&mut pre_state,
|
||||
&block,
|
||||
None,
|
||||
BlockSignatureStrategy::NoVerification,
|
||||
VerifyBlockRoot::True,
|
||||
&mut ctxt,
|
||||
spec,
|
||||
)
|
||||
.map_err(|e| format!("State transition failed: {:?}", e))?;
|
||||
println!("Process block: {}ms", t.elapsed().as_millis());
|
||||
debug!("Process block: {:?}", t.elapsed());
|
||||
|
||||
let t = std::time::Instant::now();
|
||||
pre_state
|
||||
.update_tree_hash_cache()
|
||||
.map_err(|e| format!("Unable to build tree hash cache: {:?}", e))?;
|
||||
println!("Post-block tree hash: {}ms", t.elapsed().as_millis());
|
||||
if !config.exclude_post_block_thc {
|
||||
let t = Instant::now();
|
||||
pre_state
|
||||
.update_tree_hash_cache()
|
||||
.map_err(|e| format!("Unable to build tree hash cache: {:?}", e))?;
|
||||
debug!("Post-block tree hash: {:?}", t.elapsed());
|
||||
}
|
||||
|
||||
Ok(pre_state)
|
||||
}
|
||||
@@ -135,13 +401,8 @@ pub fn load_from_ssz_with<T>(
|
||||
let mut bytes = vec![];
|
||||
file.read_to_end(&mut bytes)
|
||||
.map_err(|e| format!("Unable to read from file {:?}: {:?}", path, e))?;
|
||||
|
||||
let t = std::time::Instant::now();
|
||||
let t = Instant::now();
|
||||
let result = decoder(&bytes, spec).map_err(|e| format!("Ssz decode failed: {:?}", e));
|
||||
println!(
|
||||
"SSZ decoding {}: {}ms",
|
||||
path.display(),
|
||||
t.elapsed().as_millis()
|
||||
);
|
||||
debug!("SSZ decoding {}: {:?}", path.display(), t.elapsed());
|
||||
result
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user