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

Fix stake weight issues #998

Merged
merged 4 commits into from
Nov 25, 2024
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
19 changes: 8 additions & 11 deletions pallets/subtensor/src/rpc_info/neuron_info.rs
Original file line number Diff line number Diff line change
Expand Up @@ -79,9 +79,9 @@ impl<T: Config> Pallet<T> {

let axon_info = Self::get_axon_info(netuid, &hotkey.clone());

let prometheus_info = Self::get_prometheus_info(netuid, &hotkey.clone());
let prometheus_info = Self::get_prometheus_info(netuid, &hotkey);

let coldkey = Owner::<T>::get(hotkey.clone()).clone();
let coldkey = Owner::<T>::get(&hotkey).clone();

let active = Self::get_active_for_uid(netuid, uid);
let rank = Self::get_rank_for_uid(netuid, uid);
Expand Down Expand Up @@ -119,8 +119,8 @@ impl<T: Config> Pallet<T> {
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 neuron = NeuronInfo {
hotkey: hotkey.clone(),
coldkey: coldkey.clone(),
hotkey,
coldkey,
uid: uid.into(),
netuid: netuid.into(),
active,
Expand Down Expand Up @@ -175,15 +175,12 @@ impl<T: Config> Pallet<T> {
let pruning_score = Self::get_pruning_score_for_uid(netuid, uid);
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>)> = vec![(
coldkey.clone(),
Self::get_stake_for_hotkey_on_subnet(&hotkey, netuid).into(),
)];
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 neuron = NeuronInfoLite {
hotkey: hotkey.clone(),
coldkey: coldkey.clone(),
hotkey,
coldkey,
uid: uid.into(),
netuid: netuid.into(),
active,
Expand Down
6 changes: 4 additions & 2 deletions pallets/subtensor/src/subnets/subnet.rs
Original file line number Diff line number Diff line change
Expand Up @@ -191,8 +191,10 @@ impl<T: Config> Pallet<T> {
NetworkRegisteredAt::<T>::insert(netuid_to_register, current_block);

// --- 14. Init the pool by putting the lock as the initial alpha.
SubnetTAO::<T>::insert(netuid_to_register, 1); // add the TAO to the pool.
SubnetAlphaIn::<T>::insert(netuid_to_register, 1); // Set the alpha in based on the lock.
// add the TAO to the pool.
SubnetTAO::<T>::insert(netuid_to_register, 1_000_000_000);
// Set the alpha in based on the lock.
SubnetAlphaIn::<T>::insert(netuid_to_register, 1_000_000_000);
SubnetOwner::<T>::insert(netuid_to_register, coldkey.clone());
SubnetOwnerHotkey::<T>::insert(netuid_to_register, hotkey.clone());

Expand Down
117 changes: 117 additions & 0 deletions pallets/subtensor/tests/staking.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2494,3 +2494,120 @@ fn test_anneal_global_weight() {
);
});
}

// https://github.com/opentensor/subtensor/issues/925
// cargo test -p pallet-subtensor --test staking -- test_stake_weight_should_not_be_affected_by_zero_stakes --exact --nocapture
#[test]
#[allow(clippy::indexing_slicing)]
fn test_stake_weight_should_not_be_affected_by_zero_stakes() {
new_test_ext(1).execute_with(|| {
let registrar = AccountId::from(42); // SN Owner
let netuid: u16 = 1;
Balances::force_set_balance(RuntimeOrigin::root(), registrar, 10_000_000_000_000).unwrap();

// Add root network
add_network(0, 0, 0);

// our test subnet with id 1
SubtensorModule::register_network_with_identity(
RuntimeOrigin::signed(registrar),
registrar,
1,
None,
)
.unwrap();

let neuron_1 = AccountId::from(123);
SubtensorModule::add_balance_to_coldkey_account(&neuron_1, 10_000_000_000_000);

SubtensorModule::burned_register(RuntimeOrigin::signed(neuron_1), netuid, neuron_1)
.unwrap();
SubtensorModule::add_stake(
RuntimeOrigin::signed(neuron_1),
neuron_1,
netuid,
50_000_000,
)
.unwrap();

let stake_weights_1 = SubtensorModule::get_stake_weights_for_network(netuid);

// Check neuron 1's alpha
assert!(
stake_weights_1.0[1] > 0,
"The neuron 1 should get at least some stake"
);

let neuron_2 = AccountId::from(321);
SubtensorModule::add_balance_to_coldkey_account(&neuron_2, 10_000_000_000_000);

SubtensorModule::burned_register(RuntimeOrigin::signed(neuron_2), netuid, neuron_2)
.unwrap();
SubtensorModule::add_stake(
RuntimeOrigin::signed(neuron_2),
neuron_2,
netuid,
50_000_000,
)
.unwrap();

let stake_weights_2 = SubtensorModule::get_stake_weights_for_network(netuid);

// Check neuron 2's alpha
assert!(
stake_weights_2.0[2] > 0,
"The neuron 2 should get at least some stake"
);
});
}

// cargo test -p pallet-subtensor --test staking -- test_stake_weight_no_subnet_stake --exact --nocapture
#[test]
#[allow(clippy::indexing_slicing)]
fn test_stake_weight_no_subnet_stake() {
new_test_ext(1).execute_with(|| {
let registrar = AccountId::from(42); // SN Owner
let netuid: u16 = 1;
Balances::force_set_balance(RuntimeOrigin::root(), registrar, 10_000_000_000_000).unwrap();

// Add root network
add_network(0, 0, 0);

// our test subnet with id 1
SubtensorModule::register_network_with_identity(
RuntimeOrigin::signed(registrar),
registrar,
1,
None,
)
.unwrap();

let neuron_1 = AccountId::from(123);
SubtensorModule::add_balance_to_coldkey_account(&neuron_1, 10_000_000_000_000);

SubtensorModule::burned_register(RuntimeOrigin::signed(neuron_1), netuid, neuron_1)
.unwrap();

let stake_weights_1 = SubtensorModule::get_stake_weights_for_network(netuid);

// Check neuron 1's alpha
assert!(
stake_weights_1.0[1] == 0,
"The neuron 1's stake should be 0"
);

let neuron_2 = AccountId::from(321);
SubtensorModule::add_balance_to_coldkey_account(&neuron_2, 10_000_000_000_000);

SubtensorModule::burned_register(RuntimeOrigin::signed(neuron_2), netuid, neuron_2)
.unwrap();

let stake_weights_2 = SubtensorModule::get_stake_weights_for_network(netuid);

// Check neuron 2's alpha
assert!(
stake_weights_2.0[2] == 0,
"The neuron 2's stake should be 0"
);
});
}
Loading