Skip to content
34 changes: 31 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 @@ -1296,7 +1300,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 @@ -1673,6 +1677,30 @@ pub mod pallet {
pallet_subtensor::Pallet::<T>::set_commit_reveal_weights_version(version);
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(72)]
#[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 @@ -331,7 +331,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
5 changes: 5 additions & 0 deletions pallets/subtensor/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1464,6 +1464,11 @@ 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>>;

/// =======================================
/// ==== Subnetwork Consensus Storage ====
/// =======================================
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
4 changes: 2 additions & 2 deletions pallets/subtensor/src/macros/dispatches.rs
Original file line number Diff line number Diff line change
Expand Up @@ -279,7 +279,7 @@ mod dispatches {
/// - Attempting to commit when the user has more than the allowed limit of unrevealed commits.
///
#[pallet::call_index(99)]
#[pallet::weight((Weight::from_parts(62_300_000, 0)
#[pallet::weight((Weight::from_parts(62_630_000, 0)
.saturating_add(T::DbWeight::get().reads(7_u64))
.saturating_add(T::DbWeight::get().writes(2)), DispatchClass::Normal, Pays::No))]
pub fn commit_crv3_weights(
Expand Down Expand Up @@ -693,7 +693,7 @@ mod dispatches {
/// - Attempting to set prometheus information withing the rate limit min.
///
#[pallet::call_index(4)]
#[pallet::weight((Weight::from_parts(36_090_000, 0)
#[pallet::weight((Weight::from_parts(33_530_000, 0)
.saturating_add(T::DbWeight::get().reads(4))
.saturating_add(T::DbWeight::get().writes(1)), DispatchClass::Normal, Pays::No))]
pub fn serve_axon(
Expand Down
18 changes: 18 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 @@ -804,4 +808,18 @@ impl<T: Config> Pallet<T> {
Err(_) => None,
}
}

/// 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);
}
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?

}
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
2 changes: 1 addition & 1 deletion runtime/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -215,7 +215,7 @@ pub const VERSION: RuntimeVersion = RuntimeVersion {
// `spec_version`, and `authoring_version` are the same between Wasm and native.
// This value is set to 100 to notify Polkadot-JS App (https://polkadot.js.org/apps) to use
// the compatible custom types.
spec_version: 302,
spec_version: 303,
impl_version: 1,
apis: RUNTIME_API_VERSIONS,
transaction_version: 1,
Expand Down