Skip to content

Commit 278eb87

Browse files
committed
tests for new migration
1 parent 6a2eeb6 commit 278eb87

File tree

1 file changed

+86
-1
lines changed

1 file changed

+86
-1
lines changed

pallets/subtensor/tests/migration.rs

Lines changed: 86 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,12 @@
11
#![allow(unused, clippy::indexing_slicing, clippy::panic, clippy::unwrap_used)]
22
mod mock;
3+
use codec::{Decode, Encode};
34
use frame_support::{assert_ok, weights::Weight};
45
use frame_system::Config;
56
use mock::*;
67
use pallet_subtensor::*;
7-
use sp_core::U256;
8+
use sp_core::{crypto::Ss58Codec, U256};
9+
use substrate_fixed::types::extra::U2;
810

911
#[test]
1012
fn test_initialise_ti() {
@@ -430,3 +432,86 @@ fn run_migration_and_check(migration_name: &'static str) -> frame_support::weigh
430432
// Return the weight of the executed migration
431433
weight
432434
}
435+
436+
fn run_pending_emissions_migration_and_check(
437+
migration_name: &'static str,
438+
) -> frame_support::weights::Weight {
439+
// Execute the migration and store its weight
440+
let weight: frame_support::weights::Weight =
441+
pallet_subtensor::migrations::migrate_fix_pending_emission::migrate_fix_pending_emission::<
442+
Test,
443+
>();
444+
445+
// Check if the migration has been marked as completed
446+
assert!(HasMigrationRun::<Test>::get(
447+
migration_name.as_bytes().to_vec()
448+
));
449+
450+
// Return the weight of the executed migration
451+
weight
452+
}
453+
454+
fn get_account_id_from_ss58(ss58_str: &str) -> U256 {
455+
let account_id = sp_core::crypto::AccountId32::from_ss58check(ss58_str).unwrap();
456+
let account_id = AccountId::decode(&mut account_id.as_ref()).unwrap();
457+
account_id
458+
}
459+
460+
// SKIP_WASM_BUILD=1 RUST_LOG=info cargo test --test migration -- test_migrate_fix_pending_emissions --exact --nocapture
461+
#[test]
462+
fn test_migrate_fix_pending_emissions() {
463+
new_test_ext(1).execute_with(|| {
464+
let migration_name = "fix_pending_emission";
465+
466+
let null_account = U256::from(0); // The null account
467+
let rand_coldkeys = vec![U256::from(1), U256::from(2), U256::from(3), U256::from(4)];
468+
469+
let taostats_old_hotkey = "5Hddm3iBFD2GLT5ik7LZnT3XJUnRnN8PoeCFgGQgawUVKNm8";
470+
let taostats_new_hotkey = "5GKH9FPPnWSUoeeTJp19wVtd84XqFW4pyK2ijV2GsFbhTrP1";
471+
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);
474+
475+
let datura_old_hotkey = "5FKstHjZkh4v3qAMSBa1oJcHCLjxYZ8SNTSz1opTv4hR7gVB";
476+
let datura_new_hotkey = "5GP7c3fFazW9GXK8Up3qgu2DJBk8inu4aK9TZy3RuoSWVCMi";
477+
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);
480+
481+
// Setup the old Datura hotkey with a pending emission
482+
PendingdHotkeyEmission::<Test>::insert(&datura_old_hk_account, 10_000);
483+
// Setup the NEW Datura hotkey with a pending emission
484+
PendingdHotkeyEmission::<Test>::insert(&datura_new_hk_account, 123_456_789);
485+
let expected_datura_new_hk_pending_emission: u64 = 123_456_789 + 10_000;
486+
487+
// Setup the old TaoStats hotkey with a pending emission
488+
PendingdHotkeyEmission::<Test>::insert(&taostats_old_hk_account, 987_654);
489+
// Setup the new TaoStats hotkey with a pending emission
490+
PendingdHotkeyEmission::<Test>::insert(&taostats_new_hk_account, 100_000);
491+
// Setup the old TaoStats hotkey with a null-key stake entry
492+
Stake::<Test>::insert(&taostats_old_hk_account, &null_account, 123_456_789);
493+
let expected_taostats_new_hk_pending_emission: u64 = 987_654 + 100_000 + 123_456_789;
494+
495+
// Run migration
496+
let first_weight = run_pending_emissions_migration_and_check(migration_name);
497+
assert!(first_weight != Weight::zero());
498+
499+
// Check the pending emission is added to new Datura hotkey
500+
assert_eq!(
501+
PendingdHotkeyEmission::<Test>::get(datura_new_hk_account),
502+
expected_datura_new_hk_pending_emission
503+
);
504+
505+
// Check the pending emission is added to new the TaoStats hotkey
506+
assert_eq!(
507+
PendingdHotkeyEmission::<Test>::get(taostats_new_hk_account),
508+
expected_taostats_new_hk_pending_emission
509+
);
510+
511+
// Check the pending emission is added to new the Datura hotkey
512+
assert_eq!(
513+
PendingdHotkeyEmission::<Test>::get(datura_new_hk_account),
514+
expected_datura_new_hk_pending_emission
515+
);
516+
})
517+
}

0 commit comments

Comments
 (0)