@@ -553,8 +553,12 @@ impl<T: Config> Pallet<T> {
553
553
amount : u64 ,
554
554
) -> u64 {
555
555
let mut alpha_share_pool = Self :: get_alpha_share_pool ( hotkey. clone ( ) , netuid) ;
556
+ // We expect to add a positive amount here.
556
557
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 ( )
558
562
}
559
563
560
564
pub fn try_increase_stake_for_hotkey_and_coldkey_on_subnet (
@@ -583,14 +587,20 @@ impl<T: Config> Pallet<T> {
583
587
amount : u64 ,
584
588
) -> u64 {
585
589
let mut alpha_share_pool = Self :: get_alpha_share_pool ( hotkey. clone ( ) , netuid) ;
590
+
591
+ // We expect a negative value here
586
592
let mut actual_alpha = 0 ;
587
593
if let Ok ( value) = alpha_share_pool. try_get_value ( coldkey) {
588
594
if value >= amount {
589
595
actual_alpha =
590
596
alpha_share_pool. update_value_for_one ( coldkey, ( amount as i64 ) . neg ( ) ) ;
591
597
}
592
598
}
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 ( )
594
604
}
595
605
596
606
/// Calculates Some(Alpha) returned from pool by staking operation
0 commit comments