Altair consensus changes and refactors (#2279)

## Proposed Changes

Implement the consensus changes necessary for the upcoming Altair hard fork.

## Additional Info

This is quite a heavy refactor, with pivotal types like the `BeaconState` and `BeaconBlock` changing from structs to enums. This ripples through the whole codebase with field accesses changing to methods, e.g. `state.slot` => `state.slot()`.


Co-authored-by: realbigsean <seananderson33@gmail.com>
This commit is contained in:
Michael Sproul
2021-07-09 06:15:32 +00:00
parent 89361573d4
commit b4689e20c6
271 changed files with 9652 additions and 8444 deletions

View File

@@ -1,12 +1,16 @@
use clap::ArgMatches;
use ssz::{Decode, Encode};
use eth2_network_config::Eth2NetworkConfig;
use ssz::Encode;
use state_processing::{per_block_processing, per_slot_processing, BlockSignatureStrategy};
use std::fs::File;
use std::io::prelude::*;
use std::path::PathBuf;
use types::{BeaconState, EthSpec, SignedBeaconBlock};
use std::path::{Path, PathBuf};
use types::{BeaconState, ChainSpec, EthSpec, SignedBeaconBlock};
pub fn run_transition_blocks<T: EthSpec>(matches: &ArgMatches) -> Result<(), String> {
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")?
@@ -29,10 +33,15 @@ pub fn run_transition_blocks<T: EthSpec>(matches: &ArgMatches) -> Result<(), Str
info!("Pre-state path: {:?}", pre_state_path);
info!("Block path: {:?}", block_path);
let pre_state: BeaconState<T> = load_from_ssz(pre_state_path)?;
let block: SignedBeaconBlock<T> = load_from_ssz(block_path)?;
let eth2_network_config = Eth2NetworkConfig::load(testnet_dir)?;
let spec = &eth2_network_config.chain_spec::<T>()?;
let post_state = do_transition(pre_state, block)?;
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 post_state = do_transition(pre_state, block, spec)?;
let mut output_file =
File::create(output_path).map_err(|e| format!("Unable to create output file: {:?}", e))?;
@@ -47,15 +56,14 @@ pub fn run_transition_blocks<T: EthSpec>(matches: &ArgMatches) -> Result<(), Str
fn do_transition<T: EthSpec>(
mut pre_state: BeaconState<T>,
block: SignedBeaconBlock<T>,
spec: &ChainSpec,
) -> Result<BeaconState<T>, String> {
let spec = &T::default_spec();
pre_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 pre_state.slot.as_u64()..block.slot().as_u64() {
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))?;
}
@@ -76,11 +84,15 @@ fn do_transition<T: EthSpec>(
Ok(pre_state)
}
pub fn load_from_ssz<T: Decode>(path: PathBuf) -> Result<T, String> {
pub fn load_from_ssz_with<T>(
path: &Path,
spec: &ChainSpec,
decoder: impl FnOnce(&[u8], &ChainSpec) -> Result<T, ssz::DecodeError>,
) -> Result<T, String> {
let mut file =
File::open(path.clone()).map_err(|e| format!("Unable to open file {:?}: {:?}", path, e))?;
File::open(path).map_err(|e| format!("Unable to open file {:?}: {:?}", path, e))?;
let mut bytes = vec![];
file.read_to_end(&mut bytes)
.map_err(|e| format!("Unable to read from file {:?}: {:?}", path, e))?;
T::from_ssz_bytes(&bytes).map_err(|e| format!("Ssz decode failed: {:?}", e))
decoder(&bytes, spec).map_err(|e| format!("Ssz decode failed: {:?}", e))
}