|
1 | 1 | use super::mock::*;
|
2 | 2 | use crate::*;
|
| 3 | +use frame_support::{ |
| 4 | + assert_ok, |
| 5 | + dispatch::{GetDispatchInfo, Pays}, |
| 6 | + weights::Weight, |
| 7 | +}; |
3 | 8 | use sp_core::U256;
|
4 | 9 | use substrate_fixed::types::I96F32;
|
5 | 10 |
|
@@ -557,3 +562,64 @@ fn test_share_based_staking_stake_inject_stake_new() {
|
557 | 562 | });
|
558 | 563 | });
|
559 | 564 | }
|
| 565 | + |
| 566 | +// SKIP_WASM_BUILD=1 RUST_LOG=debug cargo test --package pallet-subtensor --lib -- tests::staking2::test_try_associate_hotkey --exact --show-output --nocapture |
| 567 | +#[test] |
| 568 | +fn test_try_associate_hotkey() { |
| 569 | + new_test_ext(1).execute_with(|| { |
| 570 | + let hotkey1 = U256::from(1); |
| 571 | + let coldkey1 = U256::from(2); |
| 572 | + let coldkey2 = U256::from(3); |
| 573 | + |
| 574 | + // Check initial association |
| 575 | + assert!(!SubtensorModule::hotkey_account_exists(&hotkey1)); |
| 576 | + |
| 577 | + // Associate hotkey1 with coldkey1 |
| 578 | + assert_ok!(SubtensorModule::try_associate_hotkey( |
| 579 | + RuntimeOrigin::signed(coldkey1), |
| 580 | + hotkey1 |
| 581 | + )); |
| 582 | + |
| 583 | + // Check that hotkey1 is associated with coldkey1 |
| 584 | + assert!(SubtensorModule::hotkey_account_exists(&hotkey1)); |
| 585 | + assert_eq!( |
| 586 | + SubtensorModule::get_owning_coldkey_for_hotkey(&hotkey1), |
| 587 | + coldkey1 |
| 588 | + ); |
| 589 | + assert_ne!(SubtensorModule::get_owned_hotkeys(&coldkey1).len(), 0); |
| 590 | + assert!(SubtensorModule::get_owned_hotkeys(&coldkey1).contains(&hotkey1)); |
| 591 | + |
| 592 | + // Verify this tx requires a fee |
| 593 | + let call = |
| 594 | + RuntimeCall::SubtensorModule(crate::Call::try_associate_hotkey { hotkey: hotkey1 }); |
| 595 | + let dispatch_info = call.get_dispatch_info(); |
| 596 | + // Verify tx weight > 0 |
| 597 | + assert!(dispatch_info.weight.all_gte(Weight::from_all(0))); |
| 598 | + // Verify pays Yes is set |
| 599 | + assert_eq!(dispatch_info.pays_fee, Pays::Yes); |
| 600 | + |
| 601 | + // Check that coldkey2 is not associated with any hotkey |
| 602 | + assert!(!SubtensorModule::get_owned_hotkeys(&coldkey2).contains(&hotkey1)); |
| 603 | + assert_eq!(SubtensorModule::get_owned_hotkeys(&coldkey2).len(), 0); |
| 604 | + |
| 605 | + // Try to associate hotkey1 with coldkey2 |
| 606 | + // Should have no effect because coldkey1 is already associated with hotkey1 |
| 607 | + assert_ok!(SubtensorModule::try_associate_hotkey( |
| 608 | + RuntimeOrigin::signed(coldkey2), |
| 609 | + hotkey1 |
| 610 | + )); |
| 611 | + |
| 612 | + // Check that hotkey1 is still associated with coldkey1 |
| 613 | + assert!(SubtensorModule::hotkey_account_exists(&hotkey1)); |
| 614 | + assert_eq!( |
| 615 | + SubtensorModule::get_owning_coldkey_for_hotkey(&hotkey1), |
| 616 | + coldkey1 |
| 617 | + ); |
| 618 | + assert_ne!(SubtensorModule::get_owned_hotkeys(&coldkey1).len(), 0); |
| 619 | + assert!(SubtensorModule::get_owned_hotkeys(&coldkey1).contains(&hotkey1)); |
| 620 | + |
| 621 | + // Check that coldkey2 is still not associated with any hotkey |
| 622 | + assert!(!SubtensorModule::get_owned_hotkeys(&coldkey2).contains(&hotkey1)); |
| 623 | + assert_eq!(SubtensorModule::get_owned_hotkeys(&coldkey2).len(), 0); |
| 624 | + }); |
| 625 | +} |
0 commit comments