Skip to content
This repository was archived by the owner on Nov 15, 2023. It is now read-only.

Commit 4aeb95f

Browse files
committed
pallet-scheduler: Fix migrations V2 to V3 (#10757)
* pallet-scheduler: Fix migrations V2 to V3 V2 already supported origins, so we need to move them over instead of setting it to `Root`. Besides that it also removes the custom `Releases` enum and moves it over to `StorageVersion`. * Fixes * Fixes * 🤦
1 parent 1c2332d commit 4aeb95f

File tree

5 files changed

+61
-90
lines changed

5 files changed

+61
-90
lines changed

bin/node/cli/src/chain_spec.rs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -362,7 +362,6 @@ pub fn testnet_genesis(
362362
assets: Default::default(),
363363
gilt: Default::default(),
364364
transaction_storage: Default::default(),
365-
scheduler: Default::default(),
366365
transaction_payment: Default::default(),
367366
}
368367
}

bin/node/testing/src/genesis.rs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -91,7 +91,6 @@ pub fn config_endowed(code: Option<&[u8]>, extra_endowed: Vec<AccountId>) -> Gen
9191
assets: Default::default(),
9292
gilt: Default::default(),
9393
transaction_storage: Default::default(),
94-
scheduler: Default::default(),
9594
transaction_payment: Default::default(),
9695
}
9796
}

frame/democracy/src/tests.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,7 @@ frame_support::construct_runtime!(
6363
{
6464
System: frame_system::{Pallet, Call, Config, Storage, Event<T>},
6565
Balances: pallet_balances::{Pallet, Call, Storage, Config<T>, Event<T>},
66-
Scheduler: pallet_scheduler::{Pallet, Call, Storage, Config, Event<T>},
66+
Scheduler: pallet_scheduler::{Pallet, Call, Storage, Event<T>},
6767
Democracy: pallet_democracy::{Pallet, Call, Storage, Config<T>, Event<T>},
6868
}
6969
);

frame/scheduler/src/lib.rs

Lines changed: 57 additions & 82 deletions
Original file line numberDiff line numberDiff line change
@@ -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>;
132132
pub 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")]
152136
mod 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

574539
impl<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
}

frame/scheduler/src/tests.rs

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ use super::*;
2121
use crate::mock::{logger, new_test_ext, root, run_to_block, Call, LoggerCall, Scheduler, Test, *};
2222
use frame_support::{
2323
assert_err, assert_noop, assert_ok,
24-
traits::{Contains, OnInitialize, PreimageProvider},
24+
traits::{Contains, GetStorageVersion, OnInitialize, PreimageProvider},
2525
Hashable,
2626
};
2727
use sp_runtime::traits::Hash;
@@ -707,9 +707,7 @@ fn migration_to_v3_works() {
707707
frame_support::migration::put_storage_value(b"Scheduler", b"Agenda", &k, old);
708708
}
709709

710-
assert_eq!(StorageVersion::<Test>::get(), Releases::V1);
711-
712-
assert!(Scheduler::migrate_v1_to_v3());
710+
Scheduler::migrate_v1_to_v3();
713711

714712
assert_eq_uvec!(
715713
Agenda::<Test>::iter().collect::<Vec<_>>(),
@@ -783,7 +781,7 @@ fn migration_to_v3_works() {
783781
]
784782
);
785783

786-
assert_eq!(StorageVersion::<Test>::get(), Releases::V3);
784+
assert_eq!(Scheduler::current_storage_version(), 3);
787785
});
788786
}
789787

0 commit comments

Comments
 (0)