Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update test for per-subnet take emission #356

Merged
merged 3 commits into from
Apr 26, 2024
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
330 changes: 330 additions & 0 deletions pallets/subtensor/tests/block_step.rs
Original file line number Diff line number Diff line change
Expand Up @@ -920,3 +920,333 @@ fn test_run_coinbase_price_less_than_1() {
assert_eq!(pending_alpha_after == pending_alpha_before, true);
})
}

#[test]
fn test_10_subnet_take_basic_ok() {
new_test_ext(1).execute_with(|| {
let netuid1 = 1;
let hotkey0 = U256::from(1);
let coldkey0 = U256::from(3);
let coldkey1 = U256::from(4);

// Create networks.
let lock_cost_1 = SubtensorModule::get_network_lock_cost();
setup_dynamic_network(netuid1, 3u16, 1u16);
SubtensorModule::add_balance_to_coldkey_account( &coldkey0, 1000_000_000_000 );
SubtensorModule::add_balance_to_coldkey_account( &coldkey1, 1000_000_000_000 );
SubtensorModule::add_balance_to_coldkey_account( &hotkey0, 1000_000_000_000 );

// The tests below assume lock costs of LC1 = 100
assert_eq!(lock_cost_1, 100_000_000_000);

// SubStake (Alpha balance)
// Subnet 1, cold0, hot0: LC1 (100)
//
// DynamicTAOReserve (get_tao_reserve) assertions
// Subnet 1: 100
//
// DynamicAlphaReserve (get_alpha_reserve) assertions
// Subnet 1: 100
//
// DynamicAlphaOutstanding (get_alpha_outstading) assertions
// Subnet 1: 100
//
assert_substake_eq!(&coldkey0, &hotkey0, netuid1, 100_000_000_000);
assert_eq!(SubtensorModule::get_tao_reserve(netuid1), 100_000_000_000);
assert_eq!(SubtensorModule::get_alpha_reserve(netuid1), 100_000_000_000);
assert_eq!(SubtensorModule::get_alpha_outstanding(netuid1), 100_000_000_000);

// Coldkey / hotkey 0 become a delegate
assert_ok!(SubtensorModule::do_become_delegate(
<<Test as Config>::RuntimeOrigin>::signed(coldkey0),
hotkey0
));

// Coldkey / hotkey 0 sets the take on subnet 1 to 10%
assert_ok!(SubtensorModule::do_decrease_take(
<<Test as Config>::RuntimeOrigin>::signed(coldkey0),
hotkey0,
netuid1,
u16::MAX / 10
));

// Nominate 100 from coldkey/hotkey 1 to hotkey0 on subnet 1
assert_ok!(SubtensorModule::add_subnet_stake(
<<Test as Config>::RuntimeOrigin>::signed(coldkey1),
hotkey0,
netuid1,
100_000_000_000
));

// SubStake (Alpha balance)
// Subnet 1, cold0, hot0: 100
// cold1, hot0: 50
//
// DynamicTAOReserve (get_tao_reserve) assertions
// Subnet 1: 200
//
// DynamicAlphaReserve (get_alpha_reserve) assertions
// Subnet 1: 50
//
// DynamicAlphaOutstanding (get_alpha_outstading) assertions
// Subnet 1: 150
//
assert_substake_eq!(&coldkey0, &hotkey0, netuid1, 100_000_000_000);
assert_substake_eq!(&coldkey1, &hotkey0, netuid1, 50_000_000_000);
assert_eq!(SubtensorModule::get_tao_reserve(netuid1), 200_000_000_000);
assert_eq!(SubtensorModule::get_alpha_reserve(netuid1), 50_000_000_000);
assert_eq!(SubtensorModule::get_alpha_outstanding(netuid1), 150_000_000_000);

// Emission
//
// Emit inflation through run_coinbase
// We will emit 0 server emission (which should go in-full to the owner of the hotkey).
// We will emit 200 validator emission, which should be distributed in-part to the nominators.
//
let emission = 200_000_000_000;
SubtensorModule::emit_inflation_through_hotkey_account(&hotkey0, netuid1, 0, emission);

// SubStake (Alpha balance)
// Subnet 1, cold0, hot0: 350 - 110 = 240
// cold1, hot0: 110
//
assert_substake_approx_eq!(&coldkey0, &hotkey0, netuid1, 240.);
assert_substake_approx_eq!(&coldkey1, &hotkey0, netuid1, 110.);
});
}

