11#![ allow( clippy:: expect_used) ]
22
33use crate :: tests:: mock:: {
4- RuntimeOrigin , SubtensorModule , Test , add_dynamic_network, new_test_ext, run_to_block,
4+ RuntimeEvent , RuntimeOrigin , SubtensorModule , System , Test , add_dynamic_network, new_test_ext,
5+ run_to_block, step_block,
56} ;
67use crate :: {
7- DefaultMinRootClaimAmount , Error , MAX_NUM_ROOT_CLAIMS , MAX_ROOT_CLAIM_THRESHOLD , NetworksAdded ,
8- NumRootClaim , NumStakingColdkeys , PendingRootAlphaDivs , RootClaimable , RootClaimableThreshold ,
9- StakingColdkeys , StakingColdkeysByIndex , SubnetAlphaIn , SubnetMechanism , SubnetMovingPrice ,
10- SubnetTAO , SubnetTaoFlow , SubtokenEnabled , Tempo , pallet,
8+ DefaultMinRootClaimAmount , Error , Event , MAX_NUM_ROOT_CLAIMS , MAX_ROOT_CLAIM_THRESHOLD ,
9+ NetworksAdded , NumRootClaim , NumStakingColdkeys , PendingRootAlphaDivs , PendingRootClaimedData ,
10+ RootClaimable , RootClaimableThreshold , StakingColdkeys , StakingColdkeysByIndex , SubnetAlphaIn ,
11+ SubnetMechanism , SubnetMovingPrice , SubnetTAO , SubnetTaoFlow , SubtokenEnabled , Tempo , pallet,
1112} ;
12- use crate :: { RootAlphaDividendsPerSubnet , SubnetRootClaimKeep , SubnetRootClaimSwap } ;
13+ use crate :: { PendingSubnetRootClaimData , RootAlphaDividendsPerSubnet } ;
1314use crate :: { RootClaimType , RootClaimTypeEnum , RootClaimed } ;
1415use approx:: assert_abs_diff_eq;
1516use frame_support:: dispatch:: RawOrigin ;
@@ -18,7 +19,7 @@ use frame_support::traits::Get;
1819use frame_support:: { assert_err, assert_noop, assert_ok} ;
1920use sp_core:: { H256 , U256 } ;
2021use sp_runtime:: DispatchError ;
21- use std:: collections:: BTreeSet ;
22+ use std:: collections:: { BTreeMap , BTreeSet } ;
2223use substrate_fixed:: types:: { I96F32 , U64F64 , U96F32 } ;
2324use subtensor_runtime_common:: { AlphaCurrency , Currency , NetUid , TaoCurrency } ;
2425use subtensor_swap_interface:: SwapHandler ;
@@ -1861,7 +1862,7 @@ fn test_claim_root_subnet_root_claim_map_keep() {
18611862 RootClaimTypeEnum :: Keep
18621863 ) , ) ;
18631864 assert_eq ! ( RootClaimType :: <Test >:: get( coldkey) , RootClaimTypeEnum :: Keep ) ;
1864- assert ! ( !SubnetRootClaimKeep :: <Test >:: contains_key( netuid) ) ;
1865+ assert ! ( !PendingSubnetRootClaimData :: <Test >:: contains_key( netuid) ) ;
18651866
18661867 assert_ok ! ( SubtensorModule :: claim_root(
18671868 RuntimeOrigin :: signed( coldkey) ,
@@ -1873,12 +1874,46 @@ fn test_claim_root_subnet_root_claim_map_keep() {
18731874 let new_stake =
18741875 SubtensorModule :: get_stake_for_hotkey_and_coldkey_on_subnet ( & hotkey, & coldkey, netuid) ;
18751876
1876- assert_eq ! ( new_stake , SubnetRootClaimKeep :: <Test >:: get ( netuid) , ) ;
1877+ assert ! ( PendingSubnetRootClaimData :: <Test >:: contains_key ( netuid) ) ;
18771878
1878- // Check SubnetRootClaimKeep after network deregistration
1879+ let root_claim_data = PendingSubnetRootClaimData :: < Test > :: get ( netuid ) ;
18791880
1880- assert_ok ! ( SubtensorModule :: do_dissolve_network( netuid) ) ;
1881- assert ! ( !SubnetRootClaimKeep :: <Test >:: contains_key( netuid) ) ;
1881+ assert_eq ! ( new_stake, root_claim_data. alpha_kept) ;
1882+
1883+ assert_eq ! ( root_claim_data. alpha_swapped, AlphaCurrency :: ZERO ) ;
1884+ assert_eq ! ( root_claim_data. tao_swapped, TaoCurrency :: ZERO ) ;
1885+
1886+ // Check PendingSubnetRootClaimData after on_finalize()
1887+
1888+ step_block ( 1 ) ;
1889+ assert ! ( !PendingSubnetRootClaimData :: <Test >:: contains_key( netuid) ) ;
1890+
1891+ // Check for the event
1892+ let event = System :: events ( )
1893+ . into_iter ( )
1894+ . find ( |e| {
1895+ matches ! (
1896+ & e. event,
1897+ RuntimeEvent :: SubtensorModule ( Event :: RootClaimedData { .. } )
1898+ )
1899+ } )
1900+ . expect ( "Event must be found" ) ;
1901+
1902+ let RuntimeEvent :: SubtensorModule ( Event :: RootClaimedData { data } ) = event. event else {
1903+ panic ! ( "Event must be RootClaimedData" )
1904+ } ;
1905+
1906+ assert_eq ! (
1907+ BTreeMap :: from( [ (
1908+ netuid,
1909+ PendingRootClaimedData {
1910+ alpha_swapped: AlphaCurrency :: ZERO ,
1911+ tao_swapped: TaoCurrency :: ZERO ,
1912+ alpha_kept: new_stake,
1913+ }
1914+ ) ] ) ,
1915+ data
1916+ ) ;
18821917 } ) ;
18831918}
18841919
@@ -1946,7 +1981,7 @@ fn test_claim_root_subnet_root_claim_map_swap() {
19461981 RootClaimTypeEnum :: Swap
19471982 ) , ) ;
19481983 assert_eq ! ( RootClaimType :: <Test >:: get( coldkey) , RootClaimTypeEnum :: Swap ) ;
1949- assert ! ( !SubnetRootClaimSwap :: <Test >:: contains_key( netuid) ) ;
1984+ assert ! ( !PendingSubnetRootClaimData :: <Test >:: contains_key( netuid) ) ;
19501985
19511986 assert_ok ! ( SubtensorModule :: claim_root(
19521987 RuntimeOrigin :: signed( coldkey) ,
@@ -1975,18 +2010,52 @@ fn test_claim_root_subnet_root_claim_map_swap() {
19752010
19762011 // Check SubnetRootClaimSwap
19772012
1978- let saved_swapped_alpha: u64 = SubnetRootClaimSwap :: < Test > :: get ( netuid) . into ( ) ;
2013+ let root_claim_data = PendingSubnetRootClaimData :: < Test > :: get ( netuid) ;
2014+
2015+ let saved_swapped_alpha: u64 = root_claim_data. alpha_swapped . into ( ) ;
2016+
2017+ let saved_swapped_tao: u64 = root_claim_data. tao_swapped . into ( ) ;
2018+
2019+ assert_eq ! ( root_claim_data. alpha_kept, AlphaCurrency :: ZERO ) ;
2020+
2021+ assert_abs_diff_eq ! (
2022+ estimated_stake_increment,
2023+ saved_swapped_tao as f64 ,
2024+ epsilon = 10f64 ,
2025+ ) ;
19792026
19802027 assert_abs_diff_eq ! (
1981- estimated_stake_increment as f64 / current_price,
2028+ estimated_stake_increment / current_price,
19822029 saved_swapped_alpha as f64 ,
19832030 epsilon = 10f64 ,
19842031 ) ;
19852032
1986- // Check SubnetRootClaimSwap after network deregistration
2033+ // Check PendingSubnetRootClaimData after block finalization
19872034
1988- assert_ok ! ( SubtensorModule :: do_dissolve_network( netuid) ) ;
1989- assert ! ( !SubnetRootClaimSwap :: <Test >:: contains_key( netuid) ) ;
2035+ step_block ( 1 ) ;
2036+ assert ! ( !PendingSubnetRootClaimData :: <Test >:: contains_key( netuid) ) ;
2037+
2038+ // Check for the event
2039+ let event = System :: events ( )
2040+ . into_iter ( )
2041+ . find ( |e| {
2042+ matches ! (
2043+ & e. event,
2044+ RuntimeEvent :: SubtensorModule ( Event :: RootClaimedData { .. } )
2045+ )
2046+ } )
2047+ . expect ( "Event must be found" ) ;
2048+
2049+ let RuntimeEvent :: SubtensorModule ( Event :: RootClaimedData { data } ) = event. event else {
2050+ panic ! ( "Event must be RootClaimedData" )
2051+ } ;
2052+
2053+ for ( event_netuid, root_claim_data) in data. into_iter ( ) {
2054+ assert_eq ! ( event_netuid, netuid) ;
2055+ assert_eq ! ( root_claim_data. alpha_kept, AlphaCurrency :: ZERO ) ;
2056+ assert_eq ! ( root_claim_data. alpha_swapped, saved_swapped_alpha. into( ) ) ;
2057+ assert_eq ! ( root_claim_data. tao_swapped, saved_swapped_tao. into( ) ) ;
2058+ }
19902059 } ) ;
19912060}
19922061
0 commit comments