From f5012329ef391ea0859af04b5cd7fbe438bc479f Mon Sep 17 00:00:00 2001 From: Aliaksandr Tsurko Date: Tue, 6 May 2025 17:13:17 +0200 Subject: [PATCH 1/6] Fix alpha price before first swap and children tests --- pallets/subtensor/src/tests/children.rs | 21 ++++++++++++++++----- pallets/swap/src/pallet/impls.rs | 10 +++++++++- 2 files changed, 25 insertions(+), 6 deletions(-) diff --git a/pallets/subtensor/src/tests/children.rs b/pallets/subtensor/src/tests/children.rs index 44eceb474d..813a0c2064 100644 --- a/pallets/subtensor/src/tests/children.rs +++ b/pallets/subtensor/src/tests/children.rs @@ -2218,11 +2218,12 @@ fn test_do_remove_stake_clears_pending_childkeys() { // Set non-default value for childkey stake threshold StakeThreshold::::set(1_000_000_000_000); + let (_, fee) = mock::swap_tao_to_alpha(netuid, StakeThreshold::::get()); SubtensorModule::increase_stake_for_hotkey_and_coldkey_on_subnet( &hotkey, &coldkey, netuid, - StakeThreshold::::get(), + StakeThreshold::::get() + fee, ); // Attempt to set child @@ -2415,13 +2416,19 @@ fn test_revoke_child_no_min_stake_check() { add_network(netuid, 13, 0); register_ok_neuron(netuid, parent, coldkey, 0); + let reserve = 1_000_000_000_000_000; + mock::setup_reserves(netuid, reserve, reserve); + mock::setup_reserves(root, reserve, reserve); + // Set minimum stake for setting children StakeThreshold::::put(1_000_000_000_000); + + let (_, fee) = mock::swap_tao_to_alpha(root, StakeThreshold::::get()); SubtensorModule::increase_stake_for_hotkey_and_coldkey_on_subnet( &parent, &coldkey, root, - StakeThreshold::::get(), + StakeThreshold::::get() + fee, ); // Schedule parent-child relationship @@ -2441,7 +2448,7 @@ fn test_revoke_child_no_min_stake_check() { &parent, &coldkey, root, - StakeThreshold::::get(), + StakeThreshold::::get() + fee, ); // Ensure the childkeys are applied @@ -2487,13 +2494,17 @@ fn test_do_set_child_registration_disabled() { add_network(netuid, 13, 0); register_ok_neuron(netuid, parent, coldkey, 0); + let reserve = 1_000_000_000_000_000; + mock::setup_reserves(netuid, reserve, reserve); + // Set minimum stake for setting children StakeThreshold::::put(1_000_000_000_000); + let (_, fee) = mock::swap_tao_to_alpha(netuid, StakeThreshold::::get()); SubtensorModule::increase_stake_for_hotkey_and_coldkey_on_subnet( &parent, &coldkey, netuid, - StakeThreshold::::get(), + StakeThreshold::::get() + fee, ); // Disable subnet registrations @@ -2512,7 +2523,7 @@ fn test_do_set_child_registration_disabled() { &parent, &coldkey, netuid, - StakeThreshold::::get(), + StakeThreshold::::get() + fee, ); // Ensure the childkeys are applied diff --git a/pallets/swap/src/pallet/impls.rs b/pallets/swap/src/pallet/impls.rs index b4b847361f..1253aea326 100644 --- a/pallets/swap/src/pallet/impls.rs +++ b/pallets/swap/src/pallet/impls.rs @@ -1152,7 +1152,15 @@ impl SwapHandler for Pallet { fn current_alpha_price(netuid: u16) -> U96F32 { let sqrt_price = AlphaSqrtPrice::::get(NetUid::from(netuid)); - U96F32::saturating_from_num(sqrt_price.saturating_mul(sqrt_price)) + let tao_reserve = T::LiquidityDataProvider::tao_reserve(netuid); + let alpha_reserve = T::LiquidityDataProvider::alpha_reserve(netuid); + + if sqrt_price == 0 && tao_reserve > 0 && alpha_reserve > 0 { + U96F32::saturating_from_num(tao_reserve) + .saturating_div(U96F32::saturating_from_num(alpha_reserve)) + } else { + U96F32::saturating_from_num(sqrt_price.saturating_mul(sqrt_price)) + } } fn min_price() -> u64 { From 95c7b4b1da61f8aa5956a9764183f65196a30c79 Mon Sep 17 00:00:00 2001 From: Aliaksandr Tsurko Date: Wed, 7 May 2025 17:25:28 +0200 Subject: [PATCH 2/6] Fix most of move stake tests --- pallets/subtensor/src/staking/stake_utils.rs | 25 +- pallets/subtensor/src/tests/move_stake.rs | 245 ++++++++----------- pallets/subtensor/src/tests/staking.rs | 11 +- 3 files changed, 133 insertions(+), 148 deletions(-) diff --git a/pallets/subtensor/src/staking/stake_utils.rs b/pallets/subtensor/src/staking/stake_utils.rs index 35c8fb1bad..2599cbb74c 100644 --- a/pallets/subtensor/src/staking/stake_utils.rs +++ b/pallets/subtensor/src/staking/stake_utils.rs @@ -802,7 +802,15 @@ impl Pallet { // Get the minimum balance (and amount) that satisfies the transaction let min_amount = { let default_stake = DefaultMinStake::::get(); - let fee = T::SwapInterface::approx_fee_amount(netuid, default_stake); + let fee = T::SwapInterface::swap( + netuid, + OrderType::Buy, + default_stake, + T::SwapInterface::max_price(), + true, + ) + .map(|res| res.fee_paid) + .unwrap_or(T::SwapInterface::approx_fee_amount(netuid, default_stake)); default_stake.saturating_add(fee) }; @@ -951,6 +959,21 @@ impl Pallet { Error::::NotEnoughStakeToWithdraw ); + // Ensure that the stake amount to be removed is above the minimum in tao equivalent. + let tao_equivalent = T::SwapInterface::swap( + origin_netuid, + OrderType::Sell, + alpha_amount, + T::SwapInterface::max_price(), + true, + ) + .map(|res| res.amount_paid_out) + .map_err(|_| Error::::InsufficientLiquidity)?; + ensure!( + tao_equivalent > DefaultMinStake::::get(), + Error::::AmountTooLow + ); + // Ensure that if partial execution is not allowed, the amount will not cause // slippage over desired if let Some(allow_partial) = maybe_allow_partial { diff --git a/pallets/subtensor/src/tests/move_stake.rs b/pallets/subtensor/src/tests/move_stake.rs index 646c965bc1..0b3bf447b2 100644 --- a/pallets/subtensor/src/tests/move_stake.rs +++ b/pallets/subtensor/src/tests/move_stake.rs @@ -21,7 +21,6 @@ fn test_do_move_success() { let origin_hotkey = U256::from(2); let destination_hotkey = U256::from(3); let stake_amount = DefaultMinStake::::get() * 10; - let fee: u64 = 0; // FIXME: DefaultStakingFee is deprecated // Set up initial stake SubtensorModule::create_account_if_non_existent(&coldkey, &origin_hotkey); @@ -41,6 +40,8 @@ fn test_do_move_success() { ); // Perform the move + let (tao_equivalent, _) = mock::swap_alpha_to_tao(netuid, alpha); + let (expected_alpha, _) = mock::swap_tao_to_alpha(netuid, tao_equivalent); assert_ok!(SubtensorModule::do_move_stake( RuntimeOrigin::signed(coldkey), origin_hotkey, @@ -65,8 +66,8 @@ fn test_do_move_success() { &coldkey, netuid ), - stake_amount - 2 * fee, - epsilon = stake_amount / 1000 + expected_alpha, + epsilon = 1000 ); }); } @@ -153,7 +154,9 @@ fn test_do_move_nonexistent_subnet() { let destination_hotkey = U256::from(3); let nonexistent_netuid = 99; // Assuming this subnet doesn't exist let stake_amount = 1_000_000; - let fee = 0; + + let reserve = stake_amount * 1000; + mock::setup_reserves(origin_netuid, reserve, reserve); // Set up initial stake SubtensorModule::stake_into_subnet( @@ -184,14 +187,13 @@ fn test_do_move_nonexistent_subnet() { ); // Check that the stake remains unchanged - assert_abs_diff_eq!( + assert_eq!( SubtensorModule::get_stake_for_hotkey_and_coldkey_on_subnet( &origin_hotkey, &coldkey, origin_netuid ), - stake_amount, - epsilon = 100 + alpha, ); }); } @@ -254,6 +256,9 @@ fn test_do_move_nonexistent_destination_hotkey() { let netuid = 1; let stake_amount = 1_000_000; + let reserve = stake_amount * 1000; + mock::setup_reserves(netuid, reserve, reserve); + // Set up initial stake SubtensorModule::create_account_if_non_existent(&coldkey, &origin_hotkey); let alpha = SubtensorModule::stake_into_subnet( @@ -286,8 +291,9 @@ fn test_do_move_nonexistent_destination_hotkey() { &coldkey, netuid ), - stake_amount + alpha ); + assert_eq!( SubtensorModule::get_stake_for_hotkey_and_coldkey_on_subnet( &nonexistent_destination_hotkey, @@ -439,7 +445,6 @@ fn test_do_move_partial_stake() { let origin_hotkey = U256::from(2); let destination_hotkey = U256::from(3); let total_stake = DefaultMinStake::::get() * 10; - let fee: u64 = 0; // FIXME: DefaultStakingFee is deprecated // Set up initial stake SubtensorModule::stake_into_subnet( @@ -457,6 +462,8 @@ fn test_do_move_partial_stake() { ); // Move partial stake + let (tao_equivalent, _) = mock::swap_alpha_to_tao(netuid, alpha); + let (expected_alpha, _) = mock::swap_tao_to_alpha(netuid, tao_equivalent); SubtensorModule::create_account_if_non_existent(&coldkey, &origin_hotkey); SubtensorModule::create_account_if_non_existent(&coldkey, &destination_hotkey); assert_ok!(SubtensorModule::do_move_stake( @@ -483,8 +490,8 @@ fn test_do_move_partial_stake() { &coldkey, netuid ), - total_stake - 2 * fee, - epsilon = total_stake / 1000 + expected_alpha, + epsilon = 1000 ); }); } @@ -502,7 +509,6 @@ fn test_do_move_multiple_times() { let hotkey1 = U256::from(2); let hotkey2 = U256::from(3); let initial_stake = DefaultMinStake::::get() * 10; - let fee: u64 = 0; // FIXME: DefaultStakingFee is deprecated // Set up initial stake SubtensorModule::create_account_if_non_existent(&coldkey, &hotkey1); @@ -517,6 +523,7 @@ fn test_do_move_multiple_times() { .unwrap(); // Move stake multiple times + let mut expected_alpha: u64 = 0; for _ in 0..3 { let alpha1 = SubtensorModule::get_stake_for_hotkey_and_coldkey_on_subnet( &hotkey1, &coldkey, netuid, @@ -532,6 +539,9 @@ fn test_do_move_multiple_times() { let alpha2 = SubtensorModule::get_stake_for_hotkey_and_coldkey_on_subnet( &hotkey2, &coldkey, netuid, ); + let (tao_equivalent, _) = mock::swap_alpha_to_tao(netuid, alpha2); + // we need expected_alpha before the last move, so we call it within the loop + expected_alpha = mock::swap_tao_to_alpha(netuid, tao_equivalent).0; assert_ok!(SubtensorModule::do_move_stake( RuntimeOrigin::signed(coldkey), hotkey2, @@ -545,8 +555,8 @@ fn test_do_move_multiple_times() { // Check final stake distribution assert_abs_diff_eq!( SubtensorModule::get_stake_for_hotkey_and_coldkey_on_subnet(&hotkey1, &coldkey, netuid), - initial_stake - 7 * fee, - epsilon = initial_stake / 1000 + expected_alpha, + epsilon = 1000, ); assert_eq!( SubtensorModule::get_stake_for_hotkey_and_coldkey_on_subnet(&hotkey2, &coldkey, netuid), @@ -567,7 +577,9 @@ fn test_do_move_wrong_origin() { let destination_hotkey = U256::from(3); let netuid = 1; let stake_amount = DefaultMinStake::::get() * 10; - let fee = 0; + + let reserve = stake_amount * 1000; + mock::setup_reserves(netuid, reserve, reserve); // Set up initial stake SubtensorModule::stake_into_subnet( @@ -607,7 +619,7 @@ fn test_do_move_wrong_origin() { &coldkey, netuid ), - stake_amount + alpha ); assert_eq!( SubtensorModule::get_stake_for_hotkey_and_coldkey_on_subnet( @@ -632,7 +644,6 @@ fn test_do_move_same_hotkey() { let coldkey = U256::from(1); let hotkey = U256::from(2); let stake_amount = DefaultMinStake::::get() * 10; - let fee: u64 = 0; // FIXME: DefaultStakingFee is deprecated // Set up initial stake SubtensorModule::create_account_if_non_existent(&coldkey, &hotkey); @@ -648,6 +659,9 @@ fn test_do_move_same_hotkey() { SubtensorModule::get_stake_for_hotkey_and_coldkey_on_subnet(&hotkey, &coldkey, netuid); // Attempt to move stake to the same hotkey + let (tao_equivalent, _) = mock::swap_alpha_to_tao(netuid, alpha); + let (expected_alpha, _) = mock::swap_tao_to_alpha(netuid, tao_equivalent); + assert_ok!(SubtensorModule::do_move_stake( RuntimeOrigin::signed(coldkey), hotkey, @@ -660,8 +674,8 @@ fn test_do_move_same_hotkey() { // Check that stake remains unchanged assert_abs_diff_eq!( SubtensorModule::get_stake_for_hotkey_and_coldkey_on_subnet(&hotkey, &coldkey, netuid), - alpha - fee, - epsilon = alpha / 1000 + expected_alpha, + epsilon = 1000 ); }); } @@ -699,6 +713,7 @@ fn test_do_move_event_emission() { // Move stake and capture events System::reset_events(); + let (tao_equivalent, _) = mock::swap_alpha_to_tao(netuid, alpha); assert_ok!(SubtensorModule::do_move_stake( RuntimeOrigin::signed(coldkey), origin_hotkey, @@ -708,8 +723,6 @@ fn test_do_move_event_emission() { alpha, )); - let fee = ::SwapInterface::approx_fee_amount(netuid, stake_amount); - // Check for the correct event emission System::assert_last_event( Event::StakeMoved( @@ -718,7 +731,7 @@ fn test_do_move_event_emission() { netuid, destination_hotkey, netuid, - stake_amount - fee * 2, // Should be TAO equivalent + tao_equivalent, // Should be TAO equivalent ) .into(), ); @@ -739,7 +752,6 @@ fn test_do_move_storage_updates() { let origin_hotkey = U256::from(2); let destination_hotkey = U256::from(3); let stake_amount = DefaultMinStake::::get() * 10; - let fee: u64 = 0; // FIXME: DefaultStakingFee is deprecated // Set up initial stake SubtensorModule::stake_into_subnet( @@ -760,6 +772,8 @@ fn test_do_move_storage_updates() { origin_netuid, ); + let (tao_equivalent, _) = mock::swap_alpha_to_tao(origin_netuid, alpha); + let (alpha2, _) = mock::swap_tao_to_alpha(destination_netuid, tao_equivalent); assert_ok!(SubtensorModule::do_move_stake( RuntimeOrigin::signed(coldkey), origin_hotkey, @@ -778,18 +792,15 @@ fn test_do_move_storage_updates() { ), 0 ); - let alpha_fee = - U96F32::from_num(fee) / ::SwapInterface::current_alpha_price(destination_netuid); - let alpha2 = U96F32::from_num(alpha) * ::SwapInterface::current_alpha_price(origin_netuid) - / ::SwapInterface::current_alpha_price(destination_netuid); + assert_abs_diff_eq!( SubtensorModule::get_stake_for_hotkey_and_coldkey_on_subnet( &destination_hotkey, &coldkey, destination_netuid ), - (alpha2 - alpha_fee).to_num::(), - epsilon = alpha / 1000 + alpha2, + epsilon = 2 ); }); } @@ -807,15 +818,14 @@ fn test_do_move_max_values() { let destination_hotkey = U256::from(3); let max_stake = u64::MAX; let netuid: u16 = add_dynamic_network(&subnet_owner_hotkey, &subnet_owner_coldkey); - let fee = 0; // Set up initial stake with maximum value SubtensorModule::create_account_if_non_existent(&coldkey, &origin_hotkey); SubtensorModule::create_account_if_non_existent(&coldkey, &destination_hotkey); // Add lots of liquidity to bypass low liquidity check - SubnetTAO::::insert(netuid, u64::MAX / 1000); - SubnetAlphaIn::::insert(netuid, u64::MAX / 1000); + let reserve = u64::MAX / 1000; + mock::setup_reserves(netuid, reserve, reserve); SubtensorModule::stake_into_subnet( &origin_hotkey, @@ -832,6 +842,7 @@ fn test_do_move_max_values() { ); // Move maximum stake + let (_, fee) = mock::swap_alpha_to_tao(netuid, alpha); assert_ok!(SubtensorModule::do_move_stake( RuntimeOrigin::signed(coldkey), origin_hotkey, @@ -850,8 +861,7 @@ fn test_do_move_max_values() { ), 0 ); - let fee = ::SwapInterface::approx_fee_amount(netuid, alpha); - let alpha_after_fee = alpha - fee; + let alpha_after_fee = alpha - fee; assert_abs_diff_eq!( SubtensorModule::get_stake_for_hotkey_and_coldkey_on_subnet( &destination_hotkey, @@ -871,13 +881,15 @@ fn test_moving_too_little_unstakes() { let hotkey_account_id = U256::from(533453); let coldkey_account_id = U256::from(55453); let amount = DefaultMinStake::::get(); - let fee: u64 = 0; // FIXME: DefaultStakingFee is deprecated //add network - let netuid: u16 = add_dynamic_network(&hotkey_account_id, &coldkey_account_id); - let netuid2: u16 = add_dynamic_network(&hotkey_account_id, &coldkey_account_id); + let netuid = add_dynamic_network(&hotkey_account_id, &coldkey_account_id); + let netuid2 = add_dynamic_network(&hotkey_account_id, &coldkey_account_id); // Give it some $$$ in his coldkey balance + + let (_, fee) = mock::swap_tao_to_alpha(netuid, amount); + SubtensorModule::add_balance_to_coldkey_account(&coldkey_account_id, amount + fee); assert_ok!(SubtensorModule::add_stake( @@ -908,7 +920,6 @@ fn test_do_transfer_success() { let subnet_owner_coldkey = U256::from(1001); let subnet_owner_hotkey = U256::from(1002); let netuid = add_dynamic_network(&subnet_owner_hotkey, &subnet_owner_coldkey); - let fee: u64 = 0; // FIXME: DefaultStakingFee is deprecated // 2. Define the origin coldkey, destination coldkey, and hotkey to be used. let origin_coldkey = U256::from(1); @@ -934,6 +945,8 @@ fn test_do_transfer_success() { ); // 4. Transfer the entire stake to the destination coldkey on the same subnet (netuid, netuid). + let (tao_equivalent, _) = mock::swap_alpha_to_tao(netuid, alpha); + let (expected_alpha, _) = mock::swap_tao_to_alpha(netuid, tao_equivalent); assert_ok!(SubtensorModule::do_transfer_stake( RuntimeOrigin::signed(origin_coldkey), destination_coldkey, @@ -958,8 +971,8 @@ fn test_do_transfer_success() { &destination_coldkey, netuid ), - stake_amount - fee, - epsilon = stake_amount / 1000 + expected_alpha, + epsilon = 1000 ); }); } @@ -1138,7 +1151,6 @@ fn test_do_transfer_different_subnets() { let destination_coldkey = U256::from(2); let hotkey = U256::from(3); let stake_amount = DefaultMinStake::::get() * 10; - let fee: u64 = 0; // FIXME: DefaultStakingFee is deprecated // 3. Create accounts if needed. SubtensorModule::create_account_if_non_existent(&origin_coldkey, &hotkey); @@ -1163,6 +1175,10 @@ fn test_do_transfer_different_subnets() { &origin_coldkey, origin_netuid, ); + + let (tao_equivalent, _) = mock::swap_alpha_to_tao(origin_netuid, alpha); + let (expected_alpha, _) = mock::swap_tao_to_alpha(destination_netuid, tao_equivalent); + assert_ok!(SubtensorModule::do_transfer_stake( RuntimeOrigin::signed(origin_coldkey), destination_coldkey, @@ -1183,17 +1199,14 @@ fn test_do_transfer_different_subnets() { ); // 8. Verify stake ended up in destination subnet for destination coldkey. - let dest_stake = SubtensorModule::get_stake_for_hotkey_and_coldkey_on_subnet( - &hotkey, - &destination_coldkey, - destination_netuid, - ); - let expected_value = U96F32::from_num(stake_amount - fee) - / ::SwapInterface::current_alpha_price(destination_netuid); assert_abs_diff_eq!( - dest_stake, - expected_value.to_num::(), - epsilon = (expected_value / 1000).to_num::() + SubtensorModule::get_stake_for_hotkey_and_coldkey_on_subnet( + &hotkey, + &destination_coldkey, + destination_netuid, + ), + expected_alpha, + epsilon = 1000 ); }); } @@ -1209,7 +1222,6 @@ fn test_do_swap_success() { let coldkey = U256::from(1); let hotkey = U256::from(2); let stake_amount = DefaultMinStake::::get() * 10; - let fee: u64 = 0; // FIXME: DefaultStakingFee is deprecated SubtensorModule::create_account_if_non_existent(&coldkey, &hotkey); SubtensorModule::stake_into_subnet( @@ -1226,6 +1238,8 @@ fn test_do_swap_success() { origin_netuid, ); + let (tao_equivalent, _) = mock::swap_alpha_to_tao(origin_netuid, alpha_before); + let (expected_alpha, _) = mock::swap_tao_to_alpha(destination_netuid, tao_equivalent); assert_ok!(SubtensorModule::do_swap_stake( RuntimeOrigin::signed(coldkey), hotkey, @@ -1248,16 +1262,8 @@ fn test_do_swap_success() { &coldkey, destination_netuid, ); - let alpha_fee = - U96F32::from_num(fee) / ::SwapInterface::current_alpha_price(destination_netuid); - let expected_value = U96F32::from_num(alpha_before) - * ::SwapInterface::current_alpha_price(origin_netuid) - / ::SwapInterface::current_alpha_price(destination_netuid); - assert_abs_diff_eq!( - alpha_after, - (expected_value - alpha_fee).to_num::(), - epsilon = (expected_value / 1000).to_num::() - ); + + assert_abs_diff_eq!(alpha_after, expected_alpha, epsilon = 1000); }); } @@ -1426,7 +1432,6 @@ fn test_do_swap_same_subnet() { let coldkey = U256::from(1); let hotkey = U256::from(2); let stake_amount = DefaultMinStake::::get() * 10; - let fee: u64 = 0; // FIXME: DefaultStakingFee is deprecated SubtensorModule::create_account_if_non_existent(&coldkey, &hotkey); SubtensorModule::stake_into_subnet( @@ -1440,14 +1445,9 @@ fn test_do_swap_same_subnet() { let alpha_before = SubtensorModule::get_stake_for_hotkey_and_coldkey_on_subnet(&hotkey, &coldkey, netuid); - let fee_as_alpha = SubtensorModule::swap_tao_for_alpha( - netuid, - fee, - ::SwapInterface::max_price(), - ) - .unwrap() - .amount_paid_out; + let (tao_equivalent, _) = mock::swap_alpha_to_tao(netuid, alpha_before); + let (expected_alpha, _) = mock::swap_tao_to_alpha(netuid, tao_equivalent); assert_ok!(SubtensorModule::do_swap_stake( RuntimeOrigin::signed(coldkey), hotkey, @@ -1458,11 +1458,7 @@ fn test_do_swap_same_subnet() { let alpha_after = SubtensorModule::get_stake_for_hotkey_and_coldkey_on_subnet(&hotkey, &coldkey, netuid); - assert_abs_diff_eq!( - alpha_after, - alpha_before - fee_as_alpha, - epsilon = alpha_after / 10000 - ); + assert_abs_diff_eq!(alpha_after, expected_alpha, epsilon = 1000); }); } @@ -1477,7 +1473,6 @@ fn test_do_swap_partial_stake() { let coldkey = U256::from(1); let hotkey = U256::from(2); let total_stake = DefaultMinStake::::get() * 10; - let fee: u64 = 0; // FIXME: DefaultStakingFee is deprecated SubtensorModule::create_account_if_non_existent(&coldkey, &hotkey); SubtensorModule::stake_into_subnet( @@ -1490,6 +1485,8 @@ fn test_do_swap_partial_stake() { .unwrap(); let swap_amount = total_stake / 2; + let (tao_equivalent, _) = mock::swap_alpha_to_tao(origin_netuid, swap_amount); + let (expected_alpha, _) = mock::swap_tao_to_alpha(destination_netuid, tao_equivalent); assert_ok!(SubtensorModule::do_swap_stake( RuntimeOrigin::signed(coldkey), hotkey, @@ -1504,23 +1501,8 @@ fn test_do_swap_partial_stake() { &coldkey, origin_netuid ), - total_stake - swap_amount, - epsilon = total_stake / 1000 - ); - - let alpha_fee = - U96F32::from_num(fee) / ::SwapInterface::current_alpha_price(destination_netuid); - let expected_value = U96F32::from_num(swap_amount) - * ::SwapInterface::current_alpha_price(origin_netuid) - / ::SwapInterface::current_alpha_price(destination_netuid); - assert_abs_diff_eq!( - SubtensorModule::get_stake_for_hotkey_and_coldkey_on_subnet( - &hotkey, - &coldkey, - destination_netuid - ), - (expected_value - alpha_fee).to_num::(), - epsilon = (expected_value / 1000).to_num::() + expected_alpha, + epsilon = 1000 ); }); } @@ -1536,7 +1518,6 @@ fn test_do_swap_storage_updates() { let coldkey = U256::from(1); let hotkey = U256::from(2); let stake_amount = DefaultMinStake::::get() * 10; - let fee: u64 = 0; // FIXME: DefaultStakingFee is deprecated SubtensorModule::create_account_if_non_existent(&coldkey, &hotkey); SubtensorModule::stake_into_subnet( @@ -1553,6 +1534,8 @@ fn test_do_swap_storage_updates() { &coldkey, origin_netuid, ); + let (tao_equivalent, _) = mock::swap_alpha_to_tao(origin_netuid, alpha); + let (expected_alpha, _) = mock::swap_tao_to_alpha(destination_netuid, tao_equivalent); assert_ok!(SubtensorModule::do_swap_stake( RuntimeOrigin::signed(coldkey), hotkey, @@ -1570,19 +1553,14 @@ fn test_do_swap_storage_updates() { 0 ); - let alpha_fee = - U96F32::from_num(fee) / ::SwapInterface::current_alpha_price(destination_netuid); - let expected_value = U96F32::from_num(alpha) - * ::SwapInterface::current_alpha_price(origin_netuid) - / ::SwapInterface::current_alpha_price(destination_netuid); assert_abs_diff_eq!( SubtensorModule::get_stake_for_hotkey_and_coldkey_on_subnet( &hotkey, &coldkey, destination_netuid ), - (expected_value - alpha_fee).to_num::(), - epsilon = (expected_value / 1000).to_num::() + expected_alpha, + epsilon = 1000 ); }); } @@ -1598,7 +1576,6 @@ fn test_do_swap_multiple_times() { let coldkey = U256::from(1); let hotkey = U256::from(2); let initial_stake = DefaultMinStake::::get() * 10; - let fee: u64 = 0; // FIXME: DefaultStakingFee is deprecated SubtensorModule::create_account_if_non_existent(&coldkey, &hotkey); SubtensorModule::stake_into_subnet( @@ -1610,7 +1587,7 @@ fn test_do_swap_multiple_times() { ) .unwrap(); - let mut total_alpha1_fee = 0; + let mut expected_alpha: u64 = 0; for _ in 0..3 { let alpha1 = SubtensorModule::get_stake_for_hotkey_and_coldkey_on_subnet( &hotkey, &coldkey, netuid1, @@ -1623,20 +1600,14 @@ fn test_do_swap_multiple_times() { netuid2, alpha1 )); - - let fee_as_alpha = SubtensorModule::swap_tao_for_alpha( - netuid1, - fee, - ::SwapInterface::max_price(), - ) - .unwrap() - .amount_paid_out; - total_alpha1_fee += fee_as_alpha; } let alpha2 = SubtensorModule::get_stake_for_hotkey_and_coldkey_on_subnet( &hotkey, &coldkey, netuid2, ); if alpha2 > 0 { + let (tao_equivalent, _) = mock::swap_alpha_to_tao(netuid2, alpha2); + // we do this in the loop, because we need the value before the swap + expected_alpha = mock::swap_tao_to_alpha(netuid1, tao_equivalent).0; assert_ok!(SubtensorModule::do_swap_stake( RuntimeOrigin::signed(coldkey), hotkey, @@ -1644,29 +1615,18 @@ fn test_do_swap_multiple_times() { netuid1, alpha2 )); - - let fee_as_alpha = SubtensorModule::swap_tao_for_alpha( - netuid1, - fee, - ::SwapInterface::max_price(), - ) - .unwrap() - .amount_paid_out; - total_alpha1_fee += fee_as_alpha; } } - let final_stake_netuid1: u64 = - SubtensorModule::get_stake_for_hotkey_and_coldkey_on_subnet(&hotkey, &coldkey, netuid1); - let final_stake_netuid2 = - SubtensorModule::get_stake_for_hotkey_and_coldkey_on_subnet(&hotkey, &coldkey, netuid2); - let expected_stake = initial_stake - total_alpha1_fee; assert_abs_diff_eq!( - final_stake_netuid1, - expected_stake, - epsilon = initial_stake / 10000 + SubtensorModule::get_stake_for_hotkey_and_coldkey_on_subnet(&hotkey, &coldkey, netuid1), + expected_alpha, + epsilon = 1000 + ); + assert_eq!( + SubtensorModule::get_stake_for_hotkey_and_coldkey_on_subnet(&hotkey, &coldkey, netuid2), + 0 ); - assert_eq!(final_stake_netuid2, 0); }); } @@ -1736,11 +1696,14 @@ fn test_swap_stake_limit_validate() { .unwrap(); // Setup limit price so that it doesn't allow much slippage at all - let limit_price = ((::SwapInterface::current_alpha_price(origin_netuid) - / ::SwapInterface::current_alpha_price(destination_netuid)) - * U96F32::from_num(1_000_000_000)) - .to_num::() - - 1_u64; + let limit_price = + ((::SwapInterface::current_alpha_price(origin_netuid) + / ::SwapInterface::current_alpha_price( + destination_netuid, + )) + * U96F32::from_num(1_000_000_000)) + .to_num::() + - 1_u64; // Swap stake limit call let call = RuntimeCall::SubtensorModule(SubtensorCall::swap_stake_limit { @@ -1919,6 +1882,8 @@ fn test_move_stake_specific_stake_into_subnet_fail() { ); // Move stake to destination subnet + let (tao_equivalent, _) = mock::swap_alpha_to_tao(origin_netuid, alpha_to_move); + let (expected_value, _) = mock::swap_tao_to_alpha(netuid, tao_equivalent); assert_ok!(SubtensorModule::move_stake( RuntimeOrigin::signed(coldkey_account_id), hotkey_account_id, @@ -1937,19 +1902,15 @@ fn test_move_stake_specific_stake_into_subnet_fail() { ), 0 ); - let fee: u64 = 0; // FIXME: DefaultStakingFee is deprecated - let alpha_fee: U96F32 = U96F32::from_num(fee) / ::SwapInterface::current_alpha_price(netuid); - let expected_value = U96F32::from_num(alpha_to_move) - * ::SwapInterface::current_alpha_price(origin_netuid) - / ::SwapInterface::current_alpha_price(netuid); + assert_abs_diff_eq!( SubtensorModule::get_stake_for_hotkey_and_coldkey_on_subnet( &hotkey_account_id, &coldkey_account_id, netuid ), - (expected_value - alpha_fee).to_num::(), - epsilon = (expected_value / 1000).to_num::() + expected_value, + epsilon = 1000 ); }); } diff --git a/pallets/subtensor/src/tests/staking.rs b/pallets/subtensor/src/tests/staking.rs index f01b3a00c5..822e708e5d 100644 --- a/pallets/subtensor/src/tests/staking.rs +++ b/pallets/subtensor/src/tests/staking.rs @@ -985,7 +985,7 @@ fn test_remove_stake_from_hotkey_account() { let hotkey_id = U256::from(5445); let coldkey_id = U256::from(5443433); let amount = 10_000; - let netuid: u16 = add_dynamic_network(&subnet_owner_hotkey, &subnet_owner_coldkey); + let netuid = add_dynamic_network(&subnet_owner_hotkey, &subnet_owner_coldkey); register_ok_neuron(netuid, hotkey_id, coldkey_id, 192213123); // Add some stake that can be removed @@ -997,9 +997,10 @@ fn test_remove_stake_from_hotkey_account() { ); // Prelimiary checks - assert_eq!( + assert_abs_diff_eq!( SubtensorModule::get_total_stake_for_hotkey(&hotkey_id), - amount + amount, + epsilon = 10 ); // Remove stake @@ -3689,8 +3690,8 @@ fn test_add_stake_limit_fill_or_kill() { SubnetTAO::::insert(netuid, tao_reserve.to_num::()); SubnetAlphaIn::::insert(netuid, alpha_in.to_num::()); let current_price = ::SwapInterface::current_alpha_price(netuid); - // FIXME it's failing because in the swap pallet, the alpha price is set only after an - // initial swap + // FIXME it's failing because in the swap pallet, the alpha price is set only after an + // initial swap assert_eq!(current_price, U96F32::from_num(1.5)); // Give it some $$$ in his coldkey balance From c0edd409ffafa8345d3faa77aae4fedd95b30c3f Mon Sep 17 00:00:00 2001 From: Aliaksandr Tsurko Date: Thu, 8 May 2025 16:51:46 +0200 Subject: [PATCH 3/6] Fix most of staking tests --- pallets/subtensor/src/staking/helpers.rs | 14 ++++- pallets/subtensor/src/tests/coinbase.rs | 3 + pallets/subtensor/src/tests/staking.rs | 78 +++++++++++++----------- 3 files changed, 56 insertions(+), 39 deletions(-) diff --git a/pallets/subtensor/src/staking/helpers.rs b/pallets/subtensor/src/staking/helpers.rs index 3987058c36..e875228a06 100644 --- a/pallets/subtensor/src/staking/helpers.rs +++ b/pallets/subtensor/src/staking/helpers.rs @@ -55,7 +55,12 @@ impl Pallet { T::SwapInterface::max_price(), true, ) - .map(|r| r.amount_paid_out.saturating_add(r.fee_paid)) + .map(|r| { + let fee: u64 = U96F32::saturating_from_num(r.fee_paid) + .saturating_mul(T::SwapInterface::current_alpha_price(netuid)) + .saturating_to_num(); + r.amount_paid_out.saturating_add(fee) + }) .unwrap_or_default() }) .sum() @@ -80,7 +85,12 @@ impl Pallet { T::SwapInterface::max_price(), true, ) - .map(|r| r.amount_paid_out.saturating_add(r.fee_paid)) + .map(|r| { + let fee: u64 = U96F32::saturating_from_num(r.fee_paid) + .saturating_mul(T::SwapInterface::current_alpha_price(netuid)) + .saturating_to_num(); + r.amount_paid_out.saturating_add(fee) + }) .unwrap_or_default() }) .sum::() diff --git a/pallets/subtensor/src/tests/coinbase.rs b/pallets/subtensor/src/tests/coinbase.rs index 030fcf655d..f8c405edbd 100644 --- a/pallets/subtensor/src/tests/coinbase.rs +++ b/pallets/subtensor/src/tests/coinbase.rs @@ -1980,6 +1980,9 @@ fn test_run_coinbase_not_started() { // Set weight-set limit to 0. SubtensorModule::set_weights_set_rate_limit(netuid, 0); + let reserve = init_stake * 1000; + mock::setup_reserves(netuid, reserve, reserve); + register_ok_neuron(netuid, hotkey, coldkey, 0); register_ok_neuron(netuid, miner_hk, miner_ck, 0); register_ok_neuron(netuid, sn_owner_hk, sn_owner_ck, 0); diff --git a/pallets/subtensor/src/tests/staking.rs b/pallets/subtensor/src/tests/staking.rs index 822e708e5d..e77be3384e 100644 --- a/pallets/subtensor/src/tests/staking.rs +++ b/pallets/subtensor/src/tests/staking.rs @@ -44,8 +44,6 @@ fn test_add_stake_dispatch_info_ok() { #[test] fn test_add_stake_ok_no_emission() { new_test_ext(1).execute_with(|| { - // this test indicates the problem with get_alpha_price - // todo!("this test will fail because of get_alpha_price implementation"); let hotkey_account_id = U256::from(533453); let coldkey_account_id = U256::from(55453); let amount = DefaultMinStake::::get() * 10; @@ -80,16 +78,19 @@ fn test_add_stake_ok_no_emission() { )); let (tao_expected, _) = mock::swap_alpha_to_tao(netuid, alpha_staked); + let approx_fee = ::SwapInterface::approx_fee_amount(netuid, amount); - assert_eq!( + assert_abs_diff_eq!( SubtensorModule::get_total_stake_for_hotkey(&hotkey_account_id), - tao_expected + tao_expected + approx_fee, // swap returns value after fee, so we need to compensate it + epsilon = 10000, ); // Check if stake has increased - assert_eq!( + assert_abs_diff_eq!( SubtensorModule::get_total_stake_for_hotkey(&hotkey_account_id), - amount - fee + amount - fee, + epsilon = 10000 ); // Check if balance has decreased @@ -132,9 +133,10 @@ fn test_dividends_with_run_to_block() { ); // Check if the initial stake has arrived - assert_eq!( + assert_abs_diff_eq!( SubtensorModule::get_total_stake_for_hotkey(&neuron_src_hotkey_id), - initial_stake + initial_stake, + epsilon = 2 ); // Check if all three neurons are registered @@ -144,9 +146,10 @@ fn test_dividends_with_run_to_block() { run_to_block(2); // Check if the stake is equal to the inital stake + transfer - assert_eq!( + assert_abs_diff_eq!( SubtensorModule::get_total_stake_for_hotkey(&neuron_src_hotkey_id), - initial_stake + initial_stake, + epsilon = 2 ); // Check if the stake is equal to the inital stake + transfer @@ -967,9 +970,10 @@ fn test_add_stake_to_hotkey_account_ok() { ); // The stake that is now in the account, should equal the amount - assert_eq!( + assert_abs_diff_eq!( SubtensorModule::get_total_stake_for_hotkey(&hotkey_id), - amount + amount, + epsilon = 2 ); }); } @@ -1220,9 +1224,10 @@ fn test_has_enough_stake_yes() { intial_amount, ); - assert_eq!( + assert_abs_diff_eq!( SubtensorModule::get_total_stake_for_hotkey(&hotkey_id), - intial_amount + intial_amount, + epsilon = 2 ); assert_eq!( SubtensorModule::get_stake_for_hotkey_and_coldkey_on_subnet( @@ -1255,9 +1260,10 @@ fn test_has_enough_stake_no() { intial_amount, ); - assert_eq!( + assert_abs_diff_eq!( SubtensorModule::get_total_stake_for_hotkey(&hotkey_id), - intial_amount + intial_amount, + epsilon = 2 ); assert_eq!( SubtensorModule::get_stake_for_hotkey_and_coldkey_on_subnet( @@ -2149,7 +2155,6 @@ fn test_get_total_delegated_stake_exclude_owner_stake() { let delegator = U256::from(3); let owner_stake = DefaultMinStake::::get() * 10; let delegator_stake = DefaultMinStake::::get() * 10 - 1; - let fee: u64 = 0; // FIXME: DefaultStakingFee is deprecated let netuid = add_dynamic_network(&delegate_hotkey, &delegate_coldkey); @@ -2164,6 +2169,7 @@ fn test_get_total_delegated_stake_exclude_owner_stake() { // Add delegator stake SubtensorModule::add_balance_to_coldkey_account(&delegator, delegator_stake); + let (_, fee) = mock::swap_tao_to_alpha(netuid, delegator_stake); assert_ok!(SubtensorModule::add_stake( RuntimeOrigin::signed(delegator), delegate_hotkey, @@ -2190,7 +2196,7 @@ fn test_get_total_delegated_stake_exclude_owner_stake() { assert_abs_diff_eq!( actual_delegated_stake, expected_delegated_stake, - epsilon = expected_delegated_stake / 1000 + epsilon = 1000 ); }); } @@ -4058,7 +4064,6 @@ fn test_remove_99_9991_per_cent_stake_removes_all() { let coldkey_account_id = U256::from(81337); let amount = 10_000_000_000; let netuid: u16 = add_dynamic_network(&subnet_owner_hotkey, &subnet_owner_coldkey); - let mut fee: u64 = 0; // FIXME: DefaultStakingFee is deprecated register_ok_neuron(netuid, hotkey_account_id, coldkey_account_id, 192213123); // Give it some $$$ in his coldkey balance @@ -4079,6 +4084,8 @@ fn test_remove_99_9991_per_cent_stake_removes_all() { netuid, ); let remove_amount = (U64F64::from_num(alpha) * U64F64::from_num(0.999991)).to_num::(); + // we expected the entire stake to be returned + let (expected_balance, _) = mock::swap_alpha_to_tao(netuid, alpha); assert_ok!(SubtensorModule::remove_stake( RuntimeOrigin::signed(coldkey_account_id), hotkey_account_id, @@ -4087,11 +4094,10 @@ fn test_remove_99_9991_per_cent_stake_removes_all() { )); // Check that all alpha was unstaked and all TAO balance was returned (less fees) - fee = fee + fee.max((remove_amount as f64 * 0.00005) as u64); assert_abs_diff_eq!( SubtensorModule::get_coldkey_balance(&coldkey_account_id), - amount - fee, - epsilon = 100000, + expected_balance, + epsilon = 10, ); assert_eq!( SubtensorModule::get_total_stake_for_hotkey(&hotkey_account_id), @@ -4307,15 +4313,14 @@ fn test_unstake_all_alpha_hits_liquidity_min() { // Try to unstake, but we reduce liquidity too far - assert_ok!(SubtensorModule::unstake_all_alpha( - RuntimeOrigin::signed(coldkey), - hotkey, - )); + assert!( + SubtensorModule::unstake_all_alpha(RuntimeOrigin::signed(coldkey), hotkey,).is_err() + ); // Expect nothing to be unstaked let new_alpha = SubtensorModule::get_stake_for_hotkey_and_coldkey_on_subnet(&hotkey, &coldkey, netuid); - assert_abs_diff_eq!(new_alpha, stake_amount, epsilon = 0,); + assert_eq!(new_alpha, stake_amount); }); } @@ -4341,16 +4346,15 @@ fn test_unstake_all_alpha_works() { ); // Setup the Alpha pool so that removing all the Alpha will keep liq above min - let remaining_tao = I96F32::from_num(u64::from(mock::SwapMinimumReserve::get()) - 1) - .saturating_add(I96F32::from(10_000_000)); - let alpha_reserves = I110F18::from(stake_amount + 10_000_000); - let alpha = stake_amount; + let alpha_reserves = stake_amount + u64::from(mock::SwapMinimumReserve::get()); + let (tao_reserves, fee) = mock::swap_alpha_to_tao(netuid, alpha_reserves); + let fee: u64 = ::SwapInterface::current_alpha_price(netuid) + .saturating_mul(U96F32::from_num(fee)) + .to_num(); + let tao_reserves = tao_reserves + fee; - let k = I110F18::from_fixed(remaining_tao) - .saturating_mul(alpha_reserves.saturating_add(I110F18::from(alpha))); - let tao_reserves = k.safe_div(alpha_reserves); - - mock::setup_reserves(netuid, tao_reserves.to_num(), alpha_reserves.to_num()); + mock::setup_reserves(netuid, tao_reserves, alpha_reserves); + mock::setup_reserves(0, tao_reserves, alpha_reserves); // Unstake all alpha to root assert_ok!(SubtensorModule::unstake_all_alpha( @@ -4360,7 +4364,7 @@ fn test_unstake_all_alpha_works() { let new_alpha = SubtensorModule::get_stake_for_hotkey_and_coldkey_on_subnet(&hotkey, &coldkey, netuid); - assert_abs_diff_eq!(new_alpha, 0, epsilon = 1_000,); + assert_abs_diff_eq!(new_alpha, 0, epsilon = 1_000); let new_root = SubtensorModule::get_stake_for_hotkey_and_coldkey_on_subnet(&hotkey, &coldkey, 0); assert!(new_root > 100_000); From 79bf78fc71fefb02aa4b8e1085c60fc7d51d0e93 Mon Sep 17 00:00:00 2001 From: Aliaksandr Tsurko Date: Thu, 8 May 2025 17:38:41 +0200 Subject: [PATCH 4/6] Fix recycle alpha tests --- pallets/subtensor/src/tests/epoch.rs | 375 ++++++++++--------- pallets/subtensor/src/tests/recycle_alpha.rs | 15 +- 2 files changed, 201 insertions(+), 189 deletions(-) diff --git a/pallets/subtensor/src/tests/epoch.rs b/pallets/subtensor/src/tests/epoch.rs index 7df41a1301..03cedd19de 100644 --- a/pallets/subtensor/src/tests/epoch.rs +++ b/pallets/subtensor/src/tests/epoch.rs @@ -4,19 +4,19 @@ clippy::unwrap_used )] -use super::mock::*; -use crate::epoch::math::safe_exp; -use crate::*; +use std::time::Instant; use approx::assert_abs_diff_eq; use frame_support::{assert_err, assert_ok}; - -// use frame_system::Config; use rand::{Rng, SeedableRng, distributions::Uniform, rngs::StdRng, seq::SliceRandom, thread_rng}; use sp_core::{Get, U256}; -// use sp_runtime::DispatchError; -use std::time::Instant; use substrate_fixed::types::I32F32; +use subtensor_swap_interface::SwapHandler; + +use super::mock; +use super::mock::*; +use crate::epoch::math::safe_exp; +use crate::*; // Normalizes (sum to 1 except 0) the input vector directly in-place. #[allow(dead_code)] @@ -1507,40 +1507,43 @@ fn test_bonds_with_liquid_alpha() { #[test] fn test_set_alpha_disabled() { new_test_ext(1).execute_with(|| { - todo!(); - // let hotkey = U256::from(1); - // let coldkey = U256::from(1 + 456); - // let netuid = add_dynamic_network(&hotkey, &coldkey); - // let signer = RuntimeOrigin::signed(coldkey); - - // // Enable Liquid Alpha and setup - // SubtensorModule::set_liquid_alpha_enabled(netuid, true); - // migrations::migrate_create_root_network::migrate_create_root_network::(); - // SubtensorModule::add_balance_to_coldkey_account(&coldkey, 1_000_000_000_000_000); - // assert_ok!(SubtensorModule::root_register(signer.clone(), hotkey,)); - // assert_ok!(SubtensorModule::add_stake( - // signer.clone(), - // hotkey, - // netuid, - // DefaultMinStake::::get() + DefaultStakingFee::::get() - // )); - // // Only owner can set alpha values - // assert_ok!(SubtensorModule::register_network(signer.clone(), hotkey)); - - // // Explicitly set to false - // SubtensorModule::set_liquid_alpha_enabled(netuid, false); - // assert_err!( - // SubtensorModule::do_set_alpha_values(signer.clone(), netuid, 12_u16, u16::MAX), - // Error::::LiquidAlphaDisabled - // ); - - // SubtensorModule::set_liquid_alpha_enabled(netuid, true); - // assert_ok!(SubtensorModule::do_set_alpha_values( - // signer.clone(), - // netuid, - // 12_u16, - // u16::MAX - // )); + let hotkey = U256::from(1); + let coldkey = U256::from(1 + 456); + let netuid = add_dynamic_network(&hotkey, &coldkey); + let signer = RuntimeOrigin::signed(coldkey); + + // Enable Liquid Alpha and setup + SubtensorModule::set_liquid_alpha_enabled(netuid, true); + migrations::migrate_create_root_network::migrate_create_root_network::(); + SubtensorModule::add_balance_to_coldkey_account(&coldkey, 1_000_000_000_000_000); + assert_ok!(SubtensorModule::root_register(signer.clone(), hotkey,)); + let fee = ::SwapInterface::approx_fee_amount( + netuid, + DefaultMinStake::::get(), + ); + assert_ok!(SubtensorModule::add_stake( + signer.clone(), + hotkey, + netuid, + DefaultMinStake::::get() + fee + )); + // Only owner can set alpha values + assert_ok!(SubtensorModule::register_network(signer.clone(), hotkey)); + + // Explicitly set to false + SubtensorModule::set_liquid_alpha_enabled(netuid, false); + assert_err!( + SubtensorModule::do_set_alpha_values(signer.clone(), netuid, 12_u16, u16::MAX), + Error::::LiquidAlphaDisabled + ); + + SubtensorModule::set_liquid_alpha_enabled(netuid, true); + assert_ok!(SubtensorModule::do_set_alpha_values( + signer.clone(), + netuid, + 12_u16, + u16::MAX + )); }); } @@ -2769,150 +2772,152 @@ fn test_compute_ema_bonds_with_liquid_alpha_sparse_empty() { #[test] fn test_get_set_alpha() { new_test_ext(1).execute_with(|| { - todo!(); - - // let netuid = 1; - // let alpha_low: u16 = 12_u16; - // let alpha_high: u16 = u16::MAX - 10; - - // let hotkey: U256 = U256::from(1); - // let coldkey: U256 = U256::from(1 + 456); - // let signer = RuntimeOrigin::signed(coldkey); - - // // Enable Liquid Alpha and setup - // SubtensorModule::set_liquid_alpha_enabled(netuid, true); - // migrations::migrate_create_root_network::migrate_create_root_network::(); - // SubtensorModule::add_balance_to_coldkey_account(&coldkey, 1_000_000_000_000_000); - // assert_ok!(SubtensorModule::root_register(signer.clone(), hotkey,)); - - // // Should fail as signer does not own the subnet - // assert_err!( - // SubtensorModule::do_set_alpha_values(signer.clone(), netuid, alpha_low, alpha_high), - // DispatchError::BadOrigin - // ); - - // assert_ok!(SubtensorModule::register_network(signer.clone(), hotkey)); - // assert_ok!(SubtensorModule::add_stake( - // signer.clone(), - // hotkey, - // netuid, - // DefaultMinStake::::get() + DefaultStakingFee::::get() - // )); - - // assert_ok!(SubtensorModule::do_set_alpha_values( - // signer.clone(), - // netuid, - // alpha_low, - // alpha_high - // )); - // let (grabbed_alpha_low, grabbed_alpha_high): (u16, u16) = - // SubtensorModule::get_alpha_values(netuid); - - // log::info!( - // "alpha_low: {:?} alpha_high: {:?}", - // grabbed_alpha_low, - // grabbed_alpha_high - // ); - // assert_eq!(grabbed_alpha_low, alpha_low); - // assert_eq!(grabbed_alpha_high, alpha_high); - - // // Convert the u16 values to decimal values - // fn unnormalize_u16_to_float(normalized_value: u16) -> f32 { - // const MAX_U16: u16 = 65535; - // normalized_value as f32 / MAX_U16 as f32 - // } - - // let alpha_low_decimal = unnormalize_u16_to_float(alpha_low); - // let alpha_high_decimal = unnormalize_u16_to_float(alpha_high); - - // let (alpha_low_32, alpha_high_32) = SubtensorModule::get_alpha_values_32(netuid); - - // let tolerance: f32 = 1e-6; // 0.000001 - - // // Check if the values are equal to the sixth decimal - // assert!( - // (alpha_low_32.to_num::() - alpha_low_decimal).abs() < tolerance, - // "alpha_low mismatch: {} != {}", - // alpha_low_32.to_num::(), - // alpha_low_decimal - // ); - // assert!( - // (alpha_high_32.to_num::() - alpha_high_decimal).abs() < tolerance, - // "alpha_high mismatch: {} != {}", - // alpha_high_32.to_num::(), - // alpha_high_decimal - // ); - - // // 1. Liquid alpha disabled - // SubtensorModule::set_liquid_alpha_enabled(netuid, false); - // assert_err!( - // SubtensorModule::do_set_alpha_values(signer.clone(), netuid, alpha_low, alpha_high), - // Error::::LiquidAlphaDisabled - // ); - // // Correct scenario after error - // SubtensorModule::set_liquid_alpha_enabled(netuid, true); // Re-enable for further tests - // assert_ok!(SubtensorModule::do_set_alpha_values( - // signer.clone(), - // netuid, - // alpha_low, - // alpha_high - // )); - - // // 2. Alpha high too low - // let alpha_high_too_low = (u16::MAX as u32 * 4 / 5) as u16 - 1; // One less than the minimum acceptable value - // assert_err!( - // SubtensorModule::do_set_alpha_values( - // signer.clone(), - // netuid, - // alpha_low, - // alpha_high_too_low - // ), - // Error::::AlphaHighTooLow - // ); - // // Correct scenario after error - // assert_ok!(SubtensorModule::do_set_alpha_values( - // signer.clone(), - // netuid, - // alpha_low, - // alpha_high - // )); - - // // 3. Alpha low too low or too high - // let alpha_low_too_low = 0_u16; - // assert_err!( - // SubtensorModule::do_set_alpha_values( - // signer.clone(), - // netuid, - // alpha_low_too_low, - // alpha_high - // ), - // Error::::AlphaLowOutOfRange - // ); - // // Correct scenario after error - // assert_ok!(SubtensorModule::do_set_alpha_values( - // signer.clone(), - // netuid, - // alpha_low, - // alpha_high - // )); - - // let alpha_low_too_high = (u16::MAX as u32 * 4 / 5) as u16 + 1; // One more than the maximum acceptable value - // assert_err!( - // SubtensorModule::do_set_alpha_values( - // signer.clone(), - // netuid, - // alpha_low_too_high, - // alpha_high - // ), - // Error::::AlphaLowOutOfRange - // ); - // // Correct scenario after error - // assert_ok!(SubtensorModule::do_set_alpha_values( - // signer.clone(), - // netuid, - // alpha_low, - // alpha_high - // )); + let netuid = 1; + let alpha_low: u16 = 12_u16; + let alpha_high: u16 = u16::MAX - 10; + + let hotkey: U256 = U256::from(1); + let coldkey: U256 = U256::from(1 + 456); + let signer = RuntimeOrigin::signed(coldkey); + + // Enable Liquid Alpha and setup + SubtensorModule::set_liquid_alpha_enabled(netuid, true); + migrations::migrate_create_root_network::migrate_create_root_network::(); + SubtensorModule::add_balance_to_coldkey_account(&coldkey, 1_000_000_000_000_000); + assert_ok!(SubtensorModule::root_register(signer.clone(), hotkey,)); + + // Should fail as signer does not own the subnet + assert_err!( + SubtensorModule::do_set_alpha_values(signer.clone(), netuid, alpha_low, alpha_high), + DispatchError::BadOrigin + ); + + assert_ok!(SubtensorModule::register_network(signer.clone(), hotkey)); + let fee = ::SwapInterface::approx_fee_amount( + netuid, + DefaultMinStake::::get(), + ); + assert_ok!(SubtensorModule::add_stake( + signer.clone(), + hotkey, + netuid, + DefaultMinStake::::get() + fee + )); + + assert_ok!(SubtensorModule::do_set_alpha_values( + signer.clone(), + netuid, + alpha_low, + alpha_high + )); + let (grabbed_alpha_low, grabbed_alpha_high): (u16, u16) = + SubtensorModule::get_alpha_values(netuid); + + log::info!( + "alpha_low: {:?} alpha_high: {:?}", + grabbed_alpha_low, + grabbed_alpha_high + ); + assert_eq!(grabbed_alpha_low, alpha_low); + assert_eq!(grabbed_alpha_high, alpha_high); + + // Convert the u16 values to decimal values + fn unnormalize_u16_to_float(normalized_value: u16) -> f32 { + const MAX_U16: u16 = 65535; + normalized_value as f32 / MAX_U16 as f32 + } + + let alpha_low_decimal = unnormalize_u16_to_float(alpha_low); + let alpha_high_decimal = unnormalize_u16_to_float(alpha_high); + + let (alpha_low_32, alpha_high_32) = SubtensorModule::get_alpha_values_32(netuid); + + let tolerance: f32 = 1e-6; // 0.000001 + + // Check if the values are equal to the sixth decimal + assert!( + (alpha_low_32.to_num::() - alpha_low_decimal).abs() < tolerance, + "alpha_low mismatch: {} != {}", + alpha_low_32.to_num::(), + alpha_low_decimal + ); + assert!( + (alpha_high_32.to_num::() - alpha_high_decimal).abs() < tolerance, + "alpha_high mismatch: {} != {}", + alpha_high_32.to_num::(), + alpha_high_decimal + ); + + // 1. Liquid alpha disabled + SubtensorModule::set_liquid_alpha_enabled(netuid, false); + assert_err!( + SubtensorModule::do_set_alpha_values(signer.clone(), netuid, alpha_low, alpha_high), + Error::::LiquidAlphaDisabled + ); + // Correct scenario after error + SubtensorModule::set_liquid_alpha_enabled(netuid, true); // Re-enable for further tests + assert_ok!(SubtensorModule::do_set_alpha_values( + signer.clone(), + netuid, + alpha_low, + alpha_high + )); + + // 2. Alpha high too low + let alpha_high_too_low = (u16::MAX as u32 * 4 / 5) as u16 - 1; // One less than the minimum acceptable value + assert_err!( + SubtensorModule::do_set_alpha_values( + signer.clone(), + netuid, + alpha_low, + alpha_high_too_low + ), + Error::::AlphaHighTooLow + ); + // Correct scenario after error + assert_ok!(SubtensorModule::do_set_alpha_values( + signer.clone(), + netuid, + alpha_low, + alpha_high + )); + + // 3. Alpha low too low or too high + let alpha_low_too_low = 0_u16; + assert_err!( + SubtensorModule::do_set_alpha_values( + signer.clone(), + netuid, + alpha_low_too_low, + alpha_high + ), + Error::::AlphaLowOutOfRange + ); + // Correct scenario after error + assert_ok!(SubtensorModule::do_set_alpha_values( + signer.clone(), + netuid, + alpha_low, + alpha_high + )); + + let alpha_low_too_high = (u16::MAX as u32 * 4 / 5) as u16 + 1; // One more than the maximum acceptable value + assert_err!( + SubtensorModule::do_set_alpha_values( + signer.clone(), + netuid, + alpha_low_too_high, + alpha_high + ), + Error::::AlphaLowOutOfRange + ); + // Correct scenario after error + assert_ok!(SubtensorModule::do_set_alpha_values( + signer.clone(), + netuid, + alpha_low, + alpha_high + )); }); } diff --git a/pallets/subtensor/src/tests/recycle_alpha.rs b/pallets/subtensor/src/tests/recycle_alpha.rs index b142e5d3c9..51ab9782ec 100644 --- a/pallets/subtensor/src/tests/recycle_alpha.rs +++ b/pallets/subtensor/src/tests/recycle_alpha.rs @@ -1,7 +1,10 @@ use approx::assert_abs_diff_eq; use frame_support::{assert_noop, assert_ok, traits::Currency}; +use serde::de::Expected; use sp_core::U256; +use sp_core::bytes::ExpectedLen; +use super::mock; use super::mock::*; use crate::*; @@ -82,6 +85,7 @@ fn test_recycle_two_stakers() { // add stake to coldkey-hotkey pair so we can recycle it let stake = 200_000; + let (expected_alpha, _) = mock::swap_tao_to_alpha(netuid, stake); increase_stake_on_coldkey_hotkey_account(&coldkey, &hotkey, stake, netuid); // add some stake to other coldkey on same hotkey. @@ -115,7 +119,7 @@ fn test_recycle_two_stakers() { &other_coldkey, netuid ), - stake, + expected_alpha, epsilon = 2 ); @@ -151,6 +155,7 @@ fn test_recycle_staker_is_nominator() { // add stake to coldkey-hotkey pair so we can recycle it let stake = 200_000; + let (expected_alpha, _) = mock::swap_tao_to_alpha(netuid, stake); increase_stake_on_coldkey_hotkey_account(&coldkey, &hotkey, stake, netuid); // add some stake to other coldkey on same hotkey. @@ -189,7 +194,7 @@ fn test_recycle_staker_is_nominator() { // Make sure the other coldkey has no change assert_abs_diff_eq!( SubtensorModule::get_stake_for_hotkey_and_coldkey_on_subnet(&hotkey, &coldkey, netuid), - stake, + expected_alpha, epsilon = 2 ); @@ -279,6 +284,7 @@ fn test_burn_staker_is_nominator() { // add stake to coldkey-hotkey pair so we can recycle it let stake = 200_000; + let (expected_alpha, _) = mock::swap_tao_to_alpha(netuid, stake); increase_stake_on_coldkey_hotkey_account(&coldkey, &hotkey, stake, netuid); // add some stake to other coldkey on same hotkey. @@ -312,7 +318,7 @@ fn test_burn_staker_is_nominator() { // Make sure the other coldkey has no change assert_abs_diff_eq!( SubtensorModule::get_stake_for_hotkey_and_coldkey_on_subnet(&hotkey, &coldkey, netuid), - stake, + expected_alpha, epsilon = 2 ); @@ -348,6 +354,7 @@ fn test_burn_two_stakers() { // add stake to coldkey-hotkey pair so we can recycle it let stake = 200_000; + let (expected_alpha, _) = mock::swap_tao_to_alpha(netuid, stake); increase_stake_on_coldkey_hotkey_account(&coldkey, &hotkey, stake, netuid); // add some stake to other coldkey on same hotkey. @@ -381,7 +388,7 @@ fn test_burn_two_stakers() { &other_coldkey, netuid ), - stake, + expected_alpha, epsilon = 2 ); From 8509dc623618b8a2be2939eb70a82fc34278358d Mon Sep 17 00:00:00 2001 From: Aliaksandr Tsurko Date: Thu, 8 May 2025 18:10:04 +0200 Subject: [PATCH 5/6] Fix burned registration tests --- pallets/subtensor/src/tests/registration.rs | 30 ++++++++++++++++++--- 1 file changed, 26 insertions(+), 4 deletions(-) diff --git a/pallets/subtensor/src/tests/registration.rs b/pallets/subtensor/src/tests/registration.rs index 1ae16d95c0..0d71ea98af 100644 --- a/pallets/subtensor/src/tests/registration.rs +++ b/pallets/subtensor/src/tests/registration.rs @@ -1,16 +1,18 @@ #![allow(clippy::unwrap_used)] use approx::assert_abs_diff_eq; -use frame_support::traits::Currency; - -use super::mock::*; -use crate::{AxonInfoOf, CustomTransactionError, Error, SubtensorSignedExtension}; use frame_support::dispatch::{DispatchClass, DispatchInfo, GetDispatchInfo, Pays}; use frame_support::sp_runtime::{DispatchError, transaction_validity::InvalidTransaction}; +use frame_support::traits::Currency; use frame_support::{assert_err, assert_noop, assert_ok}; use frame_system::Config; use sp_core::U256; use sp_runtime::traits::{DispatchInfoOf, SignedExtension}; +use subtensor_swap_interface::SwapHandler; + +use super::mock; +use super::mock::*; +use crate::{AxonInfoOf, CustomTransactionError, Error, SubtensorSignedExtension}; /******************************************** subscribing::subscribe() tests @@ -300,6 +302,9 @@ fn test_burned_registration_under_limit() { // Set the burn cost SubtensorModule::set_burn(netuid, burn_cost); + let reserve = 1_000_000_000_000; + mock::setup_reserves(netuid, reserve, reserve); + add_network(netuid, 13, 0); // Add the network // Give it some TAO to the coldkey balance; more than the burn cost SubtensorModule::add_balance_to_coldkey_account(&coldkey_account_id, burn_cost + 10_000); @@ -383,6 +388,9 @@ fn test_burned_registration_rate_allows_burn_adjustment() { // Set the burn cost SubtensorModule::set_burn(netuid, burn_cost); + let reserve = 1_000_000_000_000; + mock::setup_reserves(netuid, reserve, reserve); + add_network(netuid, 13, 0); // Add the network // Give it some TAO to the coldkey balance; more than the burn cost SubtensorModule::add_balance_to_coldkey_account(&coldkey_account_id, burn_cost + 10_000); @@ -429,6 +437,10 @@ fn test_burned_registration_ok() { //add network SubtensorModule::set_burn(netuid, burn_cost); add_network(netuid, tempo, 0); + + let reserve = 1_000_000_000_000; + mock::setup_reserves(netuid, reserve, reserve); + // Give it some $$$ in his coldkey balance SubtensorModule::add_balance_to_coldkey_account(&coldkey_account_id, 10000); // Subscribe and check extrinsic output @@ -548,6 +560,9 @@ fn test_burn_adjustment() { target_registrations_per_interval, ); + let reserve = 1_000_000_000_000; + mock::setup_reserves(netuid, reserve, reserve); + // Register key 1. let hotkey_account_id_1 = U256::from(1); let coldkey_account_id_1 = U256::from(1); @@ -601,6 +616,9 @@ fn test_burn_registration_pruning_scenarios() { SubtensorModule::set_target_registrations_per_interval(netuid, max_allowed_uids); SubtensorModule::set_immunity_period(netuid, immunity_period); + let reserve = 1_000_000_000_000; + mock::setup_reserves(netuid, reserve, reserve); + add_network(netuid, tempo, 0); let mint_balance = burn_cost * u64::from(max_allowed_uids) + 1_000_000_000; @@ -1503,6 +1521,10 @@ fn test_burn_registration_increase_recycled_rao() { let _ = Balances::deposit_creating(&coldkey_account_id, Balance::from(1_000_000_000_000_u64)); + let reserve = 1_000_000_000_000; + mock::setup_reserves(netuid, reserve, reserve); + mock::setup_reserves(netuid2, reserve, reserve); + add_network(netuid, 13, 0); assert_eq!(SubtensorModule::get_subnetwork_n(netuid), 0); From 84cd46252ab4b22ca7e6d3071f85b4a2fb282c3e Mon Sep 17 00:00:00 2001 From: Aliaksandr Tsurko Date: Thu, 8 May 2025 19:24:11 +0200 Subject: [PATCH 6/6] Fix more tests --- pallets/subtensor/src/tests/move_stake.rs | 1 - pallets/subtensor/src/tests/senate.rs | 1 - pallets/subtensor/src/tests/swap_coldkey.rs | 35 ++++++++++++--------- pallets/subtensor/src/tests/weights.rs | 15 ++++----- 4 files changed, 28 insertions(+), 24 deletions(-) diff --git a/pallets/subtensor/src/tests/move_stake.rs b/pallets/subtensor/src/tests/move_stake.rs index 0b3bf447b2..4f6ccb6d7f 100644 --- a/pallets/subtensor/src/tests/move_stake.rs +++ b/pallets/subtensor/src/tests/move_stake.rs @@ -86,7 +86,6 @@ fn test_do_move_different_subnets() { let origin_hotkey = U256::from(2); let destination_hotkey = U256::from(3); let stake_amount = DefaultMinStake::::get() * 10; - let fee: u64 = 0; // FIXME: DefaultStakingFee is deprecated mock::setup_reserves(origin_netuid, stake_amount * 100, stake_amount * 100); mock::setup_reserves(destination_netuid, stake_amount * 100, stake_amount * 100); diff --git a/pallets/subtensor/src/tests/senate.rs b/pallets/subtensor/src/tests/senate.rs index bd1bebf9b3..1efefc6130 100644 --- a/pallets/subtensor/src/tests/senate.rs +++ b/pallets/subtensor/src/tests/senate.rs @@ -562,7 +562,6 @@ fn test_senate_not_leave_when_stake_removed() { let hotkey_account_id = U256::from(6); let burn_cost = 1000; let coldkey_account_id = U256::from(667); // Neighbour of the beast, har har - let fee: u64 = 0; // FIXME: DefaultStakingFee is deprecated //add network SubtensorModule::set_burn(netuid, burn_cost); diff --git a/pallets/subtensor/src/tests/swap_coldkey.rs b/pallets/subtensor/src/tests/swap_coldkey.rs index 76c921469f..3727b9d2af 100644 --- a/pallets/subtensor/src/tests/swap_coldkey.rs +++ b/pallets/subtensor/src/tests/swap_coldkey.rs @@ -353,7 +353,6 @@ fn test_swap_with_max_values() { let netuid2 = 2u16; let stake = 10_000; let max_stake = 21_000_000_000_000_000; // 21 Million TAO; max possible balance. - let fee: u64 = 0; // FIXME: DefaultStakingFee is deprecated // Add a network add_network(netuid, 1, 0); @@ -373,7 +372,7 @@ fn test_swap_with_max_values() { mock::setup_reserves(netuid2, reserve, reserve); // Stake to hotkey on each subnet. - let (expected_stake_alpha1, fee) = mock::swap_tao_to_alpha(netuid, max_stake); + let (_, fee) = mock::swap_tao_to_alpha(netuid2, max_stake); assert_ok!(SubtensorModule::add_stake( <::RuntimeOrigin>::signed(old_coldkey), hotkey, @@ -381,7 +380,7 @@ fn test_swap_with_max_values() { max_stake )); - let (expected_stake_alpha2, fee) = mock::swap_tao_to_alpha(netuid2, max_stake); + let (_, fee) = mock::swap_tao_to_alpha(netuid2, max_stake); assert_ok!(SubtensorModule::add_stake( <::RuntimeOrigin>::signed(old_coldkey2), hotkey2, @@ -401,21 +400,25 @@ fn test_swap_with_max_values() { &mut weight )); + let expected_stake = max_stake - fee; + assert_eq!( SubtensorModule::get_total_stake_for_coldkey(&old_coldkey), 0 ); - assert_eq!( + assert_abs_diff_eq!( SubtensorModule::get_total_stake_for_coldkey(&new_coldkey), - expected_stake_alpha1 + expected_stake, + epsilon = expected_stake / 1000 ); assert_eq!( SubtensorModule::get_total_stake_for_coldkey(&old_coldkey2), 0 ); - assert_eq!( + assert_abs_diff_eq!( SubtensorModule::get_total_stake_for_coldkey(&new_coldkey2), - expected_stake_alpha2 + expected_stake, + epsilon = expected_stake / 1000 ); }); } @@ -440,7 +443,7 @@ fn test_swap_with_non_existent_new_coldkey() { // Stake to hotkey. - let (expected_stake_alpha, fee) = mock::swap_tao_to_alpha(netuid, stake); + let (_, fee) = mock::swap_tao_to_alpha(netuid, stake); assert_ok!(SubtensorModule::add_stake( <::RuntimeOrigin>::signed(old_coldkey), hotkey, @@ -459,9 +462,11 @@ fn test_swap_with_non_existent_new_coldkey() { SubtensorModule::get_total_stake_for_coldkey(&old_coldkey), 0 ); - assert_eq!( + let expected_stake = stake - fee; + assert_abs_diff_eq!( SubtensorModule::get_total_stake_for_coldkey(&new_coldkey), - expected_stake_alpha + expected_stake, + epsilon = expected_stake / 1000 ); }); } @@ -571,7 +576,7 @@ fn test_swap_concurrent_modifications() { ); register_ok_neuron(netuid, hotkey, new_coldkey, 1001000); - let (initial_stake_alpha, fee) = mock::swap_tao_to_alpha(netuid, initial_stake); + let (initial_stake_alpha, _) = mock::swap_tao_to_alpha(netuid, initial_stake); assert_ok!(SubtensorModule::add_stake( <::RuntimeOrigin>::signed(new_coldkey), hotkey, @@ -591,7 +596,7 @@ fn test_swap_concurrent_modifications() { // Wait some blocks step_block(10); - let (additional_stake_alpha, fee) = mock::swap_tao_to_alpha(netuid, additional_stake); + let (additional_stake_alpha, _) = mock::swap_tao_to_alpha(netuid, additional_stake); // Simulate concurrent stake addition assert_ok!(SubtensorModule::add_stake( @@ -855,7 +860,7 @@ fn test_swap_stake_for_coldkey() { mock::setup_reserves(netuid, reserve, reserve); // Stake to hotkeys - let (expected_stake_alpha1, fee) = mock::swap_tao_to_alpha(netuid, stake_amount1); + let (expected_stake_alpha1, _) = mock::swap_tao_to_alpha(netuid, stake_amount1); assert_ok!(SubtensorModule::add_stake( <::RuntimeOrigin>::signed(old_coldkey), @@ -864,7 +869,7 @@ fn test_swap_stake_for_coldkey() { stake_amount1 )); - let (expected_stake_alpha2, fee) = mock::swap_tao_to_alpha(netuid, stake_amount2); + let (expected_stake_alpha2, _) = mock::swap_tao_to_alpha(netuid, stake_amount2); assert_ok!(SubtensorModule::add_stake( <::RuntimeOrigin>::signed(old_coldkey), hotkey2, @@ -894,7 +899,7 @@ fn test_swap_stake_for_coldkey() { // give new coldkey some balance SubtensorModule::add_balance_to_coldkey_account(&new_coldkey, stake_amount3 + 1_000_000); // Stake to hotkey1 - let (expected_stake_alpha3, fee) = mock::swap_tao_to_alpha(netuid, stake_amount3); + let (expected_stake_alpha3, _) = mock::swap_tao_to_alpha(netuid, stake_amount3); assert_ok!(SubtensorModule::add_stake( <::RuntimeOrigin>::signed(new_coldkey), hotkey1, diff --git a/pallets/subtensor/src/tests/weights.rs b/pallets/subtensor/src/tests/weights.rs index 0d5066b181..bb40c6645d 100644 --- a/pallets/subtensor/src/tests/weights.rs +++ b/pallets/subtensor/src/tests/weights.rs @@ -1,17 +1,15 @@ #![allow(clippy::indexing_slicing)] -use super::mock; -use super::mock::*; -use crate::coinbase::run_coinbase::WeightsTlockPayload; -use crate::*; use ark_serialize::CanonicalDeserialize; use frame_support::{ assert_err, assert_ok, dispatch::{DispatchClass, DispatchResult, GetDispatchInfo, Pays}, }; +use pallet_drand::types::Pulse; use rand_chacha::{ChaCha20Rng, rand_core::SeedableRng}; use scale_info::prelude::collections::HashMap; use sha2::Digest; +use sp_core::Encode; use sp_core::{Get, H256, U256}; use sp_runtime::{ BoundedVec, DispatchError, @@ -19,6 +17,7 @@ use sp_runtime::{ }; use sp_std::collections::vec_deque::VecDeque; use substrate_fixed::types::I32F32; +use subtensor_swap_interface::SwapHandler; use tle::{ curves::drand::TinyBLS381, ibe::fullident::Identity, @@ -27,8 +26,10 @@ use tle::{ }; use w3f_bls::EngineBLS; -use pallet_drand::types::Pulse; -use sp_core::Encode; +use super::mock; +use super::mock::*; +use crate::coinbase::run_coinbase::WeightsTlockPayload; +use crate::*; /*************************** pub fn set_weights() tests @@ -300,7 +301,6 @@ fn test_set_weights_validate() { let coldkey = U256::from(0); let hotkey: U256 = U256::from(1); assert_ne!(hotkey, coldkey); - let fee: u64 = 0; // FIXME: DefaultStakingFee is deprecated let who = hotkey; // The hotkey signs this transaction @@ -341,6 +341,7 @@ fn test_set_weights_validate() { ); // Increase the stake to be equal to the minimum + let fee = ::SwapInterface::approx_fee_amount(netuid, min_stake); assert_ok!(SubtensorModule::do_add_stake( RuntimeOrigin::signed(hotkey), hotkey,