Skip to content
This repository has been archived by the owner on Nov 15, 2023. It is now read-only.

Collective pallet: max proposal weight #13771

Merged
merged 4 commits into from
Apr 19, 2023
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions bin/node/runtime/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -210,6 +210,7 @@ parameter_types! {
})
.avg_block_initialization(AVERAGE_ON_INITIALIZE_RATIO)
.build_or_panic();
pub MaxCollectivesProposalWeight: Weight = Perbill::from_percent(50) * RuntimeBlockWeights::get().max_block;
}

const_assert!(NORMAL_DISPATCH_RATIO.deconstruct() >= AVERAGE_ON_INITIALIZE_RATIO.deconstruct());
Expand Down Expand Up @@ -1009,6 +1010,7 @@ impl pallet_collective::Config<CouncilCollective> for Runtime {
type DefaultVote = pallet_collective::PrimeDefaultVote;
type WeightInfo = pallet_collective::weights::SubstrateWeight<Runtime>;
type SetMembersOrigin = EnsureRoot<Self::AccountId>;
type MaxProposalWeight = MaxCollectivesProposalWeight;
}

parameter_types! {
Expand Down Expand Up @@ -1069,6 +1071,7 @@ impl pallet_collective::Config<TechnicalCollective> for Runtime {
type DefaultVote = pallet_collective::PrimeDefaultVote;
type WeightInfo = pallet_collective::weights::SubstrateWeight<Runtime>;
type SetMembersOrigin = EnsureRoot<Self::AccountId>;
type MaxProposalWeight = MaxCollectivesProposalWeight;
}

type EnsureRootOrHalfCouncil = EitherOfDiverse<
Expand Down Expand Up @@ -1686,6 +1689,7 @@ impl pallet_collective::Config<AllianceCollective> for Runtime {
type DefaultVote = pallet_collective::PrimeDefaultVote;
type WeightInfo = pallet_collective::weights::SubstrateWeight<Runtime>;
type SetMembersOrigin = EnsureRoot<Self::AccountId>;
type MaxProposalWeight = MaxCollectivesProposalWeight;
}

parameter_types! {
Expand Down
6 changes: 5 additions & 1 deletion frame/alliance/src/mock.rs
Original file line number Diff line number Diff line change
Expand Up @@ -43,10 +43,12 @@ type AccountId = u64;

parameter_types! {
pub const BlockHashCount: BlockNumber = 250;
pub BlockWeights: frame_system::limits::BlockWeights =
frame_system::limits::BlockWeights::simple_max(Weight::MAX);
}
impl frame_system::Config for Test {
type BaseCallFilter = frame_support::traits::Everything;
type BlockWeights = ();
type BlockWeights = BlockWeights;
type BlockLength = ();
type RuntimeOrigin = RuntimeOrigin;
type RuntimeCall = RuntimeCall;
Expand Down Expand Up @@ -97,6 +99,7 @@ parameter_types! {
pub const MotionDuration: BlockNumber = MOTION_DURATION_IN_BLOCKS;
pub const MaxProposals: u32 = 100;
pub const MaxMembers: u32 = 100;
pub MaxProposalWeight: Weight = sp_runtime::Perbill::from_percent(50) * BlockWeights::get().max_block;
}
type AllianceCollective = pallet_collective::Instance1;
impl pallet_collective::Config<AllianceCollective> for Test {
Expand All @@ -109,6 +112,7 @@ impl pallet_collective::Config<AllianceCollective> for Test {
type DefaultVote = pallet_collective::PrimeDefaultVote;
type WeightInfo = ();
type SetMembersOrigin = EnsureRoot<Self::AccountId>;
type MaxProposalWeight = MaxProposalWeight;
}

parameter_types! {
Expand Down
14 changes: 14 additions & 0 deletions frame/collective/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -217,6 +217,10 @@ pub mod pallet {

/// Origin allowed to set collective members
type SetMembersOrigin: EnsureOrigin<<Self as frame_system::Config>::RuntimeOrigin>;

/// The maximum weight of a dispatch call that can be proposed and executed.
#[pallet::constant]
type MaxProposalWeight: Get<Weight>;
}

#[pallet::genesis_config]
Expand Down Expand Up @@ -710,6 +714,11 @@ impl<T: Config<I>, I: 'static> Pallet<T, I> {
) -> Result<(u32, DispatchResultWithPostInfo), DispatchError> {
let proposal_len = proposal.encoded_size();
ensure!(proposal_len <= length_bound as usize, Error::<T, I>::WrongProposalLength);
let proposal_weight = proposal.get_dispatch_info().weight;
ensure!(
proposal_weight.all_lte(T::MaxProposalWeight::get()),
Error::<T, I>::WrongProposalWeight
);
Comment on lines +674 to +678
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The proposal weight is already added to the weight of propose if threshold < 2. So, we would not even add this tx to a block when the weight is too big. TLDR: This should not be required.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

But yeah, to have the properties as do_propose_proposed it makes sense.


let proposal_hash = T::Hashing::hash_of(&proposal);
ensure!(!<ProposalOf<T, I>>::contains_key(proposal_hash), Error::<T, I>::DuplicateProposal);
Expand All @@ -732,6 +741,11 @@ impl<T: Config<I>, I: 'static> Pallet<T, I> {
) -> Result<(u32, u32), DispatchError> {
let proposal_len = proposal.encoded_size();
ensure!(proposal_len <= length_bound as usize, Error::<T, I>::WrongProposalLength);
let proposal_weight = proposal.get_dispatch_info().weight;
ensure!(
proposal_weight.all_lte(T::MaxProposalWeight::get()),
Error::<T, I>::WrongProposalWeight
);

let proposal_hash = T::Hashing::hash_of(&proposal);
ensure!(!<ProposalOf<T, I>>::contains_key(proposal_hash), Error::<T, I>::DuplicateProposal);
Expand Down
42 changes: 41 additions & 1 deletion frame/collective/src/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -90,10 +90,13 @@ pub type MaxMembers = ConstU32<100>;
parameter_types! {
pub const MotionDuration: u64 = 3;
pub const MaxProposals: u32 = 257;
pub BlockWeights: frame_system::limits::BlockWeights =
frame_system::limits::BlockWeights::simple_max(Weight::MAX);
pub static MaxProposalWeight: Weight = default_max_proposal_weight();
}
impl frame_system::Config for Test {
type BaseCallFilter = frame_support::traits::Everything;
type BlockWeights = ();
type BlockWeights = BlockWeights;
type BlockLength = ();
type DbWeight = ();
type RuntimeOrigin = RuntimeOrigin;
Expand Down Expand Up @@ -127,6 +130,7 @@ impl Config<Instance1> for Test {
type DefaultVote = PrimeDefaultVote;
type WeightInfo = ();
type SetMembersOrigin = EnsureRoot<Self::AccountId>;
type MaxProposalWeight = MaxProposalWeight;
}
impl Config<Instance2> for Test {
type RuntimeOrigin = RuntimeOrigin;
Expand All @@ -138,6 +142,7 @@ impl Config<Instance2> for Test {
type DefaultVote = MoreThanMajorityThenPrimeDefaultVote;
type WeightInfo = ();
type SetMembersOrigin = EnsureRoot<Self::AccountId>;
type MaxProposalWeight = MaxProposalWeight;
}
impl mock_democracy::Config for Test {
type RuntimeEvent = RuntimeEvent;
Expand All @@ -153,6 +158,7 @@ impl Config for Test {
type DefaultVote = PrimeDefaultVote;
type WeightInfo = ();
type SetMembersOrigin = EnsureRoot<Self::AccountId>;
type MaxProposalWeight = MaxProposalWeight;
}

pub fn new_test_ext() -> sp_io::TestExternalities {
Expand Down Expand Up @@ -184,6 +190,10 @@ fn record(event: RuntimeEvent) -> EventRecord<RuntimeEvent, H256> {
EventRecord { phase: Phase::Initialization, event, topics: vec![] }
}

fn default_max_proposal_weight() -> Weight {
sp_runtime::Perbill::from_percent(80) * BlockWeights::get().max_block
}

#[test]
fn motions_basic_environment_works() {
new_test_ext().execute_with(|| {
Expand All @@ -192,6 +202,36 @@ fn motions_basic_environment_works() {
});
}

#[test]
fn proposal_weight_limit_works() {
new_test_ext().execute_with(|| {
let proposal = make_proposal(42);
let proposal_len: u32 = proposal.using_encoded(|p| p.len() as u32);

assert_ok!(Collective::propose(
RuntimeOrigin::signed(1),
2,
Box::new(proposal.clone()),
proposal_len
));

// set a small limit for max proposal weight.
MaxProposalWeight::set(Weight::from_parts(1, 1));
assert_noop!(
Collective::propose(
RuntimeOrigin::signed(1),
2,
Box::new(proposal.clone()),
proposal_len
),
Error::<Test, Instance1>::WrongProposalWeight
);

// reset the max weight to default.
MaxProposalWeight::set(default_max_proposal_weight());
});
}

#[test]
fn close_works() {
new_test_ext().execute_with(|| {
Expand Down
2 changes: 2 additions & 0 deletions frame/utility/src/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -209,6 +209,7 @@ parameter_types! {
pub const MotionDuration: BlockNumber = MOTION_DURATION_IN_BLOCKS;
pub const MaxProposals: u32 = 100;
pub const MaxMembers: u32 = 100;
pub MaxProposalWeight: Weight = sp_runtime::Perbill::from_percent(50) * BlockWeights::get().max_block;
}

type CouncilCollective = pallet_collective::Instance1;
Expand All @@ -222,6 +223,7 @@ impl pallet_collective::Config<CouncilCollective> for Test {
type DefaultVote = pallet_collective::PrimeDefaultVote;
type WeightInfo = ();
type SetMembersOrigin = frame_system::EnsureRoot<Self::AccountId>;
type MaxProposalWeight = MaxProposalWeight;
}

impl example::Config for Test {}
Expand Down