Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 12 additions & 2 deletions pallets/subtensor/src/staking/stake_utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -546,8 +546,12 @@ impl<T: Config> Pallet<T> {
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(
Expand Down Expand Up @@ -576,14 +580,20 @@ impl<T: Config> Pallet<T> {
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 {
actual_alpha =
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
Expand Down
Loading