Skip to content

Commit

Permalink
tests: add a couple of yuma tests
Browse files Browse the repository at this point in the history
  • Loading branch information
saiintbrisson committed Apr 3, 2024
1 parent a1480b0 commit 46f69e3
Show file tree
Hide file tree
Showing 5 changed files with 185 additions and 8 deletions.
3 changes: 3 additions & 0 deletions pallets/subspace/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,9 @@ pub use autogen_weights::WeightInfo;
#[cfg(test)]
mod mock;

#[cfg(debug_assertions)]
pub use step::yuma;

// =========================
// ==== Pallet Imports =====
// =========================
Expand Down
2 changes: 1 addition & 1 deletion pallets/subspace/src/step.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ use sp_arithmetic::per_things::Percent;
use sp_std::vec;
use substrate_fixed::types::{I110F18, I32F32, I64F64};

mod yuma;
pub mod yuma;

impl<T: Config> Pallet<T> {
pub fn block_step() {
Expand Down
16 changes: 11 additions & 5 deletions pallets/subspace/src/step/yuma.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ use crate::{
};
use frame_support::dispatch::Vec;

type EmissionMap<T> = BTreeMap<ModuleKey<T>, BTreeMap<AccountKey<T>, u64>>;
pub type EmissionMap<T> = BTreeMap<ModuleKey<T>, BTreeMap<AccountKey<T>, u64>>;

pub struct YumaCalc<T: Config> {
/// The amount of modules on the subnet
Expand Down Expand Up @@ -516,11 +516,11 @@ bty::brand! {
pub type WeightsVal = Vec<Vec<(u16, I32F32)>>;
}

#[derive(Clone, Debug)]
pub struct ModuleKey<T: Config>(T::AccountId);
#[derive(Clone)]
pub struct ModuleKey<T: Config>(pub T::AccountId);

#[derive(Clone, Debug)]
pub struct AccountKey<T: Config>(T::AccountId);
#[derive(Clone)]
pub struct AccountKey<T: Config>(pub T::AccountId);

macro_rules! impl_things {
($ty:ident) => {
Expand All @@ -543,6 +543,12 @@ macro_rules! impl_things {
self.0.cmp(&other.0)
}
}

impl<T: Config> core::fmt::Debug for $ty<T> {
fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
f.write_fmt(format_args!("{}({:?})", stringify!($ty), self.0))
}
}
};
}

Expand Down
4 changes: 2 additions & 2 deletions pallets/subspace/tests/mock.rs
Original file line number Diff line number Diff line change
Expand Up @@ -289,11 +289,11 @@ pub fn add_stake_and_balance(netuid: u16, key: U256, amount: u64) {
assert_ok!(result);
}

pub fn to_nano(x: u64) -> u64 {
pub const fn to_nano(x: u64) -> u64 {
x * 10u64.pow(TOKEN_DECIMALS)
}

pub fn from_nano(x: u64) -> u64 {
pub const fn from_nano(x: u64) -> u64 {
x / 10u64.pow(TOKEN_DECIMALS)
}

Expand Down
168 changes: 168 additions & 0 deletions pallets/subspace/tests/step_yuma.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,168 @@
use crate::mock::*;
use frame_support::assert_ok;
use pallet_subspace::yuma::{AccountKey, EmissionMap, ModuleKey, YumaCalc};
use sp_core::U256;
use std::collections::BTreeMap;
mod mock;

mod utils {
use pallet_subspace::{Consensus, Dividends, Emission, Incentive, Rank, Trust};

use crate::Test;

pub fn get_rank_for_uid(netuid: u16, uid: u16) -> u16 {
Rank::<Test>::get(netuid).get(uid as usize).copied().unwrap_or_default()
}

pub fn get_trust_for_uid(netuid: u16, uid: u16) -> u16 {
Trust::<Test>::get(netuid).get(uid as usize).copied().unwrap_or_default()
}

pub fn get_consensus_for_uid(netuid: u16, uid: u16) -> u16 {
Consensus::<Test>::get(netuid).get(uid as usize).copied().unwrap_or_default()
}

pub fn get_incentive_for_uid(netuid: u16, uid: u16) -> u16 {
Incentive::<Test>::get(netuid).get(uid as usize).copied().unwrap_or_default()
}

pub fn get_dividends_for_uid(netuid: u16, uid: u16) -> u16 {
Dividends::<Test>::get(netuid).get(uid as usize).copied().unwrap_or_default()
}

pub fn get_emission_for_uid(netuid: u16, uid: u16) -> u64 {
Emission::<Test>::get(netuid).get(uid as usize).copied().unwrap_or_default()
}
}

const ONE: u64 = to_nano(1);

#[test]
fn test_1_graph() {
new_test_ext().execute_with(|| {
SubspaceModule::set_unit_emission(23148148148);
SubspaceModule::set_min_burn(0);

// Register general subnet
assert_ok!(register_module(0, 10.into(), 0));

log::info!("test_1_graph:");
let netuid: u16 = 1;
let key = U256::from(0);
let uid: u16 = 0;
let stake_amount: u64 = to_nano(100);

SubspaceModule::set_max_allowed_uids(netuid, 1);
assert_ok!(register_module(netuid, key, stake_amount));
assert_ok!(register_module(netuid, key + 1, 1));
assert_eq!(SubspaceModule::get_subnet_n(netuid), 2);

run_to_block(1); // run to next block to ensure weights are set on nodes after their registration block

assert_ok!(SubspaceModule::set_weights(
RuntimeOrigin::signed(U256::from(0)),
netuid,
vec![uid as u16],
vec![u16::MAX],
));

let emissions = YumaCalc::<Test>::new(netuid, ONE).run();

assert_eq!(
emissions,
[(ModuleKey(key), [(AccountKey(key), ONE)].into())].into()
);

let new_stake_amount = stake_amount + ONE;

assert_eq!(
SubspaceModule::get_total_stake_to(netuid, &key),
new_stake_amount
);
assert_eq!(utils::get_rank_for_uid(netuid, uid), 0);
assert_eq!(utils::get_trust_for_uid(netuid, uid), 0);
assert_eq!(utils::get_consensus_for_uid(netuid, uid), 0);
assert_eq!(utils::get_incentive_for_uid(netuid, uid), 0);
assert_eq!(utils::get_dividends_for_uid(netuid, uid), 0);
assert_eq!(utils::get_emission_for_uid(netuid, uid), ONE);
});
}

#[test]
fn test_10_graph() {
/// Function for adding a nodes to the graph.
fn add_node(netuid: u16, key: U256, uid: u16, stake_amount: u64) {
log::info!(
"+Add net:{:?} hotkey:{:?} uid:{:?} stake_amount: {:?} subn: {:?}",
netuid,
key,
uid,
stake_amount,
SubspaceModule::get_subnet_n(netuid),
);

assert_ok!(register_module(netuid, key, stake_amount));
assert_eq!(SubspaceModule::get_subnet_n(netuid) - 1, uid);
}

new_test_ext().execute_with(|| {
SubspaceModule::set_unit_emission(23148148148);
SubspaceModule::set_min_burn(0);

// Register general subnet
assert_ok!(register_module(0, 10_000.into(), 0));

log::info!("test_10_graph");

// Build the graph with 10 items
// each with 1 stake and self weights.
let n: usize = 10;
let netuid: u16 = 1;
let stake_amount_per_node = ONE;
SubspaceModule::set_max_allowed_uids(netuid, n as u16 + 1);

for i in 0..n {
add_node(netuid, U256::from(i), i as u16, stake_amount_per_node)
}

assert_ok!(register_module(netuid, U256::from(n + 1), 1));
assert_eq!(SubspaceModule::get_subnet_n(netuid), 11);

run_to_block(1); // run to next block to ensure weights are set on nodes after their registration block

for i in 0..n {
assert_ok!(SubspaceModule::set_weights(
RuntimeOrigin::signed(U256::from(n + 1)),
netuid,
vec![i as u16],
vec![u16::MAX],
));
}

let emissions = YumaCalc::<Test>::new(netuid, ONE).run();
let mut expected: EmissionMap<Test> = BTreeMap::new();

// Check return values.
let emission_per_node = ONE / n as u64;
for i in 0..n as u16 {
assert_eq!(
from_nano(SubspaceModule::get_total_stake_to(netuid, &(U256::from(i)))),
from_nano(to_nano(1) + emission_per_node)
);

assert_eq!(utils::get_rank_for_uid(netuid, i), 0);
assert_eq!(utils::get_trust_for_uid(netuid, i), 0);
assert_eq!(utils::get_consensus_for_uid(netuid, i), 0);
assert_eq!(utils::get_incentive_for_uid(netuid, i), 0);
assert_eq!(utils::get_dividends_for_uid(netuid, i), 0);
assert_eq!(utils::get_emission_for_uid(netuid, i), 99999999);

expected
.entry(ModuleKey(i.into()))
.or_default()
.insert(AccountKey(i.into()), 99999999);
}

assert_eq!(emissions, expected);
});
}

0 comments on commit 46f69e3

Please sign in to comment.