Fix slashing migration to v10 (#11924)

* Fix slashing migration to v10

* add some logs

* Fix default version

* fmt

* Move doc to struct

Co-authored-by: Wilfried Kopp <wilfried@parity.io>
This commit is contained in:
Kian Paimani
2022-07-28 11:44:02 +01:00
committed by GitHub
parent c831138c0a
commit dc4ca1bb33
2 changed files with 17 additions and 1 deletions
+1 -1
View File
@@ -882,7 +882,7 @@ enum Releases {
impl Default for Releases {
fn default() -> Self {
Releases::V8_0_0
Releases::V10_0_0
}
}
+16
View File
@@ -27,13 +27,29 @@ pub mod v10 {
#[storage_alias]
type EarliestUnappliedSlash<T: Config> = StorageValue<Pallet<T>, EraIndex>;
/// Apply any pending slashes that where queued.
///
/// That means we might slash someone a bit too early, but we will definitely
/// won't forget to slash them. The cap of 512 is somewhat randomly taken to
/// prevent us from iterating over an arbitrary large number of keys `on_runtime_upgrade`.
pub struct MigrateToV10<T>(sp_std::marker::PhantomData<T>);
impl<T: Config> OnRuntimeUpgrade for MigrateToV10<T> {
fn on_runtime_upgrade() -> frame_support::weights::Weight {
if StorageVersion::<T>::get() == Releases::V9_0_0 {
let pending_slashes = <Pallet<T> as Store>::UnappliedSlashes::iter().take(512);
for (era, slashes) in pending_slashes {
for slash in slashes {
// in the old slashing scheme, the slash era was the key at which we read
// from `UnappliedSlashes`.
log!(warn, "prematurely applying a slash ({:?}) for era {:?}", slash, era);
slashing::apply_slash::<T>(slash, era);
}
}
EarliestUnappliedSlash::<T>::kill();
StorageVersion::<T>::put(Releases::V10_0_0);
log!(info, "MigrateToV10 executed successfully");
T::DbWeight::get().reads_writes(1, 1)
} else {
log!(warn, "MigrateToV10 should be removed.");