Skip to content

Commit 419fe5f

Browse files
authored
Merge pull request #1734 from opentensor/sam-backprop-6-10-2025
backprop hotfixes 6/10/2025
2 parents 8d9cde5 + cb5db2b commit 419fe5f

File tree

4 files changed

+93
-8
lines changed

4 files changed

+93
-8
lines changed

pallets/subtensor/src/lib.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1714,7 +1714,7 @@ pub mod pallet {
17141714
}
17151715

17161716
/// Ensure subtoken enalbed
1717-
pub fn ensure_subtoken_enabled(subnet: u16) -> DispatchResult {
1717+
pub fn ensure_subtoken_enabled(subnet: u16) -> Result<(), Error<T>> {
17181718
ensure!(
17191719
SubtokenEnabled::<T>::get(subnet),
17201720
Error::<T>::SubtokenDisabled

pallets/subtensor/src/macros/dispatches.rs

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -551,11 +551,11 @@ mod dispatches {
551551
Self::do_increase_take(origin, hotkey, take)
552552
}
553553

554-
/// --- Adds stake to a hotkey. The call is made from the
555-
/// coldkey account linked in the hotkey.
556-
/// Only the associated coldkey is allowed to make staking and
557-
/// unstaking requests. This protects the neuron against
558-
/// attacks on its hotkey running in production code.
554+
/// --- Adds stake to a hotkey. The call is made from a coldkey account.
555+
/// This delegates stake to the hotkey.
556+
///
557+
/// Note: the coldkey account may own the hotkey, in which case they are
558+
/// delegating to themselves.
559559
///
560560
/// # Args:
561561
/// * 'origin': (<T as frame_system::Config>Origin):
@@ -1786,7 +1786,7 @@ mod dispatches {
17861786
///
17871787
#[pallet::call_index(88)]
17881788
#[pallet::weight((Weight::from_parts(159_200_000, 0)
1789-
.saturating_add(T::DbWeight::get().reads(13))
1789+
.saturating_add(T::DbWeight::get().reads(14))
17901790
.saturating_add(T::DbWeight::get().writes(10)), DispatchClass::Normal, Pays::No))]
17911791
pub fn add_stake_limit(
17921792
origin: OriginFor<T>,

pallets/subtensor/src/staking/stake_utils.rs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -909,6 +909,9 @@ impl<T: Config> Pallet<T> {
909909
// Ensure that the subnet exists.
910910
ensure!(Self::if_subnet_exist(netuid), Error::<T>::SubnetNotExists);
911911

912+
// Ensure that the subnet is enabled.
913+
Self::ensure_subtoken_enabled(netuid)?;
914+
912915
// Get the minimum balance (and amount) that satisfies the transaction
913916
let min_amount = DefaultMinStake::<T>::get().saturating_add(DefaultStakingFee::<T>::get());
914917

@@ -962,6 +965,9 @@ impl<T: Config> Pallet<T> {
962965
// Ensure that the subnet exists.
963966
ensure!(Self::if_subnet_exist(netuid), Error::<T>::SubnetNotExists);
964967

968+
// Ensure that the subnet is enabled.
969+
// Self::ensure_subtoken_enabled(netuid)?;
970+
965971
// Ensure that the stake amount to be removed is above the minimum in tao equivalent.
966972
if let Some(tao_equivalent) = Self::sim_swap_alpha_for_tao(netuid, alpha_unstaked) {
967973
ensure!(

pallets/subtensor/src/tests/subnet.rs

Lines changed: 80 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -239,7 +239,9 @@ fn test_subtoken_enable() {
239239
});
240240
}
241241

242-
// cargo test --package pallet-subtensor --lib -- tests::subnet::test_subtoken_enable_reject_trading_before_enable --exact --show-output
242+
// cargo test --package pallet-subtensor --lib --
243+
// tests::subnet::test_subtoken_enable_reject_trading_before_enable --exact --show-output
244+
#[allow(clippy::unwrap_used)]
243245
#[test]
244246
fn test_subtoken_enable_reject_trading_before_enable() {
245247
// ensure_subtoken_enabled
@@ -251,9 +253,20 @@ fn test_subtoken_enable_reject_trading_before_enable() {
251253
let hotkey_account_2_id: U256 = U256::from(3);
252254
let amount = DefaultMinStake::<Test>::get() * 10;
253255

256+
let stake_bal = 10_000_000_000; // 10 Alpha
257+
258+
let limit_price = 1_000_000_000; // not important
259+
254260
add_network_disable_subtoken(netuid, 10, 0);
255261
add_network_disable_subtoken(netuid2, 10, 0);
256262

263+
assert!(!SubtokenEnabled::<Test>::get(netuid));
264+
assert!(!SubtokenEnabled::<Test>::get(netuid2));
265+
266+
// Set liq high enough to not trigger other errors
267+
SubnetTAO::<Test>::set(netuid, 20_000_000_000);
268+
SubnetAlphaIn::<Test>::set(netuid, 20_000_000_000);
269+
257270
// Register so staking *could* work
258271
register_ok_neuron(netuid, hotkey_account_id, coldkey_account_id, 0);
259272
register_ok_neuron(netuid2, hotkey_account_id, coldkey_account_id, 100);
@@ -262,6 +275,14 @@ fn test_subtoken_enable_reject_trading_before_enable() {
262275

263276
SubtensorModule::add_balance_to_coldkey_account(&coldkey_account_id, 10_000);
264277

278+
// Give some stake
279+
SubtensorModule::increase_stake_for_hotkey_and_coldkey_on_subnet(
280+
&hotkey_account_id,
281+
&coldkey_account_id,
282+
netuid,
283+
stake_bal,
284+
);
285+
265286
// all trading extrinsic should be rejected.
266287
assert_noop!(
267288
SubtensorModule::add_stake(
@@ -273,6 +294,64 @@ fn test_subtoken_enable_reject_trading_before_enable() {
273294
Error::<Test>::SubtokenDisabled
274295
);
275296

297+
assert_noop!(
298+
SubtensorModule::add_stake_limit(
299+
RuntimeOrigin::signed(coldkey_account_id),
300+
hotkey_account_id,
301+
netuid,
302+
amount,
303+
limit_price,
304+
false
305+
),
306+
Error::<Test>::SubtokenDisabled
307+
);
308+
309+
// For unstake_all and unstake_all_alpha, the result is Ok, but the
310+
// operation is not performed.
311+
assert_ok!(
312+
SubtensorModule::unstake_all(
313+
RuntimeOrigin::signed(coldkey_account_id),
314+
hotkey_account_id
315+
),
316+
()
317+
);
318+
// Check that the stake is still the same
319+
assert_eq!(
320+
SubtensorModule::get_stake_for_hotkey_and_coldkey_on_subnet(
321+
&hotkey_account_id,
322+
&coldkey_account_id,
323+
netuid
324+
),
325+
stake_bal
326+
);
327+
328+
assert_ok!(
329+
SubtensorModule::unstake_all_alpha(
330+
RuntimeOrigin::signed(coldkey_account_id),
331+
hotkey_account_id
332+
),
333+
()
334+
);
335+
// Check that the stake is still the same
336+
assert_eq!(
337+
SubtensorModule::get_stake_for_hotkey_and_coldkey_on_subnet(
338+
&hotkey_account_id,
339+
&coldkey_account_id,
340+
netuid
341+
),
342+
stake_bal
343+
);
344+
345+
SubtensorModule::remove_stake_limit(
346+
RuntimeOrigin::signed(coldkey_account_id),
347+
hotkey_account_id,
348+
netuid,
349+
amount,
350+
limit_price,
351+
false,
352+
)
353+
.unwrap();
354+
276355
assert_noop!(
277356
SubtensorModule::remove_stake(
278357
RuntimeOrigin::signed(coldkey_account_id),

0 commit comments

Comments
 (0)