#[test]
fn test_20_subnet_take_basic_ok() {
new_test_ext(1).execute_with(|| {
let netuid1 = 1;
let hotkey0 = U256::from(1);
let coldkey0 = U256::from(3);
let coldkey1 = U256::from(4);

// Create networks.
let lock_cost_1 = SubtensorModule::get_network_lock_cost();
setup_dynamic_network(netuid1, 3u16, 1u16);
SubtensorModule::add_balance_to_coldkey_account( &coldkey0, 1000_000_000_000 );
SubtensorModule::add_balance_to_coldkey_account( &coldkey1, 1000_000_000_000 );
SubtensorModule::add_balance_to_coldkey_account( &hotkey0, 1000_000_000_000 );

// The tests below assume lock costs of LC1 = 100
assert_eq!(lock_cost_1, 100_000_000_000);

// SubStake (Alpha balance)
// Subnet 1, cold0, hot0: LC1 (100)
//
// DynamicTAOReserve (get_tao_reserve) assertions
// Subnet 1: 100
//
// DynamicAlphaReserve (get_alpha_reserve) assertions
// Subnet 1: 100
//
// DynamicAlphaOutstanding (get_alpha_outstading) assertions
// Subnet 1: 100
//
assert_substake_eq!(&coldkey0, &hotkey0, netuid1, 100_000_000_000);
assert_eq!(SubtensorModule::get_tao_reserve(netuid1), 100_000_000_000);
assert_eq!(SubtensorModule::get_alpha_reserve(netuid1), 100_000_000_000);
assert_eq!(SubtensorModule::get_alpha_outstanding(netuid1), 100_000_000_000);

// Coldkey / hotkey 0 become a delegate
assert_ok!(SubtensorModule::do_become_delegate(
<<Test as Config>::RuntimeOrigin>::signed(coldkey0),
hotkey0
));

// Coldkey / hotkey 0 sets the take on subnet 1 to 20%
assert_ok!(SubtensorModule::do_decrease_take(
<<Test as Config>::RuntimeOrigin>::signed(coldkey0),
hotkey0,
netuid1,
u16::MAX / 5
));

// Nominate 100 from coldkey/hotkey 1 to hotkey0 on subnet 1
assert_ok!(SubtensorModule::add_subnet_stake(
<<Test as Config>::RuntimeOrigin>::signed(coldkey1),
hotkey0,
netuid1,
100_000_000_000
));

// SubStake (Alpha balance)
// Subnet 1, cold0, hot0: 100
// cold1, hot0: 50
//
// DynamicTAOReserve (get_tao_reserve) assertions
// Subnet 1: 200
//
// DynamicAlphaReserve (get_alpha_reserve) assertions
// Subnet 1: 50
//
// DynamicAlphaOutstanding (get_alpha_outstading) assertions
// Subnet 1: 150
//
assert_substake_eq!(&coldkey0, &hotkey0, netuid1, 100_000_000_000);
assert_substake_eq!(&coldkey1, &hotkey0, netuid1, 50_000_000_000);
assert_eq!(SubtensorModule::get_tao_reserve(netuid1), 200_000_000_000);
assert_eq!(SubtensorModule::get_alpha_reserve(netuid1), 50_000_000_000);
assert_eq!(SubtensorModule::get_alpha_outstanding(netuid1), 150_000_000_000);

// Emission
//
// Emit inflation through run_coinbase
// We will emit 0 server emission (which should go in-full to the owner of the hotkey).
// We will emit 200 validator emission, which should be distributed in-part to the nominators.
//
let emission = 200_000_000_000;
SubtensorModule::emit_inflation_through_hotkey_account(&hotkey0, netuid1, 0, emission);

// SubStake (Alpha balance)
// Subnet 1, cold0, hot0: 350 - 103.3333 ~ 246.67
// cold1, hot0: 103.3333
//
assert_substake_approx_eq!(&coldkey0, &hotkey0, netuid1, 246.67);
assert_substake_approx_eq!(&coldkey1, &hotkey0, netuid1, 103.33);
});
}

