Skip to content
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion pallets/subtensor/src/macros/hooks.rs
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,9 @@ mod hooks {
// Remove all zero value entries in TotalHotkeyAlpha
.saturating_add(migrations::migrate_remove_zero_total_hotkey_alpha::migrate_remove_zero_total_hotkey_alpha::<T>())
// Wipe existing items to prevent bad decoding for new type
.saturating_add(migrations::migrate_upgrade_revealed_commitments::migrate_upgrade_revealed_commitments::<T>());
.saturating_add(migrations::migrate_upgrade_revealed_commitments::migrate_upgrade_revealed_commitments::<T>())
// Remove all entries in TotalHotkeyColdkeyStakesThisInterval
.saturating_add(migrations::migrate_remove_total_hotkey_coldkey_stakes_this_interval::migrate_remove_total_hotkey_coldkey_stakes_this_interval::<T>());
weight
}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
use super::*;
use crate::HasMigrationRun;
use frame_support::{traits::Get, weights::Weight};
use sp_io::{KillStorageResult, hashing::twox_128, storage::clear_prefix};

pub fn migrate_remove_total_hotkey_coldkey_stakes_this_interval<T: Config>() -> Weight {
let migration_name = "migrate_remove_total_hotkey_coldkey_stakes_this_interval";
let migration_name_bytes = migration_name.as_bytes().to_vec();

let mut weight = T::DbWeight::get().reads(1);
if HasMigrationRun::<T>::get(&migration_name_bytes) {
log::info!(
"Migration '{:?}' has already run. Skipping.",
migration_name
);
return weight;
}

log::info!("Running migration '{}'", migration_name);

let pallet_name = twox_128(b"SubtensorModule");
let storage_name = twox_128(b"TotalHotkeyColdkeyStakesThisInterval");
let prefix = [pallet_name, storage_name].concat();

// Remove all entries.
let removed_entries_count = match clear_prefix(&prefix, Some(u32::MAX)) {
KillStorageResult::AllRemoved(removed) => removed as u64,
KillStorageResult::SomeRemaining(removed) => {
log::info!("Failed to remove all entries from {:?}", migration_name);
removed as u64
}
};

weight = weight.saturating_add(T::DbWeight::get().writes(removed_entries_count as u64));

log::info!(
"Removed {:?} entries from TotalHotkeyColdkeyStakesThisInterval.",
removed_entries_count
);

// Mark migration as completed
HasMigrationRun::<T>::insert(&migration_name_bytes, true);
Comment thread
l0r1s marked this conversation as resolved.
Outdated
weight = weight.saturating_add(T::DbWeight::get().writes(1));

log::info!(
"Migration '{:?}' completed successfully. {:?} entries removed.",
migration_name,
removed_entries_count
);

weight
}
1 change: 1 addition & 0 deletions pallets/subtensor/src/migrations/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ pub mod migrate_init_total_issuance;
pub mod migrate_populate_owned_hotkeys;
pub mod migrate_rao;
pub mod migrate_remove_stake_map;
pub mod migrate_remove_total_hotkey_coldkey_stakes_this_interval;
pub mod migrate_remove_unused_maps_and_values;
pub mod migrate_remove_zero_total_hotkey_alpha;
pub mod migrate_set_first_emission_block_number;
Expand Down
36 changes: 36 additions & 0 deletions pallets/subtensor/src/tests/migration.rs
Original file line number Diff line number Diff line change
Expand Up @@ -555,3 +555,39 @@ fn test_migrate_revealed_commitments() {
assert!(!weight.is_zero(), "Migration weight should be non-zero");
});
}

#[test]
fn test_migrate_remove_total_hotkey_coldkey_stakes_this_interval() {
new_test_ext(1).execute_with(|| {
const MIGRATION_NAME: &str = "migrate_remove_total_hotkey_coldkey_stakes_this_interval";

let pallet_name = twox_128(b"SubtensorModule");
let storage_name = twox_128(b"TotalHotkeyColdkeyStakesThisInterval");
let prefix = [pallet_name, storage_name].concat();

// Set up 200 000 entries to be deleted.
for i in 0..200_000{
let hotkey = U256::from(i as u64);
let coldkey = U256::from(i as u64);
let key = [prefix.clone(), hotkey.encode(), coldkey.encode()].concat();
let value = (100 + i, 200 + i);
put_raw(&key, &value.encode());
}

assert!(frame_support::storage::unhashed::contains_prefixed_key(&prefix), "Entries should exist before migration.");
assert!(
!HasMigrationRun::<Test>::get(MIGRATION_NAME.as_bytes().to_vec()),
"Migration should not have run yet."
);

// Run migration
let weight = crate::migrations::migrate_remove_total_hotkey_coldkey_stakes_this_interval::migrate_remove_total_hotkey_coldkey_stakes_this_interval::<Test>();

assert!(!frame_support::storage::unhashed::contains_prefixed_key(&prefix), "All entries should have been removed.");
assert!(
HasMigrationRun::<Test>::get(MIGRATION_NAME.as_bytes().to_vec()),
"Migration should be marked as run."
);
assert!(!weight.is_zero(),"Migration weight should be non-zero.");
});
}
2 changes: 1 addition & 1 deletion runtime/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -207,7 +207,7 @@ pub const VERSION: RuntimeVersion = RuntimeVersion {
// `spec_version`, and `authoring_version` are the same between Wasm and native.
// This value is set to 100 to notify Polkadot-JS App (https://polkadot.js.org/apps) to use
// the compatible custom types.
spec_version: 261,
spec_version: 262,
impl_version: 1,
apis: RUNTIME_API_VERSIONS,
transaction_version: 1,
Expand Down
Loading