diff --git a/bin/node/runtime/src/lib.rs b/bin/node/runtime/src/lib.rs index 2d5825abc906c..27f2516254f0f 100644 --- a/bin/node/runtime/src/lib.rs +++ b/bin/node/runtime/src/lib.rs @@ -483,6 +483,7 @@ parameter_types! { // One cent: $10,000 / MB pub const PreimageByteDeposit: Balance = 1 * CENTS; pub const MaxVotes: u32 = 100; + pub const MaxProposals: u32 = 100; } impl pallet_democracy::Trait for Runtime { @@ -508,6 +509,14 @@ impl pallet_democracy::Trait for Runtime { type FastTrackVotingPeriod = FastTrackVotingPeriod; // To cancel a proposal which has been passed, 2/3 of the council must agree to it. type CancellationOrigin = pallet_collective::EnsureProportionAtLeast<_2, _3, AccountId, CouncilCollective>; + // To cancel a proposal before it has been passed, the technical committee must be unanimous or + // Root must agree. + type CancelProposalOrigin = EnsureOneOf< + AccountId, + EnsureRoot, + pallet_collective::EnsureProportionAtLeast<_1, _1, AccountId, TechnicalCollective>, + >; + type BlacklistOrigin = EnsureRoot; // Any single technical committee member may veto a coming council proposal, however they can // only do it once and it lasts only for the cooloff period. type VetoOrigin = pallet_collective::EnsureMember; @@ -519,6 +528,7 @@ impl pallet_democracy::Trait for Runtime { type PalletsOrigin = OriginCaller; type MaxVotes = MaxVotes; type WeightInfo = weights::pallet_democracy::WeightInfo; + type MaxProposals = MaxProposals; } parameter_types! { diff --git a/bin/node/runtime/src/weights/pallet_democracy.rs b/bin/node/runtime/src/weights/pallet_democracy.rs index b02da3f53a7f9..51eca2855a384 100644 --- a/bin/node/runtime/src/weights/pallet_democracy.rs +++ b/bin/node/runtime/src/weights/pallet_democracy.rs @@ -1,3 +1,5 @@ +// This file is part of Substrate. + // Copyright (C) 2020 Parity Technologies (UK) Ltd. // SPDX-License-Identifier: Apache-2.0 @@ -13,8 +15,12 @@ // See the License for the specific language governing permissions and // limitations under the License. -//! Weights for the Democracy Pallet -//! THIS FILE WAS AUTO-GENERATED USING THE SUBSTRATE BENCHMARK CLI VERSION 2.0.0-rc5 +//! Weights for pallet_democracy +//! THIS FILE WAS AUTO-GENERATED USING THE SUBSTRATE BENCHMARK CLI VERSION 2.0.0 +//! DATE: 2020-09-24, STEPS: [50], REPEAT: 20, LOW RANGE: [], HIGH RANGE: [] + +#![allow(unused_parens)] +#![allow(unused_imports)] use frame_support::{traits::Get, weights::Weight}; use sp_std::marker::PhantomData; @@ -22,134 +28,145 @@ use sp_std::marker::PhantomData; pub struct WeightInfo(PhantomData); impl pallet_democracy::WeightInfo for WeightInfo { fn propose() -> Weight { - (49113000 as Weight) - .saturating_add(T::DbWeight::get().reads(2 as Weight)) + (96_316_000 as Weight) + .saturating_add(T::DbWeight::get().reads(3 as Weight)) .saturating_add(T::DbWeight::get().writes(3 as Weight)) } fn second(s: u32, ) -> Weight { - (42067000 as Weight) - .saturating_add((220000 as Weight).saturating_mul(s as Weight)) + (58_386_000 as Weight) + .saturating_add((259_000 as Weight).saturating_mul(s as Weight)) .saturating_add(T::DbWeight::get().reads(1 as Weight)) .saturating_add(T::DbWeight::get().writes(1 as Weight)) } fn vote_new(r: u32, ) -> Weight { - (54159000 as Weight) - .saturating_add((252000 as Weight).saturating_mul(r as Weight)) + (70_374_000 as Weight) + .saturating_add((291_000 as Weight).saturating_mul(r as Weight)) .saturating_add(T::DbWeight::get().reads(3 as Weight)) .saturating_add(T::DbWeight::get().writes(3 as Weight)) } fn vote_existing(r: u32, ) -> Weight { - (54145000 as Weight) - .saturating_add((262000 as Weight).saturating_mul(r as Weight)) + (70_097_000 as Weight) + .saturating_add((296_000 as Weight).saturating_mul(r as Weight)) .saturating_add(T::DbWeight::get().reads(3 as Weight)) .saturating_add(T::DbWeight::get().writes(3 as Weight)) } fn emergency_cancel() -> Weight { - (31071000 as Weight) + (41_731_000 as Weight) .saturating_add(T::DbWeight::get().reads(2 as Weight)) .saturating_add(T::DbWeight::get().writes(2 as Weight)) } + fn blacklist(p: u32, ) -> Weight { + (117_847_000 as Weight) + .saturating_add((871_000 as Weight).saturating_mul(p as Weight)) + .saturating_add(T::DbWeight::get().reads(5 as Weight)) + .saturating_add(T::DbWeight::get().writes(6 as Weight)) + } fn external_propose(v: u32, ) -> Weight { - (14282000 as Weight) - .saturating_add((109000 as Weight).saturating_mul(v as Weight)) + (20_972_000 as Weight) + .saturating_add((114_000 as Weight).saturating_mul(v as Weight)) .saturating_add(T::DbWeight::get().reads(2 as Weight)) .saturating_add(T::DbWeight::get().writes(1 as Weight)) } fn external_propose_majority() -> Weight { - (3478000 as Weight) + (5_030_000 as Weight) .saturating_add(T::DbWeight::get().writes(1 as Weight)) } fn external_propose_default() -> Weight { - (3442000 as Weight) + (4_981_000 as Weight) .saturating_add(T::DbWeight::get().writes(1 as Weight)) } fn fast_track() -> Weight { - (30820000 as Weight) + (42_801_000 as Weight) .saturating_add(T::DbWeight::get().reads(2 as Weight)) .saturating_add(T::DbWeight::get().writes(3 as Weight)) } fn veto_external(v: u32, ) -> Weight { - (30971000 as Weight) - .saturating_add((184000 as Weight).saturating_mul(v as Weight)) + (44_115_000 as Weight) + .saturating_add((194_000 as Weight).saturating_mul(v as Weight)) .saturating_add(T::DbWeight::get().reads(2 as Weight)) .saturating_add(T::DbWeight::get().writes(2 as Weight)) } + fn cancel_proposal(p: u32, ) -> Weight { + (73_937_000 as Weight) + .saturating_add((962_000 as Weight).saturating_mul(p as Weight)) + .saturating_add(T::DbWeight::get().reads(3 as Weight)) + .saturating_add(T::DbWeight::get().writes(3 as Weight)) + } fn cancel_referendum() -> Weight { - (20431000 as Weight) + (25_233_000 as Weight) .saturating_add(T::DbWeight::get().writes(1 as Weight)) } fn cancel_queued(r: u32, ) -> Weight { - (42438000 as Weight) - .saturating_add((3284000 as Weight).saturating_mul(r as Weight)) + (48_251_000 as Weight) + .saturating_add((3_590_000 as Weight).saturating_mul(r as Weight)) .saturating_add(T::DbWeight::get().reads(2 as Weight)) .saturating_add(T::DbWeight::get().writes(2 as Weight)) } fn on_initialize_base(r: u32, ) -> Weight { - (70826000 as Weight) - .saturating_add((10716000 as Weight).saturating_mul(r as Weight)) - .saturating_add(T::DbWeight::get().reads(6 as Weight)) - .saturating_add(T::DbWeight::get().reads((2 as Weight).saturating_mul(r as Weight))) - .saturating_add(T::DbWeight::get().writes(5 as Weight)) + (17_597_000 as Weight) + .saturating_add((7_248_000 as Weight).saturating_mul(r as Weight)) + .saturating_add(T::DbWeight::get().reads(5 as Weight)) + .saturating_add(T::DbWeight::get().reads((1 as Weight).saturating_mul(r as Weight))) } fn delegate(r: u32, ) -> Weight { - (72046000 as Weight) - .saturating_add((7837000 as Weight).saturating_mul(r as Weight)) + (93_916_000 as Weight) + .saturating_add((10_794_000 as Weight).saturating_mul(r as Weight)) .saturating_add(T::DbWeight::get().reads(4 as Weight)) .saturating_add(T::DbWeight::get().reads((1 as Weight).saturating_mul(r as Weight))) .saturating_add(T::DbWeight::get().writes(4 as Weight)) .saturating_add(T::DbWeight::get().writes((1 as Weight).saturating_mul(r as Weight))) } fn undelegate(r: u32, ) -> Weight { - (41028000 as Weight) - .saturating_add((7810000 as Weight).saturating_mul(r as Weight)) + (47_855_000 as Weight) + .saturating_add((10_805_000 as Weight).saturating_mul(r as Weight)) .saturating_add(T::DbWeight::get().reads(2 as Weight)) .saturating_add(T::DbWeight::get().reads((1 as Weight).saturating_mul(r as Weight))) .saturating_add(T::DbWeight::get().writes(2 as Weight)) .saturating_add(T::DbWeight::get().writes((1 as Weight).saturating_mul(r as Weight))) } fn clear_public_proposals() -> Weight { - (3643000 as Weight) + (4_864_000 as Weight) .saturating_add(T::DbWeight::get().writes(1 as Weight)) } fn note_preimage(b: u32, ) -> Weight { - (46629000 as Weight) - .saturating_add((4000 as Weight).saturating_mul(b as Weight)) + (66_754_000 as Weight) + .saturating_add((4_000 as Weight).saturating_mul(b as Weight)) .saturating_add(T::DbWeight::get().reads(1 as Weight)) .saturating_add(T::DbWeight::get().writes(1 as Weight)) } fn note_imminent_preimage(b: u32, ) -> Weight { - (31147000 as Weight) - .saturating_add((3000 as Weight).saturating_mul(b as Weight)) + (44_664_000 as Weight) + .saturating_add((3_000 as Weight).saturating_mul(b as Weight)) .saturating_add(T::DbWeight::get().reads(1 as Weight)) .saturating_add(T::DbWeight::get().writes(1 as Weight)) } fn reap_preimage(b: u32, ) -> Weight { - (42848000 as Weight) - .saturating_add((3000 as Weight).saturating_mul(b as Weight)) + (59_968_000 as Weight) + .saturating_add((3_000 as Weight).saturating_mul(b as Weight)) .saturating_add(T::DbWeight::get().reads(2 as Weight)) .saturating_add(T::DbWeight::get().writes(1 as Weight)) } fn unlock_remove(r: u32, ) -> Weight { - (45333000 as Weight) - .saturating_add((171000 as Weight).saturating_mul(r as Weight)) + (58_573_000 as Weight) + .saturating_add((131_000 as Weight).saturating_mul(r as Weight)) .saturating_add(T::DbWeight::get().reads(3 as Weight)) .saturating_add(T::DbWeight::get().writes(3 as Weight)) } fn unlock_set(r: u32, ) -> Weight { - (44424000 as Weight) - .saturating_add((291000 as Weight).saturating_mul(r as Weight)) + (53_831_000 as Weight) + .saturating_add((324_000 as Weight).saturating_mul(r as Weight)) .saturating_add(T::DbWeight::get().reads(3 as Weight)) .saturating_add(T::DbWeight::get().writes(3 as Weight)) } fn remove_vote(r: u32, ) -> Weight { - (28250000 as Weight) - .saturating_add((283000 as Weight).saturating_mul(r as Weight)) + (31_846_000 as Weight) + .saturating_add((327_000 as Weight).saturating_mul(r as Weight)) .saturating_add(T::DbWeight::get().reads(2 as Weight)) .saturating_add(T::DbWeight::get().writes(2 as Weight)) } fn remove_other_vote(r: u32, ) -> Weight { - (28250000 as Weight) - .saturating_add((283000 as Weight).saturating_mul(r as Weight)) + (31_880_000 as Weight) + .saturating_add((222_000 as Weight).saturating_mul(r as Weight)) .saturating_add(T::DbWeight::get().reads(2 as Weight)) .saturating_add(T::DbWeight::get().writes(2 as Weight)) } diff --git a/frame/benchmarking/src/utils.rs b/frame/benchmarking/src/utils.rs index 347334e24d5f6..042f4b707aef4 100644 --- a/frame/benchmarking/src/utils.rs +++ b/frame/benchmarking/src/utils.rs @@ -219,3 +219,12 @@ pub fn account(name: &'static str, index: u32, seed pub fn whitelisted_caller() -> AccountId { account::("whitelisted_caller", 0, 0) } + +#[macro_export] +macro_rules! whitelist_account { + ($acc:ident) => { + frame_benchmarking::benchmarking::add_to_whitelist( + frame_system::Account::::hashed_key_for(&$acc).into() + ); + } +} diff --git a/frame/democracy/src/benchmarking.rs b/frame/democracy/src/benchmarking.rs index 1fa0988fbbd4d..0b822e885989e 100644 --- a/frame/democracy/src/benchmarking.rs +++ b/frame/democracy/src/benchmarking.rs @@ -19,7 +19,7 @@ use super::*; -use frame_benchmarking::{benchmarks, account}; +use frame_benchmarking::{benchmarks, account, whitelist_account}; use frame_support::{ IterableStorageMap, traits::{Currency, Get, EnsureOrigin, OnInitialize, UnfilteredDispatchable, schedule::DispatchTime}, @@ -30,7 +30,7 @@ use sp_runtime::traits::{Bounded, One}; use crate::Module as Democracy; const SEED: u32 = 0; -const MAX_REFERENDUMS: u32 = 100; +const MAX_REFERENDUMS: u32 = 99; const MAX_SECONDERS: u32 = 100; const MAX_BYTES: u32 = 16_384; @@ -63,7 +63,7 @@ fn add_proposal(n: u32) -> Result { } fn add_referendum(n: u32) -> Result { - let proposal_hash = add_proposal::(n)?; + let proposal_hash: T::Hash = T::Hashing::hash_of(&n); let vote_threshold = VoteThreshold::SimpleMajority; Democracy::::inject_referendum( @@ -100,12 +100,19 @@ benchmarks! { _ { } propose { + let p = T::MaxProposals::get(); + + for i in 0 .. (p - 1) { + add_proposal::(i)?; + } + let caller = funded_account::("caller", 0); let proposal_hash: T::Hash = T::Hashing::hash_of(&0); let value = T::MinimumDeposit::get(); + whitelist_account!(caller); }: _(RawOrigin::Signed(caller), proposal_hash, value.into()) verify { - assert_eq!(Democracy::::public_props().len(), 1, "Proposals not created."); + assert_eq!(Democracy::::public_props().len(), p as usize, "Proposals not created."); } second { @@ -122,6 +129,7 @@ benchmarks! { let deposits = Democracy::::deposit_of(0).ok_or("Proposal not created")?; assert_eq!(deposits.0.len(), (s + 1) as usize, "Seconds not recorded"); + whitelist_account!(caller); }: _(RawOrigin::Signed(caller), 0, u32::max_value()) verify { let deposits = Democracy::::deposit_of(0).ok_or("Proposal not created")?; @@ -146,7 +154,7 @@ benchmarks! { assert_eq!(votes.len(), r as usize, "Votes were not recorded."); let referendum_index = add_referendum::(r)?; - + whitelist_account!(caller); }: vote(RawOrigin::Signed(caller.clone()), referendum_index, account_vote) verify { let votes = match VotingOf::::get(&caller) { @@ -179,6 +187,7 @@ benchmarks! { let referendum_index = Democracy::::referendum_count() - 1; // This tests when a user changes a vote + whitelist_account!(caller); }: vote(RawOrigin::Signed(caller.clone()), referendum_index, new_vote) verify { let votes = match VotingOf::::get(&caller) { @@ -206,6 +215,31 @@ benchmarks! { assert!(Democracy::::referendum_status(referendum_index).is_err()); } + blacklist { + let p in 1 .. T::MaxProposals::get(); + + // Place our proposal at the end to make sure it's worst case. + for i in 0 .. p - 1 { + add_proposal::(i)?; + } + // We should really add a lot of seconds here, but we're not doing it elsewhere. + + // Place our proposal in the external queue, too. + let hash = T::Hashing::hash_of(&0); + assert!(Democracy::::external_propose(T::ExternalOrigin::successful_origin(), hash.clone()).is_ok()); + + // Add a referendum of our proposal. + let referendum_index = add_referendum::(0)?; + assert!(Democracy::::referendum_status(referendum_index).is_ok()); + + let call = Call::::blacklist(hash, Some(referendum_index)); + let origin = T::BlacklistOrigin::successful_origin(); + }: { call.dispatch_bypass_filter(origin)? } + verify { + // Referendum has been canceled + assert!(Democracy::::referendum_status(referendum_index).is_err()); + } + // Worst case scenario, we external propose a previously blacklisted proposal external_propose { let v in 1 .. MAX_VETOERS as u32; @@ -287,6 +321,15 @@ benchmarks! { assert_eq!(new_vetoers.len(), (v + 1) as usize, "vetoers not added"); } + cancel_proposal { + let p in 1 .. T::MaxProposals::get(); + + // Place our proposal at the end to make sure it's worst case. + for i in 0 .. p { + add_proposal::(i)?; + } + }: _(RawOrigin::Root, 0) + cancel_referendum { let referendum_index = add_referendum::(0)?; }: _(RawOrigin::Root, referendum_index) @@ -301,7 +344,8 @@ benchmarks! { let referendum_index = add_referendum::(r)?; }: _(RawOrigin::Root, referendum_index) - // Note that we have a separate benchmark for `launch_next` + // This measures the path of `launch_next` external. Not currently used as we simply + // assume the weight is `MaxBlockWeight` when executing. #[extra] on_initialize_external { let r in 0 .. MAX_REFERENDUMS; @@ -341,6 +385,8 @@ benchmarks! { } } + // This measures the path of `launch_next` public. Not currently used as we simply + // assume the weight is `MaxBlockWeight` when executing. #[extra] on_initialize_public { let r in 1 .. MAX_REFERENDUMS; @@ -352,6 +398,7 @@ benchmarks! { assert_eq!(Democracy::::referendum_count(), r, "referenda not created"); // Launch public + assert!(add_proposal::(r).is_ok(), "proposal not created"); LastTabledWasExternal::put(true); let block_number = T::LaunchPeriod::get(); @@ -359,7 +406,7 @@ benchmarks! { }: { Democracy::::on_initialize(block_number) } verify { // One extra because of next public - assert_eq!(Democracy::::referendum_count(), r + 1, "referenda not created"); + assert_eq!(Democracy::::referendum_count(), r + 1, "proposal not accepted"); // All should be finished for i in 0 .. r { @@ -437,6 +484,7 @@ benchmarks! { _ => return Err("Votes are not direct"), }; assert_eq!(votes.len(), r as usize, "Votes were not recorded."); + whitelist_account!(caller); }: _(RawOrigin::Signed(caller.clone()), new_delegate.clone(), Conviction::Locked1x, delegated_balance) verify { let (target, balance) = match VotingOf::::get(&caller) { @@ -488,6 +536,7 @@ benchmarks! { _ => return Err("Votes are not direct"), }; assert_eq!(votes.len(), r as usize, "Votes were not recorded."); + whitelist_account!(caller); }: _(RawOrigin::Signed(caller.clone())) verify { // Voting should now be direct @@ -508,6 +557,7 @@ benchmarks! { let caller = funded_account::("caller", 0); let encoded_proposal = vec![1; b as usize]; + whitelist_account!(caller); }: _(RawOrigin::Signed(caller), encoded_proposal.clone()) verify { let proposal_hash = T::Hashing::hash(&encoded_proposal[..]); @@ -529,6 +579,7 @@ benchmarks! { let caller = funded_account::("caller", 0); let encoded_proposal = vec![1; b as usize]; + whitelist_account!(caller); }: _(RawOrigin::Signed(caller), encoded_proposal.clone()) verify { let proposal_hash = T::Hashing::hash(&encoded_proposal[..]); @@ -555,6 +606,7 @@ benchmarks! { assert!(Preimages::::contains_key(proposal_hash)); let caller = funded_account::("caller", 0); + whitelist_account!(caller); }: _(RawOrigin::Signed(caller), proposal_hash.clone(), u32::max_value()) verify { let proposal_hash = T::Hashing::hash(&encoded_proposal[..]); @@ -577,6 +629,7 @@ benchmarks! { } let caller = funded_account::("caller", 0); + whitelist_account!(caller); }: unlock(RawOrigin::Signed(caller), locker.clone()) verify { // Note that we may want to add a `get_lock` api to actually verify @@ -614,6 +667,7 @@ benchmarks! { Democracy::::remove_vote(RawOrigin::Signed(locker.clone()).into(), referendum_index)?; let caller = funded_account::("caller", 0); + whitelist_account!(caller); }: unlock(RawOrigin::Signed(caller), locker.clone()) verify { let votes = match VotingOf::::get(&locker) { @@ -645,7 +699,7 @@ benchmarks! { assert_eq!(votes.len(), r as usize, "Votes not created"); let referendum_index = r - 1; - + whitelist_account!(caller); }: _(RawOrigin::Signed(caller.clone()), referendum_index) verify { let votes = match VotingOf::::get(&caller) { @@ -674,7 +728,7 @@ benchmarks! { assert_eq!(votes.len(), r as usize, "Votes not created"); let referendum_index = r - 1; - + whitelist_account!(caller); }: _(RawOrigin::Signed(caller.clone()), caller.clone(), referendum_index) verify { let votes = match VotingOf::::get(&caller) { @@ -765,6 +819,8 @@ mod tests { assert_ok!(test_benchmark_remove_other_vote::()); assert_ok!(test_benchmark_enact_proposal_execute::()); assert_ok!(test_benchmark_enact_proposal_slash::()); + assert_ok!(test_benchmark_blacklist::()); + assert_ok!(test_benchmark_cancel_proposal::()); }); } } diff --git a/frame/democracy/src/default_weight.rs b/frame/democracy/src/default_weight.rs index 2c74a4af2020f..28aa45ae2d603 100644 --- a/frame/democracy/src/default_weight.rs +++ b/frame/democracy/src/default_weight.rs @@ -15,143 +15,156 @@ // See the License for the specific language governing permissions and // limitations under the License. -//! Default weights for the Democracy Pallet -//! THIS FILE WAS AUTO-GENERATED USING THE SUBSTRATE BENCHMARK CLI VERSION 2.0.0-rc5 +//! Weights for pallet_democracy +//! THIS FILE WAS AUTO-GENERATED USING THE SUBSTRATE BENCHMARK CLI VERSION 2.0.0 +//! DATE: 2020-09-24, STEPS: [50], REPEAT: 20, LOW RANGE: [], HIGH RANGE: [] + +#![allow(unused_parens)] +#![allow(unused_imports)] use frame_support::weights::{Weight, constants::RocksDbWeight as DbWeight}; -/// Default implementation of weight, this is just from an example return, values may change -/// depending on the runtime. This is not meant to be used in production. impl crate::WeightInfo for () { fn propose() -> Weight { - (49113000 as Weight) - .saturating_add(DbWeight::get().reads(2 as Weight)) + (96_316_000 as Weight) + .saturating_add(DbWeight::get().reads(3 as Weight)) .saturating_add(DbWeight::get().writes(3 as Weight)) } fn second(s: u32, ) -> Weight { - (42067000 as Weight) - .saturating_add((220000 as Weight).saturating_mul(s as Weight)) + (58_386_000 as Weight) + .saturating_add((259_000 as Weight).saturating_mul(s as Weight)) .saturating_add(DbWeight::get().reads(1 as Weight)) .saturating_add(DbWeight::get().writes(1 as Weight)) } fn vote_new(r: u32, ) -> Weight { - (54159000 as Weight) - .saturating_add((252000 as Weight).saturating_mul(r as Weight)) + (70_374_000 as Weight) + .saturating_add((291_000 as Weight).saturating_mul(r as Weight)) .saturating_add(DbWeight::get().reads(3 as Weight)) .saturating_add(DbWeight::get().writes(3 as Weight)) } fn vote_existing(r: u32, ) -> Weight { - (54145000 as Weight) - .saturating_add((262000 as Weight).saturating_mul(r as Weight)) + (70_097_000 as Weight) + .saturating_add((296_000 as Weight).saturating_mul(r as Weight)) .saturating_add(DbWeight::get().reads(3 as Weight)) .saturating_add(DbWeight::get().writes(3 as Weight)) } fn emergency_cancel() -> Weight { - (31071000 as Weight) + (41_731_000 as Weight) .saturating_add(DbWeight::get().reads(2 as Weight)) .saturating_add(DbWeight::get().writes(2 as Weight)) } + fn blacklist(p: u32, ) -> Weight { + (117_847_000 as Weight) + .saturating_add((871_000 as Weight).saturating_mul(p as Weight)) + .saturating_add(DbWeight::get().reads(5 as Weight)) + .saturating_add(DbWeight::get().writes(6 as Weight)) + } fn external_propose(v: u32, ) -> Weight { - (14282000 as Weight) - .saturating_add((109000 as Weight).saturating_mul(v as Weight)) + (20_972_000 as Weight) + .saturating_add((114_000 as Weight).saturating_mul(v as Weight)) .saturating_add(DbWeight::get().reads(2 as Weight)) .saturating_add(DbWeight::get().writes(1 as Weight)) } fn external_propose_majority() -> Weight { - (3478000 as Weight) + (5_030_000 as Weight) .saturating_add(DbWeight::get().writes(1 as Weight)) } fn external_propose_default() -> Weight { - (3442000 as Weight) + (4_981_000 as Weight) .saturating_add(DbWeight::get().writes(1 as Weight)) } fn fast_track() -> Weight { - (30820000 as Weight) + (42_801_000 as Weight) .saturating_add(DbWeight::get().reads(2 as Weight)) .saturating_add(DbWeight::get().writes(3 as Weight)) } fn veto_external(v: u32, ) -> Weight { - (30971000 as Weight) - .saturating_add((184000 as Weight).saturating_mul(v as Weight)) + (44_115_000 as Weight) + .saturating_add((194_000 as Weight).saturating_mul(v as Weight)) .saturating_add(DbWeight::get().reads(2 as Weight)) .saturating_add(DbWeight::get().writes(2 as Weight)) } + fn cancel_proposal(p: u32, ) -> Weight { + (73_937_000 as Weight) + .saturating_add((962_000 as Weight).saturating_mul(p as Weight)) + .saturating_add(DbWeight::get().reads(3 as Weight)) + .saturating_add(DbWeight::get().writes(3 as Weight)) + } fn cancel_referendum() -> Weight { - (20431000 as Weight) + (25_233_000 as Weight) .saturating_add(DbWeight::get().writes(1 as Weight)) } fn cancel_queued(r: u32, ) -> Weight { - (42438000 as Weight) - .saturating_add((3284000 as Weight).saturating_mul(r as Weight)) + (48_251_000 as Weight) + .saturating_add((3_590_000 as Weight).saturating_mul(r as Weight)) .saturating_add(DbWeight::get().reads(2 as Weight)) .saturating_add(DbWeight::get().writes(2 as Weight)) } fn on_initialize_base(r: u32, ) -> Weight { - (70826000 as Weight) - .saturating_add((10716000 as Weight).saturating_mul(r as Weight)) - .saturating_add(DbWeight::get().reads(6 as Weight)) - .saturating_add(DbWeight::get().reads((2 as Weight).saturating_mul(r as Weight))) - .saturating_add(DbWeight::get().writes(5 as Weight)) + (17_597_000 as Weight) + .saturating_add((7_248_000 as Weight).saturating_mul(r as Weight)) + .saturating_add(DbWeight::get().reads(5 as Weight)) + .saturating_add(DbWeight::get().reads((1 as Weight).saturating_mul(r as Weight))) } fn delegate(r: u32, ) -> Weight { - (72046000 as Weight) - .saturating_add((7837000 as Weight).saturating_mul(r as Weight)) + (93_916_000 as Weight) + .saturating_add((10_794_000 as Weight).saturating_mul(r as Weight)) .saturating_add(DbWeight::get().reads(4 as Weight)) .saturating_add(DbWeight::get().reads((1 as Weight).saturating_mul(r as Weight))) .saturating_add(DbWeight::get().writes(4 as Weight)) .saturating_add(DbWeight::get().writes((1 as Weight).saturating_mul(r as Weight))) } fn undelegate(r: u32, ) -> Weight { - (41028000 as Weight) - .saturating_add((7810000 as Weight).saturating_mul(r as Weight)) + (47_855_000 as Weight) + .saturating_add((10_805_000 as Weight).saturating_mul(r as Weight)) .saturating_add(DbWeight::get().reads(2 as Weight)) .saturating_add(DbWeight::get().reads((1 as Weight).saturating_mul(r as Weight))) .saturating_add(DbWeight::get().writes(2 as Weight)) .saturating_add(DbWeight::get().writes((1 as Weight).saturating_mul(r as Weight))) } fn clear_public_proposals() -> Weight { - (3643000 as Weight) + (4_864_000 as Weight) .saturating_add(DbWeight::get().writes(1 as Weight)) } fn note_preimage(b: u32, ) -> Weight { - (46629000 as Weight) - .saturating_add((4000 as Weight).saturating_mul(b as Weight)) + (66_754_000 as Weight) + .saturating_add((4_000 as Weight).saturating_mul(b as Weight)) .saturating_add(DbWeight::get().reads(1 as Weight)) .saturating_add(DbWeight::get().writes(1 as Weight)) } fn note_imminent_preimage(b: u32, ) -> Weight { - (31147000 as Weight) - .saturating_add((3000 as Weight).saturating_mul(b as Weight)) + (44_664_000 as Weight) + .saturating_add((3_000 as Weight).saturating_mul(b as Weight)) .saturating_add(DbWeight::get().reads(1 as Weight)) .saturating_add(DbWeight::get().writes(1 as Weight)) } fn reap_preimage(b: u32, ) -> Weight { - (42848000 as Weight) - .saturating_add((3000 as Weight).saturating_mul(b as Weight)) + (59_968_000 as Weight) + .saturating_add((3_000 as Weight).saturating_mul(b as Weight)) .saturating_add(DbWeight::get().reads(2 as Weight)) .saturating_add(DbWeight::get().writes(1 as Weight)) } fn unlock_remove(r: u32, ) -> Weight { - (45333000 as Weight) - .saturating_add((171000 as Weight).saturating_mul(r as Weight)) + (58_573_000 as Weight) + .saturating_add((131_000 as Weight).saturating_mul(r as Weight)) .saturating_add(DbWeight::get().reads(3 as Weight)) .saturating_add(DbWeight::get().writes(3 as Weight)) } fn unlock_set(r: u32, ) -> Weight { - (44424000 as Weight) - .saturating_add((291000 as Weight).saturating_mul(r as Weight)) + (53_831_000 as Weight) + .saturating_add((324_000 as Weight).saturating_mul(r as Weight)) .saturating_add(DbWeight::get().reads(3 as Weight)) .saturating_add(DbWeight::get().writes(3 as Weight)) } fn remove_vote(r: u32, ) -> Weight { - (28250000 as Weight) - .saturating_add((283000 as Weight).saturating_mul(r as Weight)) + (31_846_000 as Weight) + .saturating_add((327_000 as Weight).saturating_mul(r as Weight)) .saturating_add(DbWeight::get().reads(2 as Weight)) .saturating_add(DbWeight::get().writes(2 as Weight)) } fn remove_other_vote(r: u32, ) -> Weight { - (28250000 as Weight) - .saturating_add((283000 as Weight).saturating_mul(r as Weight)) + (31_880_000 as Weight) + .saturating_add((222_000 as Weight).saturating_mul(r as Weight)) .saturating_add(DbWeight::get().reads(2 as Weight)) .saturating_add(DbWeight::get().writes(2 as Weight)) } diff --git a/frame/democracy/src/lib.rs b/frame/democracy/src/lib.rs index 9ed732d3234ea..884106a63b321 100644 --- a/frame/democracy/src/lib.rs +++ b/frame/democracy/src/lib.rs @@ -155,7 +155,7 @@ use sp_std::prelude::*; use sp_runtime::{ DispatchResult, DispatchError, RuntimeDebug, - traits::{Zero, Hash, Dispatchable, Saturating}, + traits::{Zero, Hash, Dispatchable, Saturating, Bounded}, }; use codec::{Encode, Decode, Input}; use frame_support::{ @@ -208,12 +208,14 @@ pub trait WeightInfo { fn vote_new(r: u32, ) -> Weight; fn vote_existing(r: u32, ) -> Weight; fn emergency_cancel() -> Weight; + fn blacklist(p: u32, ) -> Weight; fn external_propose(v: u32, ) -> Weight; fn external_propose_majority() -> Weight; fn external_propose_default() -> Weight; fn fast_track() -> Weight; fn veto_external(v: u32, ) -> Weight; fn cancel_referendum() -> Weight; + fn cancel_proposal(p: u32, ) -> Weight; fn cancel_queued(r: u32, ) -> Weight; fn on_initialize_base(r: u32, ) -> Weight; fn delegate(r: u32, ) -> Weight; @@ -285,6 +287,12 @@ pub trait Trait: frame_system::Trait + Sized { /// Origin from which any referendum may be cancelled in an emergency. type CancellationOrigin: EnsureOrigin; + /// Origin from which proposals may be blacklisted. + type BlacklistOrigin: EnsureOrigin; + + /// Origin from which a proposal may be cancelled and its backers slashed. + type CancelProposalOrigin: EnsureOrigin; + /// Origin for anyone able to veto proposals. /// /// # Warning @@ -319,6 +327,9 @@ pub trait Trait: frame_system::Trait + Sized { /// Weight information for extrinsics in this pallet. type WeightInfo: WeightInfo; + + /// The maximum number of public proposals that can exist at any time. + type MaxProposals: Get; } #[derive(Clone, Encode, Decode, RuntimeDebug)] @@ -414,8 +425,7 @@ decl_storage! { /// A record of who vetoed what. Maps proposal hash to a possible existent block number /// (until when it may not be resubmitted) and who vetoed it. - pub Blacklist get(fn blacklist): - map hasher(identity) T::Hash => Option<(T::BlockNumber, Vec)>; + pub Blacklist: map hasher(identity) T::Hash => Option<(T::BlockNumber, Vec)>; /// Record of all proposals that have been subject to emergency cancellation. pub Cancellations: map hasher(identity) T::Hash => bool; @@ -472,6 +482,8 @@ decl_event! { PreimageReaped(Hash, AccountId, Balance, AccountId), /// An \[account\] has been unlocked successfully. Unlocked(AccountId), + /// A proposal \[hash\] has been blacklisted permanently. + Blacklisted(Hash), } } @@ -544,6 +556,10 @@ decl_error! { WrongUpperBound, /// Maximum number of votes reached. MaxVotesReached, + /// The provided witness data is wrong. + InvalidWitness, + /// Maximum number of proposals reached. + TooManyProposals, } } @@ -591,19 +607,28 @@ decl_module! { /// /// Emits `Proposed`. /// - /// # - /// - Complexity: `O(1)` - /// - Db reads: `PublicPropCount`, `PublicProps` - /// - Db writes: `PublicPropCount`, `PublicProps`, `DepositOf` - /// # + /// Weight: `O(p)` #[weight = T::WeightInfo::propose()] - fn propose(origin, proposal_hash: T::Hash, #[compact] value: BalanceOf) { + fn propose(origin, + proposal_hash: T::Hash, + #[compact] value: BalanceOf, + ) { let who = ensure_signed(origin)?; ensure!(value >= T::MinimumDeposit::get(), Error::::ValueLow); - T::Currency::reserve(&who, value)?; - let index = Self::public_prop_count(); + let real_prop_count = PublicProps::::decode_len().unwrap_or(0) as u32; + let max_proposals = T::MaxProposals::get(); + ensure!(real_prop_count < max_proposals, Error::::TooManyProposals); + + if let Some((until, _)) = >::get(proposal_hash) { + ensure!( + >::block_number() >= until, + Error::::ProposalBlacklisted, + ); + } + + T::Currency::reserve(&who, value)?; PublicPropCount::put(index + 1); >::insert(index, (&[&who][..], value)); @@ -621,11 +646,7 @@ decl_module! { /// - `seconds_upper_bound`: an upper bound on the current number of seconds on this /// proposal. Extrinsic is weighted according to this value with no refund. /// - /// # - /// - Complexity: `O(S)` where S is the number of seconds a proposal already has. - /// - Db reads: `DepositOf` - /// - Db writes: `DepositOf` - /// # + /// Weight: `O(S)` where S is the number of seconds a proposal already has. #[weight = T::WeightInfo::second(*seconds_upper_bound)] fn second(origin, #[compact] proposal: PropIndex, #[compact] seconds_upper_bound: u32) { let who = ensure_signed(origin)?; @@ -648,12 +669,7 @@ decl_module! { /// - `ref_index`: The index of the referendum to vote for. /// - `vote`: The vote configuration. /// - /// # - /// - Complexity: `O(R)` where R is the number of referendums the voter has voted on. - /// weight is charged as if maximum votes. - /// - Db reads: `ReferendumInfoOf`, `VotingOf`, `balances locks` - /// - Db writes: `ReferendumInfoOf`, `VotingOf`, `balances locks` - /// # + /// Weight: `O(R)` where R is the number of referendums the voter has voted on. #[weight = T::WeightInfo::vote_new(T::MaxVotes::get()) .max(T::WeightInfo::vote_existing(T::MaxVotes::get()))] fn vote(origin, @@ -671,11 +687,7 @@ decl_module! { /// /// -`ref_index`: The index of the referendum to cancel. /// - /// # - /// - Complexity: `O(1)`. - /// - Db reads: `ReferendumInfoOf`, `Cancellations` - /// - Db writes: `ReferendumInfoOf`, `Cancellations` - /// # + /// Weight: `O(1)`. #[weight = (T::WeightInfo::emergency_cancel(), DispatchClass::Operational)] fn emergency_cancel(origin, ref_index: ReferendumIndex) { T::CancellationOrigin::ensure_origin(origin)?; @@ -695,12 +707,8 @@ decl_module! { /// /// - `proposal_hash`: The preimage hash of the proposal. /// - /// # - /// - Complexity `O(V)` with V number of vetoers in the blacklist of proposal. + /// Weight: `O(V)` with V number of vetoers in the blacklist of proposal. /// Decoding vec of length V. Charged as maximum - /// - Db reads: `NextExternal`, `Blacklist` - /// - Db writes: `NextExternal` - /// # #[weight = T::WeightInfo::external_propose(MAX_VETOERS)] fn external_propose(origin, proposal_hash: T::Hash) { T::ExternalOrigin::ensure_origin(origin)?; @@ -724,10 +732,7 @@ decl_module! { /// Unlike `external_propose`, blacklisting has no effect on this and it may replace a /// pre-scheduled `external_propose` call. /// - /// # - /// - Complexity: `O(1)` - /// - Db write: `NextExternal` - /// # + /// Weight: `O(1)` #[weight = T::WeightInfo::external_propose_majority()] fn external_propose_majority(origin, proposal_hash: T::Hash) { T::ExternalMajorityOrigin::ensure_origin(origin)?; @@ -744,10 +749,7 @@ decl_module! { /// Unlike `external_propose`, blacklisting has no effect on this and it may replace a /// pre-scheduled `external_propose` call. /// - /// # - /// - Complexity: `O(1)` - /// - Db write: `NextExternal` - /// # + /// Weight: `O(1)` #[weight = T::WeightInfo::external_propose_default()] fn external_propose_default(origin, proposal_hash: T::Hash) { T::ExternalDefaultOrigin::ensure_origin(origin)?; @@ -768,12 +770,7 @@ decl_module! { /// /// Emits `Started`. /// - /// # - /// - Complexity: `O(1)` - /// - Db reads: `NextExternal`, `ReferendumCount` - /// - Db writes: `NextExternal`, `ReferendumCount`, `ReferendumInfoOf` - /// - Base Weight: 30.1 µs - /// # + /// Weight: `O(1)` #[weight = T::WeightInfo::fast_track()] fn fast_track(origin, proposal_hash: T::Hash, @@ -818,12 +815,7 @@ decl_module! { /// /// Emits `Vetoed`. /// - /// # - /// - Complexity: `O(V + log(V))` where V is number of `existing vetoers` - /// Performs a binary search on `existing_vetoers` which should not be very large. - /// - Db reads: `NextExternal`, `Blacklist` - /// - Db writes: `NextExternal`, `Blacklist` - /// # + /// Weight: `O(V + log(V))` where V is number of `existing vetoers` #[weight = T::WeightInfo::veto_external(MAX_VETOERS)] fn veto_external(origin, proposal_hash: T::Hash) { let who = T::VetoOrigin::ensure_origin(origin)?; @@ -854,10 +846,7 @@ decl_module! { /// /// - `ref_index`: The index of the referendum to cancel. /// - /// # - /// - Complexity: `O(1)`. - /// - Db writes: `ReferendumInfoOf` - /// # + /// # Weight: `O(1)`. #[weight = T::WeightInfo::cancel_referendum()] fn cancel_referendum(origin, #[compact] ref_index: ReferendumIndex) { ensure_root(origin)?; @@ -870,11 +859,7 @@ decl_module! { /// /// - `which`: The index of the referendum to cancel. /// - /// # - /// - `O(D)` where `D` is the items in the dispatch queue. Weighted as `D = 10`. - /// - Db reads: `scheduler lookup`, scheduler agenda` - /// - Db writes: `scheduler lookup`, scheduler agenda` - /// # + /// Weight: `O(D)` where `D` is the items in the dispatch queue. Weighted as `D = 10`. #[weight = (T::WeightInfo::cancel_queued(10), DispatchClass::Operational)] fn cancel_queued(origin, which: ReferendumIndex) { ensure_root(origin)?; @@ -908,16 +893,10 @@ decl_module! { /// /// Emits `Delegated`. /// - /// # - /// - Complexity: `O(R)` where R is the number of referendums the voter delegating to has + /// Weight: `O(R)` where R is the number of referendums the voter delegating to has /// voted on. Weight is charged as if maximum votes. - /// - Db reads: 3*`VotingOf`, `origin account locks` - /// - Db writes: 3*`VotingOf`, `origin account locks` - /// - Db reads per votes: `ReferendumInfoOf` - /// - Db writes per votes: `ReferendumInfoOf` // NOTE: weight must cover an incorrect voting of origin with max votes, this is ensure // because a valid delegation cover decoding a direct voting with max votes. - /// # #[weight = T::WeightInfo::delegate(T::MaxVotes::get())] pub fn delegate( origin, @@ -941,16 +920,10 @@ decl_module! { /// /// Emits `Undelegated`. /// - /// # - /// - Complexity: `O(R)` where R is the number of referendums the voter delegating to has + /// Weight: `O(R)` where R is the number of referendums the voter delegating to has /// voted on. Weight is charged as if maximum votes. - /// - Db reads: 2*`VotingOf` - /// - Db writes: 2*`VotingOf` - /// - Db reads per votes: `ReferendumInfoOf` - /// - Db writes per votes: `ReferendumInfoOf` // NOTE: weight must cover an incorrect voting of origin with max votes, this is ensure // because a valid delegation cover decoding a direct voting with max votes. - /// # #[weight = T::WeightInfo::undelegate(T::MaxVotes::get().into())] fn undelegate(origin) -> DispatchResultWithPostInfo { let who = ensure_signed(origin)?; @@ -962,10 +935,7 @@ decl_module! { /// /// The dispatch origin of this call must be _Root_. /// - /// # - /// - `O(1)`. - /// - Db writes: `PublicProps` - /// # + /// Weight: `O(1)`. #[weight = T::WeightInfo::clear_public_proposals()] fn clear_public_proposals(origin) { ensure_root(origin)?; @@ -981,11 +951,7 @@ decl_module! { /// /// Emits `PreimageNoted`. /// - /// # - /// - Complexity: `O(E)` with E size of `encoded_proposal` (protected by a required deposit). - /// - Db reads: `Preimages` - /// - Db writes: `Preimages` - /// # + /// Weight: `O(E)` with E size of `encoded_proposal` (protected by a required deposit). #[weight = T::WeightInfo::note_preimage(encoded_proposal.len() as u32)] fn note_preimage(origin, encoded_proposal: Vec) { Self::note_preimage_inner(ensure_signed(origin)?, encoded_proposal)?; @@ -1012,11 +978,7 @@ decl_module! { /// /// Emits `PreimageNoted`. /// - /// # - /// - Complexity: `O(E)` with E size of `encoded_proposal` (protected by a required deposit). - /// - Db reads: `Preimages` - /// - Db writes: `Preimages` - /// # + /// Weight: `O(E)` with E size of `encoded_proposal` (protected by a required deposit). #[weight = T::WeightInfo::note_imminent_preimage(encoded_proposal.len() as u32)] fn note_imminent_preimage(origin, encoded_proposal: Vec) -> DispatchResultWithPostInfo { Self::note_imminent_preimage_inner(ensure_signed(origin)?, encoded_proposal)?; @@ -1052,11 +1014,7 @@ decl_module! { /// /// Emits `PreimageReaped`. /// - /// # - /// - Complexity: `O(D)` where D is length of proposal. - /// - Db reads: `Preimages`, provider account data - /// - Db writes: `Preimages` provider account data - /// # + /// Weight: `O(D)` where D is length of proposal. #[weight = T::WeightInfo::reap_preimage(*proposal_len_upper_bound)] fn reap_preimage(origin, proposal_hash: T::Hash, #[compact] proposal_len_upper_bound: u32) { let who = ensure_signed(origin)?; @@ -1090,11 +1048,7 @@ decl_module! { /// /// - `target`: The account to remove the lock on. /// - /// # - /// - Complexity `O(R)` with R number of vote of target. - /// - Db reads: `VotingOf`, `balances locks`, `target account` - /// - Db writes: `VotingOf`, `balances locks`, `target account` - /// # + /// Weight: `O(R)` with R number of vote of target. #[weight = T::WeightInfo::unlock_set(T::MaxVotes::get()) .max(T::WeightInfo::unlock_remove(T::MaxVotes::get()))] fn unlock(origin, target: T::AccountId) { @@ -1127,12 +1081,8 @@ decl_module! { /// /// - `index`: The index of referendum of the vote to be removed. /// - /// # - /// - `O(R + log R)` where R is the number of referenda that `target` has voted on. + /// Weight: `O(R + log R)` where R is the number of referenda that `target` has voted on. /// Weight is calculated for the maximum number of vote. - /// - Db reads: `ReferendumInfoOf`, `VotingOf` - /// - Db writes: `ReferendumInfoOf`, `VotingOf` - /// # #[weight = T::WeightInfo::remove_vote(T::MaxVotes::get())] fn remove_vote(origin, index: ReferendumIndex) -> DispatchResult { let who = ensure_signed(origin)?; @@ -1152,12 +1102,8 @@ decl_module! { /// referendum `index`. /// - `index`: The index of referendum of the vote to be removed. /// - /// # - /// - `O(R + log R)` where R is the number of referenda that `target` has voted on. + /// Weight: `O(R + log R)` where R is the number of referenda that `target` has voted on. /// Weight is calculated for the maximum number of vote. - /// - Db reads: `ReferendumInfoOf`, `VotingOf` - /// - Db writes: `ReferendumInfoOf`, `VotingOf` - /// # #[weight = T::WeightInfo::remove_other_vote(T::MaxVotes::get())] fn remove_other_vote(origin, target: T::AccountId, index: ReferendumIndex) -> DispatchResult { let who = ensure_signed(origin)?; @@ -1172,6 +1118,80 @@ decl_module! { ensure_root(origin)?; Self::do_enact_proposal(proposal_hash, index) } + + /// Permanently place a proposal into the blacklist. This prevents it from ever being + /// proposed again. + /// + /// If called on a queued public or external proposal, then this will result in it being + /// removed. If the `ref_index` supplied is an active referendum with the proposal hash, + /// then it will be cancelled. + /// + /// The dispatch origin of this call must be `BlacklistOrigin`. + /// + /// - `proposal_hash`: The proposal hash to blacklist permanently. + /// - `ref_index`: An ongoing referendum whose hash is `proposal_hash`, which will be + /// cancelled. + /// + /// Weight: `O(p)` (though as this is an high-privilege dispatch, we assume it has a + /// reasonable value). + #[weight = (T::WeightInfo::blacklist(T::MaxProposals::get()), DispatchClass::Operational)] + fn blacklist(origin, + proposal_hash: T::Hash, + maybe_ref_index: Option, + ) { + T::BlacklistOrigin::ensure_origin(origin)?; + + // Insert the proposal into the blacklist. + let permanent = (T::BlockNumber::max_value(), Vec::::new()); + Blacklist::::insert(&proposal_hash, permanent); + + // Remove the queued proposal, if it's there. + PublicProps::::mutate(|props| { + if let Some(index) = props.iter().position(|p| p.1 == proposal_hash) { + let (prop_index, ..) = props.remove(index); + if let Some((whos, amount)) = DepositOf::::take(prop_index) { + for who in whos.into_iter() { + T::Slash::on_unbalanced(T::Currency::slash_reserved(&who, amount).0); + } + } + } + }); + + // Remove the external queued referendum, if it's there. + if matches!(NextExternal::::get(), Some((h, ..)) if h == proposal_hash) { + NextExternal::::kill(); + } + + // Remove the referendum, if it's there. + if let Some(ref_index) = maybe_ref_index { + if let Ok(status) = Self::referendum_status(ref_index) { + if status.proposal_hash == proposal_hash { + Self::internal_cancel_referendum(ref_index); + } + } + } + + Self::deposit_event(RawEvent::Blacklisted(proposal_hash)); + } + + /// Remove a proposal. + /// + /// The dispatch origin of this call must be `CancelProposalOrigin`. + /// + /// - `prop_index`: The index of the proposal to cancel. + /// + /// Weight: `O(p)` where `p = PublicProps::::decode_len()` + #[weight = T::WeightInfo::cancel_proposal(T::MaxProposals::get())] + fn cancel_proposal(origin, #[compact] prop_index: PropIndex) { + T::CancelProposalOrigin::ensure_origin(origin)?; + + PublicProps::::mutate(|props| props.retain(|p| p.0 != prop_index)); + if let Some((whos, amount)) = DepositOf::::take(prop_index) { + for who in whos.into_iter() { + T::Slash::on_unbalanced(T::Currency::slash_reserved(&who, amount).0); + } + } + } } } @@ -1608,7 +1628,7 @@ impl Module { /// /// /// # - /// If a referendum is launched or maturing take full block weight. Otherwise: + /// If a referendum is launched or maturing, this will take full block weight. Otherwise: /// - Complexity: `O(R)` where `R` is the number of unbaked referenda. /// - Db reads: `LastTabledWasExternal`, `NextExternal`, `PublicProps`, `account`, /// `ReferendumCount`, `LowestUnbaked` diff --git a/frame/democracy/src/tests.rs b/frame/democracy/src/tests.rs index 7f23eb4cca8ad..bcc7099bb34a4 100644 --- a/frame/democracy/src/tests.rs +++ b/frame/democracy/src/tests.rs @@ -49,6 +49,8 @@ const NAY: Vote = Vote { aye: false, conviction: Conviction::None }; const BIG_AYE: Vote = Vote { aye: true, conviction: Conviction::Locked1x }; const BIG_NAY: Vote = Vote { aye: false, conviction: Conviction::Locked1x }; +const MAX_PROPOSALS: u32 = 100; + impl_outer_origin! { pub enum Origin for Test where system = frame_system {} } @@ -151,6 +153,7 @@ parameter_types! { pub const EnactmentPeriod: u64 = 2; pub const CooloffPeriod: u64 = 2; pub const MaxVotes: u32 = 100; + pub const MaxProposals: u32 = MAX_PROPOSALS; } ord_parameter_types! { pub const One: u64 = 1; @@ -194,6 +197,8 @@ impl super::Trait for Test { type ExternalDefaultOrigin = EnsureSignedBy; type FastTrackOrigin = EnsureSignedBy; type CancellationOrigin = EnsureSignedBy; + type BlacklistOrigin = EnsureRoot; + type CancelProposalOrigin = EnsureRoot; type VetoOrigin = EnsureSignedBy; type CooloffPeriod = CooloffPeriod; type PreimageByteDeposit = PreimageByteDeposit; @@ -205,6 +210,7 @@ impl super::Trait for Test { type OperationalPreimageOrigin = EnsureSignedBy; type PalletsOrigin = OriginCaller; type WeightInfo = (); + type MaxProposals = MaxProposals; } pub fn new_test_ext() -> sp_io::TestExternalities { diff --git a/frame/democracy/src/tests/external_proposing.rs b/frame/democracy/src/tests/external_proposing.rs index 473eac81cdcb0..3f9be2137906b 100644 --- a/frame/democracy/src/tests/external_proposing.rs +++ b/frame/democracy/src/tests/external_proposing.rs @@ -79,6 +79,32 @@ fn veto_external_works() { }); } +#[test] +fn external_blacklisting_should_work() { + new_test_ext().execute_with(|| { + System::set_block_number(0); + + assert_ok!(Democracy::external_propose( + Origin::signed(2), + set_balance_proposal_hash_and_note(2), + )); + + let hash = set_balance_proposal_hash(2); + assert_ok!(Democracy::blacklist(Origin::root(), hash, None)); + + fast_forward_to(2); + assert!(Democracy::referendum_status(0).is_err()); + + assert_noop!( + Democracy::external_propose( + Origin::signed(2), + set_balance_proposal_hash_and_note(2), + ), + Error::::ProposalBlacklisted, + ); + }); +} + #[test] fn external_referendum_works() { new_test_ext().execute_with(|| { diff --git a/frame/democracy/src/tests/public_proposals.rs b/frame/democracy/src/tests/public_proposals.rs index 68ec790baae86..d862aa98e7880 100644 --- a/frame/democracy/src/tests/public_proposals.rs +++ b/frame/democracy/src/tests/public_proposals.rs @@ -96,6 +96,45 @@ fn invalid_seconds_upper_bound_should_not_work() { }); } +#[test] +fn cancel_proposal_should_work() { + new_test_ext().execute_with(|| { + System::set_block_number(0); + assert_ok!(propose_set_balance_and_note(1, 2, 2)); + assert_ok!(propose_set_balance_and_note(1, 4, 4)); + assert_noop!(Democracy::cancel_proposal(Origin::signed(1), 0), BadOrigin); + assert_ok!(Democracy::cancel_proposal(Origin::root(), 0)); + assert_eq!(Democracy::backing_for(0), None); + assert_eq!(Democracy::backing_for(1), Some(4)); + }); +} + +#[test] +fn blacklisting_should_work() { + new_test_ext().execute_with(|| { + System::set_block_number(0); + let hash = set_balance_proposal_hash(2); + + assert_ok!(propose_set_balance_and_note(1, 2, 2)); + assert_ok!(propose_set_balance_and_note(1, 4, 4)); + + assert_noop!(Democracy::blacklist(Origin::signed(1), hash.clone(), None), BadOrigin); + assert_ok!(Democracy::blacklist(Origin::root(), hash, None)); + + assert_eq!(Democracy::backing_for(0), None); + assert_eq!(Democracy::backing_for(1), Some(4)); + + assert_noop!(propose_set_balance_and_note(1, 2, 2), Error::::ProposalBlacklisted); + + fast_forward_to(2); + + let hash = set_balance_proposal_hash(4); + assert!(Democracy::referendum_status(0).is_ok()); + assert_ok!(Democracy::blacklist(Origin::root(), hash, Some(0))); + assert!(Democracy::referendum_status(0).is_err()); + }); +} + #[test] fn runners_up_should_come_after() { new_test_ext().execute_with(|| { diff --git a/frame/staking/src/benchmarking.rs b/frame/staking/src/benchmarking.rs index afda58db4672f..9cef7be1cec1d 100644 --- a/frame/staking/src/benchmarking.rs +++ b/frame/staking/src/benchmarking.rs @@ -23,20 +23,12 @@ use testing_utils::*; use sp_runtime::traits::One; use frame_system::RawOrigin; -pub use frame_benchmarking::{benchmarks, account, whitelisted_caller}; +pub use frame_benchmarking::{benchmarks, account, whitelisted_caller, whitelist_account}; const SEED: u32 = 0; const MAX_SPANS: u32 = 100; const MAX_VALIDATORS: u32 = 1000; const MAX_SLASHES: u32 = 1000; -macro_rules! do_whitelist { - ($acc:ident) => { - frame_benchmarking::benchmarking::add_to_whitelist( - frame_system::Account::::hashed_key_for(&$acc).into() - ); - } -} - // Add slashing spans to a user account. Not relevant for actual use, only to benchmark // read and write operations. fn add_slashing_spans(who: &T::AccountId, spans: u32) { @@ -120,7 +112,7 @@ benchmarks! { let controller_lookup: ::Source = T::Lookup::unlookup(controller.clone()); let reward_destination = RewardDestination::Staked; let amount = T::Currency::minimum_balance() * 10.into(); - do_whitelist!(stash); + whitelist_account!(stash); }: _(RawOrigin::Signed(stash.clone()), controller_lookup, amount, reward_destination) verify { assert!(Bonded::::contains_key(stash)); @@ -132,7 +124,7 @@ benchmarks! { let max_additional = T::Currency::minimum_balance() * 10.into(); let ledger = Ledger::::get(&controller).ok_or("ledger not created before")?; let original_bonded: BalanceOf = ledger.active; - do_whitelist!(stash); + whitelist_account!(stash); }: _(RawOrigin::Signed(stash), max_additional) verify { let ledger = Ledger::::get(&controller).ok_or("ledger not created after")?; @@ -145,7 +137,7 @@ benchmarks! { let amount = T::Currency::minimum_balance() * 10.into(); let ledger = Ledger::::get(&controller).ok_or("ledger not created before")?; let original_bonded: BalanceOf = ledger.active; - do_whitelist!(controller); + whitelist_account!(controller); }: _(RawOrigin::Signed(controller.clone()), amount) verify { let ledger = Ledger::::get(&controller).ok_or("ledger not created after")?; @@ -164,7 +156,7 @@ benchmarks! { CurrentEra::put(EraIndex::max_value()); let ledger = Ledger::::get(&controller).ok_or("ledger not created before")?; let original_total: BalanceOf = ledger.total; - do_whitelist!(controller); + whitelist_account!(controller); }: withdraw_unbonded(RawOrigin::Signed(controller.clone()), s) verify { let ledger = Ledger::::get(&controller).ok_or("ledger not created after")?; @@ -183,7 +175,7 @@ benchmarks! { CurrentEra::put(EraIndex::max_value()); let ledger = Ledger::::get(&controller).ok_or("ledger not created before")?; let original_total: BalanceOf = ledger.total; - do_whitelist!(controller); + whitelist_account!(controller); }: withdraw_unbonded(RawOrigin::Signed(controller.clone()), s) verify { assert!(!Ledger::::contains_key(controller)); @@ -192,7 +184,7 @@ benchmarks! { validate { let (stash, controller) = create_stash_controller::(USER_SEED, 100, Default::default())?; let prefs = ValidatorPrefs::default(); - do_whitelist!(controller); + whitelist_account!(controller); }: _(RawOrigin::Signed(controller), prefs) verify { assert!(Validators::::contains_key(stash)); @@ -203,7 +195,7 @@ benchmarks! { let n in 1 .. MAX_NOMINATIONS as u32; let (stash, controller) = create_stash_controller::(n + 1, 100, Default::default())?; let validators = create_validators::(n, 100)?; - do_whitelist!(controller); + whitelist_account!(controller); }: _(RawOrigin::Signed(controller), validators) verify { assert!(Nominators::::contains_key(stash)); @@ -211,13 +203,13 @@ benchmarks! { chill { let (_, controller) = create_stash_controller::(USER_SEED, 100, Default::default())?; - do_whitelist!(controller); + whitelist_account!(controller); }: _(RawOrigin::Signed(controller)) set_payee { let (stash, controller) = create_stash_controller::(USER_SEED, 100, Default::default())?; assert_eq!(Payee::::get(&stash), RewardDestination::Staked); - do_whitelist!(controller); + whitelist_account!(controller); }: _(RawOrigin::Signed(controller), RewardDestination::Controller) verify { assert_eq!(Payee::::get(&stash), RewardDestination::Controller); @@ -227,7 +219,7 @@ benchmarks! { let (stash, _) = create_stash_controller::(USER_SEED, 100, Default::default())?; let new_controller = create_funded_user::("new_controller", USER_SEED, 100); let new_controller_lookup = T::Lookup::unlookup(new_controller.clone()); - do_whitelist!(stash); + whitelist_account!(stash); }: _(RawOrigin::Signed(stash), new_controller_lookup) verify { assert!(Ledger::::contains_key(&new_controller)); @@ -350,7 +342,7 @@ benchmarks! { } Ledger::::insert(controller.clone(), staking_ledger.clone()); let original_bonded: BalanceOf = staking_ledger.active; - do_whitelist!(controller); + whitelist_account!(controller); }: _(RawOrigin::Signed(controller.clone()), (l + 100).into()) verify { let ledger = Ledger::::get(&controller).ok_or("ledger not created after")?; @@ -381,7 +373,7 @@ benchmarks! { let (stash, controller) = create_stash_controller::(0, 100, Default::default())?; add_slashing_spans::(&stash, s); T::Currency::make_free_balance_be(&stash, 0.into()); - do_whitelist!(controller); + whitelist_account!(controller); }: _(RawOrigin::Signed(controller), stash.clone(), s) verify { assert!(!Bonded::::contains_key(&stash)); @@ -516,7 +508,7 @@ benchmarks! { let era = >::current_era().unwrap_or(0); let caller: T::AccountId = account("caller", n, SEED); - do_whitelist!(caller); + whitelist_account!(caller); }: { let result = >::submit_election_solution( RawOrigin::Signed(caller.clone()).into(), @@ -584,7 +576,7 @@ benchmarks! { let era = >::current_era().unwrap_or(0); let caller: T::AccountId = account("caller", n, SEED); - do_whitelist!(caller); + whitelist_account!(caller); // submit a very bad solution on-chain { @@ -638,7 +630,7 @@ benchmarks! { >::put(ElectionStatus::Open(T::BlockNumber::from(1u32))); let era = >::current_era().unwrap_or(0); let caller: T::AccountId = account("caller", n, SEED); - do_whitelist!(caller); + whitelist_account!(caller); // submit a seq-phragmen with all the good stuff on chain. {