Skip to content

Commit 44d9e75

Browse files
committed
Add test for stake weights
1 parent 1858438 commit 44d9e75

File tree

7 files changed

+84
-3
lines changed

7 files changed

+84
-3
lines changed

node/src/chain_spec/localnet.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ pub fn localnet_config() -> Result<ChainSpec, String> {
3535
vec![
3636
// Keys for debug
3737
authority_keys_from_seed("Alice"),
38-
authority_keys_from_seed("Bob"),
38+
// authority_keys_from_seed("Bob"),
3939
],
4040
// Pre-funded accounts
4141
true,

pallets/subtensor/src/epoch/run_epoch.rs

+1
Original file line numberDiff line numberDiff line change
@@ -669,6 +669,7 @@ impl<T: Config> Pallet<T> {
669669
// ===================
670670
// == Value storage ==
671671
// ===================
672+
log::warn!("netuid = {}, &stake = {:?}", netuid, &stake);
672673
let cloned_stake_weight: Vec<u16> = stake
673674
.iter()
674675
.map(|xi| fixed_proportion_to_u16(*xi))

pallets/subtensor/src/staking/alpha.rs

+2
Original file line numberDiff line numberDiff line change
@@ -84,6 +84,7 @@ impl<T: Config> Pallet<T> {
8484
pub fn get_stake_weights_for_network(netuid: u16) -> (Vec<I32F32>, Vec<u64>, Vec<u64>) {
8585
// Step 1: Get the subnet size (number of neurons).
8686
let n: u16 = Self::get_subnetwork_n(netuid);
87+
log::warn!("netuid = {:?}", netuid);
8788

8889
// Step 2: Retrieve all hotkeys (neuron keys) on this subnet.
8990
let hotkeys: Vec<(u16, T::AccountId)> =
@@ -96,6 +97,7 @@ impl<T: Config> Pallet<T> {
9697
let mut raw_alpha_stake: Vec<u64> = vec![0; n as usize];
9798
for (uid_i, hotkey) in &hotkeys {
9899
let alpha: u64 = Self::get_inherited_alpha_for_hotkey_on_subnet(hotkey, netuid);
100+
log::warn!("uid = {:?}, alpha = {:?}", uid_i, alpha);
99101
alpha_stake[*uid_i as usize] = I64F64::from_num(alpha);
100102
raw_alpha_stake[*uid_i as usize] = alpha;
101103
}

pallets/subtensor/src/staking/stake_ops.rs

+6-2
Original file line numberDiff line numberDiff line change
@@ -126,8 +126,12 @@ impl<T: Config> Pallet<T> {
126126
let subnet_tao: I96F32 = I96F32::from_num(SubnetTAO::<T>::get(netuid));
127127
let subnet_alpha: I96F32 = I96F32::from_num(SubnetAlphaIn::<T>::get(netuid));
128128

129-
log::warn!("subnet_tao = {:?}", subnet_tao);
130-
log::warn!("subnet_alpha = {:?}", subnet_alpha);
129+
log::warn!(
130+
"hotkey = {:?}, subnet_tao = {:?}, subnet_alpha = {:?}",
131+
&hotkey,
132+
subnet_tao,
133+
subnet_alpha
134+
);
131135

132136
// Step 3b: Compute constant product k = alpha * tao
133137
// This is the key to the dynamic mechanism: k remains constant

pallets/subtensor/tests/staking.rs

+70
Original file line numberDiff line numberDiff line change
@@ -2490,3 +2490,73 @@ fn test_anneal_global_weight() {
24902490
);
24912491
});
24922492
}
2493+
2494+
// https://github.com/opentensor/subtensor/issues/925
2495+
// RUST_LOG=warn cargo test -p pallet-subtensor --test staking -- test_stake_weight_should_not_be_affected_by_zero_stakes --exact --nocapture
2496+
#[test]
2497+
fn test_stake_weight_should_not_be_affected_by_zero_stakes() {
2498+
new_test_ext(1).execute_with(|| {
2499+
let registrar = AccountId::from(42);
2500+
Balances::force_set_balance(RuntimeOrigin::root(), registrar, 10_000_000_000_000).unwrap();
2501+
2502+
// the root subnet
2503+
SubtensorModule::register_network_with_identity(
2504+
RuntimeOrigin::signed(registrar),
2505+
registrar,
2506+
1,
2507+
None,
2508+
)
2509+
.unwrap();
2510+
2511+
// our test subnet with id 1
2512+
SubtensorModule::register_network_with_identity(
2513+
RuntimeOrigin::signed(registrar),
2514+
registrar,
2515+
1,
2516+
None,
2517+
)
2518+
.unwrap();
2519+
2520+
2521+
// SubtensorModule::add_stake(
2522+
// RuntimeOrigin::signed(registrar),
2523+
// registrar,
2524+
// 1,
2525+
// 1000_000_000_000,
2526+
// )
2527+
// .unwrap();
2528+
2529+
// step_epochs(1, 1);
2530+
2531+
2532+
let neuron_1 = AccountId::from(123);
2533+
Balances::force_set_balance(RuntimeOrigin::root(), neuron_1, 10_000_000_000_000).unwrap();
2534+
2535+
SubtensorModule::burned_register(RuntimeOrigin::signed(neuron_1), 1, neuron_1).unwrap();
2536+
SubtensorModule::add_stake(
2537+
RuntimeOrigin::signed(neuron_1),
2538+
neuron_1,
2539+
1,
2540+
1000_000_000_000,
2541+
)
2542+
.unwrap();
2543+
2544+
// step_epochs(1, 1);
2545+
2546+
let neuron_2 = AccountId::from(321);
2547+
Balances::force_set_balance(RuntimeOrigin::root(), neuron_2, 10_000_000_000_000).unwrap();
2548+
2549+
SubtensorModule::burned_register(RuntimeOrigin::signed(neuron_2), 1, neuron_2).unwrap();
2550+
SubtensorModule::add_stake(
2551+
RuntimeOrigin::signed(neuron_2),
2552+
neuron_2,
2553+
1,
2554+
1000_000_000_000,
2555+
)
2556+
.unwrap();
2557+
SubtensorModule::epoch(1, 10_000_000_000_000);
2558+
// step_epochs(1, 1);
2559+
2560+
dbg!(SubtensorModule::get_neurons(1));
2561+
});
2562+
}

runtime/src/lib.rs

+2
Original file line numberDiff line numberDiff line change
@@ -2069,6 +2069,7 @@ impl_runtime_apis! {
20692069
impl subtensor_custom_rpc_runtime_api::NeuronInfoRuntimeApi<Block> for Runtime {
20702070
fn get_neurons_lite(netuid: u16) -> Vec<u8> {
20712071
let result = SubtensorModule::get_neurons_lite(netuid);
2072+
log::warn!("{:?}", result);
20722073
result.encode()
20732074
}
20742075

@@ -2089,6 +2090,7 @@ impl_runtime_apis! {
20892090

20902091
fn get_neuron(netuid: u16, uid: u16) -> Vec<u8> {
20912092
let _result = SubtensorModule::get_neuron(netuid, uid);
2093+
log::warn!("{:?}", _result);
20922094
if _result.is_some() {
20932095
let result = _result.expect("Could not get NeuronInfo");
20942096
result.encode()

scripts/localnet.sh

+2
Original file line numberDiff line numberDiff line change
@@ -80,6 +80,7 @@ alice_start=(
8080
--validator
8181
--rpc-cors=all
8282
--allow-private-ipv4
83+
--log warn
8384
--discover-local
8485
--unsafe-force-node-key-generation
8586
)
@@ -95,6 +96,7 @@ bob_start=(
9596
--rpc-cors=all
9697
--allow-private-ipv4
9798
--discover-local
99+
--log warn
98100
--unsafe-force-node-key-generation
99101
)
100102

0 commit comments

Comments
 (0)