Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
45 changes: 24 additions & 21 deletions pallets/subtensor/src/delegate_info.rs
Original file line number Diff line number Diff line change
@@ -1,61 +1,64 @@
use super::*;
use frame_support::IterableStorageDoubleMap;
use serde::{Serialize, Deserialize};
use frame_support::storage::IterableStorageMap;
use frame_support::pallet_prelude::{Decode, Encode};
extern crate alloc;
use alloc::vec::Vec;
use sp_core::hexdisplay::AsBytesRef;
use codec::Compact;


#[derive(Decode, Encode, Serialize, Deserialize, PartialEq, Eq, Clone, Debug)]
#[serde(deny_unknown_fields)]
pub struct DelegateInfo {
delegate_ss58: DeAccountId,
take: u16,
nominators: Vec<(DeAccountId, u64)>, // map of nominator_ss58 to stake amount
owner_ss58: DeAccountId
#[derive(Decode, Encode, PartialEq, Eq, Clone, Debug)]
pub struct DelegateInfo<T: Config> {
delegate_ss58: T::AccountId,
take: Compact<u16>,
nominators: Vec<(T::AccountId, Compact<u64>)>, // map of nominator_ss58 to stake amount
owner_ss58: T::AccountId
}

impl<T: Config> Pallet<T> {
pub fn get_delegate( delegate_account_vec: Vec<u8> ) -> Option<DelegateInfo> {
pub fn get_delegate( delegate_account_vec: Vec<u8> ) -> Option<DelegateInfo<T>> {
if delegate_account_vec.len() != 32 {
return None;
}

let delegate: AccountIdOf<T> = T::AccountId::decode( &mut delegate_account_vec.as_bytes_ref() ).unwrap();
// Check delegate exists
if !<Delegates<T>>::contains_key( delegate.clone() ) {
return None;
}

let mut nominators = Vec::<(DeAccountId, u64)>::new();
let mut nominators = Vec::<(T::AccountId, Compact<u64>)>::new();

for ( nominator, stake ) in < Stake<T> as IterableStorageDoubleMap<T::AccountId, T::AccountId, u64> >::iter_prefix( delegate.clone() ) {
nominators.push( ( nominator.clone().encode().into(), stake ) );
nominators.push( ( nominator.clone(), stake.into() ) );
}

let owner = <Owner<T>>::get( delegate.clone() );

return Some( DelegateInfo {
delegate_ss58: delegate.clone().encode().into(),
take: <Delegates<T>>::get( delegate.clone() ),
delegate_ss58: delegate.clone(),
take: <Delegates<T>>::get( delegate.clone() ).into(),
nominators,
owner_ss58: owner.clone().encode().into()
owner_ss58: owner.clone()
});
}

pub fn get_delegates() -> Vec<DelegateInfo> {
let mut delegates = Vec::<DelegateInfo>::new();
pub fn get_delegates() -> Vec<DelegateInfo<T>> {
let mut delegates = Vec::<DelegateInfo<T>>::new();
for ( delegate, take ) in < Delegates<T> as IterableStorageMap<T::AccountId, u16> >::iter() {
let mut nominators = Vec::<(DeAccountId, u64)>::new();
let mut nominators = Vec::<(T::AccountId, Compact<u64>)>::new();
for ( nominator, stake ) in < Stake<T> as IterableStorageDoubleMap<T::AccountId, T::AccountId, u64> >::iter_prefix( delegate.clone() ) {
nominators.push( ( nominator.clone().encode().into(), stake ) );
nominators.push( ( nominator.clone(), stake.into() ) );
}

let owner = <Owner<T>>::get( delegate.clone() );

delegates.push( DelegateInfo {
delegate_ss58: delegate.clone().encode().into(),
take,
delegate_ss58: delegate.clone(),
take: take.into(),
nominators,
owner_ss58: owner.clone().encode().into()
owner_ss58: owner.clone()
});
}

Expand Down
22 changes: 2 additions & 20 deletions pallets/subtensor/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -169,20 +169,6 @@ pub mod pallet {

pub type AccountIdOf<T> = <T as frame_system::Config>::AccountId;

#[derive(Decode, Encode, Serialize, Deserialize, PartialEq, Eq, Clone, Debug)]
pub struct DeAccountId { // allows us to de/serialize the account id as a u8 vec
#[serde(with = "serde_bytes")]
id: Vec<u8>
}

impl From<Vec<u8>> for DeAccountId {
fn from(v: Vec<u8>) -> Self {
DeAccountId {
id: v.clone()
}
}
}

// ============================
// ==== Staking + Accounts ====
// ============================
Expand Down Expand Up @@ -321,12 +307,10 @@ pub mod pallet {
// --- Struct for Axon.
pub type AxonInfoOf = AxonInfo;

#[serde_as]
#[derive(Encode, Decode, Default, TypeInfo, Clone, PartialEq, Eq, Debug, Serialize, Deserialize)]
#[derive(Encode, Decode, Default, TypeInfo, Clone, PartialEq, Eq, Debug)]
pub struct AxonInfo {
pub block: u64, // --- Axon serving block.
pub version: u32, // --- Axon version
#[serde_as(as = "DisplayFromStr")] // serialize as string, deserialize from string
pub ip: u128, // --- Axon u128 encoded ip address of type v6 or v4.
pub port: u16, // --- Axon u16 encoded port.
pub ip_type: u8, // --- Axon ip type, 4 for ipv4 and 6 for ipv6.
Expand All @@ -337,12 +321,10 @@ pub mod pallet {

// --- Struct for Prometheus.
pub type PrometheusInfoOf = PrometheusInfo;
#[serde_as]
#[derive(Encode, Decode, Default, TypeInfo, Clone, PartialEq, Eq, Debug, Serialize, Deserialize)]
#[derive(Encode, Decode, Default, TypeInfo, Clone, PartialEq, Eq, Debug)]
pub struct PrometheusInfo {
pub block: u64, // --- Prometheus serving block.
pub version: u32, // --- Prometheus version.
#[serde_as(as = "DisplayFromStr")] // serialize as string, deserialize from string
pub ip: u128, // --- Prometheus u128 encoded ip address of type v6 or v4.
pub port: u16, // --- Prometheus u16 encoded port.
pub ip_type: u8, // --- Prometheus ip type, 4 for ipv4 and 6 for ipv6.
Expand Down
91 changes: 44 additions & 47 deletions pallets/subtensor/src/neuron_info.rs
Original file line number Diff line number Diff line change
@@ -1,38 +1,36 @@
use super::*;
use crate::math::*;
use serde::{Serialize, Deserialize};
use frame_support::storage::IterableStorageDoubleMap;
use frame_support::pallet_prelude::{Decode, Encode};
extern crate alloc;
use alloc::vec::Vec;
use codec::Compact;

#[derive(Decode, Encode, Serialize, Deserialize, PartialEq, Eq, Clone, Debug)]
#[serde(deny_unknown_fields)]
pub struct NeuronInfo {
hotkey: DeAccountId,
coldkey: DeAccountId,
uid: u16,
netuid: u16,
#[derive(Decode, Encode, PartialEq, Eq, Clone, Debug)]
pub struct NeuronInfo<T: Config> {
hotkey: T::AccountId,
coldkey: T::AccountId,
uid: Compact<u16>,
netuid: Compact<u16>,
active: bool,
axon_info: AxonInfo,
prometheus_info: PrometheusInfo,
stake: Vec<(DeAccountId, u64)>, // map of coldkey to stake on this neuron/hotkey (includes delegations)
rank: u16,
emission: u64,
incentive: u16,
consensus: u16,
trust: u16,
validator_trust: u16,
dividends: u16,
last_update: u64,
stake: Vec<(T::AccountId, Compact<u64>)>, // map of coldkey to stake on this neuron/hotkey (includes delegations)
rank: Compact<u16>,
emission: Compact<u64>,
incentive: Compact<u16>,
consensus: Compact<u16>,
trust: Compact<u16>,
validator_trust: Compact<u16>,
dividends: Compact<u16>,
last_update: Compact<u64>,
validator_permit: bool,
weights: Vec<u16>, // Vec uid to weight
bonds: Vec<u16>, // Vec uid to bond
pruning_score: u16
weights: Vec<(Compact<u16>, Compact<u16>)>, // Vec of (uid, weight)
bonds: Vec<(Compact<u16>, Compact<u16>)>, // Vec of (uid, bond)
pruning_score: Compact<u16>,
}

impl<T: Config> Pallet<T> {
pub fn get_neurons(netuid: u16) -> Vec<NeuronInfo> {
pub fn get_neurons(netuid: u16) -> Vec<NeuronInfo<T>> {
if !Self::if_subnet_exist(netuid) {
return Vec::new();
}
Expand All @@ -57,7 +55,7 @@ impl<T: Config> Pallet<T> {
neurons
}

fn get_neuron_subnet_exists(netuid: u16, uid: u16) -> Option<NeuronInfo> {
fn get_neuron_subnet_exists(netuid: u16, uid: u16) -> Option<NeuronInfo<T>> {
let _hotkey = Self::get_hotkey_for_net_and_uid(netuid, uid);
let hotkey;
if _hotkey.is_err() {
Expand Down Expand Up @@ -86,46 +84,45 @@ impl<T: Config> Pallet<T> {
let last_update = Self::get_last_update_for_uid( netuid, uid as u16 );
let validator_permit = Self::get_validator_permit_for_uid( netuid, uid as u16 );

let weights = Self::get_weights(netuid)[uid as usize].iter()
.map(|x| fixed_proportion_to_u16(*x)).collect::<Vec<u16>>();
let weights = <Weights<T>>::get(netuid, uid).iter()
.filter_map(|(i, w)| if *w > 0 { Some((i.into(), w.into())) } else { None })
.collect::<Vec<(Compact<u16>, Compact<u16>)>>();

let bonds = Self::get_bonds(netuid)[uid as usize].iter()
.map(|x| fixed_proportion_to_u16(*x)).collect::<Vec<u16>>();
let bonds = <Bonds<T>>::get(netuid, uid).iter()
.filter_map(|(i, b)| if *b > 0 { Some((i.into(), b.into())) } else { None })
.collect::<Vec<(Compact<u16>, Compact<u16>)>>();

let mut stakes = Vec::<(DeAccountId, u64)>::new();
for ( coldkey, stake ) in < Stake<T> as IterableStorageDoubleMap<T::AccountId, T::AccountId, u64> >::iter_prefix( hotkey.clone() ) {
stakes.push( (coldkey.clone().encode().into(), stake) );
}

let stake = stakes;
let stake: Vec<(T::AccountId, Compact<u64>)> = < Stake<T> as IterableStorageDoubleMap<T::AccountId, T::AccountId, u64> >::iter_prefix( hotkey.clone() )
.map(|(coldkey, stake)| (coldkey, stake.into()))
.collect();

let neuron = NeuronInfo {
hotkey: hotkey.clone().encode().into(),
coldkey: coldkey.clone().encode().into(),
uid,
netuid,
hotkey: hotkey.clone(),
coldkey: coldkey.clone(),
uid: uid.into(),
netuid: netuid.into(),
active,
axon_info,
prometheus_info,
stake,
rank,
emission,
incentive,
consensus,
trust,
validator_trust,
dividends,
last_update,
rank: rank.into(),
emission: emission.into(),
incentive: incentive.into(),
consensus: consensus.into(),
trust: trust.into(),
validator_trust: validator_trust.into(),
dividends: dividends.into(),
last_update: last_update.into(),
validator_permit,
weights,
bonds,
pruning_score
pruning_score: pruning_score.into()
};

return Some(neuron);
}

pub fn get_neuron(netuid: u16, uid: u16) -> Option<NeuronInfo> {
pub fn get_neuron(netuid: u16, uid: u16) -> Option<NeuronInfo<T>> {
if !Self::if_subnet_exist(netuid) {
return None;
}
Expand Down
91 changes: 45 additions & 46 deletions pallets/subtensor/src/subnet_info.rs
Original file line number Diff line number Diff line change
@@ -1,35 +1,34 @@
use super::*;
use frame_support::IterableStorageDoubleMap;
use serde::{Serialize, Deserialize};
use frame_support::storage::IterableStorageMap;
use frame_support::pallet_prelude::{Decode, Encode};
extern crate alloc;
use alloc::vec::Vec;
use codec::Compact;

#[derive(Decode, Encode, Serialize, Deserialize, PartialEq, Eq, Clone, Debug)]
#[serde(deny_unknown_fields)]
#[derive(Decode, Encode, PartialEq, Eq, Clone, Debug)]
pub struct SubnetInfo {
netuid: u16,
rho: u16,
kappa: u16,
difficulty: u64,
immunity_period: u16,
validator_batch_size: u16,
validator_sequence_length: u16,
validator_epochs_per_reset: u16,
validator_epoch_length: u16,
max_allowed_validators: u16,
min_allowed_weights: u16,
max_weights_limit: u16,
scaling_law_power: u16,
synergy_scaling_law_power: u16,
subnetwork_n: u16,
max_allowed_uids: u16,
blocks_since_last_step: u64,
tempo: u16,
network_modality: u16,
network_connect: Vec<[u16; 2]>,
emission_values: u64
netuid: Compact<u16>,
rho: Compact<u16>,
kappa: Compact<u16>,
difficulty: Compact<u64>,
immunity_period: Compact<u16>,
validator_batch_size: Compact<u16>,
validator_sequence_length: Compact<u16>,
validator_epochs_per_reset: Compact<u16>,
validator_epoch_length: Compact<u16>,
max_allowed_validators: Compact<u16>,
min_allowed_weights: Compact<u16>,
max_weights_limit: Compact<u16>,
scaling_law_power: Compact<u16>,
synergy_scaling_law_power: Compact<u16>,
subnetwork_n: Compact<u16>,
max_allowed_uids: Compact<u16>,
blocks_since_last_step: Compact<u64>,
tempo: Compact<u16>,
network_modality: Compact<u16>,
network_connect: Vec<[Compact<u16>; 2]>,
emission_values: Compact<u64>
}

impl<T: Config> Pallet<T> {
Expand Down Expand Up @@ -59,34 +58,34 @@ impl<T: Config> Pallet<T> {
let emission_values = Self::get_emission_value(netuid);


let mut network_connect: Vec<[u16; 2]> = Vec::<[u16; 2]>::new();
let mut network_connect: Vec<[Compact<u16>; 2]> = Vec::<[Compact<u16>; 2]>::new();

for ( _netuid_, con_req) in < NetworkConnect<T> as IterableStorageDoubleMap<u16, u16, u16> >::iter_prefix(netuid) {
network_connect.push([_netuid_, con_req]);
network_connect.push([_netuid_.into(), con_req.into()]);
}

return Some(SubnetInfo {
rho,
kappa,
difficulty,
immunity_period,
netuid,
validator_batch_size,
validator_sequence_length,
validator_epochs_per_reset,
validator_epoch_length,
max_allowed_validators,
min_allowed_weights,
max_weights_limit,
scaling_law_power,
synergy_scaling_law_power,
subnetwork_n,
max_allowed_uids,
blocks_since_last_step,
tempo,
network_modality,
rho: rho.into(),
kappa: kappa.into(),
difficulty: difficulty.into(),
immunity_period: immunity_period.into(),
netuid: netuid.into(),
validator_batch_size: validator_batch_size.into(),
validator_sequence_length: validator_sequence_length.into(),
validator_epochs_per_reset: validator_epochs_per_reset.into(),
validator_epoch_length: validator_epoch_length.into(),
max_allowed_validators: max_allowed_validators.into(),
min_allowed_weights: min_allowed_weights.into(),
max_weights_limit: max_weights_limit.into(),
scaling_law_power: scaling_law_power.into(),
synergy_scaling_law_power: synergy_scaling_law_power.into(),
subnetwork_n: subnetwork_n.into(),
max_allowed_uids: max_allowed_uids.into(),
blocks_since_last_step: blocks_since_last_step.into(),
tempo: tempo.into(),
network_modality: network_modality.into(),
network_connect,
emission_values
emission_values: emission_values.into()
})
}

Expand Down
Loading