Skip to content

Commit fc84df2

Browse files
committed
added migration to remove total hk/ck stakes this interval
1 parent 2a4c994 commit fc84df2

File tree

4 files changed

+91
-1
lines changed

4 files changed

+91
-1
lines changed

pallets/subtensor/src/macros/hooks.rs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -87,7 +87,9 @@ mod hooks {
8787
// Remove all zero value entries in TotalHotkeyAlpha
8888
.saturating_add(migrations::migrate_remove_zero_total_hotkey_alpha::migrate_remove_zero_total_hotkey_alpha::<T>())
8989
// Wipe existing items to prevent bad decoding for new type
90-
.saturating_add(migrations::migrate_upgrade_revealed_commitments::migrate_upgrade_revealed_commitments::<T>());
90+
.saturating_add(migrations::migrate_upgrade_revealed_commitments::migrate_upgrade_revealed_commitments::<T>())
91+
// Remove all entries in TotalHotkeyColdkeyStakesThisInterval
92+
.saturating_add(migrations::migrate_remove_total_hotkey_coldkey_stakes_this_interval::migrate_remove_total_hotkey_coldkey_stakes_this_interval::<T>());
9193
weight
9294
}
9395

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
use super::*;
2+
use crate::HasMigrationRun;
3+
use frame_support::{traits::Get, weights::Weight};
4+
use sp_io::{KillStorageResult, hashing::twox_128, storage::clear_prefix};
5+
6+
pub fn migrate_remove_total_hotkey_coldkey_stakes_this_interval<T: Config>() -> Weight {
7+
let migration_name = "migrate_remove_total_hotkey_coldkey_stakes_this_interval";
8+
let migration_name_bytes = migration_name.as_bytes().to_vec();
9+
10+
let mut weight = T::DbWeight::get().reads(1);
11+
if HasMigrationRun::<T>::get(&migration_name_bytes) {
12+
log::info!(
13+
"Migration '{:?}' has already run. Skipping.",
14+
migration_name
15+
);
16+
return weight;
17+
}
18+
19+
log::info!("Running migration '{}'", migration_name);
20+
21+
let pallet_name = twox_128(b"SubtensorModule");
22+
let storage_name = twox_128(b"TotalHotkeyColdkeyStakesThisInterval");
23+
let prefix = [pallet_name, storage_name].concat();
24+
25+
// Try to remove all entries from the storage, if some entries are remaining,
26+
// the migration will re-run again on next blocks until all entries are removed.
27+
let removed_entries_count = match clear_prefix(&prefix, Some(u32::MAX)) {
28+
KillStorageResult::AllRemoved(removed) => {
29+
// Mark migration as completed
30+
HasMigrationRun::<T>::insert(&migration_name_bytes, true);
31+
weight = weight.saturating_add(T::DbWeight::get().writes(1));
32+
33+
log::info!(
34+
"Migration '{:?}' completed successfully. {:?} entries removed.",
35+
migration_name,
36+
removed
37+
);
38+
removed
39+
}
40+
KillStorageResult::SomeRemaining(removed) => {
41+
log::info!(
42+
"Migration '{:?}' completed partially. {:?} entries removed.",
43+
migration_name,
44+
removed
45+
);
46+
removed
47+
}
48+
};
49+
50+
weight.saturating_add(T::DbWeight::get().writes(removed_entries_count as u64))
51+
}

pallets/subtensor/src/migrations/mod.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ pub mod migrate_init_total_issuance;
1010
pub mod migrate_populate_owned_hotkeys;
1111
pub mod migrate_rao;
1212
pub mod migrate_remove_stake_map;
13+
pub mod migrate_remove_total_hotkey_coldkey_stakes_this_interval;
1314
pub mod migrate_remove_unused_maps_and_values;
1415
pub mod migrate_remove_zero_total_hotkey_alpha;
1516
pub mod migrate_set_first_emission_block_number;

pallets/subtensor/src/tests/migration.rs

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -555,3 +555,39 @@ fn test_migrate_revealed_commitments() {
555555
assert!(!weight.is_zero(), "Migration weight should be non-zero");
556556
});
557557
}
558+
559+
#[test]
560+
fn test_migrate_remove_total_hotkey_coldkey_stakes_this_interval() {
561+
new_test_ext(1).execute_with(|| {
562+
const MIGRATION_NAME: &str = "migrate_remove_total_hotkey_coldkey_stakes_this_interval";
563+
564+
let pallet_name = twox_128(b"SubtensorModule");
565+
let storage_name = twox_128(b"TotalHotkeyColdkeyStakesThisInterval");
566+
let prefix = [pallet_name, storage_name].concat();
567+
568+
// Set up 200 000 entries to be deleted.
569+
for i in 0..200_000{
570+
let hotkey = U256::from(i as u64);
571+
let coldkey = U256::from(i as u64);
572+
let key = [prefix.clone(), hotkey.encode(), coldkey.encode()].concat();
573+
let value = (100 + i, 200 + i);
574+
put_raw(&key, &value.encode());
575+
}
576+
577+
assert!(frame_support::storage::unhashed::contains_prefixed_key(&prefix), "Entries should exist before migration.");
578+
assert!(
579+
!HasMigrationRun::<Test>::get(MIGRATION_NAME.as_bytes().to_vec()),
580+
"Migration should not have run yet."
581+
);
582+
583+
// Run migration
584+
let weight = crate::migrations::migrate_remove_total_hotkey_coldkey_stakes_this_interval::migrate_remove_total_hotkey_coldkey_stakes_this_interval::<Test>();
585+
586+
assert!(!frame_support::storage::unhashed::contains_prefixed_key(&prefix), "All entries should have been removed.");
587+
assert!(
588+
HasMigrationRun::<Test>::get(MIGRATION_NAME.as_bytes().to_vec()),
589+
"Migration should be marked as run."
590+
);
591+
assert!(!weight.is_zero(),"Migration weight should be non-zero.");
592+
});
593+
}

0 commit comments

Comments
 (0)