diff --git a/pallets/subtensor/src/rpc_info/neuron_info.rs b/pallets/subtensor/src/rpc_info/neuron_info.rs index 26f222d9c..781f9c5a0 100644 --- a/pallets/subtensor/src/rpc_info/neuron_info.rs +++ b/pallets/subtensor/src/rpc_info/neuron_info.rs @@ -1,6 +1,5 @@ use super::*; use frame_support::pallet_prelude::{Decode, Encode}; -use frame_support::storage::IterableStorageDoubleMap; extern crate alloc; use codec::Compact; @@ -29,6 +28,12 @@ pub struct NeuronInfo { pruning_score: Compact, } +impl NeuronInfo { + pub fn stake(&self) -> &[(T::AccountId, Compact)] { + &self.stake + } +} + #[freeze_struct("c21f0f4f22bcb2a1")] #[derive(Decode, Encode, PartialEq, Eq, Clone, Debug)] pub struct NeuronInfoLite { @@ -53,6 +58,12 @@ pub struct NeuronInfoLite { pruning_score: Compact, } +impl NeuronInfoLite { + pub fn stake(&self) -> &[(T::AccountId, Compact)] { + &self.stake + } +} + impl Pallet { pub fn get_neurons(netuid: u16) -> Vec> { if !Self::if_subnet_exist(netuid) { @@ -117,8 +128,12 @@ impl Pallet { } }) .collect::, Compact)>>(); - let stake_weight: u64 = Self::get_stake_weight(netuid, uid) as u64; - let stake: Vec<(T::AccountId, Compact)> = vec![(coldkey.clone(), stake_weight.into())]; + + let stake: Vec<(T::AccountId, Compact)> = vec![( + coldkey.clone(), + Alpha::::get((&hotkey, coldkey.clone(), netuid)).into(), + )]; + let neuron = NeuronInfo { hotkey: hotkey.clone(), coldkey: coldkey.clone(), @@ -177,12 +192,10 @@ impl Pallet { 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)> = - as IterableStorageDoubleMap>::iter_prefix( - hotkey.clone(), - ) - .map(|(coldkey, stake)| (coldkey, stake.into())) - .collect(); + let stake: Vec<(T::AccountId, Compact)> = vec![( + coldkey.clone(), + Alpha::::get((&hotkey, coldkey.clone(), netuid)).into(), + )]; let neuron = NeuronInfoLite { hotkey: hotkey.clone(), diff --git a/pallets/subtensor/src/rpc_info/stake_info.rs b/pallets/subtensor/src/rpc_info/stake_info.rs index 705f172f6..32b47d15a 100644 --- a/pallets/subtensor/src/rpc_info/stake_info.rs +++ b/pallets/subtensor/src/rpc_info/stake_info.rs @@ -18,6 +18,12 @@ pub struct StakeInfo { is_registered: bool, } +impl StakeInfo { + pub fn stake(&self) -> Compact { + self.stake + } +} + impl Pallet { fn _get_stake_info_for_coldkeys( coldkeys: Vec, @@ -27,22 +33,19 @@ impl Pallet { } let netuids: Vec = Self::get_all_subnet_netuids(); let mut stake_info: Vec<(T::AccountId, Vec>)> = 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::::get(coldkey_i.clone()); + let staking_hotkeys = StakingHotkeys::::get(coldkey_i); let mut stake_info_for_coldkey: Vec> = Vec::new(); - for netuid_i in netuids.clone().iter() { - for hotkey_i in staking_hotkeys.clone().iter() { - let alpha: u64 = - Alpha::::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::::get((hotkey_i, coldkey_i, netuid_i)); let emission: u64 = LastHotkeyColdkeyEmissionOnNetuid::::get(( - hotkey_i.clone(), - coldkey_i.clone(), - *netuid_i, + hotkey_i, coldkey_i, *netuid_i, )); - let drain: u64 = LastHotkeyEmissionDrain::::get(hotkey_i.clone()); + let drain: u64 = LastHotkeyEmissionDrain::::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(), diff --git a/pallets/subtensor/tests/staking.rs b/pallets/subtensor/tests/staking.rs index 4cd2af3b6..03cf79551 100644 --- a/pallets/subtensor/tests/staking.rs +++ b/pallets/subtensor/tests/staking.rs @@ -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; @@ -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); + }); +}