Skip to content
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

Use alpha for stake in neuron info #888

Closed
wants to merge 3 commits into from
Closed
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
31 changes: 22 additions & 9 deletions pallets/subtensor/src/rpc_info/neuron_info.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
use super::*;
use frame_support::pallet_prelude::{Decode, Encode};
use frame_support::storage::IterableStorageDoubleMap;
extern crate alloc;
use codec::Compact;

Expand Down Expand Up @@ -29,6 +28,12 @@ pub struct NeuronInfo<T: Config> {
pruning_score: Compact<u16>,
}

impl<T: Config> NeuronInfo<T> {
pub fn stake(&self) -> &[(T::AccountId, Compact<u64>)] {
&self.stake
}
}

#[freeze_struct("c21f0f4f22bcb2a1")]
#[derive(Decode, Encode, PartialEq, Eq, Clone, Debug)]
pub struct NeuronInfoLite<T: Config> {
Expand All @@ -53,6 +58,12 @@ pub struct NeuronInfoLite<T: Config> {
pruning_score: Compact<u16>,
}

impl<T: Config> NeuronInfoLite<T> {
pub fn stake(&self) -> &[(T::AccountId, Compact<u64>)] {
&self.stake
}
}

impl<T: Config> Pallet<T> {
pub fn get_neurons(netuid: u16) -> Vec<NeuronInfo<T>> {
if !Self::if_subnet_exist(netuid) {
Expand Down Expand Up @@ -117,8 +128,12 @@ impl<T: Config> Pallet<T> {
}
})
.collect::<Vec<(Compact<u16>, Compact<u16>)>>();
let stake_weight: u64 = Self::get_stake_weight(netuid, uid) as u64;
let stake: Vec<(T::AccountId, Compact<u64>)> = vec![(coldkey.clone(), stake_weight.into())];

let stake: Vec<(T::AccountId, Compact<u64>)> = vec![(
coldkey.clone(),
Alpha::<T>::get((&hotkey, coldkey.clone(), netuid)).into(),
)];

let neuron = NeuronInfo {
hotkey: hotkey.clone(),
coldkey: coldkey.clone(),
Expand Down Expand Up @@ -177,12 +192,10 @@ impl<T: Config> Pallet<T> {
let last_update = Self::get_last_update_for_uid(netuid, uid);
let validator_permit = Self::get_validator_permit_for_uid(netuid, uid);

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 stake: Vec<(T::AccountId, Compact<u64>)> = vec![(
coldkey.clone(),
Alpha::<T>::get((&hotkey, coldkey.clone(), netuid)).into(),
)];

let neuron = NeuronInfoLite {
hotkey: hotkey.clone(),
Expand Down
25 changes: 14 additions & 11 deletions pallets/subtensor/src/rpc_info/stake_info.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,12 @@ pub struct StakeInfo<T: Config> {
is_registered: bool,
}

impl<T: Config> StakeInfo<T> {
pub fn stake(&self) -> Compact<u64> {
self.stake
}
}

impl<T: Config> Pallet<T> {
fn _get_stake_info_for_coldkeys(
coldkeys: Vec<T::AccountId>,
Expand All @@ -27,22 +33,19 @@ impl<T: Config> Pallet<T> {
}
let netuids: Vec<u16> = Self::get_all_subnet_netuids();
let mut stake_info: Vec<(T::AccountId, Vec<StakeInfo<T>>)> = Vec::new();
for coldkey_i in coldkeys.clone().iter() {
for coldkey_i in coldkeys.iter() {
// Get all hotkeys associated with this coldkey.
let staking_hotkeys = StakingHotkeys::<T>::get(coldkey_i.clone());
let staking_hotkeys = StakingHotkeys::<T>::get(coldkey_i);
let mut stake_info_for_coldkey: Vec<StakeInfo<T>> = Vec::new();
for netuid_i in netuids.clone().iter() {
for hotkey_i in staking_hotkeys.clone().iter() {
let alpha: u64 =
Alpha::<T>::get((hotkey_i.clone(), coldkey_i.clone(), netuid_i));
for netuid_i in netuids.iter() {
for hotkey_i in staking_hotkeys.iter() {
let alpha: u64 = Alpha::<T>::get((hotkey_i, coldkey_i, netuid_i));
let emission: u64 = LastHotkeyColdkeyEmissionOnNetuid::<T>::get((
hotkey_i.clone(),
coldkey_i.clone(),
*netuid_i,
hotkey_i, coldkey_i, *netuid_i,
));
let drain: u64 = LastHotkeyEmissionDrain::<T>::get(hotkey_i.clone());
let drain: u64 = LastHotkeyEmissionDrain::<T>::get(hotkey_i);
let is_registered: bool =
Self::is_hotkey_registered_on_network(*netuid_i, &hotkey_i);
Self::is_hotkey_registered_on_network(*netuid_i, hotkey_i);
stake_info_for_coldkey.push(StakeInfo {
hotkey: hotkey_i.clone(),
coldkey: coldkey_i.clone(),
Expand Down
25 changes: 25 additions & 0 deletions pallets/subtensor/tests/staking.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
#![allow(clippy::unwrap_used)]
#![allow(clippy::arithmetic_side_effects)]

use codec::Encode;
use frame_support::{assert_err, assert_noop, assert_ok, traits::Currency};
use frame_system::Config;
mod mock;
Expand Down Expand Up @@ -2490,3 +2491,27 @@ fn test_anneal_global_weight() {
);
});
}

// SKIP_WASM_BUILD=1 RUST_LOG=info cargo test --test staking -- test_stake_value_is_alpha --exact --nocapture
#[test]
fn test_stake_value_is_alpha() {
new_test_ext(1).execute_with(|| {
let hotkey_id = U256::from(5445);
let coldkey_id = U256::from(5443433);
let amount: u64 = 10000;
let netuid: u16 = 1;
let tempo: u16 = 13;
let start_nonce: u64 = 0;

add_network(netuid, tempo, 0);

register_ok_neuron(netuid, hotkey_id, coldkey_id, start_nonce);

SubtensorModule::stake_into_subnet(&hotkey_id, &coldkey_id, netuid, amount);

let stake_info = SubtensorModule::get_stake_info_for_coldkey(Encode::encode(&coldkey_id));
let neuron_info = SubtensorModule::get_neuron_lite(netuid, 0);

assert_eq!(stake_info[0].stake(), neuron_info.unwrap().stake()[0].1);
});
}
Loading