#[test]
fn test_two_subnets_take_ok() {
new_test_ext(1).execute_with(|| {
let netuid1 = 1;
let netuid2 = 2;
let hotkey0 = U256::from(1);
let hotkey1 = U256::from(2);
let coldkey0 = U256::from(3);
let coldkey1 = U256::from(4);

// Create networks.
let lock_cost_1 = SubtensorModule::get_network_lock_cost();
setup_dynamic_network(netuid1, 3u16, 1u16);
let lock_cost_2 = SubtensorModule::get_network_lock_cost();
setup_dynamic_network(netuid2, 3u16, 2u16);
SubtensorModule::add_balance_to_coldkey_account( &coldkey0, 1000_000_000_000 );
SubtensorModule::add_balance_to_coldkey_account( &coldkey1, 1000_000_000_000 );
SubtensorModule::add_balance_to_coldkey_account( &hotkey0, 1000_000_000_000 );
SubtensorModule::add_balance_to_coldkey_account( &hotkey1, 1000_000_000_000 );

// The tests below assume lock costs of LC1 = LC2 = 100
assert_eq!(lock_cost_1, 100_000_000_000);
assert_eq!(lock_cost_2, 100_000_000_000);

// SubStake (Alpha balance)
// Subnet 1, cold0, hot0: LC1 (100)
//
// DynamicTAOReserve (get_tao_reserve) assertions
// Subnet 1: 100
// Subnet 2: 100
//
// DynamicAlphaReserve (get_alpha_reserve) assertions
// Subnet 1: 100
// Subnet 2: 200
//
// DynamicAlphaOutstanding (get_alpha_outstading) assertions
// Subnet 1: 100
// Subnet 2: 200
//
assert_substake_eq!(&coldkey0, &hotkey0, netuid1, 100_000_000_000);
assert_substake_eq!(&coldkey0, &hotkey1, netuid2, 200_000_000_000);
assert_eq!(SubtensorModule::get_tao_reserve(netuid1), 100_000_000_000);
assert_eq!(SubtensorModule::get_alpha_reserve(netuid1), 100_000_000_000);
assert_eq!(SubtensorModule::get_alpha_outstanding(netuid1), 100_000_000_000);
assert_eq!(SubtensorModule::get_tao_reserve(netuid2), 100_000_000_000);
assert_eq!(SubtensorModule::get_alpha_reserve(netuid2), 200_000_000_000);
assert_eq!(SubtensorModule::get_alpha_outstanding(netuid2), 200_000_000_000);

// Hotkey 0 becomes a delegate
assert_ok!(SubtensorModule::do_become_delegate(
<<Test as Config>::RuntimeOrigin>::signed(coldkey0),
hotkey0
));

// Hotkey 1 becomes a delegate
assert_ok!(SubtensorModule::do_become_delegate(
<<Test as Config>::RuntimeOrigin>::signed(coldkey0),
hotkey1
));

// Hotkey 0 sets the take on subnet 1 to 10%
assert_ok!(SubtensorModule::do_decrease_take(
<<Test as Config>::RuntimeOrigin>::signed(coldkey0),
hotkey0,
netuid1,
u16::MAX / 10
));

// Hotkey 1 sets the take on subnet 2 to 20%
assert_ok!(SubtensorModule::do_decrease_take(
<<Test as Config>::RuntimeOrigin>::signed(coldkey0),
hotkey1,
netuid2,
u16::MAX / 5
));

// Nominate 100 from coldkey1 to hotkey0 on subnet 1
assert_ok!(SubtensorModule::add_subnet_stake(
<<Test as Config>::RuntimeOrigin>::signed(coldkey1),
hotkey0,
netuid1,
100_000_000_000
));

// Nominate 100 from coldkey1 to hotkey1 on subnet 2
assert_ok!(SubtensorModule::add_subnet_stake(
<<Test as Config>::RuntimeOrigin>::signed(coldkey1),
hotkey1,
netuid2,
100_000_000_000
));

// SubStake (Alpha balance)
// Subnet 1, cold0, hot0: 100
// cold1, hot0: 50
// Subnet 2, cold0, hot1: 200
// cold1, hot1: 100
//
// DynamicTAOReserve (get_tao_reserve) assertions
// Subnet 1: 200
//
// DynamicAlphaReserve (get_alpha_reserve) assertions
// Subnet 1: 50
//
// DynamicAlphaOutstanding (get_alpha_outstading) assertions
// Subnet 1: 150
//
assert_substake_eq!(&coldkey0, &hotkey0, netuid1, 100_000_000_000);
assert_substake_eq!(&coldkey1, &hotkey0, netuid1, 50_000_000_000);
assert_substake_eq!(&coldkey0, &hotkey1, netuid2, 200_000_000_000);
assert_substake_eq!(&coldkey1, &hotkey1, netuid2, 100_000_000_000);
assert_eq!(SubtensorModule::get_tao_reserve(netuid1), 200_000_000_000);
assert_eq!(SubtensorModule::get_alpha_reserve(netuid1), 50_000_000_000);
assert_eq!(SubtensorModule::get_alpha_outstanding(netuid1), 150_000_000_000);
assert_eq!(SubtensorModule::get_tao_reserve(netuid2), 200_000_000_000);
assert_eq!(SubtensorModule::get_alpha_reserve(netuid2), 100_000_000_000);
assert_eq!(SubtensorModule::get_alpha_outstanding(netuid2), 300_000_000_000);

// Emission
//
// Emit inflation through run_coinbase
// We will emit 0 server emission (which should go in-full to the owner of the hotkey).
// We will emit 100 validator emission through each of hotkeys, which should be
// distributed in-part to the nominators.
//
let emission = 100_000_000_000;
SubtensorModule::emit_inflation_through_hotkey_account(&hotkey0, netuid1, 0, emission);
SubtensorModule::emit_inflation_through_hotkey_account(&hotkey1, netuid2, 0, emission);

// SubStake (Alpha balance)
// Subnet 1, cold0, hot0: 170
// cold1, hot0: 80
// Subnet 2, cold0, hot1: 273.34
// cold1, hot1: 126.67
//
assert_substake_approx_eq!(&coldkey0, &hotkey0, netuid1, 170.);
assert_substake_approx_eq!(&coldkey1, &hotkey0, netuid1, 80.);
assert_substake_approx_eq!(&coldkey0, &hotkey1, netuid2, 273.33);
assert_substake_approx_eq!(&coldkey1, &hotkey1, netuid2, 126.67);
});
}
38 changes: 38 additions & 0 deletions pallets/subtensor/tests/helpers.rs
Original file line number Diff line number Diff line change
Expand Up @@ -29,3 +29,41 @@ macro_rules! assert_i32f32_approx_eq {
assert_eq!(l_rounded, r_rounded);
}};
}

