Skip to content

Commit e58b165

Browse files
authored
Merge pull request #2276 from opentensor/improve-loop-alpha-map
improve loop on alpha map
2 parents 8def8b5 + ea2640f commit e58b165

File tree

3 files changed

+37
-23
lines changed

3 files changed

+37
-23
lines changed

pallets/subtensor/src/staking/remove_stake.rs

Lines changed: 28 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -484,31 +484,37 @@ 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, this_netuid), share_u64f64) in Alpha::<T>::iter_prefix((hot,)) {
494+
if this_netuid != netuid {
495+
continue;
496+
}
497+
keys_to_remove.push((hot.clone(), cold.clone()));
498+
if !hotkeys_seen.contains(hot) {
499+
hotkeys_seen.push(hot.clone());
500+
}
496501

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);
502+
// Primary: actual α value via share pool.
503+
let pool = Self::get_alpha_share_pool(hot.clone(), netuid);
504+
let actual_val_u64 = pool.try_get_value(&cold).unwrap_or(0);
500505

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-
};
506+
// Fallback: if pool uninitialized, treat raw Alpha share as value.
507+
let val_u64 = if actual_val_u64 == 0 {
508+
share_u64f64.saturating_to_num::<u64>()
509+
} else {
510+
actual_val_u64
511+
};
507512

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));
513+
if val_u64 > 0 {
514+
let val_u128 = val_u64 as u128;
515+
total_alpha_value_u128 = total_alpha_value_u128.saturating_add(val_u128);
516+
stakers.push((hot.clone(), cold, val_u128));
517+
}
512518
}
513519
}
514520

pallets/subtensor/src/tests/networks.rs

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -106,6 +106,7 @@ fn dissolve_single_alpha_out_staker_gets_all_tao() {
106106
let pot: u64 = 99_999;
107107
SubnetTAO::<Test>::insert(net, TaoCurrency::from(pot));
108108
SubtensorModule::set_subnet_locked_balance(net, 0.into());
109+
TotalHotkeyAlpha::<Test>::insert(s_hot, net, AlphaCurrency::from(5_000u64));
109110

110111
// Cold-key balance before
111112
let before = SubtensorModule::get_coldkey_balance(&s_cold);
@@ -142,6 +143,9 @@ fn dissolve_two_stakers_pro_rata_distribution() {
142143
Alpha::<Test>::insert((s1_hot, s1_cold, net), U64F64::from_num(a1));
143144
Alpha::<Test>::insert((s2_hot, s2_cold, net), U64F64::from_num(a2));
144145

146+
TotalHotkeyAlpha::<Test>::insert(s1_hot, net, AlphaCurrency::from(a1 as u64));
147+
TotalHotkeyAlpha::<Test>::insert(s2_hot, net, AlphaCurrency::from(a2 as u64));
148+
145149
let pot: u64 = 10_000;
146150
SubnetTAO::<Test>::insert(net, TaoCurrency::from(pot));
147151
SubtensorModule::set_subnet_locked_balance(net, 5_000.into()); // owner refund path present; emission = 0
@@ -627,6 +631,7 @@ fn dissolve_alpha_out_but_zero_tao_no_rewards() {
627631
SubnetTAO::<Test>::insert(net, TaoCurrency::from(0)); // zero TAO
628632
SubtensorModule::set_subnet_locked_balance(net, TaoCurrency::from(0));
629633
Emission::<Test>::insert(net, Vec::<AlphaCurrency>::new());
634+
TotalHotkeyAlpha::<Test>::insert(sh, net, AlphaCurrency::from(1_000u64));
630635

631636
let before = SubtensorModule::get_coldkey_balance(&sc);
632637
assert_ok!(SubtensorModule::do_dissolve_network(net));
@@ -672,6 +677,9 @@ fn dissolve_rounding_remainder_distribution() {
672677
SubnetTAO::<Test>::insert(net, TaoCurrency::from(1)); // TAO pot = 1
673678
SubtensorModule::set_subnet_locked_balance(net, TaoCurrency::from(0));
674679

680+
TotalHotkeyAlpha::<Test>::insert(s1h, net, AlphaCurrency::from(3u64));
681+
TotalHotkeyAlpha::<Test>::insert(s2h, net, AlphaCurrency::from(2u64));
682+
675683
// Cold-key balances before
676684
let c1_before = SubtensorModule::get_coldkey_balance(&s1c);
677685
let c2_before = SubtensorModule::get_coldkey_balance(&s2c);

runtime/src/lib.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -237,7 +237,7 @@ pub const VERSION: RuntimeVersion = RuntimeVersion {
237237
// `spec_version`, and `authoring_version` are the same between Wasm and native.
238238
// This value is set to 100 to notify Polkadot-JS App (https://polkadot.js.org/apps) to use
239239
// the compatible custom types.
240-
spec_version: 361,
240+
spec_version: 362,
241241
impl_version: 1,
242242
apis: RUNTIME_API_VERSIONS,
243243
transaction_version: 1,

0 commit comments

Comments
 (0)