Implement checkpoint sync (#2244)

## Issue Addressed

Closes #1891
Closes #1784

## Proposed Changes

Implement checkpoint sync for Lighthouse, enabling it to start from a weak subjectivity checkpoint.

## Additional Info

- [x] Return unavailable status for out-of-range blocks requested by peers (#2561)
- [x] Implement sync daemon for fetching historical blocks (#2561)
- [x] Verify chain hashes (either in `historical_blocks.rs` or the calling module)
- [x] Consistency check for initial block + state
- [x] Fetch the initial state and block from a beacon node HTTP endpoint
- [x] Don't crash fetching beacon states by slot from the API
- [x] Background service for state reconstruction, triggered by CLI flag or API call.

Considered out of scope for this PR:

- Drop the requirement to provide the `--checkpoint-block` (this would require some pretty heavy refactoring of block verification)


Co-authored-by: Diva M <divma@protonmail.com>
This commit is contained in:
Michael Sproul
2021-09-22 00:37:28 +00:00
parent 280e4fe23d
commit 9667dc2f03
71 changed files with 4012 additions and 459 deletions

View File

@@ -15,7 +15,8 @@ hex = "0.4.2"
log = "0.4.11"
serde = "1.0.116"
serde_yaml = "0.8.13"
simple_logger = "1.10.0"
serde_json = "1.0.66"
env_logger = "0.9.0"
types = { path = "../consensus/types" }
state_processing = { path = "../consensus/state_processing" }
eth2_ssz = "0.3.0"

View File

@@ -16,7 +16,6 @@ mod transition_blocks;
use clap::{App, Arg, ArgMatches, SubCommand};
use clap_utils::parse_path_with_default_in_home_dir;
use environment::EnvironmentBuilder;
use log::LevelFilter;
use parse_ssz::run_parse_ssz;
use std::path::PathBuf;
use std::process;
@@ -25,10 +24,7 @@ use transition_blocks::run_transition_blocks;
use types::{EthSpec, EthSpecId};
fn main() {
simple_logger::SimpleLogger::new()
.with_level(LevelFilter::Info)
.init()
.expect("Logger should be initialised");
env_logger::init();
let matches = App::new("Lighthouse CLI Tool")
.version(lighthouse_version::VERSION)
@@ -110,6 +106,17 @@ fn main() {
.subcommand(
SubCommand::with_name("pretty-ssz")
.about("Parses SSZ-encoded data from a file")
.arg(
Arg::with_name("format")
.short("f")
.long("format")
.value_name("FORMAT")
.takes_value(true)
.required(true)
.default_value("json")
.possible_values(&["json", "yaml"])
.help("Output format to use")
)
.arg(
Arg::with_name("type")
.value_name("TYPE")
@@ -123,7 +130,7 @@ fn main() {
.takes_value(true)
.required(true)
.help("Path to SSZ bytes"),
),
)
)
.subcommand(
SubCommand::with_name("deploy-deposit-contract")

View File

@@ -1,13 +1,33 @@
use clap::ArgMatches;
use clap_utils::parse_required;
use serde::Serialize;
use ssz::Decode;
use std::fs::File;
use std::io::Read;
use std::str::FromStr;
use types::*;
enum OutputFormat {
Json,
Yaml,
}
impl FromStr for OutputFormat {
type Err = String;
fn from_str(s: &str) -> Result<Self, Self::Err> {
match s {
"json" => Ok(Self::Json),
"yaml" => Ok(Self::Yaml),
_ => Err(format!("Invalid output format \"{}\"", s)),
}
}
}
pub fn run_parse_ssz<T: EthSpec>(matches: &ArgMatches) -> Result<(), String> {
let type_str = matches.value_of("type").ok_or("No type supplied")?;
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 =
@@ -19,24 +39,40 @@ pub fn run_parse_ssz<T: EthSpec>(matches: &ArgMatches) -> Result<(), String> {
info!("Type: {:?}", type_str);
match type_str {
"block_base" => decode_and_print::<BeaconBlockBase<T>>(&bytes)?,
"block_altair" => decode_and_print::<BeaconBlockAltair<T>>(&bytes)?,
"state_base" => decode_and_print::<BeaconStateBase<T>>(&bytes)?,
"state_altair" => decode_and_print::<BeaconStateAltair<T>>(&bytes)?,
"signed_block_base" => decode_and_print::<SignedBeaconBlockBase<T>>(&bytes, format)?,
"signed_block_altair" => decode_and_print::<SignedBeaconBlockAltair<T>>(&bytes, format)?,
"block_base" => decode_and_print::<BeaconBlockBase<T>>(&bytes, format)?,
"block_altair" => decode_and_print::<BeaconBlockAltair<T>>(&bytes, format)?,
"state_base" => decode_and_print::<BeaconStateBase<T>>(&bytes, format)?,
"state_altair" => decode_and_print::<BeaconStateAltair<T>>(&bytes, format)?,
other => return Err(format!("Unknown type: {}", other)),
};
Ok(())
}
fn decode_and_print<T: Decode + Serialize>(bytes: &[u8]) -> Result<(), String> {
fn decode_and_print<T: Decode + Serialize>(
bytes: &[u8],
output_format: OutputFormat,
) -> Result<(), String> {
let item = T::from_ssz_bytes(bytes).map_err(|e| format!("SSZ decode failed: {:?}", e))?;
println!(
"{}",
serde_yaml::to_string(&item)
.map_err(|e| format!("Unable to write object to YAML: {:?}", e))?
);
match output_format {
OutputFormat::Json => {
println!(
"{}",
serde_json::to_string(&item)
.map_err(|e| format!("Unable to write object to JSON: {:?}", e))?
);
}
OutputFormat::Yaml => {
println!(
"{}",
serde_yaml::to_string(&item)
.map_err(|e| format!("Unable to write object to YAML: {:?}", e))?
);
}
}
Ok(())
}