diff --git a/pallets/subtensor/src/tests/staking.rs b/pallets/subtensor/src/tests/staking.rs index f8552e197..f912e6cea 100644 --- a/pallets/subtensor/src/tests/staking.rs +++ b/pallets/subtensor/src/tests/staking.rs @@ -102,7 +102,12 @@ fn test_dividends_with_run_to_block() { register_ok_neuron(netuid, neuron_dest_hotkey_id, coldkey_account_id, 12323); // Add some stake to the hotkey account, so we can test for emission before the transfer takes place - SubtensorModule::increase_stake_for_hotkey_and_coldkey_on_subnet(&neuron_src_hotkey_id, &coldkey_account_id, netuid, initial_stake); + SubtensorModule::increase_stake_for_hotkey_and_coldkey_on_subnet( + &neuron_src_hotkey_id, + &coldkey_account_id, + netuid, + initial_stake, + ); // Check if the initial stake has arrived assert_eq!( @@ -138,12 +143,7 @@ fn test_add_stake_err_signature() { let netuid = 1; assert_err!( - SubtensorModule::add_stake( - RawOrigin::None.into(), - hotkey_account_id, - netuid, - amount, - ), + SubtensorModule::add_stake(RawOrigin::None.into(), hotkey_account_id, netuid, amount,), DispatchError::BadOrigin ); }); @@ -361,7 +361,12 @@ fn test_remove_stake_ok_no_emission() { assert_eq!(SubtensorModule::get_coldkey_balance(&coldkey_account_id), 0); // Give the neuron some stake to remove - SubtensorModule::increase_stake_for_hotkey_and_coldkey_on_subnet(&hotkey_account_id, &coldkey_account_id, netuid, amount); + SubtensorModule::increase_stake_for_hotkey_and_coldkey_on_subnet( + &hotkey_account_id, + &coldkey_account_id, + netuid, + amount, + ); // Do the magic assert_ok!(SubtensorModule::remove_stake( @@ -372,9 +377,7 @@ fn test_remove_stake_ok_no_emission() { )); // we do not expect the exact amount due to slippage - assert!( - SubtensorModule::get_coldkey_balance(&coldkey_account_id) > amount / 10 * 9, - ); + assert!(SubtensorModule::get_coldkey_balance(&coldkey_account_id) > amount / 10 * 9,); assert_eq!( SubtensorModule::get_total_stake_for_hotkey(&hotkey_account_id), 0 @@ -403,7 +406,12 @@ fn test_remove_stake_amount_zero() { assert_eq!(SubtensorModule::get_coldkey_balance(&coldkey_account_id), 0); // Give the neuron some stake to remove - SubtensorModule::increase_stake_for_hotkey_and_coldkey_on_subnet(&hotkey_account_id, &coldkey_account_id, netuid, amount); + SubtensorModule::increase_stake_for_hotkey_and_coldkey_on_subnet( + &hotkey_account_id, + &coldkey_account_id, + netuid, + amount, + ); // Do the magic assert_noop!( @@ -447,14 +455,19 @@ fn test_remove_stake_ok_hotkey_does_not_belong_to_coldkey() { let netuid: u16 = add_dynamic_network(&hotkey_id, &coldkey_id); // Give the neuron some stake to remove - SubtensorModule::increase_stake_for_hotkey_and_coldkey_on_subnet(&hotkey_id, &other_cold_key, netuid, amount); + SubtensorModule::increase_stake_for_hotkey_and_coldkey_on_subnet( + &hotkey_id, + &other_cold_key, + netuid, + amount, + ); assert_ok!(SubtensorModule::remove_stake( RuntimeOrigin::signed(other_cold_key), hotkey_id, netuid, amount, - )); + )); }); } @@ -476,7 +489,7 @@ fn test_remove_stake_no_enough_stake() { amount, ), Error::::NotEnoughStakeToWithdraw - ); + ); }); } @@ -505,7 +518,12 @@ fn test_remove_stake_total_balance_no_change() { assert_eq!(initial_total_balance, 0); // Give the neuron some stake to remove - SubtensorModule::increase_stake_for_hotkey_and_coldkey_on_subnet(&hotkey_account_id, &coldkey_account_id, netuid, amount); + SubtensorModule::increase_stake_for_hotkey_and_coldkey_on_subnet( + &hotkey_account_id, + &coldkey_account_id, + netuid, + amount, + ); // Do the magic assert_ok!(SubtensorModule::remove_stake( @@ -528,11 +546,7 @@ fn test_remove_stake_total_balance_no_change() { // Check total balance is equal to the added stake. Even after remove stake (no fee, includes reserved/locked balance) let total_balance = Balances::total_balance(&coldkey_account_id); - assert_abs_diff_eq!( - total_balance, - amount, - epsilon = 1, - ); + assert_abs_diff_eq!(total_balance, amount, epsilon = 1,); }); } @@ -552,14 +566,17 @@ fn test_remove_stake_total_issuance_no_change() { // Give it some $$$ in his coldkey balance SubtensorModule::add_balance_to_coldkey_account(&coldkey_account_id, amount); - + // Some basic assertions assert_eq!(SubtensorModule::get_total_stake(), 0); assert_eq!( SubtensorModule::get_total_stake_for_hotkey(&hotkey_account_id), 0 ); - assert_eq!(SubtensorModule::get_coldkey_balance(&coldkey_account_id), amount); + assert_eq!( + SubtensorModule::get_coldkey_balance(&coldkey_account_id), + amount + ); let initial_total_balance = Balances::total_balance(&coldkey_account_id); assert_eq!(initial_total_balance, amount); let inital_total_issuance = Balances::total_issuance(); @@ -594,11 +611,7 @@ fn test_remove_stake_total_issuance_no_change() { SubtensorModule::get_total_stake_for_hotkey(&hotkey_account_id), 0 ); - assert_abs_diff_eq!( - SubtensorModule::get_total_stake(), - 0, - epsilon = 1, - ); + assert_abs_diff_eq!(SubtensorModule::get_total_stake(), 0, epsilon = 1,); // Check if total issuance is equal to the added stake, even after remove stake (no fee, includes reserved/locked balance) assert_abs_diff_eq!( @@ -656,12 +669,17 @@ fn test_add_stake_to_hotkey_account_ok() { let coldkey_id = U256::from(5443433); let amount = 10_000; let netuid: u16 = add_dynamic_network(&subnet_owner_hotkey, &subnet_owner_coldkey); - register_ok_neuron(netuid, hotkey_id, coldkey_id, 192213123); + register_ok_neuron(netuid, hotkey_id, coldkey_id, 192213123); // There is not stake in the system at first, so result should be 0; assert_eq!(SubtensorModule::get_total_stake(), 0); - SubtensorModule::increase_stake_for_hotkey_and_coldkey_on_subnet(&hotkey_id, &coldkey_id, netuid, amount); + SubtensorModule::increase_stake_for_hotkey_and_coldkey_on_subnet( + &hotkey_id, + &coldkey_id, + netuid, + amount, + ); // The stake that is now in the account, should equal the amount assert_eq!( @@ -683,10 +701,15 @@ fn test_remove_stake_from_hotkey_account() { let coldkey_id = U256::from(5443433); let amount = 10_000; let netuid: u16 = add_dynamic_network(&subnet_owner_hotkey, &subnet_owner_coldkey); - register_ok_neuron(netuid, hotkey_id, coldkey_id, 192213123); + register_ok_neuron(netuid, hotkey_id, coldkey_id, 192213123); // Add some stake that can be removed - SubtensorModule::increase_stake_for_hotkey_and_coldkey_on_subnet(&hotkey_id, &coldkey_id, netuid, amount); + SubtensorModule::increase_stake_for_hotkey_and_coldkey_on_subnet( + &hotkey_id, + &coldkey_id, + netuid, + amount, + ); // Prelimiary checks assert_eq!( @@ -695,7 +718,12 @@ fn test_remove_stake_from_hotkey_account() { ); // Remove stake - SubtensorModule::decrease_stake_for_hotkey_and_coldkey_on_subnet(&hotkey_id, &coldkey_id, netuid, amount); + SubtensorModule::decrease_stake_for_hotkey_and_coldkey_on_subnet( + &hotkey_id, + &coldkey_id, + netuid, + amount, + ); // The stake on the hotkey account should be 0 assert_eq!(SubtensorModule::get_total_stake_for_hotkey(&hotkey_id), 0); @@ -723,7 +751,12 @@ fn test_remove_stake_from_hotkey_account_registered_in_various_networks() { }; // Add some stake that can be removed - SubtensorModule::increase_stake_for_hotkey_and_coldkey_on_subnet(&hotkey_id, &coldkey_id, netuid, amount); + SubtensorModule::increase_stake_for_hotkey_and_coldkey_on_subnet( + &hotkey_id, + &coldkey_id, + netuid, + amount, + ); assert_eq!( SubtensorModule::get_stake_for_uid_and_subnetwork(netuid, neuron_uid), @@ -735,7 +768,12 @@ fn test_remove_stake_from_hotkey_account_registered_in_various_networks() { ); // Remove all stake - SubtensorModule::decrease_stake_for_hotkey_and_coldkey_on_subnet(&hotkey_id, &coldkey_id, netuid, amount); + SubtensorModule::decrease_stake_for_hotkey_and_coldkey_on_subnet( + &hotkey_id, + &coldkey_id, + netuid, + amount, + ); // assert_eq!( @@ -889,14 +927,23 @@ fn test_has_enough_stake_yes() { let coldkey_id = U256::from(87989); let intial_amount = 10_000; let netuid = add_dynamic_network(&hotkey_id, &coldkey_id); - SubtensorModule::increase_stake_for_hotkey_and_coldkey_on_subnet(&hotkey_id, &coldkey_id, netuid, intial_amount); + SubtensorModule::increase_stake_for_hotkey_and_coldkey_on_subnet( + &hotkey_id, + &coldkey_id, + netuid, + intial_amount, + ); assert_eq!( SubtensorModule::get_total_stake_for_hotkey(&hotkey_id), intial_amount ); assert_eq!( - SubtensorModule::get_stake_for_hotkey_and_coldkey_on_subnet(&hotkey_id, &coldkey_id, netuid), + SubtensorModule::get_stake_for_hotkey_and_coldkey_on_subnet( + &hotkey_id, + &coldkey_id, + netuid + ), intial_amount ); assert!(SubtensorModule::has_enough_stake_on_subnet( @@ -915,14 +962,23 @@ fn test_has_enough_stake_no() { let coldkey_id = U256::from(87989); let intial_amount = 10_000; let netuid = add_dynamic_network(&hotkey_id, &coldkey_id); - SubtensorModule::increase_stake_for_hotkey_and_coldkey_on_subnet(&hotkey_id, &coldkey_id, netuid, intial_amount); + SubtensorModule::increase_stake_for_hotkey_and_coldkey_on_subnet( + &hotkey_id, + &coldkey_id, + netuid, + intial_amount, + ); assert_eq!( SubtensorModule::get_total_stake_for_hotkey(&hotkey_id), intial_amount ); assert_eq!( - SubtensorModule::get_stake_for_hotkey_and_coldkey_on_subnet(&hotkey_id, &coldkey_id, netuid), + SubtensorModule::get_stake_for_hotkey_and_coldkey_on_subnet( + &hotkey_id, + &coldkey_id, + netuid + ), intial_amount ); assert!(!SubtensorModule::has_enough_stake_on_subnet( @@ -947,7 +1003,11 @@ fn test_has_enough_stake_no_for_zero() { intial_amount ); assert_eq!( - SubtensorModule::get_stake_for_hotkey_and_coldkey_on_subnet(&hotkey_id, &coldkey_id, netuid), + SubtensorModule::get_stake_for_hotkey_and_coldkey_on_subnet( + &hotkey_id, + &coldkey_id, + netuid + ), intial_amount ); assert!(!SubtensorModule::has_enough_stake_on_subnet( @@ -970,7 +1030,11 @@ fn test_non_existent_account() { 10, ); assert_eq!( - SubtensorModule::get_stake_for_hotkey_and_coldkey_on_subnet(&U256::from(0), &U256::from(0), netuid), + SubtensorModule::get_stake_for_hotkey_and_coldkey_on_subnet( + &U256::from(0), + &U256::from(0), + netuid + ), 10 ); // No subnets => no iteration => zero total stake @@ -1612,8 +1676,14 @@ fn test_get_total_delegated_stake_single_delegator() { log::debug!("Delegator: {:?}", delegator); log::debug!("Stake amount: {}", stake_amount); log::debug!("Existential deposit: {}", existential_deposit); - log::debug!("Total stake for hotkey: {}", SubtensorModule::get_total_stake_for_hotkey(&delegate_hotkey)); - log::debug!("Delegated stake for coldkey: {}", SubtensorModule::get_total_stake_for_coldkey(&delegate_coldkey)); + log::debug!( + "Total stake for hotkey: {}", + SubtensorModule::get_total_stake_for_hotkey(&delegate_hotkey) + ); + log::debug!( + "Delegated stake for coldkey: {}", + SubtensorModule::get_total_stake_for_coldkey(&delegate_coldkey) + ); // Calculate expected delegated stake let expected_delegated_stake = stake_amount - existential_deposit; @@ -1661,6 +1731,7 @@ fn test_get_total_delegated_stake_multiple_delegators() { // assert_ok!(SubtensorModule::add_stake( // RuntimeOrigin::signed(delegator1), // delegate_hotkey, + // netuid, // stake1 // )); @@ -1669,6 +1740,7 @@ fn test_get_total_delegated_stake_multiple_delegators() { // assert_ok!(SubtensorModule::add_stake( // RuntimeOrigin::signed(delegator2), // delegate_hotkey, + // netuid, // stake2 // )); @@ -1683,12 +1755,10 @@ fn test_get_total_delegated_stake_multiple_delegators() { // let expected_total_delegated = stake1 + stake2 - 2 * existential_deposit; // let actual_total_delegated = SubtensorModule::get_total_stake_for_coldkey(&delegate_coldkey); - // assert_eq!( + // assert_abs_diff_eq!( // actual_total_delegated, // expected_total_delegated, - // "Total delegated stake should match the sum of delegators' stakes minus existential deposits. Expected: {}, Actual: {}", - // expected_total_delegated, - // actual_total_delegated + // epsilon = 1, // ); }); } @@ -1696,540 +1766,63 @@ fn test_get_total_delegated_stake_multiple_delegators() { #[test] fn test_get_total_delegated_stake_exclude_owner_stake() { new_test_ext(1).execute_with(|| { - assert!(false); - - // let netuid = 1u16; - // let delegate_coldkey = U256::from(1); - // let delegate_hotkey = U256::from(2); - // let delegator = U256::from(3); - // let owner_stake = 1000; - // let delegator_stake = 999; - // let existential_deposit = 1; // Account for the existential deposit - - // add_network(netuid, 0, 0); - // register_ok_neuron(netuid, delegate_hotkey, delegate_coldkey, 0); - - // // Make the account a delegate - // assert_ok!(SubtensorModule::become_delegate( - // RuntimeOrigin::signed(delegate_coldkey), - // delegate_hotkey - // )); - - // // Add owner stake - // SubtensorModule::add_balance_to_coldkey_account(&delegate_coldkey, owner_stake); - // assert_ok!(SubtensorModule::add_stake( - // RuntimeOrigin::signed(delegate_coldkey), - // delegate_hotkey, - // owner_stake - // )); - - // // Add delegator stake - // SubtensorModule::add_balance_to_coldkey_account(&delegator, delegator_stake); - // assert_ok!(SubtensorModule::add_stake( - // RuntimeOrigin::signed(delegator), - // delegate_hotkey, - // delegator_stake - // )); - - // // Debug prints - // println!("Owner stake: {}", owner_stake); - // println!("Delegator stake: {}", delegator_stake); - // println!("Existential deposit: {}", existential_deposit); - // println!( - // "Total stake for hotkey: {}", - // SubtensorModule::get_total_stake_for_hotkey(&delegate_hotkey) - // ); - // println!( - // "Delegated stake for coldkey: {}", - // SubtensorModule::get_total_stake_for_coldkey(&delegate_coldkey) - // ); - - // // Check the total delegated stake (should exclude owner's stake) - // let expected_delegated_stake = delegator_stake - existential_deposit; - // let actual_delegated_stake = SubtensorModule::get_total_stake_for_coldkey(&delegate_coldkey); - - // assert_eq!( - // actual_delegated_stake, expected_delegated_stake, - // "Delegated stake should exclude owner's stake. Expected: {}, Actual: {}", - // expected_delegated_stake, actual_delegated_stake - // ); - }); -} - -// SKIP_WASM_BUILD=1 RUST_LOG=debug cargo test --test staking -- test_stake_delta_tracks_adds_and_removes --exact --nocapture -#[test] -fn test_stake_delta_tracks_adds_and_removes() { - new_test_ext(1).execute_with(|| { - assert!(false); - - // let netuid = 1u16; - // let delegate_coldkey = U256::from(1); - // let delegate_hotkey = U256::from(2); - // let delegator = U256::from(3); - - // let owner_stake = 1000; - // let owner_added_stake = 123; - // let owner_removed_stake = 456; - // // Add more than removed to test that the delta is updated correctly - // let owner_adds_more_stake = owner_removed_stake + 1; - - // let delegator_added_stake = 999; - - // add_network(netuid, 0, 0); - // register_ok_neuron(netuid, delegate_hotkey, delegate_coldkey, 0); - // // Give extra stake to the owner - // SubtensorModule::increase_stake_on_coldkey_hotkey_account( - // &delegate_coldkey, - // &delegate_hotkey, - // owner_stake, - // ); - - // // Register as a delegate - // assert_ok!(SubtensorModule::become_delegate( - // RuntimeOrigin::signed(delegate_coldkey), - // delegate_hotkey - // )); - - // // Verify that the stake delta is empty - // assert_eq!( - // StakeDeltaSinceLastEmissionDrain::::get(delegate_hotkey, delegate_coldkey), - // 0 - // ); - - // // Give the coldkey some balance; extra just in case - // SubtensorModule::add_balance_to_coldkey_account( - // &delegate_coldkey, - // owner_added_stake + owner_adds_more_stake, - // ); - - // // Add some stake - // assert_ok!(SubtensorModule::add_stake( - // RuntimeOrigin::signed(delegate_coldkey), - // delegate_hotkey, - // owner_added_stake - // )); - - // // Verify that the stake delta is correct - // assert_eq!( - // StakeDeltaSinceLastEmissionDrain::::get(delegate_hotkey, delegate_coldkey), - // i128::from(owner_added_stake) - // ); - - // // Add some stake from a delegator - // SubtensorModule::add_balance_to_coldkey_account(&delegator, delegator_added_stake); - // assert_ok!(SubtensorModule::add_stake( - // RuntimeOrigin::signed(delegator), - // delegate_hotkey, - // delegator_added_stake - // )); - - // // Verify that the stake delta is unchanged for the owner - // assert_eq!( - // StakeDeltaSinceLastEmissionDrain::::get(delegate_hotkey, delegate_coldkey), - // i128::from(owner_added_stake) - // ); - - // // Remove some stake - // assert_ok!(SubtensorModule::remove_stake( - // RuntimeOrigin::signed(delegate_coldkey), - // delegate_hotkey, - // owner_removed_stake - // )); - - // // Verify that the stake delta is correct - // assert_eq!( - // StakeDeltaSinceLastEmissionDrain::::get(delegate_hotkey, delegate_coldkey), - // i128::from(owner_added_stake).saturating_sub_unsigned(owner_removed_stake.into()) - // ); - - // // Add more stake than was removed - // assert_ok!(SubtensorModule::add_stake( - // RuntimeOrigin::signed(delegate_coldkey), - // delegate_hotkey, - // owner_adds_more_stake - // )); - - // // Verify that the stake delta is correct - // assert_eq!( - // StakeDeltaSinceLastEmissionDrain::::get(delegate_hotkey, delegate_coldkey), - // i128::from(owner_added_stake) - // .saturating_add_unsigned((owner_adds_more_stake - owner_removed_stake).into()) - // ); - }); -} - -/// Test that drain_hotkey_emission sends mining emission fully to the miner, even -/// if miner is a delegate and someone is delegating. -#[test] -fn test_mining_emission_drain() { - new_test_ext(1).execute_with(|| { - assert!(false); + let netuid = 1u16; + let delegate_coldkey = U256::from(1); + let delegate_hotkey = U256::from(2); + let delegator = U256::from(3); + let owner_stake = 1000; + let delegator_stake = 999; + let existential_deposit = 1; // Account for the existential deposit - // let coldkey = U256::from(1); - // let validator = U256::from(2); - // let miner = U256::from(3); - // let nominator = U256::from(4); - // let netuid: u16 = 1; - // let root_id: u16 = 0; - // let root_tempo = 9; // neet root epoch to happen before subnet tempo - // let subnet_tempo = 10; - // let hotkey_tempo = 20; - // let stake = 100_000_000_000; - // let miner_stake = 1_000_000_000; - - // // Add network, register hotkeys, and setup network parameters - // add_network(root_id, root_tempo, 0); - // add_network(netuid, subnet_tempo, 0); - // register_ok_neuron(netuid, validator, coldkey, 0); - // register_ok_neuron(netuid, miner, coldkey, 1); - // SubtensorModule::add_balance_to_coldkey_account( - // &coldkey, - // 2 * stake + ExistentialDeposit::get(), - // ); - // SubtensorModule::add_balance_to_coldkey_account( - // &nominator, - // stake + ExistentialDeposit::get(), - // ); - // SubtensorModule::set_hotkey_emission_tempo(hotkey_tempo); - // SubtensorModule::set_weights_set_rate_limit(netuid, 0); - // step_block(subnet_tempo); - // crate::SubnetOwnerCut::::set(0); - // // All stake is active - // crate::ActivityCutoff::::set(netuid, u16::MAX); - // // There's only one validator - // crate::MaxAllowedUids::::set(netuid, 2); - // SubtensorModule::set_max_allowed_validators(netuid, 1); - - // // Set zero hotkey take for validator - // SubtensorModule::set_min_delegate_take(0); - // assert_ok!(SubtensorModule::do_become_delegate( - // RuntimeOrigin::signed(coldkey), - // validator, - // 0 - // )); + add_network(netuid, 0, 0); + register_ok_neuron(netuid, delegate_hotkey, delegate_coldkey, 0); - // // Set zero hotkey take for miner - // assert_ok!(SubtensorModule::do_become_delegate( - // RuntimeOrigin::signed(coldkey), - // miner, - // 0 - // )); + // Make the account a delegate + assert_ok!(SubtensorModule::become_delegate( + RuntimeOrigin::signed(delegate_coldkey), + delegate_hotkey + )); - // // Setup stakes: - // // Stake from validator - // // Stake from miner - // // Stake from nominator to miner - // // Give 100% of parent stake to childkey - // assert_ok!(SubtensorModule::add_stake( - // RuntimeOrigin::signed(coldkey), - // validator, - // stake - // )); - // assert_ok!(SubtensorModule::add_stake( - // RuntimeOrigin::signed(coldkey), - // miner, - // miner_stake - // )); - // assert_ok!(SubtensorModule::add_stake( - // RuntimeOrigin::signed(nominator), - // miner, - // stake - // )); - // // Make all stakes viable - // crate::StakeDeltaSinceLastEmissionDrain::::set(validator, coldkey, -1); - // crate::StakeDeltaSinceLastEmissionDrain::::set(miner, nominator, -1); - - // // Setup YUMA so that it creates emissions: - // // Validator sets weight for miner - // // Validator registers on root and - // // Sets root weights - // // Last weight update is after block at registration - // crate::Weights::::insert(netuid, 0, vec![(1, 0xFFFF)]); - // assert_ok!(SubtensorModule::do_root_register( - // RuntimeOrigin::signed(coldkey), - // validator, - // )); - // crate::Weights::::insert(root_id, 0, vec![(0, 0xFFFF), (1, 0xFFFF)]); - // crate::BlockAtRegistration::::set(netuid, 0, 1); - // crate::LastUpdate::::set(netuid, vec![2, 2]); - // crate::Kappa::::set(netuid, u16::MAX / 5); - - // // Run run_coinbase until root epoch is run - // while crate::PendingEmission::::get(netuid) == 0 { - // step_block(1); - // } - - // // Prevent further root epochs - // crate::Tempo::::set(root_id, u16::MAX); - - // // Run run_coinbase until PendingHotkeyEmission are populated - // while crate::PendingdHotkeyEmission::::get(miner) == 0 { - // step_block(1); - // } - - // // Prevent further subnet epochs - // crate::Tempo::::set(netuid, u16::MAX); - - // // Run run_coinbase until PendingHotkeyEmission is drained for both validator and miner - // step_block((hotkey_tempo * 2) as u16); - - // // Verify how emission is split between keys - // // - Validator stake increased by 50% of total emission - // // - Miner stake increased by 50% of total emission - // // - Nominator gets nothing because he staked to miner - // let miner_emission = crate::Stake::::get(miner, coldkey) - miner_stake; - // let validator_emission = crate::Stake::::get(validator, coldkey) - stake; - // let nominator_emission = crate::Stake::::get(miner, nominator) - stake; - // let total_emission = validator_emission + miner_emission + nominator_emission; - - // assert_eq!(validator_emission, total_emission / 2); - // assert_eq!(miner_emission, total_emission / 2); - // assert_eq!(nominator_emission, 0); - }); -} + // Add owner stake + SubtensorModule::add_balance_to_coldkey_account(&delegate_coldkey, owner_stake); + assert_ok!(SubtensorModule::add_stake( + RuntimeOrigin::signed(delegate_coldkey), + delegate_hotkey, + netuid, + owner_stake + )); -/// Test that drain_hotkey_emission sends mining emission fully to the miner, even -/// if miner is a delegate and someone is delegating, and miner gets some validation emissions -#[test] -fn test_mining_emission_drain_with_validation() { - new_test_ext(1).execute_with(|| { - assert!(false); + // Add delegator stake + SubtensorModule::add_balance_to_coldkey_account(&delegator, delegator_stake); + assert_ok!(SubtensorModule::add_stake( + RuntimeOrigin::signed(delegator), + delegate_hotkey, + netuid, + delegator_stake + )); - // let coldkey = U256::from(1); - // let validator_miner1 = U256::from(2); - // let validator_miner2 = U256::from(3); - // let nominator = U256::from(4); - // let netuid: u16 = 1; - // let root_id: u16 = 0; - // let root_tempo = 9; // neet root epoch to happen before subnet tempo - // let subnet_tempo = 10; - // let hotkey_tempo = 20; - // let stake = 100_000_000_000; - // let half_stake = 50_000_000_000; - - // // Add network, register hotkeys, and setup network parameters - // add_network(root_id, root_tempo, 0); - // add_network(netuid, subnet_tempo, 0); - // register_ok_neuron(netuid, validator_miner1, coldkey, 0); - // register_ok_neuron(netuid, validator_miner2, coldkey, 1); - // SubtensorModule::add_balance_to_coldkey_account( - // &coldkey, - // 2 * stake + ExistentialDeposit::get(), - // ); - // SubtensorModule::add_balance_to_coldkey_account( - // &nominator, - // stake + ExistentialDeposit::get(), - // ); - // SubtensorModule::set_hotkey_emission_tempo(hotkey_tempo); - // SubtensorModule::set_weights_set_rate_limit(netuid, 0); - // step_block(subnet_tempo); - // crate::SubnetOwnerCut::::set(0); - // // All stake is active - // crate::ActivityCutoff::::set(netuid, u16::MAX); - // // There are two validators - // crate::MaxAllowedUids::::set(netuid, 2); - // SubtensorModule::set_max_allowed_validators(netuid, 2); - - // // Set zero hotkey take for validator - // SubtensorModule::set_min_delegate_take(0); - // assert_ok!(SubtensorModule::do_become_delegate( - // RuntimeOrigin::signed(coldkey), - // validator_miner1, - // 0 - // )); - - // // Set zero hotkey take for miner - // assert_ok!(SubtensorModule::do_become_delegate( - // RuntimeOrigin::signed(coldkey), - // validator_miner2, - // 0 - // )); - - // // Setup stakes: - // // Stake from validator - // // Stake from miner - // // Stake from nominator to miner - // assert_ok!(SubtensorModule::add_stake( - // RuntimeOrigin::signed(coldkey), - // validator_miner1, - // stake - // )); - // assert_ok!(SubtensorModule::add_stake( - // RuntimeOrigin::signed(coldkey), - // validator_miner2, - // half_stake - // )); - // assert_ok!(SubtensorModule::add_stake( - // RuntimeOrigin::signed(nominator), - // validator_miner2, - // half_stake - // )); - // // Make all stakes viable - // crate::StakeDeltaSinceLastEmissionDrain::::set(validator_miner1, coldkey, -1); - // crate::StakeDeltaSinceLastEmissionDrain::::set(validator_miner2, coldkey, -1); - // crate::StakeDeltaSinceLastEmissionDrain::::set(validator_miner2, nominator, -1); - - // // Setup YUMA so that it creates emissions: - // // Validators set weights for each other - // // Validator registers on root and - // // Sets root weights - // // Last weight update is after block at registration - // crate::Weights::::insert(netuid, 0, vec![(0, 0xFFFF), (1, 0xFFFF)]); - // crate::Weights::::insert(netuid, 1, vec![(0, 0xFFFF), (1, 0xFFFF)]); - // assert_ok!(SubtensorModule::do_root_register( - // RuntimeOrigin::signed(coldkey), - // validator_miner1, - // )); - // crate::Weights::::insert(root_id, 0, vec![(0, 0xFFFF), (1, 0xFFFF)]); - // crate::BlockAtRegistration::::set(netuid, 0, 1); - // crate::BlockAtRegistration::::set(netuid, 1, 1); - // crate::LastUpdate::::set(netuid, vec![2, 2]); - // crate::Kappa::::set(netuid, u16::MAX / 5); - - // // Run run_coinbase until root epoch is run - // while crate::PendingEmission::::get(netuid) == 0 { - // step_block(1); - // } - - // // Prevent further root epochs - // crate::Tempo::::set(root_id, u16::MAX); - - // // Run run_coinbase until PendingHotkeyEmission are populated - // while crate::PendingdHotkeyEmission::::get(validator_miner1) == 0 { - // step_block(1); - // } - - // // Prevent further subnet epochs - // crate::Tempo::::set(netuid, u16::MAX); - - // // Run run_coinbase until PendingHotkeyEmission is drained for both validator and miner - // step_block((hotkey_tempo * 2) as u16); - - // // Verify how emission is split between keys - // // - 50% goes to miners and 50% goes to validators - // // - Miner's reward is treated as half miner and half validator - // // - Neuron 1 stake is increased by 50% of total emission - // // - Neuron 2 stake is increased by 37.5% of total emission (mining portion is intact, validation portion is split 50%) - // // - Nominator stake is increased by 12.5% of total emission (validation portion is distributed in 50% proportion) - // let validator_miner_emission1 = - // crate::Stake::::get(validator_miner1, coldkey) - stake; - // let validator_miner_emission2 = - // crate::Stake::::get(validator_miner2, coldkey) - half_stake; - // let nominator_emission = - // crate::Stake::::get(validator_miner2, nominator) - half_stake; - // let total_emission = - // validator_miner_emission1 + validator_miner_emission2 + nominator_emission; - - // assert_eq!(validator_miner_emission1, total_emission / 2); - // assert_eq!(validator_miner_emission2, total_emission / 1000 * 375); - // assert_eq!(nominator_emission, total_emission / 1000 * 125); - }); -} + // Debug prints + println!("Owner stake: {}", owner_stake); + println!("Delegator stake: {}", delegator_stake); + println!("Existential deposit: {}", existential_deposit); + println!( + "Total stake for hotkey: {}", + SubtensorModule::get_total_stake_for_hotkey(&delegate_hotkey) + ); + println!( + "Delegated stake for coldkey: {}", + SubtensorModule::get_total_stake_for_coldkey(&delegate_coldkey) + ); -/// Test that drain_hotkey_emission sends mining emission fully to the miners, for the -/// case of one validator, one vali-miner, and one miner -#[test] -fn test_mining_emission_drain_validator_valiminer_miner() { - new_test_ext(1).execute_with(|| { - assert!(false); + // Check the total delegated stake (should exclude owner's stake) + let expected_delegated_stake = delegator_stake - existential_deposit; + let actual_delegated_stake = + SubtensorModule::get_total_stake_for_coldkey(&delegate_coldkey); - // let coldkey = U256::from(1); - // let validator = U256::from(2); - // let validator_miner = U256::from(3); - // let miner = U256::from(4); - // let netuid: u16 = 1; - // let root_id: u16 = 0; - // let root_tempo = 9; // neet root epoch to happen before subnet tempo - // let subnet_tempo = 10; - // let hotkey_tempo = 20; - // let stake = 100_000_000_000; - - // // Add network, register hotkeys, and setup network parameters - // add_network(root_id, root_tempo, 0); - // add_network(netuid, subnet_tempo, 0); - // register_ok_neuron(netuid, validator, coldkey, 0); - // register_ok_neuron(netuid, validator_miner, coldkey, 1); - // register_ok_neuron(netuid, miner, coldkey, 2); - // SubtensorModule::add_balance_to_coldkey_account( - // &coldkey, - // 3 * stake + ExistentialDeposit::get(), - // ); - // SubtensorModule::set_hotkey_emission_tempo(hotkey_tempo); - // SubtensorModule::set_weights_set_rate_limit(netuid, 0); - // step_block(subnet_tempo); - // crate::SubnetOwnerCut::::set(0); - // // All stake is active - // crate::ActivityCutoff::::set(netuid, u16::MAX); - // // There are two validators and three neurons - // crate::MaxAllowedUids::::set(netuid, 3); - // SubtensorModule::set_max_allowed_validators(netuid, 2); - - // // Setup stakes: - // // Stake from validator - // // Stake from valiminer - // assert_ok!(SubtensorModule::add_stake( - // RuntimeOrigin::signed(coldkey), - // validator, - // stake - // )); - // assert_ok!(SubtensorModule::add_stake( - // RuntimeOrigin::signed(coldkey), - // validator_miner, - // stake - // )); - // // Make all stakes viable - // crate::StakeDeltaSinceLastEmissionDrain::::set(validator, coldkey, -1); - // crate::StakeDeltaSinceLastEmissionDrain::::set(validator_miner, coldkey, -1); - - // // Setup YUMA so that it creates emissions: - // // Validator 1 sets weight for valiminer |- to achieve equal incentive for both miners - // // Valiminer sets weights for the second miner | - // // Validator registers on root and - // // Sets root weights - // // Last weight update is after block at registration - // crate::Weights::::insert(netuid, 0, vec![(1, 0xFFFF)]); - // crate::Weights::::insert(netuid, 1, vec![(2, 0xFFFF)]); - // assert_ok!(SubtensorModule::do_root_register( - // RuntimeOrigin::signed(coldkey), - // validator, - // )); - // crate::Weights::::insert(root_id, 0, vec![(0, 0xFFFF), (1, 0xFFFF)]); - // crate::BlockAtRegistration::::set(netuid, 0, 1); - // crate::BlockAtRegistration::::set(netuid, 1, 1); - // crate::LastUpdate::::set(netuid, vec![2, 2, 2]); - // crate::Kappa::::set(netuid, u16::MAX / 5); - - // // Run run_coinbase until root epoch is run - // while crate::PendingEmission::::get(netuid) == 0 { - // step_block(1); - // } - - // // Prevent further root epochs - // crate::Tempo::::set(root_id, u16::MAX); - - // // Run run_coinbase until PendingHotkeyEmission are populated - // while crate::PendingdHotkeyEmission::::get(validator) == 0 { - // step_block(1); - // } - - // // Prevent further subnet epochs - // crate::Tempo::::set(netuid, u16::MAX); - - // // Run run_coinbase until PendingHotkeyEmission is drained for both validator and miner - // step_block((hotkey_tempo * 2) as u16); - - // // Verify how emission is split between keys - // // - 50% goes to miners and 50% goes to validators - // // - Validator gets 25% because there are two validators - // // - Valiminer gets 25% as a validator and 25% as miner - // // - Miner gets 25% as miner - // let validator_emission = crate::Stake::::get(validator, coldkey) - stake; - // let valiminer_emission = crate::Stake::::get(validator_miner, coldkey) - stake; - // let miner_emission = crate::Stake::::get(miner, coldkey); - // let total_emission = validator_emission + valiminer_emission + miner_emission; - - // assert_eq!(validator_emission, total_emission / 4); - // assert_eq!(valiminer_emission, total_emission / 2); - // assert_eq!(miner_emission, total_emission / 4); + assert_abs_diff_eq!( + actual_delegated_stake, + expected_delegated_stake, + epsilon = 1, + ); }); }