#[allow(dead_code)]
#[macro_export]
macro_rules! assert_approx_eq {
($left:expr, $right:expr $(,)?) => {{
const PRECISION: f64 = 100.;
let left = $left;
let right = $right;

let l_rounded = (PRECISION * left).round() / PRECISION;
let r_rounded = (PRECISION * right).round() / PRECISION;

assert_eq!(l_rounded, r_rounded);
}};
}

#[allow(dead_code)]
#[macro_export]
macro_rules! assert_substake_eq {
($coldkey:expr, $hotkey:expr, $netuid:expr, $amount:expr $(,)?) => {{
assert_eq!(
SubtensorModule::get_subnet_stake_for_coldkey_and_hotkey($coldkey, $hotkey, $netuid),
$amount
);
}};
}

#[allow(dead_code)]
#[macro_export]
macro_rules! assert_substake_approx_eq {
($coldkey:expr, $hotkey:expr, $netuid:expr, $amount:expr $(,)?) => {{
let subst = SubtensorModule::get_subnet_stake_for_coldkey_and_hotkey($coldkey, $hotkey, $netuid) as f64;
assert_approx_eq!(
subst / 1_000_000_000f64,
$amount
);
}};
}
6 changes: 3 additions & 3 deletions pallets/subtensor/tests/mock.rs
Original file line number Diff line number Diff line change
Expand Up @@ -481,6 +481,8 @@ pub fn add_dynamic_network(netuid: u16, tempo: u16, cold_id: u16, hot_id: u16 )
let hotkey = U256::from( hot_id );

add_network(netuid, tempo, 0);
register_ok_neuron(netuid, hotkey, coldkey, 11234);
SubtensorModule::append_neuron( netuid, &hotkey, 1 );

let initial_tao_reserve: u64 = lock_amount as u64;
let initial_dynamic_reserve: u64 = lock_amount * SubtensorModule::get_num_subnets() as u64;
Expand All @@ -504,10 +506,8 @@ pub fn add_dynamic_network(netuid: u16, tempo: u16, cold_id: u16, hot_id: u16 )
#[allow(dead_code)]
pub fn setup_dynamic_network(netuid: u16, cold_id: u16, hot_id: u16) {
SubtensorModule::set_global_stake_weight( 0 );
let hotkey = U256::from( hot_id );
add_dynamic_network( netuid, u16::MAX - 1, cold_id, hot_id );
add_dynamic_network( netuid, 10, cold_id, hot_id );
SubtensorModule::set_max_allowed_uids( netuid, 1 );
SubtensorModule::append_neuron( netuid, &hotkey, 1 );
}

#[allow(dead_code)]
Expand Down
Loading