@@ -1245,66 +1245,64 @@ fn test_remove_stake_from_hotkey_account() {
12451245 let subnet_owner_hotkey = U256 :: from ( 2 ) ;
12461246 let hotkey_id = U256 :: from ( 5445 ) ;
12471247 let coldkey_id = U256 :: from ( 5443433 ) ;
1248- let amount: u64 = 10_000 ;
1248+ let amount: AlphaBalance = 10_000u64 . into ( ) ;
12491249
12501250 let netuid = add_dynamic_network ( & subnet_owner_hotkey, & subnet_owner_coldkey) ;
12511251 register_ok_neuron ( netuid, hotkey_id, coldkey_id, 192213123 ) ;
12521252
1253- // Track baselines (alpha on subnet + tao-equivalent total) .
1253+ // Baselines before adding stake .
12541254 let alpha_before = SubtensorModule :: get_stake_for_hotkey_and_coldkey_on_subnet (
12551255 & hotkey_id,
12561256 & coldkey_id,
12571257 netuid,
12581258 ) ;
12591259 let total_before = SubtensorModule :: get_total_stake_for_hotkey ( & hotkey_id) ;
12601260
1261- // Add alpha stake ( internal helper) .
1262- let added_alpha = SubtensorModule :: increase_stake_for_hotkey_and_coldkey_on_subnet (
1261+ // Add alpha stake directly through the internal helper.
1262+ SubtensorModule :: increase_stake_for_hotkey_and_coldkey_on_subnet (
12631263 & hotkey_id,
12641264 & coldkey_id,
12651265 netuid,
1266- amount. into ( ) ,
1266+ amount,
12671267 ) ;
12681268
1269- // Alpha stake should increase by (roughly) what the share pool actually credited .
1269+ // Alpha stake should increase by exactly the credited alpha amount .
12701270 let alpha_after_add = SubtensorModule :: get_stake_for_hotkey_and_coldkey_on_subnet (
12711271 & hotkey_id,
12721272 & coldkey_id,
12731273 netuid,
12741274 ) ;
1275- assert_abs_diff_eq ! (
1276- alpha_after_add,
1277- alpha_before. saturating_add( added_alpha) ,
1278- epsilon = 2 . into( )
1279- ) ;
1275+ assert_eq ! ( alpha_after_add, alpha_before. saturating_add( amount) ) ;
1276+
1277+ // Tao-equivalent total stake should have increased from baseline.
1278+ assert ! ( SubtensorModule :: get_total_stake_for_hotkey( & hotkey_id) > total_before) ;
12801279
1281- // Remove stake: remove exactly the credited alpha.
1282- let removed_alpha = SubtensorModule :: decrease_stake_for_hotkey_and_coldkey_on_subnet (
1280+ // Remove exactly the same alpha amount .
1281+ SubtensorModule :: decrease_stake_for_hotkey_and_coldkey_on_subnet (
12831282 & hotkey_id,
12841283 & coldkey_id,
12851284 netuid,
1286- added_alpha ,
1285+ amount ,
12871286 ) ;
12881287
1289- assert_abs_diff_eq ! ( removed_alpha, added_alpha, epsilon = 2 . into( ) ) ;
1290-
1291- // Alpha stake should return to baseline.
1288+ // Alpha stake should return to its original baseline.
12921289 let alpha_after_remove = SubtensorModule :: get_stake_for_hotkey_and_coldkey_on_subnet (
12931290 & hotkey_id,
12941291 & coldkey_id,
12951292 netuid,
12961293 ) ;
1297- assert_abs_diff_eq ! ( alpha_after_remove, alpha_before, epsilon = 2 . into ( ) ) ;
1294+ assert_eq ! ( alpha_after_remove, alpha_before) ;
12981295
1299- // Tao-equivalent total should also return to baseline (price may be != 1.0) .
1296+ // Tao-equivalent total stake should also return to baseline.
13001297 assert_abs_diff_eq ! (
13011298 SubtensorModule :: get_total_stake_for_hotkey( & hotkey_id) ,
13021299 total_before,
1303- epsilon = 25 . into( )
1300+ epsilon = 10 . into( )
13041301 ) ;
13051302 } ) ;
13061303}
13071304
1305+
13081306#[ test]
13091307fn test_remove_stake_from_hotkey_account_registered_in_various_networks ( ) {
13101308 new_test_ext ( 1 ) . execute_with ( || {
@@ -4164,47 +4162,63 @@ fn test_remove_99_9991_per_cent_stake_works_precisely() {
41644162 let netuid = add_dynamic_network ( & subnet_owner_hotkey, & subnet_owner_coldkey) ;
41654163 register_ok_neuron ( netuid, hotkey_account_id, coldkey_account_id, 192213123 ) ;
41664164
4167- // Set fee rate to 0 so that alpha fee is not moved to block producer
4168- // and the hotkey stake does drop to 0
4165+ // Set fee rate to 0 so that alpha fee is not moved to block producer.
41694166 pallet_subtensor_swap:: FeeRate :: < Test > :: insert ( netuid, 0 ) ;
41704167
41714168 // Give it some $$$ in his coldkey balance (in addition to any leftover buffer from registration)
41724169 SubtensorModule :: add_balance_to_coldkey_account ( & coldkey_account_id, amount. into ( ) ) ;
41734170
4174- // Stake to hotkey account, and check if the result is ok
4171+ // Stake to hotkey account.
41754172 assert_ok ! ( SubtensorModule :: add_stake(
41764173 RuntimeOrigin :: signed( coldkey_account_id) ,
41774174 hotkey_account_id,
41784175 netuid,
41794176 amount. into( )
41804177 ) ) ;
41814178
4182- // Remove 99.9991% stake
4179+ // Remove 99.9991% stake.
41834180 let alpha = SubtensorModule :: get_stake_for_hotkey_and_coldkey_on_subnet (
41844181 & hotkey_account_id,
41854182 & coldkey_account_id,
41864183 netuid,
41874184 ) ;
4185+ let coldkey_balance_before_remove =
4186+ SubtensorModule :: get_coldkey_balance ( & coldkey_account_id) ;
4187+
41884188 remove_stake_rate_limit_for_tests ( & hotkey_account_id, & coldkey_account_id, netuid) ;
4189+
41894190 let remove_amount = AlphaBalance :: from (
41904191 ( U64F64 :: from_num ( alpha) * U64F64 :: from_num ( 0.999991 ) ) . to_num :: < u64 > ( ) ,
41914192 ) ;
4192- // we expected the entire stake to be returned
4193- let ( expected_balance, _) = mock:: swap_alpha_to_tao ( netuid, remove_amount) ;
4193+
4194+ // Expected TAO returned by swapping exactly the removed alpha.
4195+ let ( expected_returned_balance, _) = mock:: swap_alpha_to_tao ( netuid, remove_amount) ;
4196+
41944197 assert_ok ! ( SubtensorModule :: remove_stake(
41954198 RuntimeOrigin :: signed( coldkey_account_id) ,
41964199 hotkey_account_id,
41974200 netuid,
41984201 remove_amount,
41994202 ) ) ;
42004203
4201- // Check that all alpha was unstaked and all TAO balance was returned (less fees)
4204+ // Compare the returned delta, not the absolute coldkey balance, because
4205+ // registration / staking can leave a small pre-existing balance on coldkey.
4206+ let coldkey_balance_after_remove =
4207+ SubtensorModule :: get_coldkey_balance ( & coldkey_account_id) ;
4208+ let actual_returned_balance = TaoBalance :: from (
4209+ coldkey_balance_after_remove
4210+ . to_u64 ( )
4211+ . saturating_sub ( coldkey_balance_before_remove. to_u64 ( ) ) ,
4212+ ) ;
4213+
42024214 assert_abs_diff_eq ! (
4203- SubtensorModule :: get_coldkey_balance ( & coldkey_account_id ) ,
4204- expected_balance ,
4215+ actual_returned_balance ,
4216+ expected_returned_balance ,
42054217 epsilon = 10 . into( ) ,
42064218 ) ;
4219+
42074220 assert ! ( !SubtensorModule :: get_total_stake_for_hotkey( & hotkey_account_id) . is_zero( ) ) ;
4221+
42084222 let new_alpha = SubtensorModule :: get_stake_for_hotkey_and_coldkey_on_subnet (
42094223 & hotkey_account_id,
42104224 & coldkey_account_id,
0 commit comments