Skip to content

Commit 890f5b9

Browse files
committed
migrate_subnet_limit_to_default
1 parent f4f5add commit 890f5b9

File tree

4 files changed

+100
-1
lines changed

4 files changed

+100
-1
lines changed

pallets/subtensor/src/macros/hooks.rs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -135,7 +135,9 @@ mod hooks {
135135
// Migrate to fix root counters
136136
.saturating_add(migrations::migrate_fix_root_tao_and_alpha_in::migrate_fix_root_tao_and_alpha_in::<T>())
137137
// Migrate Immunity Period
138-
.saturating_add(migrations::migrate_network_immunity_period::migrate_network_immunity_period::<T>());
138+
.saturating_add(migrations::migrate_network_immunity_period::migrate_network_immunity_period::<T>())
139+
// Migrate Subnet Limit
140+
.saturating_add(migrations::migrate_subnet_limit_to_default::migrate_subnet_limit_to_default::<T>());
139141

140142
weight
141143
}
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
use super::*;
2+
use frame_support::{traits::Get, weights::Weight};
3+
use log;
4+
use scale_info::prelude::string::String;
5+
6+
pub fn migrate_subnet_limit_to_default<T: Config>() -> Weight {
7+
let mig_name: Vec<u8> = b"subnet_limit_to_default".to_vec();
8+
9+
// 1 read: 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+
log::info!("Running migration '{}'", String::from_utf8_lossy(&mig_name));
21+
22+
// Read current and compute target default
23+
let current: u16 = SubnetLimit::<T>::get();
24+
let target: u16 = DefaultSubnetLimit::<T>::get();
25+
26+
if current != target {
27+
total_weight = total_weight.saturating_add(T::DbWeight::get().reads_writes(1, 1));
28+
SubnetLimit::<T>::put(target);
29+
log::info!("SubnetLimit updated: {} -> {}", current, target);
30+
} else {
31+
total_weight = total_weight.saturating_add(T::DbWeight::get().reads(1));
32+
log::info!(
33+
"SubnetLimit already equals default ({}), no update performed.",
34+
target
35+
);
36+
}
37+
38+
// Mark as done
39+
HasMigrationRun::<T>::insert(&mig_name, true);
40+
total_weight = total_weight.saturating_add(T::DbWeight::get().writes(1));
41+
42+
log::info!(
43+
"Migration '{}' completed",
44+
String::from_utf8_lossy(&mig_name)
45+
);
46+
total_weight
47+
}

pallets/subtensor/src/migrations/mod.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@ pub mod migrate_set_registration_enable;
3737
pub mod migrate_set_subtoken_enabled;
3838
pub mod migrate_stake_threshold;
3939
pub mod migrate_subnet_identities_to_v3;
40+
pub mod migrate_subnet_limit_to_default;
4041
pub mod migrate_subnet_symbols;
4142
pub mod migrate_subnet_volume;
4243
pub mod migrate_to_v1_separate_emission;

pallets/subtensor/src/tests/migration.rs

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1252,3 +1252,52 @@ fn test_migrate_crv3_v2_to_timelocked() {
12521252
assert_eq!(round2, round);
12531253
});
12541254
}
1255+
1256+
#[test]
1257+
fn test_migrate_subnet_limit_to_default() {
1258+
new_test_ext(1).execute_with(|| {
1259+
// ------------------------------
1260+
// 0. Constants / helpers
1261+
// ------------------------------
1262+
const MIG_NAME: &[u8] = b"subnet_limit_to_default";
1263+
1264+
// Compute a non-default value safely
1265+
let default: u16 = DefaultSubnetLimit::<Test>::get();
1266+
let not_default: u16 = default.wrapping_add(1);
1267+
1268+
// ------------------------------
1269+
// 1. Pre-state: ensure a non-default value is stored
1270+
// ------------------------------
1271+
SubnetLimit::<Test>::put(not_default);
1272+
assert_eq!(
1273+
SubnetLimit::<Test>::get(),
1274+
not_default,
1275+
"precondition failed: SubnetLimit should be non-default before migration"
1276+
);
1277+
1278+
assert!(
1279+
!HasMigrationRun::<Test>::get(MIG_NAME.to_vec()),
1280+
"migration flag should be false before run"
1281+
);
1282+
1283+
// ------------------------------
1284+
// 2. Run migration
1285+
// ------------------------------
1286+
let w = crate::migrations::migrate_subnet_limit_to_default::migrate_subnet_limit_to_default::<Test>();
1287+
assert!(!w.is_zero(), "weight must be non-zero");
1288+
1289+
// ------------------------------
1290+
// 3. Verify results
1291+
// ------------------------------
1292+
assert!(
1293+
HasMigrationRun::<Test>::get(MIG_NAME.to_vec()),
1294+
"migration flag not set"
1295+
);
1296+
1297+
assert_eq!(
1298+
SubnetLimit::<Test>::get(),
1299+
default,
1300+
"SubnetLimit should be reset to the configured default"
1301+
);
1302+
});
1303+
}

0 commit comments

Comments
 (0)