Skip to content

Commit 7e93f31

Browse files
committed
Migration to fix StakingHotKeys added
1 parent 6304dbe commit 7e93f31

File tree

4 files changed

+96
-1
lines changed

4 files changed

+96
-1
lines changed

pallets/subtensor/src/macros/hooks.rs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -162,7 +162,9 @@ mod hooks {
162162
// Migrate pending emissions
163163
.saturating_add(migrations::migrate_pending_emissions::migrate_pending_emissions::<T>())
164164
// Reset unactive subnets
165-
.saturating_add(migrations::migrate_reset_unactive_sn::migrate_reset_unactive_sn::<T>());
165+
.saturating_add(migrations::migrate_reset_unactive_sn::migrate_reset_unactive_sn::<T>())
166+
// Fix staking hot keys
167+
.saturating_add(migrations::migrate_fix_staking_hot_keys::migrate_fix_staking_hot_keys::<T>());
166168
weight
167169
}
168170

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
use super::*;
2+
use frame_support::{traits::Get, weights::Weight};
3+
use log;
4+
use scale_info::prelude::string::String;
5+
use sp_std::collections::btree_map::BTreeMap;
6+
7+
pub fn migrate_fix_staking_hot_keys<T: Config>() -> Weight {
8+
let migration_name = b"migrate_fix_staking_hot_keys".to_vec();
9+
let mut weight = T::DbWeight::get().reads(1);
10+
11+
// Skip if already executed
12+
if HasMigrationRun::<T>::get(&migration_name) {
13+
log::info!(
14+
target: "runtime",
15+
"Migration '{}' already run - skipping.",
16+
String::from_utf8_lossy(&migration_name)
17+
);
18+
return weight;
19+
}
20+
21+
let mut cache: BTreeMap<T::AccountId, Vec<T::AccountId>> = BTreeMap::new();
22+
let mut storage_reads: u64 = 0;
23+
let mut storage_writes: u64 = 0;
24+
25+
for ((hotkey, coldkey, _netuid), alpha) in Alpha::<T>::iter() {
26+
if alpha == 0 {
27+
continue;
28+
}
29+
30+
let staking_hotkeys = cache.entry(coldkey.clone())
31+
.or_insert_with(|| {
32+
storage_reads = storage_reads.saturating_add(1);
33+
StakingHotkeys::<T>::get(&coldkey)
34+
});
35+
36+
if !staking_hotkeys.contains(&hotkey) {
37+
staking_hotkeys.push(hotkey.clone());
38+
storage_writes = storage_writes.saturating_add(1);
39+
StakingHotkeys::<T>::insert(&coldkey, staking_hotkeys.clone());
40+
}
41+
}
42+
weight = weight.saturating_add(T::DbWeight::get().reads_writes(storage_reads, storage_writes));
43+
44+
// Mark migration done
45+
HasMigrationRun::<T>::insert(&migration_name, true);
46+
weight = weight.saturating_add(T::DbWeight::get().writes(1));
47+
48+
log::info!(
49+
target: "runtime",
50+
"Migration '{}' completed.",
51+
String::from_utf8_lossy(&migration_name)
52+
);
53+
54+
weight
55+
}

pallets/subtensor/src/migrations/mod.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,7 @@ pub mod migrate_to_v1_separate_emission;
5858
pub mod migrate_to_v2_fixed_total_stake;
5959
pub mod migrate_transfer_ownership_to_foundation;
6060
pub mod migrate_upgrade_revealed_commitments;
61+
pub mod migrate_fix_staking_hot_keys;
6162

6263
pub(crate) fn migrate_storage<T: Config>(
6364
migration_name: &'static str,

pallets/subtensor/src/tests/migration.rs

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1138,6 +1138,43 @@ fn test_migrate_rate_limit_keys() {
11381138
});
11391139
}
11401140

1141+
#[test]
1142+
fn test_migrate_fix_staking_hot_keys() {
1143+
new_test_ext(1).execute_with(|| {
1144+
const MIGRATION_NAME: &[u8] = b"migrate_fix_staking_hot_keys";
1145+
1146+
assert!(
1147+
!HasMigrationRun::<Test>::get(MIGRATION_NAME.to_vec()),
1148+
"Migration should not have run yet"
1149+
);
1150+
1151+
// Add some data
1152+
Alpha::<Test>::insert(
1153+
(U256::from(1),
1154+
U256::from(2),
1155+
NetUid::ROOT),
1156+
U64F64::from(1_u64)
1157+
);
1158+
// Run migration
1159+
let weight = migrations::migrate_fix_staking_hot_keys::migrate_fix_staking_hot_keys::<Test>();
1160+
1161+
assert!(
1162+
HasMigrationRun::<Test>::get(MIGRATION_NAME.to_vec()),
1163+
"Migration should be marked as completed"
1164+
);
1165+
1166+
// Check migration has been marked as run
1167+
assert!(HasMigrationRun::<Test>::get(MIGRATION_NAME.to_vec()));
1168+
1169+
// Verify results
1170+
assert_eq!(
1171+
StakingHotkeys::<Test>::get(U256::from(2)),
1172+
vec![U256::from(1)]
1173+
);
1174+
});
1175+
}
1176+
1177+
11411178
#[test]
11421179
fn test_migrate_fix_root_subnet_tao() {
11431180
new_test_ext(1).execute_with(|| {

0 commit comments

Comments
 (0)