Skip to content

Commit f2d6939

Browse files
authored
Merge pull request #1706 from opentensor/fix/disabled-limit-staking-ops-if-st-enabled-false
Fix/disabled limit staking ops if st enabled false
2 parents 1b0c63a + 423b3ce commit f2d6939

File tree

5 files changed

+92
-7
lines changed

5 files changed

+92
-7
lines changed

pallets/subtensor/src/lib.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1785,7 +1785,7 @@ pub mod pallet {
17851785
}
17861786

17871787
/// Ensure subtoken enalbed
1788-
pub fn ensure_subtoken_enabled(subnet: u16) -> DispatchResult {
1788+
pub fn ensure_subtoken_enabled(subnet: u16) -> Result<(), Error<T>> {
17891789
ensure!(
17901790
SubtokenEnabled::<T>::get(subnet),
17911791
Error::<T>::SubtokenDisabled

pallets/subtensor/src/macros/dispatches.rs

Lines changed: 5 additions & 5 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):

pallets/subtensor/src/staking/stake_utils.rs

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

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

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

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

pallets/subtensor/src/tests/subnet.rs

Lines changed: 79 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -251,9 +251,20 @@ fn test_subtoken_enable_reject_trading_before_enable() {
251251
let hotkey_account_2_id: U256 = U256::from(3);
252252
let amount = DefaultMinStake::<Test>::get() * 10;
253253

254+
let stake_bal = 10_000_000_000; // 10 Alpha
255+
256+
let limit_price = 1_000_000_000; // not important
257+
254258
add_network_disable_subtoken(netuid, 10, 0);
255259
add_network_disable_subtoken(netuid2, 10, 0);
256260

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

263274
SubtensorModule::add_balance_to_coldkey_account(&coldkey_account_id, 10_000);
264275

276+
// Give some stake
277+
SubtensorModule::increase_stake_for_hotkey_and_coldkey_on_subnet(
278+
&hotkey_account_id,
279+
&coldkey_account_id,
280+
netuid,
281+
stake_bal,
282+
);
283+
265284
// all trading extrinsic should be rejected.
266285
assert_noop!(
267286
SubtensorModule::add_stake(
@@ -273,6 +292,66 @@ fn test_subtoken_enable_reject_trading_before_enable() {
273292
Error::<Test>::SubtokenDisabled
274293
);
275294

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

runtime/src/lib.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -209,7 +209,7 @@ pub const VERSION: RuntimeVersion = RuntimeVersion {
209209
// `spec_version`, and `authoring_version` are the same between Wasm and native.
210210
// This value is set to 100 to notify Polkadot-JS App (https://polkadot.js.org/apps) to use
211211
// the compatible custom types.
212-
spec_version: 273,
212+
spec_version: 274,
213213
impl_version: 1,
214214
apis: RUNTIME_API_VERSIONS,
215215
transaction_version: 1,

0 commit comments

Comments
 (0)