Skip to content

Commit 9f4b90c

Browse files
committed
add new migration method and more testing
1 parent 773670c commit 9f4b90c

File tree

2 files changed

+96
-37
lines changed

2 files changed

+96
-37
lines changed

pallets/subtensor/src/migrations/migrate_fix_pending_emission.rs

Lines changed: 44 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ fn get_account_id_from_ss58<T: Config>(ss58_str: &str) -> Result<T::AccountId, c
1919
fn migrate_pending_emissions_including_null_stake<T: Config>(
2020
old_hotkey: &T::AccountId,
2121
new_hotkey: &T::AccountId,
22+
migration_account: &T::AccountId,
2223
) -> Weight {
2324
let mut weight = T::DbWeight::get().reads(0);
2425
let null_account = &DefaultAccount::<T>::get();
@@ -30,10 +31,10 @@ fn migrate_pending_emissions_including_null_stake<T: Config>(
3031
weight.saturating_accrue(T::DbWeight::get().reads(1));
3132

3233
// Get the stake for the 0x000 key
33-
let null_stake = Stake::<T>::get(&old_hotkey, null_account);
34+
let null_stake = Stake::<T>::get(old_hotkey, null_account);
3435
weight.saturating_accrue(T::DbWeight::get().reads(1));
3536
// Remove
36-
Stake::<T>::remove(&old_hotkey, null_account);
37+
Stake::<T>::remove(old_hotkey, null_account);
3738
weight.saturating_accrue(T::DbWeight::get().writes(1));
3839

3940
let new_total_coldkey_stake =
@@ -45,29 +46,35 @@ fn migrate_pending_emissions_including_null_stake<T: Config>(
4546
}
4647
weight.saturating_accrue(T::DbWeight::get().reads_writes(1, 1));
4748

48-
let new_total_hotkey_stake = TotalHotkeyStake::<T>::get(old_hotkey).saturating_sub(null_stake);
49-
if new_total_hotkey_stake == 0 {
50-
TotalHotkeyStake::<T>::remove(old_hotkey);
51-
} else {
52-
TotalHotkeyStake::<T>::insert(old_hotkey, new_total_hotkey_stake);
53-
}
49+
let new_staking_hotkeys = StakingHotkeys::<T>::get(null_account);
50+
let new_staking_hotkeys = new_staking_hotkeys
51+
.into_iter()
52+
.filter(|hk| hk != old_hotkey)
53+
.collect::<Vec<_>>();
54+
StakingHotkeys::<T>::insert(null_account, new_staking_hotkeys);
5455
weight.saturating_accrue(T::DbWeight::get().reads_writes(1, 1));
5556

56-
// Remove the stake from the total stake and total issuance (since it is re-emitted)
57-
TotalStake::<T>::put(TotalStake::<T>::get().saturating_sub(null_stake));
58-
TotalIssuance::<T>::put(TotalIssuance::<T>::get().saturating_sub(null_stake));
59-
weight.saturating_accrue(T::DbWeight::get().reads_writes(2, 2));
57+
// Insert the stake from the null account to the MIGRATION account under the OLD hotkey
58+
Stake::<T>::insert(old_hotkey, migration_account, null_stake);
59+
TotalColdkeyStake::<T>::insert(
60+
migration_account,
61+
TotalColdkeyStake::<T>::get(migration_account).saturating_add(null_stake),
62+
);
63+
let mut new_staking_hotkeys = StakingHotkeys::<T>::get(migration_account);
64+
if !new_staking_hotkeys.contains(old_hotkey) {
65+
new_staking_hotkeys.push(old_hotkey.clone());
66+
}
67+
StakingHotkeys::<T>::insert(migration_account, new_staking_hotkeys);
68+
weight.saturating_accrue(T::DbWeight::get().reads_writes(2, 3));
6069

6170
// Get the pending emissions for the NEW hotkey
6271
let pending_emissions_new: u64 = PendingdHotkeyEmission::<T>::get(new_hotkey);
6372
weight.saturating_accrue(T::DbWeight::get().reads(1));
6473

65-
// Add stake to the pending emissions for the new hotkey and the old hotkey
74+
// Add the pending emissions for the new hotkey and the old hotkey
6675
PendingdHotkeyEmission::<T>::insert(
6776
new_hotkey,
68-
pending_emissions_new
69-
.saturating_add(pending_emissions_old)
70-
.saturating_add(null_stake),
77+
pending_emissions_new.saturating_add(pending_emissions_old),
7178
);
7279
weight.saturating_accrue(T::DbWeight::get().writes(1));
7380

@@ -80,19 +87,28 @@ pub fn do_migrate_fix_pending_emission<T: Config>() -> Weight {
8087

8188
let taostats_old_hotkey = "5Hddm3iBFD2GLT5ik7LZnT3XJUnRnN8PoeCFgGQgawUVKNm8";
8289
let taostats_new_hotkey = "5GKH9FPPnWSUoeeTJp19wVtd84XqFW4pyK2ijV2GsFbhTrP1";
90+
let migration_coldkey = "5D65DoFbapkYzJK17VRQo3HFs7FmMeicbaQern28UNDPypCT";
8391

8492
let taostats_old_hk_account = get_account_id_from_ss58::<T>(taostats_old_hotkey);
8593
let taostats_new_hk_account = get_account_id_from_ss58::<T>(taostats_new_hotkey);
86-
87-
match (taostats_old_hk_account, taostats_new_hk_account) {
88-
(Ok(taostats_old_hk_acct), Ok(taostats_new_hk_acct)) => {
94+
let migration_ck_account = get_account_id_from_ss58::<T>(migration_coldkey);
95+
96+
match (
97+
taostats_old_hk_account,
98+
taostats_new_hk_account,
99+
migration_ck_account.clone(),
100+
) {
101+
(Ok(taostats_old_hk_acct), Ok(taostats_new_hk_acct), Ok(migration_ck_account)) => {
89102
weight.saturating_accrue(migrate_pending_emissions_including_null_stake::<T>(
90103
&taostats_old_hk_acct,
91104
&taostats_new_hk_acct,
105+
&migration_ck_account,
92106
));
107+
log::info!("Migrated pending emissions from taostats old hotkey to new hotkey");
93108
}
94109
_ => {
95110
log::warn!("Failed to get account id from ss58 for taostats hotkeys");
111+
return weight;
96112
}
97113
}
98114

@@ -102,15 +118,22 @@ pub fn do_migrate_fix_pending_emission<T: Config>() -> Weight {
102118
let datura_old_hk_account = get_account_id_from_ss58::<T>(datura_old_hotkey);
103119
let datura_new_hk_account = get_account_id_from_ss58::<T>(datura_new_hotkey);
104120

105-
match (datura_old_hk_account, datura_new_hk_account) {
106-
(Ok(datura_old_hk_acct), Ok(datura_new_hk_acct)) => {
121+
match (
122+
datura_old_hk_account,
123+
datura_new_hk_account,
124+
migration_ck_account,
125+
) {
126+
(Ok(datura_old_hk_acct), Ok(datura_new_hk_acct), Ok(migration_ck_account)) => {
107127
weight.saturating_accrue(migrate_pending_emissions_including_null_stake::<T>(
108128
&datura_old_hk_acct,
109129
&datura_new_hk_acct,
130+
&migration_ck_account,
110131
));
132+
log::info!("Migrated pending emissions from datura old hotkey to new hotkey");
111133
}
112134
_ => {
113135
log::warn!("Failed to get account id from ss58 for datura hotkeys");
136+
return weight;
114137
}
115138
}
116139

pallets/subtensor/tests/migration.rs

Lines changed: 52 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -463,20 +463,22 @@ fn test_migrate_fix_pending_emissions() {
463463
new_test_ext(1).execute_with(|| {
464464
let migration_name = "fix_pending_emission";
465465

466-
let null_account = U256::from(0); // The null account
467-
let rand_coldkeys = [U256::from(1), U256::from(2), U256::from(3), U256::from(4)];
466+
let null_account = &U256::from(0); // The null account
468467

469468
let taostats_old_hotkey = "5Hddm3iBFD2GLT5ik7LZnT3XJUnRnN8PoeCFgGQgawUVKNm8";
470469
let taostats_new_hotkey = "5GKH9FPPnWSUoeeTJp19wVtd84XqFW4pyK2ijV2GsFbhTrP1";
471470

472-
let taostats_old_hk_account: AccountId = get_account_id_from_ss58(taostats_old_hotkey);
473-
let taostats_new_hk_account: AccountId = get_account_id_from_ss58(taostats_new_hotkey);
471+
let taostats_old_hk_account: &AccountId = &get_account_id_from_ss58(taostats_old_hotkey);
472+
let taostats_new_hk_account: &AccountId = &get_account_id_from_ss58(taostats_new_hotkey);
474473

475474
let datura_old_hotkey = "5FKstHjZkh4v3qAMSBa1oJcHCLjxYZ8SNTSz1opTv4hR7gVB";
476475
let datura_new_hotkey = "5GP7c3fFazW9GXK8Up3qgu2DJBk8inu4aK9TZy3RuoSWVCMi";
477476

478-
let datura_old_hk_account: AccountId = get_account_id_from_ss58(datura_old_hotkey);
479-
let datura_new_hk_account: AccountId = get_account_id_from_ss58(datura_new_hotkey);
477+
let datura_old_hk_account: &AccountId = &get_account_id_from_ss58(datura_old_hotkey);
478+
let datura_new_hk_account: &AccountId = &get_account_id_from_ss58(datura_new_hotkey);
479+
480+
let migration_coldkey = "5D65DoFbapkYzJK17VRQo3HFs7FmMeicbaQern28UNDPypCT";
481+
let migration_account: &AccountId = &get_account_id_from_ss58(migration_coldkey);
480482

481483
// "Issue" the TAO we're going to insert to stake
482484
let null_stake_datura = 123_456_789;
@@ -491,18 +493,17 @@ fn test_migrate_fix_pending_emissions() {
491493
// Setup the old Datura hotkey with a pending emission
492494
PendingdHotkeyEmission::<Test>::insert(datura_old_hk_account, 10_000);
493495
// Setup the NEW Datura hotkey with a pending emission
494-
PendingdHotkeyEmission::<Test>::insert(datura_new_hk_account, null_stake_datura);
495-
Stake::<Test>::insert(datura_old_hk_account, null_account, 123_456_789);
496-
let expected_datura_new_hk_pending_emission: u64 = 123_456_789 + 10_000 + null_stake_datura;
496+
PendingdHotkeyEmission::<Test>::insert(datura_new_hk_account, 123_456_789);
497+
Stake::<Test>::insert(datura_old_hk_account, null_account, null_stake_datura);
498+
let expected_datura_new_hk_pending_emission: u64 = 123_456_789 + 10_000;
497499

498500
// Setup the old TaoStats hotkey with a pending emission
499501
PendingdHotkeyEmission::<Test>::insert(taostats_old_hk_account, 987_654);
500502
// Setup the new TaoStats hotkey with a pending emission
501503
PendingdHotkeyEmission::<Test>::insert(taostats_new_hk_account, 100_000);
502504
// Setup the old TaoStats hotkey with a null-key stake entry
503505
Stake::<Test>::insert(taostats_old_hk_account, null_account, null_stake_tao_stats);
504-
let expected_taostats_new_hk_pending_emission: u64 =
505-
987_654 + 100_000 + null_stake_tao_stats;
506+
let expected_taostats_new_hk_pending_emission: u64 = 987_654 + 100_000;
506507

507508
let total_issuance_before = SubtensorModule::get_total_issuance();
508509

@@ -537,17 +538,52 @@ fn test_migrate_fix_pending_emissions() {
537538
assert_eq!(Stake::<Test>::get(datura_old_hk_account, null_account), 0);
538539
assert_eq!(Stake::<Test>::get(taostats_old_hk_account, null_account), 0);
539540

540-
// Check the total issuance is decreased by the null stake removed
541-
let expected_total_issuance = total_issuance_before - null_stake_total;
541+
// Check the total issuance is the SAME following migration (no TAO issued)
542+
let expected_total_issuance = total_issuance_before;
542543
assert_eq!(
543544
SubtensorModule::get_total_issuance(),
544545
expected_total_issuance
545546
);
546547

547-
// Check total stake is decreased by the null stake removed
548+
// Check total stake is the SAME following the migration (no new TAO staked)
548549
assert_eq!(TotalStake::<Test>::get(), expected_total_issuance);
550+
// Check the total stake maps are updated following the migration (removal of old null_account stake entries)
549551
assert_eq!(TotalColdkeyStake::<Test>::get(null_account), 0);
550-
assert_eq!(TotalHotkeyStake::<Test>::get(datura_old_hk_account), 0);
551-
assert_eq!(TotalHotkeyStake::<Test>::get(taostats_old_hk_account), 0);
552+
assert_eq!(
553+
SubtensorModule::get_stake_for_coldkey_and_hotkey(null_account, datura_old_hk_account),
554+
0
555+
);
556+
assert_eq!(
557+
SubtensorModule::get_stake_for_coldkey_and_hotkey(
558+
null_account,
559+
taostats_old_hk_account
560+
),
561+
0
562+
);
563+
564+
// Check staking hotkeys is updated
565+
assert_eq!(StakingHotkeys::<Test>::get(null_account), vec![]);
566+
567+
// Check the migration key has stake with both *old* hotkeys
568+
assert_eq!(
569+
SubtensorModule::get_stake_for_coldkey_and_hotkey(
570+
migration_account,
571+
datura_old_hk_account
572+
),
573+
null_stake_datura
574+
);
575+
assert_eq!(
576+
SubtensorModule::get_stake_for_coldkey_and_hotkey(
577+
migration_account,
578+
taostats_old_hk_account
579+
),
580+
null_stake_tao_stats
581+
);
582+
assert_eq!(
583+
TotalColdkeyStake::<Test>::get(migration_account),
584+
null_stake_total
585+
);
586+
assert!(StakingHotkeys::<Test>::get(migration_account).contains(&datura_old_hk_account));
587+
assert!(StakingHotkeys::<Test>::get(migration_account).contains(&taostats_old_hk_account));
552588
})
553589
}

0 commit comments

Comments
 (0)