Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feat/sn owner hotkey change #1350

Merged
merged 5 commits into from
Feb 27, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 30 additions & 0 deletions pallets/admin-utils/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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 subnet owner.
/// * `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<T>,
netuid: u16,
hotkey: T::AccountId,
) -> DispatchResult {
pallet_subtensor::Pallet::<T>::ensure_subnet_owner(origin.clone(), netuid)?;
pallet_subtensor::Pallet::<T>::set_subnet_owner_hotkey(netuid, &hotkey);

log::debug!(
"SubnetOwnerHotkeySet( netuid: {:?}, hotkey: {:?} )",
netuid,
hotkey
);
Ok(())
}
}
}

Expand Down
43 changes: 43 additions & 0 deletions pallets/admin-utils/src/tests/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1466,3 +1466,46 @@ fn test_sudo_root_sets_subnet_moving_alpha() {
assert_eq!(pallet_subtensor::SubnetMovingAlpha::<Test>::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 = <<Test as Config>::RuntimeOrigin>::signed(coldkey);
let root = RuntimeOrigin::root();
let random_account = RuntimeOrigin::signed(U256::from(123456));

pallet_subtensor::SubnetOwner::<Test>::insert(netuid, coldkey);
pallet_subtensor::SubnetOwnerHotkey::<Test>::insert(netuid, hotkey);
assert_eq!(
pallet_subtensor::SubnetOwnerHotkey::<Test>::get(netuid),
hotkey
);

assert_ok!(AdminUtils::sudo_set_subnet_owner_hotkey(
coldkey_origin,
netuid,
new_hotkey
));

assert_eq!(
pallet_subtensor::SubnetOwnerHotkey::<Test>::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
);
});
}
6 changes: 6 additions & 0 deletions pallets/subtensor/src/macros/events.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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),
}
}
27 changes: 26 additions & 1 deletion pallets/subtensor/src/utils/misc.rs
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -23,6 +23,15 @@ impl<T: Config> Pallet<T> {
}
}

pub fn ensure_subnet_owner(o: T::RuntimeOrigin, netuid: u16) -> Result<(), DispatchError> {
let coldkey = ensure_signed(o);
match coldkey {
Ok(who) if SubnetOwner::<T>::get(netuid) == who => Ok(()),
Ok(_) => Err(DispatchError::BadOrigin),
Err(x) => Err(x.into()),
}
}

// ========================
// ==== Global Setters ====
// ========================
Expand Down Expand Up @@ -743,4 +752,20 @@ impl<T: Config> Pallet<T> {
DissolveNetworkScheduleDuration::<T>::set(duration);
Self::deposit_event(Event::DissolveNetworkScheduleDurationSet(duration));
}

/// 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::<T>::insert(netuid, hotkey.clone());
Self::deposit_event(Event::SubnetOwnerHotkeySet(netuid, hotkey.clone()));
}
}
Loading