Skip to content

Commit

Permalink
test: impl tests
Browse files Browse the repository at this point in the history
  • Loading branch information
devwckd committed Jan 2, 2025
1 parent 638fcbb commit c41efec
Show file tree
Hide file tree
Showing 14 changed files with 603 additions and 27 deletions.
1 change: 1 addition & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 3 additions & 0 deletions pallets/governance/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -41,3 +41,6 @@ pallet-torus0.workspace = true
pallet-emission0.workspace = true

pallet-governance-api.workspace = true

[dev-dependencies]
test-utils.workspace = true
4 changes: 4 additions & 0 deletions pallets/governance/src/application.rs
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,10 @@ pub fn accept_application<T: crate::Config>(application_id: u32) -> DispatchResu
crate::AgentApplications::<T>::remove(application.id);

whitelist::add_to_whitelist::<T>(application.agent_key.clone())?;

let _ =
<T as crate::Config>::Currency::deposit_creating(&application.payer_key, application.cost);

crate::Pallet::<T>::deposit_event(crate::Event::<T>::ApplicationAccepted(application.id));
crate::Pallet::<T>::deposit_event(crate::Event::<T>::WhitelistAdded(application.agent_key));

Expand Down
15 changes: 9 additions & 6 deletions pallets/governance/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
#![cfg_attr(not(feature = "std"), no_std)]

mod application;
mod config;
mod curator;
mod ext;
mod proposal;
mod voting;
pub mod application;
pub mod config;
pub mod curator;
pub mod ext;
pub mod proposal;
pub mod voting;
pub mod whitelist;

use crate::application::AgentApplication;
Expand Down Expand Up @@ -99,6 +99,9 @@ pub mod pallet {
#[pallet::constant]
type DefaultTreasuryEmissionFee: Get<Percent>;

#[pallet::constant]
type DefaultProposalCost: Get<BalanceOf<T>>;

Check failure on line 103 in pallets/governance/src/lib.rs

View workflow job for this annotation

GitHub Actions / clippy

cannot find type `T` in this scope

error[E0412]: cannot find type `T` in this scope --> pallets/governance/src/lib.rs:103:49 | 103 | type DefaultProposalCost: Get<BalanceOf<T>>; | ^ not found in this scope

#[pallet::no_default_bounds]
type RuntimeEvent: From<Event<Self>>
+ IsType<<Self as polkadot_sdk::frame_system::Config>::RuntimeEvent>;
Expand Down
40 changes: 33 additions & 7 deletions pallets/governance/src/proposal.rs
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,7 @@ impl<T: crate::Config> Proposal<T> {
min_weight_control_fee,
min_staking_fee,
dividends_participation_weight,
proposal_cost,
} = data;

pallet_torus0::MinNameLength::<T>::set(min_name_length);
Expand All @@ -95,9 +96,11 @@ impl<T: crate::Config> Proposal<T> {
Percent::from_percent(min_weight_control_fee);
constraints.min_staking_fee = Percent::from_percent(min_staking_fee);
});

