@@ -546,8 +546,12 @@ impl<T: Config> Pallet<T> {
546
546
amount : u64 ,
547
547
) -> u64 {
548
548
let mut alpha_share_pool = Self :: get_alpha_share_pool ( hotkey. clone ( ) , netuid) ;
549
+ // We expect to add a positive amount here.
549
550
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 ( )
551
555
}
552
556
553
557
pub fn try_increase_stake_for_hotkey_and_coldkey_on_subnet (
@@ -576,14 +580,20 @@ impl<T: Config> Pallet<T> {
576
580
amount : u64 ,
577
581
) -> u64 {
578
582
let mut alpha_share_pool = Self :: get_alpha_share_pool ( hotkey. clone ( ) , netuid) ;
583
+
584
+ // We expect a negative value here
579
585
let mut actual_alpha = 0 ;
580
586
if let Ok ( value) = alpha_share_pool. try_get_value ( coldkey) {
581
587
if value >= amount {
582
588
actual_alpha =
583
589
alpha_share_pool. update_value_for_one ( coldkey, ( amount as i64 ) . neg ( ) ) ;
584
590
}
585
591
}
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 ( )
587
597
}
588
598
589
599
/// Calculates Some(Alpha) returned from pool by staking operation
0 commit comments