Skip to content

Commit eba6868

Browse files
fix: manage nodeByFarmID map when node is updated (#433)
1 parent fa83733 commit eba6868

File tree

6 files changed

+112
-5
lines changed

6 files changed

+112
-5
lines changed

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

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -112,7 +112,7 @@ pub mod v7 {
112112

113113
#[cfg(feature = "try-runtime")]
114114
fn post_upgrade() -> Result<(), &'static str> {
115-
assert!(PalletVersion::<T>::get() == types::StorageVersion::V7Struct);
115+
assert!(PalletVersion::<T>::get() == types::StorageVersion::V8Struct);
116116

117117
info!(
118118
"👥 TFGrid pallet migration to {:?} passes POST migrate checks ✅",
@@ -210,6 +210,11 @@ pub fn migrate_nodes<T: Config>() -> frame_support::weights::Weight {
210210
);
211211

212212
for (farm_id, nodes) in farms_with_nodes.iter() {
213+
info!(
214+
"inserting nodes: {:?} with farm id: {:?}",
215+
nodes.clone(),
216+
farm_id
217+
);
213218
NodesByFarmID::<T>::insert(farm_id, nodes);
214219
writes += 1;
215220
}
@@ -275,7 +280,7 @@ pub fn migrate_farms<T: Config>() -> frame_support::weights::Weight {
275280
);
276281

277282
// Update pallet storage version
278-
PalletVersion::<T>::set(types::StorageVersion::V7Struct);
283+
PalletVersion::<T>::set(types::StorageVersion::V8Struct);
279284
info!(" <<< Storage version upgraded");
280285

281286
// Return the weight consumed by the migration.

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

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@ pub mod farm;
3535
pub mod grid_migration;
3636
pub mod interface;
3737
pub mod nodes_migration;
38+
pub mod nodes_migration_v2;
3839
pub mod pub_config;
3940
pub mod pub_ip;
4041
pub mod twin;
@@ -1044,6 +1045,18 @@ pub mod pallet {
10441045

10451046
let old_node = Nodes::<T>::get(node_id).ok_or(Error::<T>::NodeNotExists)?;
10461047

1048+
// If the farm ID changed on the node,
1049+
// remove the node from the old map from the farm and insert into the correct one
1050+
if old_node.farm_id != farm_id {
1051+
let mut old_nodes_by_farm = NodesByFarmID::<T>::get(old_node.farm_id);
1052+
old_nodes_by_farm.retain(|&id| id != node_id);
1053+
NodesByFarmID::<T>::insert(farm_id, old_nodes_by_farm);
1054+
1055+
let mut nodes_by_farm = NodesByFarmID::<T>::get(farm_id);
1056+
nodes_by_farm.push(node_id);
1057+
NodesByFarmID::<T>::insert(farm_id, nodes_by_farm);
1058+
};
1059+
10471060
stored_node.farm_id = farm_id;
10481061
stored_node.resources = resources;
10491062
stored_node.location = location;

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

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ pub mod v7patch {
2222
}
2323

2424
fn on_runtime_upgrade() -> Weight {
25-
if PalletVersion::<T>::get() == types::StorageVersion::V6Struct {
25+
if PalletVersion::<T>::get() == types::StorageVersion::V7Struct {
2626
add_farm_nodes_index::<T>()
2727
} else {
2828
info!(" >>> Unused migration");
@@ -32,7 +32,7 @@ pub mod v7patch {
3232

3333
#[cfg(feature = "try-runtime")]
3434
fn post_upgrade() -> Result<(), &'static str> {
35-
assert!(PalletVersion::<T>::get() == types::StorageVersion::V7Struct);
35+
assert!(PalletVersion::<T>::get() == types::StorageVersion::V8Struct);
3636

3737
info!(
3838
"👥 TFGrid pallet migration to {:?} passes POST migrate checks ✅",
@@ -62,12 +62,17 @@ pub fn add_farm_nodes_index<T: Config>() -> frame_support::weights::Weight {
6262
}
6363

6464
for (farm_id, nodes) in farms_with_nodes.iter() {
65+
info!(
66+
"inserting nodes: {:?} with farm id: {:?}",
67+
nodes.clone(),
68+
farm_id
69+
);
6570
NodesByFarmID::<T>::insert(farm_id, nodes);
6671
writes += 1;
6772
}
6873

6974
// Update pallet storage version
70-
PalletVersion::<T>::set(types::StorageVersion::V7Struct);
75+
PalletVersion::<T>::set(types::StorageVersion::V8Struct);
7176
info!(" <<< Storage version upgraded");
7277

7378
// Return the weight consumed by the migration.
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 v8patch {
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::V7Struct);
19+
20+
info!("👥 TFGrid pallet to v4 passes PRE migrate checks ✅",);
21+
Ok(())
22+
}
23+
24+
fn on_runtime_upgrade() -> Weight {
25+
if PalletVersion::<T>::get() == types::StorageVersion::V7Struct {
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::V8Struct);
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::V8Struct);
78+
info!(" <<< Storage version upgraded");
79+
80+
// Return the weight consumed by the migration.
81+
T::DbWeight::get().reads_writes(reads as Weight + 1, writes as Weight + 1)
82+
}

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ pub enum StorageVersion {
1414
V5Struct,
1515
V6Struct,
1616
V7Struct,
17+
V8Struct,
1718
}
1819

1920
impl Default for StorageVersion {

substrate-node/runtime/src/lib.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -759,6 +759,7 @@ pub type Executive = frame_executive::Executive<
759759
pallet_smart_contract::contract_migration::v5::ContractMigrationV5<Runtime>,
760760
pallet_tfgrid::grid_migration::v7::GridMigration<Runtime>,
761761
pallet_tfgrid::nodes_migration::v7patch::NodesMigration<Runtime>,
762+
pallet_tfgrid::nodes_migration_v2::v8patch::FixFarmNodeIndexMap<Runtime>,
762763
),
763764
>;
764765

0 commit comments

Comments
 (0)