Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions pallets/admin-utils/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,9 @@ use sp_runtime::{traits::Member, RuntimeAppPublic};

mod benchmarking;

#[cfg(test)]
mod tests;

#[deny(missing_docs)]
#[frame_support::pallet]
pub mod pallet {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ frame_support::construct_runtime!(
{
System: frame_system = 1,
Balances: pallet_balances = 2,
AdminUtils: pallet_admin_utils = 3,
AdminUtils: crate = 3,
SubtensorModule: pallet_subtensor::{Pallet, Call, Storage, Event<T>, Error<T>} = 4,
Scheduler: pallet_scheduler::{Pallet, Call, Storage, Event<T>} = 5,
}
Expand Down Expand Up @@ -243,7 +243,7 @@ impl PrivilegeCmp<OriginCaller> for OriginPrivilegeCmp {
}
}

impl pallet_admin_utils::Config for Test {
impl crate::Config for Test {
type RuntimeEvent = RuntimeEvent;
type AuthorityId = AuraId;
type MaxAuthorities = ConstU32<32>;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,15 @@ use frame_support::{
dispatch::{DispatchClass, GetDispatchInfo, Pays},
};
use frame_system::Config;
use pallet_admin_utils::Error;
use pallet_subtensor::Error as SubtensorError;
use pallet_subtensor::{migrations, Event};
use sp_core::U256;

mod mock;
use crate::Error;
use mock::*;

mod mock;

#[test]
fn test_sudo_set_default_take() {
new_test_ext().execute_with(|| {
Expand Down Expand Up @@ -1183,7 +1184,7 @@ fn test_set_alpha_values_dispatch_info_ok() {
let netuid: u16 = 1;
let alpha_low: u16 = 12_u16;
let alpha_high: u16 = u16::MAX - 10;
let call = RuntimeCall::AdminUtils(pallet_admin_utils::Call::sudo_set_alpha_values {
let call = RuntimeCall::AdminUtils(crate::Call::sudo_set_alpha_values {
netuid,
alpha_low,
alpha_high,
Expand Down
3 changes: 3 additions & 0 deletions pallets/subtensor/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,9 @@ pub mod utils;
use crate::utils::rate_limiting::TransactionType;
use macros::{config, dispatches, errors, events, genesis, hooks};

#[cfg(test)]
mod tests;

// apparently this is stabilized since rust 1.36
extern crate alloc;

Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
use frame_support::{assert_ok, traits::Currency};
use frame_system::Config;
use sp_core::U256;
mod mock;
use mock::*;

use super::mock::*;

#[test]
fn test_batch_txs() {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
#![allow(clippy::indexing_slicing)]
use crate::mock::*;
use super::mock::*;
use frame_support::{assert_err, assert_noop, assert_ok};
mod mock;
use pallet_subtensor::{utils::rate_limiting::TransactionType, *};

use crate::{utils::rate_limiting::TransactionType, *};
use sp_core::U256;

// 1: Successful setting of a single child
Expand Down Expand Up @@ -3461,7 +3461,7 @@ fn test_childkey_take_drain() {
SubtensorModule::set_weights_set_rate_limit(netuid, 0);
SubtensorModule::set_max_allowed_validators(netuid, 2);
step_block(subnet_tempo);
pallet_subtensor::SubnetOwnerCut::<Test>::set(0);
crate::SubnetOwnerCut::<Test>::set(0);

// Set 20% childkey take
let max_take: u16 = 0xFFFF / 5;
Expand Down Expand Up @@ -3509,15 +3509,15 @@ fn test_childkey_take_drain() {
vec![(proportion, child)]
));
// Make all stakes viable
pallet_subtensor::StakeDeltaSinceLastEmissionDrain::<Test>::set(parent, coldkey, -1);
pallet_subtensor::StakeDeltaSinceLastEmissionDrain::<Test>::set(child, nominator, -1);
crate::StakeDeltaSinceLastEmissionDrain::<Test>::set(parent, coldkey, -1);
crate::StakeDeltaSinceLastEmissionDrain::<Test>::set(child, nominator, -1);

// Setup YUMA so that it creates emissions:
// Parent and child both set weights
// Parent and child register on root and
// Set root weights
pallet_subtensor::Weights::<Test>::insert(netuid, 0, vec![(0, 0xFFFF), (1, 0xFFFF)]);
pallet_subtensor::Weights::<Test>::insert(netuid, 1, vec![(0, 0xFFFF), (1, 0xFFFF)]);
crate::Weights::<Test>::insert(netuid, 0, vec![(0, 0xFFFF), (1, 0xFFFF)]);
crate::Weights::<Test>::insert(netuid, 1, vec![(0, 0xFFFF), (1, 0xFFFF)]);
assert_ok!(SubtensorModule::do_root_register(
RuntimeOrigin::signed(coldkey),
parent,
Expand All @@ -3526,17 +3526,17 @@ fn test_childkey_take_drain() {
RuntimeOrigin::signed(coldkey),
child,
));
pallet_subtensor::Weights::<Test>::insert(root_id, 0, vec![(0, 0xFFFF), (1, 0xFFFF)]);
pallet_subtensor::Weights::<Test>::insert(root_id, 1, vec![(0, 0xFFFF), (1, 0xFFFF)]);
crate::Weights::<Test>::insert(root_id, 0, vec![(0, 0xFFFF), (1, 0xFFFF)]);
crate::Weights::<Test>::insert(root_id, 1, vec![(0, 0xFFFF), (1, 0xFFFF)]);

// Run run_coinbase until PendingHotkeyEmission are populated
while pallet_subtensor::PendingdHotkeyEmission::<Test>::get(child) == 0 {
while crate::PendingdHotkeyEmission::<Test>::get(child) == 0 {
step_block(1);
}

// Prevent further subnet epochs
pallet_subtensor::Tempo::<Test>::set(netuid, u16::MAX);
pallet_subtensor::Tempo::<Test>::set(root_id, u16::MAX);
crate::Tempo::<Test>::set(netuid, u16::MAX);
crate::Tempo::<Test>::set(root_id, u16::MAX);

// Run run_coinbase until PendingHotkeyEmission is drained for both child and parent
step_block((hotkey_tempo * 2) as u16);
Expand All @@ -3545,9 +3545,9 @@ fn test_childkey_take_drain() {
// - Child stake increased by its child key take only (20% * 50% = 10% of total emission)
// - Parent stake increased by 40% of total emission
// - Nominator stake increased by 50% of total emission
let child_emission = pallet_subtensor::Stake::<Test>::get(child, coldkey);
let parent_emission = pallet_subtensor::Stake::<Test>::get(parent, coldkey) - stake;
let nominator_emission = pallet_subtensor::Stake::<Test>::get(child, nominator) - stake;
let child_emission = crate::Stake::<Test>::get(child, coldkey);
let parent_emission = crate::Stake::<Test>::get(parent, coldkey) - stake;
let nominator_emission = crate::Stake::<Test>::get(child, nominator) - stake;
let total_emission = child_emission + parent_emission + nominator_emission;

assert!(is_within_tolerance(
Expand Down Expand Up @@ -3600,7 +3600,7 @@ fn test_childkey_take_drain_validator_take() {
SubtensorModule::set_weights_set_rate_limit(netuid, 0);
SubtensorModule::set_max_allowed_validators(netuid, 2);
step_block(subnet_tempo);
pallet_subtensor::SubnetOwnerCut::<Test>::set(0);
crate::SubnetOwnerCut::<Test>::set(0);

// Set 20% childkey take
let max_take: u16 = 0xFFFF / 5;
Expand Down Expand Up @@ -3648,15 +3648,15 @@ fn test_childkey_take_drain_validator_take() {
vec![(proportion, child)]
));
// Make all stakes viable
pallet_subtensor::StakeDeltaSinceLastEmissionDrain::<Test>::set(parent, coldkey, -1);
pallet_subtensor::StakeDeltaSinceLastEmissionDrain::<Test>::set(child, nominator, -1);
crate::StakeDeltaSinceLastEmissionDrain::<Test>::set(parent, coldkey, -1);
crate::StakeDeltaSinceLastEmissionDrain::<Test>::set(child, nominator, -1);

// Setup YUMA so that it creates emissions:
// Parent and child both set weights
// Parent and child register on root and
// Set root weights
pallet_subtensor::Weights::<Test>::insert(netuid, 0, vec![(0, 0xFFFF), (1, 0xFFFF)]);
pallet_subtensor::Weights::<Test>::insert(netuid, 1, vec![(0, 0xFFFF), (1, 0xFFFF)]);
crate::Weights::<Test>::insert(netuid, 0, vec![(0, 0xFFFF), (1, 0xFFFF)]);
crate::Weights::<Test>::insert(netuid, 1, vec![(0, 0xFFFF), (1, 0xFFFF)]);
assert_ok!(SubtensorModule::do_root_register(
RuntimeOrigin::signed(coldkey),
parent,
Expand All @@ -3665,17 +3665,17 @@ fn test_childkey_take_drain_validator_take() {
RuntimeOrigin::signed(coldkey),
child,
));
pallet_subtensor::Weights::<Test>::insert(root_id, 0, vec![(0, 0xFFFF), (1, 0xFFFF)]);
pallet_subtensor::Weights::<Test>::insert(root_id, 1, vec![(0, 0xFFFF), (1, 0xFFFF)]);
crate::Weights::<Test>::insert(root_id, 0, vec![(0, 0xFFFF), (1, 0xFFFF)]);
crate::Weights::<Test>::insert(root_id, 1, vec![(0, 0xFFFF), (1, 0xFFFF)]);

// Run run_coinbase until PendingHotkeyEmission are populated
while pallet_subtensor::PendingdHotkeyEmission::<Test>::get(child) == 0 {
while crate::PendingdHotkeyEmission::<Test>::get(child) == 0 {
step_block(1);
}

// Prevent further subnet epochs
pallet_subtensor::Tempo::<Test>::set(netuid, u16::MAX);
pallet_subtensor::Tempo::<Test>::set(root_id, u16::MAX);
crate::Tempo::<Test>::set(netuid, u16::MAX);
crate::Tempo::<Test>::set(root_id, u16::MAX);

// Run run_coinbase until PendingHotkeyEmission is drained for both child and parent
step_block((hotkey_tempo * 2) as u16);
Expand All @@ -3684,9 +3684,9 @@ fn test_childkey_take_drain_validator_take() {
// - Child stake increased by its child key take (20% * 50% = 10% of total emission) plus childkey's delegate take (10%)
// - Parent stake increased by 40% of total emission
// - Nominator stake increased by 40% of total emission
let child_emission = pallet_subtensor::Stake::<Test>::get(child, coldkey);
let parent_emission = pallet_subtensor::Stake::<Test>::get(parent, coldkey) - stake;
let nominator_emission = pallet_subtensor::Stake::<Test>::get(child, nominator) - stake;
let child_emission = crate::Stake::<Test>::get(child, coldkey);
let parent_emission = crate::Stake::<Test>::get(parent, coldkey) - stake;
let nominator_emission = crate::Stake::<Test>::get(child, nominator) - stake;
let total_emission = child_emission + parent_emission + nominator_emission;

assert!(is_within_tolerance(child_emission, total_emission / 5, 500));
Expand Down
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
#![allow(unused, clippy::indexing_slicing, clippy::panic, clippy::unwrap_used)]
use crate::mock::*;
mod mock;
use super::mock::*;

use frame_support::assert_ok;
use sp_core::U256;
use substrate_fixed::types::I64F64;

use pallet_subtensor::TargetStakesPerInterval;
use crate::TargetStakesPerInterval;

// Test the ability to hash all sorts of hotkeys.
#[test]
Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
#![allow(clippy::unwrap_used)]

use crate::mock::*;
mod mock;
use sp_core::U256;

use super::mock::*;

#[test]
fn test_registration_difficulty_adjustment() {
new_test_ext(1).execute_with(|| {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,31 +4,17 @@
clippy::unwrap_used
)]

use crate::mock::*;
use super::mock::*;
use crate::epoch::math::safe_exp;
use crate::*;
use frame_support::{assert_err, assert_ok};
use frame_system::Config;
use pallet_subtensor::epoch::math::safe_exp;
use pallet_subtensor::*;
use rand::{distributions::Uniform, rngs::StdRng, seq::SliceRandom, thread_rng, Rng, SeedableRng};
use sp_core::U256;
use sp_runtime::DispatchError;
use std::time::Instant;
use substrate_fixed::types::I32F32;

mod mock;

pub fn fixed(val: f32) -> I32F32 {
I32F32::from_num(val)
}

pub fn fixed_to_u16(x: I32F32) -> u16 {
x.to_num::<u16>()
}

pub fn fixed_proportion_to_u16(x: I32F32) -> u16 {
fixed_to_u16(x * I32F32::from_num(u16::MAX))
}

// Normalizes (sum to 1 except 0) the input vector directly in-place.
#[allow(dead_code)]
pub fn inplace_normalize(x: &mut [I32F32]) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
)]
use substrate_fixed::types::{I32F32, I64F64};

use pallet_subtensor::epoch::math::*;
use crate::epoch::math::*;
use rand::{seq::SliceRandom, thread_rng, Rng};
use substrate_fixed::{
transcendental::exp,
Expand Down
Loading