Skip to content

Commit f8f6a49

Browse files
committed
fix tests
1 parent f331cf4 commit f8f6a49

File tree

3 files changed

+69
-38
lines changed

3 files changed

+69
-38
lines changed

pallets/subtensor/src/tests/staking.rs

Lines changed: 43 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -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]
13091307
fn 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,

pallets/transaction-fee/src/tests/mock.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -821,4 +821,5 @@ pub fn setup_stake(
821821
netuid,
822822
stake_amount.into(),
823823
));
824+
remove_stake_rate_limit_for_tests(hotkey, coldkey, netuid);
824825
}

pallets/transaction-fee/src/tests/mod.rs

Lines changed: 25 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -801,14 +801,6 @@ fn test_remove_stake_limit_fees_alpha() {
801801
stake_amount,
802802
);
803803

804-
// Simulate stake removal to get how much TAO should we get for unstaked Alpha
805-
let alpha_fee = AlphaBalance::from(24229); // This is measured alpha fee that matches the withdrawn tx fee
806-
let (expected_burned_tao_fees, _swap_fee) =
807-
mock::swap_alpha_to_tao(sn.subnets[0].netuid, alpha_fee);
808-
let (expected_unstaked_tao_plus_fees, _swap_fee) =
809-
mock::swap_alpha_to_tao(sn.subnets[0].netuid, unstake_amount + alpha_fee);
810-
let expected_unstaked_tao = expected_unstaked_tao_plus_fees - expected_burned_tao_fees;
811-
812804
// Forse-set signer balance to ED
813805
let current_balance = Balances::free_balance(sn.coldkey);
814806
let _ = SubtensorModule::remove_balance_from_coldkey_account(
@@ -831,6 +823,8 @@ fn test_remove_stake_limit_fees_alpha() {
831823
allow_partial: false,
832824
});
833825

826+
System::reset_events();
827+
834828
// Dispatch the extrinsic with ChargeTransactionPayment extension
835829
let info = call.get_dispatch_info();
836830
let ext = pallet_transaction_payment::ChargeTransactionPayment::<Test>::from(0.into());
@@ -849,7 +843,29 @@ fn test_remove_stake_limit_fees_alpha() {
849843
sn.subnets[0].netuid,
850844
);
851845

852-
let actual_tao_fee = balance_before + expected_unstaked_tao.into() - final_balance;
846+
let expected_unstaked_tao = System::events()
847+
.iter()
848+
.rev()
849+
.find_map(|event_record| match &event_record.event {
850+
RuntimeEvent::SubtensorModule(SubtensorEvent::StakeRemoved(
851+
coldkey,
852+
hotkey,
853+
tao_amount,
854+
alpha_amount,
855+
netuid,
856+
fee_paid,
857+
)) if coldkey == &sn.coldkey
858+
&& hotkey == &sn.hotkeys[0]
859+
&& *netuid == sn.subnets[0].netuid
860+
&& (*alpha_amount + AlphaBalance::from(*fee_paid) == unstake_amount) =>
861+
{
862+
Some(*tao_amount)
863+
}
864+
_ => None,
865+
})
866+
.expect("expected StakeRemoved event for remove_stake_limit");
867+
868+
let actual_tao_fee = balance_before + expected_unstaked_tao - final_balance;
853869
let actual_alpha_fee = alpha_before - alpha_after - unstake_amount;
854870

855871
// Remove stake extrinsic should pay fees in Alpha

0 commit comments

Comments
 (0)