From 383956c5d9dc66cddd914dbb2d4e70b2496b44bc Mon Sep 17 00:00:00 2001 From: Sam Johnson Date: Thu, 11 Sep 2025 14:27:20 -0400 Subject: [PATCH 1/9] lower hyperparameter rate limit to 2 tempos --- pallets/admin-utils/src/tests/mod.rs | 41 +++++++++++++------- pallets/subtensor/src/tests/ensure.rs | 15 ++++--- pallets/subtensor/src/utils/rate_limiting.rs | 3 ++ 3 files changed, 40 insertions(+), 19 deletions(-) diff --git a/pallets/admin-utils/src/tests/mod.rs b/pallets/admin-utils/src/tests/mod.rs index 25b1b89607..711569726b 100644 --- a/pallets/admin-utils/src/tests/mod.rs +++ b/pallets/admin-utils/src/tests/mod.rs @@ -2097,10 +2097,12 @@ fn test_owner_hyperparam_update_rate_limit_enforced() { let owner: U256 = U256::from(5); SubnetOwner::::insert(netuid, owner); - // Configure owner hyperparam RL to 2 blocks - assert_ok!(AdminUtils::sudo_set_owner_hparam_rate_limit( + // Set tempo to 1 so owner hyperparam RL = 2 tempos = 2 blocks + SubtensorModule::set_tempo(netuid, 1); + // Disable admin freeze window to avoid blocking on small tempo + assert_ok!(AdminUtils::sudo_set_admin_freeze_window( <::RuntimeOrigin>::root(), - 2 + 0 )); // First update succeeds @@ -2140,10 +2142,9 @@ fn test_owner_hyperparam_update_rate_limit_enforced() { }); } -// Verifies that when the owner hyperparameter rate limit is left at its default (0), hyperparameter -// updates are not blocked until a non-zero value is set. +// Verifies that owner hyperparameter rate limit is enforced based on tempo (2 tempos). #[test] -fn test_hyperparam_rate_limit_not_blocking_with_default() { +fn test_hyperparam_rate_limit_enforced_by_tempo() { new_test_ext().execute_with(|| { // Setup subnet and owner let netuid = NetUid::from(42); @@ -2151,23 +2152,37 @@ fn test_hyperparam_rate_limit_not_blocking_with_default() { let owner: U256 = U256::from(77); SubnetOwner::::insert(netuid, owner); - // Read the default (unset) owner hyperparam rate limit - let default_limit = pallet_subtensor::OwnerHyperparamRateLimit::::get(); - - assert_eq!(default_limit, 0); + // Set tempo to 1 so RL = 2 blocks + SubtensorModule::set_tempo(netuid, 1); + // Disable admin freeze window to avoid blocking on small tempo + assert_ok!(AdminUtils::sudo_set_admin_freeze_window( + <::RuntimeOrigin>::root(), + 0 + )); - // First owner update should always succeed + // First owner update should succeed assert_ok!(AdminUtils::sudo_set_kappa( <::RuntimeOrigin>::signed(owner), netuid, 1 )); - // With default == 0, second immediate update should also pass (no rate limiting) + // Immediate second update should fail due to tempo-based RL + assert_noop!( + AdminUtils::sudo_set_kappa( + <::RuntimeOrigin>::signed(owner), + netuid, + 2 + ), + SubtensorError::::TxRateLimitExceeded + ); + + // Advance 2 blocks (2 tempos with tempo=1) then succeed + run_to_block(SubtensorModule::get_current_block_as_u64() + 2); assert_ok!(AdminUtils::sudo_set_kappa( <::RuntimeOrigin>::signed(owner), netuid, - 2 + 3 )); }); } diff --git a/pallets/subtensor/src/tests/ensure.rs b/pallets/subtensor/src/tests/ensure.rs index a8b3843fa3..e7bd70fb5a 100644 --- a/pallets/subtensor/src/tests/ensure.rs +++ b/pallets/subtensor/src/tests/ensure.rs @@ -65,11 +65,11 @@ fn ensure_subnet_owner_or_root_distinguishes_root_and_owner() { fn ensure_root_with_rate_limit_blocks_in_freeze_window() { new_test_ext(1).execute_with(|| { let netuid = NetUid::from(1); - let tempo = 10; + let tempo: u16 = 10; add_network(netuid, 10, 0); // Set freeze window to 3 - let freeze_window = 3; + let freeze_window: u16 = 3; crate::Pallet::::set_admin_freeze_window(freeze_window); run_to_block((tempo - freeze_window + 1).into()); @@ -94,12 +94,12 @@ fn ensure_owner_or_root_with_limits_checks_rl_and_freeze() { SubtokenEnabled::::insert(netuid, true); let owner: U256 = U256::from(5); SubnetOwner::::insert(netuid, owner); - // Set freeze window to 3 + // Set freeze window to 0 initially to avoid blocking when tempo is small let freeze_window = 3; - crate::Pallet::::set_admin_freeze_window(freeze_window); + crate::Pallet::::set_admin_freeze_window(0); - // Set owner RL to 2 blocks - crate::Pallet::::set_owner_hyperparam_rate_limit(2); + // Set tempo to 1 so owner hyperparam RL = 2 blocks + crate::Pallet::::set_tempo(netuid, 1); // Outside freeze window initially; should pass and return Some(owner) let res = crate::Pallet::::ensure_sn_owner_or_root_with_limits( @@ -135,6 +135,9 @@ fn ensure_owner_or_root_with_limits_checks_rl_and_freeze() { // Now advance into the freeze window; ensure blocks // (using loop for clarity, because epoch calculation function uses netuid) + // Restore tempo and configure freeze window for this part + crate::Pallet::::set_tempo(netuid, tempo); + crate::Pallet::::set_admin_freeze_window(freeze_window); let freeze_window = freeze_window as u64; loop { let cur = crate::Pallet::::get_current_block_as_u64(); diff --git a/pallets/subtensor/src/utils/rate_limiting.rs b/pallets/subtensor/src/utils/rate_limiting.rs index e9a8bb7b12..319f759411 100644 --- a/pallets/subtensor/src/utils/rate_limiting.rs +++ b/pallets/subtensor/src/utils/rate_limiting.rs @@ -69,6 +69,9 @@ impl Pallet { match tx_type { TransactionType::SetWeightsVersionKey => (Tempo::::get(netuid) as u64) .saturating_mul(WeightsVersionKeyRateLimit::::get()), + // Owner hyperparameter updates are now rate-limited by 2 tempos on the subnet + TransactionType::OwnerHyperparamUpdate => + (Tempo::::get(netuid) as u64).saturating_mul(2), TransactionType::SetSNOwnerHotkey => DefaultSetSNOwnerHotkeyRateLimit::::get(), _ => Self::get_rate_limit(tx_type), From 8d1dc418782c7e700e85792da0c99b3c1abb3c78 Mon Sep 17 00:00:00 2001 From: Sam Johnson Date: Thu, 11 Sep 2025 16:22:57 -0400 Subject: [PATCH 2/9] allow setting storage based on tempos --- .../neuron.precompile.reveal-weights.test.ts | 4 +- .../neuron.precompile.set-weights.test.ts | 4 +- .../test/staking.precompile.reward.test.ts | 4 +- .../subnet.precompile.hyperparameter.test.ts | 4 +- pallets/admin-utils/src/benchmarking.rs | 4 +- pallets/admin-utils/src/lib.rs | 10 ++--- pallets/admin-utils/src/tests/mod.rs | 8 ++-- pallets/subtensor/src/lib.rs | 18 ++++----- pallets/subtensor/src/macros/events.rs | 4 +- pallets/subtensor/src/macros/hooks.rs | 2 + .../migrate_owner_hparam_rl_to_tempos.rs | 39 +++++++++++++++++++ pallets/subtensor/src/migrations/mod.rs | 1 + pallets/subtensor/src/utils/misc.rs | 6 +-- pallets/subtensor/src/utils/rate_limiting.rs | 9 +++-- 14 files changed, 80 insertions(+), 37 deletions(-) create mode 100644 pallets/subtensor/src/migrations/migrate_owner_hparam_rl_to_tempos.rs diff --git a/evm-tests/test/neuron.precompile.reveal-weights.test.ts b/evm-tests/test/neuron.precompile.reveal-weights.test.ts index 52ddc91967..ac9c598743 100644 --- a/evm-tests/test/neuron.precompile.reveal-weights.test.ts +++ b/evm-tests/test/neuron.precompile.reveal-weights.test.ts @@ -79,8 +79,8 @@ describe("Test neuron precompile reveal weights", () => { const sudoFreezeTx = api.tx.Sudo.sudo({ call: setFreezeWindow.decodedCall }) await waitForTransactionWithRetry(api, sudoFreezeTx, alice) - // Set OwnerHyperparamRateLimit to 0 - const setOwnerRateLimit = api.tx.AdminUtils.sudo_set_owner_hparam_rate_limit({ limit: BigInt(0) }) + // Set OwnerHyperparamTempos to 0 (disable RL) + const setOwnerRateLimit = api.tx.AdminUtils.sudo_set_owner_hparam_tempos({ tempos: 0 }) const sudoOwnerRateTx = api.tx.Sudo.sudo({ call: setOwnerRateLimit.decodedCall }) await waitForTransactionWithRetry(api, sudoOwnerRateTx, alice) } diff --git a/evm-tests/test/neuron.precompile.set-weights.test.ts b/evm-tests/test/neuron.precompile.set-weights.test.ts index 4ecc0b36db..1423a2d045 100644 --- a/evm-tests/test/neuron.precompile.set-weights.test.ts +++ b/evm-tests/test/neuron.precompile.set-weights.test.ts @@ -47,8 +47,8 @@ describe("Test neuron precompile contract, set weights function", () => { const sudoFreezeTx = api.tx.Sudo.sudo({ call: setFreezeWindow.decodedCall }) await waitForTransactionWithRetry(api, sudoFreezeTx, alice) - // Set OwnerHyperparamRateLimit to 0 - const setOwnerRateLimit = api.tx.AdminUtils.sudo_set_owner_hparam_rate_limit({ limit: BigInt(0) }) + // Set OwnerHyperparamTempos to 0 (disable RL) + const setOwnerRateLimit = api.tx.AdminUtils.sudo_set_owner_hparam_tempos({ tempos: 0 }) const sudoOwnerRateTx = api.tx.Sudo.sudo({ call: setOwnerRateLimit.decodedCall }) await waitForTransactionWithRetry(api, sudoOwnerRateTx, alice) } diff --git a/evm-tests/test/staking.precompile.reward.test.ts b/evm-tests/test/staking.precompile.reward.test.ts index 108e0ed88c..93e7010b53 100644 --- a/evm-tests/test/staking.precompile.reward.test.ts +++ b/evm-tests/test/staking.precompile.reward.test.ts @@ -48,8 +48,8 @@ describe("Test neuron precompile reward", () => { const sudoFreezeTx = api.tx.Sudo.sudo({ call: setFreezeWindow.decodedCall }) await waitForTransactionWithRetry(api, sudoFreezeTx, alice) - // Set OwnerHyperparamRateLimit to 0 - const setOwnerRateLimit = api.tx.AdminUtils.sudo_set_owner_hparam_rate_limit({ limit: BigInt(0) }) + // Set OwnerHyperparamTempos to 0 (disable RL) + const setOwnerRateLimit = api.tx.AdminUtils.sudo_set_owner_hparam_tempos({ tempos: 0 }) const sudoOwnerRateTx = api.tx.Sudo.sudo({ call: setOwnerRateLimit.decodedCall }) await waitForTransactionWithRetry(api, sudoOwnerRateTx, alice) } diff --git a/evm-tests/test/subnet.precompile.hyperparameter.test.ts b/evm-tests/test/subnet.precompile.hyperparameter.test.ts index 5d81049d41..2741b018f7 100644 --- a/evm-tests/test/subnet.precompile.hyperparameter.test.ts +++ b/evm-tests/test/subnet.precompile.hyperparameter.test.ts @@ -35,8 +35,8 @@ describe("Test the Subnet precompile contract", () => { const sudoFreezeTx = api.tx.Sudo.sudo({ call: setFreezeWindow.decodedCall }) await waitForTransactionWithRetry(api, sudoFreezeTx, alice) - // Set OwnerHyperparamRateLimit to 0 - const setOwnerRateLimit = api.tx.AdminUtils.sudo_set_owner_hparam_rate_limit({ limit: BigInt(0) }) + // Set OwnerHyperparamTempos to 0 (disable RL) + const setOwnerRateLimit = api.tx.AdminUtils.sudo_set_owner_hparam_tempos({ tempos: 0 }) const sudoOwnerRateTx = api.tx.Sudo.sudo({ call: setOwnerRateLimit.decodedCall }) await waitForTransactionWithRetry(api, sudoOwnerRateTx, alice) } diff --git a/pallets/admin-utils/src/benchmarking.rs b/pallets/admin-utils/src/benchmarking.rs index b8dafc0de2..085ee56b03 100644 --- a/pallets/admin-utils/src/benchmarking.rs +++ b/pallets/admin-utils/src/benchmarking.rs @@ -353,9 +353,9 @@ mod benchmarks { } #[benchmark] - fn sudo_set_owner_hparam_rate_limit() { + fn sudo_set_owner_hparam_tempos() { #[extrinsic_call] - _(RawOrigin::Root, 10u64/*limit*/)/*sudo_set_owner_hparam_rate_limit*/; + _(RawOrigin::Root, 2u16/*tempos*/)/*sudo_set_owner_hparam_tempos*/; } #[benchmark] diff --git a/pallets/admin-utils/src/lib.rs b/pallets/admin-utils/src/lib.rs index 4af202132f..8d20fe3750 100644 --- a/pallets/admin-utils/src/lib.rs +++ b/pallets/admin-utils/src/lib.rs @@ -1833,17 +1833,17 @@ pub mod pallet { Ok(()) } - /// Sets the owner hyperparameter rate limit (in blocks). + /// Sets the owner hyperparameter rate limit in tempos (global multiplier). /// Only callable by root. #[pallet::call_index(75)] #[pallet::weight((0, DispatchClass::Operational, Pays::No))] - pub fn sudo_set_owner_hparam_rate_limit( + pub fn sudo_set_owner_hparam_tempos( origin: OriginFor, - limit: u64, + tempos: u16, ) -> DispatchResult { ensure_root(origin)?; - pallet_subtensor::Pallet::::set_owner_hyperparam_rate_limit(limit); - log::debug!("OwnerHyperparamRateLimitSet( limit: {limit:?} ) "); + pallet_subtensor::Pallet::::set_owner_hyperparam_tempos(tempos); + log::debug!("OwnerHyperparamTemposSet( tempos: {tempos:?} ) "); Ok(()) } diff --git a/pallets/admin-utils/src/tests/mod.rs b/pallets/admin-utils/src/tests/mod.rs index 711569726b..79ea690706 100644 --- a/pallets/admin-utils/src/tests/mod.rs +++ b/pallets/admin-utils/src/tests/mod.rs @@ -1973,19 +1973,19 @@ fn test_sudo_set_admin_freeze_window_and_rate() { )); assert_eq!(pallet_subtensor::AdminFreezeWindow::::get(), 7); - // Owner hyperparam rate limit setter + // Owner hyperparam tempos setter assert_eq!( - AdminUtils::sudo_set_owner_hparam_rate_limit( + AdminUtils::sudo_set_owner_hparam_tempos( <::RuntimeOrigin>::signed(U256::from(1)), 5 ), Err(DispatchError::BadOrigin) ); - assert_ok!(AdminUtils::sudo_set_owner_hparam_rate_limit( + assert_ok!(AdminUtils::sudo_set_owner_hparam_tempos( <::RuntimeOrigin>::root(), 5 )); - assert_eq!(pallet_subtensor::OwnerHyperparamRateLimit::::get(), 5); + assert_eq!(pallet_subtensor::OwnerHyperparamTempos::::get(), 5); }); } diff --git a/pallets/subtensor/src/lib.rs b/pallets/subtensor/src/lib.rs index dd12a9b76b..235e8bee76 100644 --- a/pallets/subtensor/src/lib.rs +++ b/pallets/subtensor/src/lib.rs @@ -860,18 +860,18 @@ pub mod pallet { 50400 } - #[pallet::type_value] - /// Default value for subnet owner hyperparameter update rate limit (in blocks) - pub fn DefaultOwnerHyperparamRateLimit() -> u64 { - 0 - } - #[pallet::type_value] /// Default number of terminal blocks in a tempo during which admin operations are prohibited pub fn DefaultAdminFreezeWindow() -> u16 { 10 } + #[pallet::type_value] + /// Default number of tempos for owner hyperparameter update rate limit + pub fn DefaultOwnerHyperparamTempos() -> u16 { + 2 + } + #[pallet::type_value] /// Default value for ck burn, 18%. pub fn DefaultCKBurn() -> u64 { @@ -888,9 +888,9 @@ pub mod pallet { StorageValue<_, u16, ValueQuery, DefaultAdminFreezeWindow>; #[pallet::storage] - /// Global rate limit (in blocks) for subnet owner hyperparameter updates - pub type OwnerHyperparamRateLimit = - StorageValue<_, u64, ValueQuery, DefaultOwnerHyperparamRateLimit>; + /// Global number of tempos used to rate limit subnet owner hyperparameter updates + pub type OwnerHyperparamTempos = + StorageValue<_, u16, ValueQuery, DefaultOwnerHyperparamTempos>; #[pallet::storage] pub type ColdkeySwapScheduleDuration = diff --git a/pallets/subtensor/src/macros/events.rs b/pallets/subtensor/src/macros/events.rs index a0779cd8b1..2400e3ef8c 100644 --- a/pallets/subtensor/src/macros/events.rs +++ b/pallets/subtensor/src/macros/events.rs @@ -116,8 +116,8 @@ mod events { TxChildKeyTakeRateLimitSet(u64), /// setting the admin freeze window length (last N blocks of tempo) AdminFreezeWindowSet(u16), - /// setting the owner hyperparameter rate limit (in blocks) - OwnerHyperparamRateLimitSet(u64), + /// setting the owner hyperparameter rate limit in tempos + OwnerHyperparamTemposSet(u16), /// minimum childkey take set MinChildKeyTakeSet(u16), /// maximum childkey take set diff --git a/pallets/subtensor/src/macros/hooks.rs b/pallets/subtensor/src/macros/hooks.rs index b43f9422df..d23d5884f2 100644 --- a/pallets/subtensor/src/macros/hooks.rs +++ b/pallets/subtensor/src/macros/hooks.rs @@ -138,6 +138,8 @@ mod hooks { .saturating_add(migrations::migrate_fix_root_tao_and_alpha_in::migrate_fix_root_tao_and_alpha_in::()) // Migrate last block rate limiting storage items .saturating_add(migrations::migrate_rate_limiting_last_blocks::migrate_obsolete_rate_limiting_last_blocks_storage::()) + // Remove deprecated OwnerHyperparamRateLimit storage item + .saturating_add(migrations::migrate_owner_hparam_rl_to_tempos::migrate_owner_hyperparam_rl_to_tempos::()) // Migrate remove network modality .saturating_add(migrations::migrate_remove_network_modality::migrate_remove_network_modality::()); weight diff --git a/pallets/subtensor/src/migrations/migrate_owner_hparam_rl_to_tempos.rs b/pallets/subtensor/src/migrations/migrate_owner_hparam_rl_to_tempos.rs new file mode 100644 index 0000000000..9a5d8b4842 --- /dev/null +++ b/pallets/subtensor/src/migrations/migrate_owner_hparam_rl_to_tempos.rs @@ -0,0 +1,39 @@ +use super::*; +use crate::HasMigrationRun; +use codec::Decode; +use frame_support::weights::Weight; +use sp_io::hashing::twox_128; +use sp_io::storage::{clear, get}; + +/// Remove the deprecated OwnerHyperparamRateLimit storage item. +/// If the old value was 0 (disabled), preserve that by setting OwnerHyperparamTempos to 0. +/// Otherwise, leave the new storage at its default (2 tempos). +pub fn migrate_owner_hyperparam_rl_to_tempos() -> Weight { + let migration_name = b"migrate_owner_hyperparam_rl_to_tempos".to_vec(); + let mut weight = T::DbWeight::get().reads(1); + + if HasMigrationRun::::get(&migration_name) { + log::info!("Migration '{:?}' already executed. Skipping.", migration_name); + return weight; + } + + let pallet_name = twox_128("SubtensorModule".as_bytes()); + let storage_name = twox_128("OwnerHyperparamRateLimit".as_bytes()); + let full_key = [pallet_name, storage_name].concat(); + + if let Some(value_bytes) = get(&full_key) { + if let Ok(old_limit_blocks) = ::decode(&mut &value_bytes[..]) { + if old_limit_blocks == 0u64 { + // Preserve disabled state + Pallet::::set_owner_hyperparam_tempos(0); + } + } + + clear(&full_key); + weight = weight.saturating_add(T::DbWeight::get().writes(1)); + } + + HasMigrationRun::::insert(&migration_name, true); + weight = weight.saturating_add(T::DbWeight::get().writes(1)); + weight +} diff --git a/pallets/subtensor/src/migrations/mod.rs b/pallets/subtensor/src/migrations/mod.rs index b7265cc6d0..b5818add7b 100644 --- a/pallets/subtensor/src/migrations/mod.rs +++ b/pallets/subtensor/src/migrations/mod.rs @@ -23,6 +23,7 @@ pub mod migrate_orphaned_storage_items; pub mod migrate_populate_owned_hotkeys; pub mod migrate_rao; pub mod migrate_rate_limiting_last_blocks; +pub mod migrate_owner_hparam_rl_to_tempos; pub mod migrate_remove_commitments_rate_limit; pub mod migrate_remove_network_modality; pub mod migrate_remove_stake_map; diff --git a/pallets/subtensor/src/utils/misc.rs b/pallets/subtensor/src/utils/misc.rs index 8703b1774b..28e3599368 100644 --- a/pallets/subtensor/src/utils/misc.rs +++ b/pallets/subtensor/src/utils/misc.rs @@ -116,9 +116,9 @@ impl Pallet { Self::deposit_event(Event::AdminFreezeWindowSet(window)); } - pub fn set_owner_hyperparam_rate_limit(limit: u64) { - OwnerHyperparamRateLimit::::set(limit); - Self::deposit_event(Event::OwnerHyperparamRateLimitSet(limit)); + pub fn set_owner_hyperparam_tempos(tempos: u16) { + OwnerHyperparamTempos::::set(tempos); + Self::deposit_event(Event::OwnerHyperparamTemposSet(tempos)); } /// If owner is `Some`, record last-blocks for the provided `TransactionType`s. diff --git a/pallets/subtensor/src/utils/rate_limiting.rs b/pallets/subtensor/src/utils/rate_limiting.rs index 319f759411..9ea504159b 100644 --- a/pallets/subtensor/src/utils/rate_limiting.rs +++ b/pallets/subtensor/src/utils/rate_limiting.rs @@ -56,7 +56,6 @@ impl Pallet { TransactionType::SetChildren => 150, // 30 minutes TransactionType::SetChildkeyTake => TxChildkeyTakeRateLimit::::get(), TransactionType::RegisterNetwork => NetworkRateLimit::::get(), - TransactionType::OwnerHyperparamUpdate => OwnerHyperparamRateLimit::::get(), TransactionType::SubsubnetParameterUpdate => SubsubnetCountSetRateLimit::::get(), TransactionType::Unknown => 0, // Default to no limit for unknown types (no limit) @@ -69,9 +68,11 @@ impl Pallet { match tx_type { TransactionType::SetWeightsVersionKey => (Tempo::::get(netuid) as u64) .saturating_mul(WeightsVersionKeyRateLimit::::get()), - // Owner hyperparameter updates are now rate-limited by 2 tempos on the subnet - TransactionType::OwnerHyperparamUpdate => - (Tempo::::get(netuid) as u64).saturating_mul(2), + // Owner hyperparameter updates are rate-limited by N tempos on the subnet (sudo configurable) + TransactionType::OwnerHyperparamUpdate => { + let tempos = OwnerHyperparamTempos::::get() as u64; + (Tempo::::get(netuid) as u64).saturating_mul(tempos) + } TransactionType::SetSNOwnerHotkey => DefaultSetSNOwnerHotkeyRateLimit::::get(), _ => Self::get_rate_limit(tx_type), From 7d24770d988ec8cd10c81fbacbf9623770c06216 Mon Sep 17 00:00:00 2001 From: Sam Johnson Date: Thu, 11 Sep 2025 17:33:27 -0400 Subject: [PATCH 3/9] make hyperparameter specific --- pallets/admin-utils/src/lib.rs | 102 +++++++++---------- pallets/subtensor/src/tests/ensure.rs | 14 +-- pallets/subtensor/src/utils/rate_limiting.rs | 97 +++++++++++++++++- 3 files changed, 151 insertions(+), 62 deletions(-) diff --git a/pallets/admin-utils/src/lib.rs b/pallets/admin-utils/src/lib.rs index 8d20fe3750..4f2170599c 100644 --- a/pallets/admin-utils/src/lib.rs +++ b/pallets/admin-utils/src/lib.rs @@ -216,14 +216,14 @@ pub mod pallet { let maybe_owner = pallet_subtensor::Pallet::::ensure_sn_owner_or_root_with_limits( origin, netuid, - &[TransactionType::OwnerHyperparamUpdate], + &[TransactionType::OwnerSetServingRateLimit], )?; pallet_subtensor::Pallet::::set_serving_rate_limit(netuid, serving_rate_limit); log::debug!("ServingRateLimitSet( serving_rate_limit: {serving_rate_limit:?} ) "); pallet_subtensor::Pallet::::record_owner_rl( maybe_owner, netuid, - &[TransactionType::OwnerHyperparamUpdate], + &[TransactionType::OwnerSetServingRateLimit], ); Ok(()) } @@ -268,7 +268,7 @@ pub mod pallet { let maybe_owner = pallet_subtensor::Pallet::::ensure_sn_owner_or_root_with_limits( origin, netuid, - &[TransactionType::OwnerHyperparamUpdate], + &[TransactionType::OwnerSetMaxDifficulty], )?; ensure!( @@ -282,7 +282,7 @@ pub mod pallet { pallet_subtensor::Pallet::::record_owner_rl( maybe_owner, netuid, - &[TransactionType::OwnerHyperparamUpdate], + &[TransactionType::OwnerSetMaxDifficulty], ); Ok(()) } @@ -302,10 +302,7 @@ pub mod pallet { let maybe_owner = pallet_subtensor::Pallet::::ensure_sn_owner_or_root_with_limits( origin.clone(), netuid, - &[ - TransactionType::OwnerHyperparamUpdate, - TransactionType::SetWeightsVersionKey, - ], + &[TransactionType::SetWeightsVersionKey], )?; ensure!( @@ -316,10 +313,7 @@ pub mod pallet { pallet_subtensor::Pallet::::record_owner_rl( maybe_owner, netuid, - &[ - TransactionType::OwnerHyperparamUpdate, - TransactionType::SetWeightsVersionKey, - ], + &[TransactionType::SetWeightsVersionKey], ); pallet_subtensor::Pallet::::set_weights_version_key(netuid, weights_version_key); @@ -399,7 +393,7 @@ pub mod pallet { let maybe_owner = pallet_subtensor::Pallet::::ensure_sn_owner_or_root_with_limits( origin, netuid, - &[TransactionType::OwnerHyperparamUpdate], + &[TransactionType::OwnerSetAdjustmentAlpha], )?; ensure!( @@ -410,7 +404,7 @@ pub mod pallet { pallet_subtensor::Pallet::::record_owner_rl( maybe_owner, netuid, - &[TransactionType::OwnerHyperparamUpdate], + &[TransactionType::OwnerSetAdjustmentAlpha], ); log::debug!("AdjustmentAlphaSet( adjustment_alpha: {adjustment_alpha:?} ) "); Ok(()) @@ -431,7 +425,7 @@ pub mod pallet { let maybe_owner = pallet_subtensor::Pallet::::ensure_sn_owner_or_root_with_limits( origin, netuid, - &[TransactionType::OwnerHyperparamUpdate], + &[TransactionType::OwnerSetMaxWeightLimit], )?; ensure!( @@ -442,7 +436,7 @@ pub mod pallet { pallet_subtensor::Pallet::::record_owner_rl( maybe_owner, netuid, - &[TransactionType::OwnerHyperparamUpdate], + &[TransactionType::OwnerSetMaxWeightLimit], ); log::debug!( "MaxWeightLimitSet( netuid: {netuid:?} max_weight_limit: {max_weight_limit:?} ) " @@ -465,7 +459,7 @@ pub mod pallet { let maybe_owner = pallet_subtensor::Pallet::::ensure_sn_owner_or_root_with_limits( origin, netuid, - &[TransactionType::OwnerHyperparamUpdate], + &[TransactionType::OwnerSetImmunityPeriod], )?; ensure!( pallet_subtensor::Pallet::::if_subnet_exist(netuid), @@ -476,7 +470,7 @@ pub mod pallet { pallet_subtensor::Pallet::::record_owner_rl( maybe_owner, netuid, - &[TransactionType::OwnerHyperparamUpdate], + &[TransactionType::OwnerSetImmunityPeriod], ); log::debug!( "ImmunityPeriodSet( netuid: {netuid:?} immunity_period: {immunity_period:?} ) " @@ -499,7 +493,7 @@ pub mod pallet { let maybe_owner = pallet_subtensor::Pallet::::ensure_sn_owner_or_root_with_limits( origin, netuid, - &[TransactionType::OwnerHyperparamUpdate], + &[TransactionType::OwnerSetMinAllowedWeights], )?; ensure!( @@ -513,7 +507,7 @@ pub mod pallet { pallet_subtensor::Pallet::::record_owner_rl( maybe_owner, netuid, - &[TransactionType::OwnerHyperparamUpdate], + &[TransactionType::OwnerSetMinAllowedWeights], ); Ok(()) } @@ -557,7 +551,7 @@ pub mod pallet { let maybe_owner = pallet_subtensor::Pallet::::ensure_sn_owner_or_root_with_limits( origin, netuid, - &[TransactionType::OwnerHyperparamUpdate], + &[TransactionType::OwnerSetKappa], )?; ensure!( @@ -569,7 +563,7 @@ pub mod pallet { pallet_subtensor::Pallet::::record_owner_rl( maybe_owner, netuid, - &[TransactionType::OwnerHyperparamUpdate], + &[TransactionType::OwnerSetKappa], ); Ok(()) } @@ -585,7 +579,7 @@ pub mod pallet { let maybe_owner = pallet_subtensor::Pallet::::ensure_sn_owner_or_root_with_limits( origin, netuid, - &[TransactionType::OwnerHyperparamUpdate], + &[TransactionType::OwnerSetRho], )?; ensure!( @@ -597,7 +591,7 @@ pub mod pallet { pallet_subtensor::Pallet::::record_owner_rl( maybe_owner, netuid, - &[TransactionType::OwnerHyperparamUpdate], + &[TransactionType::OwnerSetRho], ); Ok(()) } @@ -617,7 +611,7 @@ pub mod pallet { let maybe_owner = pallet_subtensor::Pallet::::ensure_sn_owner_or_root_with_limits( origin, netuid, - &[TransactionType::OwnerHyperparamUpdate], + &[TransactionType::OwnerSetActivityCutoff], )?; ensure!( @@ -637,7 +631,7 @@ pub mod pallet { pallet_subtensor::Pallet::::record_owner_rl( maybe_owner, netuid, - &[TransactionType::OwnerHyperparamUpdate], + &[TransactionType::OwnerSetActivityCutoff], ); Ok(()) } @@ -685,7 +679,7 @@ pub mod pallet { let maybe_owner = pallet_subtensor::Pallet::::ensure_sn_owner_or_root_with_limits( origin, netuid, - &[TransactionType::OwnerHyperparamUpdate], + &[TransactionType::OwnerSetPowRegistrationAllowed], )?; pallet_subtensor::Pallet::::set_network_pow_registration_allowed( @@ -698,7 +692,7 @@ pub mod pallet { pallet_subtensor::Pallet::::record_owner_rl( maybe_owner, netuid, - &[TransactionType::OwnerHyperparamUpdate], + &[TransactionType::OwnerSetPowRegistrationAllowed], ); Ok(()) } @@ -746,7 +740,7 @@ pub mod pallet { let maybe_owner = pallet_subtensor::Pallet::::ensure_sn_owner_or_root_with_limits( origin, netuid, - &[TransactionType::OwnerHyperparamUpdate], + &[TransactionType::OwnerSetMinBurn], )?; ensure!( pallet_subtensor::Pallet::::if_subnet_exist(netuid), @@ -766,7 +760,7 @@ pub mod pallet { pallet_subtensor::Pallet::::record_owner_rl( maybe_owner, netuid, - &[TransactionType::OwnerHyperparamUpdate], + &[TransactionType::OwnerSetMinBurn], ); Ok(()) } @@ -786,7 +780,7 @@ pub mod pallet { let maybe_owner = pallet_subtensor::Pallet::::ensure_sn_owner_or_root_with_limits( origin, netuid, - &[TransactionType::OwnerHyperparamUpdate], + &[TransactionType::OwnerSetMaxBurn], )?; ensure!( pallet_subtensor::Pallet::::if_subnet_exist(netuid), @@ -806,7 +800,7 @@ pub mod pallet { pallet_subtensor::Pallet::::record_owner_rl( maybe_owner, netuid, - &[TransactionType::OwnerHyperparamUpdate], + &[TransactionType::OwnerSetMaxBurn], ); Ok(()) } @@ -881,7 +875,7 @@ pub mod pallet { let maybe_owner = pallet_subtensor::Pallet::::ensure_sn_owner_or_root_with_limits( origin, netuid, - &[TransactionType::OwnerHyperparamUpdate], + &[TransactionType::OwnerSetBondsMovingAverage], )?; if maybe_owner.is_some() { ensure!( @@ -901,7 +895,7 @@ pub mod pallet { pallet_subtensor::Pallet::::record_owner_rl( maybe_owner, netuid, - &[TransactionType::OwnerHyperparamUpdate], + &[TransactionType::OwnerSetBondsMovingAverage], ); Ok(()) } @@ -921,7 +915,7 @@ pub mod pallet { let maybe_owner = pallet_subtensor::Pallet::::ensure_sn_owner_or_root_with_limits( origin, netuid, - &[TransactionType::OwnerHyperparamUpdate], + &[TransactionType::OwnerSetBondsPenalty], )?; ensure!( @@ -933,7 +927,7 @@ pub mod pallet { pallet_subtensor::Pallet::::record_owner_rl( maybe_owner, netuid, - &[TransactionType::OwnerHyperparamUpdate], + &[TransactionType::OwnerSetBondsPenalty], ); Ok(()) } @@ -1205,7 +1199,7 @@ pub mod pallet { let maybe_owner = pallet_subtensor::Pallet::::ensure_sn_owner_or_root_with_limits( origin, netuid, - &[TransactionType::OwnerHyperparamUpdate], + &[TransactionType::OwnerToggleCommitReveal], )?; ensure!( @@ -1218,7 +1212,7 @@ pub mod pallet { pallet_subtensor::Pallet::::record_owner_rl( maybe_owner, netuid, - &[TransactionType::OwnerHyperparamUpdate], + &[TransactionType::OwnerToggleCommitReveal], ); Ok(()) } @@ -1242,14 +1236,14 @@ pub mod pallet { let maybe_owner = pallet_subtensor::Pallet::::ensure_sn_owner_or_root_with_limits( origin, netuid, - &[TransactionType::OwnerHyperparamUpdate], + &[TransactionType::OwnerToggleLiquidAlphaEnabled], )?; pallet_subtensor::Pallet::::set_liquid_alpha_enabled(netuid, enabled); log::debug!("LiquidAlphaEnableToggled( netuid: {netuid:?}, Enabled: {enabled:?} ) "); pallet_subtensor::Pallet::::record_owner_rl( maybe_owner, netuid, - &[TransactionType::OwnerHyperparamUpdate], + &[TransactionType::OwnerToggleLiquidAlphaEnabled], ); Ok(()) } @@ -1266,7 +1260,7 @@ pub mod pallet { let maybe_owner = pallet_subtensor::Pallet::::ensure_sn_owner_or_root_with_limits( origin.clone(), netuid, - &[TransactionType::OwnerHyperparamUpdate], + &[TransactionType::OwnerSetAlphaValues], )?; let res = pallet_subtensor::Pallet::::do_set_alpha_values( origin, netuid, alpha_low, alpha_high, @@ -1275,7 +1269,7 @@ pub mod pallet { pallet_subtensor::Pallet::::record_owner_rl( maybe_owner, netuid, - &[TransactionType::OwnerHyperparamUpdate], + &[TransactionType::OwnerSetAlphaValues], ); } res @@ -1373,7 +1367,7 @@ pub mod pallet { let maybe_owner = pallet_subtensor::Pallet::::ensure_sn_owner_or_root_with_limits( origin, netuid, - &[TransactionType::OwnerHyperparamUpdate], + &[TransactionType::OwnerSetWeightCommitInterval], )?; ensure!( @@ -1387,7 +1381,7 @@ pub mod pallet { pallet_subtensor::Pallet::::record_owner_rl( maybe_owner, netuid, - &[TransactionType::OwnerHyperparamUpdate], + &[TransactionType::OwnerSetWeightCommitInterval], ); Ok(()) @@ -1465,14 +1459,14 @@ pub mod pallet { let maybe_owner = pallet_subtensor::Pallet::::ensure_sn_owner_or_root_with_limits( origin, netuid, - &[TransactionType::OwnerHyperparamUpdate], + &[TransactionType::OwnerToggleTransfer], )?; let res = pallet_subtensor::Pallet::::toggle_transfer(netuid, toggle); if res.is_ok() { pallet_subtensor::Pallet::::record_owner_rl( maybe_owner, netuid, - &[TransactionType::OwnerHyperparamUpdate], + &[TransactionType::OwnerToggleTransfer], ); } res @@ -1607,7 +1601,7 @@ pub mod pallet { let maybe_owner = pallet_subtensor::Pallet::::ensure_sn_owner_or_root_with_limits( origin.clone(), netuid, - &[TransactionType::OwnerHyperparamUpdate], + &[TransactionType::OwnerSetAlphaSigmoidSteepness], )?; ensure!( @@ -1627,7 +1621,7 @@ pub mod pallet { pallet_subtensor::Pallet::::record_owner_rl( maybe_owner, netuid, - &[TransactionType::OwnerHyperparamUpdate], + &[TransactionType::OwnerSetAlphaSigmoidSteepness], ); Ok(()) } @@ -1651,7 +1645,7 @@ pub mod pallet { let maybe_owner = pallet_subtensor::Pallet::::ensure_sn_owner_or_root_with_limits( origin, netuid, - &[TransactionType::OwnerHyperparamUpdate], + &[TransactionType::OwnerToggleYuma3Enabled], )?; pallet_subtensor::Pallet::::set_yuma3_enabled(netuid, enabled); @@ -1660,7 +1654,7 @@ pub mod pallet { pallet_subtensor::Pallet::::record_owner_rl( maybe_owner, netuid, - &[TransactionType::OwnerHyperparamUpdate], + &[TransactionType::OwnerToggleYuma3Enabled], ); Ok(()) } @@ -1684,7 +1678,7 @@ pub mod pallet { let maybe_owner = pallet_subtensor::Pallet::::ensure_sn_owner_or_root_with_limits( origin, netuid, - &[TransactionType::OwnerHyperparamUpdate], + &[TransactionType::OwnerToggleBondsReset], )?; pallet_subtensor::Pallet::::set_bonds_reset(netuid, enabled); @@ -1693,7 +1687,7 @@ pub mod pallet { pallet_subtensor::Pallet::::record_owner_rl( maybe_owner, netuid, - &[TransactionType::OwnerHyperparamUpdate], + &[TransactionType::OwnerToggleBondsReset], ); Ok(()) } @@ -1797,13 +1791,13 @@ pub mod pallet { let maybe_owner = pallet_subtensor::Pallet::::ensure_sn_owner_or_root_with_limits( origin, netuid, - &[TransactionType::OwnerHyperparamUpdate], + &[TransactionType::OwnerSetOwnerImmuneNeuronLimit], )?; pallet_subtensor::Pallet::::set_owner_immune_neuron_limit(netuid, immune_neurons)?; pallet_subtensor::Pallet::::record_owner_rl( maybe_owner, netuid, - &[TransactionType::OwnerHyperparamUpdate], + &[TransactionType::OwnerSetOwnerImmuneNeuronLimit], ); Ok(()) } diff --git a/pallets/subtensor/src/tests/ensure.rs b/pallets/subtensor/src/tests/ensure.rs index e7bd70fb5a..2298a7d889 100644 --- a/pallets/subtensor/src/tests/ensure.rs +++ b/pallets/subtensor/src/tests/ensure.rs @@ -105,22 +105,24 @@ fn ensure_owner_or_root_with_limits_checks_rl_and_freeze() { let res = crate::Pallet::::ensure_sn_owner_or_root_with_limits( <::RuntimeOrigin>::signed(owner), netuid, - &[TransactionType::OwnerHyperparamUpdate], + &[TransactionType::OwnerSetKappa], ) .expect("should pass"); assert_eq!(res, Some(owner)); // Simulate previous update at current block -> next call should fail due to rate limit let now = crate::Pallet::::get_current_block_as_u64(); - crate::Pallet::::set_rate_limited_last_block( - &RateLimitKey::OwnerHyperparamUpdate(netuid), + crate::Pallet::::set_last_transaction_block_on_subnet( + &owner, + netuid, + &TransactionType::OwnerSetKappa, now, ); assert_noop!( crate::Pallet::::ensure_sn_owner_or_root_with_limits( <::RuntimeOrigin>::signed(owner), netuid, - &[TransactionType::OwnerHyperparamUpdate], + &[TransactionType::OwnerSetKappa], ), crate::Error::::TxRateLimitExceeded ); @@ -130,7 +132,7 @@ fn ensure_owner_or_root_with_limits_checks_rl_and_freeze() { assert_ok!(crate::Pallet::::ensure_sn_owner_or_root_with_limits( <::RuntimeOrigin>::signed(owner), netuid, - &[TransactionType::OwnerHyperparamUpdate] + &[TransactionType::OwnerSetKappa] )); // Now advance into the freeze window; ensure blocks @@ -151,7 +153,7 @@ fn ensure_owner_or_root_with_limits_checks_rl_and_freeze() { crate::Pallet::::ensure_sn_owner_or_root_with_limits( <::RuntimeOrigin>::signed(owner), netuid, - &[TransactionType::OwnerHyperparamUpdate], + &[TransactionType::OwnerSetKappa], ), crate::Error::::AdminActionProhibitedDuringWeightsWindow ); diff --git a/pallets/subtensor/src/utils/rate_limiting.rs b/pallets/subtensor/src/utils/rate_limiting.rs index 9ea504159b..eea9fdeaf0 100644 --- a/pallets/subtensor/src/utils/rate_limiting.rs +++ b/pallets/subtensor/src/utils/rate_limiting.rs @@ -11,8 +11,32 @@ pub enum TransactionType { RegisterNetwork, SetWeightsVersionKey, SetSNOwnerHotkey, - OwnerHyperparamUpdate, + OwnerHyperparamUpdate, // Deprecated aggregate; keep for compatibility if referenced in tests SubsubnetParameterUpdate, + // Per-hyperparameter owner updates (rate-limited independently) + OwnerSetServingRateLimit, + OwnerSetMaxDifficulty, + OwnerSetAdjustmentAlpha, + OwnerSetMaxWeightLimit, + OwnerSetImmunityPeriod, + OwnerSetMinAllowedWeights, + OwnerSetKappa, + OwnerSetRho, + OwnerSetActivityCutoff, + OwnerSetPowRegistrationAllowed, + OwnerSetMinBurn, + OwnerSetMaxBurn, + OwnerSetBondsMovingAverage, + OwnerSetBondsPenalty, + OwnerToggleCommitReveal, + OwnerToggleLiquidAlphaEnabled, + OwnerSetAlphaValues, + OwnerSetWeightCommitInterval, + OwnerToggleTransfer, + OwnerSetAlphaSigmoidSteepness, + OwnerToggleYuma3Enabled, + OwnerToggleBondsReset, + OwnerSetOwnerImmuneNeuronLimit, } /// Implement conversion from TransactionType to u16 @@ -27,6 +51,29 @@ impl From for u16 { TransactionType::SetSNOwnerHotkey => 5, TransactionType::OwnerHyperparamUpdate => 6, TransactionType::SubsubnetParameterUpdate => 7, + TransactionType::OwnerSetServingRateLimit => 10, + TransactionType::OwnerSetMaxDifficulty => 11, + TransactionType::OwnerSetAdjustmentAlpha => 12, + TransactionType::OwnerSetMaxWeightLimit => 13, + TransactionType::OwnerSetImmunityPeriod => 14, + TransactionType::OwnerSetMinAllowedWeights => 15, + TransactionType::OwnerSetKappa => 16, + TransactionType::OwnerSetRho => 17, + TransactionType::OwnerSetActivityCutoff => 18, + TransactionType::OwnerSetPowRegistrationAllowed => 19, + TransactionType::OwnerSetMinBurn => 20, + TransactionType::OwnerSetMaxBurn => 21, + TransactionType::OwnerSetBondsMovingAverage => 22, + TransactionType::OwnerSetBondsPenalty => 23, + TransactionType::OwnerToggleCommitReveal => 24, + TransactionType::OwnerToggleLiquidAlphaEnabled => 25, + TransactionType::OwnerSetAlphaValues => 26, + TransactionType::OwnerSetWeightCommitInterval => 27, + TransactionType::OwnerToggleTransfer => 28, + TransactionType::OwnerSetAlphaSigmoidSteepness => 29, + TransactionType::OwnerToggleYuma3Enabled => 30, + TransactionType::OwnerToggleBondsReset => 31, + TransactionType::OwnerSetOwnerImmuneNeuronLimit => 32, } } } @@ -42,6 +89,29 @@ impl From for TransactionType { 5 => TransactionType::SetSNOwnerHotkey, 6 => TransactionType::OwnerHyperparamUpdate, 7 => TransactionType::SubsubnetParameterUpdate, + 10 => TransactionType::OwnerSetServingRateLimit, + 11 => TransactionType::OwnerSetMaxDifficulty, + 12 => TransactionType::OwnerSetAdjustmentAlpha, + 13 => TransactionType::OwnerSetMaxWeightLimit, + 14 => TransactionType::OwnerSetImmunityPeriod, + 15 => TransactionType::OwnerSetMinAllowedWeights, + 16 => TransactionType::OwnerSetKappa, + 17 => TransactionType::OwnerSetRho, + 18 => TransactionType::OwnerSetActivityCutoff, + 19 => TransactionType::OwnerSetPowRegistrationAllowed, + 20 => TransactionType::OwnerSetMinBurn, + 21 => TransactionType::OwnerSetMaxBurn, + 22 => TransactionType::OwnerSetBondsMovingAverage, + 23 => TransactionType::OwnerSetBondsPenalty, + 24 => TransactionType::OwnerToggleCommitReveal, + 25 => TransactionType::OwnerToggleLiquidAlphaEnabled, + 26 => TransactionType::OwnerSetAlphaValues, + 27 => TransactionType::OwnerSetWeightCommitInterval, + 28 => TransactionType::OwnerToggleTransfer, + 29 => TransactionType::OwnerSetAlphaSigmoidSteepness, + 30 => TransactionType::OwnerToggleYuma3Enabled, + 31 => TransactionType::OwnerToggleBondsReset, + 32 => TransactionType::OwnerSetOwnerImmuneNeuronLimit, _ => TransactionType::Unknown, } } @@ -69,7 +139,30 @@ impl Pallet { TransactionType::SetWeightsVersionKey => (Tempo::::get(netuid) as u64) .saturating_mul(WeightsVersionKeyRateLimit::::get()), // Owner hyperparameter updates are rate-limited by N tempos on the subnet (sudo configurable) - TransactionType::OwnerHyperparamUpdate => { + TransactionType::OwnerHyperparamUpdate + | TransactionType::OwnerSetServingRateLimit + | TransactionType::OwnerSetMaxDifficulty + | TransactionType::OwnerSetAdjustmentAlpha + | TransactionType::OwnerSetMaxWeightLimit + | TransactionType::OwnerSetImmunityPeriod + | TransactionType::OwnerSetMinAllowedWeights + | TransactionType::OwnerSetKappa + | TransactionType::OwnerSetRho + | TransactionType::OwnerSetActivityCutoff + | TransactionType::OwnerSetPowRegistrationAllowed + | TransactionType::OwnerSetMinBurn + | TransactionType::OwnerSetMaxBurn + | TransactionType::OwnerSetBondsMovingAverage + | TransactionType::OwnerSetBondsPenalty + | TransactionType::OwnerToggleCommitReveal + | TransactionType::OwnerToggleLiquidAlphaEnabled + | TransactionType::OwnerSetAlphaValues + | TransactionType::OwnerSetWeightCommitInterval + | TransactionType::OwnerToggleTransfer + | TransactionType::OwnerSetAlphaSigmoidSteepness + | TransactionType::OwnerToggleYuma3Enabled + | TransactionType::OwnerToggleBondsReset + | TransactionType::OwnerSetOwnerImmuneNeuronLimit => { let tempos = OwnerHyperparamTempos::::get() as u64; (Tempo::::get(netuid) as u64).saturating_mul(tempos) } From 0d2dee41bc21135bbda67b24834b5dee2311b150 Mon Sep 17 00:00:00 2001 From: Aliaksandr Tsurko Date: Fri, 12 Sep 2025 16:52:44 +0300 Subject: [PATCH 4/9] Rename tempos -> epochs - return `rate_limit` naming --- .../neuron.precompile.reveal-weights.test.ts | 4 ++-- .../test/neuron.precompile.set-weights.test.ts | 4 ++-- evm-tests/test/staking.precompile.reward.test.ts | 4 ++-- .../subnet.precompile.hyperparameter.test.ts | 4 ++-- pallets/admin-utils/src/benchmarking.rs | 4 ++-- pallets/admin-utils/src/lib.rs | 10 +++++----- pallets/admin-utils/src/tests/mod.rs | 12 ++++-------- pallets/subtensor/src/lib.rs | 8 ++++---- pallets/subtensor/src/macros/events.rs | 4 ++-- pallets/subtensor/src/macros/hooks.rs | 2 +- ...s.rs => migrate_owner_hparam_rl_to_epochs.rs} | 16 ++++++++-------- pallets/subtensor/src/migrations/mod.rs | 2 +- pallets/subtensor/src/tests/ensure.rs | 9 ++++----- pallets/subtensor/src/utils/misc.rs | 6 +++--- pallets/subtensor/src/utils/rate_limiting.rs | 5 +++-- 15 files changed, 45 insertions(+), 49 deletions(-) rename pallets/subtensor/src/migrations/{migrate_owner_hparam_rl_to_tempos.rs => migrate_owner_hparam_rl_to_epochs.rs} (65%) diff --git a/evm-tests/test/neuron.precompile.reveal-weights.test.ts b/evm-tests/test/neuron.precompile.reveal-weights.test.ts index ac9c598743..98d1b177cc 100644 --- a/evm-tests/test/neuron.precompile.reveal-weights.test.ts +++ b/evm-tests/test/neuron.precompile.reveal-weights.test.ts @@ -79,8 +79,8 @@ describe("Test neuron precompile reveal weights", () => { const sudoFreezeTx = api.tx.Sudo.sudo({ call: setFreezeWindow.decodedCall }) await waitForTransactionWithRetry(api, sudoFreezeTx, alice) - // Set OwnerHyperparamTempos to 0 (disable RL) - const setOwnerRateLimit = api.tx.AdminUtils.sudo_set_owner_hparam_tempos({ tempos: 0 }) + // Set OwnerHyperparamRateLimit to 0 (disable RL) + const setOwnerRateLimit = api.tx.AdminUtils.sudo_set_owner_hparam_rate_limit({ epochs: 0 }) const sudoOwnerRateTx = api.tx.Sudo.sudo({ call: setOwnerRateLimit.decodedCall }) await waitForTransactionWithRetry(api, sudoOwnerRateTx, alice) } diff --git a/evm-tests/test/neuron.precompile.set-weights.test.ts b/evm-tests/test/neuron.precompile.set-weights.test.ts index 1423a2d045..fd4c5cefd9 100644 --- a/evm-tests/test/neuron.precompile.set-weights.test.ts +++ b/evm-tests/test/neuron.precompile.set-weights.test.ts @@ -47,8 +47,8 @@ describe("Test neuron precompile contract, set weights function", () => { const sudoFreezeTx = api.tx.Sudo.sudo({ call: setFreezeWindow.decodedCall }) await waitForTransactionWithRetry(api, sudoFreezeTx, alice) - // Set OwnerHyperparamTempos to 0 (disable RL) - const setOwnerRateLimit = api.tx.AdminUtils.sudo_set_owner_hparam_tempos({ tempos: 0 }) + // Set OwnerHyperparamRateLimit to 0 (disable RL) + const setOwnerRateLimit = api.tx.AdminUtils.sudo_set_owner_hparam_rate_limit({ epochs: 0 }) const sudoOwnerRateTx = api.tx.Sudo.sudo({ call: setOwnerRateLimit.decodedCall }) await waitForTransactionWithRetry(api, sudoOwnerRateTx, alice) } diff --git a/evm-tests/test/staking.precompile.reward.test.ts b/evm-tests/test/staking.precompile.reward.test.ts index 93e7010b53..cd02b6ae90 100644 --- a/evm-tests/test/staking.precompile.reward.test.ts +++ b/evm-tests/test/staking.precompile.reward.test.ts @@ -48,8 +48,8 @@ describe("Test neuron precompile reward", () => { const sudoFreezeTx = api.tx.Sudo.sudo({ call: setFreezeWindow.decodedCall }) await waitForTransactionWithRetry(api, sudoFreezeTx, alice) - // Set OwnerHyperparamTempos to 0 (disable RL) - const setOwnerRateLimit = api.tx.AdminUtils.sudo_set_owner_hparam_tempos({ tempos: 0 }) + // Set OwnerHyperparamRateLimit to 0 (disable RL) + const setOwnerRateLimit = api.tx.AdminUtils.sudo_set_owner_hparam_rate_limit({ epochs: 0 }) const sudoOwnerRateTx = api.tx.Sudo.sudo({ call: setOwnerRateLimit.decodedCall }) await waitForTransactionWithRetry(api, sudoOwnerRateTx, alice) } diff --git a/evm-tests/test/subnet.precompile.hyperparameter.test.ts b/evm-tests/test/subnet.precompile.hyperparameter.test.ts index 2741b018f7..b0e2983597 100644 --- a/evm-tests/test/subnet.precompile.hyperparameter.test.ts +++ b/evm-tests/test/subnet.precompile.hyperparameter.test.ts @@ -35,8 +35,8 @@ describe("Test the Subnet precompile contract", () => { const sudoFreezeTx = api.tx.Sudo.sudo({ call: setFreezeWindow.decodedCall }) await waitForTransactionWithRetry(api, sudoFreezeTx, alice) - // Set OwnerHyperparamTempos to 0 (disable RL) - const setOwnerRateLimit = api.tx.AdminUtils.sudo_set_owner_hparam_tempos({ tempos: 0 }) + // Set OwnerHyperparamRateLimit to 0 (disable RL) + const setOwnerRateLimit = api.tx.AdminUtils.sudo_set_owner_hparam_rate_limit({ epochs: 0 }) const sudoOwnerRateTx = api.tx.Sudo.sudo({ call: setOwnerRateLimit.decodedCall }) await waitForTransactionWithRetry(api, sudoOwnerRateTx, alice) } diff --git a/pallets/admin-utils/src/benchmarking.rs b/pallets/admin-utils/src/benchmarking.rs index 085ee56b03..186973a6d0 100644 --- a/pallets/admin-utils/src/benchmarking.rs +++ b/pallets/admin-utils/src/benchmarking.rs @@ -353,9 +353,9 @@ mod benchmarks { } #[benchmark] - fn sudo_set_owner_hparam_tempos() { + fn sudo_set_owner_hparam_rate_limit() { #[extrinsic_call] - _(RawOrigin::Root, 2u16/*tempos*/)/*sudo_set_owner_hparam_tempos*/; + _(RawOrigin::Root, 2u16/*epochs*/)/*sudo_set_owner_hparam_rate_limit*/; } #[benchmark] diff --git a/pallets/admin-utils/src/lib.rs b/pallets/admin-utils/src/lib.rs index 4f2170599c..2d66f9a3d4 100644 --- a/pallets/admin-utils/src/lib.rs +++ b/pallets/admin-utils/src/lib.rs @@ -1827,17 +1827,17 @@ pub mod pallet { Ok(()) } - /// Sets the owner hyperparameter rate limit in tempos (global multiplier). + /// Sets the owner hyperparameter rate limit in epochs (global multiplier). /// Only callable by root. #[pallet::call_index(75)] #[pallet::weight((0, DispatchClass::Operational, Pays::No))] - pub fn sudo_set_owner_hparam_tempos( + pub fn sudo_set_owner_hparam_rate_limit( origin: OriginFor, - tempos: u16, + epochs: u16, ) -> DispatchResult { ensure_root(origin)?; - pallet_subtensor::Pallet::::set_owner_hyperparam_tempos(tempos); - log::debug!("OwnerHyperparamTemposSet( tempos: {tempos:?} ) "); + pallet_subtensor::Pallet::::set_owner_hyperparam_rate_limit(epochs); + log::debug!("OwnerHyperparamRateLimitSet( epochs: {epochs:?} ) "); Ok(()) } diff --git a/pallets/admin-utils/src/tests/mod.rs b/pallets/admin-utils/src/tests/mod.rs index 79ea690706..85d284c402 100644 --- a/pallets/admin-utils/src/tests/mod.rs +++ b/pallets/admin-utils/src/tests/mod.rs @@ -1975,17 +1975,17 @@ fn test_sudo_set_admin_freeze_window_and_rate() { // Owner hyperparam tempos setter assert_eq!( - AdminUtils::sudo_set_owner_hparam_tempos( + AdminUtils::sudo_set_owner_hparam_rate_limit( <::RuntimeOrigin>::signed(U256::from(1)), 5 ), Err(DispatchError::BadOrigin) ); - assert_ok!(AdminUtils::sudo_set_owner_hparam_tempos( + assert_ok!(AdminUtils::sudo_set_owner_hparam_rate_limit( <::RuntimeOrigin>::root(), 5 )); - assert_eq!(pallet_subtensor::OwnerHyperparamTempos::::get(), 5); + assert_eq!(pallet_subtensor::OwnerHyperparamRateLimit::::get(), 5); }); } @@ -2169,11 +2169,7 @@ fn test_hyperparam_rate_limit_enforced_by_tempo() { // Immediate second update should fail due to tempo-based RL assert_noop!( - AdminUtils::sudo_set_kappa( - <::RuntimeOrigin>::signed(owner), - netuid, - 2 - ), + AdminUtils::sudo_set_kappa(<::RuntimeOrigin>::signed(owner), netuid, 2), SubtensorError::::TxRateLimitExceeded ); diff --git a/pallets/subtensor/src/lib.rs b/pallets/subtensor/src/lib.rs index 235e8bee76..382d0692e2 100644 --- a/pallets/subtensor/src/lib.rs +++ b/pallets/subtensor/src/lib.rs @@ -868,7 +868,7 @@ pub mod pallet { #[pallet::type_value] /// Default number of tempos for owner hyperparameter update rate limit - pub fn DefaultOwnerHyperparamTempos() -> u16 { + pub fn DefaultOwnerHyperparamRateLimit() -> u16 { 2 } @@ -888,9 +888,9 @@ pub mod pallet { StorageValue<_, u16, ValueQuery, DefaultAdminFreezeWindow>; #[pallet::storage] - /// Global number of tempos used to rate limit subnet owner hyperparameter updates - pub type OwnerHyperparamTempos = - StorageValue<_, u16, ValueQuery, DefaultOwnerHyperparamTempos>; + /// Global number of epochs used to rate limit subnet owner hyperparameter updates + pub type OwnerHyperparamRateLimit = + StorageValue<_, u16, ValueQuery, DefaultOwnerHyperparamRateLimit>; #[pallet::storage] pub type ColdkeySwapScheduleDuration = diff --git a/pallets/subtensor/src/macros/events.rs b/pallets/subtensor/src/macros/events.rs index 2400e3ef8c..bb41c063ea 100644 --- a/pallets/subtensor/src/macros/events.rs +++ b/pallets/subtensor/src/macros/events.rs @@ -116,8 +116,8 @@ mod events { TxChildKeyTakeRateLimitSet(u64), /// setting the admin freeze window length (last N blocks of tempo) AdminFreezeWindowSet(u16), - /// setting the owner hyperparameter rate limit in tempos - OwnerHyperparamTemposSet(u16), + /// setting the owner hyperparameter rate limit in epochs + OwnerHyperparamRateLimitSet(u16), /// minimum childkey take set MinChildKeyTakeSet(u16), /// maximum childkey take set diff --git a/pallets/subtensor/src/macros/hooks.rs b/pallets/subtensor/src/macros/hooks.rs index d23d5884f2..cbb72eed1f 100644 --- a/pallets/subtensor/src/macros/hooks.rs +++ b/pallets/subtensor/src/macros/hooks.rs @@ -139,7 +139,7 @@ mod hooks { // Migrate last block rate limiting storage items .saturating_add(migrations::migrate_rate_limiting_last_blocks::migrate_obsolete_rate_limiting_last_blocks_storage::()) // Remove deprecated OwnerHyperparamRateLimit storage item - .saturating_add(migrations::migrate_owner_hparam_rl_to_tempos::migrate_owner_hyperparam_rl_to_tempos::()) + .saturating_add(migrations::migrate_owner_hparam_rl_to_epochs::migrate_owner_hyperparam_rl_to_epochs::()) // Migrate remove network modality .saturating_add(migrations::migrate_remove_network_modality::migrate_remove_network_modality::()); weight diff --git a/pallets/subtensor/src/migrations/migrate_owner_hparam_rl_to_tempos.rs b/pallets/subtensor/src/migrations/migrate_owner_hparam_rl_to_epochs.rs similarity index 65% rename from pallets/subtensor/src/migrations/migrate_owner_hparam_rl_to_tempos.rs rename to pallets/subtensor/src/migrations/migrate_owner_hparam_rl_to_epochs.rs index 9a5d8b4842..501c00c4ad 100644 --- a/pallets/subtensor/src/migrations/migrate_owner_hparam_rl_to_tempos.rs +++ b/pallets/subtensor/src/migrations/migrate_owner_hparam_rl_to_epochs.rs @@ -5,15 +5,16 @@ use frame_support::weights::Weight; use sp_io::hashing::twox_128; use sp_io::storage::{clear, get}; -/// Remove the deprecated OwnerHyperparamRateLimit storage item. -/// If the old value was 0 (disabled), preserve that by setting OwnerHyperparamTempos to 0. -/// Otherwise, leave the new storage at its default (2 tempos). -pub fn migrate_owner_hyperparam_rl_to_tempos() -> Weight { - let migration_name = b"migrate_owner_hyperparam_rl_to_tempos".to_vec(); +/// Migrate u64 to u16 in OwnerHyperparamRateLimit and new default +pub fn migrate_owner_hyperparam_rl_to_epochs() -> Weight { + let migration_name = b"migrate_owner_hyperparam_rl_to_epochs".to_vec(); let mut weight = T::DbWeight::get().reads(1); if HasMigrationRun::::get(&migration_name) { - log::info!("Migration '{:?}' already executed. Skipping.", migration_name); + log::info!( + "Migration '{:?}' already executed. Skipping.", + migration_name + ); return weight; } @@ -25,11 +26,10 @@ pub fn migrate_owner_hyperparam_rl_to_tempos() -> Weight { if let Ok(old_limit_blocks) = ::decode(&mut &value_bytes[..]) { if old_limit_blocks == 0u64 { // Preserve disabled state - Pallet::::set_owner_hyperparam_tempos(0); + Pallet::::set_owner_hyperparam_rate_limit(0); } } - clear(&full_key); weight = weight.saturating_add(T::DbWeight::get().writes(1)); } diff --git a/pallets/subtensor/src/migrations/mod.rs b/pallets/subtensor/src/migrations/mod.rs index b5818add7b..9f0a6bce7f 100644 --- a/pallets/subtensor/src/migrations/mod.rs +++ b/pallets/subtensor/src/migrations/mod.rs @@ -20,10 +20,10 @@ pub mod migrate_fix_root_tao_and_alpha_in; pub mod migrate_identities_v2; pub mod migrate_init_total_issuance; pub mod migrate_orphaned_storage_items; +pub mod migrate_owner_hparam_rl_to_epochs; pub mod migrate_populate_owned_hotkeys; pub mod migrate_rao; pub mod migrate_rate_limiting_last_blocks; -pub mod migrate_owner_hparam_rl_to_tempos; pub mod migrate_remove_commitments_rate_limit; pub mod migrate_remove_network_modality; pub mod migrate_remove_stake_map; diff --git a/pallets/subtensor/src/tests/ensure.rs b/pallets/subtensor/src/tests/ensure.rs index 2298a7d889..22a8dbd029 100644 --- a/pallets/subtensor/src/tests/ensure.rs +++ b/pallets/subtensor/src/tests/ensure.rs @@ -65,11 +65,11 @@ fn ensure_subnet_owner_or_root_distinguishes_root_and_owner() { fn ensure_root_with_rate_limit_blocks_in_freeze_window() { new_test_ext(1).execute_with(|| { let netuid = NetUid::from(1); - let tempo: u16 = 10; + let tempo = 10; add_network(netuid, 10, 0); // Set freeze window to 3 - let freeze_window: u16 = 3; + let freeze_window = 3; crate::Pallet::::set_admin_freeze_window(freeze_window); run_to_block((tempo - freeze_window + 1).into()); @@ -96,10 +96,9 @@ fn ensure_owner_or_root_with_limits_checks_rl_and_freeze() { SubnetOwner::::insert(netuid, owner); // Set freeze window to 0 initially to avoid blocking when tempo is small let freeze_window = 3; - crate::Pallet::::set_admin_freeze_window(0); + crate::Pallet::::set_admin_freeze_window(freeze_window); - // Set tempo to 1 so owner hyperparam RL = 2 blocks - crate::Pallet::::set_tempo(netuid, 1); + crate::Pallet::::set_owner_hyperparam_rate_limit(1); // Outside freeze window initially; should pass and return Some(owner) let res = crate::Pallet::::ensure_sn_owner_or_root_with_limits( diff --git a/pallets/subtensor/src/utils/misc.rs b/pallets/subtensor/src/utils/misc.rs index 28e3599368..0dde6345a2 100644 --- a/pallets/subtensor/src/utils/misc.rs +++ b/pallets/subtensor/src/utils/misc.rs @@ -116,9 +116,9 @@ impl Pallet { Self::deposit_event(Event::AdminFreezeWindowSet(window)); } - pub fn set_owner_hyperparam_tempos(tempos: u16) { - OwnerHyperparamTempos::::set(tempos); - Self::deposit_event(Event::OwnerHyperparamTemposSet(tempos)); + pub fn set_owner_hyperparam_rate_limit(epochs: u16) { + OwnerHyperparamRateLimit::::set(epochs); + Self::deposit_event(Event::OwnerHyperparamRateLimitSet(epochs)); } /// If owner is `Some`, record last-blocks for the provided `TransactionType`s. diff --git a/pallets/subtensor/src/utils/rate_limiting.rs b/pallets/subtensor/src/utils/rate_limiting.rs index eea9fdeaf0..9221974d12 100644 --- a/pallets/subtensor/src/utils/rate_limiting.rs +++ b/pallets/subtensor/src/utils/rate_limiting.rs @@ -116,6 +116,7 @@ impl From for TransactionType { } } } + impl Pallet { // ======================== // ==== Rate Limiting ===== @@ -163,8 +164,8 @@ impl Pallet { | TransactionType::OwnerToggleYuma3Enabled | TransactionType::OwnerToggleBondsReset | TransactionType::OwnerSetOwnerImmuneNeuronLimit => { - let tempos = OwnerHyperparamTempos::::get() as u64; - (Tempo::::get(netuid) as u64).saturating_mul(tempos) + let epochs = OwnerHyperparamRateLimit::::get() as u64; + (Tempo::::get(netuid) as u64).saturating_mul(epochs) } TransactionType::SetSNOwnerHotkey => DefaultSetSNOwnerHotkeyRateLimit::::get(), From b0f963ba88251bc90385937d8c465d0177a7bd77 Mon Sep 17 00:00:00 2001 From: Aliaksandr Tsurko Date: Fri, 12 Sep 2025 18:47:55 +0300 Subject: [PATCH 5/9] Refactor TransactionType --- pallets/admin-utils/src/lib.rs | 94 ++--- pallets/admin-utils/src/tests/mod.rs | 20 +- pallets/subtensor/src/lib.rs | 4 +- .../migrate_owner_hparam_rl_to_epochs.rs | 2 +- pallets/subtensor/src/staking/set_children.rs | 27 +- pallets/subtensor/src/subnets/subnet.rs | 8 +- pallets/subtensor/src/tests/children.rs | 24 +- pallets/subtensor/src/tests/ensure.rs | 29 +- pallets/subtensor/src/tests/mock.rs | 2 +- .../subtensor/src/transaction_extension.rs | 2 +- pallets/subtensor/src/utils/misc.rs | 6 +- pallets/subtensor/src/utils/rate_limiting.rs | 321 ++++++++---------- 12 files changed, 229 insertions(+), 310 deletions(-) diff --git a/pallets/admin-utils/src/lib.rs b/pallets/admin-utils/src/lib.rs index 2d66f9a3d4..4f63fdeaa4 100644 --- a/pallets/admin-utils/src/lib.rs +++ b/pallets/admin-utils/src/lib.rs @@ -25,7 +25,7 @@ pub mod pallet { use frame_support::{dispatch::DispatchResult, pallet_prelude::StorageMap}; use frame_system::pallet_prelude::*; use pallet_evm_chain_id::{self, ChainId}; - use pallet_subtensor::utils::rate_limiting::TransactionType; + use pallet_subtensor::utils::rate_limiting::{Hyperparameter, TransactionType}; use sp_runtime::BoundedVec; use substrate_fixed::types::I96F32; use subtensor_runtime_common::{NetUid, SubId, TaoCurrency}; @@ -216,14 +216,14 @@ pub mod pallet { let maybe_owner = pallet_subtensor::Pallet::::ensure_sn_owner_or_root_with_limits( origin, netuid, - &[TransactionType::OwnerSetServingRateLimit], + &[Hyperparameter::ServingRateLimit.into()], )?; pallet_subtensor::Pallet::::set_serving_rate_limit(netuid, serving_rate_limit); log::debug!("ServingRateLimitSet( serving_rate_limit: {serving_rate_limit:?} ) "); pallet_subtensor::Pallet::::record_owner_rl( maybe_owner, netuid, - &[TransactionType::OwnerSetServingRateLimit], + &[Hyperparameter::ServingRateLimit.into()], ); Ok(()) } @@ -268,7 +268,7 @@ pub mod pallet { let maybe_owner = pallet_subtensor::Pallet::::ensure_sn_owner_or_root_with_limits( origin, netuid, - &[TransactionType::OwnerSetMaxDifficulty], + &[Hyperparameter::MaxDifficulty.into()], )?; ensure!( @@ -282,7 +282,7 @@ pub mod pallet { pallet_subtensor::Pallet::::record_owner_rl( maybe_owner, netuid, - &[TransactionType::OwnerSetMaxDifficulty], + &[Hyperparameter::MaxDifficulty.into()], ); Ok(()) } @@ -393,7 +393,7 @@ pub mod pallet { let maybe_owner = pallet_subtensor::Pallet::::ensure_sn_owner_or_root_with_limits( origin, netuid, - &[TransactionType::OwnerSetAdjustmentAlpha], + &[Hyperparameter::AdjustmentAlpha.into()], )?; ensure!( @@ -404,7 +404,7 @@ pub mod pallet { pallet_subtensor::Pallet::::record_owner_rl( maybe_owner, netuid, - &[TransactionType::OwnerSetAdjustmentAlpha], + &[Hyperparameter::AdjustmentAlpha.into()], ); log::debug!("AdjustmentAlphaSet( adjustment_alpha: {adjustment_alpha:?} ) "); Ok(()) @@ -425,7 +425,7 @@ pub mod pallet { let maybe_owner = pallet_subtensor::Pallet::::ensure_sn_owner_or_root_with_limits( origin, netuid, - &[TransactionType::OwnerSetMaxWeightLimit], + &[Hyperparameter::MaxWeightLimit.into()], )?; ensure!( @@ -436,7 +436,7 @@ pub mod pallet { pallet_subtensor::Pallet::::record_owner_rl( maybe_owner, netuid, - &[TransactionType::OwnerSetMaxWeightLimit], + &[Hyperparameter::MaxWeightLimit.into()], ); log::debug!( "MaxWeightLimitSet( netuid: {netuid:?} max_weight_limit: {max_weight_limit:?} ) " @@ -459,7 +459,7 @@ pub mod pallet { let maybe_owner = pallet_subtensor::Pallet::::ensure_sn_owner_or_root_with_limits( origin, netuid, - &[TransactionType::OwnerSetImmunityPeriod], + &[Hyperparameter::ImmunityPeriod.into()], )?; ensure!( pallet_subtensor::Pallet::::if_subnet_exist(netuid), @@ -470,7 +470,7 @@ pub mod pallet { pallet_subtensor::Pallet::::record_owner_rl( maybe_owner, netuid, - &[TransactionType::OwnerSetImmunityPeriod], + &[Hyperparameter::ImmunityPeriod.into()], ); log::debug!( "ImmunityPeriodSet( netuid: {netuid:?} immunity_period: {immunity_period:?} ) " @@ -493,7 +493,7 @@ pub mod pallet { let maybe_owner = pallet_subtensor::Pallet::::ensure_sn_owner_or_root_with_limits( origin, netuid, - &[TransactionType::OwnerSetMinAllowedWeights], + &[Hyperparameter::MinAllowedWeights.into()], )?; ensure!( @@ -507,7 +507,7 @@ pub mod pallet { pallet_subtensor::Pallet::::record_owner_rl( maybe_owner, netuid, - &[TransactionType::OwnerSetMinAllowedWeights], + &[Hyperparameter::MinAllowedWeights.into()], ); Ok(()) } @@ -551,7 +551,7 @@ pub mod pallet { let maybe_owner = pallet_subtensor::Pallet::::ensure_sn_owner_or_root_with_limits( origin, netuid, - &[TransactionType::OwnerSetKappa], + &[Hyperparameter::Kappa.into()], )?; ensure!( @@ -563,7 +563,7 @@ pub mod pallet { pallet_subtensor::Pallet::::record_owner_rl( maybe_owner, netuid, - &[TransactionType::OwnerSetKappa], + &[Hyperparameter::Kappa.into()], ); Ok(()) } @@ -579,7 +579,7 @@ pub mod pallet { let maybe_owner = pallet_subtensor::Pallet::::ensure_sn_owner_or_root_with_limits( origin, netuid, - &[TransactionType::OwnerSetRho], + &[Hyperparameter::Rho.into()], )?; ensure!( @@ -591,7 +591,7 @@ pub mod pallet { pallet_subtensor::Pallet::::record_owner_rl( maybe_owner, netuid, - &[TransactionType::OwnerSetRho], + &[Hyperparameter::Rho.into()], ); Ok(()) } @@ -611,7 +611,7 @@ pub mod pallet { let maybe_owner = pallet_subtensor::Pallet::::ensure_sn_owner_or_root_with_limits( origin, netuid, - &[TransactionType::OwnerSetActivityCutoff], + &[Hyperparameter::ActivityCutoff.into()], )?; ensure!( @@ -631,7 +631,7 @@ pub mod pallet { pallet_subtensor::Pallet::::record_owner_rl( maybe_owner, netuid, - &[TransactionType::OwnerSetActivityCutoff], + &[Hyperparameter::ActivityCutoff.into()], ); Ok(()) } @@ -679,7 +679,7 @@ pub mod pallet { let maybe_owner = pallet_subtensor::Pallet::::ensure_sn_owner_or_root_with_limits( origin, netuid, - &[TransactionType::OwnerSetPowRegistrationAllowed], + &[Hyperparameter::PowRegistrationAllowed.into()], )?; pallet_subtensor::Pallet::::set_network_pow_registration_allowed( @@ -692,7 +692,7 @@ pub mod pallet { pallet_subtensor::Pallet::::record_owner_rl( maybe_owner, netuid, - &[TransactionType::OwnerSetPowRegistrationAllowed], + &[Hyperparameter::PowRegistrationAllowed.into()], ); Ok(()) } @@ -740,7 +740,7 @@ pub mod pallet { let maybe_owner = pallet_subtensor::Pallet::::ensure_sn_owner_or_root_with_limits( origin, netuid, - &[TransactionType::OwnerSetMinBurn], + &[Hyperparameter::MinBurn.into()], )?; ensure!( pallet_subtensor::Pallet::::if_subnet_exist(netuid), @@ -760,7 +760,7 @@ pub mod pallet { pallet_subtensor::Pallet::::record_owner_rl( maybe_owner, netuid, - &[TransactionType::OwnerSetMinBurn], + &[Hyperparameter::MinBurn.into()], ); Ok(()) } @@ -780,7 +780,7 @@ pub mod pallet { let maybe_owner = pallet_subtensor::Pallet::::ensure_sn_owner_or_root_with_limits( origin, netuid, - &[TransactionType::OwnerSetMaxBurn], + &[Hyperparameter::MaxBurn.into()], )?; ensure!( pallet_subtensor::Pallet::::if_subnet_exist(netuid), @@ -800,7 +800,7 @@ pub mod pallet { pallet_subtensor::Pallet::::record_owner_rl( maybe_owner, netuid, - &[TransactionType::OwnerSetMaxBurn], + &[Hyperparameter::MaxBurn.into()], ); Ok(()) } @@ -875,7 +875,7 @@ pub mod pallet { let maybe_owner = pallet_subtensor::Pallet::::ensure_sn_owner_or_root_with_limits( origin, netuid, - &[TransactionType::OwnerSetBondsMovingAverage], + &[Hyperparameter::BondsMovingAverage.into()], )?; if maybe_owner.is_some() { ensure!( @@ -895,7 +895,7 @@ pub mod pallet { pallet_subtensor::Pallet::::record_owner_rl( maybe_owner, netuid, - &[TransactionType::OwnerSetBondsMovingAverage], + &[Hyperparameter::BondsMovingAverage.into()], ); Ok(()) } @@ -915,7 +915,7 @@ pub mod pallet { let maybe_owner = pallet_subtensor::Pallet::::ensure_sn_owner_or_root_with_limits( origin, netuid, - &[TransactionType::OwnerSetBondsPenalty], + &[Hyperparameter::BondsPenalty.into()], )?; ensure!( @@ -927,7 +927,7 @@ pub mod pallet { pallet_subtensor::Pallet::::record_owner_rl( maybe_owner, netuid, - &[TransactionType::OwnerSetBondsPenalty], + &[Hyperparameter::BondsPenalty.into()], ); Ok(()) } @@ -1199,7 +1199,7 @@ pub mod pallet { let maybe_owner = pallet_subtensor::Pallet::::ensure_sn_owner_or_root_with_limits( origin, netuid, - &[TransactionType::OwnerToggleCommitReveal], + &[Hyperparameter::CommitRevealEnabled.into()], )?; ensure!( @@ -1212,7 +1212,7 @@ pub mod pallet { pallet_subtensor::Pallet::::record_owner_rl( maybe_owner, netuid, - &[TransactionType::OwnerToggleCommitReveal], + &[Hyperparameter::CommitRevealEnabled.into()], ); Ok(()) } @@ -1236,14 +1236,14 @@ pub mod pallet { let maybe_owner = pallet_subtensor::Pallet::::ensure_sn_owner_or_root_with_limits( origin, netuid, - &[TransactionType::OwnerToggleLiquidAlphaEnabled], + &[Hyperparameter::LiquidAlphaEnabled.into()], )?; pallet_subtensor::Pallet::::set_liquid_alpha_enabled(netuid, enabled); log::debug!("LiquidAlphaEnableToggled( netuid: {netuid:?}, Enabled: {enabled:?} ) "); pallet_subtensor::Pallet::::record_owner_rl( maybe_owner, netuid, - &[TransactionType::OwnerToggleLiquidAlphaEnabled], + &[Hyperparameter::LiquidAlphaEnabled.into()], ); Ok(()) } @@ -1260,7 +1260,7 @@ pub mod pallet { let maybe_owner = pallet_subtensor::Pallet::::ensure_sn_owner_or_root_with_limits( origin.clone(), netuid, - &[TransactionType::OwnerSetAlphaValues], + &[Hyperparameter::AlphaValues.into()], )?; let res = pallet_subtensor::Pallet::::do_set_alpha_values( origin, netuid, alpha_low, alpha_high, @@ -1269,7 +1269,7 @@ pub mod pallet { pallet_subtensor::Pallet::::record_owner_rl( maybe_owner, netuid, - &[TransactionType::OwnerSetAlphaValues], + &[Hyperparameter::AlphaValues.into()], ); } res @@ -1367,7 +1367,7 @@ pub mod pallet { let maybe_owner = pallet_subtensor::Pallet::::ensure_sn_owner_or_root_with_limits( origin, netuid, - &[TransactionType::OwnerSetWeightCommitInterval], + &[Hyperparameter::WeightCommitInterval.into()], )?; ensure!( @@ -1381,7 +1381,7 @@ pub mod pallet { pallet_subtensor::Pallet::::record_owner_rl( maybe_owner, netuid, - &[TransactionType::OwnerSetWeightCommitInterval], + &[Hyperparameter::WeightCommitInterval.into()], ); Ok(()) @@ -1459,14 +1459,14 @@ pub mod pallet { let maybe_owner = pallet_subtensor::Pallet::::ensure_sn_owner_or_root_with_limits( origin, netuid, - &[TransactionType::OwnerToggleTransfer], + &[Hyperparameter::TransferEnabled.into()], )?; let res = pallet_subtensor::Pallet::::toggle_transfer(netuid, toggle); if res.is_ok() { pallet_subtensor::Pallet::::record_owner_rl( maybe_owner, netuid, - &[TransactionType::OwnerToggleTransfer], + &[Hyperparameter::TransferEnabled.into()], ); } res @@ -1601,7 +1601,7 @@ pub mod pallet { let maybe_owner = pallet_subtensor::Pallet::::ensure_sn_owner_or_root_with_limits( origin.clone(), netuid, - &[TransactionType::OwnerSetAlphaSigmoidSteepness], + &[Hyperparameter::AlphaSigmoidSteepness.into()], )?; ensure!( @@ -1621,7 +1621,7 @@ pub mod pallet { pallet_subtensor::Pallet::::record_owner_rl( maybe_owner, netuid, - &[TransactionType::OwnerSetAlphaSigmoidSteepness], + &[Hyperparameter::AlphaSigmoidSteepness.into()], ); Ok(()) } @@ -1645,7 +1645,7 @@ pub mod pallet { let maybe_owner = pallet_subtensor::Pallet::::ensure_sn_owner_or_root_with_limits( origin, netuid, - &[TransactionType::OwnerToggleYuma3Enabled], + &[Hyperparameter::Yuma3Enabled.into()], )?; pallet_subtensor::Pallet::::set_yuma3_enabled(netuid, enabled); @@ -1654,7 +1654,7 @@ pub mod pallet { pallet_subtensor::Pallet::::record_owner_rl( maybe_owner, netuid, - &[TransactionType::OwnerToggleYuma3Enabled], + &[Hyperparameter::Yuma3Enabled.into()], ); Ok(()) } @@ -1678,7 +1678,7 @@ pub mod pallet { let maybe_owner = pallet_subtensor::Pallet::::ensure_sn_owner_or_root_with_limits( origin, netuid, - &[TransactionType::OwnerToggleBondsReset], + &[Hyperparameter::BondsResetEnabled.into()], )?; pallet_subtensor::Pallet::::set_bonds_reset(netuid, enabled); @@ -1687,7 +1687,7 @@ pub mod pallet { pallet_subtensor::Pallet::::record_owner_rl( maybe_owner, netuid, - &[TransactionType::OwnerToggleBondsReset], + &[Hyperparameter::BondsResetEnabled.into()], ); Ok(()) } @@ -1791,13 +1791,13 @@ pub mod pallet { let maybe_owner = pallet_subtensor::Pallet::::ensure_sn_owner_or_root_with_limits( origin, netuid, - &[TransactionType::OwnerSetOwnerImmuneNeuronLimit], + &[Hyperparameter::ImmuneNeuronLimit.into()], )?; pallet_subtensor::Pallet::::set_owner_immune_neuron_limit(netuid, immune_neurons)?; pallet_subtensor::Pallet::::record_owner_rl( maybe_owner, netuid, - &[TransactionType::OwnerSetOwnerImmuneNeuronLimit], + &[Hyperparameter::ImmuneNeuronLimit.into()], ); Ok(()) } diff --git a/pallets/admin-utils/src/tests/mod.rs b/pallets/admin-utils/src/tests/mod.rs index 85d284c402..0e6fdc23b7 100644 --- a/pallets/admin-utils/src/tests/mod.rs +++ b/pallets/admin-utils/src/tests/mod.rs @@ -7,7 +7,7 @@ use frame_support::{ use frame_system::Config; use pallet_subtensor::{Error as SubtensorError, SubnetOwner, Tempo, WeightsVersionKeyRateLimit}; // use pallet_subtensor::{migrations, Event}; -use pallet_subtensor::Event; +use pallet_subtensor::{Event, utils::rate_limiting::TransactionType}; use sp_consensus_grandpa::AuthorityId as GrandpaId; use sp_core::{Get, Pair, U256, ed25519}; use substrate_fixed::types::I96F32; @@ -188,11 +188,10 @@ fn test_sudo_set_weights_version_key_rate_limit() { // Try to set again with // Assert rate limit not passed - assert!(!SubtensorModule::passes_rate_limit_on_subnet( - &pallet_subtensor::utils::rate_limiting::TransactionType::SetWeightsVersionKey, - &sn_owner, - netuid - )); + assert!( + !TransactionType::SetWeightsVersionKey + .passes_rate_limit_on_subnet::(&sn_owner, netuid) + ); // Try transaction assert_noop!( @@ -206,11 +205,10 @@ fn test_sudo_set_weights_version_key_rate_limit() { // Wait for rate limit to pass run_to_block(rate_limit_period + 1); - assert!(SubtensorModule::passes_rate_limit_on_subnet( - &pallet_subtensor::utils::rate_limiting::TransactionType::SetWeightsVersionKey, - &sn_owner, - netuid - )); + assert!( + TransactionType::SetWeightsVersionKey + .passes_rate_limit_on_subnet::(&sn_owner, netuid) + ); // Try transaction assert_ok!(AdminUtils::sudo_set_weights_version_key( diff --git a/pallets/subtensor/src/lib.rs b/pallets/subtensor/src/lib.rs index 382d0692e2..d2cb54306f 100644 --- a/pallets/subtensor/src/lib.rs +++ b/pallets/subtensor/src/lib.rs @@ -42,7 +42,7 @@ pub mod staking; pub mod subnets; pub mod swap; pub mod utils; -use crate::utils::rate_limiting::TransactionType; +use crate::utils::rate_limiting::{Hyperparameter, TransactionType}; use macros::{config, dispatches, errors, events, genesis, hooks}; #[cfg(test)] @@ -2189,7 +2189,7 @@ pub enum RateLimitKey { // The setting sn owner hotkey operation is rate limited per netuid SetSNOwnerHotkey(NetUid), // Generic rate limit for subnet-owner hyperparameter updates (per netuid) - OwnerHyperparamUpdate(NetUid), + OwnerHyperparamUpdate(NetUid, Hyperparameter), // Subnet registration rate limit NetworkLastRegistered, // Last tx block limit per account ID diff --git a/pallets/subtensor/src/migrations/migrate_owner_hparam_rl_to_epochs.rs b/pallets/subtensor/src/migrations/migrate_owner_hparam_rl_to_epochs.rs index 501c00c4ad..4a1bfa48be 100644 --- a/pallets/subtensor/src/migrations/migrate_owner_hparam_rl_to_epochs.rs +++ b/pallets/subtensor/src/migrations/migrate_owner_hparam_rl_to_epochs.rs @@ -3,7 +3,7 @@ use crate::HasMigrationRun; use codec::Decode; use frame_support::weights::Weight; use sp_io::hashing::twox_128; -use sp_io::storage::{clear, get}; +use sp_io::storage::get; /// Migrate u64 to u16 in OwnerHyperparamRateLimit and new default pub fn migrate_owner_hyperparam_rl_to_epochs() -> Weight { diff --git a/pallets/subtensor/src/staking/set_children.rs b/pallets/subtensor/src/staking/set_children.rs index 139ea82c5d..c6c37f7e96 100644 --- a/pallets/subtensor/src/staking/set_children.rs +++ b/pallets/subtensor/src/staking/set_children.rs @@ -48,10 +48,9 @@ impl Pallet { // Ensure the hotkey passes the rate limit. ensure!( - Self::passes_rate_limit_on_subnet( - &TransactionType::SetChildren, // Set children. - &hotkey, // Specific to a hotkey. - netuid, // Specific to a subnet. + TransactionType::SetChildren.passes_rate_limit_on_subnet::( + &hotkey, // Specific to a hotkey. + netuid, // Specific to a subnet. ), Error::::TxRateLimitExceeded ); @@ -111,12 +110,7 @@ impl Pallet { // Set last transaction block let current_block = Self::get_current_block_as_u64(); - Self::set_last_transaction_block_on_subnet( - &hotkey, - netuid, - &TransactionType::SetChildren, - current_block, - ); + TransactionType::SetChildren.set_last_block_on_subnet::(&hotkey, netuid, current_block); // Calculate cool-down block let cooldown_block = @@ -319,10 +313,9 @@ impl Pallet { if take > current_take { // Ensure the hotkey passes the rate limit. ensure!( - Self::passes_rate_limit_on_subnet( - &TransactionType::SetChildkeyTake, // Set childkey take. - &hotkey, // Specific to a hotkey. - netuid, // Specific to a subnet. + TransactionType::SetChildkeyTake.passes_rate_limit_on_subnet::( + &hotkey, // Specific to a hotkey. + netuid, // Specific to a subnet. ), Error::::TxChildkeyTakeRateLimitExceeded ); @@ -330,10 +323,9 @@ impl Pallet { // Set last transaction block let current_block = Self::get_current_block_as_u64(); - Self::set_last_transaction_block_on_subnet( + TransactionType::SetChildkeyTake.set_last_block_on_subnet::( &hotkey, netuid, - &TransactionType::SetChildkeyTake, current_block, ); @@ -341,10 +333,9 @@ impl Pallet { ChildkeyTake::::insert(hotkey.clone(), netuid, take); // Update the last transaction block - Self::set_last_transaction_block_on_subnet( + TransactionType::SetChildkeyTake.set_last_block_on_subnet::( &hotkey, netuid, - &TransactionType::SetChildkeyTake, current_block, ); diff --git a/pallets/subtensor/src/subnets/subnet.rs b/pallets/subtensor/src/subnets/subnet.rs index 38be89cba0..6241c54ef7 100644 --- a/pallets/subtensor/src/subnets/subnet.rs +++ b/pallets/subtensor/src/subnets/subnet.rs @@ -135,7 +135,7 @@ impl Pallet { // --- 4. Rate limit for network registrations. let current_block = Self::get_current_block_as_u64(); ensure!( - Self::passes_rate_limit(&TransactionType::RegisterNetwork, &coldkey), + TransactionType::RegisterNetwork.passes_rate_limit::(&coldkey), Error::::NetworkTxRateLimitExceeded ); @@ -391,8 +391,7 @@ impl Pallet { // Rate limit: 1 call per week ensure!( - Self::passes_rate_limit_on_subnet( - &TransactionType::SetSNOwnerHotkey, + TransactionType::SetSNOwnerHotkey.passes_rate_limit_on_subnet::( hotkey, // ignored netuid, // Specific to a subnet. ), @@ -401,10 +400,9 @@ impl Pallet { // Set last transaction block let current_block = Self::get_current_block_as_u64(); - Self::set_last_transaction_block_on_subnet( + TransactionType::SetSNOwnerHotkey.set_last_block_on_subnet::( hotkey, netuid, - &TransactionType::SetSNOwnerHotkey, current_block, ); diff --git a/pallets/subtensor/src/tests/children.rs b/pallets/subtensor/src/tests/children.rs index 1208add954..67dfe47fbe 100644 --- a/pallets/subtensor/src/tests/children.rs +++ b/pallets/subtensor/src/tests/children.rs @@ -955,17 +955,15 @@ fn test_childkey_take_rate_limiting() { // Helper function to log rate limit information let log_rate_limit_info = || { let current_block = SubtensorModule::get_current_block_as_u64(); - let last_block = SubtensorModule::get_last_transaction_block_on_subnet( + let last_block = TransactionType::SetChildkeyTake.last_block_on_subnet::( &hotkey, netuid, - &TransactionType::SetChildkeyTake, ); - let passes = SubtensorModule::passes_rate_limit_on_subnet( - &TransactionType::SetChildkeyTake, + let passes = TransactionType::SetChildkeyTake.passes_rate_limit_on_subnet::( &hotkey, netuid, ); - let limit = SubtensorModule::get_rate_limit_on_subnet(&TransactionType::SetChildkeyTake, netuid); + let limit = TransactionType::SetChildkeyTake.rate_limit_on_subnet::(netuid); log::info!( "Rate limit info: current_block: {}, last_block: {}, limit: {}, passes: {}, diff: {}", current_block, @@ -2489,12 +2487,7 @@ fn test_revoke_child_no_min_stake_check() { assert_eq!(children_after, vec![(proportion, child)]); // Bypass tx rate limit - SubtensorModule::set_last_transaction_block_on_subnet( - &parent, - netuid, - &TransactionType::SetChildren, - 0, - ); + TransactionType::SetChildren.set_last_block_on_subnet::(&parent, netuid, 0); // Schedule parent-child relationship revokation assert_ok!(SubtensorModule::do_schedule_children( @@ -2609,18 +2602,13 @@ fn test_set_children_rate_limit_fail_then_succeed() { // Try again after rate limit period has passed // Check rate limit - let limit = - SubtensorModule::get_rate_limit_on_subnet(&TransactionType::SetChildren, netuid); + let limit = TransactionType::SetChildren.rate_limit_on_subnet::(netuid); // Step that many blocks step_block(limit as u16); // Verify rate limit passes - assert!(SubtensorModule::passes_rate_limit_on_subnet( - &TransactionType::SetChildren, - &hotkey, - netuid - )); + assert!(TransactionType::SetChildren.passes_rate_limit_on_subnet::(&hotkey, netuid)); // Try again mock_set_children(&coldkey, &hotkey, netuid, &[(100, child2)]); diff --git a/pallets/subtensor/src/tests/ensure.rs b/pallets/subtensor/src/tests/ensure.rs index 22a8dbd029..298339defa 100644 --- a/pallets/subtensor/src/tests/ensure.rs +++ b/pallets/subtensor/src/tests/ensure.rs @@ -4,8 +4,8 @@ use sp_core::U256; use subtensor_runtime_common::NetUid; use super::mock::*; -use crate::utils::rate_limiting::TransactionType; -use crate::{RateLimitKey, SubnetOwner, SubtokenEnabled}; +use crate::utils::rate_limiting::{Hyperparameter, TransactionType}; +use crate::{OwnerHyperparamRateLimit, SubnetOwner, SubtokenEnabled}; #[test] fn ensure_subnet_owner_returns_who_and_checks_ownership() { @@ -95,33 +95,31 @@ fn ensure_owner_or_root_with_limits_checks_rl_and_freeze() { let owner: U256 = U256::from(5); SubnetOwner::::insert(netuid, owner); // Set freeze window to 0 initially to avoid blocking when tempo is small - let freeze_window = 3; - crate::Pallet::::set_admin_freeze_window(freeze_window); + crate::Pallet::::set_admin_freeze_window(0); + + // Set tempo to 1 so owner hyperparam RL = 2 blocks + crate::Pallet::::set_tempo(netuid, 1); - crate::Pallet::::set_owner_hyperparam_rate_limit(1); + assert_eq!(OwnerHyperparamRateLimit::::get(), 2); // Outside freeze window initially; should pass and return Some(owner) let res = crate::Pallet::::ensure_sn_owner_or_root_with_limits( <::RuntimeOrigin>::signed(owner), netuid, - &[TransactionType::OwnerSetKappa], + &[Hyperparameter::Kappa.into()], ) .expect("should pass"); assert_eq!(res, Some(owner)); // Simulate previous update at current block -> next call should fail due to rate limit let now = crate::Pallet::::get_current_block_as_u64(); - crate::Pallet::::set_last_transaction_block_on_subnet( - &owner, - netuid, - &TransactionType::OwnerSetKappa, - now, - ); + TransactionType::from(Hyperparameter::Kappa) + .set_last_block_on_subnet::(&owner, netuid, now); assert_noop!( crate::Pallet::::ensure_sn_owner_or_root_with_limits( <::RuntimeOrigin>::signed(owner), netuid, - &[TransactionType::OwnerSetKappa], + &[Hyperparameter::Kappa.into()], ), crate::Error::::TxRateLimitExceeded ); @@ -131,12 +129,13 @@ fn ensure_owner_or_root_with_limits_checks_rl_and_freeze() { assert_ok!(crate::Pallet::::ensure_sn_owner_or_root_with_limits( <::RuntimeOrigin>::signed(owner), netuid, - &[TransactionType::OwnerSetKappa] + &[Hyperparameter::Kappa.into()] )); // Now advance into the freeze window; ensure blocks // (using loop for clarity, because epoch calculation function uses netuid) // Restore tempo and configure freeze window for this part + let freeze_window = 3; crate::Pallet::::set_tempo(netuid, tempo); crate::Pallet::::set_admin_freeze_window(freeze_window); let freeze_window = freeze_window as u64; @@ -152,7 +151,7 @@ fn ensure_owner_or_root_with_limits_checks_rl_and_freeze() { crate::Pallet::::ensure_sn_owner_or_root_with_limits( <::RuntimeOrigin>::signed(owner), netuid, - &[TransactionType::OwnerSetKappa], + &[Hyperparameter::Kappa.into()], ), crate::Error::::AdminActionProhibitedDuringWeightsWindow ); diff --git a/pallets/subtensor/src/tests/mock.rs b/pallets/subtensor/src/tests/mock.rs index 28da96687a..672b1e44dd 100644 --- a/pallets/subtensor/src/tests/mock.rs +++ b/pallets/subtensor/src/tests/mock.rs @@ -948,7 +948,7 @@ pub fn mock_set_children_no_epochs(netuid: NetUid, parent: &U256, child_vec: &[( #[allow(dead_code)] pub fn step_rate_limit(transaction_type: &TransactionType, netuid: NetUid) { // Check rate limit - let limit = SubtensorModule::get_rate_limit_on_subnet(transaction_type, netuid); + let limit = transaction_type.rate_limit_on_subnet::(netuid); // Step that many blocks step_block(limit as u16); diff --git a/pallets/subtensor/src/transaction_extension.rs b/pallets/subtensor/src/transaction_extension.rs index 62c6a3c8ca..b56dff0ea0 100644 --- a/pallets/subtensor/src/transaction_extension.rs +++ b/pallets/subtensor/src/transaction_extension.rs @@ -279,7 +279,7 @@ where .map(|validity| (validity, Some(who.clone()), origin.clone())) } Some(Call::register_network { .. }) => { - if !Pallet::::passes_rate_limit(&TransactionType::RegisterNetwork, who) { + if !TransactionType::RegisterNetwork.passes_rate_limit::(who) { return Err(CustomTransactionError::RateLimitExceeded.into()); } diff --git a/pallets/subtensor/src/utils/misc.rs b/pallets/subtensor/src/utils/misc.rs index 0dde6345a2..ef04a8ada8 100644 --- a/pallets/subtensor/src/utils/misc.rs +++ b/pallets/subtensor/src/utils/misc.rs @@ -62,7 +62,7 @@ impl Pallet { if let Some(who) = maybe_who.as_ref() { for tx in limits.iter() { ensure!( - Self::passes_rate_limit_on_subnet(tx, who, netuid), + tx.passes_rate_limit_on_subnet::(who, netuid), Error::::TxRateLimitExceeded ); } @@ -83,7 +83,7 @@ impl Pallet { Self::ensure_not_in_admin_freeze_window(netuid, now)?; for tx in limits.iter() { ensure!( - Self::passes_rate_limit_on_subnet(tx, &who, netuid), + tx.passes_rate_limit_on_subnet::(&who, netuid), Error::::TxRateLimitExceeded ); } @@ -130,7 +130,7 @@ impl Pallet { if let Some(who) = maybe_owner { let now = Self::get_current_block_as_u64(); for tx in txs { - Self::set_last_transaction_block_on_subnet(&who, netuid, tx, now); + tx.set_last_block_on_subnet::(&who, netuid, now); } } } diff --git a/pallets/subtensor/src/utils/rate_limiting.rs b/pallets/subtensor/src/utils/rate_limiting.rs index 9221974d12..463ba4b8cd 100644 --- a/pallets/subtensor/src/utils/rate_limiting.rs +++ b/pallets/subtensor/src/utils/rate_limiting.rs @@ -4,6 +4,7 @@ use super::*; /// Enum representing different types of transactions #[derive(Copy, Clone)] +#[non_exhaustive] pub enum TransactionType { SetChildren, SetChildkeyTake, @@ -11,245 +12,189 @@ pub enum TransactionType { RegisterNetwork, SetWeightsVersionKey, SetSNOwnerHotkey, - OwnerHyperparamUpdate, // Deprecated aggregate; keep for compatibility if referenced in tests + OwnerHyperparamUpdate(Hyperparameter), SubsubnetParameterUpdate, - // Per-hyperparameter owner updates (rate-limited independently) - OwnerSetServingRateLimit, - OwnerSetMaxDifficulty, - OwnerSetAdjustmentAlpha, - OwnerSetMaxWeightLimit, - OwnerSetImmunityPeriod, - OwnerSetMinAllowedWeights, - OwnerSetKappa, - OwnerSetRho, - OwnerSetActivityCutoff, - OwnerSetPowRegistrationAllowed, - OwnerSetMinBurn, - OwnerSetMaxBurn, - OwnerSetBondsMovingAverage, - OwnerSetBondsPenalty, - OwnerToggleCommitReveal, - OwnerToggleLiquidAlphaEnabled, - OwnerSetAlphaValues, - OwnerSetWeightCommitInterval, - OwnerToggleTransfer, - OwnerSetAlphaSigmoidSteepness, - OwnerToggleYuma3Enabled, - OwnerToggleBondsReset, - OwnerSetOwnerImmuneNeuronLimit, } -/// Implement conversion from TransactionType to u16 -impl From for u16 { - fn from(tx_type: TransactionType) -> Self { - match tx_type { - TransactionType::SetChildren => 0, - TransactionType::SetChildkeyTake => 1, - TransactionType::Unknown => 2, - TransactionType::RegisterNetwork => 3, - TransactionType::SetWeightsVersionKey => 4, - TransactionType::SetSNOwnerHotkey => 5, - TransactionType::OwnerHyperparamUpdate => 6, - TransactionType::SubsubnetParameterUpdate => 7, - TransactionType::OwnerSetServingRateLimit => 10, - TransactionType::OwnerSetMaxDifficulty => 11, - TransactionType::OwnerSetAdjustmentAlpha => 12, - TransactionType::OwnerSetMaxWeightLimit => 13, - TransactionType::OwnerSetImmunityPeriod => 14, - TransactionType::OwnerSetMinAllowedWeights => 15, - TransactionType::OwnerSetKappa => 16, - TransactionType::OwnerSetRho => 17, - TransactionType::OwnerSetActivityCutoff => 18, - TransactionType::OwnerSetPowRegistrationAllowed => 19, - TransactionType::OwnerSetMinBurn => 20, - TransactionType::OwnerSetMaxBurn => 21, - TransactionType::OwnerSetBondsMovingAverage => 22, - TransactionType::OwnerSetBondsPenalty => 23, - TransactionType::OwnerToggleCommitReveal => 24, - TransactionType::OwnerToggleLiquidAlphaEnabled => 25, - TransactionType::OwnerSetAlphaValues => 26, - TransactionType::OwnerSetWeightCommitInterval => 27, - TransactionType::OwnerToggleTransfer => 28, - TransactionType::OwnerSetAlphaSigmoidSteepness => 29, - TransactionType::OwnerToggleYuma3Enabled => 30, - TransactionType::OwnerToggleBondsReset => 31, - TransactionType::OwnerSetOwnerImmuneNeuronLimit => 32, - } - } -} - -/// Implement conversion from u16 to TransactionType -impl From for TransactionType { - fn from(value: u16) -> Self { - match value { - 0 => TransactionType::SetChildren, - 1 => TransactionType::SetChildkeyTake, - 3 => TransactionType::RegisterNetwork, - 4 => TransactionType::SetWeightsVersionKey, - 5 => TransactionType::SetSNOwnerHotkey, - 6 => TransactionType::OwnerHyperparamUpdate, - 7 => TransactionType::SubsubnetParameterUpdate, - 10 => TransactionType::OwnerSetServingRateLimit, - 11 => TransactionType::OwnerSetMaxDifficulty, - 12 => TransactionType::OwnerSetAdjustmentAlpha, - 13 => TransactionType::OwnerSetMaxWeightLimit, - 14 => TransactionType::OwnerSetImmunityPeriod, - 15 => TransactionType::OwnerSetMinAllowedWeights, - 16 => TransactionType::OwnerSetKappa, - 17 => TransactionType::OwnerSetRho, - 18 => TransactionType::OwnerSetActivityCutoff, - 19 => TransactionType::OwnerSetPowRegistrationAllowed, - 20 => TransactionType::OwnerSetMinBurn, - 21 => TransactionType::OwnerSetMaxBurn, - 22 => TransactionType::OwnerSetBondsMovingAverage, - 23 => TransactionType::OwnerSetBondsPenalty, - 24 => TransactionType::OwnerToggleCommitReveal, - 25 => TransactionType::OwnerToggleLiquidAlphaEnabled, - 26 => TransactionType::OwnerSetAlphaValues, - 27 => TransactionType::OwnerSetWeightCommitInterval, - 28 => TransactionType::OwnerToggleTransfer, - 29 => TransactionType::OwnerSetAlphaSigmoidSteepness, - 30 => TransactionType::OwnerToggleYuma3Enabled, - 31 => TransactionType::OwnerToggleBondsReset, - 32 => TransactionType::OwnerSetOwnerImmuneNeuronLimit, - _ => TransactionType::Unknown, - } - } -} - -impl Pallet { - // ======================== - // ==== Rate Limiting ===== - // ======================== +impl TransactionType { /// Get the rate limit for a specific transaction type - pub fn get_rate_limit(tx_type: &TransactionType) -> u64 { - match tx_type { - TransactionType::SetChildren => 150, // 30 minutes - TransactionType::SetChildkeyTake => TxChildkeyTakeRateLimit::::get(), - TransactionType::RegisterNetwork => NetworkRateLimit::::get(), - TransactionType::SubsubnetParameterUpdate => SubsubnetCountSetRateLimit::::get(), - - TransactionType::Unknown => 0, // Default to no limit for unknown types (no limit) + pub fn rate_limit(&self) -> u64 { + match self { + Self::SetChildren => 150, // 30 minutes + Self::SetChildkeyTake => TxChildkeyTakeRateLimit::::get(), + Self::RegisterNetwork => NetworkRateLimit::::get(), + Self::SubsubnetParameterUpdate => SubsubnetCountSetRateLimit::::get(), + + Self::Unknown => 0, // Default to no limit for unknown types (no limit) _ => 0, } } - pub fn get_rate_limit_on_subnet(tx_type: &TransactionType, netuid: NetUid) -> u64 { + pub fn rate_limit_on_subnet(&self, netuid: NetUid) -> u64 { #[allow(clippy::match_single_binding)] - match tx_type { - TransactionType::SetWeightsVersionKey => (Tempo::::get(netuid) as u64) + match self { + Self::SetWeightsVersionKey => (Tempo::::get(netuid) as u64) .saturating_mul(WeightsVersionKeyRateLimit::::get()), // Owner hyperparameter updates are rate-limited by N tempos on the subnet (sudo configurable) - TransactionType::OwnerHyperparamUpdate - | TransactionType::OwnerSetServingRateLimit - | TransactionType::OwnerSetMaxDifficulty - | TransactionType::OwnerSetAdjustmentAlpha - | TransactionType::OwnerSetMaxWeightLimit - | TransactionType::OwnerSetImmunityPeriod - | TransactionType::OwnerSetMinAllowedWeights - | TransactionType::OwnerSetKappa - | TransactionType::OwnerSetRho - | TransactionType::OwnerSetActivityCutoff - | TransactionType::OwnerSetPowRegistrationAllowed - | TransactionType::OwnerSetMinBurn - | TransactionType::OwnerSetMaxBurn - | TransactionType::OwnerSetBondsMovingAverage - | TransactionType::OwnerSetBondsPenalty - | TransactionType::OwnerToggleCommitReveal - | TransactionType::OwnerToggleLiquidAlphaEnabled - | TransactionType::OwnerSetAlphaValues - | TransactionType::OwnerSetWeightCommitInterval - | TransactionType::OwnerToggleTransfer - | TransactionType::OwnerSetAlphaSigmoidSteepness - | TransactionType::OwnerToggleYuma3Enabled - | TransactionType::OwnerToggleBondsReset - | TransactionType::OwnerSetOwnerImmuneNeuronLimit => { + Self::OwnerHyperparamUpdate(_) => { let epochs = OwnerHyperparamRateLimit::::get() as u64; (Tempo::::get(netuid) as u64).saturating_mul(epochs) } - TransactionType::SetSNOwnerHotkey => DefaultSetSNOwnerHotkeyRateLimit::::get(), + Self::SetSNOwnerHotkey => DefaultSetSNOwnerHotkeyRateLimit::::get(), - _ => Self::get_rate_limit(tx_type), + _ => self.rate_limit::(), } } + pub fn passes_rate_limit(&self, key: &T::AccountId) -> bool { + let block = Pallet::::get_current_block_as_u64(); + let limit = self.rate_limit::(); + let last_block = self.last_block::(key); + + Self::check_passes_rate_limit(limit, block, last_block) + } + pub fn check_passes_rate_limit(limit: u64, block: u64, last_block: u64) -> bool { // Allow the first transaction (when last_block is 0) or if the rate limit has passed last_block == 0 || block.saturating_sub(last_block) >= limit } - pub fn passes_rate_limit(tx_type: &TransactionType, key: &T::AccountId) -> bool { - let block: u64 = Self::get_current_block_as_u64(); - let limit: u64 = Self::get_rate_limit(tx_type); - let last_block: u64 = Self::get_last_transaction_block(key, tx_type); - - Self::check_passes_rate_limit(limit, block, last_block) - } - /// Check if a transaction should be rate limited on a specific subnet - pub fn passes_rate_limit_on_subnet( - tx_type: &TransactionType, + pub fn passes_rate_limit_on_subnet( + &self, hotkey: &T::AccountId, netuid: NetUid, ) -> bool { - let block: u64 = Self::get_current_block_as_u64(); - let limit: u64 = Self::get_rate_limit_on_subnet(tx_type, netuid); - let last_block: u64 = Self::get_last_transaction_block_on_subnet(hotkey, netuid, tx_type); + let block = Pallet::::get_current_block_as_u64(); + let limit = self.rate_limit_on_subnet::(netuid); + let last_block = self.last_block_on_subnet::(hotkey, netuid); Self::check_passes_rate_limit(limit, block, last_block) } /// Get the block number of the last transaction for a specific key, and transaction type - pub fn get_last_transaction_block(key: &T::AccountId, tx_type: &TransactionType) -> u64 { - match tx_type { - TransactionType::RegisterNetwork => Self::get_network_last_lock_block(), - _ => Self::get_last_transaction_block_on_subnet(key, NetUid::ROOT, tx_type), + pub fn last_block(&self, key: &T::AccountId) -> u64 { + match self { + Self::RegisterNetwork => Pallet::::get_network_last_lock_block(), + _ => self.last_block_on_subnet::(key, NetUid::ROOT), } } - /// Get the block number of the last transaction for a specific hotkey, network, and transaction type - pub fn get_last_transaction_block_on_subnet( - hotkey: &T::AccountId, - netuid: NetUid, - tx_type: &TransactionType, - ) -> u64 { - match tx_type { - TransactionType::RegisterNetwork => Self::get_network_last_lock_block(), - TransactionType::SetSNOwnerHotkey => { - Self::get_rate_limited_last_block(&RateLimitKey::SetSNOwnerHotkey(netuid)) - } - TransactionType::OwnerHyperparamUpdate => { - Self::get_rate_limited_last_block(&RateLimitKey::OwnerHyperparamUpdate(netuid)) + /// Get the block number of the last transaction for a specific hotkey, network, and transaction + /// type + pub fn last_block_on_subnet(&self, hotkey: &T::AccountId, netuid: NetUid) -> u64 { + match self { + Self::RegisterNetwork => Pallet::::get_network_last_lock_block(), + Self::SetSNOwnerHotkey => { + Pallet::::get_rate_limited_last_block(&RateLimitKey::SetSNOwnerHotkey(netuid)) } + Self::OwnerHyperparamUpdate(hparam) => Pallet::::get_rate_limited_last_block( + &RateLimitKey::OwnerHyperparamUpdate(netuid, *hparam), + ), _ => { - let tx_as_u16: u16 = (*tx_type).into(); - TransactionKeyLastBlock::::get((hotkey, netuid, tx_as_u16)) + let tx_type: u16 = (*self).into(); + TransactionKeyLastBlock::::get((hotkey, netuid, tx_type)) } } } - /// Set the block number of the last transaction for a specific hotkey, network, and transaction type - pub fn set_last_transaction_block_on_subnet( + /// Set the block number of the last transaction for a specific hotkey, network, and transaction + /// type + pub fn set_last_block_on_subnet( + &self, key: &T::AccountId, netuid: NetUid, - tx_type: &TransactionType, block: u64, ) { - match tx_type { - TransactionType::RegisterNetwork => Self::set_network_last_lock_block(block), - TransactionType::SetSNOwnerHotkey => { - Self::set_rate_limited_last_block(&RateLimitKey::SetSNOwnerHotkey(netuid), block) - } - TransactionType::OwnerHyperparamUpdate => Self::set_rate_limited_last_block( - &RateLimitKey::OwnerHyperparamUpdate(netuid), + match self { + Self::RegisterNetwork => Pallet::::set_network_last_lock_block(block), + Self::SetSNOwnerHotkey => Pallet::::set_rate_limited_last_block( + &RateLimitKey::SetSNOwnerHotkey(netuid), + block, + ), + Self::OwnerHyperparamUpdate(hparam) => Pallet::::set_rate_limited_last_block( + &RateLimitKey::OwnerHyperparamUpdate(netuid, *hparam), block, ), _ => { - let tx_as_u16: u16 = (*tx_type).into(); - TransactionKeyLastBlock::::insert((key, netuid, tx_as_u16), block); + let tx_type: u16 = (*self).into(); + TransactionKeyLastBlock::::insert((key, netuid, tx_type), block); } } } +} + +/// Implement conversion from TransactionType to u16 +impl From for u16 { + fn from(tx_type: TransactionType) -> Self { + match tx_type { + TransactionType::SetChildren => 0, + TransactionType::SetChildkeyTake => 1, + TransactionType::Unknown => 2, + TransactionType::RegisterNetwork => 3, + TransactionType::SetWeightsVersionKey => 4, + TransactionType::SetSNOwnerHotkey => 5, + TransactionType::OwnerHyperparamUpdate(_) => 6, + TransactionType::SubsubnetParameterUpdate => 7, + } + } +} + +/// Implement conversion from u16 to TransactionType +impl From for TransactionType { + fn from(value: u16) -> Self { + match value { + 0 => TransactionType::SetChildren, + 1 => TransactionType::SetChildkeyTake, + 3 => TransactionType::RegisterNetwork, + 4 => TransactionType::SetWeightsVersionKey, + 5 => TransactionType::SetSNOwnerHotkey, + 6 => TransactionType::OwnerHyperparamUpdate(Hyperparameter::Unknown), + 7 => TransactionType::SubsubnetParameterUpdate, + _ => TransactionType::Unknown, + } + } +} + +impl From for TransactionType { + fn from(param: Hyperparameter) -> Self { + Self::OwnerHyperparamUpdate(param) + } +} + +#[derive(Encode, Decode, Clone, Copy, PartialEq, Eq, Debug, TypeInfo)] +#[non_exhaustive] +pub enum Hyperparameter { + Unknown = 0, + ServingRateLimit = 1, + MaxDifficulty = 2, + AdjustmentAlpha = 3, + MaxWeightLimit = 4, + ImmunityPeriod = 5, + MinAllowedWeights = 6, + Kappa = 7, + Rho = 8, + ActivityCutoff = 9, + PowRegistrationAllowed = 10, + MinBurn = 11, + MaxBurn = 12, + BondsMovingAverage = 13, + BondsPenalty = 14, + CommitRevealEnabled = 15, + LiquidAlphaEnabled = 16, + AlphaValues = 17, + WeightCommitInterval = 18, + TransferEnabled = 19, + AlphaSigmoidSteepness = 20, + Yuma3Enabled = 21, + BondsResetEnabled = 22, + ImmuneNeuronLimit = 23, +} + +impl Pallet { + // ======================== + // ==== Rate Limiting ===== + // ======================== pub fn remove_last_tx_block(key: &T::AccountId) { Self::remove_rate_limited_last_block(&RateLimitKey::LastTxBlock(key.clone())) From cdfccff79c885382cca865f43828e557f0922977 Mon Sep 17 00:00:00 2001 From: Aliaksandr Tsurko Date: Fri, 12 Sep 2025 19:21:26 +0300 Subject: [PATCH 6/9] Test hyperparams updates rate limited independently --- pallets/admin-utils/src/tests/mod.rs | 82 ++++++++++++++++++++++++++++ 1 file changed, 82 insertions(+) diff --git a/pallets/admin-utils/src/tests/mod.rs b/pallets/admin-utils/src/tests/mod.rs index 0e6fdc23b7..fade295f23 100644 --- a/pallets/admin-utils/src/tests/mod.rs +++ b/pallets/admin-utils/src/tests/mod.rs @@ -2181,6 +2181,88 @@ fn test_hyperparam_rate_limit_enforced_by_tempo() { }); } +// Verifies owner hyperparameters are rate-limited independently per parameter. +// Setting one hyperparameter should not block setting a different hyperparameter +// during the same rate-limit window, but it should still block itself. +#[test] +fn test_owner_hyperparam_rate_limit_independent_per_param() { + new_test_ext().execute_with(|| { + let netuid = NetUid::from(7); + add_network(netuid, 10); + + // Set subnet owner + let owner: U256 = U256::from(123); + SubnetOwner::::insert(netuid, owner); + + // Use small tempo to make RL short and deterministic (2 blocks when tempo=1) + SubtensorModule::set_tempo(netuid, 1); + // Disable admin freeze window so it doesn't interfere with small tempo + assert_ok!(AdminUtils::sudo_set_admin_freeze_window( + <::RuntimeOrigin>::root(), + 0 + )); + + // First update to kappa should succeed + assert_ok!(AdminUtils::sudo_set_kappa( + <::RuntimeOrigin>::signed(owner), + netuid, + 10 + )); + + // Immediate second update to the SAME param (kappa) should be blocked by RL + assert_noop!( + AdminUtils::sudo_set_kappa( + <::RuntimeOrigin>::signed(owner), + netuid, + 11 + ), + SubtensorError::::TxRateLimitExceeded + ); + + // Updating a DIFFERENT param (rho) should pass immediately — independent RL key + assert_ok!(AdminUtils::sudo_set_rho( + <::RuntimeOrigin>::signed(owner), + netuid, + 5 + )); + + // kappa should still be blocked until its own RL window passes + assert_noop!( + AdminUtils::sudo_set_kappa( + <::RuntimeOrigin>::signed(owner), + netuid, + 12 + ), + SubtensorError::::TxRateLimitExceeded + ); + + // rho should also be blocked for itself immediately after being set + assert_noop!( + AdminUtils::sudo_set_rho( + <::RuntimeOrigin>::signed(owner), + netuid, + 6 + ), + SubtensorError::::TxRateLimitExceeded + ); + + // Advance enough blocks to pass the RL window (2 blocks when tempo=1 and default epochs=2) + run_to_block(SubtensorModule::get_current_block_as_u64() + 2); + + // Now both hyperparameters can be updated again + assert_ok!(AdminUtils::sudo_set_kappa( + <::RuntimeOrigin>::signed(owner), + netuid, + 13 + )); + assert_ok!(AdminUtils::sudo_set_rho( + <::RuntimeOrigin>::signed(owner), + netuid, + 7 + )); + }); +} + #[test] fn test_sudo_set_max_burn() { new_test_ext().execute_with(|| { From e700fedb2dab6511ab5ef00a14a19927242e524f Mon Sep 17 00:00:00 2001 From: Aliaksandr Tsurko Date: Fri, 12 Sep 2025 19:25:10 +0300 Subject: [PATCH 7/9] Reformat --- pallets/admin-utils/src/tests/mod.rs | 6 +----- .../src/migrations/migrate_owner_hparam_rl_to_epochs.rs | 5 +---- 2 files changed, 2 insertions(+), 9 deletions(-) diff --git a/pallets/admin-utils/src/tests/mod.rs b/pallets/admin-utils/src/tests/mod.rs index fade295f23..f4a21a45c4 100644 --- a/pallets/admin-utils/src/tests/mod.rs +++ b/pallets/admin-utils/src/tests/mod.rs @@ -2238,11 +2238,7 @@ fn test_owner_hyperparam_rate_limit_independent_per_param() { // rho should also be blocked for itself immediately after being set assert_noop!( - AdminUtils::sudo_set_rho( - <::RuntimeOrigin>::signed(owner), - netuid, - 6 - ), + AdminUtils::sudo_set_rho(<::RuntimeOrigin>::signed(owner), netuid, 6), SubtensorError::::TxRateLimitExceeded ); diff --git a/pallets/subtensor/src/migrations/migrate_owner_hparam_rl_to_epochs.rs b/pallets/subtensor/src/migrations/migrate_owner_hparam_rl_to_epochs.rs index 4a1bfa48be..236bb10d5f 100644 --- a/pallets/subtensor/src/migrations/migrate_owner_hparam_rl_to_epochs.rs +++ b/pallets/subtensor/src/migrations/migrate_owner_hparam_rl_to_epochs.rs @@ -11,10 +11,7 @@ pub fn migrate_owner_hyperparam_rl_to_epochs() -> Weight { let mut weight = T::DbWeight::get().reads(1); if HasMigrationRun::::get(&migration_name) { - log::info!( - "Migration '{:?}' already executed. Skipping.", - migration_name - ); + log::info!("Migration '{migration_name:?}' already executed. Skipping."); return weight; } From c3683184fcb1a401929f6ed91432181019d35082 Mon Sep 17 00:00:00 2001 From: Aliaksandr Tsurko Date: Fri, 12 Sep 2025 19:54:27 +0300 Subject: [PATCH 8/9] Update localnet patch --- scripts/localnet_patch.sh | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/scripts/localnet_patch.sh b/scripts/localnet_patch.sh index f5a84e8348..e3bee8c5b8 100755 --- a/scripts/localnet_patch.sh +++ b/scripts/localnet_patch.sh @@ -19,7 +19,7 @@ if ! grep -q 'pub fn DefaultPendingCooldown() -> u64 {' "$DefaultPend exit 1 fi -if ! grep -q 'TransactionType::SetChildren => 150, // 30 minutes' "$SetChildren"; then +if ! grep -q 'Self::SetChildren => 150, // 30 minutes' "$SetChildren"; then echo "Error: Target string not found in $SetChildren" exit 1 fi @@ -27,6 +27,6 @@ fi # replace perl -0777 -i -pe 's|pub const DurationOfStartCall: u64 = prod_or_fast!\(7 \* 24 \* 60 \* 60 / 12, 10\);|pub const DurationOfStartCall: u64 = prod_or_fast!(5, 10);|' "$DurationOfStartCall" perl -0777 -i -pe 's|pub fn DefaultPendingCooldown\(\) -> u64 \{\s*prod_or_fast!\(7_200, 15\)\s*\}|pub fn DefaultPendingCooldown() -> u64 {\n prod_or_fast!(15, 15)\n }|g' "$DefaultPendingCooldown" -perl -0777 -i -pe 's|TransactionType::SetChildren => 150, // 30 minutes|TransactionType::SetChildren => 15, // 3 min|' "$SetChildren" +perl -0777 -i -pe 's|Self::SetChildren => 150, // 30 minutes|Self::SetChildren => 15, // 3 min|' "$SetChildren" echo "Patch applied successfully." From e3e377ffb1dd0432117f2ecfb482d3b7424dd356 Mon Sep 17 00:00:00 2001 From: Aliaksandr Tsurko Date: Mon, 15 Sep 2025 23:22:18 +0300 Subject: [PATCH 9/9] Remove owner hparam to epochs migration --- pallets/subtensor/src/macros/hooks.rs | 2 -- .../migrate_owner_hparam_rl_to_epochs.rs | 36 ------------------- pallets/subtensor/src/migrations/mod.rs | 1 - 3 files changed, 39 deletions(-) delete mode 100644 pallets/subtensor/src/migrations/migrate_owner_hparam_rl_to_epochs.rs diff --git a/pallets/subtensor/src/macros/hooks.rs b/pallets/subtensor/src/macros/hooks.rs index cbb72eed1f..b43f9422df 100644 --- a/pallets/subtensor/src/macros/hooks.rs +++ b/pallets/subtensor/src/macros/hooks.rs @@ -138,8 +138,6 @@ mod hooks { .saturating_add(migrations::migrate_fix_root_tao_and_alpha_in::migrate_fix_root_tao_and_alpha_in::()) // Migrate last block rate limiting storage items .saturating_add(migrations::migrate_rate_limiting_last_blocks::migrate_obsolete_rate_limiting_last_blocks_storage::()) - // Remove deprecated OwnerHyperparamRateLimit storage item - .saturating_add(migrations::migrate_owner_hparam_rl_to_epochs::migrate_owner_hyperparam_rl_to_epochs::()) // Migrate remove network modality .saturating_add(migrations::migrate_remove_network_modality::migrate_remove_network_modality::()); weight diff --git a/pallets/subtensor/src/migrations/migrate_owner_hparam_rl_to_epochs.rs b/pallets/subtensor/src/migrations/migrate_owner_hparam_rl_to_epochs.rs deleted file mode 100644 index 236bb10d5f..0000000000 --- a/pallets/subtensor/src/migrations/migrate_owner_hparam_rl_to_epochs.rs +++ /dev/null @@ -1,36 +0,0 @@ -use super::*; -use crate::HasMigrationRun; -use codec::Decode; -use frame_support::weights::Weight; -use sp_io::hashing::twox_128; -use sp_io::storage::get; - -/// Migrate u64 to u16 in OwnerHyperparamRateLimit and new default -pub fn migrate_owner_hyperparam_rl_to_epochs() -> Weight { - let migration_name = b"migrate_owner_hyperparam_rl_to_epochs".to_vec(); - let mut weight = T::DbWeight::get().reads(1); - - if HasMigrationRun::::get(&migration_name) { - log::info!("Migration '{migration_name:?}' already executed. Skipping."); - return weight; - } - - let pallet_name = twox_128("SubtensorModule".as_bytes()); - let storage_name = twox_128("OwnerHyperparamRateLimit".as_bytes()); - let full_key = [pallet_name, storage_name].concat(); - - if let Some(value_bytes) = get(&full_key) { - if let Ok(old_limit_blocks) = ::decode(&mut &value_bytes[..]) { - if old_limit_blocks == 0u64 { - // Preserve disabled state - Pallet::::set_owner_hyperparam_rate_limit(0); - } - } - - weight = weight.saturating_add(T::DbWeight::get().writes(1)); - } - - HasMigrationRun::::insert(&migration_name, true); - weight = weight.saturating_add(T::DbWeight::get().writes(1)); - weight -} diff --git a/pallets/subtensor/src/migrations/mod.rs b/pallets/subtensor/src/migrations/mod.rs index 9f0a6bce7f..b7265cc6d0 100644 --- a/pallets/subtensor/src/migrations/mod.rs +++ b/pallets/subtensor/src/migrations/mod.rs @@ -20,7 +20,6 @@ pub mod migrate_fix_root_tao_and_alpha_in; pub mod migrate_identities_v2; pub mod migrate_init_total_issuance; pub mod migrate_orphaned_storage_items; -pub mod migrate_owner_hparam_rl_to_epochs; pub mod migrate_populate_owned_hotkeys; pub mod migrate_rao; pub mod migrate_rate_limiting_last_blocks;