Skip to content

Commit

Permalink
fix: fix, rename and activate migration
Browse files Browse the repository at this point in the history
  • Loading branch information
dandanlen committed Nov 27, 2023
1 parent e97010e commit 0639783
Show file tree
Hide file tree
Showing 5 changed files with 56 additions and 73 deletions.
2 changes: 1 addition & 1 deletion state-chain/pallets/cf-broadcast/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@ pub enum PalletOffence {
FailedToBroadcastTransaction,
}

pub const PALLET_VERSION: StorageVersion = StorageVersion::new(1);
pub const PALLET_VERSION: StorageVersion = StorageVersion::new(2);

#[frame_support::pallet]
pub mod pallet {
Expand Down
5 changes: 2 additions & 3 deletions state-chain/pallets/cf-broadcast/src/migrations.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
pub mod add_initiated_at;
pub mod v1;
pub mod v2;

use cf_runtime_upgrade_utilities::VersionedMigration;

pub type PalletMigration<T, I> =
(VersionedMigration<crate::Pallet<T, I>, add_initiated_at::Migration<T, I>, 0, 1>,);
(VersionedMigration<crate::Pallet<T, I>, v2::Migration<T, I>, 1, 2>,);

This file was deleted.

16 changes: 0 additions & 16 deletions state-chain/pallets/cf-broadcast/src/migrations/v1.rs

This file was deleted.

53 changes: 53 additions & 0 deletions state-chain/pallets/cf-broadcast/src/migrations/v2.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
use crate::*;
use frame_support::{
migration,
pallet_prelude::Weight,
traits::{OnRuntimeUpgrade, PalletInfoAccess},
StoragePrefixedMap,
};
use sp_std::marker::PhantomData;

mod old {
use super::*;
use frame_support::{pallet_prelude::OptionQuery, Twox64Concat};

#[frame_support::storage_alias]
pub type RequestCallbacks<T: Config<I>, I: 'static> = StorageMap<
Pallet<T, I>,
Twox64Concat,
BroadcastId,
<T as Config<I>>::BroadcastCallable,
OptionQuery,
>;
}

pub struct Migration<T: Config<I>, I: 'static>(PhantomData<(T, I)>);

impl<T: Config<I>, I: 'static> OnRuntimeUpgrade for Migration<T, I> {
fn on_runtime_upgrade() -> frame_support::weights::Weight {
migration::move_prefix(
&frame_support::storage::storage_prefix(
Pallet::<T, I>::name().as_bytes(),
b"RequestCallbacks",
)[..],
RequestSuccessCallbacks::<T, I>::storage_prefix(),
);
Weight::zero()
}

#[cfg(feature = "try-runtime")]
fn pre_upgrade() -> Result<Vec<u8>, frame_support::sp_runtime::TryRuntimeError> {
let count = old::RequestCallbacks::<T, I>::iter().count() as u32;
Ok(count.encode())
}

#[cfg(feature = "try-runtime")]
fn post_upgrade(state: Vec<u8>) -> Result<(), frame_support::sp_runtime::TryRuntimeError> {
let old_count = u32::decode(&mut &*state).expect("Invalid data passed from pre_upgrade");
frame_support::ensure!(
old_count == RequestSuccessCallbacks::<T, I>::iter().count() as u32,
"Count mismatch"
);
Ok(())
}
}

0 comments on commit 0639783

Please sign in to comment.