Pull basic LMD Ghost into fork choice crate.

- Moves the basic implementation into the fork choice crate.
- Builds the option of fork choices into beacon_struct.
This commit is contained in:
Age Manning
2019-02-05 17:15:15 +11:00
parent 8109fad7bf
commit 0972c67d19
7 changed files with 214 additions and 198 deletions

View File

@@ -14,6 +14,8 @@ use types::{
AttestationData, BeaconBlock, BeaconBlockBody, BeaconState, ChainSpec, Eth1Data,
FreeAttestation, Hash256, PublicKey, Signature,
};
use fork_choice::{longest_chain, basic_lmd_ghost};
use fork_choice::{ForkChoice};
use crate::attestation_aggregator::{AttestationAggregator, Outcome as AggregationOutcome};
use crate::attestation_targets::AttestationTargets;
@@ -571,8 +573,40 @@ where
Some((block, state))
}
// For now, we give it the option of choosing which fork choice to use
pub fn fork_choice(&self, fork_choice: ForkChoice) -> Result<(), Error> {
let present_head = &self.finalized_head().beacon_block_root;
let new_head = match fork_choice {
ForkChoice::BasicLMDGhost => basic_lmd_ghost(&self.finalized_head().beacon_block_root)?,
// TODO: Implement others
_ => present_head
}
if new_head != *present_head {
let block = self
.block_store
.get_deserialized(&new_head)?
.ok_or_else(|| Error::MissingBeaconBlock(new_head))?;
let block_root = block.canonical_root();
let state = self
.state_store
.get_deserialized(&block.state_root)?
.ok_or_else(|| Error::MissingBeaconState(block.state_root))?;
let state_root = state.canonical_root();
self.update_canonical_head(block, block_root, state, state_root);
}
Ok(())
}
impl From<DBError> for Error {
fn from(e: DBError) -> Error {
Error::DBError(e.message)