Skip to content

Commit 1740fef

Browse files
committed
Fix clear_small_nomination_if_required to also clear locks
1 parent 6464408 commit 1740fef

3 files changed

Lines changed: 11 additions & 49 deletions

File tree

pallets/subtensor/src/staking/helpers.rs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -245,6 +245,9 @@ impl<T: Config> Pallet<T> {
245245
if let Ok(cleared_stake) = maybe_cleared_stake {
246246
// Add the stake to the coldkey account.
247247
Self::add_balance_to_coldkey_account(coldkey, cleared_stake.into());
248+
249+
// Clear the lock if exists
250+
Self::maybe_cleanup_lock(coldkey, netuid);
248251
} else {
249252
// Just clear small alpha
250253
let alpha =

pallets/subtensor/src/staking/lock.rs

Lines changed: 3 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,6 @@ use substrate_fixed::transcendental::exp;
44
use substrate_fixed::types::{I64F64, U64F64};
55
use subtensor_runtime_common::NetUid;
66

7-
const DUST_THRESHOLD: u64 = 100;
8-
97
impl<T: Config> Pallet<T> {
108
/// Computes exp(-dt / tau) as a U64F64 decay factor.
119
pub fn exp_decay(dt: u64, tau: u64) -> U64F64 {
@@ -165,21 +163,10 @@ impl<T: Config> Pallet<T> {
165163
Ok(())
166164
}
167165

168-
/// Clears the lock if both locked_mass and conviction have decayed below the dust threshold.
166+
/// Clears the lock. This function will be called if the alpha stake drops below minimum
167+
/// threshold.
169168
pub fn maybe_cleanup_lock(coldkey: &T::AccountId, netuid: NetUid) {
170-
if let Some(existing) = Lock::<T>::get(coldkey, netuid) {
171-
let now = Self::get_current_block_as_u64();
172-
let lock = Self::roll_forward_lock(existing, now);
173-
let dust = DUST_THRESHOLD.into();
174-
175-
if lock.locked_mass < dust
176-
&& lock.conviction < U64F64::saturating_from_num(DUST_THRESHOLD)
177-
{
178-
Lock::<T>::remove(coldkey, netuid);
179-
} else {
180-
Lock::<T>::insert(coldkey, netuid, lock);
181-
}
182-
}
169+
Lock::<T>::remove(coldkey, netuid);
183170
}
184171

185172
/// Returns the total conviction for a hotkey on a subnet,

pallets/subtensor/src/tests/locks.rs

Lines changed: 5 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -1127,34 +1127,6 @@ fn test_maybe_cleanup_lock_removes_dust() {
11271127
});
11281128
}
11291129

1130-
#[test]
1131-
fn test_maybe_cleanup_lock_preserves_active_lock() {
1132-
new_test_ext(1).execute_with(|| {
1133-
let coldkey = U256::from(1);
1134-
let hotkey = U256::from(2);
1135-
let netuid = setup_subnet_with_stake(coldkey, hotkey, 100_000_000_000);
1136-
1137-
assert_ok!(SubtensorModule::do_lock_stake(
1138-
&coldkey,
1139-
netuid,
1140-
&hotkey,
1141-
100_000u64.into(),
1142-
));
1143-
1144-
step_block(100);
1145-
1146-
SubtensorModule::maybe_cleanup_lock(&coldkey, netuid);
1147-
1148-
let lock = Lock::<Test>::get(coldkey, netuid);
1149-
assert!(lock.is_some());
1150-
// last_update should be rolled forward to current block
1151-
assert_eq!(
1152-
lock.unwrap().last_update,
1153-
SubtensorModule::get_current_block_as_u64()
1154-
);
1155-
});
1156-
}
1157-
11581130
#[test]
11591131
fn test_maybe_cleanup_lock_no_lock() {
11601132
new_test_ext(1).execute_with(|| {
@@ -1336,11 +1308,11 @@ fn test_lock_stake_extrinsic() {
13361308
}
13371309

13381310
// =========================================================================
1339-
// GROUP 14: Recycle/burn alpha bypass (BUG: bypasses lock)
1311+
// GROUP 14: Recycle/burn alpha checks against lock
13401312
// =========================================================================
13411313

13421314
#[test]
1343-
fn test_recycle_alpha_bypasses_lock() {
1315+
fn test_recycle_alpha_checks_lock() {
13441316
new_test_ext(1).execute_with(|| {
13451317
let coldkey = U256::from(1);
13461318
let hotkey = U256::from(2);
@@ -1475,11 +1447,11 @@ fn test_subnet_dissolution_and_netuid_reuse() {
14751447
}
14761448

14771449
// =========================================================================
1478-
// GROUP 16: Clear small nomination bypass
1450+
// GROUP 16: Clear small nomination checks lock
14791451
// =========================================================================
14801452

14811453
#[test]
1482-
fn test_clear_small_nomination_bypasses_lock() {
1454+
fn test_clear_small_nomination_checks_lock() {
14831455
new_test_ext(1).execute_with(|| {
14841456
let owner_coldkey = U256::from(100);
14851457
let owner_hotkey = U256::from(101);
@@ -1523,7 +1495,7 @@ fn test_clear_small_nomination_bypasses_lock() {
15231495
assert_eq!(nominator_alpha_after, AlphaBalance::ZERO);
15241496

15251497
// Lock entry still exists, now orphaned
1526-
assert!(Lock::<Test>::get(nominator, netuid).is_some());
1498+
assert!(Lock::<Test>::get(nominator, netuid).is_none());
15271499
});
15281500
}
15291501

0 commit comments

Comments
 (0)