Skip to content

Commit ee9473d

Browse files
fix: remove pricing policy id from update farm (#502)
1 parent c13b7e9 commit ee9473d

File tree

6 files changed

+122
-11
lines changed

6 files changed

+122
-11
lines changed
Lines changed: 88 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,88 @@
1+
use super::Config;
2+
use super::*;
3+
use frame_support::{traits::Get, weights::Weight};
4+
use log::{debug, info};
5+
6+
#[cfg(feature = "try-runtime")]
7+
use frame_support::traits::OnRuntimeUpgradeHelpersExt;
8+
#[cfg(feature = "try-runtime")]
9+
use sp_runtime::SaturatedConversion;
10+
11+
pub mod v10 {
12+
use super::*;
13+
use crate::Config;
14+
15+
use frame_support::{pallet_prelude::Weight, traits::OnRuntimeUpgrade};
16+
use sp_std::marker::PhantomData;
17+
pub struct FixFarmingPolicy<T: Config>(PhantomData<T>);
18+
19+
impl<T: Config> OnRuntimeUpgrade for FixFarmingPolicy<T> {
20+
#[cfg(feature = "try-runtime")]
21+
fn pre_upgrade() -> Result<(), &'static str> {
22+
assert!(PalletVersion::<T>::get() <= types::StorageVersion::V10Struct);
23+
24+
// Store number of farms in temp storage
25+
let farms_count: u64 = Farms::<T>::iter_keys().count().saturated_into();
26+
Self::set_temp_storage(farms_count, "pre_farms_count");
27+
log::info!(
28+
"🔎 FixFarmingPolicy pre migration: Number of existing farms {:?}",
29+
farms_count
30+
);
31+
32+
info!("👥 TFGrid pallet to V11 passes PRE migrate checks ✅",);
33+
Ok(())
34+
}
35+
36+
fn on_runtime_upgrade() -> Weight {
37+
if PalletVersion::<T>::get() == types::StorageVersion::V10Struct {
38+
fix_farming_policy_migration_::<T>()
39+
} else {
40+
info!(" >>> Unused migration");
41+
return 0;
42+
}
43+
}
44+
45+
#[cfg(feature = "try-runtime")]
46+
fn post_upgrade() -> Result<(), &'static str> {
47+
assert!(PalletVersion::<T>::get() >= types::StorageVersion::V11Struct);
48+
49+
// Check number of farms against pre-check result
50+
let pre_farms_count = Self::get_temp_storage("pre_farms_count").unwrap_or(0u64);
51+
assert_eq!(
52+
Farms::<T>::iter().count().saturated_into::<u64>(),
53+
pre_farms_count,
54+
"Number of farms migrated does not match"
55+
);
56+
57+
info!(
58+
"👥 TFGrid pallet migration to {:?} passes POST migrate checks ✅",
59+
Pallet::<T>::pallet_version()
60+
);
61+
62+
Ok(())
63+
}
64+
}
65+
}
66+
67+
pub fn fix_farming_policy_migration_<T: Config>() -> frame_support::weights::Weight {
68+
info!(" >>> Migrating farm storage...");
69+
70+
let mut read_writes = 0;
71+
72+
Farms::<T>::translate::<super::FarmInfoOf<T>, _>(|k, f| {
73+
let mut new_farm = f;
74+
75+
new_farm.pricing_policy_id = 1;
76+
debug!("migrated farm: {:?}", k);
77+
78+
read_writes += 1;
79+
Some(new_farm)
80+
});
81+
82+
// Update pallet storage version
83+
PalletVersion::<T>::set(types::StorageVersion::V11Struct);
84+
info!(" <<< Storage version upgraded");
85+
86+
// Return the weight consumed by the migration.
87+
T::DbWeight::get().reads_writes(read_writes as Weight, read_writes as Weight)
88+
}

substrate-node/pallets/pallet-tfgrid/src/lib.rs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@ pub mod weights;
3232
pub mod types;
3333

