Skip to content

Commit 068cc4c

Browse files
committedMar 20, 2025
clear bonds
1 parent 0dcba52 commit 068cc4c

File tree

3 files changed

+58
-1
lines changed

3 files changed

+58
-1
lines changed
 

‎pallets/subtensor/src/macros/hooks.rs

+3-1
Original file line numberDiff line numberDiff line change
@@ -81,7 +81,9 @@ mod hooks {
8181
// Remove Stake map entries
8282
.saturating_add(migrations::migrate_remove_stake_map::migrate_remove_stake_map::<T>())
8383
// Remove unused maps entries
84-
.saturating_add(migrations::migrate_remove_unused_maps_and_values::migrate_remove_unused_maps_and_values::<T>());
84+
.saturating_add(migrations::migrate_remove_unused_maps_and_values::migrate_remove_unused_maps_and_values::<T>())
85+
// Reset Bonds
86+
.saturating_add(migrations::migrate_reset_bonds::migrate_reset_bonds::<T>());
8587
weight
8688
}
8789

Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
use super::*;
2+
use frame_support::weights::Weight;
3+
use log;
4+
use scale_info::prelude::string::String;
5+
6+
pub fn migrate_reset_bonds<T: Config>() -> Weight {
7+
use frame_support::traits::Get;
8+
let migration_name = b"migrate_reset_bonds".to_vec();
9+
10+
// Start counting weight
11+
let mut weight = T::DbWeight::get().reads(1);
12+
13+
// Check if we already ran this migration
14+
if HasMigrationRun::<T>::get(&migration_name) {
15+
log::info!(
16+
target: "runtime",
17+
"Migration '{:?}' has already run. Skipping.",
18+
String::from_utf8_lossy(&migration_name)
19+
);
20+
return weight;
21+
}
22+
23+
log::info!(
24+
target: "runtime",
25+
"Running migration '{}'",
26+
String::from_utf8_lossy(&migration_name)
27+
);
28+
29+
/// ===== Migration Body =====
30+
// Clear all bonds
31+
let mut curr = Bonds::<T>::clear(u32::MAX, None);
32+
weight = weight
33+
.saturating_add(T::DbWeight::get().reads_writes(curr.loops as u64, curr.unique as u64));
34+
while curr.maybe_cursor.is_some() {
35+
curr = Bonds::<T>::clear(u32::MAX, curr.maybe_cursor.as_deref());
36+
weight = weight
37+
.saturating_add(T::DbWeight::get().reads_writes(curr.loops as u64, curr.unique as u64));
38+
}
39+
40+
/// ===== Migration End =====
41+
// -----------------------------
42+
// Mark the migration as done
43+
// -----------------------------
44+
HasMigrationRun::<T>::insert(&migration_name, true);
45+
weight = weight.saturating_add(T::DbWeight::get().writes(1));
46+
47+
log::info!(
48+
target: "runtime",
49+
"Migration '{}' completed successfully.",
50+
String::from_utf8_lossy(&migration_name)
51+
);
52+
53+
weight
54+
}

‎pallets/subtensor/src/migrations/mod.rs

+1
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ pub mod migrate_populate_owned_hotkeys;
1111
pub mod migrate_rao;
1212
pub mod migrate_remove_stake_map;
1313
pub mod migrate_remove_unused_maps_and_values;
14+
pub mod migrate_reset_bonds;
1415
pub mod migrate_set_min_burn;
1516
pub mod migrate_set_min_difficulty;
1617
pub mod migrate_stake_threshold;

0 commit comments

Comments
 (0)
Failed to load comments.