Skip to content

Commit fc0f611

Browse files
authored
Merge pull request #2139 from opentensor/kappa-sudo-only
kappa root only
2 parents 974e0e0 + 6ca4b24 commit fc0f611

File tree

6 files changed

+158
-27
lines changed

6 files changed

+158
-27
lines changed

pallets/admin-utils/src/lib.rs

Lines changed: 4 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -543,27 +543,17 @@ pub mod pallet {
543543
/// It is only callable by the root account or subnet owner.
544544
/// The extrinsic will call the Subtensor pallet to set the kappa.
545545
#[pallet::call_index(16)]
546-
#[pallet::weight(Weight::from_parts(26_210_000, 0)
547-
.saturating_add(<T as frame_system::Config>::DbWeight::get().reads(3_u64))
546+
#[pallet::weight(Weight::from_parts(15_390_000, 0)
547+
.saturating_add(<T as frame_system::Config>::DbWeight::get().reads(1_u64))
548548
.saturating_add(<T as frame_system::Config>::DbWeight::get().writes(1_u64)))]
549549
pub fn sudo_set_kappa(origin: OriginFor<T>, netuid: NetUid, kappa: u16) -> DispatchResult {
550-
let maybe_owner = pallet_subtensor::Pallet::<T>::ensure_sn_owner_or_root_with_limits(
551-
origin,
552-
netuid,
553-
&[Hyperparameter::Kappa.into()],
554-
)?;
555-
550+
ensure_root(origin)?;
556551
ensure!(
557552
pallet_subtensor::Pallet::<T>::if_subnet_exist(netuid),
558553
Error::<T>::SubnetDoesNotExist
559554
);
560555
pallet_subtensor::Pallet::<T>::set_kappa(netuid, kappa);
561556
log::debug!("KappaSet( netuid: {netuid:?} kappa: {kappa:?} ) ");
562-
pallet_subtensor::Pallet::<T>::record_owner_rl(
563-
maybe_owner,
564-
netuid,
565-
&[Hyperparameter::Kappa.into()],
566-
);
567557
Ok(())
568558
}
569559

@@ -1218,7 +1208,7 @@ pub mod pallet {
12181208
/// The extrinsic will call the Subtensor pallet to set the minimum delegate take.
12191209
#[pallet::call_index(46)]
12201210
#[pallet::weight((
1221-
Weight::from_parts(7_885_000, 0).saturating_add(T::DbWeight::get().writes(1_u64)),
1211+
Weight::from_parts(7_214_000, 0).saturating_add(T::DbWeight::get().writes(1_u64)),
12221212
DispatchClass::Operational,
12231213
Pays::Yes
12241214
))]

pallets/admin-utils/src/tests/mod.rs

