Skip to content

Commit 02457c1

Browse files
constconst
authored andcommitted
fix migration tests
1 parent 4a777df commit 02457c1

File tree

6 files changed

+78
-294
lines changed

6 files changed

+78
-294
lines changed

pallets/subtensor/src/lib.rs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1073,9 +1073,12 @@ pub mod pallet {
10731073
(H256, u64),
10741074
OptionQuery,
10751075
>;
1076+
10761077
/// ==================
10771078
/// ==== Genesis =====
10781079
/// ==================
1080+
#[pallet::storage] // --- Storage for migration run status
1081+
pub type HasMigrationRun<T: Config> = StorageMap<_, Identity, Vec<u8>, bool, ValueQuery>;
10791082

10801083
#[pallet::genesis_config]
10811084
pub struct GenesisConfig<T: Config> {

pallets/subtensor/src/macros/dispatches.rs

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -640,17 +640,17 @@ mod dispatches {
640640
}
641641

642642
/// The extrinsic for user to change its hotkey
643-
///#[pallet::call_index(70)]
644-
///#[pallet::weight((Weight::from_parts(1_940_000_000, 0)
645-
///.saturating_add(T::DbWeight::get().reads(272))
646-
///.saturating_add(T::DbWeight::get().writes(527)), DispatchClass::Operational, Pays::No))]
647-
///pub fn swap_hotkey(
648-
/// origin: OriginFor<T>,
649-
/// hotkey: T::AccountId,
650-
/// new_hotkey: T::AccountId,
651-
///) -> DispatchResultWithPostInfo {
652-
/// Self::do_swap_hotkey(origin, &hotkey, &new_hotkey)
653-
///}
643+
#[pallet::call_index(70)]
644+
#[pallet::weight((Weight::from_parts(1_940_000_000, 0)
645+
.saturating_add(T::DbWeight::get().reads(272))
646+
.saturating_add(T::DbWeight::get().writes(527)), DispatchClass::Operational, Pays::No))]
647+
pub fn swap_hotkey(
648+
origin: OriginFor<T>,
649+
hotkey: T::AccountId,
650+
new_hotkey: T::AccountId,
651+
) -> DispatchResultWithPostInfo {
652+
Self::do_swap_hotkey(origin, &hotkey, &new_hotkey)
653+
}
654654

655655
/// The extrinsic for user to change the coldkey associated with their account.
656656
///

pallets/subtensor/src/migrations/migrate_fix_total_coldkey_stake.rs

Lines changed: 31 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,10 @@ use super::*;
22
use frame_support::{
33
pallet_prelude::{Identity, OptionQuery},
44
storage_alias,
5-
traits::{Get, GetStorageVersion, StorageVersion},
5+
traits::{Get, StorageVersion},
66
weights::Weight,
77
};
8+
use alloc::string::String;
89
use sp_std::vec::Vec;
910

1011
// TODO (camfairchild): TEST MIGRATION
@@ -50,24 +51,41 @@ pub fn do_migrate_fix_total_coldkey_stake<T: Config>() -> Weight {
5051
}
5152
// Public migrate function to be called by Lib.rs on upgrade.
5253
pub fn migrate_fix_total_coldkey_stake<T: Config>() -> Weight {
53-
let current_storage_version: u16 = 7;
54-
let next_storage_version: u16 = 8;
54+
let migration_name = b"fix_total_coldkey_stake_v7".to_vec();
5555

5656
// Initialize the weight with one read operation.
5757
let mut weight = T::DbWeight::get().reads(1);
5858

59-
// Grab the current on-chain storage version.
60-
// Cant fail on retrieval.
61-
let onchain_version = Pallet::<T>::on_chain_storage_version();
62-
63-
// Only run this migration on storage version 6.
64-
if onchain_version == current_storage_version {
65-
weight = weight.saturating_add(do_migrate_fix_total_coldkey_stake::<T>());
66-
// Cant fail on insert.
67-
StorageVersion::new(next_storage_version).put::<Pallet<T>>();
68-
weight.saturating_accrue(T::DbWeight::get().writes(1));
59+
// Check if the migration has already run
60+
if HasMigrationRun::<T>::get(&migration_name) {
61+
log::info!(
62+
"Migration '{:?}' has already run. Skipping.",
63+
migration_name
64+
);
65+
return Weight::zero();
6966
}
7067

68+
log::info!(
69+
"Running migration '{}'",
70+
String::from_utf8_lossy(&migration_name)
71+
);
72+
73+
// Run the migration
74+
weight = weight.saturating_add(do_migrate_fix_total_coldkey_stake::<T>());
75+
76+
// Mark the migration as completed
77+
HasMigrationRun::<T>::insert(&migration_name, true);
78+
weight = weight.saturating_add(T::DbWeight::get().writes(1));
79+
80+
// Set the storage version to 7
81+
StorageVersion::new(7).put::<Pallet<T>>();
82+
weight = weight.saturating_add(T::DbWeight::get().writes(1));
83+
84+
log::info!(
85+
"Migration '{:?}' completed. Storage version set to 7.",
86+
String::from_utf8_lossy(&migration_name)
87+
);
88+
7189
// Return the migration weight.
7290
weight
7391
}

0 commit comments

Comments
 (0)