Skip to content

Commit a5aefdb

Browse files
fix: migration for nodes by farm id storage (#483)
1 parent ee66730 commit a5aefdb

File tree

4 files changed

+86
-2
lines changed

4 files changed

+86
-2
lines changed

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@ pub mod types;
3333

3434
pub mod farm;
3535
pub mod interface;
36+
pub mod nodes_migration;
3637
pub mod pub_config;
3738
pub mod pub_ip;
3839
pub mod twin;
Lines changed: 82 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,82 @@
1+
use super::Config;
2+
use super::*;
3+
use frame_support::{traits::Get, weights::Weight};
4+
use log::info;
5+
use sp_std::collections::btree_map::BTreeMap;
6+
7+
pub mod v9patch {
8+
use super::*;
9+
use crate::Config;
10+
11+
use frame_support::{pallet_prelude::Weight, traits::OnRuntimeUpgrade};
12+
use sp_std::marker::PhantomData;
13+
pub struct FixFarmNodeIndexMap<T: Config>(PhantomData<T>);
14+
15+
impl<T: Config> OnRuntimeUpgrade for FixFarmNodeIndexMap<T> {
16+
#[cfg(feature = "try-runtime")]
17+
fn pre_upgrade() -> Result<(), &'static str> {
18+
assert!(PalletVersion::<T>::get() == types::StorageVersion::V9Struct);
19+
20+
info!("👥 TFGrid pallet to V10 passes PRE migrate checks ✅",);
21+
Ok(())
22+
}
23+
24+
fn on_runtime_upgrade() -> Weight {
25+
if PalletVersion::<T>::get() == types::StorageVersion::V9Struct {
26+
add_farm_nodes_index::<T>()
27+
} else {
28+
info!(" >>> Unused migration");
29+
return 0;
30+
}
31+
}
32+
33+
#[cfg(feature = "try-runtime")]
34+
fn post_upgrade() -> Result<(), &'static str> {
35+
assert!(PalletVersion::<T>::get() == types::StorageVersion::V10Struct);
36+
37+
info!(
38+
"👥 TFGrid pallet migration to {:?} passes POST migrate checks ✅",
39+
Pallet::<T>::pallet_version()
40+
);
41+
42+
Ok(())
43+
}
44+
}
45+
}
46+
47+
pub fn add_farm_nodes_index<T: Config>() -> frame_support::weights::Weight {
48+
info!(" >>> Migrating nodes storage...");
49+
50+
NodesByFarmID::<T>::remove_all(None);
51+
52+
let mut reads = 0;
53+
let mut writes = 0;
54+
55+
let mut farms_with_nodes: BTreeMap<u32, Vec<u32>> = BTreeMap::new();
56+
for (_, node) in Nodes::<T>::iter() {
57+
// Add index of farm - list (nodes)
58+
farms_with_nodes
59+
.entry(node.farm_id)
60+
.or_insert(vec![])
61+
.push(node.id);
62+
63+
reads += 1;
64+
}
65+
66+
for (farm_id, nodes) in farms_with_nodes.iter() {
67+
info!(
68+
"inserting nodes: {:?} with farm id: {:?}",
69+
nodes.clone(),
70+
farm_id
71+
);
72+
NodesByFarmID::<T>::insert(farm_id, nodes);
73+
writes += 1;
74+
}
75+
76+
// Update pallet storage version
77+
PalletVersion::<T>::set(types::StorageVersion::V10Struct);
78+
info!(" <<< Storage version upgraded");
79+
80+
// Return the weight consumed by the migration.
81+
T::DbWeight::get().reads_writes(reads as Weight, writes as Weight)
82+
}

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

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,11 +16,12 @@ pub enum StorageVersion {
1616
V7Struct,
1717
V8Struct,
1818
V9Struct,
19+
V10Struct,
1920
}
2021

2122
impl Default for StorageVersion {
2223
fn default() -> StorageVersion {
23-
StorageVersion::V5Struct
24+
StorageVersion::V9Struct
2425
}
2526
}
2627

substrate-node/runtime/src/lib.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -754,7 +754,7 @@ pub type Executive = frame_executive::Executive<
754754
frame_system::ChainContext<Runtime>,
755755
Runtime,
756756
AllPalletsWithSystem,
757-
(),
757+
pallet_tfgrid::nodes_migration::v9patch::FixFarmNodeIndexMap<Runtime>,
758758
>;
759759

760760
impl_runtime_apis! {

0 commit comments

Comments
 (0)