Skip to content

Commit 17d0d48

Browse files
committed
migrate kappa to default
1 parent fe70e19 commit 17d0d48

File tree

4 files changed

+141
-1
lines changed

4 files changed

+141
-1
lines changed

pallets/subtensor/src/macros/hooks.rs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -153,7 +153,9 @@ mod hooks {
153153
// Cleanup child/parent keys
154154
.saturating_add(migrations::migrate_fix_childkeys::migrate_fix_childkeys::<T>())
155155
// Migrate AutoStakeDestinationColdkeys
156-
.saturating_add(migrations::migrate_auto_stake_destination::migrate_auto_stake_destination::<T>());
156+
.saturating_add(migrations::migrate_auto_stake_destination::migrate_auto_stake_destination::<T>())
157+
// Migrate Kappa to default (0.5)
158+
.saturating_add(migrations::migrate_kappa_map_to_default::migrate_kappa_map_to_default::<T>());
157159
weight
158160
}
159161

Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
use super::*;
2+
use frame_support::{storage::IterableStorageMap, traits::Get, weights::Weight};
3+
use log;
4+
use scale_info::prelude::string::String;
5+
6+
pub fn migrate_kappa_map_to_default<T: Config>() -> Weight {
7+
let mig_name: Vec<u8> = b"kappa_map_to_default".to_vec();
8+
9+
// 1 read for the HasMigrationRun flag
10+
let mut total_weight = T::DbWeight::get().reads(1);
11+
12+
// Run once guard
13+
if HasMigrationRun::<T>::get(&mig_name) {
14+
log::info!(
15+
"Migration '{}' already executed - skipping",
16+
String::from_utf8_lossy(&mig_name)
17+
);
18+
return total_weight;
19+
}
20+
21+
log::info!("Running migration '{}'", String::from_utf8_lossy(&mig_name));
22+
23+
let target: u16 = DefaultKappa::<T>::get();
24+
25+
let mut reads: u64 = 0;
26+
let mut writes: u64 = 0;
27+
let mut visited: u64 = 0;
28+
let mut updated: u64 = 0;
29+
let mut unchanged: u64 = 0;
30+
31+
for (netuid, current) in Kappa::<T>::iter() {
32+
visited += 1;
33+
reads += 1;
34+
35+
if current != target {
36+
Kappa::<T>::insert(netuid, target);
37+
writes += 1;
38+
updated += 1;
39+
} else {
40+
unchanged += 1;
41+
}
42+
}
43+
44+
total_weight = total_weight.saturating_add(T::DbWeight::get().reads_writes(reads, writes));
45+
46+
log::info!(
47+
"Kappa migration summary: visited={visited}, updated={updated}, unchanged={unchanged}, target_default={}",
48+
target
49+
);
50+
51+
HasMigrationRun::<T>::insert(&mig_name, true);
52+
total_weight = total_weight.saturating_add(T::DbWeight::get().writes(1));
53+
54+
log::info!(
55+
"Migration '{}' completed",
56+
String::from_utf8_lossy(&mig_name)
57+
);
58+
59+
total_weight
60+
}

pallets/subtensor/src/migrations/mod.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ pub mod migrate_fix_root_subnet_tao;
2121
pub mod migrate_fix_root_tao_and_alpha_in;
2222
pub mod migrate_identities_v2;
2323
pub mod migrate_init_total_issuance;
24+
pub mod migrate_kappa_map_to_default;
2425
pub mod migrate_network_immunity_period;
2526
pub mod migrate_network_lock_cost_2500;
2627
pub mod migrate_network_lock_reduction_interval;

pallets/subtensor/src/tests/migration.rs

Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2171,3 +2171,80 @@ fn test_migrate_network_lock_cost_2500_sets_price_and_decay() {
21712171
);
21722172
});
21732173
}
2174+
2175+
#[test]
2176+
fn test_migrate_kappa_map_to_default() {
2177+
new_test_ext(1).execute_with(|| {
2178+
// ------------------------------
2179+
// 0. Constants / helpers
2180+
// ------------------------------
2181+
const MIG_NAME: &[u8] = b"kappa_map_to_default";
2182+
2183+
let default: u16 = DefaultKappa::<Test>::get();
2184+
let not_default: u16 = default.wrapping_add(1);
2185+
2186+
// Choose a few netuids to exercise both "changed" and "unchanged" paths
2187+
let n0: u16 = 0;
2188+
let n1: u16 = 1;
2189+
let n2: u16 = 42;
2190+
2191+
// ------------------------------
2192+
// 1. Pre-state: seed non-default & default entries
2193+
// ------------------------------
2194+
Kappa::<Test>::insert(n0, not_default); // will need update
2195+
Kappa::<Test>::insert(n1, default); // already default
2196+
Kappa::<Test>::insert(n2, not_default); // will need update
2197+
2198+
assert_eq!(
2199+
Kappa::<Test>::get(n0),
2200+
not_default,
2201+
"precondition failed: Kappa[n0] should be non-default before migration"
2202+
);
2203+
assert_eq!(
2204+
Kappa::<Test>::get(n1),
2205+
default,
2206+
"precondition failed: Kappa[n1] should be default before migration"
2207+
);
2208+
assert_eq!(
2209+
Kappa::<Test>::get(n2),
2210+
not_default,
2211+
"precondition failed: Kappa[n2] should be non-default before migration"
2212+
);
2213+
2214+
assert!(
2215+
!HasMigrationRun::<Test>::get(MIG_NAME.to_vec()),
2216+
"migration flag should be false before run"
2217+
);
2218+
2219+
// ------------------------------
2220+
// 2. Run migration
2221+
// ------------------------------
2222+
let w =
2223+
crate::migrations::migrate_kappa_map_to_default::migrate_kappa_map_to_default::<Test>();
2224+
assert!(!w.is_zero(), "weight must be non-zero");
2225+
2226+
// ------------------------------
2227+
// 3. Verify results
2228+
// ------------------------------
2229+
assert!(
2230+
HasMigrationRun::<Test>::get(MIG_NAME.to_vec()),
2231+
"migration flag not set"
2232+
);
2233+
2234+
assert_eq!(
2235+
Kappa::<Test>::get(n0),
2236+
default,
2237+
"Kappa[n0] should be reset to the configured default"
2238+
);
2239+
assert_eq!(
2240+
Kappa::<Test>::get(n1),
2241+
default,
2242+
"Kappa[n1] should remain at the configured default"
2243+
);
2244+
assert_eq!(
2245+
Kappa::<Test>::get(n2),
2246+
default,
2247+
"Kappa[n2] should be reset to the configured default"
2248+
);
2249+
});
2250+
}

0 commit comments

Comments
 (0)