Skip to content

Commit 8f1d998

Browse files
authored
Merge pull request #1350 from opentensor/feat/sn-owner-hotkey-change
Feat/sn owner hotkey change
2 parents 61b127a + ad81749 commit 8f1d998

File tree

4 files changed

+105
-1
lines changed

4 files changed

+105
-1
lines changed

pallets/admin-utils/src/lib.rs

+30
Original file line numberDiff line numberDiff line change
@@ -1399,6 +1399,36 @@ pub mod pallet {
13991399
log::debug!("SubnetMovingAlphaSet( alpha: {:?} )", alpha);
14001400
Ok(())
14011401
}
1402+
1403+
/// Change the SubnetOwnerHotkey for a given subnet.
1404+
///
1405+
/// # Arguments
1406+
/// * `origin` - The origin of the call, which must be the subnet owner.
1407+
/// * `netuid` - The unique identifier for the subnet.
1408+
/// * `hotkey` - The new hotkey for the subnet owner.
1409+
///
1410+
/// # Errors
1411+
/// * `BadOrigin` - If the caller is not the subnet owner or root account.
1412+
///
1413+
/// # Weight
1414+
/// Weight is handled by the `#[pallet::weight]` attribute.
1415+
#[pallet::call_index(64)]
1416+
#[pallet::weight((0, DispatchClass::Operational, Pays::No))]
1417+
pub fn sudo_set_subnet_owner_hotkey(
1418+
origin: OriginFor<T>,
1419+
netuid: u16,
1420+
hotkey: T::AccountId,
1421+
) -> DispatchResult {
1422+
pallet_subtensor::Pallet::<T>::ensure_subnet_owner(origin.clone(), netuid)?;
1423+
pallet_subtensor::Pallet::<T>::set_subnet_owner_hotkey(netuid, &hotkey);
1424+
1425+
log::debug!(
1426+
"SubnetOwnerHotkeySet( netuid: {:?}, hotkey: {:?} )",
1427+
netuid,
1428+
hotkey
1429+
);
1430+
Ok(())
1431+
}
14021432
}
14031433
}
14041434

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

+43
Original file line numberDiff line numberDiff line change
@@ -1466,3 +1466,46 @@ fn test_sudo_root_sets_subnet_moving_alpha() {
14661466
assert_eq!(pallet_subtensor::SubnetMovingAlpha::<Test>::get(), alpha);
14671467
});
14681468
}
1469+
1470+
#[test]
1471+
fn test_sudo_set_subnet_owner_hotkey() {
1472+
new_test_ext().execute_with(|| {
1473+
let netuid: u16 = 1;
1474+
1475+
let coldkey: U256 = U256::from(1);
1476+
let hotkey: U256 = U256::from(2);
1477+
let new_hotkey: U256 = U256::from(3);
1478+
1479+
let coldkey_origin = <<Test as Config>::RuntimeOrigin>::signed(coldkey);
1480+
let root = RuntimeOrigin::root();
1481+
let random_account = RuntimeOrigin::signed(U256::from(123456));
1482+
1483+
pallet_subtensor::SubnetOwner::<Test>::insert(netuid, coldkey);
1484+
pallet_subtensor::SubnetOwnerHotkey::<Test>::insert(netuid, hotkey);
1485+
assert_eq!(
1486+
pallet_subtensor::SubnetOwnerHotkey::<Test>::get(netuid),
1487+
hotkey
1488+
);
1489+
1490+
assert_ok!(AdminUtils::sudo_set_subnet_owner_hotkey(
1491+
coldkey_origin,
1492+
netuid,
1493+
new_hotkey
1494+
));
1495+
1496+
assert_eq!(
1497+
pallet_subtensor::SubnetOwnerHotkey::<Test>::get(netuid),
1498+
new_hotkey
1499+
);
1500+
1501+
assert_noop!(
1502+
AdminUtils::sudo_set_subnet_owner_hotkey(random_account, netuid, new_hotkey),
1503+
DispatchError::BadOrigin
1504+
);
1505+
1506+
assert_noop!(
1507+
AdminUtils::sudo_set_subnet_owner_hotkey(root, netuid, new_hotkey),
1508+
DispatchError::BadOrigin
1509+
);
1510+
});
1511+
}

pallets/subtensor/src/macros/events.rs

+6
Original file line numberDiff line numberDiff line change
@@ -271,5 +271,11 @@ mod events {
271271
/// Parameters:
272272
/// (netuid, bool)
273273
TransferToggle(u16, bool),
274+
275+
/// The owner hotkey for a subnet has been set.
276+
///
277+
/// Parameters:
278+
/// (netuid, new_hotkey)
279+
SubnetOwnerHotkeySet(u16, T::AccountId),
274280
}
275281
}

pallets/subtensor/src/utils/misc.rs

+26-1
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
use super::*;
22
use crate::{
33
Error,
4-
system::{ensure_root, ensure_signed_or_root, pallet_prelude::BlockNumberFor},
4+
system::{ensure_root, ensure_signed, ensure_signed_or_root, pallet_prelude::BlockNumberFor},
55
};
66
use safe_math::*;
77
use sp_core::Get;
@@ -23,6 +23,15 @@ impl<T: Config> Pallet<T> {
2323
}
2424
}
2525

26+
pub fn ensure_subnet_owner(o: T::RuntimeOrigin, netuid: u16) -> Result<(), DispatchError> {
27+
let coldkey = ensure_signed(o);
28+
match coldkey {
29+
Ok(who) if SubnetOwner::<T>::get(netuid) == who => Ok(()),
30+
Ok(_) => Err(DispatchError::BadOrigin),
31+
Err(x) => Err(x.into()),
32+
}
33+
}
34+
2635
// ========================
2736
// ==== Global Setters ====
2837
// ========================
@@ -743,4 +752,20 @@ impl<T: Config> Pallet<T> {
743752
DissolveNetworkScheduleDuration::<T>::set(duration);
744753
Self::deposit_event(Event::DissolveNetworkScheduleDurationSet(duration));
745754
}
755+
756+
/// Set the owner hotkey for a subnet.
757+
///
758+
/// # Arguments
759+
///
760+
/// * `netuid` - The unique identifier for the subnet.
761+
/// * `hotkey` - The new hotkey for the subnet owner.
762+
///
763+
/// # Effects
764+
///
765+
/// * Update the SubnetOwnerHotkey storage.
766+
/// * Emits a SubnetOwnerHotkeySet event.
767+
pub fn set_subnet_owner_hotkey(netuid: u16, hotkey: &T::AccountId) {
768+
SubnetOwnerHotkey::<T>::insert(netuid, hotkey.clone());
769+
Self::deposit_event(Event::SubnetOwnerHotkeySet(netuid, hotkey.clone()));
770+
}
746771
}

0 commit comments

Comments
 (0)