From fc84df2dbee4f1af8221d09235fa73cbbb54b660 Mon Sep 17 00:00:00 2001 From: Loris Moulin Date: Mon, 14 Apr 2025 13:02:58 +0200 Subject: [PATCH 1/5] added migration to remove total hk/ck stakes this interval --- pallets/subtensor/src/macros/hooks.rs | 4 +- ...tal_hotkey_coldkey_stakes_this_interval.rs | 51 +++++++++++++++++++ pallets/subtensor/src/migrations/mod.rs | 1 + pallets/subtensor/src/tests/migration.rs | 36 +++++++++++++ 4 files changed, 91 insertions(+), 1 deletion(-) create mode 100644 pallets/subtensor/src/migrations/migrate_remove_total_hotkey_coldkey_stakes_this_interval.rs diff --git a/pallets/subtensor/src/macros/hooks.rs b/pallets/subtensor/src/macros/hooks.rs index 49fc4ccfe5..9599750b2d 100644 --- a/pallets/subtensor/src/macros/hooks.rs +++ b/pallets/subtensor/src/macros/hooks.rs @@ -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::()) // Wipe existing items to prevent bad decoding for new type - .saturating_add(migrations::migrate_upgrade_revealed_commitments::migrate_upgrade_revealed_commitments::()); + .saturating_add(migrations::migrate_upgrade_revealed_commitments::migrate_upgrade_revealed_commitments::()) + // Remove all entries in TotalHotkeyColdkeyStakesThisInterval + .saturating_add(migrations::migrate_remove_total_hotkey_coldkey_stakes_this_interval::migrate_remove_total_hotkey_coldkey_stakes_this_interval::()); weight } diff --git a/pallets/subtensor/src/migrations/migrate_remove_total_hotkey_coldkey_stakes_this_interval.rs b/pallets/subtensor/src/migrations/migrate_remove_total_hotkey_coldkey_stakes_this_interval.rs new file mode 100644 index 0000000000..a7be69d504 --- /dev/null +++ b/pallets/subtensor/src/migrations/migrate_remove_total_hotkey_coldkey_stakes_this_interval.rs @@ -0,0 +1,51 @@ +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() -> 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::::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(); + + // Try to remove all entries from the storage, if some entries are remaining, + // the migration will re-run again on next blocks until all entries are removed. + let removed_entries_count = match clear_prefix(&prefix, Some(u32::MAX)) { + KillStorageResult::AllRemoved(removed) => { + // Mark migration as completed + HasMigrationRun::::insert(&migration_name_bytes, true); + weight = weight.saturating_add(T::DbWeight::get().writes(1)); + + log::info!( + "Migration '{:?}' completed successfully. {:?} entries removed.", + migration_name, + removed + ); + removed + } + KillStorageResult::SomeRemaining(removed) => { + log::info!( + "Migration '{:?}' completed partially. {:?} entries removed.", + migration_name, + removed + ); + removed + } + }; + + weight.saturating_add(T::DbWeight::get().writes(removed_entries_count as u64)) +} diff --git a/pallets/subtensor/src/migrations/mod.rs b/pallets/subtensor/src/migrations/mod.rs index 23fb3cde1f..6a86b89823 100644 --- a/pallets/subtensor/src/migrations/mod.rs +++ b/pallets/subtensor/src/migrations/mod.rs @@ -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; diff --git a/pallets/subtensor/src/tests/migration.rs b/pallets/subtensor/src/tests/migration.rs index 5efc4f152a..466b6e2269 100644 --- a/pallets/subtensor/src/tests/migration.rs +++ b/pallets/subtensor/src/tests/migration.rs @@ -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::::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::(); + + assert!(!frame_support::storage::unhashed::contains_prefixed_key(&prefix), "All entries should have been removed."); + assert!( + HasMigrationRun::::get(MIGRATION_NAME.as_bytes().to_vec()), + "Migration should be marked as run." + ); + assert!(!weight.is_zero(),"Migration weight should be non-zero."); + }); +} From bc8518937a97eabcc8c249d60694603c19a8ee1b Mon Sep 17 00:00:00 2001 From: Loris Moulin Date: Mon, 14 Apr 2025 13:25:31 +0200 Subject: [PATCH 2/5] rework --- ...tal_hotkey_coldkey_stakes_this_interval.rs | 43 ++++++++++--------- 1 file changed, 22 insertions(+), 21 deletions(-) diff --git a/pallets/subtensor/src/migrations/migrate_remove_total_hotkey_coldkey_stakes_this_interval.rs b/pallets/subtensor/src/migrations/migrate_remove_total_hotkey_coldkey_stakes_this_interval.rs index a7be69d504..c32f940afe 100644 --- a/pallets/subtensor/src/migrations/migrate_remove_total_hotkey_coldkey_stakes_this_interval.rs +++ b/pallets/subtensor/src/migrations/migrate_remove_total_hotkey_coldkey_stakes_this_interval.rs @@ -22,30 +22,31 @@ pub fn migrate_remove_total_hotkey_coldkey_stakes_this_interval() -> let storage_name = twox_128(b"TotalHotkeyColdkeyStakesThisInterval"); let prefix = [pallet_name, storage_name].concat(); - // Try to remove all entries from the storage, if some entries are remaining, - // the migration will re-run again on next blocks until all entries are removed. + // Remove all entries. let removed_entries_count = match clear_prefix(&prefix, Some(u32::MAX)) { - KillStorageResult::AllRemoved(removed) => { - // Mark migration as completed - HasMigrationRun::::insert(&migration_name_bytes, true); - weight = weight.saturating_add(T::DbWeight::get().writes(1)); - - log::info!( - "Migration '{:?}' completed successfully. {:?} entries removed.", - migration_name, - removed - ); - removed - } + KillStorageResult::AllRemoved(removed) => removed as u64, KillStorageResult::SomeRemaining(removed) => { - log::info!( - "Migration '{:?}' completed partially. {:?} entries removed.", - migration_name, - removed - ); - removed + log::info!("Failed to remove all entries from {:?}", migration_name); + removed as u64 } }; - weight.saturating_add(T::DbWeight::get().writes(removed_entries_count 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::::insert(&migration_name_bytes, true); + weight = weight.saturating_add(T::DbWeight::get().writes(1)); + + log::info!( + "Migration '{:?}' completed successfully. {:?} entries removed.", + migration_name, + removed_entries_count + ); + + weight } From beab08ab872c3acdb6fd26f0287c6e2ce6d612f9 Mon Sep 17 00:00:00 2001 From: Loris Moulin Date: Mon, 14 Apr 2025 17:11:23 +0200 Subject: [PATCH 3/5] bump spec version --- runtime/src/lib.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/runtime/src/lib.rs b/runtime/src/lib.rs index 41117a6c5d..c152f4f703 100644 --- a/runtime/src/lib.rs +++ b/runtime/src/lib.rs @@ -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, From f6bc0cad1295b3c3e16b1fd8076e135128e9bda3 Mon Sep 17 00:00:00 2001 From: Loris Moulin Date: Wed, 16 Apr 2025 12:37:41 +0200 Subject: [PATCH 4/5] run ci From 6bcb63bb396bb23d2bcbf97782fa46532ef6f2a7 Mon Sep 17 00:00:00 2001 From: Loris Moulin Date: Mon, 21 Apr 2025 17:45:01 +0200 Subject: [PATCH 5/5] set migration as ran only if all entry removed --- ...tal_hotkey_coldkey_stakes_this_interval.rs | 21 +++++++++---------- 1 file changed, 10 insertions(+), 11 deletions(-) diff --git a/pallets/subtensor/src/migrations/migrate_remove_total_hotkey_coldkey_stakes_this_interval.rs b/pallets/subtensor/src/migrations/migrate_remove_total_hotkey_coldkey_stakes_this_interval.rs index c32f940afe..16782a5d8c 100644 --- a/pallets/subtensor/src/migrations/migrate_remove_total_hotkey_coldkey_stakes_this_interval.rs +++ b/pallets/subtensor/src/migrations/migrate_remove_total_hotkey_coldkey_stakes_this_interval.rs @@ -24,24 +24,23 @@ pub fn migrate_remove_total_hotkey_coldkey_stakes_this_interval() -> // Remove all entries. let removed_entries_count = match clear_prefix(&prefix, Some(u32::MAX)) { - KillStorageResult::AllRemoved(removed) => removed as u64, + KillStorageResult::AllRemoved(removed) => { + log::info!("Removed all entries from {:?}.", storage_name); + + // Mark migration as completed + HasMigrationRun::::insert(&migration_name_bytes, true); + weight = weight.saturating_add(T::DbWeight::get().writes(1)); + + removed as u64 + } KillStorageResult::SomeRemaining(removed) => { - log::info!("Failed to remove all entries from {:?}", migration_name); + log::info!("Failed to remove all entries from {:?}", storage_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::::insert(&migration_name_bytes, true); - weight = weight.saturating_add(T::DbWeight::get().writes(1)); - log::info!( "Migration '{:?}' completed successfully. {:?} entries removed.", migration_name,