Skip to content

Commit 213b7b1

Browse files
committed
use next_block/run_to_block _no_epoch
1 parent f7e1531 commit 213b7b1

File tree

1 file changed

+50
-21
lines changed

1 file changed

+50
-21
lines changed

pallets/subtensor/src/tests/epoch.rs

+50-21
Original file line numberDiff line numberDiff line change
@@ -982,14 +982,36 @@ fn test_512_graph_random_weights() {
982982
// });
983983
// }
984984

985+
fn next_block_no_epoch(netuid: u16) -> u64 {
986+
// high tempo to skip automatic epochs in on_initialize
987+
let high_tempo: u16 = u16::MAX - 1;
988+
let old_tempo: u16 = SubtensorModule::get_tempo(netuid);
989+
990+
SubtensorModule::set_tempo(netuid, high_tempo);
991+
let new_block = next_block();
992+
SubtensorModule::set_tempo(netuid, old_tempo);
993+
994+
new_block
995+
}
996+
997+
fn run_to_block_no_epoch(netuid: u16, n: u64) {
998+
// high tempo to skip automatic epochs in on_initialize
999+
let high_tempo: u16 = u16::MAX - 1;
1000+
let old_tempo: u16 = SubtensorModule::get_tempo(netuid);
1001+
1002+
SubtensorModule::set_tempo(netuid, high_tempo);
1003+
run_to_block(n);
1004+
SubtensorModule::set_tempo(netuid, old_tempo);
1005+
}
1006+
9851007
// Test bonds exponential moving average over a sequence of epochs.
9861008
#[test]
9871009
fn test_bonds() {
9881010
new_test_ext(1).execute_with(|| {
9891011
let sparse: bool = true;
9901012
let n: u16 = 8;
9911013
let netuid: u16 = 1;
992-
let tempo: u16 = u16::MAX - 1; // high tempo to skip automatic epochs in on_initialize, use manual epochs instead
1014+
let tempo: u16 = 1;
9931015
let max_stake: u64 = 4;
9941016
let stakes: Vec<u64> = vec![1, 2, 3, 4, 0, 0, 0, 0];
9951017
let block_number = System::block_number();
@@ -1018,7 +1040,7 @@ fn test_bonds() {
10181040
SubtensorModule::set_max_allowed_validators(netuid, n);
10191041
assert_eq!( SubtensorModule::get_max_allowed_validators(netuid), n);
10201042
SubtensorModule::epoch( netuid, 1_000_000_000 ); // run first epoch to set allowed validators
1021-
next_block(); // run to next block to ensure weights are set on nodes after their registration block
1043+
next_block_no_epoch(netuid); // run to next block to ensure weights are set on nodes after their registration block
10221044

10231045
// === Set weights [val->srv1: 0.1, val->srv2: 0.2, val->srv3: 0.3, val->srv4: 0.4]
10241046
for uid in 0..(n/2) as u64 {
@@ -1068,7 +1090,8 @@ fn test_bonds() {
10681090
// === Set self-weight only on val1
10691091
let uid = 0;
10701092
assert_ok!(SubtensorModule::set_weights(RuntimeOrigin::signed(U256::from(uid)), netuid, vec![uid], vec![u16::MAX], 0));
1071-
next_block();
1093+
next_block_no_epoch(netuid);
1094+
10721095
if sparse { SubtensorModule::epoch( netuid, 1_000_000_000 ); }
10731096
else { SubtensorModule::epoch_dense( netuid, 1_000_000_000 ); }
10741097
/* n: 8
@@ -1115,7 +1138,8 @@ fn test_bonds() {
11151138
// === Set self-weight only on val2
11161139
let uid = 1;
11171140
assert_ok!(SubtensorModule::set_weights(RuntimeOrigin::signed(U256::from(uid)), netuid, vec![uid], vec![u16::MAX], 0));
1118-
next_block();
1141+
next_block_no_epoch(netuid);
1142+
11191143
if sparse { SubtensorModule::epoch( netuid, 1_000_000_000 ); }
11201144
else { SubtensorModule::epoch_dense( netuid, 1_000_000_000 ); }
11211145
/* current_block: 3
@@ -1151,7 +1175,8 @@ fn test_bonds() {
11511175
// === Set self-weight only on val3
11521176
let uid = 2;
11531177
assert_ok!(SubtensorModule::set_weights(RuntimeOrigin::signed(U256::from(uid)), netuid, vec![uid], vec![u16::MAX], 0));
1154-
next_block();
1178+
next_block_no_epoch(netuid);
1179+
11551180
if sparse { SubtensorModule::epoch( netuid, 1_000_000_000 ); }
11561181
else { SubtensorModule::epoch_dense( netuid, 1_000_000_000 ); }
11571182
/* current_block: 4
@@ -1186,7 +1211,8 @@ fn test_bonds() {
11861211

11871212
// === Set val3->srv4: 1
11881213
assert_ok!(SubtensorModule::set_weights(RuntimeOrigin::signed(U256::from(2)), netuid, vec![7], vec![u16::MAX], 0));
1189-
next_block();
1214+
next_block_no_epoch(netuid);
1215+
11901216
if sparse { SubtensorModule::epoch( netuid, 1_000_000_000 ); }
11911217
else { SubtensorModule::epoch_dense( netuid, 1_000_000_000 ); }
11921218
/* current_block: 5
@@ -1219,7 +1245,8 @@ fn test_bonds() {
12191245
assert_eq!(bonds[2][7], 49150);
12201246
assert_eq!(bonds[3][7], 65535);
12211247

1222-
next_block();
1248+
next_block_no_epoch(netuid);
1249+
12231250
if sparse { SubtensorModule::epoch( netuid, 1_000_000_000 ); }
12241251
else { SubtensorModule::epoch_dense( netuid, 1_000_000_000 ); }
12251252
/* current_block: 6
@@ -1240,7 +1267,8 @@ fn test_bonds() {
12401267
assert_eq!(bonds[2][7], 49150);
12411268
assert_eq!(bonds[3][7], 65535);
12421269

1243-
next_block();
1270+
next_block_no_epoch(netuid);
1271+
12441272
if sparse { SubtensorModule::epoch( netuid, 1_000_000_000 ); }
12451273
else { SubtensorModule::epoch_dense( netuid, 1_000_000_000 ); }
12461274
/* current_block: 7
@@ -1261,7 +1289,8 @@ fn test_bonds() {
12611289
assert_eq!(bonds[2][7], 49150);
12621290
assert_eq!(bonds[3][7], 65535);
12631291

1264-
next_block();
1292+
next_block_no_epoch(netuid);
1293+
12651294
if sparse { SubtensorModule::epoch( netuid, 1_000_000_000 ); }
12661295
else { SubtensorModule::epoch_dense( netuid, 1_000_000_000 ); }
12671296
/* current_block: 8
@@ -1286,7 +1315,7 @@ fn test_bonds_with_liquid_alpha() {
12861315
let sparse: bool = true;
12871316
let n: u16 = 8;
12881317
let netuid: u16 = 1;
1289-
let tempo: u16 = u16::MAX - 1; // high tempo to skip automatic epochs in on_initialize, use manual epochs instead
1318+
let tempo: u16 = 1;
12901319
let max_stake: u64 = 4;
12911320
let stakes: Vec<u64> = vec![1, 2, 3, 4, 0, 0, 0, 0];
12921321
let block_number = System::block_number();
@@ -1326,7 +1355,7 @@ fn test_bonds_with_liquid_alpha() {
13261355

13271356
// Initilize with first epoch
13281357
SubtensorModule::epoch(netuid, 1_000_000_000);
1329-
next_block();
1358+
next_block_no_epoch(netuid);
13301359

13311360
// Set weights
13321361
for uid in 0..(n / 2) {
@@ -1417,7 +1446,7 @@ fn test_bonds_with_liquid_alpha() {
14171446
vec![u16::MAX],
14181447
0
14191448
));
1420-
next_block();
1449+
next_block_no_epoch(netuid);
14211450
if sparse {
14221451
SubtensorModule::epoch(netuid, 1_000_000_000);
14231452
} else {
@@ -1439,7 +1468,7 @@ fn test_bonds_with_liquid_alpha() {
14391468
vec![u16::MAX],
14401469
0
14411470
));
1442-
next_block();
1471+
next_block_no_epoch(netuid);
14431472
if sparse {
14441473
SubtensorModule::epoch(netuid, 1_000_000_000);
14451474
} else {
@@ -1543,7 +1572,7 @@ fn test_active_stake() {
15431572
let sparse: bool = true;
15441573
let n: u16 = 4;
15451574
let netuid: u16 = 1;
1546-
let tempo: u16 = u16::MAX - 1; // high tempo to skip automatic epochs in on_initialize, use manual epochs instead
1575+
let tempo: u16 = 1;
15471576
let block_number: u64 = System::block_number();
15481577
let stake: u64 = 1;
15491578
add_network(netuid, tempo, 0);
@@ -1586,7 +1615,7 @@ fn test_active_stake() {
15861615
SubtensorModule::set_max_allowed_validators(netuid, n);
15871616
assert_eq!(SubtensorModule::get_max_allowed_validators(netuid), n);
15881617
SubtensorModule::epoch(netuid, 1_000_000_000); // run first epoch to set allowed validators
1589-
next_block(); // run to next block to ensure weights are set on nodes after their registration block
1618+
next_block_no_epoch(netuid); // run to next block to ensure weights are set on nodes after their registration block
15901619

15911620
// === Set weights [val1->srv1: 0.5, val1->srv2: 0.5, val2->srv1: 0.5, val2->srv2: 0.5]
15921621
for uid in 0..(n / 2) as u64 {
@@ -1627,7 +1656,7 @@ fn test_active_stake() {
16271656
}
16281657
}
16291658
let activity_cutoff: u64 = SubtensorModule::get_activity_cutoff(netuid) as u64;
1630-
run_to_block(activity_cutoff + 2); // run to block where validator (uid 0, 1) weights become outdated
1659+
run_to_block_no_epoch(netuid, activity_cutoff + 2); // run to block where validator (uid 0, 1) weights become outdated
16311660

16321661
// === Update uid 0 weights
16331662
assert_ok!(SubtensorModule::set_weights(
@@ -1697,7 +1726,7 @@ fn test_active_stake() {
16971726
vec![u16::MAX / (n / 2); (n / 2) as usize],
16981727
0
16991728
));
1700-
run_to_block(activity_cutoff + 3); // run to block where validator (uid 0, 1) weights become outdated
1729+
run_to_block_no_epoch(netuid, activity_cutoff + 3); // run to block where validator (uid 0, 1) weights become outdated
17011730
if sparse {
17021731
SubtensorModule::epoch(netuid, 1_000_000_000);
17031732
} else {
@@ -1750,7 +1779,7 @@ fn test_outdated_weights() {
17501779
let sparse: bool = true;
17511780
let n: u16 = 4;
17521781
let netuid: u16 = 1;
1753-
let tempo: u16 = u16::MAX - 1; // high tempo to skip automatic epochs in on_initialize, use manual epochs instead
1782+
let tempo: u16 = 0;
17541783
let mut block_number: u64 = System::block_number();
17551784
let stake: u64 = 1;
17561785
add_network(netuid, tempo, 0);
@@ -1796,7 +1825,7 @@ fn test_outdated_weights() {
17961825
assert_eq!(SubtensorModule::get_max_allowed_validators(netuid), n);
17971826
SubtensorModule::epoch(netuid, 1_000_000_000); // run first epoch to set allowed validators
17981827
assert_eq!(SubtensorModule::get_registrations_this_block(netuid), 4);
1799-
block_number = next_block(); // run to next block to ensure weights are set on nodes after their registration block
1828+
block_number = next_block_no_epoch(netuid); // run to next block to ensure weights are set on nodes after their registration block
18001829
assert_eq!(SubtensorModule::get_registrations_this_block(netuid), 0);
18011830

18021831
// === Set weights [val1->srv1: 2/3, val1->srv2: 1/3, val2->srv1: 2/3, val2->srv2: 1/3, srv1->srv1: 1, srv2->srv2: 1]
@@ -1877,7 +1906,7 @@ fn test_outdated_weights() {
18771906
SubtensorModule::get_hotkey_for_net_and_uid(netuid, deregistered_uid)
18781907
.expect("Not registered")
18791908
);
1880-
next_block(); // run to next block to outdate weights and bonds set on deregistered uid
1909+
next_block_no_epoch(netuid); // run to next block to outdate weights and bonds set on deregistered uid
18811910

18821911
// === Update weights from only uid=0
18831912
assert_ok!(SubtensorModule::set_weights(
@@ -2290,7 +2319,7 @@ fn test_deregistered_miner_bonds() {
22902319
// We expect the old bonds for server2, (uid3), to be reset.
22912320
// For server1, (uid2), the bond should be higher than before.
22922321
assert!(
2293-
bond_0_2_new > bond_0_2,
2322+
bond_0_2_new >= bond_0_2,
22942323
"bond_0_2_new: {}, bond_0_2: {}",
22952324
bond_0_2_new,
22962325
bond_0_2

0 commit comments

Comments
 (0)