Skip to content

Commit

Permalink
voting tests
Browse files Browse the repository at this point in the history
  • Loading branch information
Your Name committed Dec 18, 2023
1 parent 571776b commit 52a897a
Show file tree
Hide file tree
Showing 5 changed files with 65 additions and 21 deletions.
5 changes: 5 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -286,3 +286,8 @@ Fuck shit up, dont blame me if things go south, use code as is, peace.

## 7. Acknowledgments
**parralax**


TEST FNS

cargo test -p pallet-subspace --test test_voting
2 changes: 1 addition & 1 deletion pallets/subspace/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -604,6 +604,7 @@ pub mod pallet {
pub enum Error<T> {
ModuleNameAlreadyExists, // --- Thrown when a module name already exists.
NetworkDoesNotExist, // --- Thrown when the network does not exist.
TooFewVotesForNewProposal,
NetworkExist, // --- Thrown when the network already exist.
InvalidIpType, /* ---- Thrown when the user tries to serve an module which
* is not of type 4 (IPv4) or 6 (IPv6). */
Expand Down Expand Up @@ -1070,7 +1071,6 @@ pub mod pallet {
params.vote_mode = vote_mode;
params.vote_threshold = vote_threshold;


Self::do_add_global_proposal(origin, params)
}

Expand Down
29 changes: 23 additions & 6 deletions pallets/subspace/src/voting.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ use core::ops::Add;

use frame_support::{pallet_prelude::DispatchResult};
use scale_info::prelude::string::String;

use frame_support::assert_ok;
use super::*;
use crate::utils::{is_vec_str};

Expand Down Expand Up @@ -31,6 +31,7 @@ impl<T: Config> Pallet<T> {
}



pub fn do_add_subnet_proposal(
origin: T::RuntimeOrigin,
// params
Expand All @@ -47,6 +48,16 @@ impl<T: Config> Pallet<T> {



pub fn is_proposal_participant(
key: &T::AccountId,
proposal_id: u64,
) -> bool {
let proposal: Proposal<T> = Proposals::<T>::get(proposal_id);
return proposal.participants.contains(key);
}



pub fn do_update_proposal(
origin: T::RuntimeOrigin,
proposal_id: u64,
Expand Down Expand Up @@ -106,7 +117,8 @@ impl<T: Config> Pallet<T> {
proposal.participants.push(key.clone());
proposal.votes = proposal.votes.saturating_add(voting_power);

Self::check_proposal(proposal.clone())?; // check if proposal is valid
let result = Self::check_proposal(proposal.clone()); // check if proposal is valid
assert_ok!(result);

// update the proposal
Voter2Info::<T>::insert(key, voter_info);
Expand Down Expand Up @@ -151,6 +163,7 @@ impl<T: Config> Pallet<T> {
proposal.participants.push(key.clone());
proposal.votes = proposal.votes.saturating_add(voting_power);


// update the proposal
Voter2Info::<T>::insert(key, voter_info);
Proposals::<T>::insert(proposal_id, proposal);
Expand All @@ -174,16 +187,16 @@ impl<T: Config> Pallet<T> {
}

pub fn has_max_proposals() -> bool {
return Self::num_proposals() >= MaxProposals::<T>::get()
return Self::num_proposals() >= Self::get_max_proposals();
}


pub fn check_proposal(proposal: Proposal<T>) -> DispatchResult {

// remove lowest voted proposal
if Self::has_max_proposals() {
let mut least_voted_proposal_id: u64 = 0;
let mut least_votes: u64 = 0;
let mut least_voted_proposal_id: u64 = u64::MAX;
let mut least_votes: u64 = u64::MAX;

for (proposal_id, proposal) in Proposals::<T>::iter() {

Expand All @@ -200,7 +213,8 @@ impl<T: Config> Pallet<T> {
}
}

assert!(proposal.votes > least_votes);
ensure!(proposal.votes > least_votes, Error::<T>::TooFewVotesForNewProposal);

// remove proposal participants
let proposal = Proposals::<T>::get(least_voted_proposal_id);
for participant in proposal.participants {
Expand Down Expand Up @@ -238,6 +252,9 @@ impl<T: Config> Pallet<T> {
proposal_id: u64,
) -> bool {
let proposal: Proposal<T> = Proposals::<T>::get(proposal_id);
if proposal.participants.len() == 0 {
return false;
}
return proposal.participants[0] == *key;
}
pub fn default_proposal() -> Proposal<T> {
Expand Down
3 changes: 2 additions & 1 deletion pallets/subspace/tests/test_subnets.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ fn test_add_subnets() {
let mut max_allowed_subnets: u16 = SubspaceModule::get_max_allowed_subnets();
let mut expected_subnets = 0;

for i in 0..num_subnets {
for i in 0..num_subnets{
register_module(i, U256::from(0), stake_per_module);
expected_subnets += 1;
if expected_subnets > max_allowed_subnets {
Expand Down Expand Up @@ -75,6 +75,7 @@ fn test_set_single_temple(tempo: u16) {
);

let subnet_params = SubspaceModule::subnet_params(netuid);

assert_eq!(subnet_params.tempo, tempo);
assert_eq!(subnet_params.min_stake, min_stake);
assert_eq!(subnet_params.max_allowed_uids, params.max_allowed_uids);
Expand Down
47 changes: 34 additions & 13 deletions pallets/subspace/tests/test_voting.rs
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,8 @@ fn test_max_proposals() {
let n = 100;
let keys: Vec<U256> = (0..n).into_iter().map(|x| U256::from(x)).collect();
let mut stakes= vec![1_000_000_000; n];
stakes[n-1] = 1_000_000_1000;
// increase incrementally to avoid overflow
let mut stakes = stakes.iter_mut().enumerate().map(|(i, x)| *x + i as u64).collect::<Vec<u64>>();

for (i, key) in keys.iter().enumerate() {
assert_ok!(register_module(netuid, *key, stakes[i]));
Expand All @@ -89,30 +90,46 @@ fn test_max_proposals() {
SubspaceModule::set_global_params(params.clone());

assert_eq!(params.vote_mode, "stake".as_bytes().to_vec(), "vote mode not set");

let max_proposals = SubspaceModule::get_max_proposals();
let modes = ["authority".as_bytes().to_vec(), "stake".as_bytes().to_vec()];

let mut subnet_params = SubspaceModule::subnet_params(netuid);
subnet_params.vote_mode = "stake".as_bytes().to_vec();
SubspaceModule::set_subnet_params(netuid, subnet_params.clone());
subnet_params = SubspaceModule::subnet_params(netuid);
assert_eq!(subnet_params.vote_mode, "stake".as_bytes().to_vec(), "vote mode not set");

for i in 0..n {
let proposals = SubspaceModule::get_subnet_proposals(netuid);
let has_max_proposals = SubspaceModule::has_max_proposals();
let max_proposals = SubspaceModule::get_max_proposals();
if i % 2 == 0 {
assert_ok!(SubspaceModule::do_add_global_proposal(get_origin(keys[i as usize]), params.clone()));
} else {
assert_ok!(SubspaceModule::do_add_subnet_proposal(get_origin(keys[i as usize]), netuid, subnet_params.clone()));
}
let num_proposals = SubspaceModule::num_proposals();
let proposals = SubspaceModule::get_global_proposals();
let has_max_proposals = SubspaceModule::has_max_proposals();
// assert_eq!(max_proposals, (n - 1) as u64, "proposal not added");
println!("max_proposals: {:?}", max_proposals);
println!("has_max_proposals: {:?}", has_max_proposals);
println!("num_proposals: {:?}", num_proposals);

println!("proposals: {:?}", proposals.len());

assert_ok!(SubspaceModule::do_add_global_proposal(get_origin(keys[i as usize]), params.clone()));
let num_subnet_proposals = SubspaceModule::num_subnet_proposals(netuid);
let num_global_proposals = SubspaceModule::num_global_proposals();
assert_eq!(num_subnet_proposals + num_global_proposals, num_proposals, "proposal not added");

if num_proposals >= max_proposals {
assert_eq!(SubspaceModule::has_max_proposals(), true, "proposal not added");
} else {
assert_eq!(SubspaceModule::has_max_proposals(), false, "proposal not added");
}

assert!(proposals.len() as u64 <= max_proposals, "proposal not added");
}

assert_ok!(SubspaceModule::do_add_global_proposal(get_origin(keys[n-1 as usize]), params.clone()));

// we have not passed the threshold yet
let proposals = SubspaceModule::get_subnet_proposals(netuid);
assert_eq!(SubspaceModule::has_max_proposals(), true, "proposal not added");
assert_eq!(SubspaceModule::num_proposals(), max_proposals, "proposal not added");

println!("proposals: {:?}", proposals);



});
Expand Down Expand Up @@ -214,3 +231,7 @@ fn test_unvote() {
});

}




0 comments on commit 52a897a

Please sign in to comment.