Skip to content

Commit

Permalink
Fix test_add_stake_dispatch_info_ok, make test_add_stake_ok_no_emissi…
Browse files Browse the repository at this point in the history
…on build
  • Loading branch information
gztensor committed Jan 7, 2025
1 parent 18b78fb commit a41ff21
Show file tree
Hide file tree
Showing 4 changed files with 79 additions and 62 deletions.
8 changes: 4 additions & 4 deletions pallets/subtensor/src/subnets/subnet.rs
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,7 @@ impl<T: Config> Pallet<T> {
/// # Returns
/// * `u16` - The next available mechanism ID.
pub fn get_next_netuid() -> u16 {
let mut next_netuid = 0;
let mut next_netuid = 1; // do not allow creation of root
let netuids: Vec<u16> = Self::get_all_subnet_netuids();
loop {
if !netuids.contains(&next_netuid) {
Expand Down Expand Up @@ -205,7 +205,7 @@ impl<T: Config> Pallet<T> {
// --- 8. Set initial and custom parameters for the network.
let default_tempo = DefaultTempo::<T>::get();
Self::init_new_network(netuid_to_register, default_tempo);
log::debug!("init_new_network: {:?}", netuid_to_register);
println!("init_new_network: {:?}", netuid_to_register);

// --- 9 . Add the caller to the neuron set.
Self::create_account_if_non_existent(&coldkey, hotkey);
Expand All @@ -230,8 +230,8 @@ impl<T: Config> Pallet<T> {

// --- 14. Init the pool by putting the lock as the initial alpha.
TokenSymbol::<T>::insert(netuid_to_register, Self::get_symbol_for_subnet(netuid_to_register) ); // Set subnet token symbol.
SubnetTAO::<T>::insert(netuid_to_register, 1); // add the infintesimal amount of TAO to the pool.
SubnetAlphaIn::<T>::insert(netuid_to_register, 1); // add infintesimal amount of alpha to the pool.
SubnetTAO::<T>::insert(netuid_to_register, 1_000_000); // add the infintesimal amount of TAO to the pool.
SubnetAlphaIn::<T>::insert(netuid_to_register, 1_000_000); // add infintesimal amount of alpha to the pool.
SubnetOwner::<T>::insert(netuid_to_register, coldkey.clone());
SubnetOwnerHotkey::<T>::insert(netuid_to_register, hotkey.clone());

Expand Down
3 changes: 0 additions & 3 deletions pallets/subtensor/src/tests/coinbase.rs
Original file line number Diff line number Diff line change
Expand Up @@ -29,10 +29,7 @@ fn test_dynamic_function_various_values() {
SubnetMechanism::<Test>::insert(1, 1);
SubnetTAO::<Test>::insert(1, (price * 1_000_000_000.0) as u64);
SubnetAlphaIn::<Test>::insert(1, 1_000_000_000);

println!("price: {}, tao_in: {}, alpha_emission: {}", price, tao_in, alpha_emission);
let (tao_in_emission, alpha_in_emission, alpha_out_emission) = SubtensorModule::get_dynamic_tao_emission( 1, tao_in, alpha_emission);
println!("tao_in_emission: {}, alpha_in_emission: {}, alpha_out_emission: {}", tao_in_emission, alpha_in_emission, alpha_out_emission);
assert!(tao_in_emission <= tao_in as u64, "tao_in_emission is greater than tao_in");
assert!(alpha_in_emission <= alpha_emission as u64, "alpha_in_emission is greater than alpha_emission");
assert!(alpha_out_emission <= 2 * alpha_emission as u64, "alpha_out_emission is greater than 2 * alpha_emission");
Expand Down
26 changes: 26 additions & 0 deletions pallets/subtensor/src/tests/mock.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@ use sp_runtime::{
};
use sp_std::cmp::Ordering;

use crate::*;

type Block = frame_system::mocking::MockBlock<Test>;

// Configure a mock runtime to test the pallet.
Expand Down Expand Up @@ -660,6 +662,30 @@ pub fn add_network(netuid: u16, tempo: u16, _modality: u16) {
SubtensorModule::set_network_pow_registration_allowed(netuid, true);
}

#[allow(dead_code)]
pub fn add_dynamic_network(netuid: u16, tempo: u16, hotkey: &U256, coldkey: &U256) -> u16 {
// SubtensorModule::init_new_network(netuid, tempo);
// SubtensorModule::set_network_registration_allowed(netuid, true);
// SubtensorModule::set_network_pow_registration_allowed(netuid, true);
// SubnetMechanism::<Test>::insert(netuid, 1);
// TokenSymbol::<Test>::insert(netuid, SubtensorModule::get_symbol_for_subnet(netuid) ); // Set subnet token symbol.
// SubnetTAO::<Test>::insert(netuid, 1_000_000_000); // add the infintesimal amount of TAO to the pool.
// SubnetAlphaIn::<Test>::insert(netuid, 1_000_000_000); // add infintesimal amount of alpha to the pool.
// SubnetOwner::<Test>::insert(netuid, coldkey.clone());
// SubnetOwnerHotkey::<Test>::insert(netuid, hotkey.clone());

let netuid = SubtensorModule::get_next_netuid();
let lock_cost = SubtensorModule::get_network_lock_cost();
SubtensorModule::add_balance_to_coldkey_account(coldkey, lock_cost);

assert_ok!(
SubtensorModule::register_network(
RawOrigin::Signed(coldkey.clone()).into(),
hotkey.clone()
));
netuid
}

// Helper function to set up a neuron with stake
#[allow(dead_code)]
pub fn setup_neuron_with_stake(netuid: u16, hotkey: U256, coldkey: U256, stake: u64) {
Expand Down
104 changes: 49 additions & 55 deletions pallets/subtensor/src/tests/staking.rs
Original file line number Diff line number Diff line change
@@ -1,14 +1,13 @@
#![allow(clippy::unwrap_used)]
#![allow(clippy::arithmetic_side_effects)]

// use frame_support::{assert_err, assert_noop, assert_ok, traits::Currency};
use frame_support::{assert_err, assert_ok};
use frame_support::{assert_err, assert_noop, assert_ok, traits::Currency};
use frame_system::Config;

use super::mock::*;
use crate::*;
// use frame_support::dispatch::{DispatchClass, DispatchInfo, GetDispatchInfo, Pays};
// use frame_support::sp_runtime::DispatchError;
use frame_support::dispatch::{DispatchClass, DispatchInfo, GetDispatchInfo, Pays};
use frame_support::sp_runtime::DispatchError;
use sp_core::{H256, U256};

/***********************************************************
Expand All @@ -18,71 +17,66 @@ use sp_core::{H256, U256};
#[test]
fn test_add_stake_dispatch_info_ok() {
new_test_ext(1).execute_with(|| {
assert!(false);

// let hotkey = U256::from(0);
// let amount_staked = 5000;
// let call = RuntimeCall::SubtensorModule(SubtensorCall::add_stake {
// hotkey,
// amount_staked,
// });
// assert_eq!(
// call.get_dispatch_info(),
// DispatchInfo {
// weight: frame_support::weights::Weight::from_parts(1_074_000_000, 0),
// class: DispatchClass::Normal,
// pays_fee: Pays::No
// }
// );
let hotkey = U256::from(0);
let amount_staked = 5000;
let netuid = 1;
let call = RuntimeCall::SubtensorModule(SubtensorCall::add_stake {
hotkey,
netuid,
amount_staked,
});
assert_eq!(
call.get_dispatch_info(),
DispatchInfo {
weight: frame_support::weights::Weight::from_parts(1_074_000_000, 0),
class: DispatchClass::Normal,
pays_fee: Pays::No
}
);
});
}
#[test]
fn test_add_stake_ok_no_emission() {
new_test_ext(1).execute_with(|| {
assert!(false);

// let hotkey_account_id = U256::from(533453);
// let coldkey_account_id = U256::from(55453);
// let netuid: u16 = 1;
// let tempo: u16 = 13;
// let start_nonce: u64 = 0;
let hotkey_account_id = U256::from(533453);
let coldkey_account_id = U256::from(55453);

// //add network
// add_network(netuid, tempo, 0);
//add network
let netuid: u16 = add_dynamic_network(0, 0, &hotkey_account_id, &coldkey_account_id);

// // Register neuron
// register_ok_neuron(netuid, hotkey_account_id, coldkey_account_id, start_nonce);
println!("Created subnet {:?}", netuid);

// // Give it some $$$ in his coldkey balance
// SubtensorModule::add_balance_to_coldkey_account(&coldkey_account_id, 10000);
// Give it some $$$ in his coldkey balance
SubtensorModule::add_balance_to_coldkey_account(&coldkey_account_id, 10000);

// // Check we have zero staked before transfer
// assert_eq!(
// SubtensorModule::get_total_stake_for_hotkey(&hotkey_account_id),
// 0
// );
// Check we have zero staked before transfer
assert_eq!(
SubtensorModule::get_total_stake_for_hotkey(&hotkey_account_id),
0
);

// // Also total stake should be zero
// assert_eq!(SubtensorModule::get_total_stake(), 0);
// Also total stake should be zero
assert_eq!(SubtensorModule::get_total_stake(), 0);

// // Transfer to hotkey account, and check if the result is ok
// assert_ok!(SubtensorModule::add_stake(
// <<Test as Config>::RuntimeOrigin>::signed(coldkey_account_id),
// hotkey_account_id,
// 10000
// ));
// Transfer to hotkey account, and check if the result is ok
assert_ok!(SubtensorModule::add_stake(
<<Test as Config>::RuntimeOrigin>::signed(coldkey_account_id),
hotkey_account_id,
netuid,
10000
));

// // Check if stake has increased
// assert_eq!(
// SubtensorModule::get_total_stake_for_hotkey(&hotkey_account_id),
// 9999
// );
// Check if stake has increased
assert_eq!(
SubtensorModule::get_total_stake_for_hotkey(&hotkey_account_id),
9999
);

// // Check if balance has decreased
// assert_eq!(SubtensorModule::get_coldkey_balance(&coldkey_account_id), 1);
// Check if balance has decreased
assert_eq!(SubtensorModule::get_coldkey_balance(&coldkey_account_id), 1);

// // Check if total stake has increased accordingly.
// assert_eq!(SubtensorModule::get_total_stake(), 9999);
// Check if total stake has increased accordingly.
assert_eq!(SubtensorModule::get_total_stake(), 9999);
});
}

Expand Down

0 comments on commit a41ff21

Please sign in to comment.