Skip to content

Commit

Permalink
resolved tests for tempo max
Browse files Browse the repository at this point in the history
  • Loading branch information
Your Name authored and steinerkelvin committed Feb 13, 2024
1 parent 0f4f4bf commit fb78f6e
Show file tree
Hide file tree
Showing 6 changed files with 30 additions and 42 deletions.
9 changes: 2 additions & 7 deletions pallets/subspace/src/registration.rs
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@ impl<T: Config> Pallet<T> {

// adding the stake amount
Self::do_add_stake(origin.clone(), netuid, module_key.clone(), stake_amount)?;

// CONSTANT INITIAL BURN
if min_burn > 0 {
ensure!(stake_amount >= min_burn, Error::<T>::NotEnoughStakeToRegister);
Expand Down Expand Up @@ -229,8 +229,6 @@ impl<T: Config> Pallet<T> {
// use default parameters
//



let num_subnets: u16 = Self::num_subnets();
let max_subnets: u16 = Self::getglobal_max_allowed_subnets();
// if we have not reached the max number of subnets, then we can start a new one
Expand All @@ -253,10 +251,7 @@ impl<T: Config> Pallet<T> {
params.name = name.clone();
let netuid = Self::add_subnet(params);
Founder::<T>::insert(netuid, founder_key.clone());





Ok(())
}

Expand Down
2 changes: 0 additions & 2 deletions pallets/subspace/src/staking.rs
Original file line number Diff line number Diff line change
Expand Up @@ -125,8 +125,6 @@ impl<T: Config> Pallet<T> {
log::info!("StakeAdded( key:{:?}, stake_to_be_added:{:?} )", key, amount);
Self::deposit_event(Event::StakeAdded(key, module_key, amount));



// --- 6. Ok and return.
Ok(())
}
Expand Down
2 changes: 0 additions & 2 deletions pallets/subspace/src/step.rs
Original file line number Diff line number Diff line change
Expand Up @@ -170,8 +170,6 @@ impl<T: Config> Pallet<T> {
incentive[*uid_i as usize] = I32F32::from_num(1.0);
}
}


inplace_normalize(&mut incentive); // range: I32F32(0, 1)

// =================================
Expand Down
12 changes: 3 additions & 9 deletions pallets/subspace/src/subnet.rs
Original file line number Diff line number Diff line change
Expand Up @@ -645,14 +645,11 @@ impl<T: Config> Pallet<T> {
// ==== Global Setters ====
// ========================
pub fn set_tempo(netuid: u16, tempo: u16) {
Tempo::<T>::insert(netuid, tempo);
Tempo::<T>::insert(netuid, tempo.max(100));
}

pub fn set_founder_share(netuid: u16, mut founder_share: u16) {
if founder_share > 100 {
founder_share = 100;
}
FounderShare::<T>::insert(netuid, founder_share);
FounderShare::<T>::insert(netuid, founder_share.min(100));
}
pub fn get_founder_share(netuid: u16) -> u16 {
return FounderShare::<T>::get(netuid)
Expand All @@ -667,10 +664,7 @@ impl<T: Config> Pallet<T> {
}

pub fn set_incentive_ratio(netuid: u16, mut incentive_ratio: u16) {
if incentive_ratio > 100 {
incentive_ratio = 100;
}
IncentiveRatio::<T>::insert(netuid, incentive_ratio);
IncentiveRatio::<T>::insert(netuid, incentive_ratio.min(100));
}

pub fn get_founder(netuid: u16) -> T::AccountId {
Expand Down
2 changes: 1 addition & 1 deletion pallets/subspace/tests/test_burn.rs
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ fn test_burn() {
incentives,
emissions);

step_epoch(1);
step_epoch(netuid);


let dividends = SubspaceModule::get_dividends(netuid);
Expand Down
45 changes: 24 additions & 21 deletions pallets/subspace/tests/test_step.rs
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,7 @@ fn test_dividends_same_stake() {


let stakes_before : Vec<u64> = SubspaceModule::get_stakes(netuid);
step_block(1);
step_epoch(netuid);
let incentives: Vec<u16> = SubspaceModule::get_incentives(netuid);
let dividends: Vec<u16> = SubspaceModule::get_dividends(netuid);
let emissions: Vec<u64> = SubspaceModule::get_emissions(netuid);
Expand Down Expand Up @@ -147,7 +147,7 @@ fn test_dividends_diff_stake() {
let n_list: Vec<u16> = vec![10, 50, 100, 1000];
let blocks_per_epoch_list: u64 = 1;
let stake_per_module: u64 = 10_000;
let tempo : u16 = 10;
let tempo : u16 = 100;

// SETUP NETWORK
for i in 0..n {
Expand Down Expand Up @@ -238,7 +238,7 @@ fn test_pruning() {
let n: u16 = 100;
let blocks_per_epoch_list: u64 = 1;
let stake_per_module: u64 = 10_000;
let tempo: u16 = 1;
let tempo: u16 = 100;

// SETUP NETWORK
register_n_modules(netuid, n, stake_per_module);
Expand Down Expand Up @@ -308,11 +308,12 @@ fn test_lowest_priority_mechanism() {
let n_list: Vec<u16> = vec![10, 50, 100, 1000];
let blocks_per_epoch_list: u64 = 1;
let stake_per_module: u64 = 10_000;
let tempo : u16 = 100;

// SETUP NETWORK
register_n_modules(netuid, n, stake_per_module);

SubspaceModule::set_tempo(netuid, 1);
SubspaceModule::set_tempo(netuid, tempo);
SubspaceModule::set_max_allowed_weights(netuid, n);
SubspaceModule::set_min_allowed_weights(netuid, 0);

Expand All @@ -334,7 +335,7 @@ fn test_lowest_priority_mechanism() {
weight_values[prune_uid as usize] = 0;
set_weights(netuid, keys[0], weight_uids.clone(), weight_values.clone());

step_block(1);
step_block(tempo);
let incentives: Vec<u16> = SubspaceModule::get_incentives(netuid);
let dividends: Vec<u16> = SubspaceModule::get_dividends(netuid);
let emissions: Vec<u64> = SubspaceModule::get_emissions(netuid);
Expand Down Expand Up @@ -497,7 +498,7 @@ fn test_incentives() {
let mut params = SubspaceModule::subnet_params(netuid);
params.min_allowed_weights = 0;
params.max_allowed_weights = n;
params.tempo = 1;
params.tempo = 100;


let keys = SubspaceModule::get_keys(netuid);
Expand All @@ -509,7 +510,7 @@ fn test_incentives() {
let weight_values: Vec<u16> = [1, 1].to_vec();

set_weights(netuid, keys[0], weight_uids.clone(), weight_values.clone());
step_block(1);
step_block(params.tempo);

let incentives: Vec<u16> = SubspaceModule::get_incentives(netuid);
let emissions: Vec<u64> = SubspaceModule::get_emissions(netuid);
Expand All @@ -526,13 +527,13 @@ fn test_incentives() {
set_weights(netuid, keys[0], weight_uids.clone(), weight_values.clone());
set_weights(netuid, keys[9], weight_uids.clone(), weight_values.clone());

step_block(1);
step_block(params.tempo);

let incentives: Vec<u16> = SubspaceModule::get_incentives(netuid);
let emissions: Vec<u64> = SubspaceModule::get_emissions(netuid);

// evaluate votees
let delta : u64 = 100;
let delta : u64 = 100 * params.tempo as u64;
assert!(incentives[1] > 0);

assert!(emissions[2] > 2 * emissions[1] - delta &&
Expand Down Expand Up @@ -561,7 +562,7 @@ fn test_trust() {
let mut params = SubspaceModule::subnet_params(netuid);
params.min_allowed_weights = 0;
params.max_allowed_weights = n;
params.tempo = 1;
params.tempo = 100;
params.trust_ratio = 100;

SubspaceModule::set_subnet_params(netuid, params.clone());
Expand All @@ -579,7 +580,7 @@ fn test_trust() {
let weight_uids: Vec<u16> = [1, 2].to_vec();
let weight_values: Vec<u16> = [1, 1].to_vec();
set_weights(netuid, keys[9], weight_uids.clone(), weight_values.clone());
step_block(1);
step_block(params.tempo);

let trust: Vec<u16> = SubspaceModule::get_trust(netuid);
let emission : Vec<u64> = SubspaceModule::get_emissions(netuid);
Expand Down Expand Up @@ -855,14 +856,16 @@ fn test_founder_share() {
let founder_share = SubspaceModule::get_founder_share(netuid);
let founder_ratio: f64 = founder_share as f64 / 100.0;

let subnet_params = SubspaceModule::subnet_params(netuid);


let founder_stake_before = SubspaceModule::get_stake_for_key(netuid, &founder_key);
println!("founder_stake_before: {:?}", founder_stake_before);
// vote to avoid key[0] as we want to see the key[0] burn
step_epoch(netuid);
let total_emission = SubspaceModule::get_subnet_emission(netuid);
let expected_founder_share = (total_emission as f64 * founder_ratio) as u64;
let expected_emission = total_emission;
let total_emission = SubspaceModule::get_subnet_emission(netuid) * subnet_params.tempo as u64;
let expected_emission = total_emission as u64;
let expected_founder_share = (expected_emission as f64 * founder_ratio) as u64;
let emissions = SubspaceModule::get_emissions(netuid);
let dividends = SubspaceModule::get_dividends(netuid);
let incentives = SubspaceModule::get_incentives(netuid);
Expand All @@ -884,23 +887,23 @@ fn test_founder_share() {


let calculated_founder_share = SubspaceModule::get_stake_for_key(netuid, &founder_key) - founder_stake_before - founder_emission;
let delta: u64 = 1000;
let delta: u64 = 100000;


println!("expected_emission: {:?}", expected_emission);
println!("total_emission: {:?}", total_emission);
assert!(expected_emission > calcualted_total_emission - delta );
assert!(expected_emission < calcualted_total_emission + delta );
assert!(expected_emission > calcualted_total_emission - delta , "expected_emission: {} != calcualted_total_emission: {}", expected_emission, calcualted_total_emission);
assert!(expected_emission < calcualted_total_emission + delta , "expected_emission: {} != calcualted_total_emission: {}", expected_emission, calcualted_total_emission);

println!("calculated_founder_share: {:?}", calculated_founder_share);
println!("expected_founder_share: {:?}", expected_founder_share);
assert!(expected_founder_share > calculated_founder_share - delta );
assert!(expected_founder_share < calculated_founder_share + delta );

assert!(expected_founder_share > calculated_founder_share - delta , "expected_founder_share: {} != calculated_founder_share: {}", expected_founder_share, calculated_founder_share);
assert!(expected_founder_share < calculated_founder_share + delta , "expected_founder_share: {} != calculated_founder_share: {}", expected_founder_share, calculated_founder_share);

});




}


Expand Down

0 comments on commit fb78f6e

Please sign in to comment.