@@ -63,7 +63,7 @@ use frame_support::{
6363 dispatch:: { DispatchError , DispatchResult , Dispatchable , Parameter } ,
6464 traits:: {
6565 schedule:: { self , DispatchTime , MaybeHashed } ,
66- EnsureOrigin , Get , IsType , OriginTrait , PrivilegeCmp ,
66+ EnsureOrigin , Get , IsType , OriginTrait , PalletInfoAccess , PrivilegeCmp , StorageVersion ,
6767 } ,
6868 weights:: { GetDispatchInfo , Weight } ,
6969} ;
@@ -132,22 +132,6 @@ pub type ScheduledOf<T> = ScheduledV3Of<T>;
132132pub type Scheduled < Call , BlockNumber , PalletsOrigin , AccountId > =
133133 ScheduledV2 < Call , BlockNumber , PalletsOrigin , AccountId > ;
134134
135- // A value placed in storage that represents the current version of the Scheduler storage.
136- // This value is used by the `on_runtime_upgrade` logic to determine whether we run
137- // storage migration logic.
138- #[ derive( Encode , Decode , Clone , Copy , PartialEq , Eq , PartialOrd , Ord , RuntimeDebug , TypeInfo ) ]
139- enum Releases {
140- V1 ,
141- V2 ,
142- V3 ,
143- }
144-
145- impl Default for Releases {
146- fn default ( ) -> Self {
147- Releases :: V1
148- }
149- }
150-
151135#[ cfg( feature = "runtime-benchmarks" ) ]
152136mod preimage_provider {
153137 use frame_support:: traits:: PreimageRecipient ;
@@ -201,8 +185,12 @@ pub mod pallet {
201185 } ;
202186 use frame_system:: pallet_prelude:: * ;
203187
188+ /// The current storage version.
189+ const STORAGE_VERSION : StorageVersion = StorageVersion :: new ( 3 ) ;
190+
204191 #[ pallet:: pallet]
205192 #[ pallet:: generate_store( pub ( super ) trait Store ) ]
193+ #[ pallet:: storage_version( STORAGE_VERSION ) ]
206194 #[ pallet:: without_storage_info]
207195 pub struct Pallet < T > ( _ ) ;
208196
@@ -268,12 +256,6 @@ pub mod pallet {
268256 pub ( crate ) type Lookup < T : Config > =
269257 StorageMap < _ , Twox64Concat , Vec < u8 > , TaskAddress < T :: BlockNumber > > ;
270258
271- /// Storage version of the pallet.
272- ///
273- /// New networks start with last version.
274- #[ pallet:: storage]
275- pub ( crate ) type StorageVersion < T > = StorageValue < _ , Releases , ValueQuery > ;
276-
277259 /// Events type.
278260 #[ pallet:: event]
279261 #[ pallet:: generate_deposit( pub ( super ) fn deposit_event) ]
@@ -308,23 +290,6 @@ pub mod pallet {
308290 RescheduleNoChange ,
309291 }
310292
311- #[ pallet:: genesis_config]
312- pub struct GenesisConfig ;
313-
314- #[ cfg( feature = "std" ) ]
315- impl Default for GenesisConfig {
316- fn default ( ) -> Self {
317- Self
318- }
319- }
320-
321- #[ pallet:: genesis_build]
322- impl < T : Config > GenesisBuild < T > for GenesisConfig {
323- fn build ( & self ) {
324- StorageVersion :: < T > :: put ( Releases :: V3 ) ;
325- }
326- }
327-
328293 #[ pallet:: hooks]
329294 impl < T : Config > Hooks < BlockNumberFor < T > > for Pallet < T > {
330295 /// Execute the scheduled calls
@@ -573,19 +538,19 @@ pub mod pallet {
573538
574539impl < T : Config > Pallet < T > {
575540 /// Migrate storage format from V1 to V3.
576- /// Return true if migration is performed.
577- pub fn migrate_v1_to_v3 ( ) -> bool {
578- if StorageVersion :: < T > :: get ( ) == Releases :: V1 {
579- StorageVersion :: < T > :: put ( Releases :: V3 ) ;
580-
581- Agenda :: < T > :: translate :: <
582- Vec < Option < ScheduledV1 < <T as Config >:: Call , T :: BlockNumber > > > ,
583- _ ,
584- > ( |_, agenda| {
541+ ///
542+ /// Returns the weight consumed by this migration.
543+ pub fn migrate_v1_to_v3 ( ) -> Weight {
544+ let mut weight = T :: DbWeight :: get ( ) . reads_writes ( 1 , 1 ) ;
545+
546+ Agenda :: < T > :: translate :: < Vec < Option < ScheduledV1 < <T as Config >:: Call , T :: BlockNumber > > > , _ > (
547+ |_, agenda| {
585548 Some (
586549 agenda
587550 . into_iter ( )
588551 . map ( |schedule| {
552+ weight. saturating_accrue ( T :: DbWeight :: get ( ) . reads_writes ( 1 , 1 ) ) ;
553+
589554 schedule. map ( |schedule| ScheduledV3 {
590555 maybe_id : schedule. maybe_id ,
591556 priority : schedule. priority ,
@@ -597,56 +562,66 @@ impl<T: Config> Pallet<T> {
597562 } )
598563 . collect :: < Vec < _ > > ( ) ,
599564 )
600- } ) ;
565+ } ,
566+ ) ;
601567
602- true
603- } else {
604- false
605- }
568+ frame_support:: storage:: migration:: remove_storage_prefix (
569+ Self :: name ( ) . as_bytes ( ) ,
570+ b"StorageVersion" ,
571+ & [ ] ,
572+ ) ;
573+
574+ StorageVersion :: new ( 3 ) . put :: < Self > ( ) ;
575+
576+ weight + T :: DbWeight :: get ( ) . writes ( 2 )
606577 }
607578
608579 /// Migrate storage format from V2 to V3.
609- /// Return true if migration is performed.
580+ ///
581+ /// Returns the weight consumed by this migration.
610582 pub fn migrate_v2_to_v3 ( ) -> Weight {
611- if StorageVersion :: < T > :: get ( ) == Releases :: V2 {
612- StorageVersion :: < T > :: put ( Releases :: V3 ) ;
613-
614- let mut weight = T :: DbWeight :: get ( ) . reads_writes ( 1 , 1 ) ;
583+ let mut weight = T :: DbWeight :: get ( ) . reads_writes ( 1 , 1 ) ;
615584
616- Agenda :: < T > :: translate :: < Vec < Option < ScheduledV2Of < T > > > , _ > ( |_, agenda| {
617- Some (
618- agenda
619- . into_iter ( )
620- . map ( |schedule| {
621- weight. saturating_accrue ( T :: DbWeight :: get ( ) . reads_writes ( 1 , 1 ) ) ;
622- schedule. map ( |schedule| ScheduledV3 {
623- maybe_id : schedule. maybe_id ,
624- priority : schedule. priority ,
625- call : schedule. call . into ( ) ,
626- maybe_periodic : schedule. maybe_periodic ,
627- origin : system:: RawOrigin :: Root . into ( ) ,
628- _phantom : Default :: default ( ) ,
629- } )
585+ Agenda :: < T > :: translate :: < Vec < Option < ScheduledV2Of < T > > > , _ > ( |_, agenda| {
586+ Some (
587+ agenda
588+ . into_iter ( )
589+ . map ( |schedule| {
590+ weight. saturating_accrue ( T :: DbWeight :: get ( ) . reads_writes ( 1 , 1 ) ) ;
591+ schedule. map ( |schedule| ScheduledV3 {
592+ maybe_id : schedule. maybe_id ,
593+ priority : schedule. priority ,
594+ call : schedule. call . into ( ) ,
595+ maybe_periodic : schedule. maybe_periodic ,
596+ origin : schedule. origin ,
597+ _phantom : Default :: default ( ) ,
630598 } )
631- . collect :: < Vec < _ > > ( ) ,
632- )
633- } ) ;
599+ } )
600+ . collect :: < Vec < _ > > ( ) ,
601+ )
602+ } ) ;
634603
635- weight
636- } else {
637- 0
638- }
604+ frame_support:: storage:: migration:: remove_storage_prefix (
605+ Self :: name ( ) . as_bytes ( ) ,
606+ b"StorageVersion" ,
607+ & [ ] ,
608+ ) ;
609+
610+ StorageVersion :: new ( 3 ) . put :: < Self > ( ) ;
611+
612+ weight + T :: DbWeight :: get ( ) . writes ( 2 )
639613 }
640614
641615 #[ cfg( feature = "try-runtime" ) ]
642616 pub fn pre_migrate_to_v3 ( ) -> Result < ( ) , & ' static str > {
643- assert ! ( StorageVersion :: <T >:: get( ) < Releases :: V3 ) ;
644617 Ok ( ( ) )
645618 }
646619
647620 #[ cfg( feature = "try-runtime" ) ]
648621 pub fn post_migrate_to_v3 ( ) -> Result < ( ) , & ' static str > {
649- assert ! ( StorageVersion :: <T >:: get( ) == Releases :: V3 ) ;
622+ use frame_support:: dispatch:: GetStorageVersion ;
623+
624+ assert ! ( Self :: current_storage_version( ) == 3 ) ;
650625 for k in Agenda :: < T > :: iter_keys ( ) {
651626 let _ = Agenda :: < T > :: try_get ( k) . map_err ( |( ) | "Invalid item in Agenda" ) ?;
652627 }
0 commit comments