Skip to content

Commit 738baca

Browse files
authored
Merge pull request #1369 from opentensor/fix/no-abs-in-pool-math
don't use abs directly
2 parents 3836861 + 2c562d6 commit 738baca

File tree

1 file changed

+12
-2
lines changed

1 file changed

+12
-2
lines changed

pallets/subtensor/src/staking/stake_utils.rs

+12-2
Original file line numberDiff line numberDiff line change
@@ -553,8 +553,12 @@ impl<T: Config> Pallet<T> {
553553
amount: u64,
554554
) -> u64 {
555555
let mut alpha_share_pool = Self::get_alpha_share_pool(hotkey.clone(), netuid);
556+
// We expect to add a positive amount here.
556557
let actual_alpha = alpha_share_pool.update_value_for_one(coldkey, amount as i64);
557-
actual_alpha.unsigned_abs()
558+
559+
// We should return a positive amount, or 0 if the operation failed.
560+
// e.g. the stake was removed due to precision issues.
561+
actual_alpha.max(0).unsigned_abs()
558562
}
559563

560564
pub fn try_increase_stake_for_hotkey_and_coldkey_on_subnet(
@@ -583,14 +587,20 @@ impl<T: Config> Pallet<T> {
583587
amount: u64,
584588
) -> u64 {
585589
let mut alpha_share_pool = Self::get_alpha_share_pool(hotkey.clone(), netuid);
590+
591+
// We expect a negative value here
586592
let mut actual_alpha = 0;
587593
if let Ok(value) = alpha_share_pool.try_get_value(coldkey) {
588594
if value >= amount {
589595
actual_alpha =
590596
alpha_share_pool.update_value_for_one(coldkey, (amount as i64).neg());
591597
}
592598
}
593-
actual_alpha.unsigned_abs()
599+
600+
// Get the negation of the removed alpha, and clamp at 0.
601+
// This ensures we return a positive value, but only if
602+
// `actual_alpha` was negative (i.e. a decrease in stake).
603+
actual_alpha.neg().max(0).unsigned_abs()
594604
}
595605

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

0 commit comments

Comments
 (0)