opentensor / subtensor

Bittensor Blockchain Layer
The Unlicense
160 stars 159 forks source link

Clean up/refactor Pallet type from the Subtensor module #983

Open ales-tsurko opened 1 week ago

ales-tsurko commented 1 week ago

Is your feature request related to a problem? Please describe.

Here is the list of methods of Pallet type:

accumulate_hotkey_emission
accumulate_nominator_emission
add_balance_to_coldkey_account
add_stake
adjust_global_weight
adjust_registration_terms_for_networks
adjust_senate
adjust_tempos
alpha_to_global
append_neuron
axon_passes_rate_limit
batch_reveal_weights
become_delegate
block_step
blocks_until_next_epoch
burn_tokens
burned_register
calculate_logistic_params
calculate_tempos
can_remove_balance_from_coldkey_account
check_len_uids_within_allowed
check_length
check_rate_limit
check_validator_permit
check_version_key
check_weights_min_stake
checked_allowed_register
clamp_alpha_values
clear_small_nomination_if_required
clear_small_nominations
coinbase
coldkey_owns_hotkey
commit_weights
compute_alpha_values
compute_ema_bonds
compute_ema_bonds_normal
compute_ema_bonds_normal_sparse
compute_ema_bonds_sparse
compute_ema_bonds_with_liquid_alpha
compute_ema_bonds_with_liquid_alpha_sparse
contains_invalid_root_uids
contains_invalid_uids
create_account_if_non_existent
create_seal_hash
create_work_for_block_number
decrease_take
decrease_total_stake
delegate_hotkey
dissolve_network
distribute_owner_cut
do_add_stake
do_adjust_senate
do_batch_reveal_weights
do_become_delegate
do_burned_registration
do_commit_weights
do_decrease_take
do_faucet
do_increase_take
do_move_stake
do_register_network
do_registration
do_remove_stake
do_reveal_weights
do_root_register
do_serve_axon
do_serve_prometheus
do_set_alpha_values
do_set_childkey_take
do_set_children
do_set_identity
do_set_senate_required_stake_perc
do_set_subnet_identity
do_set_weights
do_swap_coldkey
do_swap_hotkey
do_take_checks
do_vote_root
drain_hotkey_emission
emit_into_subnet
ensure_subnet_owner_or_root
epoch
epoch_dense
exceeds_tx_delegate_take_rate_limit
exceeds_tx_rate_limit
faucet
get_active
get_active_for_uid
get_activity_cutoff
get_adjustment_alpha
get_adjustment_interval
get_all_dynamic_info
get_all_staked_hotkeys
get_all_subnet_netuids
get_alpha_values
get_alpha_values_32
get_axon_info
get_block_at_registration
get_block_emission
get_block_emission_for_issuance
get_block_hash_from_u64
get_blocks_since_last_step
get_bonds
get_bonds_moving_average
get_bonds_sparse
get_burn_as_u64
get_burn_registrations_this_interval
get_childkey_take
get_children
get_coldkey_balance
get_coldkey_for_hotkey
get_commit_reveal_weights_enabled
get_consensus
get_consensus_for_uid
get_current_block_as_u64
get_default_childkey_take
get_default_delegate_take
get_delegate
get_delegated
get_delegates
get_difficulty
get_difficulty_as_u64
get_dividends
get_dividends_for_uid
get_dynamic_info
get_emission
get_emission_for_uid
get_emission_value
get_emissions_history
get_epoch_index
get_float_kappa
get_float_rho
get_float_subnet_owner_cut
get_global_for_hotkey
get_global_for_hotkey_and_coldey_on_subnet
get_global_for_hotkey_and_coldkey
get_global_for_hotkey_on_subnet
get_global_weight
get_hotkey_emission_tempo
get_hotkey_for_net_and_uid
get_hotkey_take
get_immunity_period
get_incentive
get_incentive_for_uid
get_inherited_alpha_for_hotkey_on_subnet
get_inherited_global_for_hotkey_on_subnet
get_kappa
get_key_swap_cost
get_last_adjustment_block
get_last_mechanism_step_block
get_last_transaction_block
get_last_tx_block
get_last_tx_block_childkey_take
get_last_tx_block_delegate_take
get_last_update
get_last_update_for_uid
get_liquid_alpha_enabled
get_lock_reduction_interval
get_max_allowed_uids
get_max_allowed_validators
get_max_burn_as_u64
get_max_childkey_take
get_max_delegate_take
get_max_difficulty
get_max_registrations_per_block
get_max_root_validators
get_max_subnets
get_max_weight_limit
get_min_allowed_weights
get_min_burn_as_u64
get_min_childkey_take
get_min_delegate_take
get_min_difficulty
get_network_immunity_period
get_network_last_lock
get_network_last_lock_block
get_network_lock_cost
get_network_max_stake
get_network_min_lock
get_network_pow_registration_allowed
get_network_registered_block
get_network_registration_allowed
get_neuron
get_neuron_block_at_registration
get_neuron_is_immune
get_neuron_lite
get_neuron_to_prune
get_neurons
get_neurons_lite
get_next_netuid
get_nominator_min_required_stake
get_normalized_stake
get_num_root_validators
get_num_subnets
get_number_of_subnets
get_owned_hotkeys
get_owning_coldkey_for_hotkey
get_parents
get_pending_emission
get_pending_hotkey_emission_on_netuid
get_pow_registrations_this_interval
get_priority_set_weights
get_prometheus_info
get_pruning_score
get_pruning_score_for_uid
get_rank
get_rank_for_uid
get_rao_recycled
get_rate_limit
get_registered_networks_for_hotkey
get_registrations_this_block
get_registrations_this_interval
get_reveal_blocks
get_reveal_period
get_rho
get_root_netuid
get_root_weights
get_scaling_law_power
get_serving_rate_limit
get_stake_for_coldkey_on_subnet
get_stake_for_hotkey_and_coldkey_on_subnet
get_stake_for_hotkey_on_subnet
get_stake_for_uid_and_subnetwork
get_stake_info_for_coldkey
get_stake_info_for_coldkeys
get_stake_weight
get_stake_weights_for_network
get_stakes_this_interval_for_coldkey_hotkey
get_subnet_emission_value
get_subnet_hyperparams
get_subnet_info
get_subnet_info_v2
get_subnet_locked_balance
get_subnet_mechanism
get_subnet_owner
get_subnet_owner_cut
get_subnet_state
get_subnets_info
get_subnets_info_v2
get_subnetwork_n
get_target_registrations_per_interval
get_target_stakes_per_interval
get_tempo
get_total_delegated_stake
get_total_hotkey_delegated_stake
get_total_issuance
get_total_stake
get_total_subnet_locked
get_trust
get_trust_for_uid
get_tx_childkey_take_rate_limit
get_tx_delegate_take_rate_limit
get_tx_rate_limit
get_uid_for_net_and_hotkey
get_validator_permit
get_validator_permit_for_uid
get_validator_prune_len
get_validator_trust
get_validator_trust_for_uid
get_weights
get_weights_min_stake
get_weights_set_rate_limit
get_weights_sparse
get_weights_version_key
has_axon_info
has_duplicate_uids
has_enough_stake
has_enough_stake_on_subnet
has_prometheus_info
hash_block_and_hotkey
hash_hotkey_to_u64
hash_meets_difficulty
hash_to_vec
hotkey_account_exists
hotkey_is_delegate
if_subnet_exist
increase_rao_recycled
increase_take
increase_total_stake
init_new_network
is_commit_expired
is_hotkey_registered_on_any_network
is_hotkey_registered_on_network
is_registration_allowed
is_reveal_block_range
is_self_weight
is_senate_member
is_subnet_owner
is_uid_exist_on_network
is_valid_identity
is_valid_ip_address
is_valid_ip_type
is_valid_subnet_identity
kill_coldkey_account
limits_for_coldkey
limits_for_hotkey
max_weight_limited
move_stake
normalize_weights
passes_rate_limit_globally
passes_rate_limit_on_subnet
perform_hotkey_swap
perform_swap_coldkey
prometheus_passes_rate_limit
register
register_network
register_network_with_identity
register_neuron
remove_balance_from_coldkey_account
remove_stake
replace_neuron
reveal_weights
root_register
run_coinbase
schedule_dissolve_network
schedule_swap_coldkey
serve_axon
serve_axon_tls
serve_prometheus
set_active_for_uid
set_activity_cutoff
set_adjustment_alpha
set_adjustment_interval
set_blocks_since_last_step
set_bonds_moving_average
set_burn
set_burn_registrations_this_interval
set_childkey_take
set_children
set_coldkey_swap_schedule_duration
set_commit_reveal_weights_enabled
set_difficulty
set_global_weight
set_hotkey_emission_tempo
set_identity
set_immunity_period
set_kappa
set_last_adjustment_block
set_last_mechanism_step_block
set_last_transaction_block
set_last_tx_block
set_last_tx_block_childkey_take
set_last_tx_block_delegate_take
set_last_update_for_uid
set_liquid_alpha_enabled
set_lock_reduction_interval
set_max_allowed_uids
set_max_allowed_validators
set_max_burn
set_max_childkey_take
set_max_delegate_take
set_max_difficulty
set_max_registrations_per_block
set_max_subnets
set_max_weight_limit
set_min_allowed_weights
set_min_burn
set_min_childkey_take
set_min_delegate_take
set_min_difficulty
set_network_immunity_period
set_network_last_lock
set_network_max_stake
set_network_min_lock
set_network_pow_registration_allowed
set_network_rate_limit
set_network_registration_allowed
set_nominator_min_required_stake
set_pow_registrations_this_interval
set_pruning_score_for_uid
set_rao_recycled
set_registrations_this_block
set_registrations_this_interval
set_reveal_period
set_rho
set_root_weights
set_scaling_law_power
set_senate_required_stake_perc
set_serving_rate_limit
set_stake_interval
set_stakes_this_interval_for_coldkey_hotkey
set_subnet_identity
set_subnet_locked_balance
set_subnet_owner_cut
set_target_registrations_per_interval
set_target_stakes_per_interval
set_tempo
set_total_issuance
set_tx_childkey_take_rate_limit
set_tx_delegate_take_rate_limit
set_tx_rate_limit
set_validator_permit_for_uid
set_validator_prune_len
set_weights
set_weights_min_stake
set_weights_set_rate_limit
set_weights_version_key
should_drain_hotkey
should_run_epoch
source_hotkey_emission
source_nominator_emission
stake_into_subnet
sudo
sudo_hotfix_swap_coldkey_delegates
sudo_set_max_childkey_take
sudo_set_min_childkey_take
sudo_set_tx_childkey_take_rate_limit
sudo_unchecked_weight
swap_coldkey
swap_hotkey
swap_senate_member
uids_match_values
unstake_from_subnet
upgraded_burn
upgraded_difficulty
validate_axon_data
validate_prometheus_data
vec_to_hash
vote

