Skip to content

Commit

Permalink
Add test for stake value consistency
Browse files Browse the repository at this point in the history
  • Loading branch information
ales-otf committed Oct 30, 2024
1 parent 44d9fee commit 5dcb332
Show file tree
Hide file tree
Showing 3 changed files with 43 additions and 0 deletions.
12 changes: 12 additions & 0 deletions pallets/subtensor/src/rpc_info/neuron_info.rs
Original file line number Diff line number Diff line change
Expand Up @@ -28,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 @@ -52,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
6 changes: 6 additions & 0 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 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);
});
}

0 comments on commit 5dcb332

Please sign in to comment.