1- #[ cfg( feature = "try-runtime" ) ]
2- use frame_support:: ensure;
31use frame_support:: {
42 log, storage_alias,
53 traits:: { Get , OnRuntimeUpgrade , PalletInfoAccess , StorageVersion } ,
64 weights:: Weight ,
75} ;
8- #[ cfg( feature = "try-runtime" ) ]
9- use pallets_support:: ensure_storage_version;
10- use pallets_support:: StorageMigration ;
116use primitives:: CommitteeSeats ;
127#[ cfg( feature = "try-runtime" ) ]
13- use sp_std:: vec:: Vec ;
8+ use {
9+ codec:: { Decode , Encode } ,
10+ frame_support:: ensure,
11+ pallets_support:: ensure_storage_version,
12+ sp_std:: vec:: Vec ,
13+ } ;
1414
1515use crate :: { migrations:: Validators , Config , EraValidators } ;
1616
@@ -31,9 +31,11 @@ type NextEraCommitteeSize = StorageValue<Elections, CommitteeSeats>;
3131/// `CommitteeSeats`.
3232pub struct Migration < T , P > ( sp_std:: marker:: PhantomData < ( T , P ) > ) ;
3333
34- impl < T : Config , P : PalletInfoAccess > StorageMigration for Migration < T , P > {
35- #[ cfg( feature = "try-runtime" ) ]
36- const MIGRATION_STORAGE_PREFIX : & ' static [ u8 ] = b"PALLET_ELECTIONS::V2_TO_V3_MIGRATION" ;
34+ #[ cfg( feature = "try-runtime" ) ]
35+ #[ derive( Decode , Encode ) ]
36+ struct MigrationChecksState {
37+ committee_size : Option < u32 > ,
38+ next_era_committee_size : Option < u32 > ,
3739}
3840
3941impl < T : Config , P : PalletInfoAccess > OnRuntimeUpgrade for Migration < T , P > {
@@ -108,16 +110,17 @@ impl<T: Config, P: PalletInfoAccess> OnRuntimeUpgrade for Migration<T, P> {
108110 ensure_storage_version :: < P > ( 2 ) ?;
109111
110112 let committee_size = CommitteeSize :: get ( ) ;
111- Self :: store_temp ( "committee_size" , committee_size) ;
112-
113113 let next_era_committee_size = NextEraCommitteeSize :: get ( ) ;
114- Self :: store_temp ( "next_era_committee_size" , next_era_committee_size) ;
115114
116- Ok ( Vec :: new ( ) )
115+ Ok ( MigrationChecksState {
116+ committee_size,
117+ next_era_committee_size,
118+ }
119+ . encode ( ) )
117120 }
118121
119122 #[ cfg( feature = "try-runtime" ) ]
120- fn post_upgrade ( _state : Vec < u8 > ) -> Result < ( ) , & ' static str > {
123+ fn post_upgrade ( state : Vec < u8 > ) -> Result < ( ) , & ' static str > {
121124 ensure_storage_version :: < P > ( 3 ) ?;
122125
123126 let new_committee_size = CommitteeSize :: get ( ) . ok_or ( "No `CommitteeSize` in the storage" ) ?;
@@ -129,10 +132,11 @@ impl<T: Config, P: PalletInfoAccess> OnRuntimeUpgrade for Migration<T, P> {
129132 let next_era_reserved_validators = NextEraReservedValidators :: < T > :: get ( )
130133 . ok_or ( "No `NextEraReservedValidators` in the storage" ) ?;
131134
132- let old_committee_size =
133- Self :: read_temp :: < Option < u32 > > ( "committee_size" ) . unwrap_or_default ( ) ;
134- let old_next_era_committee_size =
135- Self :: read_temp :: < Option < u32 > > ( "next_era_committee_size" ) . unwrap_or_default ( ) ;
135+ let MigrationChecksState {
136+ committee_size : old_committee_size,
137+ next_era_committee_size : old_next_era_committee_size,
138+ } = <MigrationChecksState >:: decode ( & mut & * state)
139+ . map_err ( |_| "Failed to decode old state" ) ?;
136140
137141 let currently_reserved = current_era_validators. reserved . len ( ) ;
138142 ensure ! (
@@ -141,7 +145,9 @@ impl<T: Config, P: PalletInfoAccess> OnRuntimeUpgrade for Migration<T, P> {
141145 ) ;
142146 ensure ! (
143147 new_committee_size. non_reserved_seats
144- == old_committee_size. saturating_sub( currently_reserved as u32 ) ,
148+ == old_committee_size
149+ . unwrap_or_default( )
150+ . saturating_sub( currently_reserved as u32 ) ,
145151 "Mismatch between `CurrentEraValidators` and `CommitteeSize`"
146152 ) ;
147153
@@ -152,7 +158,9 @@ impl<T: Config, P: PalletInfoAccess> OnRuntimeUpgrade for Migration<T, P> {
152158 ) ;
153159 ensure ! (
154160 new_next_era_committee_size. non_reserved_seats
155- == old_next_era_committee_size. saturating_sub( next_reserved as u32 ) ,
161+ == old_next_era_committee_size
162+ . unwrap_or_default( )
163+ . saturating_sub( next_reserved as u32 ) ,
156164 "Mismatch between `NextEraReservedValidators` and `NextEraCommitteeSize`"
157165 ) ;
158166
0 commit comments