Skip to content

Commit 3f79fb2

Browse files
committed
add tests for precision loss during recycle/burn
1 parent 64fc846 commit 3f79fb2

File tree

1 file changed

+83
-0
lines changed

1 file changed

+83
-0
lines changed

pallets/subtensor/src/tests/recycle_alpha.rs

Lines changed: 83 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
use approx::assert_abs_diff_eq;
22
use frame_support::{assert_noop, assert_ok, traits::Currency};
33
use sp_core::U256;
4+
use substrate_fixed::types::U64F64;
45
use subtensor_runtime_common::{AlphaCurrency, Currency as CurrencyT};
56

67
use super::mock;
@@ -543,3 +544,85 @@ fn test_burn_errors() {
543544
);
544545
});
545546
}
547+
548+
#[test]
549+
fn test_recycle_precision_loss() {
550+
new_test_ext(1).execute_with(|| {
551+
let coldkey = U256::from(1);
552+
let hotkey = U256::from(2);
553+
554+
let netuid = add_dynamic_network(&hotkey, &coldkey);
555+
556+
Balances::make_free_balance_be(&coldkey, 1_000_000_000);
557+
// sanity check
558+
assert!(SubtensorModule::if_subnet_exist(netuid));
559+
560+
// add stake to coldkey-hotkey pair so we can recycle it
561+
let stake = 200_000;
562+
increase_stake_on_coldkey_hotkey_account(&coldkey, &hotkey, stake.into(), netuid);
563+
564+
// get initial total issuance and alpha out
565+
let initial_alpha = TotalHotkeyAlpha::<Test>::get(hotkey, netuid);
566+
let initial_net_alpha = SubnetAlphaOut::<Test>::get(netuid);
567+
568+
// amount to recycle
569+
let recycle_amount = AlphaCurrency::from(stake);
570+
571+
// Modify the alpha pool denominator so it's low-precision
572+
let denominator = U64F64::from_num(0.0000001);
573+
TotalHotkeyShares::<Test>::insert(hotkey, netuid, denominator);
574+
Alpha::<Test>::insert((&hotkey, &coldkey, netuid), denominator);
575+
576+
// recycle, expect error due to precision loss
577+
assert_noop!(
578+
SubtensorModule::recycle_alpha(
579+
RuntimeOrigin::signed(coldkey),
580+
hotkey,
581+
recycle_amount,
582+
netuid
583+
),
584+
Error::<Test>::PrecisionLoss
585+
);
586+
});
587+
}
588+
589+
#[test]
590+
fn test_burn_precision_loss() {
591+
new_test_ext(1).execute_with(|| {
592+
let coldkey = U256::from(1);
593+
let hotkey = U256::from(2);
594+
595+
let netuid = add_dynamic_network(&hotkey, &coldkey);
596+
597+
Balances::make_free_balance_be(&coldkey, 1_000_000_000);
598+
// sanity check
599+
assert!(SubtensorModule::if_subnet_exist(netuid));
600+
601+
// add stake to coldkey-hotkey pair so we can recycle it
602+
let stake = 200_000;
603+
increase_stake_on_coldkey_hotkey_account(&coldkey, &hotkey, stake.into(), netuid);
604+
605+
// get initial total issuance and alpha out
606+
let initial_alpha = TotalHotkeyAlpha::<Test>::get(hotkey, netuid);
607+
let initial_net_alpha = SubnetAlphaOut::<Test>::get(netuid);
608+
609+
// amount to recycle
610+
let burn_amount = AlphaCurrency::from(stake);
611+
612+
// Modify the alpha pool denominator so it's low-precision
613+
let denominator = U64F64::from_num(0.0000001);
614+
TotalHotkeyShares::<Test>::insert(hotkey, netuid, denominator);
615+
Alpha::<Test>::insert((&hotkey, &coldkey, netuid), denominator);
616+
617+
// burn, expect error due to precision loss
618+
assert_noop!(
619+
SubtensorModule::burn_alpha(
620+
RuntimeOrigin::signed(coldkey),
621+
hotkey,
622+
burn_amount,
623+
netuid
624+
),
625+
Error::<Test>::PrecisionLoss
626+
);
627+
});
628+
}

0 commit comments

Comments
 (0)