3434
pub mod farm;
35+
pub mod farm_migration;
3536
pub mod interface;
3637
pub mod nodes_migration;
3738
pub mod pub_config;
@@ -766,7 +767,6 @@ pub mod pallet {
766767
origin: OriginFor<T>,
767768
id: u32,
768769
name: FarmNameInput<T>,
769-
pricing_policy_id: u32,
770770
) -> DispatchResultWithPostInfo {
771771
let address = ensure_signed(origin)?;
772772

@@ -795,7 +795,6 @@ pub mod pallet {
795795
FarmIdByName::<T>::remove(name.clone());
796796

797797
stored_farm.name = new_farm_name;
798-
stored_farm.pricing_policy_id = pricing_policy_id;
799798

800799
Farms::<T>::insert(id, &stored_farm);
801800
FarmIdByName::<T>::insert(name, stored_farm.id);

substrate-node/pallets/pallet-tfgrid/src/nodes_migration.rs

Lines changed: 25 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,15 @@
11
use super::Config;
22
use super::*;
33
use frame_support::{traits::Get, weights::Weight};
4-
use log::info;
4+
use log::{debug, info};
55
use sp_std::collections::btree_map::BTreeMap;
66

7-
pub mod v9patch {
7+
#[cfg(feature = "try-runtime")]
8+
use frame_support::traits::OnRuntimeUpgradeHelpersExt;
9+
#[cfg(feature = "try-runtime")]
10+
use sp_runtime::SaturatedConversion;
11+
12+
pub mod v9 {
813
use super::*;
914
use crate::Config;
1015

@@ -17,6 +22,14 @@ pub mod v9patch {
1722
fn pre_upgrade() -> Result<(), &'static str> {
1823
assert!(PalletVersion::<T>::get() == types::StorageVersion::V9Struct);
1924

25+
// Store number of nodes in temp storage
26+
let nodes_count: u64 = Nodes::<T>::iter_keys().count().saturated_into();
27+
Self::set_temp_storage(nodes_count, "pre_nodes_count");
28+
log::info!(
29+
"🔎 FixFarmingPolicy pre migration: Number of existing nodes {:?}",
30+
nodes_count
31+
);
32+
2033
info!("👥 TFGrid pallet to V10 passes PRE migrate checks ✅",);
2134
Ok(())
2235
}
@@ -32,7 +45,15 @@ pub mod v9patch {
3245

3346
#[cfg(feature = "try-runtime")]
3447
fn post_upgrade() -> Result<(), &'static str> {
35-
assert!(PalletVersion::<T>::get() == types::StorageVersion::V10Struct);
48+
assert!(PalletVersion::<T>::get() >= types::StorageVersion::V10Struct);
49+
50+
// Check number of nodes against pre-check result
51+
let pre_nodes_count = Self::get_temp_storage("pre_nodes_count").unwrap_or(0u64);
52+
assert_eq!(
53+
Nodes::<T>::iter().count().saturated_into::<u64>(),
54+
pre_nodes_count,
55+
"Number of nodes migrated does not match"
56+
);
3657

3758
info!(
3859
"👥 TFGrid pallet migration to {:?} passes POST migrate checks ✅",
@@ -64,7 +85,7 @@ pub fn add_farm_nodes_index<T: Config>() -> frame_support::weights::Weight {
6485
}
6586

6687
for (farm_id, nodes) in farms_with_nodes.iter() {
67-
info!(
88+
debug!(
6889
"inserting nodes: {:?} with farm id: {:?}",
6990
nodes.clone(),
7091
farm_id

substrate-node/pallets/pallet-tfgrid/src/tests.rs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -351,7 +351,6 @@ fn test_update_farm_name_works() {
351351
Origin::signed(bob()),
352352
2,
353353
farm_name.0.clone(),
354-
1
355354
));
356355
});
357356
}
@@ -379,7 +378,7 @@ fn test_update_farm_existing_name_fails() {
379378

380379
let farm_name = get_farm_name(b"alice_farm");
381380
assert_noop!(
382-
TfgridModule::update_farm(Origin::signed(bob()), 2, farm_name.0.clone(), 1),
381+
TfgridModule::update_farm(Origin::signed(bob()), 2, farm_name.0.clone()),
383382
Error::<TestRuntime>::InvalidFarmName
384383
);
385384
});

substrate-node/pallets/pallet-tfgrid/src/types.rs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ use sp_std::vec::Vec;
55
use tfchain_support::types::{FarmCertification, NodeCertification};
66

77
/// Utility type for managing upgrades/migrations.
8-
#[derive(Encode, Decode, Clone, Debug, PartialEq, TypeInfo)]
8+
#[derive(Encode, Decode, Clone, Debug, PartialEq, PartialOrd, TypeInfo)]
99
pub enum StorageVersion {
1010
V1Struct,
1111
V2Struct,
@@ -17,11 +17,12 @@ pub enum StorageVersion {
1717
V8Struct,
1818
V9Struct,
1919
V10Struct,
20+
V11Struct,
2021
}
2122

2223
impl Default for StorageVersion {
2324
fn default() -> StorageVersion {
24-
StorageVersion::V9Struct
25+
StorageVersion::V11Struct
2526
}
2627
}
2728

substrate-node/runtime/src/lib.rs

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -754,7 +754,10 @@ pub type Executive = frame_executive::Executive<
754754
frame_system::ChainContext<Runtime>,
755755
Runtime,
756756
AllPalletsWithSystem,
757-
pallet_tfgrid::nodes_migration::v9patch::FixFarmNodeIndexMap<Runtime>,
757+
(
758+
pallet_tfgrid::nodes_migration::v9::FixFarmNodeIndexMap<Runtime>,
759+
pallet_tfgrid::farm_migration::v10::FixFarmingPolicy<Runtime>,
760+
),
758761
>;
759762

760763
impl_runtime_apis! {

0 commit comments

Comments
 (0)