Skip to content

Commit 1b43926

Browse files
committed
add test cases
1 parent ef5f1a7 commit 1b43926

File tree

5 files changed

+117
-4
lines changed

5 files changed

+117
-4
lines changed

pallets/admin-utils/src/lib.rs

Lines changed: 29 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -512,7 +512,11 @@ pub mod pallet {
512512
.saturating_add(<T as frame_system::Config>::DbWeight::get().reads(1_u64))
513513
.saturating_add(<T as frame_system::Config>::DbWeight::get().writes(1_u64)))]
514514
pub fn sudo_set_kappa(origin: OriginFor<T>, netuid: NetUid, kappa: u16) -> DispatchResult {
515-
ensure_root(origin)?;
515+
if pallet_subtensor::Pallet::<T>::get_kappa_can_set_by_owner(netuid) {
516+
pallet_subtensor::Pallet::<T>::ensure_subnet_owner_or_root(origin, netuid)?;
517+
} else {
518+
ensure_root(origin)?;
519+
}
516520

517521
ensure!(
518522
pallet_subtensor::Pallet::<T>::if_subnet_exist(netuid),
@@ -1673,6 +1677,30 @@ pub mod pallet {
16731677
pallet_subtensor::Pallet::<T>::set_commit_reveal_weights_version(version);
16741678
Ok(())
16751679
}
1680+
1681+
/// The extrinsic sets the kappa for a subnet.
1682+
/// It is only callable by the root account or subnet owner.
1683+
/// The extrinsic will call the Subtensor pallet to set the kappa.
1684+
#[pallet::call_index(72)]
1685+
#[pallet::weight(Weight::from_parts(16_740_000, 0)
1686+
.saturating_add(<T as frame_system::Config>::DbWeight::get().reads(1_u64))
1687+
.saturating_add(<T as frame_system::Config>::DbWeight::get().writes(1_u64)))]
1688+
pub fn sudo_set_kappa_can_set_by_owner(
1689+
origin: OriginFor<T>,
1690+
netuid: NetUid,
1691+
kappa_can_set_by_owner: bool,
1692+
) -> DispatchResult {
1693+
ensure_root(origin)?;
1694+
1695+
pallet_subtensor::Pallet::<T>::set_kappa_can_set_by_owner(
1696+
netuid,
1697+
kappa_can_set_by_owner,
1698+
);
1699+
log::debug!(
1700+
"KappaCanSetByOwnerSet( netuid: {netuid:?} kappaCanSet: {kappa_can_set_by_owner:?} ) "
1701+
);
1702+
Ok(())
1703+
}
16761704
}
16771705
}
16781706

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

Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -585,6 +585,7 @@ fn test_sudo_set_kappa() {
585585
let to_be_set: u16 = 10;
586586
add_network(netuid, 10);
587587
let init_value: u16 = SubtensorModule::get_kappa(netuid);
588+
let can_set_by_owner: bool = SubtensorModule::get_kappa_can_set_by_owner(netuid);
588589
assert_eq!(
589590
AdminUtils::sudo_set_kappa(
590591
<<Test as Config>::RuntimeOrigin>::signed(U256::from(1)),
@@ -601,6 +602,8 @@ fn test_sudo_set_kappa() {
601602
),
602603
Err(Error::<Test>::SubnetDoesNotExist.into())
603604
);
605+
606+
assert_eq!(can_set_by_owner, true);
604607
assert_eq!(SubtensorModule::get_kappa(netuid), init_value);
605608
assert_ok!(AdminUtils::sudo_set_kappa(
606609
<<Test as Config>::RuntimeOrigin>::root(),
@@ -611,6 +614,65 @@ fn test_sudo_set_kappa() {
611614
});
612615
}
613616

617+
#[test]
618+
fn test_subnet_owner_set_kappa() {
619+
new_test_ext().execute_with(|| {
620+
let netuid = NetUid::from(1);
621+
let to_be_set: u16 = 10;
622+
add_network(netuid, 10);
623+
624+
let owner = pallet_subtensor::pallet::SubnetOwner::<Test>::get(netuid);
625+
let init_value: u16 = SubtensorModule::get_kappa(netuid);
626+
let can_set_by_owner: bool = SubtensorModule::get_kappa_can_set_by_owner(netuid);
627+
assert_eq!(
628+
AdminUtils::sudo_set_kappa(
629+
<<Test as Config>::RuntimeOrigin>::signed(U256::from(1)),
630+
netuid,
631+
to_be_set
632+
),
633+
Err(DispatchError::BadOrigin)
634+
);
635+
assert_eq!(
636+
AdminUtils::sudo_set_kappa(
637+
<<Test as Config>::RuntimeOrigin>::root(),
638+
netuid.next(),
639+
to_be_set
640+
),
641+
Err(Error::<Test>::SubnetDoesNotExist.into())
642+
);
643+
644+
assert_eq!(can_set_by_owner, true);
645+
assert_eq!(SubtensorModule::get_kappa(netuid), init_value);
646+
assert_ok!(AdminUtils::sudo_set_kappa(
647+
<<Test as Config>::RuntimeOrigin>::signed(owner),
648+
netuid,
649+
to_be_set
650+
));
651+
assert_eq!(SubtensorModule::get_kappa(netuid), to_be_set);
652+
653+
assert_ok!(AdminUtils::sudo_set_kappa_can_set_by_owner(
654+
<<Test as Config>::RuntimeOrigin>::root(),
655+
netuid,
656+
false
657+
));
658+
assert_eq!(SubtensorModule::get_kappa_can_set_by_owner(netuid), false);
659+
660+
assert_eq!(
661+
AdminUtils::sudo_set_kappa(
662+
<<Test as Config>::RuntimeOrigin>::signed(owner),
663+
netuid,
664+
to_be_set
665+
),
666+
Err(DispatchError::BadOrigin)
667+
);
668+
assert_ok!(AdminUtils::sudo_set_kappa(
669+
<<Test as Config>::RuntimeOrigin>::root(),
670+
netuid,
671+
to_be_set
672+
));
673+
});
674+
}
675+
614676
#[test]
615677
fn test_sudo_set_rho() {
616678
new_test_ext().execute_with(|| {

pallets/subtensor/src/lib.rs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1464,6 +1464,11 @@ pub mod pallet {
14641464
pub type SubtokenEnabled<T> =
14651465
StorageMap<_, Identity, NetUid, bool, ValueQuery, DefaultFalse<T>>;
14661466

1467+
#[pallet::storage]
1468+
/// --- MAP ( netuid ) --> If Kappa can be set by owner
1469+
pub type KappaCanSetByOwner<T> =
1470+
StorageMap<_, Identity, NetUid, bool, ValueQuery, DefaultTrue<T>>;
1471+
14671472
/// =======================================
14681473
/// ==== Subnetwork Consensus Storage ====
14691474
/// =======================================

pallets/subtensor/src/macros/config.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ mod config {
1515
{
1616
/// call type
1717
type RuntimeCall: Parameter
18-
+ Dispatchable<RuntimeOrigin = Self::RuntimeOrigin>
18+
+ Dispatchable<RuntimeOrigin = <Self as frame_system::Config>::RuntimeOrigin>
1919
+ From<Call<Self>>
2020
+ IsType<<Self as frame_system::Config>::RuntimeCall>
2121
+ From<frame_system::Call<Self>>;
@@ -25,11 +25,11 @@ mod config {
2525

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

3131
/// Origin checking for council majority
32-
type CouncilOrigin: EnsureOrigin<Self::RuntimeOrigin>;
32+
type CouncilOrigin: EnsureOrigin<<Self as frame_system::Config>::RuntimeOrigin>;
3333

3434
/// Currency type that will be used to place deposits on neurons
3535
type Currency: fungible::Balanced<Self::AccountId, Balance = u64>

pallets/subtensor/src/utils/misc.rs

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -248,6 +248,10 @@ impl<T: Config> Pallet<T> {
248248
BlockAtRegistration::<T>::get(netuid, neuron_uid)
249249
}
250250

251+
pub fn get_kappa_can_set_by_owner(netuid: NetUid) -> bool {
252+
KappaCanSetByOwner::<T>::get(netuid)
253+
}
254+
251255
// ========================
252256
// ===== Take checks ======
253257
// ========================
@@ -804,4 +808,18 @@ impl<T: Config> Pallet<T> {
804808
Err(_) => None,
805809
}
806810
}
811+
812+
/// Set if Kappa can be set by owner
813+
///
814+
/// # Arguments
815+
///
816+
/// * `netuid` - The unique identifier for the subnet.
817+
/// * `can_set` - Whether Kappa can be set by owner.
818+
///
819+
/// # Effects
820+
///
821+
/// * Update the KappaCanSetByOwner storage.
822+
pub fn set_kappa_can_set_by_owner(netuid: NetUid, can_set: bool) {
823+
KappaCanSetByOwner::<T>::insert(netuid, can_set);
824+
}
807825
}

0 commit comments

Comments
 (0)