Skip to content
33 changes: 30 additions & 3 deletions pallets/admin-utils/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -509,10 +509,14 @@ pub mod pallet {
/// The extrinsic will call the Subtensor pallet to set the kappa.
#[pallet::call_index(16)]
#[pallet::weight(Weight::from_parts(16_740_000, 0)
.saturating_add(<T as frame_system::Config>::DbWeight::get().reads(1_u64))
.saturating_add(<T as frame_system::Config>::DbWeight::get().reads(2_u64))
.saturating_add(<T as frame_system::Config>::DbWeight::get().writes(1_u64)))]
pub fn sudo_set_kappa(origin: OriginFor<T>, netuid: NetUid, kappa: u16) -> DispatchResult {
pallet_subtensor::Pallet::<T>::ensure_subnet_owner_or_root(origin, netuid)?;
if pallet_subtensor::Pallet::<T>::get_kappa_can_set_by_owner(netuid) {
pallet_subtensor::Pallet::<T>::ensure_subnet_owner_or_root(origin, netuid)?;
} else {
ensure_root(origin)?;
}

ensure!(
pallet_subtensor::Pallet::<T>::if_subnet_exist(netuid),
Expand Down Expand Up @@ -1221,7 +1225,7 @@ pub mod pallet {
/// # Weight
/// Weight is handled by the `#[pallet::weight]` attribute.
#[pallet::call_index(57)]
#[pallet::weight(Weight::from_parts(19_320_000, 0)
#[pallet::weight(Weight::from_parts(15_130_000, 0)
.saturating_add(<T as frame_system::Config>::DbWeight::get().reads(1_u64))
.saturating_add(<T as frame_system::Config>::DbWeight::get().writes(1_u64)))]
pub fn sudo_set_commit_reveal_weights_interval(
Expand Down Expand Up @@ -1623,6 +1627,29 @@ pub mod pallet {
log::debug!("CKBurnSet( burn: {burn:?} ) ");
Ok(())
}
/// The extrinsic sets the kappa for a subnet.
/// It is only callable by the root account or subnet owner.
/// The extrinsic will call the Subtensor pallet to set the kappa.
#[pallet::call_index(74)]
#[pallet::weight(Weight::from_parts(16_740_000, 0)
.saturating_add(<T as frame_system::Config>::DbWeight::get().reads(1_u64))
.saturating_add(<T as frame_system::Config>::DbWeight::get().writes(1_u64)))]
pub fn sudo_set_kappa_can_set_by_owner(
origin: OriginFor<T>,
netuid: NetUid,
kappa_can_set_by_owner: bool,
) -> DispatchResult {
ensure_root(origin)?;

pallet_subtensor::Pallet::<T>::set_kappa_can_set_by_owner(
netuid,
kappa_can_set_by_owner,
);
log::debug!(
"KappaCanSetByOwnerSet( netuid: {netuid:?} kappaCanSet: {kappa_can_set_by_owner:?} ) "
);
Ok(())
}
}
}

