Skip to content

Commit 2c562d6

Browse files
committed
don't use abs directly
1 parent b5477fa commit 2c562d6

File tree

1 file changed

+12
-2
lines changed

1 file changed

+12
-2
lines changed

pallets/subtensor/src/staking/stake_utils.rs

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -546,8 +546,12 @@ impl<T: Config> Pallet<T> {
546546
amount: u64,
547547
) -> u64 {
548548
let mut alpha_share_pool = Self::get_alpha_share_pool(hotkey.clone(), netuid);
549+
// We expect to add a positive amount here.
549550
let actual_alpha = alpha_share_pool.update_value_for_one(coldkey, amount as i64);
550-
actual_alpha.unsigned_abs()
551+
552+
// We should return a positive amount, or 0 if the operation failed.
553+
// e.g. the stake was removed due to precision issues.
554+
actual_alpha.max(0).unsigned_abs()
551555
}
552556

553557
pub fn try_increase_stake_for_hotkey_and_coldkey_on_subnet(
@@ -576,14 +580,20 @@ impl<T: Config> Pallet<T> {
576580
amount: u64,
577581
) -> u64 {
578582
let mut alpha_share_pool = Self::get_alpha_share_pool(hotkey.clone(), netuid);
583+
584+
// We expect a negative value here
579585
let mut actual_alpha = 0;
580586
if let Ok(value) = alpha_share_pool.try_get_value(coldkey) {
581587
if value >= amount {
582588
actual_alpha =
583589
alpha_share_pool.update_value_for_one(coldkey, (amount as i64).neg());
584590
}
585591
}
586-
actual_alpha.unsigned_abs()
592+
593+
// Get the negation of the removed alpha, and clamp at 0.
594+
// This ensures we return a positive value, but only if
595+
// `actual_alpha` was negative (i.e. a decrease in stake).
596+
actual_alpha.neg().max(0).unsigned_abs()
587597
}
588598

589599
/// Calculates Some(Alpha) returned from pool by staking operation

0 commit comments

Comments
 (0)