Skip to content

Commit e2a60a1

Browse files
authored
Merge pull request #745 from opentensor/junius-add-sudo-call-for-schedule
add sudo call to set duration for schedule call
2 parents 9e2686e + b4c1712 commit e2a60a1

File tree

9 files changed

+394
-23
lines changed

9 files changed

+394
-23
lines changed

pallets/admin-utils/src/lib.rs

Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ pub use pallet::*;
44
pub mod weights;
55
pub use weights::WeightInfo;
66

7+
use frame_system::pallet_prelude::BlockNumberFor;
78
use sp_runtime::{traits::Member, RuntimeAppPublic};
89

910
mod benchmarking;
@@ -1128,6 +1129,73 @@ pub mod pallet {
11281129

11291130
Ok(())
11301131
}
1132+
1133+
/// Sets the duration of the coldkey swap schedule.
1134+
///
1135+
/// This extrinsic allows the root account to set the duration for the coldkey swap schedule.
1136+
/// The coldkey swap schedule determines how long it takes for a coldkey swap operation to complete.
1137+
///
1138+
/// # Arguments
1139+
/// * `origin` - The origin of the call, which must be the root account.
1140+
/// * `duration` - The new duration for the coldkey swap schedule, in number of blocks.
1141+
///
1142+
/// # Errors
1143+
/// * `BadOrigin` - If the caller is not the root account.
1144+
///
1145+
/// # Weight
1146+
/// Weight is handled by the `#[pallet::weight]` attribute.
1147+
#[pallet::call_index(54)]
1148+
#[pallet::weight((0, DispatchClass::Operational, Pays::No))]
1149+
pub fn sudo_set_coldkey_swap_schedule_duration(
1150+
origin: OriginFor<T>,
1151+
duration: BlockNumberFor<T>,
1152+
) -> DispatchResult {
1153+
// Ensure the call is made by the root account
1154+
ensure_root(origin)?;
1155+
1156+
// Set the new duration of schedule coldkey swap
1157+
pallet_subtensor::Pallet::<T>::set_coldkey_swap_schedule_duration(duration);
1158+
1159+
// Log the change
1160+
log::trace!("ColdkeySwapScheduleDurationSet( duration: {:?} )", duration);
1161+
1162+
Ok(())
1163+
}
1164+
1165+
/// Sets the duration of the dissolve network schedule.
1166+
///
1167+
/// This extrinsic allows the root account to set the duration for the dissolve network schedule.
1168+
/// The dissolve network schedule determines how long it takes for a network dissolution operation to complete.
1169+
///
1170+
/// # Arguments
1171+
/// * `origin` - The origin of the call, which must be the root account.
1172+
/// * `duration` - The new duration for the dissolve network schedule, in number of blocks.
1173+
///
1174+
/// # Errors
1175+
/// * `BadOrigin` - If the caller is not the root account.
1176+
///
1177+
/// # Weight
1178+
/// Weight is handled by the `#[pallet::weight]` attribute.
1179+
#[pallet::call_index(55)]
1180+
#[pallet::weight((0, DispatchClass::Operational, Pays::No))]
1181+
pub fn sudo_set_dissolve_network_schedule_duration(
1182+
origin: OriginFor<T>,
1183+
duration: BlockNumberFor<T>,
1184+
) -> DispatchResult {
1185+
// Ensure the call is made by the root account
1186+
ensure_root(origin)?;
1187+
1188+
// Set the duration of schedule dissolve network
1189+
pallet_subtensor::Pallet::<T>::set_dissolve_network_schedule_duration(duration);
1190+
1191+
// Log the change
1192+
log::trace!(
1193+
"DissolveNetworkScheduleDurationSet( duration: {:?} )",
1194+
duration
1195+
);
1196+
1197+
Ok(())
1198+
}
11311199
}
11321200
}
11331201

pallets/admin-utils/tests/tests.rs