Expand Down
62 changes: 62 additions & 0 deletions pallets/admin-utils/src/tests/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -585,6 +585,7 @@ fn test_sudo_set_kappa() {
let to_be_set: u16 = 10;
add_network(netuid, 10);
let init_value: u16 = SubtensorModule::get_kappa(netuid);
let can_set_by_owner: bool = SubtensorModule::get_kappa_can_set_by_owner(netuid);
assert_eq!(
AdminUtils::sudo_set_kappa(
<<Test as Config>::RuntimeOrigin>::signed(U256::from(1)),
Expand All @@ -601,6 +602,8 @@ fn test_sudo_set_kappa() {
),
Err(Error::<Test>::SubnetDoesNotExist.into())
);

assert!(can_set_by_owner);
assert_eq!(SubtensorModule::get_kappa(netuid), init_value);
assert_ok!(AdminUtils::sudo_set_kappa(
<<Test as Config>::RuntimeOrigin>::root(),
Expand All @@ -611,6 +614,65 @@ fn test_sudo_set_kappa() {
});
}

#[test]
fn test_subnet_owner_set_kappa() {
new_test_ext().execute_with(|| {
let netuid = NetUid::from(1);
let to_be_set: u16 = 10;
add_network(netuid, 10);

let owner = pallet_subtensor::pallet::SubnetOwner::<Test>::get(netuid);
let init_value: u16 = SubtensorModule::get_kappa(netuid);
let can_set_by_owner: bool = SubtensorModule::get_kappa_can_set_by_owner(netuid);
assert_eq!(
AdminUtils::sudo_set_kappa(
<<Test as Config>::RuntimeOrigin>::signed(U256::from(1)),
netuid,
to_be_set
),
Err(DispatchError::BadOrigin)
);
assert_eq!(
AdminUtils::sudo_set_kappa(
<<Test as Config>::RuntimeOrigin>::root(),
netuid.next(),
to_be_set
),
Err(Error::<Test>::SubnetDoesNotExist.into())
);

assert!(can_set_by_owner);
assert_eq!(SubtensorModule::get_kappa(netuid), init_value);
assert_ok!(AdminUtils::sudo_set_kappa(
<<Test as Config>::RuntimeOrigin>::signed(owner),
netuid,
to_be_set
));
assert_eq!(SubtensorModule::get_kappa(netuid), to_be_set);

assert_ok!(AdminUtils::sudo_set_kappa_can_set_by_owner(
<<Test as Config>::RuntimeOrigin>::root(),
netuid,
false
));
assert!(!SubtensorModule::get_kappa_can_set_by_owner(netuid));

assert_eq!(
AdminUtils::sudo_set_kappa(
<<Test as Config>::RuntimeOrigin>::signed(owner),
netuid,
to_be_set
),
Err(DispatchError::BadOrigin)
);
assert_ok!(AdminUtils::sudo_set_kappa(
<<Test as Config>::RuntimeOrigin>::root(),
netuid,
to_be_set
));
});
}

#[test]
fn test_sudo_set_rho() {
new_test_ext().execute_with(|| {
Expand Down
2 changes: 1 addition & 1 deletion pallets/drand/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -332,7 +332,7 @@ pub mod pallet {
impl<T: Config> Pallet<T> {
/// Verify and write a pulse from the beacon into the runtime
#[pallet::call_index(0)]
#[pallet::weight(Weight::from_parts(4_294_000_000, 0)
#[pallet::weight(Weight::from_parts(5_228_000_000, 0)
.saturating_add(T::DbWeight::get().reads(3_u64))
.saturating_add(T::DbWeight::get().writes(4_u64)))]
pub fn write_pulse(
Expand Down
4 changes: 4 additions & 0 deletions pallets/subtensor/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1474,6 +1474,10 @@ pub mod pallet {
pub type SubtokenEnabled<T> =
StorageMap<_, Identity, NetUid, bool, ValueQuery, DefaultFalse<T>>;

#[pallet::storage]
/// --- MAP ( netuid ) --> If Kappa can be set by owner
pub type KappaCanSetByOwner<T> =
StorageMap<_, Identity, NetUid, bool, ValueQuery, DefaultTrue<T>>;
#[pallet::type_value]
/// Default value for burn keys limit
pub fn DefaultImmuneOwnerUidsLimit<T: Config>() -> u16 {
Expand Down
6 changes: 3 additions & 3 deletions pallets/subtensor/src/macros/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ mod config {
{
/// call type
type RuntimeCall: Parameter
+ Dispatchable<RuntimeOrigin = Self::RuntimeOrigin>
+ Dispatchable<RuntimeOrigin = <Self as frame_system::Config>::RuntimeOrigin>
+ From<Call<Self>>
+ IsType<<Self as frame_system::Config>::RuntimeCall>
+ From<frame_system::Call<Self>>;
Expand All @@ -25,11 +25,11 @@ mod config {

/// A sudo-able call.
type SudoRuntimeCall: Parameter
+ UnfilteredDispatchable<RuntimeOrigin = Self::RuntimeOrigin>
+ UnfilteredDispatchable<RuntimeOrigin = <Self as frame_system::Config>::RuntimeOrigin>
+ GetDispatchInfo;

/// Origin checking for council majority
type CouncilOrigin: EnsureOrigin<Self::RuntimeOrigin>;
type CouncilOrigin: EnsureOrigin<<Self as frame_system::Config>::RuntimeOrigin>;

/// Currency type that will be used to place deposits on neurons
type Currency: fungible::Balanced<Self::AccountId, Balance = u64>
Expand Down
2 changes: 1 addition & 1 deletion pallets/subtensor/src/macros/dispatches.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2031,7 +2031,7 @@ mod dispatches {
/// * commit_reveal_version (`u16`):
/// - The client (bittensor-drand) version
#[pallet::call_index(113)]
#[pallet::weight((Weight::from_parts(64_530_000, 0)
#[pallet::weight((Weight::from_parts(79_930_000, 0)
.saturating_add(T::DbWeight::get().reads(7_u64))
.saturating_add(T::DbWeight::get().writes(2)), DispatchClass::Normal, Pays::No))]
pub fn commit_timelocked_weights(
Expand Down
17 changes: 17 additions & 0 deletions pallets/subtensor/src/utils/misc.rs
Original file line number Diff line number Diff line change
Expand Up @@ -248,6 +248,10 @@ impl<T: Config> Pallet<T> {
BlockAtRegistration::<T>::get(netuid, neuron_uid)
}

pub fn get_kappa_can_set_by_owner(netuid: NetUid) -> bool {
KappaCanSetByOwner::<T>::get(netuid)
}

Comment on lines +252 to +254
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not sure if this is needed, the code called inside the function is explicit enough?

// ========================
// ===== Take checks ======
// ========================
Expand Down Expand Up @@ -805,6 +809,19 @@ impl<T: Config> Pallet<T> {
}
}

/// Set if Kappa can be set by owner
///
/// # Arguments
///
/// * `netuid` - The unique identifier for the subnet.
/// * `can_set` - Whether Kappa can be set by owner.
///
/// # Effects
///
/// * Update the KappaCanSetByOwner storage.
pub fn set_kappa_can_set_by_owner(netuid: NetUid, can_set: bool) {
KappaCanSetByOwner::<T>::insert(netuid, can_set);
}
/// Set the per-subnet limit (for the given `netuid`) on the number of **owner-immune**
/// neurons (UIDs).
///
Expand Down
16 changes: 5 additions & 11 deletions precompiles/src/subnet.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ use core::marker::PhantomData;
use frame_support::dispatch::{GetDispatchInfo, PostDispatchInfo};
use frame_support::traits::ConstU32;
use frame_system::RawOrigin;
use pallet_evm::{AddressMapping, PrecompileHandle};
use pallet_evm::{AddressMapping, ExitError, PrecompileFailure, PrecompileHandle};
use precompile_utils::{EvmResult, prelude::BoundedString};
use sp_core::H256;
use sp_runtime::traits::Dispatchable;
Expand Down Expand Up @@ -373,16 +373,10 @@ where

#[precompile::public("setKappa(uint16,uint16)")]
#[precompile::payable]
fn set_kappa(handle: &mut impl PrecompileHandle, netuid: u16, kappa: u16) -> EvmResult<()> {
let call = pallet_admin_utils::Call::<R>::sudo_set_kappa {
netuid: netuid.into(),
kappa,
};

handle.try_dispatch_runtime_call::<R, _>(
call,
RawOrigin::Signed(handle.caller_account_id::<R>()),
)
fn set_kappa(_handle: &mut impl PrecompileHandle, _netuid: u16, _kappa: u16) -> EvmResult<()> {
Err(PrecompileFailure::Error {
exit_status: ExitError::Other("Bad origin".into()),
})
}

#[precompile::public("getRho(uint16)")]
Expand Down