From 2128d411bc6544202e159edd72f3d78fdb25d33e Mon Sep 17 00:00:00 2001 From: Paul Hauner Date: Tue, 21 May 2019 12:58:51 +1000 Subject: [PATCH] Migrate fork_choice over to new DB --- eth2/fork_choice/src/bitwise_lmd_ghost.rs | 56 ++++++++------------- eth2/fork_choice/src/lib.rs | 4 -- eth2/fork_choice/src/longest_chain.rs | 28 ++++------- eth2/fork_choice/src/optimized_lmd_ghost.rs | 56 ++++++++------------- eth2/fork_choice/src/slow_lmd_ghost.rs | 20 ++++---- 5 files changed, 65 insertions(+), 99 deletions(-) diff --git a/eth2/fork_choice/src/bitwise_lmd_ghost.rs b/eth2/fork_choice/src/bitwise_lmd_ghost.rs index 0bbac6bb68..a76970f016 100644 --- a/eth2/fork_choice/src/bitwise_lmd_ghost.rs +++ b/eth2/fork_choice/src/bitwise_lmd_ghost.rs @@ -3,10 +3,7 @@ extern crate bit_vec; use crate::{ForkChoice, ForkChoiceError}; use bit_vec::BitVec; -use db::{ - stores::{BeaconBlockStore, BeaconStateStore}, - ClientDB, -}; +use db::Store; use log::{debug, trace}; use std::collections::HashMap; use std::marker::PhantomData; @@ -34,7 +31,7 @@ fn power_of_2_below(x: u64) -> u64 { } /// Stores the necessary data structures to run the optimised bitwise lmd ghost algorithm. -pub struct BitwiseLMDGhost { +pub struct BitwiseLMDGhost { /// A cache of known ancestors at given heights for a specific block. //TODO: Consider FnvHashMap cache: HashMap, Hash256>, @@ -46,30 +43,21 @@ pub struct BitwiseLMDGhost { /// The latest attestation targets as a map of validator index to block hash. //TODO: Could this be a fixed size vec latest_attestation_targets: HashMap, - /// Block storage access. - block_store: Arc>, - /// State storage access. - state_store: Arc>, + /// Block and state storage. + store: Arc, max_known_height: SlotHeight, _phantom: PhantomData, } -impl BitwiseLMDGhost -where - T: ClientDB + Sized, -{ - pub fn new( - block_store: Arc>, - state_store: Arc>, - ) -> Self { +impl BitwiseLMDGhost { + pub fn new(store: Arc) -> Self { BitwiseLMDGhost { cache: HashMap::new(), ancestors: vec![HashMap::new(); 16], latest_attestation_targets: HashMap::new(), children: HashMap::new(), max_known_height: SlotHeight::new(0), - block_store, - state_store, + store, _phantom: PhantomData, } } @@ -89,8 +77,8 @@ where let mut latest_votes: HashMap = HashMap::new(); // gets the current weighted votes let current_state: BeaconState = self - .state_store - .get_deserialized(&state_root)? + .store + .get(&state_root)? .ok_or_else(|| ForkChoiceError::MissingBeaconState(*state_root))?; let active_validator_indices = @@ -121,8 +109,8 @@ where // return None if we can't get the block from the db. let block_height = { let block_slot = self - .block_store - .get_deserialized(&block_hash) + .store + .get::(&block_hash) .ok()? .expect("Should have returned already if None") .slot; @@ -243,7 +231,7 @@ where } } -impl ForkChoice for BitwiseLMDGhost { +impl ForkChoice for BitwiseLMDGhost { fn add_block( &mut self, block: &BeaconBlock, @@ -252,8 +240,8 @@ impl ForkChoice for BitwiseLMDGhost { ) -> Result<(), ForkChoiceError> { // get the height of the parent let parent_height = self - .block_store - .get_deserialized(&block.previous_block_root)? + .store + .get::(&block.previous_block_root)? .ok_or_else(|| ForkChoiceError::MissingBeaconBlock(block.previous_block_root))? .slot .height(spec.genesis_slot); @@ -304,16 +292,16 @@ impl ForkChoice for BitwiseLMDGhost { trace!("Old attestation found: {:?}", attestation_target); // get the height of the target block let block_height = self - .block_store - .get_deserialized(&target_block_root)? + .store + .get::(&target_block_root)? .ok_or_else(|| ForkChoiceError::MissingBeaconBlock(*target_block_root))? .slot .height(spec.genesis_slot); // get the height of the past target block let past_block_height = self - .block_store - .get_deserialized(&attestation_target)? + .store + .get::(&attestation_target)? .ok_or_else(|| ForkChoiceError::MissingBeaconBlock(*attestation_target))? .slot .height(spec.genesis_slot); @@ -337,8 +325,8 @@ impl ForkChoice for BitwiseLMDGhost { justified_block_start ); let block = self - .block_store - .get_deserialized(&justified_block_start)? + .store + .get::(&justified_block_start)? .ok_or_else(|| ForkChoiceError::MissingBeaconBlock(*justified_block_start))?; let block_slot = block.slot; @@ -429,8 +417,8 @@ impl ForkChoice for BitwiseLMDGhost { // didn't find head yet, proceed to next iteration // update block height block_height = self - .block_store - .get_deserialized(¤t_head)? + .store + .get::(¤t_head)? .ok_or_else(|| ForkChoiceError::MissingBeaconBlock(current_head))? .slot .height(spec.genesis_slot); diff --git a/eth2/fork_choice/src/lib.rs b/eth2/fork_choice/src/lib.rs index 50aedb7e28..ed3a1ce084 100644 --- a/eth2/fork_choice/src/lib.rs +++ b/eth2/fork_choice/src/lib.rs @@ -16,11 +16,9 @@ //! [`slow_lmd_ghost`]: struct.SlowLmdGhost.html //! [`bitwise_lmd_ghost`]: struct.OptimisedLmdGhost.html -/* pub mod bitwise_lmd_ghost; pub mod longest_chain; pub mod optimized_lmd_ghost; -*/ pub mod slow_lmd_ghost; // use db::stores::BeaconBlockAtSlotError; @@ -28,11 +26,9 @@ pub mod slow_lmd_ghost; use db::Error as DBError; use types::{BeaconBlock, ChainSpec, Hash256}; -/* pub use bitwise_lmd_ghost::BitwiseLMDGhost; pub use longest_chain::LongestChain; pub use optimized_lmd_ghost::OptimizedLMDGhost; -*/ pub use slow_lmd_ghost::SlowLMDGhost; /// Defines the interface for Fork Choices. Each Fork choice will define their own data structures diff --git a/eth2/fork_choice/src/longest_chain.rs b/eth2/fork_choice/src/longest_chain.rs index 423edc567f..6aaf56c321 100644 --- a/eth2/fork_choice/src/longest_chain.rs +++ b/eth2/fork_choice/src/longest_chain.rs @@ -1,31 +1,25 @@ use crate::{ForkChoice, ForkChoiceError}; -use db::{stores::BeaconBlockStore, ClientDB}; +use db::Store; use std::sync::Arc; use types::{BeaconBlock, ChainSpec, Hash256, Slot}; -pub struct LongestChain -where - T: ClientDB + Sized, -{ +pub struct LongestChain { /// List of head block hashes head_block_hashes: Vec, - /// Block storage access. - block_store: Arc>, + /// Block storage. + store: Arc, } -impl LongestChain -where - T: ClientDB + Sized, -{ - pub fn new(block_store: Arc>) -> Self { +impl LongestChain { + pub fn new(store: Arc) -> Self { LongestChain { head_block_hashes: Vec::new(), - block_store, + store, } } } -impl ForkChoice for LongestChain { +impl ForkChoice for LongestChain { fn add_block( &mut self, block: &BeaconBlock, @@ -55,9 +49,9 @@ impl ForkChoice for LongestChain { * Load all the head_block hashes from the DB as SszBeaconBlocks. */ for (index, block_hash) in self.head_block_hashes.iter().enumerate() { - let block = self - .block_store - .get_deserialized(&block_hash)? + let block: BeaconBlock = self + .store + .get(&block_hash)? .ok_or_else(|| ForkChoiceError::MissingBeaconBlock(*block_hash))?; head_blocks.push((index, block)); } diff --git a/eth2/fork_choice/src/optimized_lmd_ghost.rs b/eth2/fork_choice/src/optimized_lmd_ghost.rs index 3f585e3c17..aa7d65c85c 100644 --- a/eth2/fork_choice/src/optimized_lmd_ghost.rs +++ b/eth2/fork_choice/src/optimized_lmd_ghost.rs @@ -2,10 +2,7 @@ extern crate bit_vec; use crate::{ForkChoice, ForkChoiceError}; -use db::{ - stores::{BeaconBlockStore, BeaconStateStore}, - ClientDB, -}; +use db::Store; use log::{debug, trace}; use std::cmp::Ordering; use std::collections::HashMap; @@ -34,7 +31,7 @@ fn power_of_2_below(x: u64) -> u64 { } /// Stores the necessary data structures to run the optimised lmd ghost algorithm. -pub struct OptimizedLMDGhost { +pub struct OptimizedLMDGhost { /// A cache of known ancestors at given heights for a specific block. //TODO: Consider FnvHashMap cache: HashMap, Hash256>, @@ -46,30 +43,21 @@ pub struct OptimizedLMDGhost { /// The latest attestation targets as a map of validator index to block hash. //TODO: Could this be a fixed size vec latest_attestation_targets: HashMap, - /// Block storage access. - block_store: Arc>, - /// State storage access. - state_store: Arc>, + /// Block and state storage. + store: Arc, max_known_height: SlotHeight, _phantom: PhantomData, } -impl OptimizedLMDGhost -where - T: ClientDB + Sized, -{ - pub fn new( - block_store: Arc>, - state_store: Arc>, - ) -> Self { +impl OptimizedLMDGhost { + pub fn new(store: Arc) -> Self { OptimizedLMDGhost { cache: HashMap::new(), ancestors: vec![HashMap::new(); 16], latest_attestation_targets: HashMap::new(), children: HashMap::new(), max_known_height: SlotHeight::new(0), - block_store, - state_store, + store, _phantom: PhantomData, } } @@ -89,8 +77,8 @@ where let mut latest_votes: HashMap = HashMap::new(); // gets the current weighted votes let current_state: BeaconState = self - .state_store - .get_deserialized(&state_root)? + .store + .get(&state_root)? .ok_or_else(|| ForkChoiceError::MissingBeaconState(*state_root))?; let active_validator_indices = @@ -121,8 +109,8 @@ where // return None if we can't get the block from the db. let block_height = { let block_slot = self - .block_store - .get_deserialized(&block_hash) + .store + .get::(&block_hash) .ok()? .expect("Should have returned already if None") .slot; @@ -214,7 +202,7 @@ where } } -impl ForkChoice for OptimizedLMDGhost { +impl ForkChoice for OptimizedLMDGhost { fn add_block( &mut self, block: &BeaconBlock, @@ -223,8 +211,8 @@ impl ForkChoice for OptimizedLMDGhost { ) -> Result<(), ForkChoiceError> { // get the height of the parent let parent_height = self - .block_store - .get_deserialized(&block.previous_block_root)? + .store + .get::(&block.previous_block_root)? .ok_or_else(|| ForkChoiceError::MissingBeaconBlock(block.previous_block_root))? .slot .height(spec.genesis_slot); @@ -275,16 +263,16 @@ impl ForkChoice for OptimizedLMDGhost { trace!("Old attestation found: {:?}", attestation_target); // get the height of the target block let block_height = self - .block_store - .get_deserialized(&target_block_root)? + .store + .get::(&target_block_root)? .ok_or_else(|| ForkChoiceError::MissingBeaconBlock(*target_block_root))? .slot .height(spec.genesis_slot); // get the height of the past target block let past_block_height = self - .block_store - .get_deserialized(&attestation_target)? + .store + .get::(&attestation_target)? .ok_or_else(|| ForkChoiceError::MissingBeaconBlock(*attestation_target))? .slot .height(spec.genesis_slot); @@ -308,8 +296,8 @@ impl ForkChoice for OptimizedLMDGhost { justified_block_start ); let block = self - .block_store - .get_deserialized(&justified_block_start)? + .store + .get::(&justified_block_start)? .ok_or_else(|| ForkChoiceError::MissingBeaconBlock(*justified_block_start))?; let block_slot = block.slot; @@ -400,8 +388,8 @@ impl ForkChoice for OptimizedLMDGhost { // didn't find head yet, proceed to next iteration // update block height block_height = self - .block_store - .get_deserialized(¤t_head)? + .store + .get::(¤t_head)? .ok_or_else(|| ForkChoiceError::MissingBeaconBlock(current_head))? .slot .height(spec.genesis_slot); diff --git a/eth2/fork_choice/src/slow_lmd_ghost.rs b/eth2/fork_choice/src/slow_lmd_ghost.rs index 7768c28675..a41eacbb26 100644 --- a/eth2/fork_choice/src/slow_lmd_ghost.rs +++ b/eth2/fork_choice/src/slow_lmd_ghost.rs @@ -1,7 +1,7 @@ extern crate db; use crate::{ForkChoice, ForkChoiceError}; -use db::{Store, StoreItem}; +use db::Store; use log::{debug, trace}; use std::collections::HashMap; use std::marker::PhantomData; @@ -16,7 +16,7 @@ pub struct SlowLMDGhost { latest_attestation_targets: HashMap, /// Stores the children for any given parent. children: HashMap>, - /// Persistent storage + /// Block and state storage. store: Arc, _phantom: PhantomData, } @@ -85,8 +85,8 @@ impl SlowLMDGhost { for (vote_hash, votes) in latest_votes.iter() { let (root_at_slot, _) = self - .block_store - .block_at_slot(&vote_hash, block_slot)? + .store + .get_block_at_preceeding_slot(*vote_hash, block_slot)? .ok_or_else(|| ForkChoiceError::MissingBeaconBlock(*block_root))?; if root_at_slot == *block_root { count += votes; @@ -138,16 +138,16 @@ impl ForkChoice for SlowLMDGhost { trace!("Old attestation found: {:?}", attestation_target); // get the height of the target block let block_height = self - .block_store - .get_deserialized(&target_block_root)? + .store + .get::(&target_block_root)? .ok_or_else(|| ForkChoiceError::MissingBeaconBlock(*target_block_root))? .slot .height(spec.genesis_slot); // get the height of the past target block let past_block_height = self - .block_store - .get_deserialized(&attestation_target)? + .store + .get::(&attestation_target)? .ok_or_else(|| ForkChoiceError::MissingBeaconBlock(*attestation_target))? .slot .height(spec.genesis_slot); @@ -168,8 +168,8 @@ impl ForkChoice for SlowLMDGhost { ) -> Result { debug!("Running LMD Ghost Fork-choice rule"); let start = self - .block_store - .get_deserialized(&justified_block_start)? + .store + .get::(&justified_block_start)? .ok_or_else(|| ForkChoiceError::MissingBeaconBlock(*justified_block_start))?; let start_state_root = start.state_root;