Skip to content

Commit 79f5dd9

Browse files
committed
add weights
1 parent eea9be4 commit 79f5dd9

File tree

1 file changed

+36
-15
lines changed

1 file changed

+36
-15
lines changed

pallets/subtensor/src/migrations/migrate_reset_unactive_sn.rs

Lines changed: 36 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -56,18 +56,21 @@ pub fn migrate_reset_unactive_sn<T: Config>() -> Weight {
5656
PendingValidatorEmission::<T>::remove(*netuid);
5757
PendingRootAlphaDivs::<T>::remove(*netuid);
5858
PendingOwnerCut::<T>::remove(*netuid);
59+
weight = weight.saturating_add(T::DbWeight::get().writes(4));
5960

6061
// Reset pool
6162
let actual_tao_lock_amount = SubnetLocked::<T>::get(*netuid);
6263
let actual_tao_lock_amount_less_pool_tao =
6364
actual_tao_lock_amount.saturating_sub(pool_initial_tao);
65+
weight = weight.saturating_add(T::DbWeight::get().reads(1));
6466

6567
// Recycle already emitted TAO
6668
let subnet_tao = SubnetTAO::<T>::get(*netuid);
6769
if subnet_tao > pool_initial_tao {
6870
Pallet::<T>::recycle_tao(subnet_tao.saturating_sub(pool_initial_tao));
6971
}
7072
SubnetTAO::<T>::insert(*netuid, pool_initial_tao);
73+
weight = weight.saturating_add(T::DbWeight::get().reads_writes(1, 1));
7174

7275
// Reset pool alpha
7376
SubnetAlphaIn::<T>::insert(*netuid, pool_initial_alpha);
@@ -76,40 +79,58 @@ pub fn migrate_reset_unactive_sn<T: Config>() -> Weight {
7679
SubnetVolume::<T>::insert(*netuid, 0u128);
7780
// Reset recycled (from init_new_network)
7881
RAORecycledForRegistration::<T>::insert(*netuid, actual_tao_lock_amount_less_pool_tao);
82+
weight = weight.saturating_add(T::DbWeight::get().writes(4));
7983

8084
// Reset v3 pool
8185
T::SwapInterface::clear_protocol_liquidity(*netuid).unwrap_or_else(|e| {
8286
log::error!("Failed to clear protocol liquidity for netuid {netuid:?}: {e:?}");
8387
});
88+
// might be based on ticks but this is a rough estimate
89+
weight = weight.saturating_add(T::DbWeight::get().reads_writes(6, 14));
8490

8591
// Reset Alpha stake entries for this subnet
8692
let mut to_reset = Vec::new();
87-
for (hotkey, _, alpha) in
88-
TotalHotkeyAlpha::<T>::iter().filter(|(_, netuid_, _)| *netuid_ == *netuid)
89-
{
93+
for (hotkey, netuid_, alpha) in TotalHotkeyAlpha::<T>::iter() {
94+
weight = weight.saturating_add(T::DbWeight::get().reads(1));
95+
if netuid_ != *netuid {
96+
// skip netuids that are not the subnet we are resetting
97+
continue;
98+
}
99+
90100
if alpha > AlphaCurrency::from(0) {
91101
to_reset.push((hotkey, netuid, alpha));
92102
}
93103
}
94-
for (hotkey, netuid_, _) in to_reset {
95-
TotalHotkeyAlpha::<T>::remove(&hotkey, netuid_);
96-
TotalHotkeyShares::<T>::remove(&hotkey, netuid_);
97-
TotalHotkeyAlphaLastEpoch::<T>::remove(&hotkey, netuid_);
104+
105+
for (hotkey, netuid_i, _) in to_reset {
106+
TotalHotkeyAlpha::<T>::remove(&hotkey, netuid_i);
107+
TotalHotkeyShares::<T>::remove(&hotkey, netuid_i);
108+
TotalHotkeyAlphaLastEpoch::<T>::remove(&hotkey, netuid_i);
109+
weight = weight.saturating_add(T::DbWeight::get().writes(3));
98110

99111
// Reset root claimable and claimed
100112
RootClaimable::<T>::mutate(&hotkey, |claimable| {
101-
claimable.remove(netuid_);
113+
claimable.remove(netuid_i);
102114
});
103-
let _ = RootClaimed::<T>::clear_prefix((netuid_, &hotkey), u32::MAX, None);
115+
weight = weight.saturating_add(T::DbWeight::get().reads_writes(1, 1));
116+
let removal_result =
117+
RootClaimed::<T>::clear_prefix((netuid_i, &hotkey), u32::MAX, None);
118+
weight = weight.saturating_add(
119+
T::DbWeight::get()
120+
.reads_writes(removal_result.loops as u64, removal_result.backend as u64),
121+
);
104122

105123
let mut to_reset_alpha: Vec<(&T::AccountId, T::AccountId, NetUid)> = Vec::new();
106-
for ((coldkey, _), _) in
107-
Alpha::<T>::iter_prefix((&hotkey,)).filter(|((_, netuid_), _)| *netuid_ == *netuid)
108-
{
109-
to_reset_alpha.push((&hotkey, coldkey, *netuid_));
124+
for ((coldkey, netuid_j), _) in Alpha::<T>::iter_prefix((&hotkey,)) {
125+
weight = weight.saturating_add(T::DbWeight::get().reads(1));
126+
if netuid_j != *netuid_i {
127+
continue;
128+
}
129+
to_reset_alpha.push((&hotkey, coldkey, netuid_j));
110130
}
111-
for (hotkey, coldkey, netuid_) in to_reset_alpha {
112-
Alpha::<T>::remove((hotkey, coldkey, netuid_));
131+
for (hotkey, coldkey, netuid_j) in to_reset_alpha {
132+
Alpha::<T>::remove((hotkey, coldkey, netuid_j));
133+
weight = weight.saturating_add(T::DbWeight::get().writes(1));
113134
}
114135
}
115136
}

0 commit comments

Comments
 (0)