diff --git a/substrate-node/pallets/pallet-tfgrid/src/migrations/mod.rs b/substrate-node/pallets/pallet-tfgrid/src/migrations/mod.rs index ff3651387..579556993 100644 --- a/substrate-node/pallets/pallet-tfgrid/src/migrations/mod.rs +++ b/substrate-node/pallets/pallet-tfgrid/src/migrations/mod.rs @@ -3,3 +3,4 @@ pub mod v10; pub mod v11; pub mod v12; pub mod v13; +pub mod v14; diff --git a/substrate-node/pallets/pallet-tfgrid/src/migrations/v10.rs b/substrate-node/pallets/pallet-tfgrid/src/migrations/v10.rs index 495caec5b..52ae8077c 100644 --- a/substrate-node/pallets/pallet-tfgrid/src/migrations/v10.rs +++ b/substrate-node/pallets/pallet-tfgrid/src/migrations/v10.rs @@ -18,7 +18,7 @@ impl OnRuntimeUpgrade for FixFarmNodeIndexMap { let nodes_count: u64 = Nodes::::iter().count() as u64; log::info!( - "🔎 FixFarmingPolicy pre migration: Number of existing nodes {:?}", + "🔎 FixFarmNodeIndexMap pre migration: Number of existing nodes {:?}", nodes_count ); diff --git a/substrate-node/pallets/pallet-tfgrid/src/migrations/v14.rs b/substrate-node/pallets/pallet-tfgrid/src/migrations/v14.rs new file mode 100644 index 000000000..02fd8b67c --- /dev/null +++ b/substrate-node/pallets/pallet-tfgrid/src/migrations/v14.rs @@ -0,0 +1,89 @@ +use crate::{types::FarmingPolicy, *}; +use frame_support::{traits::Get, traits::OnRuntimeUpgrade, weights::Weight}; +use log::{debug, info}; +use sp_std::marker::PhantomData; + +#[cfg(feature = "try-runtime")] +use codec::Decode; +#[cfg(feature = "try-runtime")] +use sp_std::vec::Vec; + +pub struct FixFarmingPoliciesMap(PhantomData); + +impl OnRuntimeUpgrade for FixFarmingPoliciesMap { + #[cfg(feature = "try-runtime")] + fn pre_upgrade() -> Result, &'static str> { + info!("current pallet version: {:?}", PalletVersion::::get()); + assert!(PalletVersion::::get() >= types::StorageVersion::V13Struct); + + let policies_count: u64 = FarmingPoliciesMap::::iter().count() as u64; + info!( + "🔎 FixFarmingPoliciesMap pre migration: Number of existing farming policies {:?}", + policies_count + ); + + info!("👥 TFGrid pallet to V14 passes PRE migrate checks ✅",); + Ok(policies_count.encode()) + } + + fn on_runtime_upgrade() -> Weight { + if PalletVersion::::get() == types::StorageVersion::V13Struct { + fix_farming_policies_map_migration_::() + } else { + info!(" >>> Unused TFGrid pallet V14 migration"); + Weight::zero() + } + } + + #[cfg(feature = "try-runtime")] + fn post_upgrade(pre_policies_count: Vec) -> Result<(), &'static str> { + info!("current pallet version: {:?}", PalletVersion::::get()); + assert!(PalletVersion::::get() >= types::StorageVersion::V14Struct); + + // Check number of farming policies against pre-check result + let pre_policies_count: u64 = Decode::decode(&mut pre_policies_count.as_slice()) + .expect("the state parameter should be something that was generated by pre_upgrade"); + assert_eq!( + FarmingPoliciesMap::::iter().count() as u64, + pre_policies_count, + "Number of farming policies migrated does not match" + ); + + info!( + "👥 TFGrid pallet migration to {:?} passes POST migrate checks ✅", + Pallet::::pallet_version() + ); + + Ok(()) + } +} + +pub fn fix_farming_policies_map_migration_() -> frame_support::weights::Weight { + info!(" >>> Migrating farming policies storage..."); + + let mut read_writes = 0; + + FarmingPoliciesMap::::translate::, _>(|k, fp| { + debug!("Farming policy #{:?}: start migrating", k); + debug!(" id was: {:?}", fp.id); + let mut new_farming_policy = fp.clone(); + new_farming_policy.id = k; + debug!(" id is now: {:?}", new_farming_policy.id); + debug!("Farming policy #{:?} succesfully migrated", k); + + read_writes += 1; + Some(new_farming_policy) + }); + + info!( + " <<< Farming policies storage updated! Migrated {} Farming policies ✅", + read_writes + ); + + // Update pallet storage version + PalletVersion::::set(types::StorageVersion::V14Struct); + info!(" <<< Storage version upgraded"); + + // Return the weight consumed by the migration. + T::DbWeight::get().reads_writes(read_writes, read_writes + 1) +} diff --git a/substrate-node/pallets/pallet-tfgrid/src/types.rs b/substrate-node/pallets/pallet-tfgrid/src/types.rs index 4255dba6b..9f06a6bb3 100644 --- a/substrate-node/pallets/pallet-tfgrid/src/types.rs +++ b/substrate-node/pallets/pallet-tfgrid/src/types.rs @@ -20,11 +20,12 @@ pub enum StorageVersion { V11Struct, V12Struct, V13Struct, + V14Struct, } impl Default for StorageVersion { fn default() -> StorageVersion { - StorageVersion::V13Struct + StorageVersion::V14Struct } } diff --git a/substrate-node/runtime/src/lib.rs b/substrate-node/runtime/src/lib.rs index 9c8042315..4210e9f62 100644 --- a/substrate-node/runtime/src/lib.rs +++ b/substrate-node/runtime/src/lib.rs @@ -770,6 +770,7 @@ type Migrations = ( pallet_tfgrid::migrations::v11::FixFarmingPolicy, pallet_tfgrid::migrations::v12::InputValidation, pallet_tfgrid::migrations::v13::FixPublicIP, + pallet_tfgrid::migrations::v14::FixFarmingPoliciesMap, ); // follows Substrate's non destructive way of eliminating otherwise required