Skip to content

Commit 0299a83

Browse files
committed
improve loop on alpha map
1 parent 8def8b5 commit 0299a83

File tree

1 file changed

+25
-22
lines changed

1 file changed

+25
-22
lines changed

pallets/subtensor/src/staking/remove_stake.rs

Lines changed: 25 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -484,31 +484,34 @@ impl<T: Config> Pallet<T> {
484484
let mut stakers: Vec<(T::AccountId, T::AccountId, u128)> = Vec::new();
485485
let mut total_alpha_value_u128: u128 = 0;
486486

487-
for ((hot, cold, this_netuid), share_u64f64) in Alpha::<T>::iter() {
488-
if this_netuid != netuid {
489-
continue;
490-
}
491-
492-
keys_to_remove.push((hot.clone(), cold.clone()));
493-
if !hotkeys_seen.contains(&hot) {
494-
hotkeys_seen.push(hot.clone());
495-
}
487+
let hotkeys_in_subnet: Vec<T::AccountId> = TotalHotkeyAlpha::<T>::iter()
488+
.filter(|(_, this_netuid, _)| *this_netuid == netuid)
489+
.map(|(hot, _, _)| hot.clone())
490+
.collect::<Vec<_>>();
491+
492+
for hot in hotkeys_in_subnet.iter() {
493+
for ((cold, _), share_u64f64) in Alpha::<T>::iter_prefix((hot,)) {
494+
keys_to_remove.push((hot.clone(), cold.clone()));
495+
if !hotkeys_seen.contains(&hot) {
496+
hotkeys_seen.push(hot.clone());
497+
}
496498

497-
// Primary: actual α value via share pool.
498-
let pool = Self::get_alpha_share_pool(hot.clone(), netuid);
499-
let actual_val_u64 = pool.try_get_value(&cold).unwrap_or(0);
499+
// Primary: actual α value via share pool.
500+
let pool = Self::get_alpha_share_pool(hot.clone(), netuid);
501+
let actual_val_u64 = pool.try_get_value(&cold).unwrap_or(0);
500502

501-
// Fallback: if pool uninitialized, treat raw Alpha share as value.
502-
let val_u64 = if actual_val_u64 == 0 {
503-
share_u64f64.saturating_to_num::<u64>()
504-
} else {
505-
actual_val_u64
506-
};
503+
// Fallback: if pool uninitialized, treat raw Alpha share as value.
504+
let val_u64 = if actual_val_u64 == 0 {
505+
share_u64f64.saturating_to_num::<u64>()
506+
} else {
507+
actual_val_u64
508+
};
507509

508-
if val_u64 > 0 {
509-
let val_u128 = val_u64 as u128;
510-
total_alpha_value_u128 = total_alpha_value_u128.saturating_add(val_u128);
511-
stakers.push((hot, cold, val_u128));
510+
if val_u64 > 0 {
511+
let val_u128 = val_u64 as u128;
512+
total_alpha_value_u128 = total_alpha_value_u128.saturating_add(val_u128);
513+
stakers.push((hot.clone(), cold, val_u128));
514+
}
512515
}
513516
}
514517

0 commit comments

Comments
 (0)