From 2c562d616a1d529aaa0de8ab67115c472057a2cc Mon Sep 17 00:00:00 2001 From: Cameron Fairchild Date: Thu, 6 Mar 2025 12:14:30 -0500 Subject: [PATCH] don't use abs directly --- pallets/subtensor/src/staking/stake_utils.rs | 14 ++++++++++++-- 1 file changed, 12 insertions(+), 2 deletions(-) diff --git a/pallets/subtensor/src/staking/stake_utils.rs b/pallets/subtensor/src/staking/stake_utils.rs index 0a3659b6a..4a4c9f8aa 100644 --- a/pallets/subtensor/src/staking/stake_utils.rs +++ b/pallets/subtensor/src/staking/stake_utils.rs @@ -546,8 +546,12 @@ impl Pallet { amount: u64, ) -> u64 { let mut alpha_share_pool = Self::get_alpha_share_pool(hotkey.clone(), netuid); + // We expect to add a positive amount here. let actual_alpha = alpha_share_pool.update_value_for_one(coldkey, amount as i64); - actual_alpha.unsigned_abs() + + // We should return a positive amount, or 0 if the operation failed. + // e.g. the stake was removed due to precision issues. + actual_alpha.max(0).unsigned_abs() } pub fn try_increase_stake_for_hotkey_and_coldkey_on_subnet( @@ -576,6 +580,8 @@ impl Pallet { amount: u64, ) -> u64 { let mut alpha_share_pool = Self::get_alpha_share_pool(hotkey.clone(), netuid); + + // We expect a negative value here let mut actual_alpha = 0; if let Ok(value) = alpha_share_pool.try_get_value(coldkey) { if value >= amount { @@ -583,7 +589,11 @@ impl Pallet { alpha_share_pool.update_value_for_one(coldkey, (amount as i64).neg()); } } - actual_alpha.unsigned_abs() + + // Get the negation of the removed alpha, and clamp at 0. + // This ensures we return a positive value, but only if + // `actual_alpha` was negative (i.e. a decrease in stake). + actual_alpha.neg().max(0).unsigned_abs() } /// Calculates Some(Alpha) returned from pool by staking operation