2020pub type VersionNumber = u32 ;
2121
2222// the current expected version of the storage
23- pub const CURRENT_VERSION : VersionNumber = 1 ;
23+ pub const CURRENT_VERSION : VersionNumber = 2 ;
2424
2525#[ cfg( any( test, feature = "migrate" ) ) ]
2626mod inner {
27- use crate :: { Store , Module , Trait } ;
28- use support:: { StorageLinkedMap , StorageValue } ;
29- use sp_std:: vec:: Vec ;
27+ use crate :: { Store , Module , Trait , ValidatorInfoForEra , SessionInterface } ;
28+ use support:: { StorageLinkedMap , StorageValue , StorageMap , StoragePrefixedMap } ;
29+ use sp_std:: { vec, vec :: Vec } ;
3030 use super :: { CURRENT_VERSION , VersionNumber } ;
3131
3232 // the minimum supported version of the migration logic.
@@ -60,6 +60,42 @@ mod inner {
6060 support:: print ( "Finished migrating Staking storage to v1." ) ;
6161 }
6262
63+ // migrate storage from v1 to v2.
64+ //
65+ // * populate `EraStartSessionIndex` storage with only the current era start
66+ // * populate `ValidatorForEra` storage with information from:
67+ // * `Trait::SessionInterface::validators` for validator set
68+ // * `Stakers` for exposure of validators
69+ // * `Validators` for prefs of validators
70+ // * populate `SlotStakeForEra` storage with information from `SlotStake`
71+ // * remove `CurrentEraStartSessionIndex` storage
72+ // * remove `CurrentElected` storage
73+ // * remove `SlotStake` storage
74+ pub fn from_v1_to_v2 < T : Trait > ( version : & mut VersionNumber ) {
75+ if * version != 1 { return }
76+ * version += 1 ;
77+
78+ let current_era = <Module < T > >:: current_era ( ) ;
79+ let current_validator_infos = T :: SessionInterface :: validators ( ) . into_iter ( )
80+ . map ( |validator| ValidatorInfoForEra {
81+ prefs : <Module < T > >:: validators ( & validator) ,
82+ exposure : <Module < T > as Store >:: Stakers :: get ( & validator) ,
83+ stash : validator,
84+ } )
85+ . collect :: < Vec < _ > > ( ) ;
86+ let current_slot_stake = <Module < T > as Store >:: SlotStake :: get ( ) ;
87+
88+ <Module < T > as Store >:: ValidatorForEra :: insert ( & current_era, current_validator_infos) ;
89+ <Module < T > as Store >:: SlotStakeForEra :: insert ( & current_era, current_slot_stake) ;
90+ <Module < T > as Store >:: EraStartSessionIndex :: put ( vec ! [ current_era] ) ;
91+
92+ <Module < T > as Store >:: Stakers :: remove_all ( ) ;
93+ <Module < T > as Store >:: CurrentElected :: kill ( ) ;
94+ <Module < T > as Store >:: SlotStake :: kill ( ) ;
95+
96+ support:: print ( "Finished migrating Staking storage to v2." ) ;
97+ }
98+
6399 pub ( super ) fn perform_migrations < T : Trait > ( ) {
64100 <Module < T > as Store >:: StorageVersion :: mutate ( |version| {
65101 if * version < MIN_SUPPORTED_VERSION {
@@ -72,6 +108,7 @@ mod inner {
72108 if * version == CURRENT_VERSION { return }
73109
74110 to_v1 :: < T > ( version) ;
111+ from_v1_to_v2 :: < T > ( version) ;
75112 } ) ;
76113 }
77114}
0 commit comments