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,14 +1,14 @@
use super::per_block_processing::{errors::BlockProcessingError, process_deposit};
use super::per_block_processing::{
errors::BlockProcessingError, process_operations::process_deposit,
};
use crate::common::DepositDataTree;
use crate::upgrade::upgrade_to_altair;
use safe_arith::{ArithError, SafeArith};
use tree_hash::TreeHash;
use types::DEPOSIT_TREE_DEPTH;
use types::*;
/// Initialize a `BeaconState` from genesis data.
///
/// Spec v0.12.1
// TODO: this is quite inefficient and we probably want to rethink how we do this
pub fn initialize_beacon_state_from_eth1<T: EthSpec>(
eth1_block_hash: Hash256,
eth1_timestamp: u64,
@@ -33,42 +33,53 @@ pub fn initialize_beacon_state_from_eth1<T: EthSpec>(
deposit_tree
.push_leaf(deposit.data.tree_hash_root())
.map_err(BlockProcessingError::MerkleTreeError)?;
state.eth1_data.deposit_root = deposit_tree.root();
state.eth1_data_mut().deposit_root = deposit_tree.root();
process_deposit(&mut state, &deposit, spec, true)?;
}
process_activations(&mut state, spec)?;
// To support testnets with Altair enabled from genesis, perform a possible state upgrade here.
// This must happen *after* deposits and activations are processed or the calculation of sync
// committees during the upgrade will fail. It's a bit cheeky to do this instead of having
// separate Altair genesis initialization logic, but it turns out that our
// use of `BeaconBlock::empty` in `BeaconState::new` is sufficient to correctly initialise
// the `latest_block_header` as per:
// https://github.com/ethereum/eth2.0-specs/pull/2323
if spec.fork_name_at_epoch(state.current_epoch()) == ForkName::Altair {
upgrade_to_altair(&mut state, spec)?;
}
// Now that we have our validators, initialize the caches (including the committees)
state.build_all_caches(spec)?;
// Set genesis validators root for domain separation and chain versioning
state.genesis_validators_root = state.update_validators_tree_hash_cache()?;
*state.genesis_validators_root_mut() = state.update_validators_tree_hash_cache()?;
Ok(state)
}
/// Determine whether a candidate genesis state is suitable for starting the chain.
///
/// Spec v0.12.1
pub fn is_valid_genesis_state<T: EthSpec>(state: &BeaconState<T>, spec: &ChainSpec) -> bool {
state
.get_active_validator_indices(T::genesis_epoch(), spec)
.map_or(false, |active_validators| {
state.genesis_time >= spec.min_genesis_time
state.genesis_time() >= spec.min_genesis_time
&& active_validators.len() as u64 >= spec.min_genesis_active_validator_count
})
}
/// Activate genesis validators, if their balance is acceptable.
///
/// Spec v0.12.1
pub fn process_activations<T: EthSpec>(
state: &mut BeaconState<T>,
spec: &ChainSpec,
) -> Result<(), Error> {
for (index, validator) in state.validators.iter_mut().enumerate() {
let balance = state.balances[index];
let (validators, balances) = state.validators_and_balances_mut();
for (index, validator) in validators.iter_mut().enumerate() {
let balance = balances
.get(index)
.copied()
.ok_or(Error::BalancesOutOfBounds(index))?;
validator.effective_balance = std::cmp::min(
balance.safe_sub(balance.safe_rem(spec.effective_balance_increment)?)?,
spec.max_effective_balance,