11//! Utilities for managing database schema changes.
2- mod migration_schema_v10;
3- mod migration_schema_v11;
42mod migration_schema_v12;
53mod migration_schema_v13;
6- mod migration_schema_v6;
7- mod migration_schema_v7;
8- mod migration_schema_v8;
9- mod migration_schema_v9;
10- mod types;
114
12- use crate :: beacon_chain:: { BeaconChainTypes , ETH1_CACHE_DB_KEY , FORK_CHOICE_DB_KEY } ;
5+ use crate :: beacon_chain:: { BeaconChainTypes , ETH1_CACHE_DB_KEY } ;
136use crate :: eth1_chain:: SszEth1 ;
14- use crate :: persisted_fork_choice:: {
15- PersistedForkChoiceV1 , PersistedForkChoiceV10 , PersistedForkChoiceV11 , PersistedForkChoiceV7 ,
16- PersistedForkChoiceV8 ,
17- } ;
187use crate :: types:: ChainSpec ;
198use slog:: { warn, Logger } ;
209use std:: sync:: Arc ;
@@ -23,6 +12,7 @@ use store::metadata::{SchemaVersion, CURRENT_SCHEMA_VERSION};
2312use store:: { Error as StoreError , StoreItem } ;
2413
2514/// Migrate the database from one schema version to another, applying all requisite mutations.
15+ #[ allow( clippy:: only_used_in_recursion) ] // spec is not used but likely to be used in future
2616pub fn migrate_schema < T : BeaconChainTypes > (
2717 db : Arc < HotColdDB < T :: EthSpec , T :: HotStore , T :: ColdStore > > ,
2818 deposit_contract_deploy_block : u64 ,
@@ -62,156 +52,9 @@ pub fn migrate_schema<T: BeaconChainTypes>(
6252 }
6353
6454 //
65- // Migrations from before SchemaVersion(5 ) are deprecated.
55+ // Migrations from before SchemaVersion(11 ) are deprecated.
6656 //
6757
68- // Migration for adding `execution_status` field to the fork choice store.
69- ( SchemaVersion ( 5 ) , SchemaVersion ( 6 ) ) => {
70- // Database operations to be done atomically
71- let mut ops = vec ! [ ] ;
72-
73- // The top-level `PersistedForkChoice` struct is still V1 but will have its internal
74- // bytes for the fork choice updated to V6.
75- let fork_choice_opt = db. get_item :: < PersistedForkChoiceV1 > ( & FORK_CHOICE_DB_KEY ) ?;
76- if let Some ( mut persisted_fork_choice) = fork_choice_opt {
77- migration_schema_v6:: update_execution_statuses :: < T > ( & mut persisted_fork_choice)
78- . map_err ( StoreError :: SchemaMigrationError ) ?;
79-
80- // Store the converted fork choice store under the same key.
81- ops. push ( persisted_fork_choice. as_kv_store_op ( FORK_CHOICE_DB_KEY ) ) ;
82- }
83-
84- db. store_schema_version_atomically ( to, ops) ?;
85-
86- Ok ( ( ) )
87- }
88- // 1. Add `proposer_boost_root`.
89- // 2. Update `justified_epoch` to `justified_checkpoint` and `finalized_epoch` to
90- // `finalized_checkpoint`.
91- // 3. This migration also includes a potential update to the justified
92- // checkpoint in case the fork choice store's justified checkpoint and finalized checkpoint
93- // combination does not actually exist for any blocks in fork choice. This was possible in
94- // the consensus spec prior to v1.1.6.
95- //
96- // Relevant issues:
97- //
98- // https://github.com/sigp/lighthouse/issues/2741
99- // https://github.com/ethereum/consensus-specs/pull/2727
100- // https://github.com/ethereum/consensus-specs/pull/2730
101- ( SchemaVersion ( 6 ) , SchemaVersion ( 7 ) ) => {
102- // Database operations to be done atomically
103- let mut ops = vec ! [ ] ;
104-
105- let fork_choice_opt = db. get_item :: < PersistedForkChoiceV1 > ( & FORK_CHOICE_DB_KEY ) ?;
106- if let Some ( persisted_fork_choice_v1) = fork_choice_opt {
107- // This migrates the `PersistedForkChoiceStore`, adding the `proposer_boost_root` field.
108- let mut persisted_fork_choice_v7 = persisted_fork_choice_v1. into ( ) ;
109-
110- let result = migration_schema_v7:: update_fork_choice :: < T > (
111- & mut persisted_fork_choice_v7,
112- db. clone ( ) ,
113- ) ;
114-
115- // Fall back to re-initializing fork choice from an anchor state if necessary.
116- if let Err ( e) = result {
117- warn ! ( log, "Unable to migrate to database schema 7, re-initializing fork choice" ; "error" => ?e) ;
118- migration_schema_v7:: update_with_reinitialized_fork_choice :: < T > (
119- & mut persisted_fork_choice_v7,
120- db. clone ( ) ,
121- spec,
122- )
123- . map_err ( StoreError :: SchemaMigrationError ) ?;
124- }
125-
126- // Store the converted fork choice store under the same key.
127- ops. push ( persisted_fork_choice_v7. as_kv_store_op ( FORK_CHOICE_DB_KEY ) ) ;
128- }
129-
130- db. store_schema_version_atomically ( to, ops) ?;
131-
132- Ok ( ( ) )
133- }
134- // Migration to add an `epoch` key to the fork choice's balances cache.
135- ( SchemaVersion ( 7 ) , SchemaVersion ( 8 ) ) => {
136- let mut ops = vec ! [ ] ;
137- let fork_choice_opt = db. get_item :: < PersistedForkChoiceV7 > ( & FORK_CHOICE_DB_KEY ) ?;
138- if let Some ( fork_choice) = fork_choice_opt {
139- let updated_fork_choice =
140- migration_schema_v8:: update_fork_choice :: < T > ( fork_choice, db. clone ( ) ) ?;
141-
142- ops. push ( updated_fork_choice. as_kv_store_op ( FORK_CHOICE_DB_KEY ) ) ;
143- }
144-
145- db. store_schema_version_atomically ( to, ops) ?;
146-
147- Ok ( ( ) )
148- }
149- // Upgrade from v8 to v9 to separate the execution payloads into their own column.
150- ( SchemaVersion ( 8 ) , SchemaVersion ( 9 ) ) => {
151- migration_schema_v9:: upgrade_to_v9 :: < T > ( db. clone ( ) , log) ?;
152- db. store_schema_version ( to)
153- }
154- // Downgrade from v9 to v8 to ignore the separation of execution payloads
155- // NOTE: only works before the Bellatrix fork epoch.
156- ( SchemaVersion ( 9 ) , SchemaVersion ( 8 ) ) => {
157- migration_schema_v9:: downgrade_from_v9 :: < T > ( db. clone ( ) , log) ?;
158- db. store_schema_version ( to)
159- }
160- ( SchemaVersion ( 9 ) , SchemaVersion ( 10 ) ) => {
161- let mut ops = vec ! [ ] ;
162- let fork_choice_opt = db. get_item :: < PersistedForkChoiceV8 > ( & FORK_CHOICE_DB_KEY ) ?;
163- if let Some ( fork_choice) = fork_choice_opt {
164- let updated_fork_choice = migration_schema_v10:: update_fork_choice ( fork_choice) ?;
165-
166- ops. push ( updated_fork_choice. as_kv_store_op ( FORK_CHOICE_DB_KEY ) ) ;
167- }
168-
169- db. store_schema_version_atomically ( to, ops) ?;
170-
171- Ok ( ( ) )
172- }
173- ( SchemaVersion ( 10 ) , SchemaVersion ( 9 ) ) => {
174- let mut ops = vec ! [ ] ;
175- let fork_choice_opt = db. get_item :: < PersistedForkChoiceV10 > ( & FORK_CHOICE_DB_KEY ) ?;
176- if let Some ( fork_choice) = fork_choice_opt {
177- let updated_fork_choice = migration_schema_v10:: downgrade_fork_choice ( fork_choice) ?;
178-
179- ops. push ( updated_fork_choice. as_kv_store_op ( FORK_CHOICE_DB_KEY ) ) ;
180- }
181-
182- db. store_schema_version_atomically ( to, ops) ?;
183-
184- Ok ( ( ) )
185- }
186- // Upgrade from v10 to v11 adding support for equivocating indices to fork choice.
187- ( SchemaVersion ( 10 ) , SchemaVersion ( 11 ) ) => {
188- let mut ops = vec ! [ ] ;
189- let fork_choice_opt = db. get_item :: < PersistedForkChoiceV10 > ( & FORK_CHOICE_DB_KEY ) ?;
190- if let Some ( fork_choice) = fork_choice_opt {
191- let updated_fork_choice = migration_schema_v11:: update_fork_choice ( fork_choice) ;
192-
193- ops. push ( updated_fork_choice. as_kv_store_op ( FORK_CHOICE_DB_KEY ) ) ;
194- }
195-
196- db. store_schema_version_atomically ( to, ops) ?;
197-
198- Ok ( ( ) )
199- }
200- // Downgrade from v11 to v10 removing support for equivocating indices from fork choice.
201- ( SchemaVersion ( 11 ) , SchemaVersion ( 10 ) ) => {
202- let mut ops = vec ! [ ] ;
203- let fork_choice_opt = db. get_item :: < PersistedForkChoiceV11 > ( & FORK_CHOICE_DB_KEY ) ?;
204- if let Some ( fork_choice) = fork_choice_opt {
205- let updated_fork_choice =
206- migration_schema_v11:: downgrade_fork_choice ( fork_choice, log) ;
207-
208- ops. push ( updated_fork_choice. as_kv_store_op ( FORK_CHOICE_DB_KEY ) ) ;
209- }
210-
211- db. store_schema_version_atomically ( to, ops) ?;
212-
213- Ok ( ( ) )
214- }
21558 // Upgrade from v11 to v12 to store richer metadata in the attestation op pool.
21659 ( SchemaVersion ( 11 ) , SchemaVersion ( 12 ) ) => {
21760 let ops = migration_schema_v12:: upgrade_to_v12 :: < T > ( db. clone ( ) , log) ?;
0 commit comments