Skip to content

Commit d91dc3c

Browse files
authored
Fix/run set pending children on epoch (#1171)
* add back running set-pending-children * add test * move to step block * commit Cargo.lock * use mock sched children at end of loop instead * set reg diff before try reg again * oops, add netuid * fix tests using add network tempo 0 -> 1 * chore: fmt * only wait blocks after schedule all children for tests * fix test: wip * charge fee for swap and add for tests * wip * chore: clippy * chore: fmt * use get_dynamic_tao_emission for testing emission * spec bump
1 parent 357a9e1 commit d91dc3c

File tree

11 files changed

+225
-76
lines changed

11 files changed

+225
-76
lines changed

pallets/subtensor/src/coinbase/block_step.rs

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,10 +14,24 @@ impl<T: Config + pallet_drand::Config> Pallet<T> {
1414
log::debug!("Block emission: {:?}", block_emission);
1515
// --- 3. Run emission through network.
1616
Self::run_coinbase(block_emission);
17+
18+
// --- 4. Set pending children on the epoch; but only after the coinbase has been run.
19+
Self::try_set_pending_children(block_number);
20+
1721
// Return ok.
1822
Ok(())
1923
}
2024

25+
fn try_set_pending_children(block_number: u64) {
26+
let subnets: Vec<u16> = Self::get_all_subnet_netuids();
27+
for &netuid in subnets.iter() {
28+
if Self::should_run_epoch(netuid, block_number) {
29+
// Set pending children on the epoch.
30+
Self::do_set_pending_children(netuid);
31+
}
32+
}
33+
}
34+
2135
/// Adjusts the network difficulties/burns of every active network. Resetting state parameters.
2236
///
2337
pub fn adjust_registration_terms_for_networks() {

pallets/subtensor/src/tests/children.rs

Lines changed: 154 additions & 37 deletions
Large diffs are not rendered by default.

pallets/subtensor/src/tests/epoch.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -493,7 +493,7 @@ fn init_run_epochs(
493493
// new_test_ext(1).execute_with(|| {
494494
// log::info!("test_overflow:");
495495
// let netuid: u16 = 1;
496-
// add_network(netuid, 0, 0);
496+
// add_network(netuid, 1, 0);
497497
// SubtensorModule::set_max_allowed_uids(netuid, 3);
498498
// SubtensorModule::increase_stake_on_coldkey_hotkey_account(
499499
// &U256::from(0),

pallets/subtensor/src/tests/mock.rs

Lines changed: 20 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -601,6 +601,7 @@ pub(crate) fn step_epochs(count: u16, netuid: u16) {
601601
SubtensorModule::get_tempo(netuid),
602602
SubtensorModule::get_current_block_as_u64(),
603603
);
604+
log::info!("Blocks to next epoch: {:?}", blocks_to_next_epoch);
604605
step_block(blocks_to_next_epoch as u16);
605606

606607
assert!(SubtensorModule::should_run_epoch(
@@ -686,16 +687,28 @@ pub fn setup_neuron_with_stake(netuid: u16, hotkey: U256, coldkey: U256, stake:
686687
increase_stake_on_coldkey_hotkey_account(&coldkey, &hotkey, stake, netuid);
687688
}
688689

690+
#[allow(dead_code)]
691+
pub fn wait_set_pending_children_cooldown(netuid: u16) {
692+
let cooldown = DefaultPendingCooldown::<Test>::get();
693+
step_block(cooldown as u16); // Wait for cooldown to pass
694+
step_epochs(1, netuid); // Run next epoch
695+
}
696+
689697
#[allow(dead_code)]
690698
pub fn wait_and_set_pending_children(netuid: u16) {
691699
let original_block = System::block_number();
692-
System::set_block_number(System::block_number() + 7300);
700+
wait_set_pending_children_cooldown(netuid);
693701
SubtensorModule::do_set_pending_children(netuid);
694702
System::set_block_number(original_block);
695703
}
696704

697705
#[allow(dead_code)]
698-
pub fn mock_set_children(coldkey: &U256, parent: &U256, netuid: u16, child_vec: &[(u64, U256)]) {
706+
pub fn mock_schedule_children(
707+
coldkey: &U256,
708+
parent: &U256,
709+
netuid: u16,
710+
child_vec: &[(u64, U256)],
711+
) {
699712
// Set minimum stake for setting children
700713
StakeThreshold::<Test>::put(0);
701714

@@ -706,6 +719,11 @@ pub fn mock_set_children(coldkey: &U256, parent: &U256, netuid: u16, child_vec:
706719
netuid,
707720
child_vec.to_vec()
708721
));
722+
}
723+
724+
#[allow(dead_code)]
725+
pub fn mock_set_children(coldkey: &U256, parent: &U256, netuid: u16, child_vec: &[(u64, U256)]) {
726+
mock_schedule_children(coldkey, parent, netuid, child_vec);
709727
wait_and_set_pending_children(netuid);
710728
}
711729

pallets/subtensor/src/tests/move_stake.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -242,7 +242,7 @@ fn test_do_move_nonexistent_destination_hotkey() {
242242
SubtensorModule::stake_into_subnet(&origin_hotkey, &coldkey, netuid, stake_amount, fee);
243243

244244
// Attempt to move stake from a non-existent origin hotkey
245-
add_network(netuid, 0, 0);
245+
add_network(netuid, 1, 0);
246246
assert_noop!(
247247
SubtensorModule::do_move_stake(
248248
RuntimeOrigin::signed(coldkey),
@@ -523,7 +523,7 @@ fn test_do_move_wrong_origin() {
523523
);
524524

525525
// Attempt to move stake with wrong origin
526-
add_network(netuid, 0, 0);
526+
add_network(netuid, 1, 0);
527527
SubtensorModule::create_account_if_non_existent(&coldkey, &origin_hotkey);
528528
SubtensorModule::create_account_if_non_existent(&coldkey, &destination_hotkey);
529529
assert_err!(

pallets/subtensor/src/tests/registration.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1211,7 +1211,7 @@ fn test_registration_get_uid_to_prune_all_in_immunity_period() {
12111211
new_test_ext(1).execute_with(|| {
12121212
System::set_block_number(0);
12131213
let netuid: u16 = 1;
1214-
add_network(netuid, 0, 0);
1214+
add_network(netuid, 1, 0);
12151215
log::info!("add network");
12161216
register_ok_neuron(netuid, U256::from(0), U256::from(0), 39420842);
12171217
register_ok_neuron(netuid, U256::from(1), U256::from(1), 12412392);
@@ -1235,7 +1235,7 @@ fn test_registration_get_uid_to_prune_none_in_immunity_period() {
12351235
new_test_ext(1).execute_with(|| {
12361236
System::set_block_number(0);
12371237
let netuid: u16 = 1;
1238-
add_network(netuid, 0, 0);
1238+
add_network(netuid, 1, 0);
12391239
log::info!("add network");
12401240
register_ok_neuron(netuid, U256::from(0), U256::from(0), 39420842);
12411241
register_ok_neuron(netuid, U256::from(1), U256::from(1), 12412392);

pallets/subtensor/src/tests/senate.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -473,7 +473,7 @@ fn test_senate_leave_vote_removal() {
473473
// Add two networks.
474474
let root_netuid: u16 = 0;
475475
let other_netuid: u16 = 5;
476-
add_network(other_netuid, 0, 0);
476+
add_network(other_netuid, 1, 0);
477477
SubtensorModule::set_burn(other_netuid, 0);
478478
SubtensorModule::set_max_registrations_per_block(other_netuid, 1000);
479479
SubtensorModule::set_target_registrations_per_interval(other_netuid, 1000);

pallets/subtensor/src/tests/staking.rs

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1295,7 +1295,7 @@ fn test_delegate_take_can_be_decreased() {
12951295

12961296
// Register the neuron to a new network
12971297
let netuid = 1;
1298-
add_network(netuid, 0, 0);
1298+
add_network(netuid, 1, 0);
12991299
register_ok_neuron(netuid, hotkey0, coldkey0, 124124);
13001300

13011301
// Coldkey / hotkey 0 become delegates with 9% take
@@ -1330,7 +1330,7 @@ fn test_can_set_min_take_ok() {
13301330

13311331
// Register the neuron to a new network
13321332
let netuid = 1;
1333-
add_network(netuid, 0, 0);
1333+
add_network(netuid, 1, 0);
13341334
register_ok_neuron(netuid, hotkey0, coldkey0, 124124);
13351335

13361336
// Coldkey / hotkey 0 become delegates
@@ -1362,7 +1362,7 @@ fn test_delegate_take_can_not_be_increased_with_decrease_take() {
13621362

13631363
// Register the neuron to a new network
13641364
let netuid = 1;
1365-
add_network(netuid, 0, 0);
1365+
add_network(netuid, 1, 0);
13661366
register_ok_neuron(netuid, hotkey0, coldkey0, 124124);
13671367

13681368
// Set min take
@@ -1397,7 +1397,7 @@ fn test_delegate_take_can_be_increased() {
13971397

13981398
// Register the neuron to a new network
13991399
let netuid = 1;
1400-
add_network(netuid, 0, 0);
1400+
add_network(netuid, 1, 0);
14011401
register_ok_neuron(netuid, hotkey0, coldkey0, 124124);
14021402

14031403
// Coldkey / hotkey 0 become delegates with 9% take
@@ -1432,7 +1432,7 @@ fn test_delegate_take_can_not_be_decreased_with_increase_take() {
14321432

14331433
// Register the neuron to a new network
14341434
let netuid = 1;
1435-
add_network(netuid, 0, 0);
1435+
add_network(netuid, 1, 0);
14361436
register_ok_neuron(netuid, hotkey0, coldkey0, 124124);
14371437

14381438
// Coldkey / hotkey 0 become delegates with 9% take
@@ -1471,7 +1471,7 @@ fn test_delegate_take_can_be_increased_to_limit() {
14711471

14721472
// Register the neuron to a new network
14731473
let netuid = 1;
1474-
add_network(netuid, 0, 0);
1474+
add_network(netuid, 1, 0);
14751475
register_ok_neuron(netuid, hotkey0, coldkey0, 124124);
14761476

14771477
// Coldkey / hotkey 0 become delegates with 9% take
@@ -1509,7 +1509,7 @@ fn test_delegate_take_can_not_be_increased_beyond_limit() {
15091509

15101510
// Register the neuron to a new network
15111511
let netuid = 1;
1512-
add_network(netuid, 0, 0);
1512+
add_network(netuid, 1, 0);
15131513
register_ok_neuron(netuid, hotkey0, coldkey0, 124124);
15141514

15151515
// Coldkey / hotkey 0 become delegates with 9% take
@@ -1551,7 +1551,7 @@ fn test_rate_limits_enforced_on_increase_take() {
15511551

15521552
// Register the neuron to a new network
15531553
let netuid = 1;
1554-
add_network(netuid, 0, 0);
1554+
add_network(netuid, 1, 0);
15551555
register_ok_neuron(netuid, hotkey0, coldkey0, 124124);
15561556

15571557
// Coldkey / hotkey 0 become delegates with 9% take
@@ -1678,7 +1678,7 @@ fn test_get_total_delegated_stake_no_delegations() {
16781678
let coldkey = U256::from(2);
16791679
let netuid = 1u16;
16801680

1681-
add_network(netuid, 0, 0);
1681+
add_network(netuid, 1, 0);
16821682
register_ok_neuron(netuid, delegate, coldkey, 0);
16831683

16841684
// Check that there's no delegated stake

pallets/subtensor/src/tests/swap_hotkey.rs

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -222,7 +222,7 @@ fn test_swap_subnet_membership() {
222222
let netuid = 0u16;
223223
let mut weight = Weight::zero();
224224

225-
add_network(netuid, 0, 1);
225+
add_network(netuid, 1, 1);
226226
IsNetworkMember::<Test>::insert(old_hotkey, netuid, true);
227227
assert_ok!(SubtensorModule::perform_hotkey_swap(
228228
&old_hotkey,
@@ -247,7 +247,7 @@ fn test_swap_uids_and_keys() {
247247
let uid = 5u16;
248248
let mut weight = Weight::zero();
249249

250-
add_network(netuid, 0, 1);
250+
add_network(netuid, 1, 1);
251251
IsNetworkMember::<Test>::insert(old_hotkey, netuid, true);
252252
Uids::<Test>::insert(netuid, old_hotkey, uid);
253253
Keys::<Test>::insert(netuid, uid, old_hotkey);
@@ -276,7 +276,7 @@ fn test_swap_prometheus() {
276276
let prometheus_info = PrometheusInfo::default();
277277
let mut weight = Weight::zero();
278278

279-
add_network(netuid, 0, 1);
279+
add_network(netuid, 1, 1);
280280
IsNetworkMember::<Test>::insert(old_hotkey, netuid, true);
281281
Prometheus::<Test>::insert(netuid, old_hotkey, prometheus_info.clone());
282282

@@ -306,7 +306,7 @@ fn test_swap_axons() {
306306
let axon_info = AxonInfo::default();
307307
let mut weight = Weight::zero();
308308

309-
add_network(netuid, 0, 1);
309+
add_network(netuid, 1, 1);
310310
IsNetworkMember::<Test>::insert(old_hotkey, netuid, true);
311311
Axons::<Test>::insert(netuid, old_hotkey, axon_info.clone());
312312

@@ -333,7 +333,7 @@ fn test_swap_certificates() {
333333
let certificate = NeuronCertificate::try_from(vec![1, 2, 3]).unwrap();
334334
let mut weight = Weight::zero();
335335

336-
add_network(netuid, 0, 1);
336+
add_network(netuid, 1, 1);
337337
IsNetworkMember::<Test>::insert(old_hotkey, netuid, true);
338338
NeuronCertificates::<Test>::insert(netuid, old_hotkey, certificate.clone());
339339

@@ -366,7 +366,7 @@ fn test_swap_weight_commits() {
366366
weight_commits.push_back((H256::from_low_u64_be(100), 200, 1, 1));
367367
let mut weight = Weight::zero();
368368

369-
add_network(netuid, 0, 1);
369+
add_network(netuid, 1, 1);
370370
IsNetworkMember::<Test>::insert(old_hotkey, netuid, true);
371371
WeightCommits::<Test>::insert(netuid, old_hotkey, weight_commits.clone());
372372

@@ -397,7 +397,7 @@ fn test_swap_loaded_emission() {
397397
let validator_emission = 1000u64;
398398
let mut weight = Weight::zero();
399399

400-
add_network(netuid, 0, 1);
400+
add_network(netuid, 1, 1);
401401
IsNetworkMember::<Test>::insert(old_hotkey, netuid, true);
402402
LoadedEmission::<Test>::insert(
403403
netuid,
@@ -537,8 +537,8 @@ fn test_swap_hotkey_with_multiple_subnets() {
537537
let netuid2 = 1;
538538
let mut weight = Weight::zero();
539539

540-
add_network(netuid1, 0, 1);
541-
add_network(netuid2, 0, 1);
540+
add_network(netuid1, 1, 1);
541+
add_network(netuid2, 1, 1);
542542
IsNetworkMember::<Test>::insert(old_hotkey, netuid1, true);
543543
IsNetworkMember::<Test>::insert(old_hotkey, netuid2, true);
544544

@@ -639,8 +639,8 @@ fn test_swap_hotkey_with_multiple_coldkeys_and_subnets() {
639639
let mut weight = Weight::zero();
640640

641641
// Set up initial state
642-
add_network(netuid1, 0, 1);
643-
add_network(netuid2, 0, 1);
642+
add_network(netuid1, 1, 1);
643+
add_network(netuid2, 1, 1);
644644
register_ok_neuron(netuid1, old_hotkey, coldkey1, 1234);
645645
register_ok_neuron(netuid2, old_hotkey, coldkey1, 1234);
646646

@@ -1258,7 +1258,7 @@ fn test_swap_parent_hotkey_childkey_maps() {
12581258
let coldkey = U256::from(2);
12591259
let child = U256::from(3);
12601260
let parent_new = U256::from(4);
1261-
add_network(netuid, 0, 0);
1261+
add_network(netuid, 1, 0);
12621262
SubtensorModule::create_account_if_non_existent(&coldkey, &parent_old);
12631263

12641264
// Set child and verify state maps
@@ -1301,7 +1301,7 @@ fn test_swap_child_hotkey_childkey_maps() {
13011301
let coldkey = U256::from(2);
13021302
let child_old = U256::from(3);
13031303
let child_new = U256::from(4);
1304-
add_network(netuid, 0, 0);
1304+
add_network(netuid, 1, 0);
13051305
SubtensorModule::create_account_if_non_existent(&coldkey, &child_old);
13061306
SubtensorModule::create_account_if_non_existent(&coldkey, &parent);
13071307

pallets/subtensor/src/tests/weights.rs

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -82,7 +82,7 @@ fn test_set_rootweights_validate() {
8282
});
8383

8484
// Create netuid
85-
add_network(netuid, 0, 0);
85+
add_network(netuid, 1, 0);
8686
// Register the hotkey
8787
SubtensorModule::append_neuron(netuid, &hotkey, 0);
8888
crate::Owner::<Test>::insert(hotkey, coldkey);
@@ -197,7 +197,7 @@ fn test_commit_weights_validate() {
197197
});
198198

199199
// Create netuid
200-
add_network(netuid, 0, 0);
200+
add_network(netuid, 1, 0);
201201
// Register the hotkey
202202
SubtensorModule::append_neuron(netuid, &hotkey, 0);
203203
crate::Owner::<Test>::insert(hotkey, coldkey);
@@ -306,7 +306,7 @@ fn test_set_weights_validate() {
306306
});
307307

308308
// Create netuid
309-
add_network(netuid, 0, 0);
309+
add_network(netuid, 1, 0);
310310
// Register the hotkey
311311
SubtensorModule::append_neuron(netuid, &hotkey, 0);
312312
crate::Owner::<Test>::insert(hotkey, coldkey);
@@ -380,7 +380,7 @@ fn test_reveal_weights_validate() {
380380
});
381381

382382
// Create netuid
383-
add_network(netuid, 0, 0);
383+
add_network(netuid, 1, 0);
384384
// Register the hotkey
385385
SubtensorModule::append_neuron(netuid, &hotkey, 0);
386386
crate::Owner::<Test>::insert(hotkey, coldkey);
@@ -521,7 +521,7 @@ fn test_set_stake_threshold_failed() {
521521
let hotkey = U256::from(0);
522522
let coldkey = U256::from(0);
523523

524-
add_network(netuid, 0, 0);
524+
add_network(netuid, 1, 0);
525525
register_ok_neuron(netuid, hotkey, coldkey, 2143124);
526526
SubtensorModule::set_stake_threshold(20_000_000_000_000);
527527
SubtensorModule::add_balance_to_coldkey_account(&hotkey, u64::MAX);
@@ -583,8 +583,8 @@ fn test_weights_version_key() {
583583
let netuid0: u16 = 1;
584584
let netuid1: u16 = 2;
585585

586-
add_network(netuid0, 0, 0);
587-
add_network(netuid1, 0, 0);
586+
add_network(netuid0, 1, 0);
587+
add_network(netuid1, 1, 0);
588588
register_ok_neuron(netuid0, hotkey, coldkey, 2143124);
589589
register_ok_neuron(netuid1, hotkey, coldkey, 3124124);
590590

@@ -1421,7 +1421,7 @@ fn test_check_len_uids_within_allowed_not_within_network_pool() {
14211421
fn test_set_weights_commit_reveal_enabled_error() {
14221422
new_test_ext(0).execute_with(|| {
14231423
let netuid: u16 = 1;
1424-
add_network(netuid, 0, 0);
1424+
add_network(netuid, 1, 0);
14251425
register_ok_neuron(netuid, U256::from(1), U256::from(2), 10);
14261426

14271427
let uids = vec![0];

0 commit comments

Comments
 (0)