From 0b057ba7d1f3c8557752ffe0b77be98378e70a22 Mon Sep 17 00:00:00 2001 From: Cameron Fairchild Date: Wed, 26 Feb 2025 19:03:03 -0500 Subject: [PATCH 1/4] allow setting the SN owner hotkey --- pallets/admin-utils/src/lib.rs | 30 ++++++++++++++++++++++++++ pallets/subtensor/src/macros/events.rs | 6 ++++++ pallets/subtensor/src/utils/misc.rs | 5 +++++ 3 files changed, 41 insertions(+) diff --git a/pallets/admin-utils/src/lib.rs b/pallets/admin-utils/src/lib.rs index 7bc9e986e..6e68a47c3 100644 --- a/pallets/admin-utils/src/lib.rs +++ b/pallets/admin-utils/src/lib.rs @@ -1399,6 +1399,36 @@ pub mod pallet { log::debug!("SubnetMovingAlphaSet( alpha: {:?} )", alpha); Ok(()) } + + /// Change the SubnetOwnerHotkey for a given subnet. + /// + /// # Arguments + /// * `origin` - The origin of the call, which must be the root account. + /// * `netuid` - The unique identifier for the subnet. + /// * `hotkey` - The new hotkey for the subnet owner. + /// + /// # Errors + /// * `BadOrigin` - If the caller is not the subnet owner or root account. + /// + /// # Weight + /// Weight is handled by the `#[pallet::weight]` attribute. + #[pallet::call_index(64)] + #[pallet::weight((0, DispatchClass::Operational, Pays::No))] + pub fn sudo_set_subnet_owner_hotkey( + origin: OriginFor, + netuid: u16, + hotkey: T::AccountId, + ) -> DispatchResult { + pallet_subtensor::Pallet::::ensure_subnet_owner_or_root(origin.clone(), netuid)?; + pallet_subtensor::Pallet::::set_subnet_owner_hotkey(netuid, hotkey); + + log::debug!( + "SubnetOwnerHotkeySet( netuid: {:?}, hotkey: {:?} )", + netuid, + hotkey + ); + Ok(()) + } } } diff --git a/pallets/subtensor/src/macros/events.rs b/pallets/subtensor/src/macros/events.rs index 40890b018..3290c343c 100644 --- a/pallets/subtensor/src/macros/events.rs +++ b/pallets/subtensor/src/macros/events.rs @@ -271,5 +271,11 @@ mod events { /// Parameters: /// (netuid, bool) TransferToggle(u16, bool), + + /// The owner hotkey for a subnet has been set. + /// + /// Parameters: + /// (netuid, new_hotkey) + SubnetOwnerHotkeySet(u16, T::AccountId), } } diff --git a/pallets/subtensor/src/utils/misc.rs b/pallets/subtensor/src/utils/misc.rs index bd093a76b..e6d5e5cc9 100644 --- a/pallets/subtensor/src/utils/misc.rs +++ b/pallets/subtensor/src/utils/misc.rs @@ -743,4 +743,9 @@ impl Pallet { DissolveNetworkScheduleDuration::::set(duration); Self::deposit_event(Event::DissolveNetworkScheduleDurationSet(duration)); } + + pub fn set_subnet_owner_hotkey(netuid: u16, hotkey: T::AccountId) { + SubnetOwnerHotkey::::insert(netuid, hotkey); + Self::deposit_event(Event::SubnetOwnerHotkeySet(netuid, hotkey)); + } } From 974b4771df57e0d0ae9e864f431ab16ec32d7aef Mon Sep 17 00:00:00 2001 From: Cameron Fairchild Date: Wed, 26 Feb 2025 19:18:40 -0500 Subject: [PATCH 2/4] docs and clippy --- pallets/admin-utils/src/lib.rs | 2 +- pallets/subtensor/src/utils/misc.rs | 17 ++++++++++++++--- 2 files changed, 15 insertions(+), 4 deletions(-) diff --git a/pallets/admin-utils/src/lib.rs b/pallets/admin-utils/src/lib.rs index 6e68a47c3..622a08c79 100644 --- a/pallets/admin-utils/src/lib.rs +++ b/pallets/admin-utils/src/lib.rs @@ -1420,7 +1420,7 @@ pub mod pallet { hotkey: T::AccountId, ) -> DispatchResult { pallet_subtensor::Pallet::::ensure_subnet_owner_or_root(origin.clone(), netuid)?; - pallet_subtensor::Pallet::::set_subnet_owner_hotkey(netuid, hotkey); + pallet_subtensor::Pallet::::set_subnet_owner_hotkey(netuid, &hotkey); log::debug!( "SubnetOwnerHotkeySet( netuid: {:?}, hotkey: {:?} )", diff --git a/pallets/subtensor/src/utils/misc.rs b/pallets/subtensor/src/utils/misc.rs index e6d5e5cc9..6af46549e 100644 --- a/pallets/subtensor/src/utils/misc.rs +++ b/pallets/subtensor/src/utils/misc.rs @@ -744,8 +744,19 @@ impl Pallet { Self::deposit_event(Event::DissolveNetworkScheduleDurationSet(duration)); } - pub fn set_subnet_owner_hotkey(netuid: u16, hotkey: T::AccountId) { - SubnetOwnerHotkey::::insert(netuid, hotkey); - Self::deposit_event(Event::SubnetOwnerHotkeySet(netuid, hotkey)); + /// Set the owner hotkey for a subnet. + /// + /// # Arguments + /// + /// * `netuid` - The unique identifier for the subnet. + /// * `hotkey` - The new hotkey for the subnet owner. + /// + /// # Effects + /// + /// * Update the SubnetOwnerHotkey storage. + /// * Emits a SubnetOwnerHotkeySet event. + pub fn set_subnet_owner_hotkey(netuid: u16, hotkey: &T::AccountId) { + SubnetOwnerHotkey::::insert(netuid, hotkey.clone()); + Self::deposit_event(Event::SubnetOwnerHotkeySet(netuid, hotkey.clone())); } } From d498f231bd9183c59bb26ae2ab3d2a248eb03075 Mon Sep 17 00:00:00 2001 From: Cameron Fairchild Date: Thu, 27 Feb 2025 17:15:44 -0500 Subject: [PATCH 3/4] only settable by SN owner --- pallets/admin-utils/src/lib.rs | 4 ++-- pallets/subtensor/src/utils/misc.rs | 11 ++++++++++- 2 files changed, 12 insertions(+), 3 deletions(-) diff --git a/pallets/admin-utils/src/lib.rs b/pallets/admin-utils/src/lib.rs index 622a08c79..fe2f102a7 100644 --- a/pallets/admin-utils/src/lib.rs +++ b/pallets/admin-utils/src/lib.rs @@ -1403,7 +1403,7 @@ pub mod pallet { /// Change the SubnetOwnerHotkey for a given subnet. /// /// # Arguments - /// * `origin` - The origin of the call, which must be the root account. + /// * `origin` - The origin of the call, which must be the subnet owner. /// * `netuid` - The unique identifier for the subnet. /// * `hotkey` - The new hotkey for the subnet owner. /// @@ -1419,7 +1419,7 @@ pub mod pallet { netuid: u16, hotkey: T::AccountId, ) -> DispatchResult { - pallet_subtensor::Pallet::::ensure_subnet_owner_or_root(origin.clone(), netuid)?; + pallet_subtensor::Pallet::::ensure_subnet_owner(origin.clone(), netuid)?; pallet_subtensor::Pallet::::set_subnet_owner_hotkey(netuid, &hotkey); log::debug!( diff --git a/pallets/subtensor/src/utils/misc.rs b/pallets/subtensor/src/utils/misc.rs index 6af46549e..bca9a456b 100644 --- a/pallets/subtensor/src/utils/misc.rs +++ b/pallets/subtensor/src/utils/misc.rs @@ -1,7 +1,7 @@ use super::*; use crate::{ Error, - system::{ensure_root, ensure_signed_or_root, pallet_prelude::BlockNumberFor}, + system::{ensure_root, ensure_signed, ensure_signed_or_root, pallet_prelude::BlockNumberFor}, }; use safe_math::*; use sp_core::Get; @@ -23,6 +23,15 @@ impl Pallet { } } + pub fn ensure_subnet_owner(o: T::RuntimeOrigin, netuid: u16) -> Result<(), DispatchError> { + let coldkey = ensure_signed(o); + match coldkey { + Ok(who) if SubnetOwner::::get(netuid) == who => Ok(()), + Ok(_) => Err(DispatchError::BadOrigin), + Err(x) => Err(x.into()), + } + } + // ======================== // ==== Global Setters ==== // ======================== From 99463837c8c96d9e53d05f5ec3e206297e0a5af4 Mon Sep 17 00:00:00 2001 From: Cameron Fairchild Date: Thu, 27 Feb 2025 17:20:45 -0500 Subject: [PATCH 4/4] add test for perms --- pallets/admin-utils/src/tests/mod.rs | 43 ++++++++++++++++++++++++++++ 1 file changed, 43 insertions(+) diff --git a/pallets/admin-utils/src/tests/mod.rs b/pallets/admin-utils/src/tests/mod.rs index 6c879635e..6f6cdad97 100644 --- a/pallets/admin-utils/src/tests/mod.rs +++ b/pallets/admin-utils/src/tests/mod.rs @@ -1466,3 +1466,46 @@ fn test_sudo_root_sets_subnet_moving_alpha() { assert_eq!(pallet_subtensor::SubnetMovingAlpha::::get(), alpha); }); } + +#[test] +fn test_sudo_set_subnet_owner_hotkey() { + new_test_ext().execute_with(|| { + let netuid: u16 = 1; + + let coldkey: U256 = U256::from(1); + let hotkey: U256 = U256::from(2); + let new_hotkey: U256 = U256::from(3); + + let coldkey_origin = <::RuntimeOrigin>::signed(coldkey); + let root = RuntimeOrigin::root(); + let random_account = RuntimeOrigin::signed(U256::from(123456)); + + pallet_subtensor::SubnetOwner::::insert(netuid, coldkey); + pallet_subtensor::SubnetOwnerHotkey::::insert(netuid, hotkey); + assert_eq!( + pallet_subtensor::SubnetOwnerHotkey::::get(netuid), + hotkey + ); + + assert_ok!(AdminUtils::sudo_set_subnet_owner_hotkey( + coldkey_origin, + netuid, + new_hotkey + )); + + assert_eq!( + pallet_subtensor::SubnetOwnerHotkey::::get(netuid), + new_hotkey + ); + + assert_noop!( + AdminUtils::sudo_set_subnet_owner_hotkey(random_account, netuid, new_hotkey), + DispatchError::BadOrigin + ); + + assert_noop!( + AdminUtils::sudo_set_subnet_owner_hotkey(root, netuid, new_hotkey), + DispatchError::BadOrigin + ); + }); +}