|
1 | 1 | use approx::assert_abs_diff_eq; |
2 | 2 | use frame_support::{assert_noop, assert_ok, traits::Currency}; |
3 | 3 | use sp_core::U256; |
| 4 | +use substrate_fixed::types::U64F64; |
4 | 5 | use subtensor_runtime_common::{AlphaCurrency, Currency as CurrencyT}; |
5 | 6 |
|
6 | 7 | use super::mock; |
@@ -543,3 +544,77 @@ fn test_burn_errors() { |
543 | 544 | ); |
544 | 545 | }); |
545 | 546 | } |
| 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 | + // amount to recycle |
| 565 | + let recycle_amount = AlphaCurrency::from(stake / 2); |
| 566 | + |
| 567 | + // Modify the alpha pool denominator so it's low-precision |
| 568 | + let denominator = U64F64::from_num(0.00000001); |
| 569 | + TotalHotkeyShares::<Test>::insert(hotkey, netuid, denominator); |
| 570 | + Alpha::<Test>::insert((&hotkey, &coldkey, netuid), denominator); |
| 571 | + |
| 572 | + // recycle, expect error due to precision loss |
| 573 | + assert_noop!( |
| 574 | + SubtensorModule::recycle_alpha( |
| 575 | + RuntimeOrigin::signed(coldkey), |
| 576 | + hotkey, |
| 577 | + recycle_amount, |
| 578 | + netuid |
| 579 | + ), |
| 580 | + Error::<Test>::PrecisionLoss |
| 581 | + ); |
| 582 | + }); |
| 583 | +} |
| 584 | + |
| 585 | +#[test] |
| 586 | +fn test_burn_precision_loss() { |
| 587 | + new_test_ext(1).execute_with(|| { |
| 588 | + let coldkey = U256::from(1); |
| 589 | + let hotkey = U256::from(2); |
| 590 | + |
| 591 | + let netuid = add_dynamic_network(&hotkey, &coldkey); |
| 592 | + |
| 593 | + Balances::make_free_balance_be(&coldkey, 1_000_000_000); |
| 594 | + // sanity check |
| 595 | + assert!(SubtensorModule::if_subnet_exist(netuid)); |
| 596 | + |
| 597 | + // add stake to coldkey-hotkey pair so we can recycle it |
| 598 | + let stake = 200_000; |
| 599 | + increase_stake_on_coldkey_hotkey_account(&coldkey, &hotkey, stake.into(), netuid); |
| 600 | + |
| 601 | + // amount to recycle |
| 602 | + let burn_amount = AlphaCurrency::from(stake / 2); |
| 603 | + |
| 604 | + // Modify the alpha pool denominator so it's low-precision |
| 605 | + let denominator = U64F64::from_num(0.00000001); |
| 606 | + TotalHotkeyShares::<Test>::insert(hotkey, netuid, denominator); |
| 607 | + Alpha::<Test>::insert((&hotkey, &coldkey, netuid), denominator); |
| 608 | + |
| 609 | + // burn, expect error due to precision loss |
| 610 | + assert_noop!( |
| 611 | + SubtensorModule::burn_alpha( |
| 612 | + RuntimeOrigin::signed(coldkey), |
| 613 | + hotkey, |
| 614 | + burn_amount, |
| 615 | + netuid |
| 616 | + ), |
| 617 | + Error::<Test>::PrecisionLoss |
| 618 | + ); |
| 619 | + }); |
| 620 | +} |
0 commit comments