diff --git a/pallets/subtensor/src/rpc_info/metagraph.rs b/pallets/subtensor/src/rpc_info/metagraph.rs index 04325fef4d..9d61d12470 100644 --- a/pallets/subtensor/src/rpc_info/metagraph.rs +++ b/pallets/subtensor/src/rpc_info/metagraph.rs @@ -2,6 +2,7 @@ use super::*; extern crate alloc; use crate::epoch::math::*; use codec::Compact; +use frame_support::IterableStorageDoubleMap; use frame_support::pallet_prelude::{Decode, Encode}; use substrate_fixed::types::I64F64; use substrate_fixed::types::I96F32; @@ -107,7 +108,7 @@ pub struct Metagraph { alpha_dividends_per_hotkey: Vec<(AccountId, Compact)>, // List of dividend payout in alpha via subnet. } -#[freeze_struct("182c7375fee9db7b")] +#[freeze_struct("2eca518cf84390fa")] #[derive(Decode, Encode, PartialEq, Eq, Clone, Debug, TypeInfo)] pub struct SelectiveMetagraph { // Subnet index @@ -205,6 +206,9 @@ pub struct SelectiveMetagraph { // Dividend break down. tao_dividends_per_hotkey: Option)>>, // List of dividend payouts in tao via root. alpha_dividends_per_hotkey: Option)>>, // List of dividend payout in alpha via subnet. + + // validators + validators: Option>>, // List of validators } impl SelectiveMetagraph @@ -361,7 +365,7 @@ where Some(SelectiveMetagraphIndex::AlphaDividendsPerHotkey) => { self.alpha_dividends_per_hotkey = other.alpha_dividends_per_hotkey.clone() } - + Some(SelectiveMetagraphIndex::Validators) => self.validators = other.validators.clone(), None => {} }; } @@ -445,6 +449,7 @@ where total_stake: None, tao_dividends_per_hotkey: None, alpha_dividends_per_hotkey: None, + validators: None, } } } @@ -522,6 +527,7 @@ pub enum SelectiveMetagraphIndex { TotalStake, TaoDividendsPerHotkey, AlphaDividendsPerHotkey, + Validators, } impl SelectiveMetagraphIndex { @@ -599,6 +605,7 @@ impl SelectiveMetagraphIndex { 69 => Some(SelectiveMetagraphIndex::TotalStake), 70 => Some(SelectiveMetagraphIndex::TaoDividendsPerHotkey), 71 => Some(SelectiveMetagraphIndex::AlphaDividendsPerHotkey), + 72 => Some(SelectiveMetagraphIndex::Validators), _ => None, } } @@ -1356,6 +1363,7 @@ impl Pallet { ..Default::default() } } + Some(SelectiveMetagraphIndex::Validators) => Self::get_validators(netuid), None => SelectiveMetagraph { // Subnet index netuid: netuid.into(), @@ -1363,6 +1371,45 @@ impl Pallet { }, } } + + fn get_validators(netuid: u16) -> SelectiveMetagraph { + let stake_threshold = Self::get_stake_threshold(); + let hotkeys: Vec<(u16, T::AccountId)> = + as IterableStorageDoubleMap>::iter_prefix(netuid) + .collect(); + let validator_permits: Vec = Self::get_validator_permit(netuid); + + // filter according to validator_permits + let hotkeys: Vec<&(u16, T::AccountId)> = hotkeys + .iter() + .filter(|(uid, _)| *validator_permits.get(*uid as usize).unwrap_or(&false)) + .collect::>(); + + // map hotkeys to validators with stake + let mut validators: Vec<(u16, I64F64)> = hotkeys + .iter() + .map(|(uid, hotkey)| { + let stake = Self::get_stake_weights_for_hotkey_on_subnet(hotkey, netuid); + (*uid, stake.0) + }) + .collect(); + + // sort validators by stake + validators.sort_by(|a, b| a.1.cmp(&b.1)); + + let validators: Vec> = validators + .iter() + .filter(|(_uid, stake)| *stake > stake_threshold) + .map(|(uid, _)| Compact::from(*uid)) + .collect::>(); + + SelectiveMetagraph { + // Subnet index + netuid: netuid.into(), + validators: Some(validators), + ..Default::default() + } + } } #[test] @@ -1441,6 +1488,7 @@ fn test_selective_metagraph() { total_stake: None, tao_dividends_per_hotkey: None, alpha_dividends_per_hotkey: None, + validators: None, }; // test init value