Skip to content

Commit ea355f2

Browse files
committed
Fix tests
1 parent b2639e7 commit ea355f2

File tree

2 files changed

+86
-54
lines changed

2 files changed

+86
-54
lines changed

pallets/subtensor/src/tests/children.rs

Lines changed: 77 additions & 54 deletions
Original file line numberDiff line numberDiff line change
@@ -1554,18 +1554,20 @@ fn test_set_network_max_stake_update() {
15541554
#[test]
15551555
fn test_children_stake_values() {
15561556
new_test_ext(1).execute_with(|| {
1557+
let subnet_owner_coldkey = U256::from(1001);
1558+
let subnet_owner_hotkey = U256::from(1002);
1559+
let netuid: u16 = add_dynamic_network(&subnet_owner_hotkey, &subnet_owner_coldkey);
1560+
15571561
let coldkey = U256::from(1);
15581562
let hotkey = U256::from(2);
15591563
let child1 = U256::from(3);
15601564
let child2 = U256::from(4);
15611565
let child3 = U256::from(5);
1562-
let netuid: u16 = 1;
15631566
let proportion1: u64 = u64::MAX / 4;
15641567
let proportion2: u64 = u64::MAX / 4;
15651568
let proportion3: u64 = u64::MAX / 4;
15661569

15671570
// Add network and register hotkey
1568-
add_network(netuid, 13, 0);
15691571
SubtensorModule::set_max_registrations_per_block(netuid, 4);
15701572
SubtensorModule::set_target_registrations_per_interval(netuid, 4);
15711573
register_ok_neuron(netuid, hotkey, coldkey, 0);
@@ -1580,10 +1582,9 @@ fn test_children_stake_values() {
15801582
);
15811583

15821584
// Set multiple children with proportions.
1583-
mock_set_children(
1584-
&coldkey,
1585-
&hotkey,
1585+
mock_set_children_no_epochs(
15861586
netuid,
1587+
&hotkey,
15871588
&[
15881589
(proportion1, child1),
15891590
(proportion2, child2),
@@ -1830,28 +1831,28 @@ fn test_get_stake_for_hotkey_on_subnet_multiple_coldkeys() {
18301831
#[test]
18311832
fn test_get_stake_for_hotkey_on_subnet_single_parent_child() {
18321833
new_test_ext(1).execute_with(|| {
1833-
let netuid: u16 = 1;
1834+
let subnet_owner_coldkey = U256::from(1001);
1835+
let subnet_owner_hotkey = U256::from(1002);
18341836
let parent = U256::from(1);
18351837
let child = U256::from(2);
18361838
let coldkey = U256::from(3);
1837-
1838-
add_network(netuid, 1, 0);
1839+
let netuid: u16 = add_dynamic_network(&subnet_owner_hotkey, &subnet_owner_coldkey);
18391840
register_ok_neuron(netuid, parent, coldkey, 0);
18401841
register_ok_neuron(netuid, child, coldkey, 0);
18411842

18421843
SubtensorModule::increase_stake_for_hotkey_and_coldkey_on_subnet(
1843-
&parent, &coldkey, netuid, 1000,
1844+
&parent, &coldkey, netuid, 1_000_000_000,
18441845
);
18451846

1846-
mock_set_children(&coldkey, &parent, netuid, &[(u64::MAX, child)]);
1847+
mock_set_children_no_epochs(netuid, &parent, &[(u64::MAX, child)]);
18471848

18481849
assert_eq!(
18491850
SubtensorModule::get_inherited_for_hotkey_on_subnet(&parent, netuid),
18501851
0
18511852
);
18521853
assert_eq!(
18531854
SubtensorModule::get_inherited_for_hotkey_on_subnet(&child, netuid),
1854-
1000
1855+
1_000_000_000
18551856
);
18561857
});
18571858
}
@@ -1867,13 +1868,15 @@ fn test_get_stake_for_hotkey_on_subnet_single_parent_child() {
18671868
#[test]
18681869
fn test_get_stake_for_hotkey_on_subnet_multiple_parents_single_child() {
18691870
new_test_ext(1).execute_with(|| {
1870-
let netuid: u16 = 1;
1871+
let subnet_owner_coldkey = U256::from(1001);
1872+
let subnet_owner_hotkey = U256::from(1002);
1873+
let netuid: u16 = add_dynamic_network(&subnet_owner_hotkey, &subnet_owner_coldkey);
1874+
18711875
let parent1 = U256::from(1);
18721876
let parent2 = U256::from(2);
18731877
let child = U256::from(3);
18741878
let coldkey = U256::from(4);
18751879

1876-
add_network(netuid, 1, 0);
18771880
register_ok_neuron(netuid, parent1, coldkey, 0);
18781881
register_ok_neuron(netuid, parent2, coldkey, 0);
18791882
register_ok_neuron(netuid, child, coldkey, 0);
@@ -1885,8 +1888,8 @@ fn test_get_stake_for_hotkey_on_subnet_multiple_parents_single_child() {
18851888
&parent2, &coldkey, netuid, 2000,
18861889
);
18871890

1888-
mock_set_children(&coldkey, &parent1, netuid, &[(u64::MAX / 2, child)]);
1889-
mock_set_children(&coldkey, &parent2, netuid, &[(u64::MAX / 2, child)]);
1891+
mock_set_children_no_epochs(netuid, &parent1, &[(u64::MAX / 2, child)]);
1892+
mock_set_children_no_epochs(netuid, &parent2, &[(u64::MAX / 2, child)]);
18901893

18911894
close(
18921895
SubtensorModule::get_inherited_for_hotkey_on_subnet(&parent1, netuid),
@@ -1920,13 +1923,15 @@ fn test_get_stake_for_hotkey_on_subnet_multiple_parents_single_child() {
19201923
#[test]
19211924
fn test_get_stake_for_hotkey_on_subnet_single_parent_multiple_children() {
19221925
new_test_ext(1).execute_with(|| {
1923-
let netuid: u16 = 1;
1926+
let subnet_owner_coldkey = U256::from(1001);
1927+
let subnet_owner_hotkey = U256::from(1002);
1928+
let netuid: u16 = add_dynamic_network(&subnet_owner_hotkey, &subnet_owner_coldkey);
1929+
19241930
let parent = U256::from(1);
19251931
let child1 = U256::from(2);
19261932
let child2 = U256::from(3);
19271933
let coldkey = U256::from(4);
19281934

1929-
add_network(netuid, 1, 0);
19301935
register_ok_neuron(netuid, parent, coldkey, 0);
19311936
register_ok_neuron(netuid, child1, coldkey, 0);
19321937
register_ok_neuron(netuid, child2, coldkey, 0);
@@ -1939,10 +1944,9 @@ fn test_get_stake_for_hotkey_on_subnet_single_parent_multiple_children() {
19391944
total_stake,
19401945
);
19411946

1942-
mock_set_children(
1943-
&coldkey,
1944-
&parent,
1947+
mock_set_children_no_epochs(
19451948
netuid,
1949+
&parent,
19461950
&[(u64::MAX / 3, child1), (u64::MAX / 3, child2)],
19471951
);
19481952

@@ -1983,13 +1987,15 @@ fn test_get_stake_for_hotkey_on_subnet_single_parent_multiple_children() {
19831987
#[test]
19841988
fn test_get_stake_for_hotkey_on_subnet_edge_cases() {
19851989
new_test_ext(1).execute_with(|| {
1986-
let netuid: u16 = 1;
1990+
let subnet_owner_coldkey = U256::from(1001);
1991+
let subnet_owner_hotkey = U256::from(1002);
1992+
let netuid: u16 = add_dynamic_network(&subnet_owner_hotkey, &subnet_owner_coldkey);
1993+
19871994
let parent = U256::from(1);
19881995
let child1 = U256::from(2);
19891996
let child2 = U256::from(3);
19901997
let coldkey = U256::from(4);
19911998

1992-
add_network(netuid, 1, 0);
19931999
register_ok_neuron(netuid, parent, coldkey, 0);
19942000
register_ok_neuron(netuid, child1, coldkey, 0);
19952001
register_ok_neuron(netuid, child2, coldkey, 0);
@@ -2007,10 +2013,9 @@ fn test_get_stake_for_hotkey_on_subnet_edge_cases() {
20072013
);
20082014

20092015
// Test with 0% and 100% stake allocation
2010-
mock_set_children(
2011-
&coldkey,
2012-
&parent,
2016+
mock_set_children_no_epochs(
20132017
netuid,
2018+
&parent,
20142019
&[(0, child1), (u64::MAX, child2)],
20152020
);
20162021

@@ -2051,7 +2056,10 @@ fn test_get_stake_for_hotkey_on_subnet_edge_cases() {
20512056
#[test]
20522057
fn test_get_stake_for_hotkey_on_subnet_complex_hierarchy() {
20532058
new_test_ext(1).execute_with(|| {
2054-
let netuid: u16 = 1;
2059+
let subnet_owner_coldkey = U256::from(1001);
2060+
let subnet_owner_hotkey = U256::from(1002);
2061+
let netuid: u16 = add_dynamic_network(&subnet_owner_hotkey, &subnet_owner_coldkey);
2062+
20552063
let parent = U256::from(1);
20562064
let child1 = U256::from(2);
20572065
let child2 = U256::from(3);
@@ -2061,7 +2069,6 @@ fn test_get_stake_for_hotkey_on_subnet_complex_hierarchy() {
20612069
let coldkey_child2 = U256::from(7);
20622070
let coldkey_grandchild = U256::from(8);
20632071

2064-
add_network(netuid, 1, 0);
20652072
SubtensorModule::set_max_registrations_per_block(netuid, 1000);
20662073
SubtensorModule::set_target_registrations_per_interval(netuid, 1000);
20672074
register_ok_neuron(netuid, parent, coldkey_parent, 0);
@@ -2096,10 +2103,9 @@ fn test_get_stake_for_hotkey_on_subnet_complex_hierarchy() {
20962103
);
20972104

20982105
// Step 1: Set children for parent
2099-
mock_set_children(
2100-
&coldkey_parent,
2101-
&parent,
2106+
mock_set_children_no_epochs(
21022107
netuid,
2108+
&parent,
21032109
&[(u64::MAX / 2, child1), (u64::MAX / 2, child2)],
21042110
);
21052111

@@ -2133,7 +2139,7 @@ fn test_get_stake_for_hotkey_on_subnet_complex_hierarchy() {
21332139
close(child2_stake_1, 499, 10, "Child2 should have 499 stake");
21342140

21352141
// Step 2: Set children for child1
2136-
mock_set_children(&coldkey_child1, &child1, netuid, &[(u64::MAX, grandchild)]);
2142+
mock_set_children_no_epochs(netuid, &child1, &[(u64::MAX, grandchild)]);
21372143

21382144
log::info!("After setting child1's children:");
21392145
log::info!(
@@ -2810,8 +2816,9 @@ fn test_childkey_set_weights_single_parent() {
28102816
fn test_set_weights_no_parent() {
28112817
// Verify that a regular key without a parent delegation is effected by the minimum stake requirements
28122818
new_test_ext(1).execute_with(|| {
2813-
let netuid: u16 = 1;
2814-
add_network(netuid, 1, 0);
2819+
let subnet_owner_coldkey = U256::from(1001);
2820+
let subnet_owner_hotkey = U256::from(1002);
2821+
let netuid: u16 = add_dynamic_network(&subnet_owner_hotkey, &subnet_owner_coldkey);
28152822

28162823
let hotkey: U256 = U256::from(2);
28172824
let spare_hk: U256 = U256::from(3);
@@ -3108,8 +3115,15 @@ fn test_childkey_take_drain_validator_take() {
31083115
#[test]
31093116
fn test_parent_child_chain_emission() {
31103117
new_test_ext(1).execute_with(|| {
3111-
let netuid: u16 = 1;
3112-
add_network(netuid, 1, 0);
3118+
let subnet_owner_coldkey = U256::from(1001);
3119+
let subnet_owner_hotkey = U256::from(1002);
3120+
let netuid: u16 = add_dynamic_network(&subnet_owner_hotkey, &subnet_owner_coldkey);
3121+
Tempo::<Test>::insert(netuid, 1);
3122+
3123+
// Setup large LPs to prevent slippage
3124+
SubnetTAO::<Test>::insert(netuid, 1_000_000_000_000_000);
3125+
SubnetAlphaIn::<Test>::insert(netuid, 1_000_000_000_000_000);
3126+
31133127
// Set owner cut to 0
31143128
SubtensorModule::set_subnet_owner_cut(0_u16);
31153129

@@ -3132,7 +3146,10 @@ fn test_parent_child_chain_emission() {
31323146
SubtensorModule::add_balance_to_coldkey_account(&coldkey_c, 1_000);
31333147

31343148
// Swap to alpha
3135-
let total_tao: I96F32 = I96F32::from_num(300_000 + 100_000 + 50_000);
3149+
let stake_a = 300_000_000_000_u64;
3150+
let stake_b = 100_000_000_000_u64;
3151+
let stake_c = 50_000_000_000_u64;
3152+
let total_tao: I96F32 = I96F32::from_num(stake_a + stake_b + stake_c);
31363153
let total_alpha: I96F32 = I96F32::from_num(SubtensorModule::swap_tao_for_alpha(
31373154
netuid,
31383155
total_tao.to_num::<u64>(),
@@ -3144,19 +3161,19 @@ fn test_parent_child_chain_emission() {
31443161
&hotkey_a,
31453162
&coldkey_a,
31463163
netuid,
3147-
(total_alpha * I96F32::from_num(300_000) / total_tao).saturating_to_num::<u64>(),
3164+
(total_alpha * I96F32::from_num(stake_a) / total_tao).saturating_to_num::<u64>(),
31483165
);
31493166
SubtensorModule::increase_stake_for_hotkey_and_coldkey_on_subnet(
31503167
&hotkey_b,
31513168
&coldkey_b,
31523169
netuid,
3153-
(total_alpha * I96F32::from_num(100_000) / total_tao).saturating_to_num::<u64>(),
3170+
(total_alpha * I96F32::from_num(stake_b) / total_tao).saturating_to_num::<u64>(),
31543171
);
31553172
SubtensorModule::increase_stake_for_hotkey_and_coldkey_on_subnet(
31563173
&hotkey_c,
31573174
&coldkey_c,
31583175
netuid,
3159-
(total_alpha * I96F32::from_num(50_000) / total_tao).saturating_to_num::<u64>(),
3176+
(total_alpha * I96F32::from_num(stake_c) / total_tao).saturating_to_num::<u64>(),
31603177
);
31613178

31623179
// Get old stakes
@@ -3174,24 +3191,33 @@ fn test_parent_child_chain_emission() {
31743191
log::info!("rel_stake_a: {:?}", rel_stake_a); // 0.6666 -> 2/3
31753192
log::info!("rel_stake_b: {:?}", rel_stake_b); // 0.2222 -> 2/9
31763193
log::info!("rel_stake_c: {:?}", rel_stake_c); // 0.1111 -> 1/9
3177-
assert_eq!(rel_stake_a, I96F32::from_num(300_000) / total_tao);
3178-
assert_eq!(rel_stake_b, I96F32::from_num(100_000) / total_tao);
3179-
assert_eq!(rel_stake_c, I96F32::from_num(50_000) / total_tao);
3194+
assert!(
3195+
(rel_stake_a - I96F32::from_num(stake_a) / total_tao).abs() < 0.001
3196+
);
3197+
assert!(
3198+
(rel_stake_b - I96F32::from_num(stake_b) / total_tao).abs() < 0.001
3199+
);
3200+
assert!(
3201+
(rel_stake_c - I96F32::from_num(stake_c) / total_tao).abs() < 0.001
3202+
);
31803203

31813204
// Set parent-child relationships
31823205
// A -> B (50% of A's stake)
3183-
mock_schedule_children(&coldkey_a, &hotkey_a, netuid, &[(u64::MAX / 2, hotkey_b)]);
3206+
mock_set_children_no_epochs(netuid, &hotkey_a, &[(u64::MAX / 2, hotkey_b)]);
31843207

31853208
// B -> C (50% of B's stake)
3186-
mock_schedule_children(&coldkey_b, &hotkey_b, netuid, &[(u64::MAX / 2, hotkey_c)]);
3187-
wait_and_set_pending_children(netuid); // Don't want to run blocks before both children are scheduled
3209+
mock_set_children_no_epochs(netuid, &hotkey_b, &[(u64::MAX / 2, hotkey_c)]);
31883210

31893211
// Get old stakes after children are scheduled
31903212
let stake_a_old: u64 = SubtensorModule::get_total_stake_for_hotkey(&hotkey_a);
31913213
let stake_b_old: u64 = SubtensorModule::get_total_stake_for_hotkey(&hotkey_b);
31923214
let stake_c_old: u64 = SubtensorModule::get_total_stake_for_hotkey(&hotkey_c);
31933215

31943216
let total_stake_old: I96F32 = I96F32::from_num(stake_a_old + stake_b_old + stake_c_old);
3217+
log::info!("Old stake for hotkey A: {:?}", stake_a_old);
3218+
log::info!("Old stake for hotkey B: {:?}", stake_b_old);
3219+
log::info!("Old stake for hotkey C: {:?}", stake_c_old);
3220+
log::info!("Total old stake: {:?}", total_stake_old);
31953221

31963222
// Set CHK take rate to 1/9
31973223
let chk_take: I96F32 = I96F32::from_num(1_f64 / 9_f64);
@@ -3203,8 +3229,6 @@ fn test_parent_child_chain_emission() {
32033229
SubtensorModule::set_tao_weight(0);
32043230

32053231
let hardcoded_emission: I96F32 = I96F32::from_num(1_000_000); // 1 million (adjust as needed)
3206-
let emission_as_alpha =
3207-
I96F32::from_num(hardcoded_emission) / SubtensorModule::get_alpha_price(netuid);
32083232

32093233
// Set pending emission to 0
32103234
PendingEmission::<Test>::insert(netuid, 0);
@@ -3238,8 +3262,8 @@ fn test_parent_child_chain_emission() {
32383262

32393263
// Verify the final stake distribution
32403264
let stake_inc_eps: I96F32 = I96F32::from_num(1e-4); // 4 decimal places
3241-
// Each child has chk_take take
3242-
3265+
3266+
// Each child has chk_take take
32433267
let expected_a = I96F32::from_num(2_f64 / 3_f64)
32443268
* (I96F32::from_num(1_f64) - (I96F32::from_num(1_f64 / 2_f64) * chk_take));
32453269
assert!(
@@ -3289,11 +3313,10 @@ fn test_parent_child_chain_emission() {
32893313
total_stake_new
32903314
);
32913315

3292-
let eps: I96F32 = I96F32::from_num(10_000);
3293-
assert!(
3294-
(total_stake_new - (total_stake_old + emission_as_alpha)).abs() <= eps,
3295-
"Total stake should have increased by the hardcoded emission amount {:?}",
3296-
total_stake_new - (total_stake_old + emission_as_alpha)
3316+
assert_abs_diff_eq!(
3317+
total_stake_inc.to_num::<u64>(),
3318+
hardcoded_emission.to_num::<u64>(),
3319+
epsilon = 10_000,
32973320
);
32983321
});
32993322
}

pallets/subtensor/src/tests/mock.rs

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -727,6 +727,15 @@ pub fn mock_set_children(coldkey: &U256, parent: &U256, netuid: u16, child_vec:
727727
wait_and_set_pending_children(netuid);
728728
}
729729

730+
#[allow(dead_code)]
731+
pub fn mock_set_children_no_epochs(netuid: u16, parent: &U256, child_vec: &[(u64, U256)]) {
732+
let backup_block = SubtensorModule::get_current_block_as_u64();
733+
PendingChildKeys::<Test>::insert(netuid, parent, (child_vec, 0));
734+
System::set_block_number(1);
735+
SubtensorModule::do_set_pending_children(netuid);
736+
System::set_block_number(backup_block);
737+
}
738+
730739
// Helper function to wait for the rate limit
731740
#[allow(dead_code)]
732741
pub fn step_rate_limit(transaction_type: &TransactionType, netuid: u16) {

0 commit comments

Comments
 (0)