Skip to content

Commit

Permalink
updates for fc store unrealized justification tracking
Browse files Browse the repository at this point in the history
  • Loading branch information
realbigsean committed Jun 14, 2022
1 parent e166d55 commit dc51a24
Show file tree
Hide file tree
Showing 10 changed files with 262 additions and 127 deletions.
36 changes: 32 additions & 4 deletions beacon_node/beacon_chain/src/beacon_fork_choice_store.rs
Original file line number Diff line number Diff line change
Expand Up @@ -155,6 +155,8 @@ pub struct BeaconForkChoiceStore<E: EthSpec, Hot: ItemStore<E>, Cold: ItemStore<
justified_checkpoint: Checkpoint,
justified_balances: Vec<u64>,
best_justified_checkpoint: Checkpoint,
unrealized_justified_checkpoint: Checkpoint,
unrealized_finalized_checkpoint: Checkpoint,
proposer_boost_root: Hash256,
_phantom: PhantomData<E>,
}
Expand Down Expand Up @@ -201,6 +203,8 @@ where
justified_balances: anchor_state.balances().clone().into(),
finalized_checkpoint,
best_justified_checkpoint: justified_checkpoint,
unrealized_justified_checkpoint: justified_checkpoint,
unrealized_finalized_checkpoint: finalized_checkpoint,
proposer_boost_root: Hash256::zero(),
_phantom: PhantomData,
}
Expand All @@ -216,6 +220,8 @@ where
justified_checkpoint: self.justified_checkpoint,
justified_balances: self.justified_balances.clone(),
best_justified_checkpoint: self.best_justified_checkpoint,
unrealized_justified_checkpoint: self.unrealized_justified_checkpoint,
unrealized_finalized_checkpoint: self.unrealized_finalized_checkpoint,
proposer_boost_root: self.proposer_boost_root,
}
}
Expand All @@ -233,6 +239,8 @@ where
justified_checkpoint: persisted.justified_checkpoint,
justified_balances: persisted.justified_balances,
best_justified_checkpoint: persisted.best_justified_checkpoint,
unrealized_justified_checkpoint: persisted.unrealized_justified_checkpoint,
unrealized_finalized_checkpoint: persisted.unrealized_finalized_checkpoint,
proposer_boost_root: persisted.proposer_boost_root,
_phantom: PhantomData,
})
Expand Down Expand Up @@ -280,6 +288,14 @@ where
&self.finalized_checkpoint
}

fn unrealized_justified_checkpoint(&self) -> &Checkpoint {
&self.unrealized_justified_checkpoint
}

fn unrealized_finalized_checkpoint(&self) -> &Checkpoint {
&self.unrealized_finalized_checkpoint
}

fn proposer_boost_root(&self) -> Hash256 {
self.proposer_boost_root
}
Expand Down Expand Up @@ -323,29 +339,41 @@ where
self.best_justified_checkpoint = checkpoint
}

fn set_unrealized_justified_checkpoint(&mut self, checkpoint: Checkpoint) {
self.unrealized_justified_checkpoint = checkpoint;
}

fn set_unrealized_finalized_checkpoint(&mut self, checkpoint: Checkpoint) {
self.unrealized_finalized_checkpoint = checkpoint;
}

fn set_proposer_boost_root(&mut self, proposer_boost_root: Hash256) {
self.proposer_boost_root = proposer_boost_root;
}
}

/// A container which allows persisting the `BeaconForkChoiceStore` to the on-disk database.
#[superstruct(
variants(V1, V7, V8),
variants(V1, V7, V8, V10),
variant_attributes(derive(Encode, Decode)),
no_enum
)]
pub struct PersistedForkChoiceStore {
#[superstruct(only(V1, V7))]
pub balances_cache: BalancesCacheV1,
#[superstruct(only(V8))]
#[superstruct(only(V8, V10))]
pub balances_cache: BalancesCacheV8,
pub time: Slot,
pub finalized_checkpoint: Checkpoint,
pub justified_checkpoint: Checkpoint,
pub justified_balances: Vec<u64>,
pub best_justified_checkpoint: Checkpoint,
#[superstruct(only(V7, V8))]
#[superstruct(only(V10))]
pub unrealized_justified_checkpoint: Checkpoint,
#[superstruct(only(V10))]
pub unrealized_finalized_checkpoint: Checkpoint,
#[superstruct(only(V7, V8, V10))]
pub proposer_boost_root: Hash256,
}

