Migrate fork_choice over to new DB

This commit is contained in:
Paul Hauner 2019-05-21 12:58:51 +10:00
parent f8c425d6b4
commit 2128d411bc
No known key found for this signature in database
GPG Key ID: 5E2CFF9B75FA63DF
5 changed files with 65 additions and 99 deletions

View File

@ -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<T: ClientDB + Sized, E> {
pub struct BitwiseLMDGhost<T, E> {
/// A cache of known ancestors at given heights for a specific block.
//TODO: Consider FnvHashMap
cache: HashMap<CacheKey<u64>, Hash256>,
@ -46,30 +43,21 @@ pub struct BitwiseLMDGhost<T: ClientDB + Sized, E> {
/// 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<u64, Hash256>,
/// Block storage access.
block_store: Arc<BeaconBlockStore<T>>,
/// State storage access.
state_store: Arc<BeaconStateStore<T>>,
/// Block and state storage.
store: Arc<T>,
max_known_height: SlotHeight,
_phantom: PhantomData<E>,
}
impl<T, E: EthSpec> BitwiseLMDGhost<T, E>
where
T: ClientDB + Sized,
{
pub fn new(
block_store: Arc<BeaconBlockStore<T>>,
state_store: Arc<BeaconStateStore<T>>,
) -> Self {
impl<T: Store, E: EthSpec> BitwiseLMDGhost<T, E> {
pub fn new(store: Arc<T>) -> 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<Hash256, u64> = HashMap::new();
// gets the current weighted votes
let current_state: BeaconState<E> = 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::<BeaconBlock>(&block_hash)
.ok()?
.expect("Should have returned already if None")
.slot;
@ -243,7 +231,7 @@ where
}
}
impl<T: ClientDB + Sized, E: EthSpec> ForkChoice for BitwiseLMDGhost<T, E> {
impl<T: Store, E: EthSpec> ForkChoice for BitwiseLMDGhost<T, E> {
fn add_block(
&mut self,
block: &BeaconBlock,
@ -252,8 +240,8 @@ impl<T: ClientDB + Sized, E: EthSpec> ForkChoice for BitwiseLMDGhost<T, E> {
) -> Result<(), ForkChoiceError> {
// get the height of the parent
let parent_height = self
.block_store
.get_deserialized(&block.previous_block_root)?
.store
.get::<BeaconBlock>(&block.previous_block_root)?
.ok_or_else(|| ForkChoiceError::MissingBeaconBlock(block.previous_block_root))?
.slot
.height(spec.genesis_slot);
@ -304,16 +292,16 @@ impl<T: ClientDB + Sized, E: EthSpec> ForkChoice for BitwiseLMDGhost<T, E> {
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::<BeaconBlock>(&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::<BeaconBlock>(&attestation_target)?
.ok_or_else(|| ForkChoiceError::MissingBeaconBlock(*attestation_target))?
.slot
.height(spec.genesis_slot);
@ -337,8 +325,8 @@ impl<T: ClientDB + Sized, E: EthSpec> ForkChoice for BitwiseLMDGhost<T, E> {
justified_block_start
);
let block = self
.block_store
.get_deserialized(&justified_block_start)?
.store
.get::<BeaconBlock>(&justified_block_start)?
.ok_or_else(|| ForkChoiceError::MissingBeaconBlock(*justified_block_start))?;
let block_slot = block.slot;
@ -429,8 +417,8 @@ impl<T: ClientDB + Sized, E: EthSpec> ForkChoice for BitwiseLMDGhost<T, E> {
// didn't find head yet, proceed to next iteration
// update block height
block_height = self
.block_store
.get_deserialized(&current_head)?
.store
.get::<BeaconBlock>(&current_head)?
.ok_or_else(|| ForkChoiceError::MissingBeaconBlock(current_head))?
.slot
.height(spec.genesis_slot);

View File

@ -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

View File

@ -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<T>
where
T: ClientDB + Sized,
{
pub struct LongestChain<T> {
/// List of head block hashes
head_block_hashes: Vec<Hash256>,
/// Block storage access.
block_store: Arc<BeaconBlockStore<T>>,
/// Block storage.
store: Arc<T>,
}
impl<T> LongestChain<T>
where
T: ClientDB + Sized,
{
pub fn new(block_store: Arc<BeaconBlockStore<T>>) -> Self {
impl<T: Store> LongestChain<T> {
pub fn new(store: Arc<T>) -> Self {
LongestChain {
head_block_hashes: Vec::new(),
block_store,
store,
}
}
}
impl<T: ClientDB + Sized> ForkChoice for LongestChain<T> {
impl<T: Store> ForkChoice for LongestChain<T> {
fn add_block(
&mut self,
block: &BeaconBlock,
@ -55,9 +49,9 @@ impl<T: ClientDB + Sized> ForkChoice for LongestChain<T> {
* 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));
}

View File

@ -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<T: ClientDB + Sized, E> {
pub struct OptimizedLMDGhost<T, E> {
/// A cache of known ancestors at given heights for a specific block.
//TODO: Consider FnvHashMap
cache: HashMap<CacheKey<u64>, Hash256>,
@ -46,30 +43,21 @@ pub struct OptimizedLMDGhost<T: ClientDB + Sized, E> {
/// 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<u64, Hash256>,
/// Block storage access.
block_store: Arc<BeaconBlockStore<T>>,
/// State storage access.
state_store: Arc<BeaconStateStore<T>>,
/// Block and state storage.
store: Arc<T>,
max_known_height: SlotHeight,
_phantom: PhantomData<E>,
}
impl<T, E: EthSpec> OptimizedLMDGhost<T, E>
where
T: ClientDB + Sized,
{
pub fn new(
block_store: Arc<BeaconBlockStore<T>>,
state_store: Arc<BeaconStateStore<T>>,
) -> Self {
impl<T: Store, E: EthSpec> OptimizedLMDGhost<T, E> {
pub fn new(store: Arc<T>) -> 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<Hash256, u64> = HashMap::new();
// gets the current weighted votes
let current_state: BeaconState<E> = 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::<BeaconBlock>(&block_hash)
.ok()?
.expect("Should have returned already if None")
.slot;
@ -214,7 +202,7 @@ where
}
}
impl<T: ClientDB + Sized, E: EthSpec> ForkChoice for OptimizedLMDGhost<T, E> {
impl<T: Store, E: EthSpec> ForkChoice for OptimizedLMDGhost<T, E> {
fn add_block(
&mut self,
block: &BeaconBlock,
@ -223,8 +211,8 @@ impl<T: ClientDB + Sized, E: EthSpec> ForkChoice for OptimizedLMDGhost<T, E> {
) -> Result<(), ForkChoiceError> {
// get the height of the parent
let parent_height = self
.block_store
.get_deserialized(&block.previous_block_root)?
.store
.get::<BeaconBlock>(&block.previous_block_root)?
.ok_or_else(|| ForkChoiceError::MissingBeaconBlock(block.previous_block_root))?
.slot
.height(spec.genesis_slot);
@ -275,16 +263,16 @@ impl<T: ClientDB + Sized, E: EthSpec> ForkChoice for OptimizedLMDGhost<T, E> {
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::<BeaconBlock>(&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::<BeaconBlock>(&attestation_target)?
.ok_or_else(|| ForkChoiceError::MissingBeaconBlock(*attestation_target))?
.slot
.height(spec.genesis_slot);
@ -308,8 +296,8 @@ impl<T: ClientDB + Sized, E: EthSpec> ForkChoice for OptimizedLMDGhost<T, E> {
justified_block_start
);
let block = self
.block_store
.get_deserialized(&justified_block_start)?
.store
.get::<BeaconBlock>(&justified_block_start)?
.ok_or_else(|| ForkChoiceError::MissingBeaconBlock(*justified_block_start))?;
let block_slot = block.slot;
@ -400,8 +388,8 @@ impl<T: ClientDB + Sized, E: EthSpec> ForkChoice for OptimizedLMDGhost<T, E> {
// didn't find head yet, proceed to next iteration
// update block height
block_height = self
.block_store
.get_deserialized(&current_head)?
.store
.get::<BeaconBlock>(&current_head)?
.ok_or_else(|| ForkChoiceError::MissingBeaconBlock(current_head))?
.slot
.height(spec.genesis_slot);

View File

@ -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<T, E> {
latest_attestation_targets: HashMap<u64, Hash256>,
/// Stores the children for any given parent.
children: HashMap<Hash256, Vec<Hash256>>,
/// Persistent storage
/// Block and state storage.
store: Arc<T>,
_phantom: PhantomData<E>,
}
@ -85,8 +85,8 @@ impl<T: Store, E: EthSpec> SlowLMDGhost<T, E> {
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<T: Store, E: EthSpec> ForkChoice for SlowLMDGhost<T, E> {
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::<BeaconBlock>(&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::<BeaconBlock>(&attestation_target)?
.ok_or_else(|| ForkChoiceError::MissingBeaconBlock(*attestation_target))?
.slot
.height(spec.genesis_slot);
@ -168,8 +168,8 @@ impl<T: Store, E: EthSpec> ForkChoice for SlowLMDGhost<T, E> {
) -> Result<Hash256, ForkChoiceError> {
debug!("Running LMD Ghost Fork-choice rule");
let start = self
.block_store
.get_deserialized(&justified_block_start)?
.store
.get::<BeaconBlock>(&justified_block_start)?
.ok_or_else(|| ForkChoiceError::MissingBeaconBlock(*justified_block_start))?;
let start_state_root = start.state_root;