Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: farming policies map ids #591

Merged
merged 2 commits into from
Jan 31, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -3,3 +3,4 @@ pub mod v10;
pub mod v11;
pub mod v12;
pub mod v13;
pub mod v14;
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ impl<T: Config> OnRuntimeUpgrade for FixFarmNodeIndexMap<T> {

let nodes_count: u64 = Nodes::<T>::iter().count() as u64;
log::info!(
"🔎 FixFarmingPolicy pre migration: Number of existing nodes {:?}",
"🔎 FixFarmNodeIndexMap pre migration: Number of existing nodes {:?}",
nodes_count
);

Expand Down
89 changes: 89 additions & 0 deletions substrate-node/pallets/pallet-tfgrid/src/migrations/v14.rs
Original file line number Diff line number Diff line change
@@ -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<T: Config>(PhantomData<T>);

impl<T: Config> OnRuntimeUpgrade for FixFarmingPoliciesMap<T> {
#[cfg(feature = "try-runtime")]
fn pre_upgrade() -> Result<Vec<u8>, &'static str> {
info!("current pallet version: {:?}", PalletVersion::<T>::get());
assert!(PalletVersion::<T>::get() >= types::StorageVersion::V13Struct);

let policies_count: u64 = FarmingPoliciesMap::<T>::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::<T>::get() == types::StorageVersion::V13Struct {
fix_farming_policies_map_migration_::<T>()
} else {
info!(" >>> Unused TFGrid pallet V14 migration");
Weight::zero()
}
}

#[cfg(feature = "try-runtime")]
fn post_upgrade(pre_policies_count: Vec<u8>) -> Result<(), &'static str> {
info!("current pallet version: {:?}", PalletVersion::<T>::get());
assert!(PalletVersion::<T>::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::<T>::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::<T>::pallet_version()
);

Ok(())
}
}

pub fn fix_farming_policies_map_migration_<T: Config>() -> frame_support::weights::Weight {
info!(" >>> Migrating farming policies storage...");

let mut read_writes = 0;

FarmingPoliciesMap::<T>::translate::<FarmingPolicy<T::BlockNumber>, _>(|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::<T>::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)
}
3 changes: 2 additions & 1 deletion substrate-node/pallets/pallet-tfgrid/src/types.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,11 +20,12 @@ pub enum StorageVersion {
V11Struct,
V12Struct,
V13Struct,
V14Struct,
}

impl Default for StorageVersion {
fn default() -> StorageVersion {
StorageVersion::V13Struct
StorageVersion::V14Struct
}
}

Expand Down
1 change: 1 addition & 0 deletions substrate-node/runtime/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -770,6 +770,7 @@ type Migrations = (
pallet_tfgrid::migrations::v11::FixFarmingPolicy<Runtime>,
pallet_tfgrid::migrations::v12::InputValidation<Runtime>,
pallet_tfgrid::migrations::v13::FixPublicIP<Runtime>,
pallet_tfgrid::migrations::v14::FixFarmingPoliciesMap<Runtime>,
);

// follows Substrate's non destructive way of eliminating otherwise required
Expand Down