pub type PersistedForkChoiceStore = PersistedForkChoiceStoreV8;
pub type PersistedForkChoiceStore = PersistedForkChoiceStoreV10;
10 changes: 7 additions & 3 deletions beacon_node/beacon_chain/src/persisted_fork_choice.rs
Original file line number Diff line number Diff line change
@@ -1,16 +1,17 @@
use crate::beacon_fork_choice_store::{
PersistedForkChoiceStoreV1, PersistedForkChoiceStoreV7, PersistedForkChoiceStoreV8,
PersistedForkChoiceStoreV1, PersistedForkChoiceStoreV10, PersistedForkChoiceStoreV7,
PersistedForkChoiceStoreV8,
};
use ssz::{Decode, Encode};
use ssz_derive::{Decode, Encode};
use store::{DBColumn, Error, StoreItem};
use superstruct::superstruct;

// If adding a new version you should update this type alias and fix the breakages.
pub type PersistedForkChoice = PersistedForkChoiceV8;
pub type PersistedForkChoice = PersistedForkChoiceV10;

#[superstruct(
variants(V1, V7, V8),
variants(V1, V7, V8, V10),
variant_attributes(derive(Encode, Decode)),
no_enum
)]
Expand All @@ -22,6 +23,8 @@ pub struct PersistedForkChoice {
pub fork_choice_store: PersistedForkChoiceStoreV7,
#[superstruct(only(V8))]
pub fork_choice_store: PersistedForkChoiceStoreV8,
#[superstruct(only(V10))]
pub fork_choice_store: PersistedForkChoiceStoreV10,
}

