Skip to content

Commit

Permalink
removed setting weights on key itself
Browse files Browse the repository at this point in the history
  • Loading branch information
YourUsername committed Mar 12, 2024
1 parent 5a48cdd commit c72e3e8
Show file tree
Hide file tree
Showing 5 changed files with 34 additions and 46 deletions.
1 change: 1 addition & 0 deletions pallets/subspace/src/step.rs
Original file line number Diff line number Diff line change
Expand Up @@ -145,6 +145,7 @@ impl<T: Config> Pallet<T> {
})
.collect();

// Remove self-weight by masking diagonal.
weights = mask_diag_sparse(&weights);

// Normalize remaining weights.
Expand Down
6 changes: 1 addition & 5 deletions pallets/subspace/src/weights.rs
Original file line number Diff line number Diff line change
Expand Up @@ -60,11 +60,7 @@ impl<T: Config> Pallet<T> {
let min_allowed_length: usize = Self::get_min_allowed_weights(netuid) as usize;
let max_allowed_length: usize = Self::get_max_allowed_weights(netuid) as usize;

// TODO: fix this function
ensure!(
!Self::is_self_weight(uid, &uids),
Error::<T>::NoSelfWeight
);
ensure!(!Self::is_self_weight(uid, &uids), Error::<T>::NoSelfWeight);

ensure!(
uids.len() >= min_allowed_length,
Expand Down
16 changes: 11 additions & 5 deletions pallets/subspace/tests/burn.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,10 @@ fn test_burn() {
let n = 20;
let initial_stake: u64 = 1000;
let tempo = 100;

let keys: Vec<U256> = (0..n).map(U256::from).collect();
let stakes: Vec<u64> = std::iter::repeat(initial_stake * 1_000_000_000).take(n).collect();
let voter_key = keys[1];
let voter_key_index = 1;
let voter_key = keys[voter_key_index];
let mut subnet_params = SubspaceModule::subnet_params(netuid);
subnet_params.tempo = tempo;

Expand All @@ -30,9 +30,15 @@ fn test_burn() {

SubspaceModule::set_global_params(params);

// vote to avoid key[0] as we want to see the key[0] burn
let votes: Vec<u16> = std::iter::repeat(1).take(n).collect();
let uids: Vec<u16> = (0..n).map(|x| x as u16).collect();
let votes: Vec<u16> = (0..n)
.filter(|&x| x != voter_key_index)
.map(|_| 1)
.collect();

let uids: Vec<u16> = (0..n)
.filter(|&x| x != voter_key_index)
.map(|x| x as u16)
.collect();

assert_ok!(SubspaceModule::set_weights(
get_origin(voter_key),
Expand Down
31 changes: 19 additions & 12 deletions pallets/subspace/tests/step.rs
Original file line number Diff line number Diff line change
Expand Up @@ -307,7 +307,6 @@ fn test_pruning() {
});
}

// TODO:
#[test]
fn test_lowest_priority_mechanism() {
new_test_ext().execute_with(|| {
Expand All @@ -326,24 +325,32 @@ fn test_lowest_priority_mechanism() {
SubspaceModule::set_max_allowed_weights(netuid, n);
SubspaceModule::set_min_allowed_weights(netuid, 0);

// for i in 0..n {

// let key: U256 = U256::from(i);
// register_module( netuid, key, stake_per_module );

// }
let keys = SubspaceModule::get_keys(netuid);
let _uids = SubspaceModule::get_uids(netuid);
let voter_idx = 0;

// do a list of ones for weights
let weight_uids: Vec<u16> = (0..n).collect();
// do a list of ones for weights
// Create a list of UIDs excluding the voter_idx
let weight_uids: Vec<u16> = (0..n).filter(|&x| x != voter_idx).map(|x| x as u16).collect();

// Create a list of ones for weights, excluding the voter_idx
let mut weight_values: Vec<u16> = weight_uids.iter().map(|_x| 1_u16).collect();

let prune_uid: u16 = n - 1;
weight_values[prune_uid as usize] = 0;
set_weights(netuid, keys[0], weight_uids.clone(), weight_values.clone());

// Check if the prune_uid is still valid after excluding the voter_idx
if prune_uid != voter_idx {
// Find the index of prune_uid in the updated weight_uids vector
if let Some(prune_idx) = weight_uids.iter().position(|&uid| uid == prune_uid) {
weight_values[prune_idx] = 0;
}
}

set_weights(
netuid,
keys[voter_idx as usize],
weight_uids.clone(),
weight_values.clone(),
);
step_block(tempo);
let incentives: Vec<u16> = SubspaceModule::get_incentives(netuid);
let dividends: Vec<u16> = SubspaceModule::get_dividends(netuid);
Expand Down
26 changes: 2 additions & 24 deletions pallets/subspace/tests/weights.rs
Original file line number Diff line number Diff line change
Expand Up @@ -170,7 +170,7 @@ fn test_set_max_allowed_uids() {
SubspaceModule::set_max_allowed_weights(netuid, max_allowed_uids);

// Should fail because we are only setting a single value and its not the self weight.
let weight_keys: Vec<u16> = (0..max_allowed_uids).collect(); // not weight.
let weight_keys: Vec<u16> = (1..max_allowed_uids + 1).collect(); // not weight.
let weight_values: Vec<u16> = vec![1; max_allowed_uids as usize]; // random value.
let result = SubspaceModule::set_weights(
RuntimeOrigin::signed(account_id),
Expand Down Expand Up @@ -218,9 +218,8 @@ fn test_is_self_weight_weights_length_not_one() {

let uids: Vec<u16> = Vec::from_iter((0..max_allowed).map(|id| id + 1));
let uid: u16 = uids[0];
let weights: Vec<u16> = Vec::from_iter((0..max_allowed).map(|id| id + 1));

let expected = false;
let expected = true;
let result = SubspaceModule::is_self_weight(uid, &uids);

assert_eq!(
Expand All @@ -230,27 +229,6 @@ fn test_is_self_weight_weights_length_not_one() {
});
}

/// Check _falsey_ path for uid vs uids[0]
#[test]
fn test_is_self_weight_uid_not_in_uids() {
new_test_ext().execute_with(|| {
let max_allowed: u16 = 3;

let uids: Vec<u16> = Vec::from_iter((0..max_allowed).map(|id| id + 1));
let uid: u16 = uids[1];
let weights: Vec<u16> = vec![0];

let expected = false;
let result = SubspaceModule::is_self_weight(uid, &uids);

assert_eq!(
expected, result,
"Failed get expected result when `uid != uids[0]`"
);
});
}

/// Check _truthy_ path
/// @TODO: double-check if this really be desired behavior
#[test]
fn test_is_self_weight_uid_in_uids() {
Expand Down

0 comments on commit c72e3e8

Please sign in to comment.