Skip to content

Commit 3b5b8d9

Browse files
committed
use clear instead of remove in migration
1 parent a67742a commit 3b5b8d9

File tree

1 file changed

+41
-29
lines changed

1 file changed

+41
-29
lines changed

pallets/subtensor/src/migrations/migrate_clear_rank_trust_pruning_maps.rs

Lines changed: 41 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -21,48 +21,60 @@ pub fn migrate_clear_rank_trust_pruning_maps<T: Config>() -> Weight {
2121

2222
let mut total_reads: u64 = 0;
2323
let mut total_writes: u64 = 0;
24+
let limit: u32 = u32::MAX;
2425

2526
// ------------------------------
26-
// 1) Rank: collect keys, then remove
27+
// 1) Rank: clear in one go
2728
// ------------------------------
28-
let rank_keys: Vec<NetUid> = Rank::<T>::iter_keys().collect();
29-
let rank_removed = rank_keys.len() as u64;
30-
total_reads = total_reads.saturating_add(rank_removed);
31-
for k in rank_keys {
32-
Rank::<T>::remove(k);
33-
total_writes = total_writes.saturating_add(1);
34-
}
29+
let rank_res = Rank::<T>::clear(limit, None);
30+
let rank_reads = rank_res.loops as u64;
31+
let rank_writes = rank_res.backend as u64;
32+
total_reads = total_reads.saturating_add(rank_reads);
33+
total_writes = total_writes.saturating_add(rank_writes);
34+
35+
log::info!(
36+
"Rank wipe: backend={}, loops={}, cursor_is_none={}",
37+
rank_res.backend,
38+
rank_res.loops,
39+
rank_res.maybe_cursor.is_none(),
40+
);
3541

3642
// ------------------------------
37-
// 2) Trust: collect keys, then remove
43+
// 2) Trust: clear in one go
3844
// ------------------------------
39-
let trust_keys: Vec<NetUid> = Trust::<T>::iter_keys().collect();
40-
let trust_removed = trust_keys.len() as u64;
41-
total_reads = total_reads.saturating_add(trust_removed);
42-
for k in trust_keys {
43-
Trust::<T>::remove(k);
44-
total_writes = total_writes.saturating_add(1);
45-
}
45+
let trust_res = Trust::<T>::clear(limit, None);
46+
let trust_reads = trust_res.loops as u64;
47+
let trust_writes = trust_res.backend as u64;
48+
total_reads = total_reads.saturating_add(trust_reads);
49+
total_writes = total_writes.saturating_add(trust_writes);
50+
51+
log::info!(
52+
"Trust wipe: backend={}, loops={}, cursor_is_none={}",
53+
trust_res.backend,
54+
trust_res.loops,
55+
trust_res.maybe_cursor.is_none(),
56+
);
4657

4758
// ------------------------------
48-
// 3) PruningScores: collect keys, then remove
59+
// 3) PruningScores: clear in one go
4960
// ------------------------------
50-
let ps_keys: Vec<NetUid> = PruningScores::<T>::iter_keys().collect();
51-
let ps_removed = ps_keys.len() as u64;
52-
total_reads = total_reads.saturating_add(ps_removed);
53-
for k in ps_keys {
54-
PruningScores::<T>::remove(k);
55-
total_writes = total_writes.saturating_add(1);
56-
}
61+
let ps_res = PruningScores::<T>::clear(limit, None);
62+
let ps_reads = ps_res.loops as u64;
63+
let ps_writes = ps_res.backend as u64;
64+
total_reads = total_reads.saturating_add(ps_reads);
65+
total_writes = total_writes.saturating_add(ps_writes);
66+
67+
log::info!(
68+
"PruningScores wipe: backend={}, loops={}, cursor_is_none={}",
69+
ps_res.backend,
70+
ps_res.loops,
71+
ps_res.maybe_cursor.is_none(),
72+
);
5773

58-
// Accumulate reads/writes into the total weight
74+
// Accumulate reads/writes from Rank/Trust/PruningScores into the total weight
5975
total_weight =
6076
total_weight.saturating_add(T::DbWeight::get().reads_writes(total_reads, total_writes));
6177

62-
log::info!("Rank wipe: removed={rank_removed}");
63-
log::info!("Trust wipe: removed={trust_removed}");
64-
log::info!("PruningScores wipe: removed={ps_removed}");
65-
6678
// Mark migration as done
6779
HasMigrationRun::<T>::insert(&mig_name, true);
6880
total_weight = total_weight.saturating_add(T::DbWeight::get().writes(1));

0 commit comments

Comments
 (0)