Skip to content

Commit e110578

Browse files
muharemnathanwhit
authored andcommitted
Collective pallet: max proposal weight (paritytech#13771)
* collective: max proposal weight * fix test --------- Co-authored-by: parity-processbot <>
1 parent 39d7116 commit e110578

File tree

5 files changed

+66
-2
lines changed

5 files changed

+66
-2
lines changed

bin/node/runtime/src/lib.rs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -210,6 +210,7 @@ parameter_types! {
210210
})
211211
.avg_block_initialization(AVERAGE_ON_INITIALIZE_RATIO)
212212
.build_or_panic();
213+
pub MaxCollectivesProposalWeight: Weight = Perbill::from_percent(50) * RuntimeBlockWeights::get().max_block;
213214
}
214215

215216
const_assert!(NORMAL_DISPATCH_RATIO.deconstruct() >= AVERAGE_ON_INITIALIZE_RATIO.deconstruct());
@@ -1012,6 +1013,7 @@ impl pallet_collective::Config<CouncilCollective> for Runtime {
10121013
type DefaultVote = pallet_collective::PrimeDefaultVote;
10131014
type WeightInfo = pallet_collective::weights::SubstrateWeight<Runtime>;
10141015
type SetMembersOrigin = EnsureRoot<Self::AccountId>;
1016+
type MaxProposalWeight = MaxCollectivesProposalWeight;
10151017
}
10161018

