Skip to content

RPC to get validators #1614

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

Open
wants to merge 12 commits into
base: devnet-ready
Choose a base branch
from
52 changes: 50 additions & 2 deletions pallets/subtensor/src/rpc_info/metagraph.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -107,7 +108,7 @@ pub struct Metagraph<AccountId: TypeInfo + Encode + Decode> {
alpha_dividends_per_hotkey: Vec<(AccountId, Compact<u64>)>, // 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<AccountId: TypeInfo + Encode + Decode + Clone> {
// Subnet index
Expand Down Expand Up @@ -205,6 +206,9 @@ pub struct SelectiveMetagraph<AccountId: TypeInfo + Encode + Decode + Clone> {
// Dividend break down.
tao_dividends_per_hotkey: Option<Vec<(AccountId, Compact<u64>)>>, // List of dividend payouts in tao via root.
alpha_dividends_per_hotkey: Option<Vec<(AccountId, Compact<u64>)>>, // List of dividend payout in alpha via subnet.

// validators
validators: Option<Vec<Compact<u16>>>, // List of validators
}

impl<AccountId> SelectiveMetagraph<AccountId>
Expand Down Expand Up @@ -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 => {}
};
}
Expand Down Expand Up @@ -445,6 +449,7 @@ where
total_stake: None,
tao_dividends_per_hotkey: None,
alpha_dividends_per_hotkey: None,
validators: None,
}
}
}
Expand Down Expand Up @@ -522,6 +527,7 @@ pub enum SelectiveMetagraphIndex {
TotalStake,
TaoDividendsPerHotkey,
AlphaDividendsPerHotkey,
Validators,
}

impl SelectiveMetagraphIndex {
Expand Down Expand Up @@ -599,6 +605,7 @@ impl SelectiveMetagraphIndex {
69 => Some(SelectiveMetagraphIndex::TotalStake),
70 => Some(SelectiveMetagraphIndex::TaoDividendsPerHotkey),
71 => Some(SelectiveMetagraphIndex::AlphaDividendsPerHotkey),
72 => Some(SelectiveMetagraphIndex::Validators),
_ => None,
}
}
Expand Down Expand Up @@ -1356,13 +1363,53 @@ impl<T: Config> Pallet<T> {
..Default::default()
}
}
Some(SelectiveMetagraphIndex::Validators) => Self::get_validators(netuid),
None => SelectiveMetagraph {
// Subnet index
netuid: netuid.into(),
..Default::default()
},
}
}

fn get_validators(netuid: u16) -> SelectiveMetagraph<T::AccountId> {
let stake_threshold = Self::get_stake_threshold();
let hotkeys: Vec<(u16, T::AccountId)> =
<Keys<T> as IterableStorageDoubleMap<u16, u16, T::AccountId>>::iter_prefix(netuid)
.collect();
let validator_permits: Vec<bool> = 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::<Vec<_>>();

// 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<Compact<u16>> = validators
.iter()
.filter(|(_uid, stake)| *stake > stake_threshold)
.map(|(uid, _)| Compact::from(*uid))
.collect::<Vec<_>>();

SelectiveMetagraph {
// Subnet index
netuid: netuid.into(),
validators: Some(validators),
..Default::default()
}
}
}

#[test]
Expand Down Expand Up @@ -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
Expand Down
Loading