lighthouse/beacon_node/beacon_chain/src/fork_choice.rs

192 lines
6.3 KiB
Rust
Raw Normal View History

2019-06-15 19:05:34 +00:00
use crate::{BeaconChain, BeaconChainTypes};
use lmd_ghost::LmdGhost;
use state_processing::common::get_attesting_indices_unsorted;
use std::sync::Arc;
2019-06-15 19:05:34 +00:00
use store::{Error as StoreError, Store};
use types::{Attestation, BeaconBlock, BeaconState, BeaconStateError, Epoch, EthSpec, Hash256};
type Result<T> = std::result::Result<T, Error>;
#[derive(Debug, PartialEq)]
pub enum Error {
2019-06-15 19:05:34 +00:00
MissingBlock(Hash256),
MissingState(Hash256),
BackendError(String),
BeaconStateError(BeaconStateError),
2019-06-15 19:05:34 +00:00
StoreError(StoreError),
}
2019-06-15 19:05:34 +00:00
pub struct ForkChoice<T: BeaconChainTypes> {
backend: T::LmdGhost,
/// Used for resolving the `0x00..00` alias back to genesis.
///
/// Does not necessarily need to be the _actual_ genesis, it suffices to be the finalized root
/// whenever the struct was instantiated.
genesis_block_root: Hash256,
}
2019-06-15 19:05:34 +00:00
impl<T: BeaconChainTypes> ForkChoice<T> {
/// Instantiate a new fork chooser.
///
/// "Genesis" does not necessarily need to be the absolute genesis, it can be some finalized
/// block.
pub fn new(
store: Arc<T::Store>,
genesis_block: &BeaconBlock,
genesis_block_root: Hash256,
) -> Self {
Self {
backend: T::LmdGhost::new(store, genesis_block, genesis_block_root),
genesis_block_root,
}
}
2019-06-15 19:05:34 +00:00
pub fn find_head(&self, chain: &BeaconChain<T>) -> Result<Hash256> {
let start_slot = |epoch: Epoch| epoch.start_slot(T::EthSpec::slots_per_epoch());
2019-06-15 19:05:34 +00:00
// From the specification:
//
// Let justified_head be the descendant of finalized_head with the highest epoch that has
// been justified for at least 1 epoch ... If no such descendant exists,
// set justified_head to finalized_head.
let (start_state, start_block_root, start_block_slot) = {
2019-06-15 19:05:34 +00:00
let state = chain.current_state();
let (block_root, block_slot) =
if state.current_epoch() + 1 > state.current_justified_epoch {
(
state.current_justified_root,
start_slot(state.current_justified_epoch),
)
} else {
(state.finalized_root, start_slot(state.finalized_epoch))
};
2019-06-15 19:05:34 +00:00
let block = chain
.store
.get::<BeaconBlock>(&block_root)?
.ok_or_else(|| Error::MissingBlock(block_root))?;
// Resolve the `0x00.. 00` alias back to genesis
let block_root = if block_root == Hash256::zero() {
self.genesis_block_root
} else {
block_root
};
2019-06-15 19:05:34 +00:00
let state = chain
.store
.get::<BeaconState<T::EthSpec>>(&block.state_root)?
.ok_or_else(|| Error::MissingState(block.state_root))?;
(state, block_root, block_slot)
2019-06-15 19:05:34 +00:00
};
// A function that returns the weight for some validator index.
let weight = |validator_index: usize| -> Option<u64> {
start_state
.validator_registry
.get(validator_index)
2019-06-24 05:31:36 +00:00
.map(|v| v.effective_balance)
2019-06-15 19:05:34 +00:00
};
self.backend
.find_head(start_block_slot, start_block_root, weight)
2019-06-15 19:05:34 +00:00
.map_err(Into::into)
}
2019-06-15 22:19:08 +00:00
/// Process all attestations in the given `block`.
///
/// Assumes the block (and therefore it's attestations) are valid. It is a logic error to
/// provide an invalid block.
pub fn process_block(
&self,
state: &BeaconState<T::EthSpec>,
block: &BeaconBlock,
block_root: Hash256,
2019-06-15 22:19:08 +00:00
) -> Result<()> {
// Note: we never count the block as a latest message, only attestations.
//
// I (Paul H) do not have an explicit reference to this, but I derive it from this
// document:
//
// https://github.com/ethereum/eth2.0-specs/blob/v0.7.0/specs/core/0_fork-choice.md
for attestation in &block.body.attestations {
self.process_attestation_from_block(state, attestation)?;
}
self.backend.process_block(block, block_root)?;
2019-06-15 22:19:08 +00:00
Ok(())
}
fn process_attestation_from_block(
&self,
2019-06-15 19:05:34 +00:00
state: &BeaconState<T::EthSpec>,
attestation: &Attestation,
) -> Result<()> {
let validator_indices = get_attesting_indices_unsorted(
state,
&attestation.data,
&attestation.aggregation_bitfield,
)?;
let block_slot = state.get_attestation_slot(&attestation.data)?;
let block_hash = attestation.data.beacon_block_root;
2019-06-15 23:11:02 +00:00
// Ignore any attestations to the zero hash.
//
// This is an edge case that results from the spec aliasing the zero hash to the genesis
// block. Attesters may attest to the zero hash if they have never seen a block.
//
// We have two options here:
//
// 1. Apply all zero-hash attestations to the zero hash.
// 2. Ignore all attestations to the zero hash.
//
// (1) becomes weird once we hit finality and fork choice drops the genesis block. (2) is
2019-07-26 19:26:06 +00:00
// fine because votes to the genesis block are not useful; all validators implicitly attest
// to genesis just by being present in the chain.
2019-06-15 23:11:02 +00:00
if block_hash != Hash256::zero() {
for validator_index in validator_indices {
self.backend
.process_attestation(validator_index, block_hash, block_slot)?;
2019-06-15 23:11:02 +00:00
}
}
Ok(())
}
/// Inform the fork choice that the given block (and corresponding root) have been finalized so
/// it may prune it's storage.
///
/// `finalized_block_root` must be the root of `finalized_block`.
pub fn process_finalization(
&self,
finalized_block: &BeaconBlock,
finalized_block_root: Hash256,
) -> Result<()> {
self.backend
.update_finalized_root(finalized_block, finalized_block_root)
.map_err(Into::into)
}
}
impl From<BeaconStateError> for Error {
fn from(e: BeaconStateError) -> Error {
Error::BeaconStateError(e)
}
}
2019-06-15 19:05:34 +00:00
impl From<StoreError> for Error {
fn from(e: StoreError) -> Error {
Error::StoreError(e)
}
}
impl From<String> for Error {
fn from(e: String) -> Error {
Error::BackendError(e)
}
}