diff --git a/Cargo.toml b/Cargo.toml index 9723d5ce6..27c473bb9 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -1,7 +1,7 @@ [workspace] members = [ "eth2/fork_choice", - "eth2/fork_choice_2", + "eth2/lmd_ghost", "eth2/operation_pool", "eth2/state_processing", "eth2/types", diff --git a/eth2/fork_choice_2/src/lib.rs b/eth2/fork_choice_2/src/lib.rs deleted file mode 100644 index 1c2451666..000000000 --- a/eth2/fork_choice_2/src/lib.rs +++ /dev/null @@ -1,36 +0,0 @@ -pub mod reduced_tree; - -use std::sync::Arc; -use types::{Hash256, Slot}; - -type Result = std::result::Result; - -#[derive(Debug, PartialEq)] -pub enum Error { - BackendError(String), -} - -pub trait LmdGhostBackend: Send + Sync { - fn new(store: Arc) -> Self; - - fn process_message( - &self, - validator_index: usize, - block_hash: Hash256, - block_slot: Slot, - ) -> Result<()>; - - fn find_head(&self) -> Result; -} - -pub struct ForkChoice { - algorithm: T, -} - -impl> ForkChoice { - pub fn new(store: Arc) -> Self { - Self { - algorithm: T::new(store), - } - } -} diff --git a/eth2/fork_choice_2/Cargo.toml b/eth2/lmd_ghost/Cargo.toml similarity index 88% rename from eth2/fork_choice_2/Cargo.toml rename to eth2/lmd_ghost/Cargo.toml index 61b3f7dcf..788708faa 100644 --- a/eth2/fork_choice_2/Cargo.toml +++ b/eth2/lmd_ghost/Cargo.toml @@ -1,19 +1,20 @@ [package] -name = "fork_choice_2" +name = "lmd_ghost" version = "0.1.0" authors = ["Age Manning ", "Paul Hauner "] edition = "2018" [dependencies] +parking_lot = "0.7" store = { path = "../../beacon_node/store" } ssz = { path = "../utils/ssz" } +state_processing = { path = "../state_processing" } types = { path = "../types" } log = "0.4.6" bit-vec = "0.5.0" [dev-dependencies] criterion = "0.2" -parking_lot = "0.7" hex = "0.3.2" yaml-rust = "0.4.2" bls = { path = "../utils/bls" } diff --git a/eth2/lmd_ghost/src/lib.rs b/eth2/lmd_ghost/src/lib.rs new file mode 100644 index 000000000..27801595a --- /dev/null +++ b/eth2/lmd_ghost/src/lib.rs @@ -0,0 +1,99 @@ +pub mod reduced_tree; + +use state_processing::common::get_attesting_indices_unsorted; +use std::marker::PhantomData; +use std::sync::Arc; +use types::{Attestation, BeaconBlock, BeaconState, BeaconStateError, EthSpec, Hash256, Slot}; + +type Result = std::result::Result; + +#[derive(Debug, PartialEq)] +pub enum Error { + BackendError(String), + BeaconStateError(BeaconStateError), +} + +pub trait LmdGhostBackend: Send + Sync { + fn new(store: Arc) -> Self; + + fn process_message( + &self, + validator_index: usize, + block_hash: Hash256, + block_slot: Slot, + ) -> Result<()>; + + fn find_head(&self) -> Result; +} + +pub struct ForkChoice { + backend: T, + _phantom: PhantomData, +} + +impl ForkChoice +where + T: LmdGhostBackend, + E: EthSpec, +{ + pub fn new(store: Arc) -> Self { + Self { + backend: T::new(store), + _phantom: PhantomData, + } + } + + pub fn find_head(&self) -> Result { + self.backend.find_head() + } + + pub fn process_attestation( + &self, + state: &BeaconState, + attestation: &Attestation, + ) -> Result<()> { + let validator_indices = get_attesting_indices_unsorted( + state, + &attestation.data, + &attestation.aggregation_bitfield, + )?; + + let block_hash = attestation.data.target_root; + + // TODO: what happens when the target root is not the same slot as the block? + let block_slot = attestation + .data + .target_epoch + .start_slot(E::slots_per_epoch()); + + for validator_index in validator_indices { + self.backend + .process_message(validator_index, block_hash, block_slot)?; + } + + Ok(()) + } + + pub fn process_block( + &self, + state: &BeaconState, + block: &BeaconBlock, + block_hash: Hash256, + block_proposer: usize, + ) -> Result<()> { + self.backend + .process_message(block_proposer, block_hash, block.slot)?; + + for attestation in &block.body.attestations { + self.process_attestation(state, attestation)?; + } + + Ok(()) + } +} + +impl From for Error { + fn from(e: BeaconStateError) -> Error { + Error::BeaconStateError(e) + } +} diff --git a/eth2/fork_choice_2/src/reduced_tree.rs b/eth2/lmd_ghost/src/reduced_tree.rs similarity index 99% rename from eth2/fork_choice_2/src/reduced_tree.rs rename to eth2/lmd_ghost/src/reduced_tree.rs index ca32c17f6..b0e82d984 100644 --- a/eth2/fork_choice_2/src/reduced_tree.rs +++ b/eth2/lmd_ghost/src/reduced_tree.rs @@ -69,7 +69,7 @@ pub struct Vote { slot: Slot, } -impl LmdGhostBackend for ThreadSafeReducedTree +impl LmdGhostBackend for ThreadSafeReducedTree where T: Store, E: EthSpec,