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

Fix/run set pending children on epoch #1171

Merged
merged 20 commits into from
Jan 24, 2025
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
14 changes: 14 additions & 0 deletions pallets/subtensor/src/coinbase/block_step.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,24 @@ impl<T: Config + pallet_drand::Config> Pallet<T> {
log::debug!("Block emission: {:?}", block_emission);
// --- 3. Run emission through network.
Self::run_coinbase(block_emission);

// --- 4. Set pending children on the epoch; but only after the coinbase has been run.
Self::try_set_pending_children(block_number);

// Return ok.
Ok(())
}

fn try_set_pending_children(block_number: u64) {
let subnets: Vec<u16> = Self::get_all_subnet_netuids();
for &netuid in subnets.iter() {
if Self::should_run_epoch(netuid, block_number) {
// Set pending children on the epoch.
Self::do_set_pending_children(netuid);
}
}
}

/// Adjusts the network difficulties/burns of every active network. Resetting state parameters.
///
pub fn adjust_registration_terms_for_networks() {
Expand Down
191 changes: 154 additions & 37 deletions pallets/subtensor/src/tests/children.rs

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion pallets/subtensor/src/tests/epoch.rs
Original file line number Diff line number Diff line change
Expand Up @@ -493,7 +493,7 @@ fn init_run_epochs(
// new_test_ext(1).execute_with(|| {
// log::info!("test_overflow:");
// let netuid: u16 = 1;
// add_network(netuid, 0, 0);
// add_network(netuid, 1, 0);
// SubtensorModule::set_max_allowed_uids(netuid, 3);
// SubtensorModule::increase_stake_on_coldkey_hotkey_account(
// &U256::from(0),
Expand Down
22 changes: 20 additions & 2 deletions pallets/subtensor/src/tests/mock.rs
Original file line number Diff line number Diff line change
Expand Up @@ -601,6 +601,7 @@ pub(crate) fn step_epochs(count: u16, netuid: u16) {
SubtensorModule::get_tempo(netuid),
SubtensorModule::get_current_block_as_u64(),
);
log::info!("Blocks to next epoch: {:?}", blocks_to_next_epoch);
step_block(blocks_to_next_epoch as u16);

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

#[allow(dead_code)]
pub fn wait_set_pending_children_cooldown(netuid: u16) {
let cooldown = DefaultPendingCooldown::<Test>::get();
step_block(cooldown as u16); // Wait for cooldown to pass
step_epochs(1, netuid); // Run next epoch
}

#[allow(dead_code)]
pub fn wait_and_set_pending_children(netuid: u16) {
let original_block = System::block_number();
System::set_block_number(System::block_number() + 7300);
wait_set_pending_children_cooldown(netuid);
SubtensorModule::do_set_pending_children(netuid);
System::set_block_number(original_block);
}

#[allow(dead_code)]
pub fn mock_set_children(coldkey: &U256, parent: &U256, netuid: u16, child_vec: &[(u64, U256)]) {
pub fn mock_schedule_children(
coldkey: &U256,
parent: &U256,
netuid: u16,
child_vec: &[(u64, U256)],
) {
// Set minimum stake for setting children
StakeThreshold::<Test>::put(0);

Expand All @@ -706,6 +719,11 @@ pub fn mock_set_children(coldkey: &U256, parent: &U256, netuid: u16, child_vec:
netuid,
child_vec.to_vec()
));
}

#[allow(dead_code)]
pub fn mock_set_children(coldkey: &U256, parent: &U256, netuid: u16, child_vec: &[(u64, U256)]) {
mock_schedule_children(coldkey, parent, netuid, child_vec);
wait_and_set_pending_children(netuid);
}

Expand Down
4 changes: 2 additions & 2 deletions pallets/subtensor/src/tests/move_stake.rs
Original file line number Diff line number Diff line change
Expand Up @@ -242,7 +242,7 @@ fn test_do_move_nonexistent_destination_hotkey() {
SubtensorModule::stake_into_subnet(&origin_hotkey, &coldkey, netuid, stake_amount, fee);

// Attempt to move stake from a non-existent origin hotkey
add_network(netuid, 0, 0);
add_network(netuid, 1, 0);
assert_noop!(
SubtensorModule::do_move_stake(
RuntimeOrigin::signed(coldkey),
Expand Down Expand Up @@ -523,7 +523,7 @@ fn test_do_move_wrong_origin() {
);

// Attempt to move stake with wrong origin
add_network(netuid, 0, 0);
add_network(netuid, 1, 0);
SubtensorModule::create_account_if_non_existent(&coldkey, &origin_hotkey);
SubtensorModule::create_account_if_non_existent(&coldkey, &destination_hotkey);
assert_err!(
Expand Down
4 changes: 2 additions & 2 deletions pallets/subtensor/src/tests/registration.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1211,7 +1211,7 @@ fn test_registration_get_uid_to_prune_all_in_immunity_period() {
new_test_ext(1).execute_with(|| {
System::set_block_number(0);
let netuid: u16 = 1;
add_network(netuid, 0, 0);
add_network(netuid, 1, 0);
log::info!("add network");
register_ok_neuron(netuid, U256::from(0), U256::from(0), 39420842);
register_ok_neuron(netuid, U256::from(1), U256::from(1), 12412392);
Expand All @@ -1235,7 +1235,7 @@ fn test_registration_get_uid_to_prune_none_in_immunity_period() {
new_test_ext(1).execute_with(|| {
System::set_block_number(0);
let netuid: u16 = 1;
add_network(netuid, 0, 0);
add_network(netuid, 1, 0);
log::info!("add network");
register_ok_neuron(netuid, U256::from(0), U256::from(0), 39420842);
register_ok_neuron(netuid, U256::from(1), U256::from(1), 12412392);
Expand Down
2 changes: 1 addition & 1 deletion pallets/subtensor/src/tests/senate.rs
Original file line number Diff line number Diff line change
Expand Up @@ -473,7 +473,7 @@ fn test_senate_leave_vote_removal() {
// Add two networks.
let root_netuid: u16 = 0;
let other_netuid: u16 = 5;
add_network(other_netuid, 0, 0);
add_network(other_netuid, 1, 0);
SubtensorModule::set_burn(other_netuid, 0);
SubtensorModule::set_max_registrations_per_block(other_netuid, 1000);
SubtensorModule::set_target_registrations_per_interval(other_netuid, 1000);
Expand Down
18 changes: 9 additions & 9 deletions pallets/subtensor/src/tests/staking.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1295,7 +1295,7 @@ fn test_delegate_take_can_be_decreased() {

// Register the neuron to a new network
let netuid = 1;
add_network(netuid, 0, 0);
add_network(netuid, 1, 0);
register_ok_neuron(netuid, hotkey0, coldkey0, 124124);

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

// Register the neuron to a new network
let netuid = 1;
add_network(netuid, 0, 0);
add_network(netuid, 1, 0);
register_ok_neuron(netuid, hotkey0, coldkey0, 124124);

// Coldkey / hotkey 0 become delegates
Expand Down Expand Up @@ -1362,7 +1362,7 @@ fn test_delegate_take_can_not_be_increased_with_decrease_take() {

// Register the neuron to a new network
let netuid = 1;
add_network(netuid, 0, 0);
add_network(netuid, 1, 0);
register_ok_neuron(netuid, hotkey0, coldkey0, 124124);

// Set min take
Expand Down Expand Up @@ -1397,7 +1397,7 @@ fn test_delegate_take_can_be_increased() {

// Register the neuron to a new network
let netuid = 1;
add_network(netuid, 0, 0);
add_network(netuid, 1, 0);
register_ok_neuron(netuid, hotkey0, coldkey0, 124124);

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

// Register the neuron to a new network
let netuid = 1;
add_network(netuid, 0, 0);
add_network(netuid, 1, 0);
register_ok_neuron(netuid, hotkey0, coldkey0, 124124);

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

// Register the neuron to a new network
let netuid = 1;
add_network(netuid, 0, 0);
add_network(netuid, 1, 0);
register_ok_neuron(netuid, hotkey0, coldkey0, 124124);

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

// Register the neuron to a new network
let netuid = 1;
add_network(netuid, 0, 0);
add_network(netuid, 1, 0);
register_ok_neuron(netuid, hotkey0, coldkey0, 124124);

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

// Register the neuron to a new network
let netuid = 1;
add_network(netuid, 0, 0);
add_network(netuid, 1, 0);
register_ok_neuron(netuid, hotkey0, coldkey0, 124124);

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

add_network(netuid, 0, 0);
add_network(netuid, 1, 0);
register_ok_neuron(netuid, delegate, coldkey, 0);

// Check that there's no delegated stake
Expand Down
26 changes: 13 additions & 13 deletions pallets/subtensor/src/tests/swap_hotkey.rs
Original file line number Diff line number Diff line change
Expand Up @@ -222,7 +222,7 @@ fn test_swap_subnet_membership() {
let netuid = 0u16;
let mut weight = Weight::zero();

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

add_network(netuid, 0, 1);
add_network(netuid, 1, 1);
IsNetworkMember::<Test>::insert(old_hotkey, netuid, true);
Uids::<Test>::insert(netuid, old_hotkey, uid);
Keys::<Test>::insert(netuid, uid, old_hotkey);
Expand Down Expand Up @@ -276,7 +276,7 @@ fn test_swap_prometheus() {
let prometheus_info = PrometheusInfo::default();
let mut weight = Weight::zero();

add_network(netuid, 0, 1);
add_network(netuid, 1, 1);
IsNetworkMember::<Test>::insert(old_hotkey, netuid, true);
Prometheus::<Test>::insert(netuid, old_hotkey, prometheus_info.clone());

Expand Down Expand Up @@ -306,7 +306,7 @@ fn test_swap_axons() {
let axon_info = AxonInfo::default();
let mut weight = Weight::zero();

add_network(netuid, 0, 1);
add_network(netuid, 1, 1);
IsNetworkMember::<Test>::insert(old_hotkey, netuid, true);
Axons::<Test>::insert(netuid, old_hotkey, axon_info.clone());

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

add_network(netuid, 0, 1);
add_network(netuid, 1, 1);
IsNetworkMember::<Test>::insert(old_hotkey, netuid, true);
NeuronCertificates::<Test>::insert(netuid, old_hotkey, certificate.clone());

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

add_network(netuid, 0, 1);
add_network(netuid, 1, 1);
IsNetworkMember::<Test>::insert(old_hotkey, netuid, true);
WeightCommits::<Test>::insert(netuid, old_hotkey, weight_commits.clone());

Expand Down Expand Up @@ -397,7 +397,7 @@ fn test_swap_loaded_emission() {
let validator_emission = 1000u64;
let mut weight = Weight::zero();

add_network(netuid, 0, 1);
add_network(netuid, 1, 1);
IsNetworkMember::<Test>::insert(old_hotkey, netuid, true);
LoadedEmission::<Test>::insert(
netuid,
Expand Down Expand Up @@ -537,8 +537,8 @@ fn test_swap_hotkey_with_multiple_subnets() {
let netuid2 = 1;
let mut weight = Weight::zero();

add_network(netuid1, 0, 1);
add_network(netuid2, 0, 1);
add_network(netuid1, 1, 1);
add_network(netuid2, 1, 1);
IsNetworkMember::<Test>::insert(old_hotkey, netuid1, true);
IsNetworkMember::<Test>::insert(old_hotkey, netuid2, true);

Expand Down Expand Up @@ -639,8 +639,8 @@ fn test_swap_hotkey_with_multiple_coldkeys_and_subnets() {
let mut weight = Weight::zero();

// Set up initial state
add_network(netuid1, 0, 1);
add_network(netuid2, 0, 1);
add_network(netuid1, 1, 1);
add_network(netuid2, 1, 1);
register_ok_neuron(netuid1, old_hotkey, coldkey1, 1234);
register_ok_neuron(netuid2, old_hotkey, coldkey1, 1234);

Expand Down Expand Up @@ -1258,7 +1258,7 @@ fn test_swap_parent_hotkey_childkey_maps() {
let coldkey = U256::from(2);
let child = U256::from(3);
let parent_new = U256::from(4);
add_network(netuid, 0, 0);
add_network(netuid, 1, 0);
SubtensorModule::create_account_if_non_existent(&coldkey, &parent_old);

// Set child and verify state maps
Expand Down Expand Up @@ -1301,7 +1301,7 @@ fn test_swap_child_hotkey_childkey_maps() {
let coldkey = U256::from(2);
let child_old = U256::from(3);
let child_new = U256::from(4);
add_network(netuid, 0, 0);
add_network(netuid, 1, 0);
SubtensorModule::create_account_if_non_existent(&coldkey, &child_old);
SubtensorModule::create_account_if_non_existent(&coldkey, &parent);

Expand Down
16 changes: 8 additions & 8 deletions pallets/subtensor/src/tests/weights.rs
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@ fn test_set_rootweights_validate() {
});

// Create netuid
add_network(netuid, 0, 0);
add_network(netuid, 1, 0);
// Register the hotkey
SubtensorModule::append_neuron(netuid, &hotkey, 0);
crate::Owner::<Test>::insert(hotkey, coldkey);
Expand Down Expand Up @@ -197,7 +197,7 @@ fn test_commit_weights_validate() {
});

// Create netuid
add_network(netuid, 0, 0);
add_network(netuid, 1, 0);
// Register the hotkey
SubtensorModule::append_neuron(netuid, &hotkey, 0);
crate::Owner::<Test>::insert(hotkey, coldkey);
Expand Down Expand Up @@ -306,7 +306,7 @@ fn test_set_weights_validate() {
});

// Create netuid
add_network(netuid, 0, 0);
add_network(netuid, 1, 0);
// Register the hotkey
SubtensorModule::append_neuron(netuid, &hotkey, 0);
crate::Owner::<Test>::insert(hotkey, coldkey);
Expand Down Expand Up @@ -380,7 +380,7 @@ fn test_reveal_weights_validate() {
});

// Create netuid
add_network(netuid, 0, 0);
add_network(netuid, 1, 0);
// Register the hotkey
SubtensorModule::append_neuron(netuid, &hotkey, 0);
crate::Owner::<Test>::insert(hotkey, coldkey);
Expand Down Expand Up @@ -521,7 +521,7 @@ fn test_set_stake_threshold_failed() {
let hotkey = U256::from(0);
let coldkey = U256::from(0);

add_network(netuid, 0, 0);
add_network(netuid, 1, 0);
register_ok_neuron(netuid, hotkey, coldkey, 2143124);
SubtensorModule::set_stake_threshold(20_000_000_000_000);
SubtensorModule::add_balance_to_coldkey_account(&hotkey, u64::MAX);
Expand Down Expand Up @@ -583,8 +583,8 @@ fn test_weights_version_key() {
let netuid0: u16 = 1;
let netuid1: u16 = 2;

add_network(netuid0, 0, 0);
add_network(netuid1, 0, 0);
add_network(netuid0, 1, 0);
add_network(netuid1, 1, 0);
register_ok_neuron(netuid0, hotkey, coldkey, 2143124);
register_ok_neuron(netuid1, hotkey, coldkey, 3124124);

Expand Down Expand Up @@ -1421,7 +1421,7 @@ fn test_check_len_uids_within_allowed_not_within_network_pool() {
fn test_set_weights_commit_reveal_enabled_error() {
new_test_ext(0).execute_with(|| {
let netuid: u16 = 1;
add_network(netuid, 0, 0);
add_network(netuid, 1, 0);
register_ok_neuron(netuid, U256::from(1), U256::from(2), 10);

let uids = vec![0];
Expand Down
2 changes: 1 addition & 1 deletion runtime/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -220,7 +220,7 @@ pub const VERSION: RuntimeVersion = RuntimeVersion {
// `spec_version`, and `authoring_version` are the same between Wasm and native.
// This value is set to 100 to notify Polkadot-JS App (https://polkadot.js.org/apps) to use
// the compatible custom types.
spec_version: 223,
spec_version: 224,
impl_version: 1,
apis: RUNTIME_API_VERSIONS,
transaction_version: 1,
Expand Down
Loading