From 06397837b9f1c3b848c9b8a3e121b1c4d63b6af7 Mon Sep 17 00:00:00 2001 From: Daniel Date: Mon, 27 Nov 2023 13:37:50 +0100 Subject: [PATCH] fix: fix, rename and activate migration --- state-chain/pallets/cf-broadcast/src/lib.rs | 2 +- .../pallets/cf-broadcast/src/migrations.rs | 5 +- .../src/migrations/add_initiated_at.rs | 53 ------------------- .../pallets/cf-broadcast/src/migrations/v1.rs | 16 ------ .../pallets/cf-broadcast/src/migrations/v2.rs | 53 +++++++++++++++++++ 5 files changed, 56 insertions(+), 73 deletions(-) delete mode 100644 state-chain/pallets/cf-broadcast/src/migrations/add_initiated_at.rs delete mode 100644 state-chain/pallets/cf-broadcast/src/migrations/v1.rs create mode 100644 state-chain/pallets/cf-broadcast/src/migrations/v2.rs diff --git a/state-chain/pallets/cf-broadcast/src/lib.rs b/state-chain/pallets/cf-broadcast/src/lib.rs index 45bffe73f85..a428a923919 100644 --- a/state-chain/pallets/cf-broadcast/src/lib.rs +++ b/state-chain/pallets/cf-broadcast/src/lib.rs @@ -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 { diff --git a/state-chain/pallets/cf-broadcast/src/migrations.rs b/state-chain/pallets/cf-broadcast/src/migrations.rs index ebf1607f2c6..3eaa166e9bf 100644 --- a/state-chain/pallets/cf-broadcast/src/migrations.rs +++ b/state-chain/pallets/cf-broadcast/src/migrations.rs @@ -1,7 +1,6 @@ -pub mod add_initiated_at; -pub mod v1; +pub mod v2; use cf_runtime_upgrade_utilities::VersionedMigration; pub type PalletMigration = - (VersionedMigration, add_initiated_at::Migration, 0, 1>,); + (VersionedMigration, v2::Migration, 1, 2>,); diff --git a/state-chain/pallets/cf-broadcast/src/migrations/add_initiated_at.rs b/state-chain/pallets/cf-broadcast/src/migrations/add_initiated_at.rs deleted file mode 100644 index af2811d5320..00000000000 --- a/state-chain/pallets/cf-broadcast/src/migrations/add_initiated_at.rs +++ /dev/null @@ -1,53 +0,0 @@ -use crate::*; -#[cfg(feature = "try-runtime")] -use frame_support::dispatch::DispatchError; -use frame_support::{traits::OnRuntimeUpgrade, weights::Weight}; -use sp_std::marker::PhantomData; - -mod old { - use frame_support::pallet_prelude::OptionQuery; - - use super::*; - - #[frame_support::storage_alias] - pub type TransactionOutIdToBroadcastId, I: 'static> = - StorageMap, Twox64Concat, TransactionOutIdFor, BroadcastId, OptionQuery>; -} - -pub struct Migration, I: 'static>(PhantomData<(T, I)>); - -impl, I: 'static> OnRuntimeUpgrade for Migration { - fn on_runtime_upgrade() -> frame_support::weights::Weight { - let chain_height = T::ChainTracking::get_block_height(); - - TransactionOutIdToBroadcastId::::translate::(|_id, old| { - Some((old, chain_height)) - }); - - Weight::zero() - } - - #[cfg(feature = "try-runtime")] - fn pre_upgrade() -> Result, DispatchError> { - use frame_support::ensure; - - let chain_height = T::ChainTracking::get_block_height(); - // If it's at 0 something went wrong with the initialisation. Also since initiated_at is the - // last thing being decoded, this acts as a check that the rest of the decoding worked. - ensure!(chain_height > 0u32.into(), "chain_height is 0"); - Ok(chain_height.encode()) - } - - #[cfg(feature = "try-runtime")] - fn post_upgrade(state: Vec) -> Result<(), DispatchError> { - use frame_support::ensure; - - let pre_upgrade_height = ChainBlockNumberFor::::decode(&mut &state[..]) - .map_err(|_| "Failed to decode pre-upgrade state.")?; - - for (_out_id, (_b_id, initiated_at)) in TransactionOutIdToBroadcastId::::iter() { - ensure!(initiated_at >= pre_upgrade_height, "initiated_at is 0"); - } - Ok(()) - } -} diff --git a/state-chain/pallets/cf-broadcast/src/migrations/v1.rs b/state-chain/pallets/cf-broadcast/src/migrations/v1.rs deleted file mode 100644 index 42e61e45467..00000000000 --- a/state-chain/pallets/cf-broadcast/src/migrations/v1.rs +++ /dev/null @@ -1,16 +0,0 @@ -use crate::*; -use frame_support::{migration, pallet_prelude::Weight, traits::OnRuntimeUpgrade}; -use sp_std::marker::PhantomData; - -pub struct Migration, I: 'static>(PhantomData<(T, I)>); - -impl, I: 'static> OnRuntimeUpgrade for Migration { - fn on_runtime_upgrade() -> frame_support::weights::Weight { - migration::move_storage_from_pallet( - Pallet::::storage_metadata().prefix.as_bytes(), - b"RequestCallbacks", - b"RequestSuccessCallbacks", - ); - Weight::zero() - } -} diff --git a/state-chain/pallets/cf-broadcast/src/migrations/v2.rs b/state-chain/pallets/cf-broadcast/src/migrations/v2.rs new file mode 100644 index 00000000000..777a228249d --- /dev/null +++ b/state-chain/pallets/cf-broadcast/src/migrations/v2.rs @@ -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, I: 'static> = StorageMap< + Pallet, + Twox64Concat, + BroadcastId, + >::BroadcastCallable, + OptionQuery, + >; +} + +pub struct Migration, I: 'static>(PhantomData<(T, I)>); + +impl, I: 'static> OnRuntimeUpgrade for Migration { + fn on_runtime_upgrade() -> frame_support::weights::Weight { + migration::move_prefix( + &frame_support::storage::storage_prefix( + Pallet::::name().as_bytes(), + b"RequestCallbacks", + )[..], + RequestSuccessCallbacks::::storage_prefix(), + ); + Weight::zero() + } + + #[cfg(feature = "try-runtime")] + fn pre_upgrade() -> Result, frame_support::sp_runtime::TryRuntimeError> { + let count = old::RequestCallbacks::::iter().count() as u32; + Ok(count.encode()) + } + + #[cfg(feature = "try-runtime")] + fn post_upgrade(state: Vec) -> 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::::iter().count() as u32, + "Count mismatch" + ); + Ok(()) + } +}