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

Hotfix/no registration no emission #474

Merged
merged 7 commits into from
May 27, 2024
Merged
Show file tree
Hide file tree
Changes from 4 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
6 changes: 3 additions & 3 deletions pallets/subtensor/src/block_step.rs
Original file line number Diff line number Diff line change
Expand Up @@ -109,9 +109,9 @@ impl<T: Config> Pallet<T> {
pub fn generate_emission(block_number: u64) {
// --- 1. Iterate across each network and add pending emission into stash.
for (netuid, tempo) in <Tempo<T> as IterableStorageMap<u16, u16>>::iter() {
// Skip the root network.
if netuid == Self::get_root_netuid() {
// Root emission is burned.
// Skip the root network or subnets with registrations turned off
if netuid == Self::get_root_netuid() || !Self::is_registration_allowed(netuid) {
// Root emission or subnet emission is burned
continue;
}

Expand Down
18 changes: 18 additions & 0 deletions pallets/subtensor/src/root.rs
Original file line number Diff line number Diff line change
Expand Up @@ -286,6 +286,24 @@ impl<T: Config> Pallet<T> {
Self::deposit_event(Event::NetworkRateLimitSet(limit));
}

/// Checks if registrations are allowed for a given subnet.
///
/// This function retrieves the subnet hyperparameters for the specified subnet and checks the `registration_allowed` flag.
/// If the subnet doesn't exist or doesn't have hyperparameters defined, it returns `false`.
///
/// # Arguments
///
/// * `netuid` - The unique identifier of the subnet.
///
/// # Returns
///
/// * `bool` - `true` if registrations are allowed for the subnet, `false` otherwise.
pub fn is_registration_allowed(netuid: u16) -> bool {
Self::get_subnet_hyperparams(netuid)
.map(|params| params.registration_allowed)
.unwrap_or(false)
}

// Computes and sets emission values for the root network which determine the emission for all subnets.
//
// This function is responsible for calculating emission based on network weights, stake values,
Expand Down
2 changes: 1 addition & 1 deletion pallets/subtensor/src/subnet_info.rs
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ pub struct SubnetHyperparams {
weights_rate_limit: Compact<u64>,
adjustment_interval: Compact<u16>,
activity_cutoff: Compact<u16>,
registration_allowed: bool,
pub registration_allowed: bool,
target_regs_per_interval: Compact<u16>,
min_burn: Compact<u64>,
max_burn: Compact<u64>,
Expand Down
67 changes: 67 additions & 0 deletions pallets/subtensor/tests/block_step.rs
distributedstatemachine marked this conversation as resolved.
Show resolved Hide resolved
Original file line number Diff line number Diff line change
Expand Up @@ -803,3 +803,70 @@ fn test_burn_adjustment_case_e_zero_registrations() {
assert_eq!(adjusted_diff, 5_000);
});
}

#[test]
fn test_emission_based_on_registration_status() {
new_test_ext(1).execute_with(|| {
let n: u16 = 100;
let netuid_off: u16 = 1;
let netuid_on: u16 = 2;
let tempo: u16 = 1;
let netuids: Vec<u16> = vec![netuid_off, netuid_on];
let emissions: Vec<u64> = vec![1000000000, 1000000000];

// Add subnets with registration turned off and on
add_network(netuid_off, tempo, 0);
add_network(netuid_on, tempo, 0);
SubtensorModule::set_max_allowed_uids(netuid_off, n);
SubtensorModule::set_max_allowed_uids(netuid_on, n);
SubtensorModule::set_emission_values(&netuids, emissions).unwrap();
SubtensorModule::set_network_registration_allowed(netuid_off, false);
SubtensorModule::set_network_registration_allowed(netuid_on, true);

// Populate the subnets with neurons
for i in 0..n {
SubtensorModule::append_neuron(netuid_off, &U256::from(i), 0);
SubtensorModule::append_neuron(netuid_on, &U256::from(i), 0);
}

// Generate emission at block 0
let block: u64 = 0;
SubtensorModule::generate_emission(block);

// Verify that no emission tuples are loaded for the subnet with registration off
assert!(SubtensorModule::get_loaded_emission_tuples(netuid_off).is_none());

// Verify that emission tuples are loaded for the subnet with registration on
assert!(SubtensorModule::get_loaded_emission_tuples(netuid_on).is_some());
assert_eq!(
SubtensorModule::get_loaded_emission_tuples(netuid_on)
.unwrap()
.len(),
n as usize
);

// Step to the next epoch block
let epoch_block: u16 = tempo;
step_block(epoch_block);

// Verify that no emission tuples are loaded for the subnet with registration off
assert!(SubtensorModule::get_loaded_emission_tuples(netuid_off).is_none());
log::info!(
"Emissions for netuid with registration off: {:?}",
SubtensorModule::get_loaded_emission_tuples(netuid_off)
);

// Verify that emission tuples are loaded for the subnet with registration on
assert!(SubtensorModule::get_loaded_emission_tuples(netuid_on).is_some());
log::info!(
"Emissions for netuid with registration on: {:?}",
SubtensorModule::get_loaded_emission_tuples(netuid_on)
);
assert_eq!(
SubtensorModule::get_loaded_emission_tuples(netuid_on)
.unwrap()
.len(),
n as usize
);
});
}
Loading