Lines changed: 16 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -2025,7 +2025,7 @@ fn test_freeze_window_blocks_root_and_owner() {
20252025
let owner: U256 = U256::from(9);
20262026
SubnetOwner::<Test>::insert(netuid, owner);
20272027
assert_noop!(
2028-
AdminUtils::sudo_set_kappa(
2028+
AdminUtils::sudo_set_commit_reveal_weights_interval(
20292029
<<Test as Config>::RuntimeOrigin>::signed(owner),
20302030
netuid,
20312031
77
@@ -2112,14 +2112,14 @@ fn test_owner_hyperparam_update_rate_limit_enforced() {
21122112
));
21132113

21142114
// First update succeeds
2115-
assert_ok!(AdminUtils::sudo_set_kappa(
2115+
assert_ok!(AdminUtils::sudo_set_commit_reveal_weights_interval(
21162116
<<Test as Config>::RuntimeOrigin>::signed(owner),
21172117
netuid,
21182118
11
21192119
));
21202120
// Immediate second update fails due to TxRateLimitExceeded
21212121
assert_noop!(
2122-
AdminUtils::sudo_set_kappa(
2122+
AdminUtils::sudo_set_commit_reveal_weights_interval(
21232123
<<Test as Config>::RuntimeOrigin>::signed(owner),
21242124
netuid,
21252125
12
@@ -2130,7 +2130,7 @@ fn test_owner_hyperparam_update_rate_limit_enforced() {
21302130
// Advance less than limit still fails
21312131
run_to_block(SubtensorModule::get_current_block_as_u64() + 1);
21322132
assert_noop!(
2133-
AdminUtils::sudo_set_kappa(
2133+
AdminUtils::sudo_set_commit_reveal_weights_interval(
21342134
<<Test as Config>::RuntimeOrigin>::signed(owner),
21352135
netuid,
21362136
13
@@ -2140,7 +2140,7 @@ fn test_owner_hyperparam_update_rate_limit_enforced() {
21402140

21412141
// Advance one more block to pass the limit; should succeed
21422142
run_to_block(SubtensorModule::get_current_block_as_u64() + 1);
2143-
assert_ok!(AdminUtils::sudo_set_kappa(
2143+
assert_ok!(AdminUtils::sudo_set_commit_reveal_weights_interval(
21442144
<<Test as Config>::RuntimeOrigin>::signed(owner),
21452145
netuid,
21462146
14
@@ -2167,21 +2167,25 @@ fn test_hyperparam_rate_limit_enforced_by_tempo() {
21672167
));
21682168

21692169
// First owner update should succeed
2170-
assert_ok!(AdminUtils::sudo_set_kappa(
2170+
assert_ok!(AdminUtils::sudo_set_commit_reveal_weights_interval(
21712171
<<Test as Config>::RuntimeOrigin>::signed(owner),
21722172
netuid,
21732173
1
21742174
));
21752175

21762176
// Immediate second update should fail due to tempo-based RL
21772177
assert_noop!(
2178-
AdminUtils::sudo_set_kappa(<<Test as Config>::RuntimeOrigin>::signed(owner), netuid, 2),
2178+
AdminUtils::sudo_set_commit_reveal_weights_interval(
2179+
<<Test as Config>::RuntimeOrigin>::signed(owner),
2180+
netuid,
2181+
2
2182+
),
21792183
SubtensorError::<Test>::TxRateLimitExceeded
21802184
);
21812185

21822186
// Advance 2 blocks (2 tempos with tempo=1) then succeed
21832187
run_to_block(SubtensorModule::get_current_block_as_u64() + 2);
2184-
assert_ok!(AdminUtils::sudo_set_kappa(
2188+
assert_ok!(AdminUtils::sudo_set_commit_reveal_weights_interval(
21852189
<<Test as Config>::RuntimeOrigin>::signed(owner),
21862190
netuid,
21872191
3
@@ -2211,15 +2215,15 @@ fn test_owner_hyperparam_rate_limit_independent_per_param() {
22112215
));
22122216

22132217
// First update to kappa should succeed
2214-
assert_ok!(AdminUtils::sudo_set_kappa(
2218+
assert_ok!(AdminUtils::sudo_set_commit_reveal_weights_interval(
22152219
<<Test as Config>::RuntimeOrigin>::signed(owner),
22162220
netuid,
22172221
10
22182222
));
22192223

22202224
// Immediate second update to the SAME param (kappa) should be blocked by RL
22212225
assert_noop!(
2222-
AdminUtils::sudo_set_kappa(
2226+
AdminUtils::sudo_set_commit_reveal_weights_interval(
22232227
<<Test as Config>::RuntimeOrigin>::signed(owner),
22242228
netuid,
22252229
11
@@ -2236,7 +2240,7 @@ fn test_owner_hyperparam_rate_limit_independent_per_param() {
22362240

22372241
// kappa should still be blocked until its own RL window passes
22382242
assert_noop!(
2239-
AdminUtils::sudo_set_kappa(
2243+
AdminUtils::sudo_set_commit_reveal_weights_interval(
22402244
<<Test as Config>::RuntimeOrigin>::signed(owner),
22412245
netuid,
22422246
12
@@ -2254,7 +2258,7 @@ fn test_owner_hyperparam_rate_limit_independent_per_param() {
22542258
run_to_block(SubtensorModule::get_current_block_as_u64() + 2);
22552259

22562260
// Now both hyperparameters can be updated again
2257-
assert_ok!(AdminUtils::sudo_set_kappa(
2261+
assert_ok!(AdminUtils::sudo_set_commit_reveal_weights_interval(
22582262
<<Test as Config>::RuntimeOrigin>::signed(owner),
22592263
netuid,
22602264
13

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: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
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_kappa_map_to_default<T: Config>() -> Weight {
7+
let mig_name: Vec<u8> = b"kappa_map_to_default".to_vec();
8+
let mig_name_str = String::from_utf8_lossy(&mig_name);
9+
10+
// 1 read for the HasMigrationRun flag
11+
let mut total_weight = T::DbWeight::get().reads(1);
12+
13+
// Run once guard
14+
if HasMigrationRun::<T>::get(&mig_name) {
15+
log::info!("Migration '{mig_name_str}' already executed - skipping");
16+
return total_weight;
17+
}
18+
19+
log::info!("Running migration '{mig_name_str}'");
20+
21+
let target: u16 = DefaultKappa::<T>::get();
22+
23+
let mut reads: u64 = 0;
24+
let mut writes: u64 = 0;
25+
let mut visited: u64 = 0;
26+
let mut updated: u64 = 0;
27+
let mut unchanged: u64 = 0;
28+
29+
for (netuid, current) in Kappa::<T>::iter() {
30+
visited = visited.saturating_add(1);
31+
reads = reads.saturating_add(1);
32+
33+
if current != target {
34+
Kappa::<T>::insert(netuid, target);
35+
writes = writes.saturating_add(1);
36+
updated = updated.saturating_add(1);
37+
} else {
38+
unchanged = unchanged.saturating_add(1);
39+
}
40+
}
41+
42+
total_weight = total_weight.saturating_add(T::DbWeight::get().reads_writes(reads, writes));
43+
44+
log::info!(
45+
"Kappa migration summary: visited={visited}, updated={updated}, unchanged={unchanged}, target_default={target}"
46+
);
47+
48+
HasMigrationRun::<T>::insert(&mig_name, true);
49+
total_weight = total_weight.saturating_add(T::DbWeight::get().writes(1));
50+
51+
log::info!("Migration '{mig_name_str}' completed");
52+
53+
total_weight
54+
}

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: 80 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2171,3 +2171,83 @@ 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+
let default: u16 = DefaultKappa::<Test>::get();
2183+
2184+
let not_default: u16 = if default == u16::MAX {
2185+
default.saturating_sub(1)
2186+
} else {
2187+
default.saturating_add(1)
2188+
};
2189+
2190+
// ------------------------------
2191+
// 1. Pre-state: seed using the correct key type (NetUid)
2192+
// ------------------------------
2193+
let n0: NetUid = 0u16.into();
2194+
let n1: NetUid = 1u16.into();
2195+
let n2: NetUid = 42u16.into();
2196+
2197+
Kappa::<Test>::insert(n0, not_default);
2198+
Kappa::<Test>::insert(n1, default);
2199+
Kappa::<Test>::insert(n2, not_default);
2200+
2201+
assert_eq!(
2202+
Kappa::<Test>::get(n0),
2203+
not_default,
2204+
"precondition failed: Kappa[n0] should be non-default before migration"
2205+
);
2206+
assert_eq!(
2207+
Kappa::<Test>::get(n1),
2208+
default,
2209+
"precondition failed: Kappa[n1] should be default before migration"
2210+
);
2211+
assert_eq!(
2212+
Kappa::<Test>::get(n2),
2213+
not_default,
2214+
"precondition failed: Kappa[n2] should be non-default before migration"
2215+
);
2216+
2217+
assert!(
2218+
!HasMigrationRun::<Test>::get(MIG_NAME.to_vec()),
2219+
"migration flag should be false before run"
2220+
);
2221+
2222+
// ------------------------------
2223+
// 2. Run migration
2224+
// ------------------------------
2225+
let w =
2226+
crate::migrations::migrate_kappa_map_to_default::migrate_kappa_map_to_default::<Test>();
2227+
assert!(!w.is_zero(), "weight must be non-zero");
2228+
2229+
// ------------------------------
2230+
// 3. Verify results
2231+
// ------------------------------
2232+
assert!(
2233+
HasMigrationRun::<Test>::get(MIG_NAME.to_vec()),
2234+
"migration flag not set"
2235+
);
2236+
2237+
assert_eq!(
2238+
Kappa::<Test>::get(n0),
2239+
default,
2240+
"Kappa[n0] should be reset to the configured default"
2241+
);
2242+
assert_eq!(
2243+
Kappa::<Test>::get(n1),
2244+
default,
2245+
"Kappa[n1] should remain at the configured default"
2246+
);
2247+
assert_eq!(
2248+
Kappa::<Test>::get(n2),
2249+
default,
2250+
"Kappa[n2] should be reset to the configured default"
2251+
);
2252+
});
2253+
}

0 commit comments

Comments
 (0)