Skip to content

Commit

Permalink
feat: add governance config fields to global params proposal
Browse files Browse the repository at this point in the history
  • Loading branch information
devwckd committed Jan 8, 2025
1 parent 429277f commit c336b88
Show file tree
Hide file tree
Showing 5 changed files with 72 additions and 12 deletions.
1 change: 1 addition & 0 deletions pallets/governance/src/benchmarks.rs
Original file line number Diff line number Diff line change
Expand Up @@ -129,6 +129,7 @@ benchmarks! {
min_staking_fee: 1,
dividends_participation_weight: Percent::zero(),
proposal_cost: 0,
..Default::default()
};
let data = vec![0];

Expand Down
12 changes: 8 additions & 4 deletions pallets/governance/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -99,10 +99,6 @@ pub mod pallet {
#[pallet::constant]
type MaxApplicationDataLength: Get<u32>;

#[pallet::constant]
#[pallet::no_default_bounds]
type ApplicationExpiration: Get<BlockNumberFor<Self>>;

#[pallet::constant]
type MaxPenaltyPercentage: Get<Percent>;

Expand Down Expand Up @@ -136,6 +132,10 @@ pub mod pallet {
#[pallet::no_default_bounds]
type DefaultProposalRewardInterval: Get<BlockNumberFor<Self>>;

#[pallet::constant]
#[pallet::no_default_bounds]
type EmissionProposalMinimumTime: Get<BlockNumberFor<Self>>;

#[pallet::no_default_bounds]
type RuntimeEvent: From<Event<Self>>
+ IsType<<Self as polkadot_sdk::frame_system::Config>::RuntimeEvent>;
Expand Down Expand Up @@ -492,6 +492,10 @@ pub mod pallet {
InvalidMinStakingFee,
/// Invalid params given to Emission proposal
InvalidEmissionProposalData,
/// Invalid proposal reward interval given to global params proposal
InvalidProposalRewardInterval,
/// Invalid agent application cost given to global params proposal
InvalidAgentApplicationCost,
}
}

Expand Down
63 changes: 59 additions & 4 deletions pallets/governance/src/proposal.rs
Original file line number Diff line number Diff line change
@@ -1,13 +1,14 @@
use crate::config::GovernanceConfiguration;
use crate::frame::traits::ExistenceRequirement;
use crate::BoundedBTreeSet;
use crate::BoundedVec;
use crate::DebugNoBound;
use crate::NotDelegatingVotingPower;
use crate::TypeInfo;
use crate::{
AccountIdOf, BalanceOf, DaoTreasuryAddress, Error, GlobalGovernanceConfig, Proposals,
UnrewardedProposals,
};
use crate::{GovernanceConfiguration, NotDelegatingVotingPower};
use codec::{Decode, Encode, MaxEncodedLen};
use polkadot_sdk::frame_election_provider_support::Get;
use polkadot_sdk::frame_support::traits::Currency;
Expand Down Expand Up @@ -98,6 +99,12 @@ impl<T: crate::Config> Proposal<T> {
min_staking_fee,
dividends_participation_weight,
proposal_cost,
proposal_expiration,
agent_application_cost,
agent_application_expiration,
proposal_reward_treasury_allocation,
max_proposal_reward_treasury_allocation,
proposal_reward_interval,
} = data;

pallet_torus0::MinNameLength::<T>::set(min_name_length);
Expand All @@ -113,8 +120,15 @@ impl<T: crate::Config> Proposal<T> {
});
pallet_emission0::MaxAllowedWeights::<T>::set(max_allowed_weights);
pallet_emission0::MinStakePerWeight::<T>::set(min_stake_per_weight);
crate::GlobalGovernanceConfig::<T>::mutate(|config| {
config.proposal_cost = proposal_cost;

crate::GlobalGovernanceConfig::<T>::set(GovernanceConfiguration::<T> {
proposal_cost,
proposal_expiration,
agent_application_cost,
agent_application_expiration,
proposal_reward_treasury_allocation,
max_proposal_reward_treasury_allocation,
proposal_reward_interval,
});
}
ProposalData::TransferDaoTreasury { account, amount } => {
Expand Down Expand Up @@ -205,6 +219,12 @@ pub struct GlobalParamsData<T: crate::Config> {
pub min_staking_fee: u8,
pub dividends_participation_weight: Percent,
pub proposal_cost: BalanceOf<T>,
pub proposal_expiration: BlockNumberFor<T>,
pub agent_application_cost: BalanceOf<T>,
pub agent_application_expiration: BlockNumberFor<T>,
pub proposal_reward_treasury_allocation: Percent,
pub max_proposal_reward_treasury_allocation: BalanceOf<T>,
pub proposal_reward_interval: BlockNumberFor<T>,
}

impl<T: crate::Config> Default for GlobalParamsData<T> {
Expand All @@ -220,6 +240,13 @@ impl<T: crate::Config> Default for GlobalParamsData<T> {
dividends_participation_weight:
<T as pallet_torus0::Config>::DefaultDividendsParticipationWeight::get(),
proposal_cost: T::DefaultProposalCost::get(),
proposal_expiration: T::DefaultProposalExpiration::get(),
agent_application_cost: T::DefaultAgentApplicationCost::get(),
agent_application_expiration: T::DefaultAgentApplicationExpiration::get(),
proposal_reward_treasury_allocation: T::DefaultProposalRewardTreasuryAllocation::get(),
max_proposal_reward_treasury_allocation:
T::DefaultMaxProposalRewardTreasuryAllocation::get(),
proposal_reward_interval: T::DefaultProposalRewardInterval::get(),
}
}
}
Expand Down Expand Up @@ -261,6 +288,28 @@ impl<T: crate::Config> GlobalParamsData<T> {
crate::Error::<T>::InvalidProposalCost
);

let proposal_expiration_num: u64 = self
.proposal_expiration
.try_into()
.map_err(|_| crate::Error::<T>::InvalidProposalExpiration)?;

ensure!(
(5_400..u64::MAX).contains(&proposal_expiration_num),
crate::Error::<T>::InvalidProposalExpiration
);

ensure!(
self.agent_application_cost <= 100_000_000_000_000_000_000_000,
crate::Error::<T>::InvalidAgentApplicationCost
);

ensure!(
self.proposal_reward_interval
<= BlockNumberFor::<T>::try_from(U256::from(100_000))
.map_err(|_| crate::Error::<T>::InvalidProposalRewardInterval)?,
crate::Error::<T>::InvalidProposalRewardInterval
);

Ok(())
}
}
Expand Down Expand Up @@ -385,10 +434,16 @@ fn add_proposal<T: crate::Config>(

let current_block = <polkadot_sdk::frame_system::Pallet<T>>::block_number();

let expiration_block = match &data {
ProposalData::Emission { .. } => (current_block + T::EmissionProposalMinimumTime::get())
.max(current_block + config.proposal_expiration),
_ => current_block + config.proposal_expiration,
};

let proposal = Proposal::<T> {
id: proposal_id,
proposer,
expiration_block: current_block + config.proposal_expiration,
expiration_block,
data,
status: ProposalStatus::Open {
votes_for: BoundedBTreeSet::new(),
Expand Down
4 changes: 2 additions & 2 deletions runtime/src/configs.rs
Original file line number Diff line number Diff line change
Expand Up @@ -372,8 +372,6 @@ impl pallet_governance::Config for Runtime {

type MaxApplicationDataLength = ConstU32<256>;

type ApplicationExpiration = ConstU64<2000>;

type MaxPenaltyPercentage = MaxPenaltyPercentage;

type DefaultTreasuryEmissionFee = DefaultTreasuryEmissionFee;
Expand All @@ -392,6 +390,8 @@ impl pallet_governance::Config for Runtime {

type DefaultProposalRewardInterval = ConstU64<75_600>;

type EmissionProposalMinimumTime = ConstU64<21_600>;

type RuntimeEvent = RuntimeEvent;

type Currency = Balances;
Expand Down
4 changes: 2 additions & 2 deletions test-utils/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -184,8 +184,6 @@ impl pallet_governance::Config for Test {

type MaxApplicationDataLength = ConstU32<256>;

type ApplicationExpiration = ConstU64<2000>;

type MaxPenaltyPercentage = MaxPenaltyPercentage;

type DefaultTreasuryEmissionFee = DefaultTreasuryEmissionFee;
Expand All @@ -204,6 +202,8 @@ impl pallet_governance::Config for Test {

type DefaultProposalRewardInterval = ConstU64<75_600>;

type EmissionProposalMinimumTime = ConstU64<21_600>;

type RuntimeEvent = RuntimeEvent;

type Currency = Balances;
Expand Down

0 comments on commit c336b88

Please sign in to comment.