Skip to content

Commit 4aad852

Browse files
committed
make delegate info useful again
1 parent 2a229a9 commit 4aad852

File tree

1 file changed

+44
-27
lines changed

1 file changed

+44
-27
lines changed

pallets/subtensor/src/rpc_info/delegate_info.rs

Lines changed: 44 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,18 @@
11
use super::*;
22
use frame_support::pallet_prelude::{Decode, Encode};
3-
use frame_support::storage::IterableStorageMap;
4-
use frame_support::IterableStorageDoubleMap;
3+
use frame_support::IterableStorageMap;
54
use safe_math::*;
65
use substrate_fixed::types::U64F64;
76
extern crate alloc;
7+
use alloc::collections::BTreeMap;
88
use codec::Compact;
99

10-
#[freeze_struct("66105c2cfec0608d")]
10+
#[freeze_struct("f729f2481d94a1de")]
1111
#[derive(Decode, Encode, PartialEq, Eq, Clone, Debug, TypeInfo)]
1212
pub struct DelegateInfo<AccountId: TypeInfo + Encode + Decode> {
1313
delegate_ss58: AccountId,
1414
take: Compact<u16>,
15-
nominators: Vec<(AccountId, Compact<u64>)>, // map of nominator_ss58 to stake amount
15+
nominators: Vec<(AccountId, Vec<(Compact<u16>, Compact<u64>)>)>, // map of nominator_ss58 to netuid and stake amount
1616
owner_ss58: AccountId,
1717
registrations: Vec<Compact<u16>>, // Vec of netuid this delegate is registered on
1818
validator_permits: Vec<Compact<u16>>, // Vec of netuid this delegate has validator permit on
@@ -49,19 +49,38 @@ impl<T: Config> Pallet<T> {
4949
Self::return_per_1000_tao(take, total_stake, emissions_per_day)
5050
}
5151

52-
fn get_delegate_by_existing_account(delegate: AccountIdOf<T>) -> DelegateInfo<T::AccountId> {
53-
let mut nominators = Vec::<(T::AccountId, Compact<u64>)>::new();
52+
fn get_delegate_by_existing_account(
53+
delegate: AccountIdOf<T>,
54+
skip_nominators: bool,
55+
) -> DelegateInfo<T::AccountId> {
56+
let mut nominators = Vec::<(T::AccountId, Vec<(Compact<u16>, Compact<u64>)>)>::new();
57+
let mut nominator_map = BTreeMap::<T::AccountId, Vec<(Compact<u16>, Compact<u64>)>>::new();
58+
59+
if !skip_nominators {
60+
let mut alpha_share_pools = vec![];
61+
for netuid in Self::get_all_subnet_netuids() {
62+
let alpha_share_pool = Self::get_alpha_share_pool(delegate.clone(), netuid);
63+
alpha_share_pools.push(alpha_share_pool);
64+
}
65+
66+
for ((nominator, netuid), alpha_stake) in Alpha::<T>::iter_prefix((delegate.clone(),)) {
67+
if alpha_stake == 0 {
68+
continue;
69+
}
70+
71+
if let Some(alpha_share_pool) = alpha_share_pools.get(netuid as usize) {
72+
let coldkey_stake = alpha_share_pool.get_value_from_shares(alpha_stake);
5473

55-
for (nominator, stake) in
56-
<Stake<T> as IterableStorageDoubleMap<T::AccountId, T::AccountId, u64>>::iter_prefix(
57-
delegate.clone(),
58-
)
59-
{
60-
if stake == 0 {
61-
continue;
74+
nominator_map
75+
.entry(nominator.clone())
76+
.or_insert(Vec::new())
77+
.push((netuid.into(), coldkey_stake.into()));
78+
}
79+
}
80+
81+
for (nominator, stakes) in nominator_map {
82+
nominators.push((nominator, stakes));
6283
}
63-
// Only add nominators with stake
64-
nominators.push((nominator.clone(), stake.into()));
6584
}
6685

6786
let registrations = Self::get_registered_networks_for_hotkey(&delegate.clone());
@@ -112,7 +131,7 @@ impl<T: Config> Pallet<T> {
112131
return None;
113132
}
114133

115-
let delegate_info = Self::get_delegate_by_existing_account(delegate.clone());
134+
let delegate_info = Self::get_delegate_by_existing_account(delegate.clone(), false);
116135
Some(delegate_info)
117136
}
118137

@@ -121,7 +140,7 @@ impl<T: Config> Pallet<T> {
121140
pub fn get_delegates() -> Vec<DelegateInfo<T::AccountId>> {
122141
let mut delegates = Vec::<DelegateInfo<T::AccountId>>::new();
123142
for delegate in <Delegates<T> as IterableStorageMap<T::AccountId, u16>>::iter_keys() {
124-
let delegate_info = Self::get_delegate_by_existing_account(delegate.clone());
143+
let delegate_info = Self::get_delegate_by_existing_account(delegate.clone(), false);
125144
delegates.push(delegate_info);
126145
}
127146

@@ -136,16 +155,14 @@ impl<T: Config> Pallet<T> {
136155
let mut delegates: Vec<(DelegateInfo<T::AccountId>, Compact<u64>)> = Vec::new();
137156
for delegate in <Delegates<T> as IterableStorageMap<T::AccountId, u16>>::iter_keys() {
138157
// Staked to this delegate, so add to list
139-
let delegate_info = Self::get_delegate_by_existing_account(delegate.clone());
140-
delegates.push((
141-
delegate_info,
142-
Self::get_stake_for_hotkey_and_coldkey_on_subnet(
143-
&delegatee,
144-
&delegate,
145-
Self::get_root_netuid(),
146-
)
147-
.into(),
148-
));
158+
for (netuid, _) in Alpha::<T>::iter_prefix((delegate.clone(), delegatee.clone())) {
159+
let delegate_info = Self::get_delegate_by_existing_account(delegate.clone(), true);
160+
delegates.push((
161+
delegate_info,
162+
Self::get_stake_for_hotkey_and_coldkey_on_subnet(&delegate, &delegatee, netuid)
163+
.into(),
164+
));
165+
}
149166
}
150167

151168
delegates

0 commit comments

Comments
 (0)