Apply store refactor to new fork choice

This commit is contained in:
Michael Sproul 2020-06-17 12:50:32 +10:00
parent e6f97bf466
commit 81c9fe3817
No known key found for this signature in database
GPG Key ID: 77B1309D2E54E914
6 changed files with 33 additions and 13 deletions

1
Cargo.lock generated
View File

@ -4561,7 +4561,6 @@ dependencies = [
"db-key",
"eth2_ssz",
"eth2_ssz_derive",
"fork_choice",
"itertools 0.9.0",
"lazy_static",
"leveldb",

View File

@ -203,7 +203,9 @@ pub struct BeaconChain<T: BeaconChainTypes> {
pub genesis_validators_root: Hash256,
/// A state-machine that is updated with information from the network and chooses a canonical
/// head block.
pub fork_choice: RwLock<ForkChoice<BeaconForkChoiceStore<T::Store, T::EthSpec>, T::EthSpec>>,
pub fork_choice: RwLock<
ForkChoice<BeaconForkChoiceStore<T::EthSpec, T::HotStore, T::ColdStore>, T::EthSpec>,
>,
/// A handler for events generated by the beacon chain.
pub event_handler: T::EventHandler,
/// Used to track the heads of the beacon chain.

View File

@ -9,7 +9,7 @@ use fork_choice::ForkChoiceStore;
use ssz_derive::{Decode, Encode};
use std::marker::PhantomData;
use std::sync::Arc;
use store::{Error as StoreError, Store};
use store::{Error as StoreError, HotColdDB, ItemStore};
use types::{
BeaconBlock, BeaconState, BeaconStateError, Checkpoint, EthSpec, Hash256, SignedBeaconBlock,
Slot,
@ -158,8 +158,8 @@ impl BalancesCache {
/// Implements `fork_choice::ForkChoiceStore` in order to provide a persistent backing to the
/// `fork_choice::ForkChoice` struct.
#[derive(Debug)]
pub struct BeaconForkChoiceStore<S, E> {
store: Arc<S>,
pub struct BeaconForkChoiceStore<E: EthSpec, Hot: ItemStore<E>, Cold: ItemStore<E>> {
store: Arc<HotColdDB<E, Hot, Cold>>,
balances_cache: BalancesCache,
time: Slot,
finalized_checkpoint: Checkpoint,
@ -169,7 +169,12 @@ pub struct BeaconForkChoiceStore<S, E> {
_phantom: PhantomData<E>,
}
impl<S, E> PartialEq for BeaconForkChoiceStore<S, E> {
impl<E, Hot, Cold> PartialEq for BeaconForkChoiceStore<E, Hot, Cold>
where
E: EthSpec,
Hot: ItemStore<E>,
Cold: ItemStore<E>,
{
/// This implementation ignores the `store` and `slot_clock`.
fn eq(&self, other: &Self) -> bool {
self.balances_cache == other.balances_cache
@ -181,7 +186,12 @@ impl<S, E> PartialEq for BeaconForkChoiceStore<S, E> {
}
}
impl<S: Store<E>, E: EthSpec> BeaconForkChoiceStore<S, E> {
impl<E, Hot, Cold> BeaconForkChoiceStore<E, Hot, Cold>
where
E: EthSpec,
Hot: ItemStore<E>,
Cold: ItemStore<E>,
{
/// Initialize `Self` from some `anchor` checkpoint which may or may not be the genesis state.
///
/// ## Specification
@ -193,7 +203,10 @@ impl<S: Store<E>, E: EthSpec> BeaconForkChoiceStore<S, E> {
/// ## Notes:
///
/// It is assumed that `anchor` is already persisted in `store`.
pub fn get_forkchoice_store(store: Arc<S>, anchor: &BeaconSnapshot<E>) -> Self {
pub fn get_forkchoice_store(
store: Arc<HotColdDB<E, Hot, Cold>>,
anchor: &BeaconSnapshot<E>,
) -> Self {
let anchor_state = &anchor.beacon_state;
let mut anchor_block_header = anchor_state.latest_block_header.clone();
if anchor_block_header.state_root == Hash256::zero() {
@ -235,7 +248,7 @@ impl<S: Store<E>, E: EthSpec> BeaconForkChoiceStore<S, E> {
/// Restore `Self` from a previously-generated `PersistedForkChoiceStore`.
pub fn from_persisted(
persisted: PersistedForkChoiceStore,
store: Arc<S>,
store: Arc<HotColdDB<E, Hot, Cold>>,
) -> Result<Self, Error> {
Ok(Self {
store,
@ -250,7 +263,12 @@ impl<S: Store<E>, E: EthSpec> BeaconForkChoiceStore<S, E> {
}
}
impl<S: Store<E>, E: EthSpec> ForkChoiceStore<E> for BeaconForkChoiceStore<S, E> {
impl<E, Hot, Cold> ForkChoiceStore<E> for BeaconForkChoiceStore<E, Hot, Cold>
where
E: EthSpec,
Hot: ItemStore<E>,
Cold: ItemStore<E>,
{
type Error = Error;
fn get_current_slot(&self) -> Slot {

View File

@ -29,5 +29,4 @@ serde_derive = "1.0.110"
lazy_static = "1.4.0"
lighthouse_metrics = { path = "../../common/lighthouse_metrics" }
lru = "0.5.1"
fork_choice = { path = "../../consensus/fork_choice" }
sloggers = "1.0.0"

View File

@ -34,6 +34,7 @@ pub const SPLIT_DB_KEY: &str = "FREEZERDBSPLITFREEZERDBSPLITFREE";
///
/// Stores vector fields like the `block_roots` and `state_roots` separately, and only stores
/// intermittent "restore point" states pre-finalization.
#[derive(Debug)]
pub struct HotColdDB<E: EthSpec, Hot: ItemStore<E>, Cold: ItemStore<E>> {
/// The slot and state root at the point where the database is split between hot and cold.
///

View File

@ -9,7 +9,7 @@ use fork_choice::{
SAFE_SLOTS_TO_UPDATE_JUSTIFIED,
};
use std::sync::Mutex;
use store::{MemoryStore, Store};
use store::{MemoryStore, StoreConfig};
use types::{
test_utils::{generate_deterministic_keypair, generate_deterministic_keypairs},
Epoch, EthSpec, IndexedAttestation, MainnetEthSpec, Slot,
@ -41,6 +41,7 @@ impl ForkChoiceTest {
generate_deterministic_keypairs(VALIDATOR_COUNT),
// Ensure we always have an aggregator for each slot.
u64::max_value(),
StoreConfig::default(),
);
Self { harness }
@ -49,7 +50,7 @@ impl ForkChoiceTest {
/// Get a value from the `ForkChoice` instantiation.
fn get<T, U>(&self, func: T) -> U
where
T: Fn(&BeaconForkChoiceStore<MemoryStore<E>, E>) -> U,
T: Fn(&BeaconForkChoiceStore<E, MemoryStore<E>, MemoryStore<E>>) -> U,
{
func(&self.harness.chain.fork_choice.read().fc_store())
}