Skip to content

Commit 2b82413

Browse files
committed
Properly Persist AnchorState in ForkChoice
1 parent 8c071ce commit 2b82413

File tree

7 files changed

+115
-18
lines changed

7 files changed

+115
-18
lines changed

beacon_node/beacon_chain/src/beacon_fork_choice_store.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -389,9 +389,9 @@ pub struct PersistedForkChoiceStore {
389389
pub equivocating_indices: BTreeSet<u64>,
390390
}
391391

392-
impl Into<PersistedForkChoiceStore> for PersistedForkChoiceStoreV11 {
393-
fn into(self) -> PersistedForkChoiceStore {
394-
PersistedForkChoiceStore {
392+
impl Into<PersistedForkChoiceStoreV17> for PersistedForkChoiceStoreV11 {
393+
fn into(self) -> PersistedForkChoiceStoreV17 {
394+
PersistedForkChoiceStoreV17 {
395395
balances_cache: self.balances_cache,
396396
time: self.time,
397397
finalized_checkpoint: self.finalized_checkpoint,
@@ -405,7 +405,7 @@ impl Into<PersistedForkChoiceStore> for PersistedForkChoiceStoreV11 {
405405
}
406406
}
407407

408-
impl Into<PersistedForkChoiceStoreV11> for PersistedForkChoiceStore {
408+
impl Into<PersistedForkChoiceStoreV11> for PersistedForkChoiceStoreV17 {
409409
fn into(self) -> PersistedForkChoiceStoreV11 {
410410
PersistedForkChoiceStoreV11 {
411411
balances_cache: self.balances_cache,

beacon_node/beacon_chain/src/persisted_fork_choice.rs

Lines changed: 30 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -5,31 +5,34 @@ use store::{DBColumn, Error, StoreItem};
55
use superstruct::superstruct;
66

77
// If adding a new version you should update this type alias and fix the breakages.
8-
pub type PersistedForkChoice = PersistedForkChoiceV17;
8+
pub type PersistedForkChoice = PersistedForkChoiceV20;
99

1010
#[superstruct(
11-
variants(V11, V17),
11+
variants(V11, V17, V20),
1212
variant_attributes(derive(Encode, Decode)),
1313
no_enum
1414
)]
1515
pub struct PersistedForkChoice {
16-
pub fork_choice: fork_choice::PersistedForkChoice,
16+
#[superstruct(only(V11, V17))]
17+
pub fork_choice: fork_choice::PersistedForkChoiceV19,
18+
#[superstruct(only(V20))]
19+
pub fork_choice: fork_choice::PersistedForkChoiceV20,
1720
#[superstruct(only(V11))]
1821
pub fork_choice_store: PersistedForkChoiceStoreV11,
19-
#[superstruct(only(V17))]
22+
#[superstruct(only(V17, V20))]
2023
pub fork_choice_store: PersistedForkChoiceStoreV17,
2124
}
2225

23-
impl Into<PersistedForkChoice> for PersistedForkChoiceV11 {
24-
fn into(self) -> PersistedForkChoice {
25-
PersistedForkChoice {
26+
impl Into<PersistedForkChoiceV17> for PersistedForkChoiceV11 {
27+
fn into(self) -> PersistedForkChoiceV17 {
28+
PersistedForkChoiceV17 {
2629
fork_choice: self.fork_choice,
2730
fork_choice_store: self.fork_choice_store.into(),
2831
}
2932
}
3033
}
3134

32-
impl Into<PersistedForkChoiceV11> for PersistedForkChoice {
35+
impl Into<PersistedForkChoiceV11> for PersistedForkChoiceV17 {
3336
fn into(self) -> PersistedForkChoiceV11 {
3437
PersistedForkChoiceV11 {
3538
fork_choice: self.fork_choice,
@@ -38,6 +41,24 @@ impl Into<PersistedForkChoiceV11> for PersistedForkChoice {
3841
}
3942
}
4043

44+
impl Into<PersistedForkChoiceV20> for PersistedForkChoiceV17 {
45+
fn into(self) -> PersistedForkChoiceV20 {
46+
PersistedForkChoiceV20 {
47+
fork_choice: self.fork_choice.into(),
48+
fork_choice_store: self.fork_choice_store,
49+
}
50+
}
51+
}
52+
53+
impl Into<PersistedForkChoiceV17> for PersistedForkChoiceV20 {
54+
fn into(self) -> PersistedForkChoiceV17 {
55+
PersistedForkChoiceV17 {
56+
fork_choice: self.fork_choice.into(),
57+
fork_choice_store: self.fork_choice_store,
58+
}
59+
}
60+
}
61+
4162
macro_rules! impl_store_item {
4263
($type:ty) => {
4364
impl StoreItem for $type {
@@ -58,3 +79,4 @@ macro_rules! impl_store_item {
5879

5980
impl_store_item!(PersistedForkChoiceV11);
6081
impl_store_item!(PersistedForkChoiceV17);
82+
impl_store_item!(PersistedForkChoiceV20);

beacon_node/beacon_chain/src/schema_change.rs

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
mod migration_schema_v17;
33
mod migration_schema_v18;
44
mod migration_schema_v19;
5+
mod migration_schema_v20;
56

67
use crate::beacon_chain::BeaconChainTypes;
78
use crate::types::ChainSpec;
@@ -78,6 +79,14 @@ pub fn migrate_schema<T: BeaconChainTypes>(
7879
let ops = migration_schema_v19::downgrade_from_v19::<T>(db.clone(), log)?;
7980
db.store_schema_version_atomically(to, ops)
8081
}
82+
(SchemaVersion(19), SchemaVersion(20)) => {
83+
let ops = migration_schema_v20::upgrade_to_v20::<T>(db.clone(), log)?;
84+
db.store_schema_version_atomically(to, ops)
85+
}
86+
(SchemaVersion(20), SchemaVersion(19)) => {
87+
let ops = migration_schema_v20::downgrade_from_v20::<T>(db.clone(), log)?;
88+
db.store_schema_version_atomically(to, ops)
89+
}
8190
// Anything else is an error.
8291
(_, _) => Err(HotColdDBError::UnsupportedSchemaVersion {
8392
target_version: to,
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
use crate::beacon_chain::{BeaconChainTypes, FORK_CHOICE_DB_KEY};
2+
use crate::persisted_fork_choice::{PersistedForkChoiceV17, PersistedForkChoiceV20};
3+
use slog::{debug, Logger};
4+
use std::sync::Arc;
5+
use store::{Error, HotColdDB, KeyValueStoreOp, StoreItem};
6+
7+
pub fn upgrade_to_v20<T: BeaconChainTypes>(
8+
db: Arc<HotColdDB<T::EthSpec, T::HotStore, T::ColdStore>>,
9+
log: Logger,
10+
) -> Result<Vec<KeyValueStoreOp>, Error> {
11+
let v17 = db
12+
.get_item::<PersistedForkChoiceV17>(&FORK_CHOICE_DB_KEY)?
13+
.ok_or_else(|| Error::SchemaMigrationError("fork choice missing from database".into()))?;
14+
15+
let v20: PersistedForkChoiceV20 = v17.into();
16+
17+
debug!(log, "Adding anchor_state to fork choice");
18+
19+
Ok(vec![v20.as_kv_store_op(FORK_CHOICE_DB_KEY)])
20+
}
21+
22+
pub fn downgrade_from_v20<T: BeaconChainTypes>(
23+
db: Arc<HotColdDB<T::EthSpec, T::HotStore, T::ColdStore>>,
24+
log: Logger,
25+
) -> Result<Vec<KeyValueStoreOp>, Error> {
26+
let v20 = db
27+
.get_item::<PersistedForkChoiceV20>(&FORK_CHOICE_DB_KEY)?
28+
.ok_or_else(|| Error::SchemaMigrationError("fork choice missing from database".into()))?;
29+
30+
let v17: PersistedForkChoiceV17 = v20.into();
31+
32+
debug!(log, "Dropping anchor_state from fork choice.");
33+
34+
Ok(vec![v17.as_kv_store_op(FORK_CHOICE_DB_KEY)])
35+
}

beacon_node/store/src/metadata.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ use ssz::{Decode, Encode};
44
use ssz_derive::{Decode, Encode};
55
use types::{Checkpoint, Hash256, Slot};
66

7-
pub const CURRENT_SCHEMA_VERSION: SchemaVersion = SchemaVersion(19);
7+
pub const CURRENT_SCHEMA_VERSION: SchemaVersion = SchemaVersion(20);
88

99
// All the keys that get stored under the `BeaconMeta` column.
1010
//

consensus/fork_choice/src/fork_choice.rs

Lines changed: 34 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ use std::cmp::Ordering;
1717
use std::collections::BTreeSet;
1818
use std::marker::PhantomData;
1919
use std::time::Duration;
20+
use types::superstruct;
2021
use types::{
2122
consts::merge::INTERVALS_PER_SLOT, AbstractExecPayload, AttestationShufflingId,
2223
AttesterSlashing, BeaconBlockRef, BeaconState, BeaconStateError, ChainSpec, Checkpoint, Epoch,
@@ -284,7 +285,8 @@ fn dequeue_attestations(
284285
/// latter case only occurs when we weak subjectivity sync against a state which is not finalized.
285286
/// This could be necessary in extreme circumstances where finalization extends past the data
286287
/// availabilty boundary post-deneb.
287-
#[derive(Clone, Copy, Debug, PartialEq)]
288+
#[derive(Clone, Copy, Debug, PartialEq, Encode, Decode)]
289+
#[ssz(enum_behaviour = "tag")]
288290
pub enum AnchorState {
289291
/// The finalized checkpoint is truly finalized
290292
Finalized,
@@ -1552,8 +1554,7 @@ where
15521554
// Will be updated in the following call to `Self::get_head`.
15531555
head_root: Hash256::zero(),
15541556
},
1555-
// FIXME: actually persist / load anchor state!!!
1556-
anchor_state: AnchorState::Finalized,
1557+
anchor_state: persisted.anchor_state,
15571558
_phantom: PhantomData,
15581559
};
15591560

@@ -1587,6 +1588,7 @@ where
15871588
PersistedForkChoice {
15881589
proto_array_bytes: self.proto_array().as_bytes(),
15891590
queued_attestations: self.queued_attestations().to_vec(),
1591+
anchor_state: self.anchor_state,
15901592
}
15911593
}
15921594
}
@@ -1680,10 +1682,38 @@ where
16801682
/// Helper struct that is used to encode/decode the state of the `ForkChoice` as SSZ bytes.
16811683
///
16821684
/// This is used when persisting the state of the fork choice to disk.
1683-
#[derive(Encode, Decode, Clone)]
1685+
1686+
pub type PersistedForkChoice = PersistedForkChoiceV20;
1687+
1688+
#[superstruct(
1689+
variants(V19, V20),
1690+
variant_attributes(derive(Encode, Decode, Clone)),
1691+
no_enum
1692+
)]
16841693
pub struct PersistedForkChoice {
16851694
pub proto_array_bytes: Vec<u8>,
16861695
queued_attestations: Vec<QueuedAttestation>,
1696+
#[superstruct(only(V20))]
1697+
pub anchor_state: AnchorState,
1698+
}
1699+
1700+
impl Into<PersistedForkChoiceV20> for PersistedForkChoiceV19 {
1701+
fn into(self) -> PersistedForkChoiceV20 {
1702+
PersistedForkChoiceV20 {
1703+
proto_array_bytes: self.proto_array_bytes,
1704+
queued_attestations: self.queued_attestations,
1705+
anchor_state: AnchorState::Finalized,
1706+
}
1707+
}
1708+
}
1709+
1710+
impl Into<PersistedForkChoiceV19> for PersistedForkChoiceV20 {
1711+
fn into(self) -> PersistedForkChoiceV19 {
1712+
PersistedForkChoiceV19 {
1713+
proto_array_bytes: self.proto_array_bytes,
1714+
queued_attestations: self.queued_attestations,
1715+
}
1716+
}
16871717
}
16881718

16891719
#[cfg(test)]

consensus/fork_choice/src/lib.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,8 @@ mod fork_choice_store;
44
pub use crate::fork_choice::{
55
AnchorState, AttestationFromBlock, Error, ForkChoice, ForkChoiceView,
66
ForkchoiceUpdateParameters, InvalidAttestation, InvalidBlock, PayloadVerificationStatus,
7-
PersistedForkChoice, QueuedAttestation, ResetPayloadStatuses,
7+
PersistedForkChoice, PersistedForkChoiceV19, PersistedForkChoiceV20, QueuedAttestation,
8+
ResetPayloadStatuses,
89
};
910
pub use fork_choice_store::ForkChoiceStore;
1011
pub use proto_array::{

0 commit comments

Comments
 (0)