10171019
parameter_types! {
@@ -1072,6 +1074,7 @@ impl pallet_collective::Config<TechnicalCollective> for Runtime {
10721074
type DefaultVote = pallet_collective::PrimeDefaultVote;
10731075
type WeightInfo = pallet_collective::weights::SubstrateWeight<Runtime>;
10741076
type SetMembersOrigin = EnsureRoot<Self::AccountId>;
1077+
type MaxProposalWeight = MaxCollectivesProposalWeight;
10751078
}
10761079

10771080
type EnsureRootOrHalfCouncil = EitherOfDiverse<
@@ -1700,6 +1703,7 @@ impl pallet_collective::Config<AllianceCollective> for Runtime {
17001703
type DefaultVote = pallet_collective::PrimeDefaultVote;
17011704
type WeightInfo = pallet_collective::weights::SubstrateWeight<Runtime>;
17021705
type SetMembersOrigin = EnsureRoot<Self::AccountId>;
1706+
type MaxProposalWeight = MaxCollectivesProposalWeight;
17031707
}
17041708

17051709
parameter_types! {

frame/alliance/src/mock.rs

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,10 +43,12 @@ type AccountId = u64;
4343

4444
parameter_types! {
4545
pub const BlockHashCount: BlockNumber = 250;
46+
pub BlockWeights: frame_system::limits::BlockWeights =
47+
frame_system::limits::BlockWeights::simple_max(Weight::MAX);
4648
}
4749
impl frame_system::Config for Test {
4850
type BaseCallFilter = frame_support::traits::Everything;
49-
type BlockWeights = ();
51+
type BlockWeights = BlockWeights;
5052
type BlockLength = ();
5153
type RuntimeOrigin = RuntimeOrigin;
5254
type RuntimeCall = RuntimeCall;
@@ -97,6 +99,7 @@ parameter_types! {
9799
pub const MotionDuration: BlockNumber = MOTION_DURATION_IN_BLOCKS;
98100
pub const MaxProposals: u32 = 100;
99101
pub const MaxMembers: u32 = 100;
102+
pub MaxProposalWeight: Weight = sp_runtime::Perbill::from_percent(50) * BlockWeights::get().max_block;
100103
}
101104
type AllianceCollective = pallet_collective::Instance1;
102105
impl pallet_collective::Config<AllianceCollective> for Test {
@@ -109,6 +112,7 @@ impl pallet_collective::Config<AllianceCollective> for Test {
109112
type DefaultVote = pallet_collective::PrimeDefaultVote;
110113
type WeightInfo = ();
111114
type SetMembersOrigin = EnsureRoot<Self::AccountId>;
115+
type MaxProposalWeight = MaxProposalWeight;
112116
}
113117

114118
parameter_types! {

frame/collective/src/lib.rs

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -217,6 +217,10 @@ pub mod pallet {
217217

218218
/// Origin allowed to set collective members
219219
type SetMembersOrigin: EnsureOrigin<<Self as frame_system::Config>::RuntimeOrigin>;
220+
221+
/// The maximum weight of a dispatch call that can be proposed and executed.
222+
#[pallet::constant]
223+
type MaxProposalWeight: Get<Weight>;
220224
}
221225

222226
#[pallet::genesis_config]
@@ -667,6 +671,11 @@ impl<T: Config<I>, I: 'static> Pallet<T, I> {
667671
) -> Result<(u32, DispatchResultWithPostInfo), DispatchError> {
668672
let proposal_len = proposal.encoded_size();
669673
ensure!(proposal_len <= length_bound as usize, Error::<T, I>::WrongProposalLength);
674+
let proposal_weight = proposal.get_dispatch_info().weight;
675+
ensure!(
676+
proposal_weight.all_lte(T::MaxProposalWeight::get()),
677+
Error::<T, I>::WrongProposalWeight
678+
);
670679

671680
let proposal_hash = T::Hashing::hash_of(&proposal);
672681
ensure!(!<ProposalOf<T, I>>::contains_key(proposal_hash), Error::<T, I>::DuplicateProposal);
@@ -689,6 +698,11 @@ impl<T: Config<I>, I: 'static> Pallet<T, I> {
689698
) -> Result<(u32, u32), DispatchError> {
690699
let proposal_len = proposal.encoded_size();
691700
ensure!(proposal_len <= length_bound as usize, Error::<T, I>::WrongProposalLength);
701+
let proposal_weight = proposal.get_dispatch_info().weight;
702+
ensure!(
703+
proposal_weight.all_lte(T::MaxProposalWeight::get()),
704+
Error::<T, I>::WrongProposalWeight
705+
);
692706

693707
let proposal_hash = T::Hashing::hash_of(&proposal);
694708
ensure!(!<ProposalOf<T, I>>::contains_key(proposal_hash), Error::<T, I>::DuplicateProposal);

frame/collective/src/tests.rs

Lines changed: 41 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -90,10 +90,13 @@ pub type MaxMembers = ConstU32<100>;
9090
parameter_types! {
9191
pub const MotionDuration: u64 = 3;
9292
pub const MaxProposals: u32 = 257;
93+
pub BlockWeights: frame_system::limits::BlockWeights =
94+
frame_system::limits::BlockWeights::simple_max(Weight::MAX);
95+
pub static MaxProposalWeight: Weight = default_max_proposal_weight();
9396
}
9497
impl frame_system::Config for Test {
9598
type BaseCallFilter = frame_support::traits::Everything;
96-
type BlockWeights = ();
99+
type BlockWeights = BlockWeights;
97100
type BlockLength = ();
98101
type DbWeight = ();
99102
type RuntimeOrigin = RuntimeOrigin;
@@ -127,6 +130,7 @@ impl Config<Instance1> for Test {
127130
type DefaultVote = PrimeDefaultVote;
128131
type WeightInfo = ();
129132
type SetMembersOrigin = EnsureRoot<Self::AccountId>;
133+
type MaxProposalWeight = MaxProposalWeight;
130134
}
131135
impl Config<Instance2> for Test {
132136
type RuntimeOrigin = RuntimeOrigin;
@@ -138,6 +142,7 @@ impl Config<Instance2> for Test {
138142
type DefaultVote = MoreThanMajorityThenPrimeDefaultVote;
139143
type WeightInfo = ();
140144
type SetMembersOrigin = EnsureRoot<Self::AccountId>;
145+
type MaxProposalWeight = MaxProposalWeight;
141146
}
142147
impl mock_democracy::Config for Test {
143148
type RuntimeEvent = RuntimeEvent;
@@ -153,6 +158,7 @@ impl Config for Test {
153158
type DefaultVote = PrimeDefaultVote;
154159
type WeightInfo = ();
155160
type SetMembersOrigin = EnsureRoot<Self::AccountId>;
161+
type MaxProposalWeight = MaxProposalWeight;
156162
}
157163

158164
pub struct ExtBuilder {}
@@ -201,6 +207,10 @@ fn record(event: RuntimeEvent) -> EventRecord<RuntimeEvent, H256> {
201207
EventRecord { phase: Phase::Initialization, event, topics: vec![] }
202208
}
203209

210+
fn default_max_proposal_weight() -> Weight {
211+
sp_runtime::Perbill::from_percent(80) * BlockWeights::get().max_block
212+
}
213+
204214
#[test]
205215
fn motions_basic_environment_works() {
206216
ExtBuilder::default().build_and_execute(|| {
@@ -209,6 +219,36 @@ fn motions_basic_environment_works() {
209219
});
210220
}
211221

222+
#[test]
223+
fn proposal_weight_limit_works() {
224+
ExtBuilder::default().build_and_execute(|| {
225+
let proposal = make_proposal(42);
226+
let proposal_len: u32 = proposal.using_encoded(|p| p.len() as u32);
227+
228+
assert_ok!(Collective::propose(
229+
RuntimeOrigin::signed(1),
230+
2,
231+
Box::new(proposal.clone()),
232+
proposal_len
233+
));
234+
235+
// set a small limit for max proposal weight.
236+
MaxProposalWeight::set(Weight::from_parts(1, 1));
237+
assert_noop!(
238+
Collective::propose(
239+
RuntimeOrigin::signed(1),
240+
2,
241+
Box::new(proposal.clone()),
242+
proposal_len
243+
),
244+
Error::<Test, Instance1>::WrongProposalWeight
245+
);
246+
247+
// reset the max weight to default.
248+
MaxProposalWeight::set(default_max_proposal_weight());
249+
});
250+
}
251+
212252
#[test]
213253
fn close_works() {
214254
ExtBuilder::default().build_and_execute(|| {

frame/utility/src/tests.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -209,6 +209,7 @@ parameter_types! {
209209
pub const MotionDuration: BlockNumber = MOTION_DURATION_IN_BLOCKS;
210210
pub const MaxProposals: u32 = 100;
211211
pub const MaxMembers: u32 = 100;
212+
pub MaxProposalWeight: Weight = sp_runtime::Perbill::from_percent(50) * BlockWeights::get().max_block;
212213
}
213214

214215
type CouncilCollective = pallet_collective::Instance1;
@@ -222,6 +223,7 @@ impl pallet_collective::Config<CouncilCollective> for Test {
222223
type DefaultVote = pallet_collective::PrimeDefaultVote;
223224
type WeightInfo = ();
224225
type SetMembersOrigin = frame_system::EnsureRoot<Self::AccountId>;
226+
type MaxProposalWeight = MaxProposalWeight;
225227
}
226228

227229
impl example::Config for Test {}

0 commit comments

Comments
 (0)