pallet_emission0::MaxAllowedWeights::<T>::set(max_allowed_weights);
pallet_emission0::MinStakePerWeight::<T>::set(min_weight_stake);
crate::GlobalGovernanceConfig::<T>::mutate(|config| {
config.proposal_cost = proposal_cost;
});
}
ProposalData::TransferDaoTreasury { account, amount } => {
<T as crate::Config>::Currency::transfer(
Expand Down Expand Up @@ -186,6 +189,24 @@ pub struct GlobalParamsData<T: crate::Config> {
pub min_weight_control_fee: u8,
pub min_staking_fee: u8,
pub dividends_participation_weight: Percent,
pub proposal_cost: BalanceOf<T>,
}

impl<T: crate::Config> Default for GlobalParamsData<T> {
fn default() -> Self {
Self {
min_name_length: T::DefaultMinNameLength::get(),
max_name_length: T::DefaultMaxNameLength::get(),
max_allowed_agents: T::DefaultMaxAllowedAgents::get(),
max_allowed_weights: T::DefaultMaxAllowedWeights::get(),
min_weight_stake: 0,
min_weight_control_fee: T::DefaultMinWeightControlFee::get(),
min_staking_fee: T::DefaultMinStakingFee::get(),
dividends_participation_weight:
<T as pallet_torus0::Config>::DefaultDividendsParticipationWeight::get(),
proposal_cost: T::DefaultProposalCost::get(),
}
}
}

impl<T: crate::Config> GlobalParamsData<T> {
Expand All @@ -199,16 +220,21 @@ impl<T: crate::Config> GlobalParamsData<T> {
crate::Error::<T>::InvalidMaxNameLength
);
ensure!(
self.max_allowed_agents < 2000,
self.max_allowed_agents <= 50000,
crate::Error::<T>::InvalidMaxAllowedAgents
);
ensure!(
self.max_allowed_weights < 2000,
self.max_allowed_weights <= 2000,
crate::Error::<T>::InvalidMaxAllowedWeights
);
ensure!(
self.min_weight_control_fee > 10,
crate::Error::<T>::InvalidMaxAllowedWeights
self.min_weight_control_fee >= 5,
crate::Error::<T>::InvalidMinWeightControlFee
);

ensure!(
self.proposal_cost <= to_nano(50_000),

Check failure on line 236 in pallets/governance/src/proposal.rs

View workflow job for this annotation

GitHub Actions / clippy

cannot find function `to_nano` in this scope

error[E0425]: cannot find function `to_nano` in this scope --> pallets/governance/src/proposal.rs:236:35 | 236 | self.proposal_cost <= to_nano(50_000), | ^^^^^^^ not found in this scope
crate::Error::<T>::InvalidProposalCost
);

Ok(())
Expand Down Expand Up @@ -464,7 +490,7 @@ fn calc_stake<T: crate::Config>(
pallet_torus0::stake::sum_staking_to::<T>(voter)
};

let delegated_stake = pallet_torus0::stake::get_staking_to_vector::<T>(voter)
let delegated_stake = pallet_torus0::stake::get_staked_by_vector::<T>(voter)
.into_iter()
.filter(|(staker, _)| !not_delegating.contains(staker))
.map(|(_, stake)| stake)
Expand Down Expand Up @@ -525,7 +551,7 @@ pub fn tick_proposal_rewards<T: crate::Config>(block_number: u64) {
);
}

fn get_reward_allocation<T: crate::Config>(
pub fn get_reward_allocation<T: crate::Config>(
governance_config: &GovernanceConfiguration<T>,
n: u16,
) -> Result<I92F36, DispatchError> {
Expand Down
16 changes: 8 additions & 8 deletions pallets/governance/src/voting.rs
Original file line number Diff line number Diff line change
Expand Up @@ -77,18 +77,18 @@ pub fn remove_vote<T: crate::Config>(voter: AccountIdOf<T>, proposal_id: u64) ->
}

pub fn enable_delegation<T: crate::Config>(delegator: AccountIdOf<T>) -> DispatchResult {
crate::NotDelegatingVotingPower::<T>::mutate(|delegators| {
delegators.remove(&delegator);
});

Ok(())
}

pub fn disable_delegation<T: crate::Config>(delegator: AccountIdOf<T>) -> DispatchResult {
crate::NotDelegatingVotingPower::<T>::mutate(|delegators| {
delegators
.try_insert(delegator.clone())
.map(|_| ())
.map_err(|_| Error::<T>::InternalError.into())
})
}

pub fn disable_delegation<T: crate::Config>(delegator: AccountIdOf<T>) -> DispatchResult {
crate::NotDelegatingVotingPower::<T>::mutate(|delegators| {
delegators.remove(&delegator);
});

Ok(())
}
94 changes: 94 additions & 0 deletions pallets/governance/tests/application.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,94 @@
use pallet_governance::AgentApplications;
use pallet_governance::GlobalGovernanceConfig;
use test_utils::*;

#[test]
fn whitelist_executes_application_correctly() {
new_test_ext().execute_with(|| {
zero_min_burn();

let key = 0;
let adding_key = 1;
pallet_governance::Curators::<Test>::insert(key, ());

let proposal_cost = GlobalGovernanceConfig::<Test>::get().agent_application_cost;
let data = "test".as_bytes().to_vec();

add_balance(key, proposal_cost + 1);
// first submit an application
let balance_before = get_balance(key);

assert_ok!(pallet_governance::Pallet::<Test>::submit_application(
get_origin(key),
adding_key,
data.clone(),
));

let balance_after = get_balance(key);
assert_eq!(balance_after, balance_before - proposal_cost);

let mut application_id: u32 = u32::MAX;
for (_, value) in AgentApplications::<Test>::iter() {
assert_eq!(value.agent_key, adding_key);
assert_eq!(value.data, data);
application_id = value.id;
}

assert_ok!(pallet_governance::Pallet::<Test>::accept_application(
get_origin(key),
application_id
));

let balance_after_accept = get_balance(key);

assert_eq!(balance_after_accept, balance_before);

assert!(pallet_governance::whitelist::is_whitelisted::<Test>(
&adding_key
));
});
}

#[test]
fn application_denied_doesnt_add_to_whitelist() {
new_test_ext().execute_with(|| {
let key = 0;
let adding_key = 1;
pallet_governance::Curators::<Test>::insert(key, ());

let proposal_cost = GlobalGovernanceConfig::<Test>::get().agent_application_cost;
let data = "test".as_bytes().to_vec();

add_balance(key, proposal_cost + 1);
let balance_before = get_balance(key);

assert_ok!(pallet_governance::Pallet::<Test>::submit_application(
get_origin(key),
adding_key,
data.clone(),
));

let balance_after = get_balance(key);
assert_eq!(balance_after, balance_before - proposal_cost);

let mut application_id: u32 = u32::MAX;
for (_, value) in AgentApplications::<Test>::iter() {
assert_eq!(value.agent_key, adding_key);
assert_eq!(value.data, data);
application_id = value.id;
}

assert_ok!(pallet_governance::Pallet::<Test>::deny_application(
get_origin(key),
application_id
));

let balance_after_accept = get_balance(key);

assert_eq!(balance_after_accept, balance_after);

assert!(!pallet_governance::whitelist::is_whitelisted::<Test>(
&adding_key
));
});
}
38 changes: 38 additions & 0 deletions pallets/governance/tests/curator.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
use crate::pallet_governance::Error;
use pallet_governance::Curators;
use polkadot_sdk::frame_support::assert_err;
use test_utils::*;

#[test]
fn user_is_added_to_whitelist() {
new_test_ext().execute_with(|| {
let curator_key = 0;
let module_key = 1;
Curators::<Test>::insert(curator_key, ());

assert_ok!(pallet_governance::Pallet::<Test>::add_to_whitelist(
get_origin(curator_key),
module_key
));

assert!(pallet_governance::whitelist::is_whitelisted::<Test>(
&module_key
))
});
}

#[test]
fn only_curators_can_whitelist() {
new_test_ext().execute_with(|| {
let not_curator_key = 0;
let module_key = 1;

assert_err!(
pallet_governance::Pallet::<Test>::add_to_whitelist(
get_origin(not_curator_key),
module_key
),
Error::<Test>::NotCurator
);
});
}
Loading

0 comments on commit c41efec

Please sign in to comment.