Skip to content

Commit 4234b95

Browse files
authored
Merge pull request #1614 from opentensor/only-validator-rpc
RPC to get validators
2 parents 98c4e86 + 317d34c commit 4234b95

File tree

1 file changed

+50
-2
lines changed

1 file changed

+50
-2
lines changed

pallets/subtensor/src/rpc_info/metagraph.rs

Lines changed: 50 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ use super::*;
22
extern crate alloc;
33
use crate::epoch::math::*;
44
use codec::Compact;
5+
use frame_support::IterableStorageDoubleMap;
56
use frame_support::pallet_prelude::{Decode, Encode};
67
use substrate_fixed::types::I64F64;
78
use substrate_fixed::types::I96F32;
@@ -107,7 +108,7 @@ pub struct Metagraph<AccountId: TypeInfo + Encode + Decode> {
107108
alpha_dividends_per_hotkey: Vec<(AccountId, Compact<u64>)>, // List of dividend payout in alpha via subnet.
108109
}
109110

110-
#[freeze_struct("182c7375fee9db7b")]
111+
#[freeze_struct("2eca518cf84390fa")]
111112
#[derive(Decode, Encode, PartialEq, Eq, Clone, Debug, TypeInfo)]
112113
pub struct SelectiveMetagraph<AccountId: TypeInfo + Encode + Decode + Clone> {
113114
// Subnet index
@@ -205,6 +206,9 @@ pub struct SelectiveMetagraph<AccountId: TypeInfo + Encode + Decode + Clone> {
205206
// Dividend break down.
206207
tao_dividends_per_hotkey: Option<Vec<(AccountId, Compact<u64>)>>, // List of dividend payouts in tao via root.
207208
alpha_dividends_per_hotkey: Option<Vec<(AccountId, Compact<u64>)>>, // List of dividend payout in alpha via subnet.
209+
210+
// validators
211+
validators: Option<Vec<Compact<u16>>>, // List of validators
208212
}
209213

210214
impl<AccountId> SelectiveMetagraph<AccountId>
@@ -361,7 +365,7 @@ where
361365
Some(SelectiveMetagraphIndex::AlphaDividendsPerHotkey) => {
362366
self.alpha_dividends_per_hotkey = other.alpha_dividends_per_hotkey.clone()
363367
}
364-
368+
Some(SelectiveMetagraphIndex::Validators) => self.validators = other.validators.clone(),
365369
None => {}
366370
};
367371
}
@@ -445,6 +449,7 @@ where
445449
total_stake: None,
446450
tao_dividends_per_hotkey: None,
447451
alpha_dividends_per_hotkey: None,
452+
validators: None,
448453
}
449454
}
450455
}
@@ -522,6 +527,7 @@ pub enum SelectiveMetagraphIndex {
522527
TotalStake,
523528
TaoDividendsPerHotkey,
524529
AlphaDividendsPerHotkey,
530+
Validators,
525531
}
526532

527533
impl SelectiveMetagraphIndex {
@@ -599,6 +605,7 @@ impl SelectiveMetagraphIndex {
599605
69 => Some(SelectiveMetagraphIndex::TotalStake),
600606
70 => Some(SelectiveMetagraphIndex::TaoDividendsPerHotkey),
601607
71 => Some(SelectiveMetagraphIndex::AlphaDividendsPerHotkey),
608+
72 => Some(SelectiveMetagraphIndex::Validators),
602609
_ => None,
603610
}
604611
}
@@ -1356,13 +1363,53 @@ impl<T: Config> Pallet<T> {
13561363
..Default::default()
13571364
}
13581365
}
1366+
Some(SelectiveMetagraphIndex::Validators) => Self::get_validators(netuid),
13591367
None => SelectiveMetagraph {
13601368
// Subnet index
13611369
netuid: netuid.into(),
13621370
..Default::default()
13631371
},
13641372
}
13651373
}
1374+
1375+
fn get_validators(netuid: u16) -> SelectiveMetagraph<T::AccountId> {
1376+
let stake_threshold = Self::get_stake_threshold();
1377+
let hotkeys: Vec<(u16, T::AccountId)> =
1378+
<Keys<T> as IterableStorageDoubleMap<u16, u16, T::AccountId>>::iter_prefix(netuid)
1379+
.collect();
1380+
let validator_permits: Vec<bool> = Self::get_validator_permit(netuid);
1381+
1382+
// filter according to validator_permits
1383+
let hotkeys: Vec<&(u16, T::AccountId)> = hotkeys
1384+
.iter()
1385+
.filter(|(uid, _)| *validator_permits.get(*uid as usize).unwrap_or(&false))
1386+
.collect::<Vec<_>>();
1387+
1388+
// map hotkeys to validators with stake
1389+
let mut validators: Vec<(u16, I64F64)> = hotkeys
1390+
.iter()
1391+
.map(|(uid, hotkey)| {
1392+
let stake = Self::get_stake_weights_for_hotkey_on_subnet(hotkey, netuid);
1393+
(*uid, stake.0)
1394+
})
1395+
.collect();
1396+
1397+
// sort validators by stake
1398+
validators.sort_by(|a, b| a.1.cmp(&b.1));
1399+
1400+
let validators: Vec<Compact<u16>> = validators
1401+
.iter()
1402+
.filter(|(_uid, stake)| *stake > stake_threshold)
1403+
.map(|(uid, _)| Compact::from(*uid))
1404+
.collect::<Vec<_>>();
1405+
1406+
SelectiveMetagraph {
1407+
// Subnet index
1408+
netuid: netuid.into(),
1409+
validators: Some(validators),
1410+
..Default::default()
1411+
}
1412+
}
13661413
}
13671414

13681415
#[test]
@@ -1441,6 +1488,7 @@ fn test_selective_metagraph() {
14411488
total_stake: None,
14421489
tao_dividends_per_hotkey: None,
14431490
alpha_dividends_per_hotkey: None,
1491+
validators: None,
14441492
};
14451493

14461494
// test init value

0 commit comments

Comments
 (0)