macro_rules! impl_store_item {
Expand All @@ -45,3 +48,4 @@ macro_rules! impl_store_item {
impl_store_item!(PersistedForkChoiceV1);
impl_store_item!(PersistedForkChoiceV7);
impl_store_item!(PersistedForkChoiceV8);
impl_store_item!(PersistedForkChoiceV10);
39 changes: 32 additions & 7 deletions beacon_node/beacon_chain/src/schema_change/migration_schema_v10.rs
Original file line number Diff line number Diff line change
@@ -1,14 +1,15 @@
use crate::persisted_fork_choice::PersistedForkChoiceV8;
use crate::beacon_fork_choice_store::{PersistedForkChoiceStoreV10, PersistedForkChoiceStoreV8};
use crate::persisted_fork_choice::{PersistedForkChoiceV10, PersistedForkChoiceV8};
use crate::schema_change::{
types::{SszContainerV7, SszContainerV9},
types::{SszContainerV10, SszContainerV7},
StoreError,
};
use proto_array::core::SszContainer;
use ssz::{Decode, Encode};

pub fn update_fork_choice(
mut fork_choice: PersistedForkChoiceV8,
) -> Result<PersistedForkChoiceV8, StoreError> {
) -> Result<PersistedForkChoiceV10, StoreError> {
let ssz_container_v7 = SszContainerV7::from_ssz_bytes(
&fork_choice.fork_choice.proto_array_bytes,
)
Expand All @@ -21,10 +22,34 @@ pub fn update_fork_choice(

// These transformations instantiate `node.unrealized_justified_checkpoint` and
// `node.unrealized_finalized_checkpoint` to `None`.
let ssz_container_v9: SszContainerV9 = ssz_container_v7.into();
let ssz_container: SszContainer = ssz_container_v9.into();

let ssz_container_v10: SszContainerV10 = ssz_container_v7.into();
let ssz_container: SszContainer = ssz_container_v10.into();
fork_choice.fork_choice.proto_array_bytes = ssz_container.as_ssz_bytes();

Ok(fork_choice)
Ok(fork_choice.into())
}

impl From<PersistedForkChoiceStoreV8> for PersistedForkChoiceStoreV10 {
fn from(other: PersistedForkChoiceStoreV8) -> Self {
Self {
balances_cache: other.balances_cache,
time: other.time,
finalized_checkpoint: other.finalized_checkpoint,
justified_checkpoint: other.justified_checkpoint,
justified_balances: other.justified_balances,
best_justified_checkpoint: other.best_justified_checkpoint,
unrealized_justified_checkpoint: other.best_justified_checkpoint,
unrealized_finalized_checkpoint: other.finalized_checkpoint,
proposer_boost_root: other.proposer_boost_root,
}
}
}

impl From<PersistedForkChoiceV8> for PersistedForkChoiceV10 {
fn from(other: PersistedForkChoiceV8) -> Self {
Self {
fork_choice: other.fork_choice,
fork_choice_store: other.fork_choice_store.into(),
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
use crate::beacon_chain::BeaconChainTypes;
use crate::beacon_fork_choice_store::{PersistedForkChoiceStoreV1, PersistedForkChoiceStoreV7};
use crate::persisted_fork_choice::{PersistedForkChoiceV1, PersistedForkChoiceV7};
use crate::schema_change::types::{ProtoNodeV6, SszContainerV6, SszContainerV7, SszContainerV9};
use crate::schema_change::types::{ProtoNodeV6, SszContainerV10, SszContainerV6, SszContainerV7};
use crate::types::{Checkpoint, Epoch, Hash256};
use crate::types::{EthSpec, Slot};
use crate::{BeaconForkChoiceStore, BeaconSnapshot};
Expand Down Expand Up @@ -82,8 +82,8 @@ pub(crate) fn update_fork_choice<T: BeaconChainTypes>(
// to `None`.
let ssz_container_v7: SszContainerV7 =
ssz_container_v6.into_ssz_container_v7(justified_checkpoint, finalized_checkpoint);
let ssz_container_v9: SszContainerV9 = ssz_container_v7.into();
let ssz_container: SszContainer = ssz_container_v9.into();
let ssz_container_v10: SszContainerV10 = ssz_container_v7.into();
let ssz_container: SszContainer = ssz_container_v10.into();
let mut fork_choice: ProtoArrayForkChoice = ssz_container.into();

update_checkpoints::<T>(finalized_checkpoint.root, &nodes_v6, &mut fork_choice, db)
Expand Down
40 changes: 20 additions & 20 deletions beacon_node/beacon_chain/src/schema_change/types.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ four_byte_option_impl!(four_byte_option_usize, usize);
four_byte_option_impl!(four_byte_option_checkpoint, Checkpoint);

#[superstruct(
variants(V1, V6, V7, V9),
variants(V1, V6, V7, V10),
variant_attributes(derive(Clone, PartialEq, Debug, Encode, Decode)),
no_enum
)]
Expand All @@ -30,23 +30,23 @@ pub struct ProtoNode {
#[superstruct(only(V1, V6))]
pub finalized_epoch: Epoch,
#[ssz(with = "four_byte_option_checkpoint")]
#[superstruct(only(V7, V9))]
#[superstruct(only(V7, V10))]
pub justified_checkpoint: Option<Checkpoint>,
#[ssz(with = "four_byte_option_checkpoint")]
#[superstruct(only(V7, V9))]
#[superstruct(only(V7, V10))]
pub finalized_checkpoint: Option<Checkpoint>,
pub weight: u64,
#[ssz(with = "four_byte_option_usize")]
pub best_child: Option<usize>,
#[ssz(with = "four_byte_option_usize")]
pub best_descendant: Option<usize>,
#[superstruct(only(V6, V7, V9))]
#[superstruct(only(V6, V7, V10))]
pub execution_status: ExecutionStatus,
#[ssz(with = "four_byte_option_checkpoint")]
#[superstruct(only(V9))]
#[superstruct(only(V10))]
pub unrealized_justified_checkpoint: Option<Checkpoint>,
#[ssz(with = "four_byte_option_checkpoint")]
#[superstruct(only(V9))]
#[superstruct(only(V10))]
pub unrealized_finalized_checkpoint: Option<Checkpoint>,
}

Expand Down Expand Up @@ -94,9 +94,9 @@ impl Into<ProtoNodeV7> for ProtoNodeV6 {
}
}

impl Into<ProtoNodeV9> for ProtoNodeV7 {
fn into(self) -> ProtoNodeV9 {
ProtoNodeV9 {
impl Into<ProtoNodeV10> for ProtoNodeV7 {
fn into(self) -> ProtoNodeV10 {
ProtoNodeV10 {
slot: self.slot,
state_root: self.state_root,
target_root: self.target_root,
Expand All @@ -116,7 +116,7 @@ impl Into<ProtoNodeV9> for ProtoNodeV7 {
}
}

impl Into<ProtoNode> for ProtoNodeV9 {
impl Into<ProtoNode> for ProtoNodeV10 {
fn into(self) -> ProtoNode {
ProtoNode {
slot: self.slot,
Expand Down Expand Up @@ -159,7 +159,7 @@ impl From<ProtoNode> for ProtoNodeV7 {
}

#[superstruct(
variants(V1, V6, V7, V9),
variants(V1, V6, V7, V10),
variant_attributes(derive(Encode, Decode)),
no_enum
)]
Expand All @@ -172,20 +172,20 @@ pub struct SszContainer {
pub justified_epoch: Epoch,
#[superstruct(only(V1, V6))]
pub finalized_epoch: Epoch,
#[superstruct(only(V7, V9))]
#[superstruct(only(V7, V10))]
pub justified_checkpoint: Checkpoint,
#[superstruct(only(V7, V9))]
#[superstruct(only(V7, V10))]
pub finalized_checkpoint: Checkpoint,
#[superstruct(only(V1))]
pub nodes: Vec<ProtoNodeV1>,
#[superstruct(only(V6))]
pub nodes: Vec<ProtoNodeV6>,
#[superstruct(only(V7))]
pub nodes: Vec<ProtoNodeV7>,
#[superstruct(only(V9))]
pub nodes: Vec<ProtoNodeV9>,
#[superstruct(only(V10))]
pub nodes: Vec<ProtoNodeV10>,
pub indices: Vec<(Hash256, usize)>,
#[superstruct(only(V7, V9))]
#[superstruct(only(V7, V10))]
pub previous_proposer_boost: ProposerBoost,
}

Expand Down Expand Up @@ -226,11 +226,11 @@ impl SszContainerV6 {
}
}

impl Into<SszContainerV9> for SszContainerV7 {
fn into(self) -> SszContainerV9 {
impl Into<SszContainerV10> for SszContainerV7 {
fn into(self) -> SszContainerV10 {
let nodes = self.nodes.into_iter().map(Into::into).collect();

SszContainerV9 {
SszContainerV10 {
votes: self.votes,
balances: self.balances,
prune_threshold: self.prune_threshold,
Expand All @@ -243,7 +243,7 @@ impl Into<SszContainerV9> for SszContainerV7 {
}
}

impl Into<SszContainer> for SszContainerV9 {
impl Into<SszContainer> for SszContainerV10 {
fn into(self) -> SszContainer {
let nodes = self.nodes.into_iter().map(Into::into).collect();

Expand Down
2 changes: 1 addition & 1 deletion beacon_node/store/src/metadata.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ use ssz::{Decode, Encode};
use ssz_derive::{Decode, Encode};
use types::{Checkpoint, Hash256, Slot};

pub const CURRENT_SCHEMA_VERSION: SchemaVersion = SchemaVersion(9);
pub const CURRENT_SCHEMA_VERSION: SchemaVersion = SchemaVersion(10);

// All the keys that get stored under the `BeaconMeta` column.
//
Expand Down
Loading

0 comments on commit dc51a24

Please sign in to comment.