It's 419 methods in a single type. The type is bloated. Because of this it's hard to debug and navigate the code. A lot of these methods used just to access storage. Take, for example Pallet::get_active here is its code:

    pub fn get_active(netuid: u16) -> Vec<bool> {
        Active::<T>::get(netuid)
    }

And there is only one place where it's used:

    pub fn set_active_for_uid(netuid: u16, uid: u16, active: bool) {
        let mut updated_active_vec = Self::get_active(netuid);
        let Some(updated_active) = updated_active_vec.get_mut(uid as usize) else {
            return;
        };
        *updated_active = active;
        Active::<T>::insert(netuid, updated_active_vec);
    }

This method can be safely removed and it's no shorter than just using the storage directly.

Describe the solution you'd like

  1. The best way to clear everything up would be providing a script, which could analyze the usage of Pallet's methods within the codebase. rustdoc and rust-analyzer could be leveraged for this.
  2. The getters/setters for storage, should be replaced with storage API.
  3. The public API which exists only for tests, should be removed and the tests should be moved to the unit level (https://github.com/opentensor/subtensor/issues/926).
  4. Consider refactoring, to encapsulate API, which Pallet type doesn't need.
  5. Consider following the style of Rust API Guidelines. Particularly, getters naming convention: https://rust-lang.github.io/api-guidelines/naming.html#getter-names-follow-rust-convention-c-getter

Describe alternatives you've considered

No response

Additional context

No response