use super::*; extern crate alloc; use crate::epoch::math::*; use codec::Compact; use frame_support::pallet_prelude::{Decode, Encode}; use sp_runtime::PerU16; use substrate_fixed::types::I64F64; use subtensor_runtime_common::{AlphaBalance, NetUid, NetUidStorageIndex, TaoBalance}; #[freeze_struct("b48b8accfd0ac902")] #[derive(Decode, Encode, PartialEq, Eq, Clone, Debug, TypeInfo)] pub struct SubnetState { netuid: Compact, hotkeys: Vec, coldkeys: Vec, active: Vec, validator_permit: Vec, pruning_score: Vec>, last_update: Vec>, emission: Vec>, dividends: Vec>, incentives: Vec>, consensus: Vec>, trust: Vec>, rank: Vec>, block_at_registration: Vec>, alpha_stake: Vec>, tao_stake: Vec>, total_stake: Vec>, emission_history: Vec>>, // identities: Vec, // tao_stake: Compact, // incentive: Compact, // consensus: Compact, // trust: Compact, // validator_trust: Compact, // dividends: Compact, // // has no weights or bonds } impl Pallet { /// Retrieves the emission history for a list of hotkeys across all subnets. /// /// This function iterates over all subnets and collects the last emission value /// for each hotkey in the provided list. The result is a vector of vectors, where /// each inner vector contains the emission values for a specific subnet. /// /// # Arguments /// /// * `hotkeys`: A vector of hotkeys (account IDs) for which the emission history is to be retrieved. /// /// # Returns /// /// * `Vec>>`: A vector of vectors containing the emission history for each hotkey across all subnets. pub fn get_emissions_history(hotkeys: Vec) -> Vec>> { let mut result: Vec>> = vec![]; for netuid in Self::get_all_subnet_netuids() { let mut hotkeys_emissions: Vec> = vec![]; for hotkey in hotkeys.clone() { let last_emission: Compact = LastHotkeyEmissionOnNetuid::::get(hotkey.clone(), netuid).into(); hotkeys_emissions.push(last_emission); } result.push(hotkeys_emissions.clone()); } result } /// Retrieves the state of a specific subnet. /// /// This function gathers various metrics and data points for a given subnet, identified by its `netuid`. /// It collects information such as hotkeys, coldkeys, block at registration, active status, validator permits, /// pruning scores, last updates, emissions, dividends, incentives, consensus, trust, rank, local stake, global stake, /// stake weight, and emission history. /// /// # Arguments /// /// * `netuid`: The unique identifier of the subnet for which the state is to be retrieved. /// /// # Returns /// /// * `Option>`: An optional `SubnetState` struct containing the collected data for the subnet. /// Returns `None` if the subnet does not exist. pub fn get_subnet_state(netuid: NetUid) -> Option> { if !Self::if_subnet_exist(netuid) { return None; } let n: u16 = Self::get_subnetwork_n(netuid); let mut hotkeys: Vec = vec![]; let mut coldkeys: Vec = vec![]; let mut block_at_registration: Vec> = vec![]; // let mut identities: Vec = vec![]; for uid in 0..n { let hotkey = Keys::::get(netuid, uid); let coldkey = Owner::::get(hotkey.clone()); hotkeys.push(hotkey); coldkeys.push(coldkey); block_at_registration.push(BlockAtRegistration::::get(netuid, uid).into()); // identities.push( Identities::::get( coldkey.clone() ) ); } let active: Vec = Active::::get(netuid); let validator_permit: Vec = ValidatorPermit::::get(netuid); let pruning_score: Vec> = Vec::new(); // Deprecated: no longer computed let last_update: Vec> = LastUpdate::::get(NetUidStorageIndex::from(netuid)) .into_iter() .map(Compact::from) .collect(); let emission = Emission::::get(netuid) .into_iter() .map(Compact::from) .collect(); let dividends: Vec> = Dividends::::get(netuid) .into_iter() .map(Compact::from) .collect(); let incentives: Vec> = Incentive::::get(NetUidStorageIndex::from(netuid)) .into_iter() .map(Compact::from) .collect(); let consensus: Vec> = Consensus::::get(netuid) .into_iter() .map(Compact::from) .collect(); let trust: Vec> = Vec::new(); // Deprecated: no longer computed let rank: Vec> = Vec::new(); // Deprecated: no longer computed let (total_stake_fl, alpha_stake_fl, tao_stake_fl): ( Vec, Vec, Vec, ) = Self::get_stake_weights_for_network(netuid); let alpha_stake: Vec> = alpha_stake_fl .iter() .map(|xi| Compact::from(AlphaBalance::from(fixed64_to_u64(*xi)))) .collect(); let tao_stake: Vec> = tao_stake_fl .iter() .map(|xi| Compact::from(TaoBalance::from(fixed64_to_u64(*xi)))) .collect(); let total_stake: Vec> = total_stake_fl .iter() .map(|xi| Compact::from(TaoBalance::from(fixed64_to_u64(*xi)))) .collect(); let emission_history = Self::get_emissions_history(hotkeys.clone()); Some(SubnetState { netuid: netuid.into(), hotkeys, coldkeys, active, validator_permit, pruning_score, last_update, emission, dividends, incentives, consensus, trust, rank, block_at_registration, alpha_stake, tao_stake, total_stake, emission_history, }) } }