Skip to content

Commit a2b8d70

Browse files
Merge pull request #645 from opentensor/fix/total-coldkey-stake-migration-clear-first
Fix/total coldkey stake migration; clear map first
2 parents 5adbbd9 + 23f540c commit a2b8d70

File tree

3 files changed

+56
-0
lines changed

3 files changed

+56
-0
lines changed

pallets/subtensor/src/migration.rs

+7
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,13 @@ pub fn do_migrate_fix_total_coldkey_stake<T: Config>() -> Weight {
3737
// Initialize the weight with one read operation.
3838
let mut weight = T::DbWeight::get().reads(1);
3939

40+
// Clear everything from the map first, no limit (u32::MAX)
41+
let removal_results = TotalColdkeyStake::<T>::clear(u32::MAX, None);
42+
// 1 read/write per removal
43+
let entries_removed: u64 = removal_results.backend.into();
44+
weight =
45+
weight.saturating_add(T::DbWeight::get().reads_writes(entries_removed, entries_removed));
46+
4047
// Iterate through all staking hotkeys.
4148
for (coldkey, hotkey_vec) in StakingHotkeys::<T>::iter() {
4249
// Init the zero value.

pallets/subtensor/tests/migration.rs

+17
Original file line numberDiff line numberDiff line change
@@ -392,6 +392,23 @@ fn test_migrate_fix_total_coldkey_stake_runs_once() {
392392
})
393393
}
394394

395+
// SKIP_WASM_BUILD=1 RUST_LOG=info cargo test --test migration -- test_migrate_fix_total_coldkey_stake_starts_with_value_no_stake_map_entries --exact --nocapture
396+
#[test]
397+
fn test_migrate_fix_total_coldkey_stake_starts_with_value_no_stake_map_entries() {
398+
new_test_ext(1).execute_with(|| {
399+
let migration_name = "fix_total_coldkey_stake_v7";
400+
let coldkey = U256::from(0);
401+
TotalColdkeyStake::<Test>::insert(coldkey, 123_456_789);
402+
403+
// Notably, coldkey has no stake map or staking_hotkeys map entries
404+
405+
let weight = run_migration_and_check(migration_name);
406+
assert!(weight != Weight::zero());
407+
// Therefore 0
408+
assert_eq!(TotalColdkeyStake::<Test>::get(coldkey), 0);
409+
})
410+
}
411+
395412
fn run_migration_and_check(migration_name: &'static str) -> frame_support::weights::Weight {
396413
// Execute the migration and store its weight
397414
let weight: frame_support::weights::Weight =

pallets/subtensor/tests/staking.rs

+32
Original file line numberDiff line numberDiff line change
@@ -4719,3 +4719,35 @@ fn test_do_schedule_coldkey_swap_regular_user_passes_min_balance() {
47194719
);
47204720
});
47214721
}
4722+
4723+
// SKIP_WASM_BUILD=1 RUST_LOG=info cargo test --test staking -- test_emission_creates_staking_hotkeys_entry --exact --nocapture
4724+
#[test]
4725+
fn test_emission_creates_staking_hotkeys_entry() {
4726+
new_test_ext(1).execute_with(|| {
4727+
let hotkey0 = U256::from(1);
4728+
let hotkey1 = U256::from(2);
4729+
4730+
let coldkey = U256::from(3);
4731+
4732+
// Add to Owner map
4733+
Owner::<Test>::insert(hotkey0, coldkey);
4734+
Owner::<Test>::insert(hotkey1, coldkey);
4735+
OwnedHotkeys::<Test>::insert(coldkey, vec![hotkey0, hotkey1]);
4736+
4737+
// Emit through hotkey
4738+
SubtensorModule::emit_inflation_through_hotkey_account(&hotkey0, 0, 1_000);
4739+
4740+
// Verify StakingHotkeys has an entry
4741+
assert_eq!(StakingHotkeys::<Test>::get(coldkey).len(), 1);
4742+
assert!(StakingHotkeys::<Test>::get(coldkey).contains(&hotkey0));
4743+
4744+
// Try again with another emission on hotkey1
4745+
SubtensorModule::emit_inflation_through_hotkey_account(&hotkey1, 0, 2_000);
4746+
4747+
// Verify both hotkeys are now in the map
4748+
assert_eq!(StakingHotkeys::<Test>::get(coldkey).len(), 2);
4749+
let final_map = StakingHotkeys::<Test>::get(coldkey);
4750+
assert!(final_map.contains(&hotkey0));
4751+
assert!(final_map.contains(&hotkey1));
4752+
})
4753+
}

0 commit comments

Comments
 (0)