use frame_support::pallet_macros::pallet_section; /// A [`pallet_section`] that defines the events for a pallet. /// This can later be imported into the pallet using [`import_section`]. #[pallet_section] mod hooks { // ================ // ==== Hooks ===== // ================ #[pallet::hooks] impl Hooks> for Pallet { // ---- Called on the initialization of this pallet. (the order of on_finalize calls is determined in the runtime) // // # Args: // * 'n': (BlockNumberFor): // - The number of the block we are initializing. fn on_initialize(block_number: BlockNumberFor) -> Weight { let hotkey_swap_clean_up_weight = Self::clean_up_hotkey_swap_records(block_number); let block_step_result = Self::block_step(); match block_step_result { Ok(_) => { // --- If the block step was successful, return the weight. log::debug!("Successfully ran block step."); Weight::from_parts(110_634_229_000_u64, 0) .saturating_add(T::DbWeight::get().reads(8304_u64)) .saturating_add(T::DbWeight::get().writes(110_u64)) .saturating_add(hotkey_swap_clean_up_weight) } Err(e) => { // --- If the block step was unsuccessful, return the weight anyway. log::error!("Error while stepping block: {:?}", e); Weight::from_parts(110_634_229_000_u64, 0) .saturating_add(T::DbWeight::get().reads(8304_u64)) .saturating_add(T::DbWeight::get().writes(110_u64)) .saturating_add(hotkey_swap_clean_up_weight) } } } fn on_runtime_upgrade() -> frame_support::weights::Weight { // --- Migrate storage let mut weight = frame_support::weights::Weight::from_parts(0, 0); // Hex encoded foundation coldkey let hex = hex_literal::hex![ "feabaafee293d3b76dae304e2f9d885f77d2b17adab9e17e921b321eccd61c77" ]; weight = weight // Initializes storage version (to 1) .saturating_add(migrations::migrate_to_v1_separate_emission::migrate_to_v1_separate_emission::()) // Storage version v1 -> v2 .saturating_add(migrations::migrate_to_v2_fixed_total_stake::migrate_to_v2_fixed_total_stake::()) // Doesn't check storage version. TODO: Remove after upgrade .saturating_add(migrations::migrate_create_root_network::migrate_create_root_network::()) // Storage version v2 -> v3 .saturating_add(migrations::migrate_transfer_ownership_to_foundation::migrate_transfer_ownership_to_foundation::( hex, )) // Storage version v3 -> v4 .saturating_add(migrations::migrate_delete_subnet_21::migrate_delete_subnet_21::()) // Storage version v4 -> v5 .saturating_add(migrations::migrate_delete_subnet_3::migrate_delete_subnet_3::()) // Populate OwnedHotkeys map for coldkey swap. Doesn't update storage vesion. // Storage version v6 -> v7 .saturating_add(migrations::migrate_populate_owned_hotkeys::migrate_populate_owned::()) // Migrate Commit-Reval 2.0 .saturating_add(migrations::migrate_commit_reveal_v2::migrate_commit_reveal_2::()) // Migrate to RAO .saturating_add(migrations::migrate_rao::migrate_rao::()) // Fix the IsNetworkMember map to be consistent with other storage maps .saturating_add(migrations::migrate_fix_is_network_member::migrate_fix_is_network_member::()) .saturating_add(migrations::migrate_subnet_volume::migrate_subnet_volume::()) // Set the min burn across all subnets to a new minimum .saturating_add(migrations::migrate_set_min_burn::migrate_set_min_burn::()) // Set the min difficulty across all subnets to a new minimum .saturating_add(migrations::migrate_set_min_difficulty::migrate_set_min_difficulty::()) // Remove Stake map entries .saturating_add(migrations::migrate_remove_stake_map::migrate_remove_stake_map::()) // Remove unused maps entries .saturating_add(migrations::migrate_remove_unused_maps_and_values::migrate_remove_unused_maps_and_values::()) // Set last emission block number for all existed subnets before start call feature applied .saturating_add(migrations::migrate_set_first_emission_block_number::migrate_set_first_emission_block_number::()) // Remove all zero value entries in TotalHotkeyAlpha .saturating_add(migrations::migrate_remove_zero_total_hotkey_alpha::migrate_remove_zero_total_hotkey_alpha::()) // Wipe existing items to prevent bad decoding for new type .saturating_add(migrations::migrate_upgrade_revealed_commitments::migrate_upgrade_revealed_commitments::()) // Set subtoken enabled for all existed subnets .saturating_add(migrations::migrate_set_subtoken_enabled::migrate_set_subtoken_enabled::()) // Remove all entries in TotalHotkeyColdkeyStakesThisInterval .saturating_add(migrations::migrate_remove_total_hotkey_coldkey_stakes_this_interval::migrate_remove_total_hotkey_coldkey_stakes_this_interval::()) // Wipe the deprecated RateLimit storage item in the commitments pallet .saturating_add(migrations::migrate_remove_commitments_rate_limit::migrate_remove_commitments_rate_limit::()) // Remove all entries in orphaned storage items .saturating_add( migrations::migrate_orphaned_storage_items::migrate_orphaned_storage_items::( ), ) // Reset bonds moving average .saturating_add(migrations::migrate_reset_bonds_moving_average::migrate_reset_bonds_moving_average::()) // Reset max burn .saturating_add(migrations::migrate_reset_max_burn::migrate_reset_max_burn::()) // Migrate ColdkeySwapScheduled structure to new format .saturating_add(migrations::migrate_coldkey_swap_scheduled::migrate_coldkey_swap_scheduled::()) // Fix the root subnet TAO storage value .saturating_add(migrations::migrate_fix_root_subnet_tao::migrate_fix_root_subnet_tao::()) // Fix the owner disable the registration .saturating_add(migrations::migrate_set_registration_enable::migrate_set_registration_enable::()) // Migrate subnet symbols to fix the shift after subnet 81 .saturating_add(migrations::migrate_subnet_symbols::migrate_subnet_symbols::()) // Migrate CRV3 add commit_block .saturating_add(migrations::migrate_crv3_commits_add_block::migrate_crv3_commits_add_block::()) // Migrate Commit-Reveal Settings .saturating_add(migrations::migrate_commit_reveal_settings::migrate_commit_reveal_settings::()) //Migrate CRV3 to TimelockedCommits .saturating_add(migrations::migrate_crv3_v2_to_timelocked::migrate_crv3_v2_to_timelocked::()) // Migrate to fix root counters .saturating_add(migrations::migrate_fix_root_tao_and_alpha_in::migrate_fix_root_tao_and_alpha_in::()) // Migrate last block rate limiting storage items .saturating_add(migrations::migrate_rate_limiting_last_blocks::migrate_obsolete_rate_limiting_last_blocks_storage::()) // Re-encode rate limit keys after introducing OwnerHyperparamUpdate variant .saturating_add(migrations::migrate_rate_limit_keys::migrate_rate_limit_keys::()) // Remove AddStakeBurn entries from LastRateLimitedBlock .saturating_add(migrations::migrate_remove_add_stake_burn_rate_limit::migrate_remove_add_stake_burn_rate_limit::()) // Migrate remove network modality .saturating_add(migrations::migrate_remove_network_modality::migrate_remove_network_modality::()) // Migrate Immunity Period .saturating_add(migrations::migrate_network_immunity_period::migrate_network_immunity_period::()) // Migrate Subnet Limit .saturating_add(migrations::migrate_subnet_limit_to_default::migrate_subnet_limit_to_default::()) // Migrate Lock Reduction Interval .saturating_add(migrations::migrate_network_lock_reduction_interval::migrate_network_lock_reduction_interval::()) // Migrate subnet locked balances .saturating_add(migrations::migrate_subnet_locked::migrate_restore_subnet_locked::()) // Migrate subnet burn cost to 2500 .saturating_add(migrations::migrate_network_lock_cost_2500::migrate_network_lock_cost_2500::()) // Cleanup child/parent keys .saturating_add(migrations::migrate_fix_childkeys::migrate_fix_childkeys::()) // Migrate AutoStakeDestinationColdkeys .saturating_add(migrations::migrate_auto_stake_destination::migrate_auto_stake_destination::()) // Migrate Kappa to default (0.5) .saturating_add(migrations::migrate_kappa_map_to_default::migrate_kappa_map_to_default::()) // Remove obsolete map entries .saturating_add(migrations::migrate_remove_tao_dividends::migrate_remove_tao_dividends::()) // Re-init tao flows .saturating_add(migrations::migrate_init_tao_flow::migrate_init_tao_flow::()) // Migrate pending emissions .saturating_add(migrations::migrate_pending_emissions::migrate_pending_emissions::()) // Reset unactive subnets .saturating_add(migrations::migrate_reset_unactive_sn::migrate_reset_unactive_sn::()) // Remove old identity map entries(Identities, SubnetIdentities, SubnetIdentitiesV2) .saturating_add(migrations::migrate_remove_old_identity_maps::migrate_remove_old_identity_maps::()) // Remove unknown neuron axon, certificate prom .saturating_add(migrations::migrate_remove_unknown_neuron_axon_cert_prom::migrate_remove_unknown_neuron_axon_cert_prom::()) // Fix staking hot keys .saturating_add(migrations::migrate_fix_staking_hot_keys::migrate_fix_staking_hot_keys::()) // Migrate coldkey swap scheduled to announcements .saturating_add(migrations::migrate_coldkey_swap_scheduled_to_announcements::migrate_coldkey_swap_scheduled_to_announcements::()) // Migration for new Neuron Registration .saturating_add(migrations::migrate_clear_deprecated_registration_maps::migrate_clear_deprecated_registration_maps::()) // Migrate fix bad hk swap .saturating_add(migrations::migrate_fix_bad_hk_swap::migrate_fix_bad_hk_swap::()) // Fix RootClaimed overclaim caused by single-subnet hotkey swap bug .saturating_add(migrations::migrate_fix_root_claimed_overclaim::migrate_fix_root_claimed_overclaim::()) // Mint missing SubnetTAO and SubnetLocked into subnet accounts to make TotalIssuance match in balances and subtensor .saturating_add(migrations::migrate_subnet_balances::migrate_subnet_balances::()) // Fix testnet Subtensor TotalIssuance after the EVM fees issue. .saturating_add(migrations::migrate_fix_total_issuance_evm_fees::migrate_fix_total_issuance_evm_fees::()) // Remove deprecated conviction lock storage. .saturating_add(migrations::migrate_remove_deprecated_conviction_maps::migrate_remove_deprecated_conviction_maps::()) // Reset testnet conviction lock storage before deploying the current design. .saturating_add(migrations::migrate_reset_tnet_conviction_locks::migrate_reset_tnet_conviction_locks::()) // Seed LastEpochBlock for dynamic-tempo / owner-triggered-epochs feature .saturating_add(migrations::migrate_dynamic_tempo::migrate_dynamic_tempo::()) // Populate locking reverse map. .saturating_add(migrations::migrate_populate_locking_coldkeys::migrate_populate_locking_coldkeys::()) // Capture the runtime-upgrade block for TAO-in refund cutover. .saturating_add(migrations::migrate_tao_in_refund_deployment_block::migrate_tao_in_refund_deployment_block::()) // Fix lock state left behind by subnet-scoped hotkey swaps. .saturating_add(migrations::migrate_fix_subnet_hotkey_lock_swaps::migrate_fix_subnet_hotkey_lock_swaps::()) // Populate reverse lookup index for EVM address associations. .saturating_add(migrations::migrate_associated_evm_address_index::migrate_associated_evm_address_index::()) // Fold deprecated SubnetTaoProvided / SubnetAlphaInProvided residuals into the // main AMM reserves (issue #2793). Guarded by HasMigrationRun, so it only runs once. .saturating_add(migrations::migrate_cleanup_swap_v3::migrate_cleanup_swap_v3::()) // Remove orphan SubnetIdentitiesV3 entries left for recycled netuids. .saturating_add(migrations::migrate_clear_orphan_subnet_identities_v3::migrate_clear_orphan_subnet_identities_v3::()); weight } #[cfg(feature = "try-runtime")] fn try_state(_n: BlockNumberFor) -> Result<(), sp_runtime::TryRuntimeError> { // Disabled: https://github.com/RaoFoundation/subtensor/pull/1166 // Self::check_total_stake()?; Ok(()) } fn on_idle(_block: BlockNumberFor, limit: Weight) -> Weight { let mut weight = Self::remove_data_for_dissolved_networks(limit); if weight.all_lt(limit) { weight.saturating_accrue(Self::process_network_registration_queue()); } weight } } impl Pallet { // This function is to clean up the old hotkey swap records // It just clean up for one subnet at a time, according to the block number pub(crate) fn clean_up_hotkey_swap_records(block_number: BlockNumberFor) -> Weight { let mut weight = Weight::from_parts(0, 0); let hotkey_swap_on_subnet_interval = T::HotkeySwapOnSubnetInterval::get(); let block_number: u64 = TryInto::try_into(block_number) .ok() .expect("blockchain will not exceed 2^64 blocks; QED."); weight.saturating_accrue(T::DbWeight::get().reads(2_u64)); let netuids = Self::get_all_subnet_netuids(); weight.saturating_accrue(T::DbWeight::get().reads(netuids.len() as u64)); if let Some(slot) = block_number.checked_rem(hotkey_swap_on_subnet_interval) { // only handle the subnet with the same residue as current block number by HotkeySwapOnSubnetInterval for netuid in netuids.iter().filter(|netuid| { (u16::from(**netuid) as u64).checked_rem(hotkey_swap_on_subnet_interval) == Some(slot) }) { // Iterate over all the coldkeys in the subnet for (coldkey, swap_block_number) in LastHotkeySwapOnNetuid::::iter_prefix(netuid) { // Clean up out of date swap records if swap_block_number.saturating_add(hotkey_swap_on_subnet_interval) < block_number { LastHotkeySwapOnNetuid::::remove(netuid, coldkey); weight.saturating_accrue(T::DbWeight::get().writes(1_u64)); } weight.saturating_accrue(T::DbWeight::get().reads(1_u64)); } } } weight } } }