@@ -19,39 +19,42 @@ pub fn upgrade_to_v23<T: BeaconChainTypes>(
1919 _log : Logger ,
2020) -> Result < Vec < KeyValueStoreOp > , Error > {
2121 // Set the head-tracker to empty
22-
2322 let Some ( persisted_beacon_chain_v22) =
2423 db. get_item :: < PersistedBeaconChainV22 > ( & BEACON_CHAIN_DB_KEY ) ?
2524 else {
26- // If there is no persisted beacon chain, ignore the upgrade
27- return Ok ( vec ! [ ] ) ;
25+ return Err ( Error :: MigrationError (
26+ "No persisted beacon chain found in DB. Datadir could be incorrect or DB could be corrupt" . to_string ( )
27+ ) ) ;
2828 } ;
2929
3030 let persisted_beacon_chain = PersistedBeaconChain {
3131 genesis_block_root : persisted_beacon_chain_v22. genesis_block_root ,
3232 } ;
3333
34- db . put_item :: < PersistedBeaconChain > ( & BEACON_CHAIN_DB_KEY , & persisted_beacon_chain ) ? ;
34+ let ops = vec ! [ persisted_beacon_chain . as_kv_store_op ( BEACON_CHAIN_DB_KEY ) ] ;
3535
36- todo ! ( ) ;
36+ Ok ( ops )
3737}
3838
3939pub fn downgrade_from_v23 < T : BeaconChainTypes > (
4040 db : Arc < HotColdDB < T :: EthSpec , T :: HotStore , T :: ColdStore > > ,
4141 log : Logger ,
4242) -> Result < Vec < KeyValueStoreOp > , Error > {
43- // recreate head-tracker from the fork-choice
44-
45- let Some ( persisted_fork_choice) = db. get_item :: < PersistedForkChoice > ( & FORK_CHOICE_DB_KEY ) ?
43+ let Some ( persisted_beacon_chain) = db. get_item :: < PersistedBeaconChain > ( & BEACON_CHAIN_DB_KEY ) ?
4644 else {
47- // Is it possible for the fork-choice to not exist on a downgrade?
48- return Ok ( vec ! [ ] ) ;
45+ // The `PersistedBeaconChain` must exist if fork choice exists.
46+ return Err ( Error :: MigrationError (
47+ "No persisted beacon chain found in DB. Datadir could be incorrect or DB could be corrupt" . to_string ( ) ,
48+ ) ) ;
4949 } ;
5050
51- let Some ( persisted_beacon_chain) = db. get_item :: < PersistedBeaconChain > ( & BEACON_CHAIN_DB_KEY ) ?
51+ // Recreate head-tracker from fork choice.
52+ let Some ( persisted_fork_choice) = db. get_item :: < PersistedForkChoice > ( & FORK_CHOICE_DB_KEY ) ?
5253 else {
53- // TODO: Is it possible for persisted beacon chain to be missing if the fork choice exists?
54- return Ok ( vec ! [ ] ) ;
54+ // Fork choice should exist if the database exists.
55+ return Err ( Error :: MigrationError (
56+ "No fork choice found in DB" . to_string ( ) ,
57+ ) ) ;
5558 } ;
5659
5760 let fc_store =
@@ -62,7 +65,8 @@ pub fn downgrade_from_v23<T: BeaconChainTypes>(
6265 ) )
6366 } ) ?;
6467
65- // TODO: what value to choose here?
68+ // Doesn't matter what policy we use for invalid payloads, as our head calculation just
69+ // considers descent from finalization.
6670 let reset_payload_statuses = ResetPayloadStatuses :: OnlyWithInvalidPayload ;
6771 let fork_choice = ForkChoice :: from_persisted (
6872 persisted_fork_choice. fork_choice ,
@@ -91,9 +95,9 @@ pub fn downgrade_from_v23<T: BeaconChainTypes>(
9195 } ,
9296 } ;
9397
94- db . put_item :: < PersistedBeaconChainV22 > ( & BEACON_CHAIN_DB_KEY , & persisted_beacon_chain_v22 ) ? ;
98+ let ops = vec ! [ persisted_beacon_chain_v22 . as_kv_store_op ( BEACON_CHAIN_DB_KEY ) ] ;
9599
96- todo ! ( ) ;
100+ Ok ( ops )
97101}
98102
99103/// Helper struct that is used to encode/decode the state of the `HeadTracker` as SSZ bytes.
0 commit comments