From 8f6e8d63caefaebc56a6a4ea309bbd0dc1595253 Mon Sep 17 00:00:00 2001 From: Cameron Fairchild Date: Tue, 16 Jul 2024 18:02:54 -0400 Subject: [PATCH 1/6] add test first --- pallets/subtensor/tests/migration.rs | 17 +++++++++++++++++ 1 file changed, 17 insertions(+) diff --git a/pallets/subtensor/tests/migration.rs b/pallets/subtensor/tests/migration.rs index 8323cab8a..088e00bc9 100644 --- a/pallets/subtensor/tests/migration.rs +++ b/pallets/subtensor/tests/migration.rs @@ -392,6 +392,23 @@ fn test_migrate_fix_total_coldkey_stake_runs_once() { }) } +// SKIP_WASM_BUILD=1 RUST_LOG=info cargo test --test migration -- test_migrate_fix_total_coldkey_stake_starts_with_value_no_more_entries --exact --nocapture +#[test] +fn test_migrate_fix_total_coldkey_stake_starts_with_value_no_more_entries() { + new_test_ext(1).execute_with(|| { + let migration_name = "fix_total_coldkey_stake_v7"; + let coldkey = U256::from(0); + TotalColdkeyStake::::insert(coldkey, 123_456_789); + + // Notably, coldkey has no stake map or staking_hotkeys map entries + + let weight = run_migration_and_check(migration_name); + assert!(weight != Weight::zero()); + // Therefore 0 + assert_eq!(TotalColdkeyStake::::get(coldkey), 0); + }) +} + fn run_migration_and_check(migration_name: &'static str) -> frame_support::weights::Weight { // Execute the migration and store its weight let weight: frame_support::weights::Weight = From f25bdda714de3f569a4e7a107cedb45e7716b77a Mon Sep 17 00:00:00 2001 From: Cameron Fairchild Date: Tue, 16 Jul 2024 18:06:00 -0400 Subject: [PATCH 2/6] clear map before rest of migration --- pallets/subtensor/src/migration.rs | 3 +++ 1 file changed, 3 insertions(+) diff --git a/pallets/subtensor/src/migration.rs b/pallets/subtensor/src/migration.rs index 20e91237f..82cd8d0b6 100644 --- a/pallets/subtensor/src/migration.rs +++ b/pallets/subtensor/src/migration.rs @@ -37,6 +37,9 @@ pub fn do_migrate_fix_total_coldkey_stake() -> Weight { // Initialize the weight with one read operation. let mut weight = T::DbWeight::get().reads(1); + // Clear everything from the map first + let _ = TotalColdkeyStake::::clear(u32::MAX, None); + // Iterate through all staking hotkeys. for (coldkey, hotkey_vec) in StakingHotkeys::::iter() { // Init the zero value. From e009ff13b64174533504898d93c2140e547fe399 Mon Sep 17 00:00:00 2001 From: Cameron Fairchild Date: Tue, 16 Jul 2024 18:07:54 -0400 Subject: [PATCH 3/6] track weight for clear/removal --- pallets/subtensor/src/migration.rs | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/pallets/subtensor/src/migration.rs b/pallets/subtensor/src/migration.rs index 82cd8d0b6..e7e2b9795 100644 --- a/pallets/subtensor/src/migration.rs +++ b/pallets/subtensor/src/migration.rs @@ -38,7 +38,11 @@ pub fn do_migrate_fix_total_coldkey_stake() -> Weight { let mut weight = T::DbWeight::get().reads(1); // Clear everything from the map first - let _ = TotalColdkeyStake::::clear(u32::MAX, None); + let removal_results = TotalColdkeyStake::::clear(u32::MAX, None); + // 1 read/write per removal + let entries_removed: u64 = removal_results.backend.into(); + weight = + weight.saturating_add(T::DbWeight::get().reads_writes(entries_removed, entries_removed)); // Iterate through all staking hotkeys. for (coldkey, hotkey_vec) in StakingHotkeys::::iter() { From 480b1c5fb9f4336d30131fa9d02d2c36e0a80aea Mon Sep 17 00:00:00 2001 From: Cameron Fairchild Date: Tue, 16 Jul 2024 18:10:12 -0400 Subject: [PATCH 4/6] add comment --- pallets/subtensor/src/migration.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/pallets/subtensor/src/migration.rs b/pallets/subtensor/src/migration.rs index e7e2b9795..866ff08fd 100644 --- a/pallets/subtensor/src/migration.rs +++ b/pallets/subtensor/src/migration.rs @@ -37,7 +37,7 @@ pub fn do_migrate_fix_total_coldkey_stake() -> Weight { // Initialize the weight with one read operation. let mut weight = T::DbWeight::get().reads(1); - // Clear everything from the map first + // Clear everything from the map first, no limit (u32::MAX) let removal_results = TotalColdkeyStake::::clear(u32::MAX, None); // 1 read/write per removal let entries_removed: u64 = removal_results.backend.into(); From eb4a57cd15f8ed3b798a787903c02c7952634328 Mon Sep 17 00:00:00 2001 From: Cameron Fairchild Date: Tue, 16 Jul 2024 18:14:25 -0400 Subject: [PATCH 5/6] rename test --- pallets/subtensor/tests/migration.rs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/pallets/subtensor/tests/migration.rs b/pallets/subtensor/tests/migration.rs index 088e00bc9..d47155862 100644 --- a/pallets/subtensor/tests/migration.rs +++ b/pallets/subtensor/tests/migration.rs @@ -392,9 +392,9 @@ fn test_migrate_fix_total_coldkey_stake_runs_once() { }) } -// SKIP_WASM_BUILD=1 RUST_LOG=info cargo test --test migration -- test_migrate_fix_total_coldkey_stake_starts_with_value_no_more_entries --exact --nocapture +// 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 #[test] -fn test_migrate_fix_total_coldkey_stake_starts_with_value_no_more_entries() { +fn test_migrate_fix_total_coldkey_stake_starts_with_value_no_stake_map_entries() { new_test_ext(1).execute_with(|| { let migration_name = "fix_total_coldkey_stake_v7"; let coldkey = U256::from(0); From 23f540c331959cae2441ebc8c88aa191c8f0c31c Mon Sep 17 00:00:00 2001 From: Cameron Fairchild Date: Tue, 16 Jul 2024 19:07:34 -0400 Subject: [PATCH 6/6] add test for emission appends staking hotkeys map --- pallets/subtensor/tests/staking.rs | 32 ++++++++++++++++++++++++++++++ 1 file changed, 32 insertions(+) diff --git a/pallets/subtensor/tests/staking.rs b/pallets/subtensor/tests/staking.rs index 5db439e5b..cb2cac4ef 100644 --- a/pallets/subtensor/tests/staking.rs +++ b/pallets/subtensor/tests/staking.rs @@ -4719,3 +4719,35 @@ fn test_do_schedule_coldkey_swap_regular_user_passes_min_balance() { ); }); } + +// SKIP_WASM_BUILD=1 RUST_LOG=info cargo test --test staking -- test_emission_creates_staking_hotkeys_entry --exact --nocapture +#[test] +fn test_emission_creates_staking_hotkeys_entry() { + new_test_ext(1).execute_with(|| { + let hotkey0 = U256::from(1); + let hotkey1 = U256::from(2); + + let coldkey = U256::from(3); + + // Add to Owner map + Owner::::insert(hotkey0, coldkey); + Owner::::insert(hotkey1, coldkey); + OwnedHotkeys::::insert(coldkey, vec![hotkey0, hotkey1]); + + // Emit through hotkey + SubtensorModule::emit_inflation_through_hotkey_account(&hotkey0, 0, 1_000); + + // Verify StakingHotkeys has an entry + assert_eq!(StakingHotkeys::::get(coldkey).len(), 1); + assert!(StakingHotkeys::::get(coldkey).contains(&hotkey0)); + + // Try again with another emission on hotkey1 + SubtensorModule::emit_inflation_through_hotkey_account(&hotkey1, 0, 2_000); + + // Verify both hotkeys are now in the map + assert_eq!(StakingHotkeys::::get(coldkey).len(), 2); + let final_map = StakingHotkeys::::get(coldkey); + assert!(final_map.contains(&hotkey0)); + assert!(final_map.contains(&hotkey1)); + }) +}