Lines changed: 75 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
use frame_support::sp_runtime::DispatchError;
22
use frame_support::{
3-
assert_err, assert_ok,
3+
assert_err, assert_noop, assert_ok,
44
dispatch::{DispatchClass, GetDispatchInfo, Pays},
55
};
66
use frame_system::Config;
@@ -1361,3 +1361,77 @@ fn test_sudo_get_set_alpha() {
13611361
));
13621362
});
13631363
}
1364+
1365+
#[test]
1366+
fn test_sudo_set_coldkey_swap_schedule_duration() {
1367+
new_test_ext().execute_with(|| {
1368+
// Arrange
1369+
let root = RuntimeOrigin::root();
1370+
let non_root = RuntimeOrigin::signed(U256::from(1));
1371+
let new_duration = 100u32.into();
1372+
1373+
// Act & Assert: Non-root account should fail
1374+
assert_noop!(
1375+
AdminUtils::sudo_set_coldkey_swap_schedule_duration(non_root, new_duration),
1376+
DispatchError::BadOrigin
1377+
);
1378+
1379+
// Act: Root account should succeed
1380+
assert_ok!(AdminUtils::sudo_set_coldkey_swap_schedule_duration(
1381+
root.clone(),
1382+
new_duration
1383+
));
1384+
1385+
// Assert: Check if the duration was actually set
1386+
assert_eq!(
1387+
pallet_subtensor::ColdkeySwapScheduleDuration::<Test>::get(),
1388+
new_duration
1389+
);
1390+
1391+
// Act & Assert: Setting the same value again should succeed (idempotent operation)
1392+
assert_ok!(AdminUtils::sudo_set_coldkey_swap_schedule_duration(
1393+
root,
1394+
new_duration
1395+
));
1396+
1397+
// You might want to check for events here if your pallet emits them
1398+
System::assert_last_event(Event::ColdkeySwapScheduleDurationSet(new_duration).into());
1399+
});
1400+
}
1401+
1402+
#[test]
1403+
fn test_sudo_set_dissolve_network_schedule_duration() {
1404+
new_test_ext().execute_with(|| {
1405+
// Arrange
1406+
let root = RuntimeOrigin::root();
1407+
let non_root = RuntimeOrigin::signed(U256::from(1));
1408+
let new_duration = 200u32.into();
1409+
1410+
// Act & Assert: Non-root account should fail
1411+
assert_noop!(
1412+
AdminUtils::sudo_set_dissolve_network_schedule_duration(non_root, new_duration),
1413+
DispatchError::BadOrigin
1414+
);
1415+
1416+
// Act: Root account should succeed
1417+
assert_ok!(AdminUtils::sudo_set_dissolve_network_schedule_duration(
1418+
root.clone(),
1419+
new_duration
1420+
));
1421+
1422+
// Assert: Check if the duration was actually set
1423+
assert_eq!(
1424+
pallet_subtensor::DissolveNetworkScheduleDuration::<Test>::get(),
1425+
new_duration
1426+
);
1427+
1428+
// Act & Assert: Setting the same value again should succeed (idempotent operation)
1429+
assert_ok!(AdminUtils::sudo_set_dissolve_network_schedule_duration(
1430+
root,
1431+
new_duration
1432+
));
1433+
1434+
// You might want to check for events here if your pallet emits them
1435+
System::assert_last_event(Event::DissolveNetworkScheduleDurationSet(new_duration).into());
1436+
});
1437+
}

pallets/subtensor/src/benchmarks.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -312,7 +312,7 @@ benchmarks! {
312312
let amount_to_be_staked = 100_000_000_000_000u64;
313313
Subtensor::<T>::add_balance_to_coldkey_account(&coldkey.clone(), amount_to_be_staked);
314314
assert_ok!(Subtensor::<T>::register_network(RawOrigin::Signed(coldkey.clone()).into()));
315-
}: dissolve_network(RawOrigin::Signed(coldkey), 1)
315+
}: dissolve_network(RawOrigin::Root, coldkey, 1)
316316

317317
// swap_hotkey {
318318
// let seed: u32 = 1;

pallets/subtensor/src/coinbase/root.rs

