diff --git a/README.md b/README.md index 123fec005..dc8288879 100644 --- a/README.md +++ b/README.md @@ -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 \ No newline at end of file diff --git a/pallets/subspace/src/lib.rs b/pallets/subspace/src/lib.rs index c77b5976b..6ca74fe3a 100644 --- a/pallets/subspace/src/lib.rs +++ b/pallets/subspace/src/lib.rs @@ -604,6 +604,7 @@ pub mod pallet { pub enum Error { 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). */ @@ -1070,7 +1071,6 @@ pub mod pallet { params.vote_mode = vote_mode; params.vote_threshold = vote_threshold; - Self::do_add_global_proposal(origin, params) } diff --git a/pallets/subspace/src/voting.rs b/pallets/subspace/src/voting.rs index 0edb4f993..1cce58bce 100644 --- a/pallets/subspace/src/voting.rs +++ b/pallets/subspace/src/voting.rs @@ -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}; @@ -31,6 +31,7 @@ impl Pallet { } + pub fn do_add_subnet_proposal( origin: T::RuntimeOrigin, // params @@ -47,6 +48,16 @@ impl Pallet { + pub fn is_proposal_participant( + key: &T::AccountId, + proposal_id: u64, + ) -> bool { + let proposal: Proposal = Proposals::::get(proposal_id); + return proposal.participants.contains(key); + } + + + pub fn do_update_proposal( origin: T::RuntimeOrigin, proposal_id: u64, @@ -106,7 +117,8 @@ impl Pallet { 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::::insert(key, voter_info); @@ -151,6 +163,7 @@ impl Pallet { proposal.participants.push(key.clone()); proposal.votes = proposal.votes.saturating_add(voting_power); + // update the proposal Voter2Info::::insert(key, voter_info); Proposals::::insert(proposal_id, proposal); @@ -174,7 +187,7 @@ impl Pallet { } pub fn has_max_proposals() -> bool { - return Self::num_proposals() >= MaxProposals::::get() + return Self::num_proposals() >= Self::get_max_proposals(); } @@ -182,8 +195,8 @@ impl Pallet { // 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::::iter() { @@ -200,7 +213,8 @@ impl Pallet { } } - assert!(proposal.votes > least_votes); + ensure!(proposal.votes > least_votes, Error::::TooFewVotesForNewProposal); + // remove proposal participants let proposal = Proposals::::get(least_voted_proposal_id); for participant in proposal.participants { @@ -238,6 +252,9 @@ impl Pallet { proposal_id: u64, ) -> bool { let proposal: Proposal = Proposals::::get(proposal_id); + if proposal.participants.len() == 0 { + return false; + } return proposal.participants[0] == *key; } pub fn default_proposal() -> Proposal { diff --git a/pallets/subspace/tests/test_subnets.rs b/pallets/subspace/tests/test_subnets.rs index 0ec848b31..2cd37a457 100644 --- a/pallets/subspace/tests/test_subnets.rs +++ b/pallets/subspace/tests/test_subnets.rs @@ -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 { @@ -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); diff --git a/pallets/subspace/tests/test_voting.rs b/pallets/subspace/tests/test_voting.rs index 488fc4860..0a406db42 100644 --- a/pallets/subspace/tests/test_voting.rs +++ b/pallets/subspace/tests/test_voting.rs @@ -75,7 +75,8 @@ fn test_max_proposals() { let n = 100; let keys: Vec = (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::>(); for (i, key) in keys.iter().enumerate() { assert_ok!(register_module(netuid, *key, stakes[i])); @@ -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); - }); @@ -214,3 +231,7 @@ fn test_unvote() { }); } + + + +