@@ -3692,111 +3692,121 @@ fn test_add_stake_specific_stake_into_subnet_fail() {
3692
3692
} ) ;
3693
3693
}
3694
3694
3695
+ // cargo test --package pallet-subtensor --lib -- tests::staking::test_remove_99_999_per_cent_stake_removes_all --exact --show-output
3695
3696
#[ test]
3696
- // RUST_LOG=info cargo test --package pallet-subtensor --lib -- tests::staking::test_move_stake_specific_stake_into_subnet_fail --exact --show-output
3697
- fn test_move_stake_specific_stake_into_subnet_fail ( ) {
3697
+ fn test_remove_99_9991_per_cent_stake_removes_all ( ) {
3698
+ // When we remove stake, the total issuance of the balances pallet should not change
3699
+ // this is because the stake should be part of the coldkey account balance (reserved/locked)
3700
+ // then the removed stake just becomes free balance
3698
3701
new_test_ext ( 1 ) . execute_with ( || {
3699
- let sn_owner_coldkey = U256 :: from ( 55453 ) ;
3700
-
3701
- let hotkey_account_id = U256 :: from ( 533453 ) ;
3702
- let coldkey_account_id = U256 :: from ( 55454 ) ;
3703
- let hotkey_owner_account_id = U256 :: from ( 533454 ) ;
3704
-
3705
- let existing_shares: U64F64 =
3706
- U64F64 :: from_num ( 161_986_254 ) . saturating_div ( U64F64 :: from_num ( u64:: MAX ) ) ;
3707
- let existing_stake = 36_711_495_953 ;
3708
-
3709
- let tao_in = 2_409_892_148_947 ;
3710
- let alpha_in = 15_358_708_513_716 ;
3711
-
3712
- let tao_staked = 200_000_000 ;
3702
+ let subnet_owner_coldkey = U256 :: from ( 1 ) ;
3703
+ let subnet_owner_hotkey = U256 :: from ( 2 ) ;
3704
+ let hotkey_account_id = U256 :: from ( 581337 ) ;
3705
+ let coldkey_account_id = U256 :: from ( 81337 ) ;
3706
+ let amount = 10_000_000_000 ;
3707
+ let netuid: u16 = add_dynamic_network ( & subnet_owner_hotkey, & subnet_owner_coldkey) ;
3708
+ let fee = DefaultStakingFee :: < Test > :: get ( ) ;
3709
+ register_ok_neuron ( netuid, hotkey_account_id, coldkey_account_id, 192213123 ) ;
3713
3710
3714
- //add network
3715
- let netuid : u16 = add_dynamic_network ( & sn_owner_coldkey , & sn_owner_coldkey ) ;
3711
+ // Give it some $$$ in his coldkey balance
3712
+ SubtensorModule :: add_balance_to_coldkey_account ( & coldkey_account_id , amount ) ;
3716
3713
3717
- let origin_netuid: u16 = add_dynamic_network ( & sn_owner_coldkey, & sn_owner_coldkey) ;
3714
+ // Stake to hotkey account, and check if the result is ok
3715
+ assert_ok ! ( SubtensorModule :: add_stake(
3716
+ RuntimeOrigin :: signed( coldkey_account_id) ,
3717
+ hotkey_account_id,
3718
+ netuid,
3719
+ amount
3720
+ ) ) ;
3718
3721
3719
- // Register hotkey on netuid
3720
- register_ok_neuron ( netuid, hotkey_account_id, hotkey_owner_account_id, 0 ) ;
3721
- // Register hotkey on origin netuid
3722
- register_ok_neuron ( origin_netuid, hotkey_account_id, hotkey_owner_account_id, 0 ) ;
3722
+ // Remove 99.9991% stake
3723
+ let alpha = SubtensorModule :: get_stake_for_hotkey_and_coldkey_on_subnet (
3724
+ & hotkey_account_id,
3725
+ & coldkey_account_id,
3726
+ netuid,
3727
+ ) ;
3728
+ assert_ok ! ( SubtensorModule :: remove_stake(
3729
+ RuntimeOrigin :: signed( coldkey_account_id) ,
3730
+ hotkey_account_id,
3731
+ netuid,
3732
+ ( U64F64 :: from_num( alpha) * U64F64 :: from_num( 0.999991 ) ) . to_num:: <u64 >( )
3733
+ ) ) ;
3723
3734
3724
- // Check we have zero staked
3735
+ // Check that all alpha was unstaked and all TAO balance was returned (less fees)
3736
+ assert_abs_diff_eq ! (
3737
+ SubtensorModule :: get_coldkey_balance( & coldkey_account_id) ,
3738
+ amount - fee * 2 ,
3739
+ epsilon = 10000 ,
3740
+ ) ;
3725
3741
assert_eq ! (
3726
3742
SubtensorModule :: get_total_stake_for_hotkey( & hotkey_account_id) ,
3727
3743
0
3728
3744
) ;
3729
-
3730
- // Set a hotkey pool for the hotkey on destination subnet
3731
- let mut hotkey_pool = SubtensorModule :: get_alpha_share_pool ( hotkey_account_id, netuid) ;
3732
- hotkey_pool. update_value_for_one ( & hotkey_owner_account_id, 1234 ) ; // Doesn't matter, will be overridden
3733
-
3734
- // Adjust the total hotkey stake and shares to match the existing values
3735
- TotalHotkeyShares :: < Test > :: insert ( hotkey_account_id, netuid, existing_shares) ;
3736
- TotalHotkeyAlpha :: < Test > :: insert ( hotkey_account_id, netuid, existing_stake) ;
3737
-
3738
- // Make the hotkey a delegate
3739
- Delegates :: < Test > :: insert ( hotkey_account_id, 0 ) ;
3740
-
3741
- // Setup Subnet pool
3742
- SubnetAlphaIn :: < Test > :: insert ( netuid, alpha_in) ;
3743
- SubnetTAO :: < Test > :: insert ( netuid, tao_in) ;
3744
-
3745
- // Give TAO balance to coldkey
3746
- SubtensorModule :: add_balance_to_coldkey_account (
3745
+ let new_alpha = SubtensorModule :: get_stake_for_hotkey_and_coldkey_on_subnet (
3746
+ & hotkey_account_id,
3747
3747
& coldkey_account_id,
3748
- tao_staked + 1_000_000_000 ,
3748
+ netuid ,
3749
3749
) ;
3750
+ assert_eq ! ( new_alpha, 0 ) ;
3751
+ } ) ;
3752
+ }
3753
+
3754
+ // cargo test --package pallet-subtensor --lib -- tests::staking::test_remove_99_9989_per_cent_stake_leaves_a_little --exact --show-output
3755
+ #[ test]
3756
+ fn test_remove_99_9989_per_cent_stake_leaves_a_little ( ) {
3757
+ // When we remove stake, the total issuance of the balances pallet should not change
3758
+ // this is because the stake should be part of the coldkey account balance (reserved/locked)
3759
+ // then the removed stake just becomes free balance
3760
+ new_test_ext ( 1 ) . execute_with ( || {
3761
+ let subnet_owner_coldkey = U256 :: from ( 1 ) ;
3762
+ let subnet_owner_hotkey = U256 :: from ( 2 ) ;
3763
+ let hotkey_account_id = U256 :: from ( 581337 ) ;
3764
+ let coldkey_account_id = U256 :: from ( 81337 ) ;
3765
+ let amount = 10_000_000_000 ;
3766
+ let netuid: u16 = add_dynamic_network ( & subnet_owner_hotkey, & subnet_owner_coldkey) ;
3767
+ let fee = DefaultStakingFee :: < Test > :: get ( ) ;
3768
+ register_ok_neuron ( netuid, hotkey_account_id, coldkey_account_id, 192213123 ) ;
3750
3769
3751
- // Setup Subnet pool for origin netuid
3752
- SubnetAlphaIn :: < Test > :: insert ( origin_netuid, alpha_in + 10_000_000 ) ;
3753
- SubnetTAO :: < Test > :: insert ( origin_netuid, tao_in + 10_000_000 ) ;
3770
+ // Give it some $$$ in his coldkey balance
3771
+ SubtensorModule :: add_balance_to_coldkey_account ( & coldkey_account_id, amount) ;
3754
3772
3755
- // Add stake as new hotkey
3773
+ // Stake to hotkey account, and check if the result is ok
3756
3774
assert_ok ! ( SubtensorModule :: add_stake(
3757
3775
RuntimeOrigin :: signed( coldkey_account_id) ,
3758
3776
hotkey_account_id,
3759
- origin_netuid,
3760
- tao_staked,
3761
- ) , ) ;
3762
- let alpha_to_move = SubtensorModule :: get_stake_for_hotkey_and_coldkey_on_subnet (
3777
+ netuid,
3778
+ amount
3779
+ ) ) ;
3780
+
3781
+ // Remove 99.9989% stake
3782
+ let alpha = SubtensorModule :: get_stake_for_hotkey_and_coldkey_on_subnet (
3763
3783
& hotkey_account_id,
3764
3784
& coldkey_account_id,
3765
- origin_netuid ,
3785
+ netuid ,
3766
3786
) ;
3767
-
3768
- // Move stake to destination subnet
3769
- assert_ok ! ( SubtensorModule :: move_stake(
3787
+ assert_ok ! ( SubtensorModule :: remove_stake(
3770
3788
RuntimeOrigin :: signed( coldkey_account_id) ,
3771
3789
hotkey_account_id,
3772
- hotkey_account_id,
3773
- origin_netuid,
3774
3790
netuid,
3775
- alpha_to_move ,
3791
+ ( U64F64 :: from_num ( alpha ) * U64F64 :: from_num ( 0.99 ) ) . to_num :: < u64 > ( )
3776
3792
) ) ;
3777
3793
3778
- // Check that the stake has been moved
3779
- assert_eq ! (
3780
- SubtensorModule :: get_stake_for_hotkey_and_coldkey_on_subnet(
3781
- & hotkey_account_id,
3782
- & coldkey_account_id,
3783
- origin_netuid
3784
- ) ,
3785
- 0
3794
+ // Check that all alpha was unstaked and 99% TAO balance was returned (less fees)
3795
+ assert_abs_diff_eq ! (
3796
+ SubtensorModule :: get_coldkey_balance( & coldkey_account_id) ,
3797
+ ( amount as f64 * 0.99 ) as u64 - fee * 2 ,
3798
+ epsilon = amount / 1000 ,
3786
3799
) ;
3787
- let fee = DefaultStakingFee :: < Test > :: get ( ) ;
3788
- let alpha_fee: I96F32 = I96F32 :: from_num ( fee) / SubtensorModule :: get_alpha_price ( netuid) ;
3789
- let expected_value = I96F32 :: from_num ( alpha_to_move)
3790
- * SubtensorModule :: get_alpha_price ( origin_netuid)
3791
- / SubtensorModule :: get_alpha_price ( netuid) ;
3792
3800
assert_abs_diff_eq ! (
3793
- SubtensorModule :: get_stake_for_hotkey_and_coldkey_on_subnet(
3794
- & hotkey_account_id,
3795
- & coldkey_account_id,
3796
- netuid
3797
- ) ,
3798
- ( expected_value - alpha_fee) . to_num:: <u64 >( ) ,
3799
- epsilon = ( expected_value / 1000 ) . to_num:: <u64 >( )
3801
+ SubtensorModule :: get_total_stake_for_hotkey( & hotkey_account_id) ,
3802
+ ( amount as f64 * 0.01 ) as u64 ,
3803
+ epsilon = amount / 1000 ,
3804
+ ) ;
3805
+ let new_alpha = SubtensorModule :: get_stake_for_hotkey_and_coldkey_on_subnet (
3806
+ & hotkey_account_id,
3807
+ & coldkey_account_id,
3808
+ netuid,
3800
3809
) ;
3810
+ assert_abs_diff_eq ! ( new_alpha, ( alpha as f64 * 0.01 ) as u64 , epsilon = 10 ) ;
3801
3811
} ) ;
3802
3812
}
0 commit comments