Lines changed: 6 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -992,30 +992,27 @@ impl<T: Config> Pallet<T> {
992992
/// * 'SubNetworkDoesNotExist': If the specified network does not exist.
993993
/// * 'NotSubnetOwner': If the caller does not own the specified subnet.
994994
///
995-
pub fn user_remove_network(origin: T::RuntimeOrigin, netuid: u16) -> dispatch::DispatchResult {
996-
// --- 1. Ensure the function caller is a signed user.
997-
let coldkey = ensure_signed(origin)?;
998-
999-
// --- 2. Ensure this subnet exists.
995+
pub fn user_remove_network(coldkey: T::AccountId, netuid: u16) -> dispatch::DispatchResult {
996+
// --- 1. Ensure this subnet exists.
1000997
ensure!(
1001998
Self::if_subnet_exist(netuid),
1002999
Error::<T>::SubNetworkDoesNotExist
10031000
);
10041001

1005-
// --- 3. Ensure the caller owns this subnet.
1002+
// --- 2. Ensure the caller owns this subnet.
10061003
ensure!(
10071004
SubnetOwner::<T>::get(netuid) == coldkey,
10081005
Error::<T>::NotSubnetOwner
10091006
);
10101007

1011-
// --- 4. Explicitly erase the network and all its parameters.
1008+
// --- 2. Explicitly erase the network and all its parameters.
10121009
Self::remove_network(netuid);
10131010

1014-
// --- 5. Emit the NetworkRemoved event.
1011+
// --- 3. Emit the NetworkRemoved event.
10151012
log::debug!("NetworkRemoved( netuid:{:?} )", netuid);
10161013
Self::deposit_event(Event::NetworkRemoved(netuid));
10171014

1018-
// --- 6. Return success.
1015+
// --- 5. Return success.
10191016
Ok(())
10201017
}
10211018

pallets/subtensor/src/macros/dispatches.rs

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -682,7 +682,7 @@ mod dispatches {
682682
new_coldkey: T::AccountId,
683683
) -> DispatchResultWithPostInfo {
684684
// Ensure it's called with root privileges (scheduler has root privileges)
685-
ensure_root(origin.clone())?;
685+
ensure_root(origin)?;
686686
log::info!("swap_coldkey: {:?} -> {:?}", old_coldkey, new_coldkey);
687687

688688
Self::do_swap_coldkey(&old_coldkey, &new_coldkey)
@@ -931,8 +931,13 @@ mod dispatches {
931931
#[pallet::weight((Weight::from_parts(119_000_000, 0)
932932
.saturating_add(T::DbWeight::get().reads(6))
933933
.saturating_add(T::DbWeight::get().writes(31)), DispatchClass::Operational, Pays::No))]
934-
pub fn dissolve_network(origin: OriginFor<T>, netuid: u16) -> DispatchResult {
935-
Self::user_remove_network(origin, netuid)
934+
pub fn dissolve_network(
935+
origin: OriginFor<T>,
936+
coldkey: T::AccountId,
937+
netuid: u16,
938+
) -> DispatchResult {
939+
ensure_root(origin)?;
940+
Self::user_remove_network(coldkey, netuid)
936941
}
937942

938943
/// Set a single child for a given hotkey on a specified network.
@@ -1101,7 +1106,10 @@ mod dispatches {
11011106
let duration: BlockNumberFor<T> = DissolveNetworkScheduleDuration::<T>::get();
11021107
let when: BlockNumberFor<T> = current_block.saturating_add(duration);
11031108

1104-
let call = Call::<T>::dissolve_network { netuid };
1109+
let call = Call::<T>::dissolve_network {
1110+
coldkey: who.clone(),
1111+
netuid,
1112+
};
11051113

11061114
let bound_call = T::Preimages::bound(LocalCallOf::<T>::from(call.clone()))
11071115
.map_err(|_| Error::<T>::FailedToSchedule)?;
@@ -1110,7 +1118,7 @@ mod dispatches {
11101118
DispatchTime::At(when),
11111119
None,
11121120
63,
1113-
frame_system::RawOrigin::Signed(who.clone()).into(),
1121+
frame_system::RawOrigin::Root.into(),
11141122
bound_call,
11151123
)
11161124
.map_err(|_| Error::<T>::FailedToSchedule)?;

pallets/subtensor/src/macros/events.rs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -196,5 +196,9 @@ mod events {
196196
/// extrinsic execution block number
197197
execution_block: BlockNumberFor<T>,
198198
},
199+
/// The duration of schedule coldkey swap has been set
200+
ColdkeySwapScheduleDurationSet(BlockNumberFor<T>),
201+
/// The duration of dissolve network has been set
202+
DissolveNetworkScheduleDurationSet(BlockNumberFor<T>),
199203
}
200204
}

pallets/subtensor/src/utils/misc.rs

Lines changed: 31 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
use super::*;
22
use crate::{
3-
system::{ensure_root, ensure_signed_or_root},
3+
system::{ensure_root, ensure_signed_or_root, pallet_prelude::BlockNumberFor},
44
Error,
55
};
66
use sp_core::Get;
@@ -750,4 +750,34 @@ impl<T: Config> Pallet<T> {
750750
// Emit an event to notify listeners about the change
751751
Self::deposit_event(Event::NetworkMaxStakeSet(netuid, max_stake));
752752
}
753+
754+
/// Set the duration for coldkey swap
755+
///
756+
/// # Arguments
757+
///
758+
/// * `duration` - The blocks for coldkey swap execution.
759+
///
760+
/// # Effects
761+
///
762+
/// * Update the ColdkeySwapScheduleDuration storage.
763+
/// * Emits a ColdkeySwapScheduleDurationSet evnet.
764+
pub fn set_coldkey_swap_schedule_duration(duration: BlockNumberFor<T>) {
765+
ColdkeySwapScheduleDuration::<T>::set(duration);
766+
Self::deposit_event(Event::ColdkeySwapScheduleDurationSet(duration));
767+
}
768+
769+
/// Set the duration for dissolve network
770+
///
771+
/// # Arguments
772+
///
773+
/// * `duration` - The blocks for dissolve network execution.
774+
///
775+
/// # Effects
776+
///
777+
/// * Update the DissolveNetworkScheduleDuration storage.
778+
/// * Emits a DissolveNetworkScheduleDurationSet evnet.
779+
pub fn set_dissolve_network_schedule_duration(duration: BlockNumberFor<T>) {
780+
DissolveNetworkScheduleDuration::<T>::set(duration);
781+
Self::deposit_event(Event::DissolveNetworkScheduleDurationSet(duration));
782+
}
753783
}

0 commit comments

Comments
 (0)