|
1 | | -use crate::tests::mock::{RuntimeOrigin, SubtensorModule, Test, add_dynamic_network, new_test_ext}; |
| 1 | +use crate::tests::mock::{ |
| 2 | + RuntimeOrigin, SubtensorModule, Test, add_dynamic_network, new_test_ext, run_to_block, |
| 3 | +}; |
2 | 4 | use crate::{ |
3 | 5 | NetworksAdded, RootClaimable, SubnetAlphaIn, SubnetMechanism, SubnetTAO, SubtokenEnabled, |
4 | 6 | Tempo, pallet, |
5 | 7 | }; |
6 | 8 | use crate::{RootClaimType, RootClaimTypeEnum, RootClaimed}; |
7 | 9 | use approx::assert_abs_diff_eq; |
8 | 10 | use frame_support::assert_ok; |
9 | | -use sp_core::U256; |
| 11 | +use sp_core::{H256, U256}; |
10 | 12 | use substrate_fixed::types::{I96F32, U96F32}; |
11 | 13 | use subtensor_runtime_common::{AlphaCurrency, Currency, NetUid, TaoCurrency}; |
12 | 14 | use subtensor_swap_interface::SwapHandler; |
@@ -771,3 +773,94 @@ fn test_claim_root_with_run_coinbase() { |
771 | 773 | assert!(new_stake > 0); |
772 | 774 | }); |
773 | 775 | } |
| 776 | + |
| 777 | +#[test] |
| 778 | +fn test_claim_root_bloch_hash_indices() { |
| 779 | + new_test_ext(1).execute_with(|| { |
| 780 | + let k = 15u64; |
| 781 | + let n = 15000u64; |
| 782 | + |
| 783 | + // 1 |
| 784 | + let hash = sp_core::keccak_256(b"some"); |
| 785 | + let mut indices = SubtensorModule::block_hash_to_indices(H256(hash), k, n); |
| 786 | + indices.sort(); |
| 787 | + |
| 788 | + assert!(indices.len() <= k as usize); |
| 789 | + assert!(!indices.iter().any(|i| *i >= n)); |
| 790 | + // precomputed values |
| 791 | + let expected_result = vec![ |
| 792 | + 265, 630, 1286, 1558, 4496, 4861, 5517, 5789, 6803, 8096, 9092, 11034, 11399, 12055, |
| 793 | + 12327, |
| 794 | + ]; |
| 795 | + assert_eq!(indices, expected_result); |
| 796 | + |
| 797 | + // 2 |
| 798 | + let hash = sp_core::keccak_256(b"some2"); |
| 799 | + let mut indices = SubtensorModule::block_hash_to_indices(H256(hash), k, n); |
| 800 | + indices.sort(); |
| 801 | + |
| 802 | + assert!(indices.len() <= k as usize); |
| 803 | + assert!(!indices.iter().any(|i| *i >= n)); |
| 804 | + // precomputed values |
| 805 | + let expected_result = vec![ |
| 806 | + 61, 246, 1440, 2855, 3521, 5236, 6130, 6615, 8511, 9405, 9890, 11786, 11971, 13165, |
| 807 | + 14580, |
| 808 | + ]; |
| 809 | + assert_eq!(indices, expected_result); |
| 810 | + }); |
| 811 | +} |
| 812 | + |
| 813 | +#[test] |
| 814 | +fn test_claim_root_with_block_emissions() { |
| 815 | + new_test_ext(0).execute_with(|| { |
| 816 | + let owner_coldkey = U256::from(1001); |
| 817 | + let hotkey = U256::from(1002); |
| 818 | + let coldkey = U256::from(1003); |
| 819 | + let netuid = add_dynamic_network(&hotkey, &owner_coldkey); |
| 820 | + |
| 821 | + Tempo::<Test>::insert(netuid, 1); |
| 822 | + SubtensorModule::set_tao_weight(u64::MAX); // Set TAO weight to 1.0 |
| 823 | + |
| 824 | + let root_stake = 200_000_000u64; |
| 825 | + SubnetTAO::<Test>::insert(NetUid::ROOT, TaoCurrency::from(root_stake)); |
| 826 | + |
| 827 | + SubtensorModule::increase_stake_for_hotkey_and_coldkey_on_subnet( |
| 828 | + &hotkey, |
| 829 | + &coldkey, |
| 830 | + NetUid::ROOT, |
| 831 | + root_stake.into(), |
| 832 | + ); |
| 833 | + SubtensorModule::maybe_add_coldkey_index(&coldkey); |
| 834 | + |
| 835 | + let initial_total_hotkey_alpha = 10_000_000u64; |
| 836 | + SubtensorModule::increase_stake_for_hotkey_and_coldkey_on_subnet( |
| 837 | + &hotkey, |
| 838 | + &owner_coldkey, |
| 839 | + netuid, |
| 840 | + initial_total_hotkey_alpha.into(), |
| 841 | + ); |
| 842 | + |
| 843 | + assert_ok!(SubtensorModule::set_root_claim_type( |
| 844 | + RuntimeOrigin::signed(coldkey), |
| 845 | + RootClaimTypeEnum::Keep |
| 846 | + ),); |
| 847 | + assert_eq!(RootClaimType::<Test>::get(coldkey), RootClaimTypeEnum::Keep); |
| 848 | + |
| 849 | + // Distribute pending root alpha |
| 850 | + |
| 851 | + let initial_stake: u64 = |
| 852 | + SubtensorModule::get_stake_for_hotkey_and_coldkey_on_subnet(&hotkey, &coldkey, netuid) |
| 853 | + .into(); |
| 854 | + assert_eq!(initial_stake, 0u64); |
| 855 | + |
| 856 | + run_to_block(2); |
| 857 | + |
| 858 | + // Check stake after block emissions |
| 859 | + |
| 860 | + let new_stake: u64 = |
| 861 | + SubtensorModule::get_stake_for_hotkey_and_coldkey_on_subnet(&hotkey, &coldkey, netuid) |
| 862 | + .into(); |
| 863 | + |
| 864 | + assert!(new_stake > 0); |
| 865 | + }); |
| 866 | +} |
0 commit comments