From c8f69856716c65a6a3d7809fdf0a4cc6afa241c4 Mon Sep 17 00:00:00 2001 From: kianenigma Date: Mon, 21 Sep 2020 10:59:53 +0200 Subject: [PATCH 1/8] Update elections-phragmen weight to WeightInfo --- bin/node/runtime/src/lib.rs | 21 ++- bin/node/runtime/src/weights/mod.rs | 1 + .../src/weights/pallet_elections_phragmen.rs | 88 ++++++++++ frame/elections-phragmen/src/benchmarking.rs | 163 +++++++++++------- .../elections-phragmen/src/default_weights.rs | 87 ++++++++++ frame/elections-phragmen/src/lib.rs | 108 +++++------- frame/elections/src/lib.rs | 7 + 7 files changed, 336 insertions(+), 139 deletions(-) create mode 100644 bin/node/runtime/src/weights/pallet_elections_phragmen.rs create mode 100644 frame/elections-phragmen/src/default_weights.rs diff --git a/bin/node/runtime/src/lib.rs b/bin/node/runtime/src/lib.rs index 0de78464bddec..69a3967e5b9a3 100644 --- a/bin/node/runtime/src/lib.rs +++ b/bin/node/runtime/src/lib.rs @@ -234,13 +234,20 @@ impl InstanceFilter for ProxyType { fn filter(&self, c: &Call) -> bool { match self { ProxyType::Any => true, - ProxyType::NonTransfer => !matches!(c, - Call::Balances(..) | Call::Vesting(pallet_vesting::Call::vested_transfer(..)) - | Call::Indices(pallet_indices::Call::transfer(..)) + ProxyType::NonTransfer => !matches!( + c, + Call::Balances(..) | + Call::Vesting(pallet_vesting::Call::vested_transfer(..)) | + Call::Indices(pallet_indices::Call::transfer(..)) ), - ProxyType::Governance => matches!(c, - Call::Democracy(..) | Call::Council(..) | Call::Society(..) - | Call::TechnicalCommittee(..) | Call::Elections(..) | Call::Treasury(..) + ProxyType::Governance => matches!( + c, + Call::Democracy(..) | + Call::Council(..) | + Call::Society(..) | + Call::TechnicalCommittee(..) | + Call::Elections(..) | + Call::Treasury(..) ), ProxyType::Staking => matches!(c, Call::Staking(..)), } @@ -559,7 +566,7 @@ impl pallet_elections_phragmen::Trait for Runtime { type DesiredMembers = DesiredMembers; type DesiredRunnersUp = DesiredRunnersUp; type TermDuration = TermDuration; - type WeightInfo = (); + type WeightInfo = weights::pallet_elections_phragmen::WeightInfo; } parameter_types! { diff --git a/bin/node/runtime/src/weights/mod.rs b/bin/node/runtime/src/weights/mod.rs index 668b9462a7d6a..164d86bb87715 100644 --- a/bin/node/runtime/src/weights/mod.rs +++ b/bin/node/runtime/src/weights/mod.rs @@ -29,3 +29,4 @@ pub mod pallet_staking; pub mod pallet_timestamp; pub mod pallet_utility; pub mod pallet_vesting; +pub mod pallet_elections_phragmen; diff --git a/bin/node/runtime/src/weights/pallet_elections_phragmen.rs b/bin/node/runtime/src/weights/pallet_elections_phragmen.rs new file mode 100644 index 0000000000000..2071cc28eca5a --- /dev/null +++ b/bin/node/runtime/src/weights/pallet_elections_phragmen.rs @@ -0,0 +1,88 @@ +// This file is part of Substrate. + +// Copyright (C) 2019-2020 Parity Technologies (UK) Ltd. +// SPDX-License-Identifier: Apache-2.0 + +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +//! THIS FILE WAS AUTO-GENERATED USING THE SUBSTRATE BENCHMARK CLI VERSION 2.0.0-rc6 + +#![allow(unused_parens)] +#![allow(unused_imports)] + +use frame_support::weights::{Weight, constants::RocksDbWeight as DbWeight}; + +pub struct WeightInfo; +impl pallet_elections_phragmen::WeightInfo for WeightInfo { + fn vote(v: u32, ) -> Weight { + (35890000 as Weight) + .saturating_add((168000 as Weight).saturating_mul(v as Weight)) + .saturating_add(DbWeight::get().reads(5 as Weight)) + .saturating_add(DbWeight::get().writes(2 as Weight)) + } + fn vote_update(v: u32, ) -> Weight { + (23406000 as Weight) + .saturating_add((312000 as Weight).saturating_mul(v as Weight)) + .saturating_add(DbWeight::get().reads(5 as Weight)) + .saturating_add(DbWeight::get().writes(2 as Weight)) + } + fn remove_voter() -> Weight { + (35814000 as Weight) + .saturating_add(DbWeight::get().reads(2 as Weight)) + .saturating_add(DbWeight::get().writes(2 as Weight)) + } + fn report_defunct_voter_correct(c: u32, v: u32, ) -> Weight { + (0 as Weight) + .saturating_add((1917000 as Weight).saturating_mul(c as Weight)) + .saturating_add((28128000 as Weight).saturating_mul(v as Weight)) + .saturating_add(DbWeight::get().reads(6 as Weight)) + .saturating_add(DbWeight::get().writes(3 as Weight)) + } + fn report_defunct_voter_incorrect(c: u32, v: u32, ) -> Weight { + (0 as Weight) + .saturating_add((1946000 as Weight).saturating_mul(c as Weight)) + .saturating_add((28514000 as Weight).saturating_mul(v as Weight)) + .saturating_add(DbWeight::get().reads(4 as Weight)) + } + fn submit_candidacy(c: u32, ) -> Weight { + (41826000 as Weight) + .saturating_add((235000 as Weight).saturating_mul(c as Weight)) + .saturating_add(DbWeight::get().reads(3 as Weight)) + .saturating_add(DbWeight::get().writes(1 as Weight)) + } + fn renounce_candidacy_candidate(c: u32, ) -> Weight { + (27830000 as Weight) + .saturating_add((181000 as Weight).saturating_mul(c as Weight)) + .saturating_add(DbWeight::get().reads(1 as Weight)) + .saturating_add(DbWeight::get().writes(1 as Weight)) + } + fn renounce_candidacy_members() -> Weight { + (41689000 as Weight) + .saturating_add(DbWeight::get().reads(3 as Weight)) + .saturating_add(DbWeight::get().writes(4 as Weight)) + } + fn renounce_candidacy_runners_up() -> Weight { + (27689000 as Weight) + .saturating_add(DbWeight::get().reads(1 as Weight)) + .saturating_add(DbWeight::get().writes(1 as Weight)) + } + fn remove_member_with_replacement() -> Weight { + (41510000 as Weight) + .saturating_add(DbWeight::get().reads(4 as Weight)) + .saturating_add(DbWeight::get().writes(5 as Weight)) + } + fn remove_member_wrong_refund() -> Weight { + (5159000 as Weight) + .saturating_add(DbWeight::get().reads(1 as Weight)) + } +} diff --git a/frame/elections-phragmen/src/benchmarking.rs b/frame/elections-phragmen/src/benchmarking.rs index 6de9ad57e244f..aaf6d0ea83eb5 100644 --- a/frame/elections-phragmen/src/benchmarking.rs +++ b/frame/elections-phragmen/src/benchmarking.rs @@ -28,10 +28,18 @@ use crate::Module as Elections; const BALANCE_FACTOR: u32 = 250; const MAX_VOTERS: u32 = 500; -const MAX_CANDIDATES: u32 = 100; +const MAX_CANDIDATES: u32 = 200; type Lookup = <::Lookup as StaticLookup>::Source; +macro_rules! whitelist { + ($acc:ident) => { + frame_benchmarking::benchmarking::add_to_whitelist( + frame_system::Account::::hashed_key_for(&$acc).into() + ); + }; +} + /// grab new account with infinite balance. fn endowed_account(name: &'static str, index: u32) -> T::AccountId { let account: T::AccountId = account(name, index, 0); @@ -40,6 +48,7 @@ fn endowed_account(name: &'static str, index: u32) -> T::AccountId { // important to increase the total issuance since T::CurrencyToVote will need it to be sane for // phragmen to work. T::Currency::issue(amount); + account } @@ -61,7 +70,7 @@ fn candidate_count() -> u32 { /// Get the number of votes of a voter. fn vote_count_of(who: &T::AccountId) -> u32 { - >::get(who).1.len() as u32 + >::get(who).votes.len() as u32 } /// A `DefunctVoter` struct with correct value @@ -94,7 +103,7 @@ fn submit_candidates_with_self_vote(c: u32, prefix: &'static str) let candidates = submit_candidates::(c, prefix)?; let stake = default_stake::(BALANCE_FACTOR); let _ = candidates.iter().map(|c| - submit_voter::(c.clone(), vec![c.clone()], stake) + submit_voter::(c.clone(), vec![c.clone()], stake).map(|_| ()) ).collect::>()?; Ok(candidates) } @@ -102,7 +111,7 @@ fn submit_candidates_with_self_vote(c: u32, prefix: &'static str) /// Submit one voter. fn submit_voter(caller: T::AccountId, votes: Vec, stake: BalanceOf) - -> Result<(), &'static str> + -> Result { >::vote(RawOrigin::Signed(caller).into(), votes, stake) .map_err(|_| "failed to submit vote") @@ -131,7 +140,7 @@ fn distribute_voters(mut all_candidates: Vec, num_voters /// Fill the seats of members and runners-up up until `m`. Note that this might include either only /// members, or members and runners-up. fn fill_seats_up_to(m: u32) -> Result, &'static str> { - let candidates = submit_candidates_with_self_vote::(m, "fill_seats_up_to")?; + let _ = submit_candidates_with_self_vote::(m, "fill_seats_up_to")?; assert_eq!(>::candidates().len() as u32, m, "wrong number of candidates."); >::do_phragmen(); assert_eq!(>::candidates().len(), 0, "some candidates remaining."); @@ -140,7 +149,13 @@ fn fill_seats_up_to(m: u32) -> Result, &'static str> m as usize, "wrong number of members and runners-up", ); - Ok(candidates) + Ok( + >::members() + .into_iter() + .map(|(x, _)| x) + .chain(>::runners_up().into_iter().map(|(x, _)| x)) + .collect() + ) } /// removes all the storage items to reverse any genesis state. @@ -152,50 +167,46 @@ fn clean() { } benchmarks! { - _ { - // User account seed - let u in 0 .. 1000 => (); - } + _ {} // -- Signed ones vote { - let u in ...; - // we fix the number of voted candidates to max - let v = MAXIMUM_VOTE; + let v in 1 .. (MAXIMUM_VOTE as u32); clean::(); // create a bunch of candidates. - let all_candidates = submit_candidates::(MAXIMUM_VOTE as u32, "candidates")?; + let all_candidates = submit_candidates::(v, "candidates")?; - let caller = endowed_account::("caller", u); + let caller = endowed_account::("caller", 0); let stake = default_stake::(BALANCE_FACTOR); // vote for all of them. - let votes = all_candidates.into_iter().take(v).collect(); + let votes = all_candidates; + whitelist!(caller); }: _(RawOrigin::Signed(caller), votes, stake) vote_update { - let u in ...; - // we fix the number of voted candidates to max - let v = MAXIMUM_VOTE; + let v in 1 .. (MAXIMUM_VOTE as u32); clean::(); // create a bunch of candidates. - let all_candidates = submit_candidates::(MAXIMUM_VOTE as u32, "candidates")?; + let all_candidates = submit_candidates::(v, "candidates")?; - let caller = endowed_account::("caller", u); + let caller = endowed_account::("caller", 0); let stake = default_stake::(BALANCE_FACTOR); // original votes. - let mut votes = all_candidates.into_iter().take(v).collect::>(); + let mut votes = all_candidates; submit_voter::(caller.clone(), votes.clone(), stake)?; + // new votes. votes.rotate_left(1); + + whitelist!(caller); }: vote(RawOrigin::Signed(caller), votes, stake) remove_voter { - let u in ...; // we fix the number of voted candidates to max let v = MAXIMUM_VOTE as u32; clean::(); @@ -203,11 +214,12 @@ benchmarks! { // create a bunch of candidates. let all_candidates = submit_candidates::(v, "candidates")?; - let caller = endowed_account::("caller", u); + let caller = endowed_account::("caller", 0); let stake = default_stake::(BALANCE_FACTOR); submit_voter::(caller.clone(), all_candidates, stake)?; + whitelist!(caller); }: _(RawOrigin::Signed(caller)) report_defunct_voter_correct { @@ -217,11 +229,11 @@ benchmarks! { // number of candidates that the reported voter voted for. The worse case of search here is // basically `c * v`. let v in 1 .. (MAXIMUM_VOTE as u32); - // we fix the number of members to when members and runners-up to the desired. We'll be in + // we fix the number of members to the number of desired members and runners-up. We'll be in // this state almost always. let m = T::DesiredMembers::get() + T::DesiredRunnersUp::get(); - clean::(); + clean::(); let stake = default_stake::(BALANCE_FACTOR); // create m members and runners combined. @@ -231,14 +243,8 @@ benchmarks! { let bailing_candidates = submit_candidates::(v, "bailing_candidates")?; let all_candidates = submit_candidates::(c, "all_candidates")?; - // account 1 is the reporter and it doesn't matter how many it votes. But it has to be a - // voter. + // account 1 is the reporter and must be whitelisted. let account_1 = endowed_account::("caller", 0); - submit_voter::( - account_1.clone(), - all_candidates.iter().take(1).cloned().collect(), - stake, - )?; // account 2 votes for all of the mentioned candidates. let account_2 = endowed_account::("caller_2", 1); @@ -248,7 +254,9 @@ benchmarks! { stake, )?; - // all the bailers go away. + // all the bailers go away. NOTE: we can simplify this. There's no need to create all these + // candidates and remove them. The defunct voter can just vote for random accounts as long + // as there are enough members (potential candidates). bailing_candidates.into_iter().for_each(|b| { let count = candidate_count::(); assert!(>::renounce_candidacy( @@ -256,10 +264,13 @@ benchmarks! { Renouncing::Candidate(count), ).is_ok()); }); - let defunct = defunct_for::(account_2.clone()); - }: report_defunct_voter(RawOrigin::Signed(account_1.clone()), defunct) + + let defunct_info = defunct_for::(account_2.clone()); + whitelist!(account_1); + + assert!(>::is_voter(&account_2)); + }: report_defunct_voter(RawOrigin::Signed(account_1.clone()), defunct_info) verify { - assert!(>::is_voter(&account_1)); assert!(!>::is_voter(&account_2)); #[cfg(test)] { @@ -276,7 +287,7 @@ benchmarks! { // number of candidates that the reported voter voted for. The worse case of search here is // basically `c * v`. let v in 1 .. (MAXIMUM_VOTE as u32); - // we fix the number of members to when members and runners-up to the desired. We'll be in + // we fix the number of members to the number of desired members and runners-up. We'll be in // this state almost always. let m = T::DesiredMembers::get() + T::DesiredRunnersUp::get(); @@ -289,18 +300,14 @@ benchmarks! { // create a bunch of candidates as well. let all_candidates = submit_candidates::(c, "candidates")?; - // account 1 is the reporter and it doesn't matter how many it votes. + // account 1 is the reporter and need to be whitelisted. let account_1 = endowed_account::("caller", 0); - submit_voter::( - account_1.clone(), - all_candidates.iter().take(1).cloned().collect(), - stake, - )?; // account 2 votes for a bunch of crap, and finally a correct candidate. let account_2 = endowed_account::("caller_2", 1); - let mut invalid: Vec = - (0..(v-1)).map(|seed| account::("invalid", 0, seed).clone()).collect(); + let mut invalid: Vec = (0..(v-1)) + .map(|seed| account::("invalid", 0, seed).clone()) + .collect(); invalid.push(all_candidates.last().unwrap().clone()); submit_voter::( account_2.clone(), @@ -308,11 +315,11 @@ benchmarks! { stake, )?; - let defunct = defunct_for::(account_2.clone()); - // no one bails out. account_1 is slashed and removed as voter now. - }: report_defunct_voter(RawOrigin::Signed(account_1.clone()), defunct) + let defunct_info = defunct_for::(account_2.clone()); + whitelist!(account_1); + }: report_defunct_voter(RawOrigin::Signed(account_1.clone()), defunct_info) verify { - assert!(!>::is_voter(&account_1)); + // account 2 is still a voter. assert!(>::is_voter(&account_2)); #[cfg(test)] { @@ -325,7 +332,7 @@ benchmarks! { submit_candidacy { // number of already existing candidates. let c in 1 .. MAX_CANDIDATES; - // we fix the number of members to when members and runners-up to the desired. We'll be in + // we fix the number of members to the number of desired members and runners-up. We'll be in // this state almost always. let m = T::DesiredMembers::get() + T::DesiredRunnersUp::get(); @@ -340,6 +347,7 @@ benchmarks! { // we assume worse case that: extrinsic is successful and candidate is not duplicate. let candidate_account = endowed_account::("caller", 0); + whitelist!(candidate_account); }: _(RawOrigin::Signed(candidate_account.clone()), candidate_count::()) verify { #[cfg(test)] @@ -355,7 +363,7 @@ benchmarks! { // limited by the runtime bound, nonetheless we fill them by `m`. // number of already existing candidates. let c in 1 .. MAX_CANDIDATES; - // we fix the number of members to when members and runners-up to the desired. We'll be in + // we fix the number of members to the number of desired members and runners-up. We'll be in // this state almost always. let m = T::DesiredMembers::get() + T::DesiredRunnersUp::get(); @@ -367,6 +375,7 @@ benchmarks! { let bailing = all_candidates[0].clone(); // Should be ("caller", 0) let count = candidate_count::(); + whitelist!(bailing); }: renounce_candidacy(RawOrigin::Signed(bailing), Renouncing::Candidate(count)) verify { #[cfg(test)] @@ -377,11 +386,10 @@ benchmarks! { } } - renounce_candidacy_member_runner_up { + renounce_candidacy_members { // removing members and runners will be cheaper than a candidate. // we fix the number of members to when members and runners-up to the desired. We'll be in // this state almost always. - let u in ...; let m = T::DesiredMembers::get() + T::DesiredRunnersUp::get(); clean::(); @@ -389,14 +397,34 @@ benchmarks! { let members_and_runners_up = fill_seats_up_to::(m)?; let bailing = members_and_runners_up[0].clone(); - let renouncing = if >::is_member(&bailing) { - Renouncing::Member - } else if >::is_runner_up(&bailing) { - Renouncing::RunnerUp - } else { - panic!("Bailing must be a member or runner-up for this bench to be sane."); - }; - }: renounce_candidacy(RawOrigin::Signed(bailing.clone()), renouncing) + assert!(>::is_member(&bailing)); + + whitelist!(bailing); + }: renounce_candidacy(RawOrigin::Signed(bailing.clone()), Renouncing::Member) + verify { + #[cfg(test)] + { + // reset members in between benchmark tests. + use crate::tests::MEMBERS; + MEMBERS.with(|m| *m.borrow_mut() = vec![]); + } + } + + renounce_candidacy_runners_up { + // removing members and runners will be cheaper than a candidate. + // we fix the number of members to when members and runners-up to the desired. We'll be in + // this state almost always. + let m = T::DesiredMembers::get() + T::DesiredRunnersUp::get(); + clean::(); + + // create m members and runners combined. + let members_and_runners_up = fill_seats_up_to::(m)?; + + let bailing = members_and_runners_up[T::DesiredMembers::get() as usize + 1].clone(); + assert!(>::is_runner_up(&bailing)); + + whitelist!(bailing); + }: renounce_candidacy(RawOrigin::Signed(bailing.clone()), Renouncing::RunnerUp) verify { #[cfg(test)] { @@ -407,6 +435,7 @@ benchmarks! { } // -- Root ones + #[extra] // this calls into phragmen and consumes a full block for now. remove_member_without_replacement { // worse case is when we remove a member and we have no runner as a replacement. This // triggers phragmen again. The only parameter is how many candidates will compete for the @@ -440,7 +469,6 @@ benchmarks! { remove_member_with_replacement { // easy case. We have a runner up. Nothing will have that much of an impact. m will be // number of members and runners. There is always at least one runner. - let u in ...; let m = T::DesiredMembers::get() + T::DesiredRunnersUp::get(); clean::(); @@ -461,7 +489,6 @@ benchmarks! { remove_member_wrong_refund { // The root call by mistake indicated that this will have no replacement, while it has! // this has now consumed a lot of weight and need to refund. - let u in ...; let m = T::DesiredMembers::get() + T::DesiredRunnersUp::get(); clean::(); @@ -484,6 +511,7 @@ benchmarks! { } } + #[extra] on_initialize { // if n % TermDuration is zero, then we run phragmen. The weight function must and should // check this as it is cheap to do so. TermDuration is not a storage item, it is a constant @@ -514,6 +542,7 @@ benchmarks! { } } + #[extra] phragmen { // This is just to focus on phragmen in the context of this module. We always select 20 // members, this is hard-coded in the runtime and cannot be trivially changed at this stage. @@ -578,7 +607,11 @@ mod tests { }); ExtBuilder::default().desired_members(13).desired_runners_up(7).build_and_execute(|| { - assert_ok!(test_benchmark_renounce_candidacy_member_runner_up::()); + assert_ok!(test_benchmark_renounce_candidacy_runners_up::()); + }); + + ExtBuilder::default().desired_members(13).desired_runners_up(7).build_and_execute(|| { + assert_ok!(test_benchmark_renounce_candidacy_members::()); }); ExtBuilder::default().desired_members(13).desired_runners_up(7).build_and_execute(|| { diff --git a/frame/elections-phragmen/src/default_weights.rs b/frame/elections-phragmen/src/default_weights.rs new file mode 100644 index 0000000000000..9a718b973cc50 --- /dev/null +++ b/frame/elections-phragmen/src/default_weights.rs @@ -0,0 +1,87 @@ +// This file is part of Substrate. + +// Copyright (C) 2019-2020 Parity Technologies (UK) Ltd. +// SPDX-License-Identifier: Apache-2.0 + +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +//! THIS FILE WAS AUTO-GENERATED USING THE SUBSTRATE BENCHMARK CLI VERSION 2.0.0-rc6 + +#![allow(unused_parens)] +#![allow(unused_imports)] + +use frame_support::weights::{Weight, constants::RocksDbWeight as DbWeight}; + +impl crate::WeightInfo for () { + fn vote(v: u32, ) -> Weight { + (35890000 as Weight) + .saturating_add((168000 as Weight).saturating_mul(v as Weight)) + .saturating_add(DbWeight::get().reads(5 as Weight)) + .saturating_add(DbWeight::get().writes(2 as Weight)) + } + fn vote_update(v: u32, ) -> Weight { + (23406000 as Weight) + .saturating_add((312000 as Weight).saturating_mul(v as Weight)) + .saturating_add(DbWeight::get().reads(5 as Weight)) + .saturating_add(DbWeight::get().writes(2 as Weight)) + } + fn remove_voter() -> Weight { + (35814000 as Weight) + .saturating_add(DbWeight::get().reads(2 as Weight)) + .saturating_add(DbWeight::get().writes(2 as Weight)) + } + fn report_defunct_voter_correct(c: u32, v: u32, ) -> Weight { + (0 as Weight) + .saturating_add((1917000 as Weight).saturating_mul(c as Weight)) + .saturating_add((28128000 as Weight).saturating_mul(v as Weight)) + .saturating_add(DbWeight::get().reads(6 as Weight)) + .saturating_add(DbWeight::get().writes(3 as Weight)) + } + fn report_defunct_voter_incorrect(c: u32, v: u32, ) -> Weight { + (0 as Weight) + .saturating_add((1946000 as Weight).saturating_mul(c as Weight)) + .saturating_add((28514000 as Weight).saturating_mul(v as Weight)) + .saturating_add(DbWeight::get().reads(4 as Weight)) + } + fn submit_candidacy(c: u32, ) -> Weight { + (41826000 as Weight) + .saturating_add((235000 as Weight).saturating_mul(c as Weight)) + .saturating_add(DbWeight::get().reads(3 as Weight)) + .saturating_add(DbWeight::get().writes(1 as Weight)) + } + fn renounce_candidacy_candidate(c: u32, ) -> Weight { + (27830000 as Weight) + .saturating_add((181000 as Weight).saturating_mul(c as Weight)) + .saturating_add(DbWeight::get().reads(1 as Weight)) + .saturating_add(DbWeight::get().writes(1 as Weight)) + } + fn renounce_candidacy_members() -> Weight { + (41689000 as Weight) + .saturating_add(DbWeight::get().reads(3 as Weight)) + .saturating_add(DbWeight::get().writes(4 as Weight)) + } + fn renounce_candidacy_runners_up() -> Weight { + (27689000 as Weight) + .saturating_add(DbWeight::get().reads(1 as Weight)) + .saturating_add(DbWeight::get().writes(1 as Weight)) + } + fn remove_member_with_replacement() -> Weight { + (41510000 as Weight) + .saturating_add(DbWeight::get().reads(4 as Weight)) + .saturating_add(DbWeight::get().writes(5 as Weight)) + } + fn remove_member_wrong_refund() -> Weight { + (5159000 as Weight) + .saturating_add(DbWeight::get().reads(1 as Weight)) + } +} diff --git a/frame/elections-phragmen/src/lib.rs b/frame/elections-phragmen/src/lib.rs index 372ae11b148ff..e0ec0491ee92c 100644 --- a/frame/elections-phragmen/src/lib.rs +++ b/frame/elections-phragmen/src/lib.rs @@ -91,12 +91,12 @@ use sp_runtime::{ }; use frame_support::{ decl_storage, decl_event, ensure, decl_module, decl_error, - weights::{Weight, constants::{WEIGHT_PER_MICROS, WEIGHT_PER_NANOS}}, + weights::Weight, storage::{StorageMap, IterableStorageMap}, dispatch::{DispatchResultWithPostInfo, WithPostDispatchInfo}, traits::{ Currency, Get, LockableCurrency, LockIdentifier, ReservableCurrency, WithdrawReasons, - ChangeMembers, OnUnbalanced, WithdrawReason, Contains, BalanceStatus, InitializeMembers, + ChangeMembers, OnUnbalanced, WithdrawReason, Contains, InitializeMembers, BalanceStatus, ContainsLengthBound, } }; @@ -104,6 +104,7 @@ use sp_npos_elections::{build_support_map, ExtendedBalance, VoteWeight, Election use frame_system::{ensure_signed, ensure_root}; mod benchmarking; +mod default_weights; /// The maximum votes allowed per voter. pub const MAXIMUM_VOTE: usize = 16; @@ -138,35 +139,17 @@ pub struct DefunctVoter { } pub trait WeightInfo { - fn vote(u: u32, ) -> Weight; - fn vote_update(u: u32, ) -> Weight; - fn remove_voter(u: u32, ) -> Weight; + fn vote(v: u32, ) -> Weight; + fn vote_update(v: u32, ) -> Weight; + fn remove_voter() -> Weight; fn report_defunct_voter_correct(c: u32, v: u32, ) -> Weight; fn report_defunct_voter_incorrect(c: u32, v: u32, ) -> Weight; fn submit_candidacy(c: u32, ) -> Weight; fn renounce_candidacy_candidate(c: u32, ) -> Weight; - fn renounce_candidacy_member_runner_up(u: u32, ) -> Weight; - fn remove_member_without_replacement(c: u32, ) -> Weight; - fn remove_member_with_replacement(u: u32, ) -> Weight; - fn remove_member_wrong_refund(u: u32, ) -> Weight; - fn on_initialize(c: u32, ) -> Weight; - fn phragmen(c: u32, v: u32, e: u32, ) -> Weight; -} - -impl WeightInfo for () { - fn vote(_u: u32, ) -> Weight { 1_000_000_000 } - fn vote_update(_u: u32, ) -> Weight { 1_000_000_000 } - fn remove_voter(_u: u32, ) -> Weight { 1_000_000_000 } - fn report_defunct_voter_correct(_c: u32, _v: u32, ) -> Weight { 1_000_000_000 } - fn report_defunct_voter_incorrect(_c: u32, _v: u32, ) -> Weight { 1_000_000_000 } - fn submit_candidacy(_c: u32, ) -> Weight { 1_000_000_000 } - fn renounce_candidacy_candidate(_c: u32, ) -> Weight { 1_000_000_000 } - fn renounce_candidacy_member_runner_up(_u: u32, ) -> Weight { 1_000_000_000 } - fn remove_member_without_replacement(_c: u32, ) -> Weight { 1_000_000_000 } - fn remove_member_with_replacement(_u: u32, ) -> Weight { 1_000_000_000 } - fn remove_member_wrong_refund(_u: u32, ) -> Weight { 1_000_000_000 } - fn on_initialize(_c: u32, ) -> Weight { 1_000_000_000 } - fn phragmen(_c: u32, _v: u32, _e: u32, ) -> Weight { 1_000_000_000 } + fn renounce_candidacy_members() -> Weight; + fn renounce_candidacy_runners_up() -> Weight; + fn remove_member_with_replacement() -> Weight; + fn remove_member_wrong_refund() -> Weight; } pub trait Trait: frame_system::Trait { @@ -350,13 +333,14 @@ decl_module! { /// State reads: /// - Candidates.len() + Members.len() + RunnersUp.len() /// - Voting (is_voter) + /// - Lock /// - [AccountBalance(who) (unreserve + total_balance)] /// State writes: /// - Voting /// - Lock /// - [AccountBalance(who) (unreserve -- only when creating a new voter)] /// # - #[weight = 50 * WEIGHT_PER_MICROS + T::DbWeight::get().reads_writes(4, 2)] + #[weight = T::WeightInfo::vote(votes.len() as u32)] fn vote( origin, votes: Vec, @@ -412,7 +396,7 @@ decl_module! { /// - Locks /// - [AccountData(who)] /// # - #[weight = 35 * WEIGHT_PER_MICROS + T::DbWeight::get().reads_writes(1, 2)] + #[weight = T::WeightInfo::remove_voter()] fn remove_voter(origin) { let who = ensure_signed(origin)?; ensure!(Self::is_voter(&who), Error::::MustBeVoter); @@ -447,15 +431,14 @@ decl_module! { /// - Voting(reporter || target) /// Note: the db access is worse with respect to db, which is when the report is correct. /// # - #[weight = - Weight::from(defunct.candidate_count).saturating_mul(2 * WEIGHT_PER_MICROS) - .saturating_add(Weight::from(defunct.vote_count).saturating_mul(19 * WEIGHT_PER_MICROS)) - .saturating_add(T::DbWeight::get().reads_writes(6, 3)) - ] + #[weight = T::WeightInfo::report_defunct_voter_correct( + defunct.candidate_count, + defunct.vote_count, + )] fn report_defunct_voter( origin, defunct: DefunctVoter<::Source>, - ) { + ) -> DispatchResultWithPostInfo { let reporter = ensure_signed(origin)?; let target = T::Lookup::lookup(defunct.who)?; @@ -482,19 +465,25 @@ decl_module! { ); let valid = Self::is_defunct_voter(&votes); - if valid { + let maybe_refund = if valid { // reporter will get the voting bond of the target T::Currency::repatriate_reserved(&target, &reporter, T::VotingBond::get(), BalanceStatus::Free)?; // remove the target. They are defunct. Self::do_remove_voter(&target, false); + None } else { // slash the bond of the reporter. let imbalance = T::Currency::slash_reserved(&reporter, T::VotingBond::get()).0; T::BadReport::on_unbalanced(imbalance); // remove the reporter. Self::do_remove_voter(&reporter, false); - } + Some(T::WeightInfo::report_defunct_voter_incorrect( + defunct.candidate_count, + defunct.vote_count, + )) + }; Self::deposit_event(RawEvent::VoterReported(target, reporter, valid)); + Ok(maybe_refund.into()) } /// Submit oneself for candidacy. @@ -509,7 +498,6 @@ decl_module! { /// Base weight = 33.33 µs /// Complexity of candidate_count: 0.375 µs /// State reads: - /// - Candidates.len() /// - Candidates /// - Members /// - RunnersUp @@ -518,11 +506,7 @@ decl_module! { /// - [AccountBalance(who)] /// - Candidates /// # - #[weight = - (35 * WEIGHT_PER_MICROS) - .saturating_add(Weight::from(*candidate_count).saturating_mul(375 * WEIGHT_PER_NANOS)) - .saturating_add(T::DbWeight::get().reads_writes(4, 1)) - ] + #[weight = T::WeightInfo::submit_candidacy(*candidate_count)] fn submit_candidacy(origin, #[compact] candidate_count: u32) { let who = ensure_signed(origin)?; @@ -582,23 +566,11 @@ decl_module! { /// State writes: /// - RunnersUp (remove_and_replace_member), /// - [AccountData(who) (unreserve)] - /// - /// Weight note: The call into changeMembers need to be accounted for. /// #[weight = match *renouncing { - Renouncing::Candidate(count) => { - (18 * WEIGHT_PER_MICROS) - .saturating_add(Weight::from(count).saturating_mul(235 * WEIGHT_PER_NANOS)) - .saturating_add(T::DbWeight::get().reads_writes(1, 1)) - }, - Renouncing::Member => { - 46 * WEIGHT_PER_MICROS + - T::DbWeight::get().reads_writes(2, 2) - }, - Renouncing::RunnerUp => { - 46 * WEIGHT_PER_MICROS + - T::DbWeight::get().reads_writes(1, 1) - } + Renouncing::Candidate(count) => T::WeightInfo::renounce_candidacy_candidate(count), + Renouncing::Member => T::WeightInfo::renounce_candidacy_members(), + Renouncing::RunnerUp => T::WeightInfo::renounce_candidacy_runners_up(), }] fn renounce_candidacy(origin, renouncing: Renouncing) { let who = ensure_signed(origin)?; @@ -659,7 +631,7 @@ decl_module! { /// Else, since this is a root call and will go into phragmen, we assume full block for now. /// # #[weight = if *has_replacement { - 50 * WEIGHT_PER_MICROS + T::DbWeight::get().reads_writes(3, 2) + T::WeightInfo::remove_member_with_replacement() } else { T::MaximumBlockWeight::get() }] @@ -677,7 +649,7 @@ decl_module! { return Err(Error::::InvalidReplacement.with_weight( // refund. The weight value comes from a benchmark which is special to this. // 5.751 µs - 6 * WEIGHT_PER_MICROS + T::DbWeight::get().reads_writes(1, 0) + T::WeightInfo::remove_member_wrong_refund() )); } // else, prediction was correct. @@ -832,7 +804,9 @@ impl Module { /// Reads Members, RunnersUp, Candidates and Voting(who) from database. fn is_defunct_voter(votes: &[T::AccountId]) -> bool { votes.iter().all(|v| - !Self::is_member(v) && !Self::is_runner_up(v) && !Self::is_candidate(v).is_ok() + !Self::is_member(v) && + !Self::is_runner_up(v) && + !Self::is_candidate(v).is_ok() ) } @@ -906,12 +880,12 @@ impl Module { // used for prime election. let voters_and_stakes = Voting::::iter() - .map(|(voter, (stake, targets))| { (voter, stake, targets) }) + .map(|(voter, (stake, votes))| { (voter, stake, votes) }) .collect::>(); // used for phragmen. let voters_and_votes = voters_and_stakes.iter() .cloned() - .map(|(voter, stake, targets)| { (voter, to_votes(stake), targets)} ) + .map(|(voter, stake, votes)| { (voter, to_votes(stake), votes)} ) .collect::>(); let maybe_phragmen_result = sp_npos_elections::seq_phragmen::( num_to_elect, @@ -976,8 +950,8 @@ impl Module { // of the votes. i.e. the first person a voter votes for gets a 16x multiplier, // the next person gets a 15x multiplier, an so on... (assuming `MAXIMUM_VOTE` = 16) let mut prime_votes: Vec<_> = new_members.iter().map(|c| (&c.0, BalanceOf::::zero())).collect(); - for (_, stake, targets) in voters_and_stakes.into_iter() { - for (vote_multiplier, who) in targets.iter() + for (_, stake, votes) in voters_and_stakes.into_iter() { + for (vote_multiplier, who) in votes.iter() .enumerate() .map(|(vote_position, who)| ((MAXIMUM_VOTE - vote_position) as u32, who)) { @@ -2434,7 +2408,7 @@ mod tests { assert_err_with_weight!( Elections::remove_member(Origin::root(), 4, true), Error::::InvalidReplacement, - Some(6000000), + Some(30159000), // only thing that matters for now is that it is NOT the full block. ); }); @@ -2456,7 +2430,7 @@ mod tests { assert_err_with_weight!( Elections::remove_member(Origin::root(), 4, false), Error::::InvalidReplacement, - Some(6000000) // only thing that matters for now is that it is NOT the full block. + Some(30159000) // only thing that matters for now is that it is NOT the full block. ); }); } diff --git a/frame/elections/src/lib.rs b/frame/elections/src/lib.rs index a5c6d0eb2ba2d..9b61a9b3509a9 100644 --- a/frame/elections/src/lib.rs +++ b/frame/elections/src/lib.rs @@ -15,6 +15,13 @@ // See the License for the specific language governing permissions and // limitations under the License. +//! # WARNING: NOT ACTIVELY MAINTAINED +//! +//! This pallet is currently not maintained and should not be used in production until further +//! notice. +//! +//! --- +//! //! Election module for stake-weighted membership selection of a collective. //! //! The composition of a set of account IDs works according to one or more approval votes From 20153c087859186d33295f10f03231857cfd8413 Mon Sep 17 00:00:00 2001 From: kianenigma Date: Mon, 21 Sep 2020 11:28:54 +0200 Subject: [PATCH 2/8] Fix benchmark tests --- frame/elections-phragmen/src/benchmarking.rs | 19 ++++++++++++++----- 1 file changed, 14 insertions(+), 5 deletions(-) diff --git a/frame/elections-phragmen/src/benchmarking.rs b/frame/elections-phragmen/src/benchmarking.rs index aaf6d0ea83eb5..f4a83c670e853 100644 --- a/frame/elections-phragmen/src/benchmarking.rs +++ b/frame/elections-phragmen/src/benchmarking.rs @@ -70,7 +70,7 @@ fn candidate_count() -> u32 { /// Get the number of votes of a voter. fn vote_count_of(who: &T::AccountId) -> u32 { - >::get(who).votes.len() as u32 + >::get(who).1.len() as u32 } /// A `DefunctVoter` struct with correct value @@ -111,10 +111,9 @@ fn submit_candidates_with_self_vote(c: u32, prefix: &'static str) /// Submit one voter. fn submit_voter(caller: T::AccountId, votes: Vec, stake: BalanceOf) - -> Result + -> Result<(), sp_runtime::DispatchError> { >::vote(RawOrigin::Signed(caller).into(), votes, stake) - .map_err(|_| "failed to submit vote") } /// create `num_voter` voters who randomly vote for at most `votes` of `all_candidates` if @@ -243,8 +242,13 @@ benchmarks! { let bailing_candidates = submit_candidates::(v, "bailing_candidates")?; let all_candidates = submit_candidates::(c, "all_candidates")?; - // account 1 is the reporter and must be whitelisted. + // account 1 is the reporter and must be whitelisted, and a voter. let account_1 = endowed_account::("caller", 0); + submit_voter::( + account_1.clone(), + all_candidates.iter().take(1).cloned().collect(), + stake, + )?; // account 2 votes for all of the mentioned candidates. let account_2 = endowed_account::("caller_2", 1); @@ -300,8 +304,13 @@ benchmarks! { // create a bunch of candidates as well. let all_candidates = submit_candidates::(c, "candidates")?; - // account 1 is the reporter and need to be whitelisted. + // account 1 is the reporter and need to be whitelisted, and a voter. let account_1 = endowed_account::("caller", 0); + submit_voter::( + account_1.clone(), + all_candidates.iter().take(1).cloned().collect(), + stake, + )?; // account 2 votes for a bunch of crap, and finally a correct candidate. let account_2 = endowed_account::("caller_2", 1); From e3414a74b0f1d7f6349f3e1edb0c298125d94620 Mon Sep 17 00:00:00 2001 From: kianenigma Date: Mon, 21 Sep 2020 16:18:26 +0200 Subject: [PATCH 3/8] Update weights --- .../src/weights/pallet_elections_phragmen.rs | 39 ++++++++++--------- frame/elections-phragmen/src/benchmarking.rs | 2 +- .../elections-phragmen/src/default_weights.rs | 39 ++++++++++--------- 3 files changed, 41 insertions(+), 39 deletions(-) diff --git a/bin/node/runtime/src/weights/pallet_elections_phragmen.rs b/bin/node/runtime/src/weights/pallet_elections_phragmen.rs index 2071cc28eca5a..a6c9d42f7c95d 100644 --- a/bin/node/runtime/src/weights/pallet_elections_phragmen.rs +++ b/bin/node/runtime/src/weights/pallet_elections_phragmen.rs @@ -25,64 +25,65 @@ use frame_support::weights::{Weight, constants::RocksDbWeight as DbWeight}; pub struct WeightInfo; impl pallet_elections_phragmen::WeightInfo for WeightInfo { fn vote(v: u32, ) -> Weight { - (35890000 as Weight) - .saturating_add((168000 as Weight).saturating_mul(v as Weight)) + (316_492_000 as Weight) + .saturating_add((440_000 as Weight).saturating_mul(v as Weight)) .saturating_add(DbWeight::get().reads(5 as Weight)) .saturating_add(DbWeight::get().writes(2 as Weight)) } fn vote_update(v: u32, ) -> Weight { - (23406000 as Weight) - .saturating_add((312000 as Weight).saturating_mul(v as Weight)) + (180_930_000 as Weight) + .saturating_add((444_000 as Weight).saturating_mul(v as Weight)) .saturating_add(DbWeight::get().reads(5 as Weight)) .saturating_add(DbWeight::get().writes(2 as Weight)) } fn remove_voter() -> Weight { - (35814000 as Weight) + (256_953_000 as Weight) .saturating_add(DbWeight::get().reads(2 as Weight)) .saturating_add(DbWeight::get().writes(2 as Weight)) } fn report_defunct_voter_correct(c: u32, v: u32, ) -> Weight { (0 as Weight) - .saturating_add((1917000 as Weight).saturating_mul(c as Weight)) - .saturating_add((28128000 as Weight).saturating_mul(v as Weight)) - .saturating_add(DbWeight::get().reads(6 as Weight)) + .saturating_add((91_785_000 as Weight).saturating_mul(c as Weight)) + .saturating_add((1_326_665_000 as Weight).saturating_mul(v as Weight)) + .saturating_add(DbWeight::get().reads(7 as Weight)) .saturating_add(DbWeight::get().writes(3 as Weight)) } fn report_defunct_voter_incorrect(c: u32, v: u32, ) -> Weight { (0 as Weight) - .saturating_add((1946000 as Weight).saturating_mul(c as Weight)) - .saturating_add((28514000 as Weight).saturating_mul(v as Weight)) - .saturating_add(DbWeight::get().reads(4 as Weight)) + .saturating_add((91_822_000 as Weight).saturating_mul(c as Weight)) + .saturating_add((1_327_023_000 as Weight).saturating_mul(v as Weight)) + .saturating_add(DbWeight::get().reads(6 as Weight)) + .saturating_add(DbWeight::get().writes(2 as Weight)) } fn submit_candidacy(c: u32, ) -> Weight { - (41826000 as Weight) - .saturating_add((235000 as Weight).saturating_mul(c as Weight)) + (341_953_000 as Weight) + .saturating_add((13_693_000 as Weight).saturating_mul(c as Weight)) .saturating_add(DbWeight::get().reads(3 as Weight)) .saturating_add(DbWeight::get().writes(1 as Weight)) } fn renounce_candidacy_candidate(c: u32, ) -> Weight { - (27830000 as Weight) - .saturating_add((181000 as Weight).saturating_mul(c as Weight)) + (125_921_000 as Weight) + .saturating_add((6_860_000 as Weight).saturating_mul(c as Weight)) .saturating_add(DbWeight::get().reads(1 as Weight)) .saturating_add(DbWeight::get().writes(1 as Weight)) } fn renounce_candidacy_members() -> Weight { - (41689000 as Weight) + (389_571_000 as Weight) .saturating_add(DbWeight::get().reads(3 as Weight)) .saturating_add(DbWeight::get().writes(4 as Weight)) } fn renounce_candidacy_runners_up() -> Weight { - (27689000 as Weight) + (191_813_000 as Weight) .saturating_add(DbWeight::get().reads(1 as Weight)) .saturating_add(DbWeight::get().writes(1 as Weight)) } fn remove_member_with_replacement() -> Weight { - (41510000 as Weight) + (365_327_000 as Weight) .saturating_add(DbWeight::get().reads(4 as Weight)) .saturating_add(DbWeight::get().writes(5 as Weight)) } fn remove_member_wrong_refund() -> Weight { - (5159000 as Weight) + (20_302_000 as Weight) .saturating_add(DbWeight::get().reads(1 as Weight)) } } diff --git a/frame/elections-phragmen/src/benchmarking.rs b/frame/elections-phragmen/src/benchmarking.rs index f4a83c670e853..e7c3719480b70 100644 --- a/frame/elections-phragmen/src/benchmarking.rs +++ b/frame/elections-phragmen/src/benchmarking.rs @@ -103,7 +103,7 @@ fn submit_candidates_with_self_vote(c: u32, prefix: &'static str) let candidates = submit_candidates::(c, prefix)?; let stake = default_stake::(BALANCE_FACTOR); let _ = candidates.iter().map(|c| - submit_voter::(c.clone(), vec![c.clone()], stake).map(|_| ()) + submit_voter::(c.clone(), vec![c.clone()], stake) ).collect::>()?; Ok(candidates) } diff --git a/frame/elections-phragmen/src/default_weights.rs b/frame/elections-phragmen/src/default_weights.rs index 9a718b973cc50..5b192856bc84f 100644 --- a/frame/elections-phragmen/src/default_weights.rs +++ b/frame/elections-phragmen/src/default_weights.rs @@ -24,64 +24,65 @@ use frame_support::weights::{Weight, constants::RocksDbWeight as DbWeight}; impl crate::WeightInfo for () { fn vote(v: u32, ) -> Weight { - (35890000 as Weight) - .saturating_add((168000 as Weight).saturating_mul(v as Weight)) + (316_492_000 as Weight) + .saturating_add((440_000 as Weight).saturating_mul(v as Weight)) .saturating_add(DbWeight::get().reads(5 as Weight)) .saturating_add(DbWeight::get().writes(2 as Weight)) } fn vote_update(v: u32, ) -> Weight { - (23406000 as Weight) - .saturating_add((312000 as Weight).saturating_mul(v as Weight)) + (180_930_000 as Weight) + .saturating_add((444_000 as Weight).saturating_mul(v as Weight)) .saturating_add(DbWeight::get().reads(5 as Weight)) .saturating_add(DbWeight::get().writes(2 as Weight)) } fn remove_voter() -> Weight { - (35814000 as Weight) + (256_953_000 as Weight) .saturating_add(DbWeight::get().reads(2 as Weight)) .saturating_add(DbWeight::get().writes(2 as Weight)) } fn report_defunct_voter_correct(c: u32, v: u32, ) -> Weight { (0 as Weight) - .saturating_add((1917000 as Weight).saturating_mul(c as Weight)) - .saturating_add((28128000 as Weight).saturating_mul(v as Weight)) - .saturating_add(DbWeight::get().reads(6 as Weight)) + .saturating_add((91_785_000 as Weight).saturating_mul(c as Weight)) + .saturating_add((1_326_665_000 as Weight).saturating_mul(v as Weight)) + .saturating_add(DbWeight::get().reads(7 as Weight)) .saturating_add(DbWeight::get().writes(3 as Weight)) } fn report_defunct_voter_incorrect(c: u32, v: u32, ) -> Weight { (0 as Weight) - .saturating_add((1946000 as Weight).saturating_mul(c as Weight)) - .saturating_add((28514000 as Weight).saturating_mul(v as Weight)) - .saturating_add(DbWeight::get().reads(4 as Weight)) + .saturating_add((91_822_000 as Weight).saturating_mul(c as Weight)) + .saturating_add((1_327_023_000 as Weight).saturating_mul(v as Weight)) + .saturating_add(DbWeight::get().reads(6 as Weight)) + .saturating_add(DbWeight::get().writes(2 as Weight)) } fn submit_candidacy(c: u32, ) -> Weight { - (41826000 as Weight) - .saturating_add((235000 as Weight).saturating_mul(c as Weight)) + (341_953_000 as Weight) + .saturating_add((13_693_000 as Weight).saturating_mul(c as Weight)) .saturating_add(DbWeight::get().reads(3 as Weight)) .saturating_add(DbWeight::get().writes(1 as Weight)) } fn renounce_candidacy_candidate(c: u32, ) -> Weight { - (27830000 as Weight) - .saturating_add((181000 as Weight).saturating_mul(c as Weight)) + (125_921_000 as Weight) + .saturating_add((6_860_000 as Weight).saturating_mul(c as Weight)) .saturating_add(DbWeight::get().reads(1 as Weight)) .saturating_add(DbWeight::get().writes(1 as Weight)) } fn renounce_candidacy_members() -> Weight { - (41689000 as Weight) + (389_571_000 as Weight) .saturating_add(DbWeight::get().reads(3 as Weight)) .saturating_add(DbWeight::get().writes(4 as Weight)) } fn renounce_candidacy_runners_up() -> Weight { - (27689000 as Weight) + (191_813_000 as Weight) .saturating_add(DbWeight::get().reads(1 as Weight)) .saturating_add(DbWeight::get().writes(1 as Weight)) } fn remove_member_with_replacement() -> Weight { - (41510000 as Weight) + (365_327_000 as Weight) .saturating_add(DbWeight::get().reads(4 as Weight)) .saturating_add(DbWeight::get().writes(5 as Weight)) } fn remove_member_wrong_refund() -> Weight { - (5159000 as Weight) + (20_302_000 as Weight) .saturating_add(DbWeight::get().reads(1 as Weight)) } } From 0b8a85e7f119d4927fd3822ce099332e6851b284 Mon Sep 17 00:00:00 2001 From: kianenigma Date: Mon, 21 Sep 2020 16:43:36 +0200 Subject: [PATCH 4/8] Update test --- frame/elections-phragmen/src/lib.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/frame/elections-phragmen/src/lib.rs b/frame/elections-phragmen/src/lib.rs index e0ec0491ee92c..252a2f39b1751 100644 --- a/frame/elections-phragmen/src/lib.rs +++ b/frame/elections-phragmen/src/lib.rs @@ -2408,7 +2408,7 @@ mod tests { assert_err_with_weight!( Elections::remove_member(Origin::root(), 4, true), Error::::InvalidReplacement, - Some(30159000), // only thing that matters for now is that it is NOT the full block. + Some(45302000), // only thing that matters for now is that it is NOT the full block. ); }); From 91d315b19c9446149f7d8a49ceec4a1c7bca40b2 Mon Sep 17 00:00:00 2001 From: kianenigma Date: Mon, 21 Sep 2020 17:52:18 +0200 Subject: [PATCH 5/8] Update another thest :" --- frame/elections-phragmen/src/lib.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/frame/elections-phragmen/src/lib.rs b/frame/elections-phragmen/src/lib.rs index 252a2f39b1751..ce9ba11143c18 100644 --- a/frame/elections-phragmen/src/lib.rs +++ b/frame/elections-phragmen/src/lib.rs @@ -2430,7 +2430,7 @@ mod tests { assert_err_with_weight!( Elections::remove_member(Origin::root(), 4, false), Error::::InvalidReplacement, - Some(30159000) // only thing that matters for now is that it is NOT the full block. + Some(45302000) // only thing that matters for now is that it is NOT the full block. ); }); } From 00674e8485279720deec316fda91791181974850 Mon Sep 17 00:00:00 2001 From: Shawn Tabrizi Date: Mon, 21 Sep 2020 23:30:41 +0200 Subject: [PATCH 6/8] Weights from benchmarking machine ./substrate2/target/release/substrate benchmark --chain dev --steps 50 --repeat 20 --pallet pallet_elections_phragmen --extrinsic "*" --raw --execution=wasm --wasm-execution=compiled --output --- .../elections-phragmen/src/default_weights.rs | 34 +++++++++---------- 1 file changed, 17 insertions(+), 17 deletions(-) diff --git a/frame/elections-phragmen/src/default_weights.rs b/frame/elections-phragmen/src/default_weights.rs index 5b192856bc84f..4025e61d15af4 100644 --- a/frame/elections-phragmen/src/default_weights.rs +++ b/frame/elections-phragmen/src/default_weights.rs @@ -24,65 +24,65 @@ use frame_support::weights::{Weight, constants::RocksDbWeight as DbWeight}; impl crate::WeightInfo for () { fn vote(v: u32, ) -> Weight { - (316_492_000 as Weight) - .saturating_add((440_000 as Weight).saturating_mul(v as Weight)) + (91_489_000 as Weight) + .saturating_add((199_000 as Weight).saturating_mul(v as Weight)) .saturating_add(DbWeight::get().reads(5 as Weight)) .saturating_add(DbWeight::get().writes(2 as Weight)) } fn vote_update(v: u32, ) -> Weight { - (180_930_000 as Weight) - .saturating_add((444_000 as Weight).saturating_mul(v as Weight)) + (56_511_000 as Weight) + .saturating_add((245_000 as Weight).saturating_mul(v as Weight)) .saturating_add(DbWeight::get().reads(5 as Weight)) .saturating_add(DbWeight::get().writes(2 as Weight)) } fn remove_voter() -> Weight { - (256_953_000 as Weight) + (76_714_000 as Weight) .saturating_add(DbWeight::get().reads(2 as Weight)) .saturating_add(DbWeight::get().writes(2 as Weight)) } fn report_defunct_voter_correct(c: u32, v: u32, ) -> Weight { (0 as Weight) - .saturating_add((91_785_000 as Weight).saturating_mul(c as Weight)) - .saturating_add((1_326_665_000 as Weight).saturating_mul(v as Weight)) + .saturating_add((1_743_000 as Weight).saturating_mul(c as Weight)) + .saturating_add((31_750_000 as Weight).saturating_mul(v as Weight)) .saturating_add(DbWeight::get().reads(7 as Weight)) .saturating_add(DbWeight::get().writes(3 as Weight)) } fn report_defunct_voter_incorrect(c: u32, v: u32, ) -> Weight { (0 as Weight) - .saturating_add((91_822_000 as Weight).saturating_mul(c as Weight)) - .saturating_add((1_327_023_000 as Weight).saturating_mul(v as Weight)) + .saturating_add((1_733_000 as Weight).saturating_mul(c as Weight)) + .saturating_add((31_861_000 as Weight).saturating_mul(v as Weight)) .saturating_add(DbWeight::get().reads(6 as Weight)) .saturating_add(DbWeight::get().writes(2 as Weight)) } fn submit_candidacy(c: u32, ) -> Weight { - (341_953_000 as Weight) - .saturating_add((13_693_000 as Weight).saturating_mul(c as Weight)) + (74_714_000 as Weight) + .saturating_add((315_000 as Weight).saturating_mul(c as Weight)) .saturating_add(DbWeight::get().reads(3 as Weight)) .saturating_add(DbWeight::get().writes(1 as Weight)) } fn renounce_candidacy_candidate(c: u32, ) -> Weight { - (125_921_000 as Weight) - .saturating_add((6_860_000 as Weight).saturating_mul(c as Weight)) + (50_408_000 as Weight) + .saturating_add((159_000 as Weight).saturating_mul(c as Weight)) .saturating_add(DbWeight::get().reads(1 as Weight)) .saturating_add(DbWeight::get().writes(1 as Weight)) } fn renounce_candidacy_members() -> Weight { - (389_571_000 as Weight) + (79_626_000 as Weight) .saturating_add(DbWeight::get().reads(3 as Weight)) .saturating_add(DbWeight::get().writes(4 as Weight)) } fn renounce_candidacy_runners_up() -> Weight { - (191_813_000 as Weight) + (49_715_000 as Weight) .saturating_add(DbWeight::get().reads(1 as Weight)) .saturating_add(DbWeight::get().writes(1 as Weight)) } fn remove_member_with_replacement() -> Weight { - (365_327_000 as Weight) + (76_572_000 as Weight) .saturating_add(DbWeight::get().reads(4 as Weight)) .saturating_add(DbWeight::get().writes(5 as Weight)) } fn remove_member_wrong_refund() -> Weight { - (20_302_000 as Weight) + (8_777_000 as Weight) .saturating_add(DbWeight::get().reads(1 as Weight)) } } From 862cb847ef0aae130ba0f58532339dae602625c3 Mon Sep 17 00:00:00 2001 From: Shawn Tabrizi Date: Mon, 21 Sep 2020 23:32:24 +0200 Subject: [PATCH 7/8] Update weights from the benchmarking machine --- .../src/weights/pallet_elections_phragmen.rs | 34 +++++++++---------- 1 file changed, 17 insertions(+), 17 deletions(-) diff --git a/bin/node/runtime/src/weights/pallet_elections_phragmen.rs b/bin/node/runtime/src/weights/pallet_elections_phragmen.rs index a6c9d42f7c95d..f7ce1620a1f56 100644 --- a/bin/node/runtime/src/weights/pallet_elections_phragmen.rs +++ b/bin/node/runtime/src/weights/pallet_elections_phragmen.rs @@ -25,65 +25,65 @@ use frame_support::weights::{Weight, constants::RocksDbWeight as DbWeight}; pub struct WeightInfo; impl pallet_elections_phragmen::WeightInfo for WeightInfo { fn vote(v: u32, ) -> Weight { - (316_492_000 as Weight) - .saturating_add((440_000 as Weight).saturating_mul(v as Weight)) + (91_489_000 as Weight) + .saturating_add((199_000 as Weight).saturating_mul(v as Weight)) .saturating_add(DbWeight::get().reads(5 as Weight)) .saturating_add(DbWeight::get().writes(2 as Weight)) } fn vote_update(v: u32, ) -> Weight { - (180_930_000 as Weight) - .saturating_add((444_000 as Weight).saturating_mul(v as Weight)) + (56_511_000 as Weight) + .saturating_add((245_000 as Weight).saturating_mul(v as Weight)) .saturating_add(DbWeight::get().reads(5 as Weight)) .saturating_add(DbWeight::get().writes(2 as Weight)) } fn remove_voter() -> Weight { - (256_953_000 as Weight) + (76_714_000 as Weight) .saturating_add(DbWeight::get().reads(2 as Weight)) .saturating_add(DbWeight::get().writes(2 as Weight)) } fn report_defunct_voter_correct(c: u32, v: u32, ) -> Weight { (0 as Weight) - .saturating_add((91_785_000 as Weight).saturating_mul(c as Weight)) - .saturating_add((1_326_665_000 as Weight).saturating_mul(v as Weight)) + .saturating_add((1_743_000 as Weight).saturating_mul(c as Weight)) + .saturating_add((31_750_000 as Weight).saturating_mul(v as Weight)) .saturating_add(DbWeight::get().reads(7 as Weight)) .saturating_add(DbWeight::get().writes(3 as Weight)) } fn report_defunct_voter_incorrect(c: u32, v: u32, ) -> Weight { (0 as Weight) - .saturating_add((91_822_000 as Weight).saturating_mul(c as Weight)) - .saturating_add((1_327_023_000 as Weight).saturating_mul(v as Weight)) + .saturating_add((1_733_000 as Weight).saturating_mul(c as Weight)) + .saturating_add((31_861_000 as Weight).saturating_mul(v as Weight)) .saturating_add(DbWeight::get().reads(6 as Weight)) .saturating_add(DbWeight::get().writes(2 as Weight)) } fn submit_candidacy(c: u32, ) -> Weight { - (341_953_000 as Weight) - .saturating_add((13_693_000 as Weight).saturating_mul(c as Weight)) + (74_714_000 as Weight) + .saturating_add((315_000 as Weight).saturating_mul(c as Weight)) .saturating_add(DbWeight::get().reads(3 as Weight)) .saturating_add(DbWeight::get().writes(1 as Weight)) } fn renounce_candidacy_candidate(c: u32, ) -> Weight { - (125_921_000 as Weight) - .saturating_add((6_860_000 as Weight).saturating_mul(c as Weight)) + (50_408_000 as Weight) + .saturating_add((159_000 as Weight).saturating_mul(c as Weight)) .saturating_add(DbWeight::get().reads(1 as Weight)) .saturating_add(DbWeight::get().writes(1 as Weight)) } fn renounce_candidacy_members() -> Weight { - (389_571_000 as Weight) + (79_626_000 as Weight) .saturating_add(DbWeight::get().reads(3 as Weight)) .saturating_add(DbWeight::get().writes(4 as Weight)) } fn renounce_candidacy_runners_up() -> Weight { - (191_813_000 as Weight) + (49_715_000 as Weight) .saturating_add(DbWeight::get().reads(1 as Weight)) .saturating_add(DbWeight::get().writes(1 as Weight)) } fn remove_member_with_replacement() -> Weight { - (365_327_000 as Weight) + (76_572_000 as Weight) .saturating_add(DbWeight::get().reads(4 as Weight)) .saturating_add(DbWeight::get().writes(5 as Weight)) } fn remove_member_wrong_refund() -> Weight { - (20_302_000 as Weight) + (8_777_000 as Weight) .saturating_add(DbWeight::get().reads(1 as Weight)) } } From abd7db2027f3766a1de74630ae7a186e79d747e0 Mon Sep 17 00:00:00 2001 From: kianenigma Date: Tue, 22 Sep 2020 13:24:06 +0200 Subject: [PATCH 8/8] Fix tests one last time --- frame/elections-phragmen/src/lib.rs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/frame/elections-phragmen/src/lib.rs b/frame/elections-phragmen/src/lib.rs index ce9ba11143c18..358a81f3a57a3 100644 --- a/frame/elections-phragmen/src/lib.rs +++ b/frame/elections-phragmen/src/lib.rs @@ -2408,7 +2408,7 @@ mod tests { assert_err_with_weight!( Elections::remove_member(Origin::root(), 4, true), Error::::InvalidReplacement, - Some(45302000), // only thing that matters for now is that it is NOT the full block. + Some(33777000), // only thing that matters for now is that it is NOT the full block. ); }); @@ -2430,7 +2430,7 @@ mod tests { assert_err_with_weight!( Elections::remove_member(Origin::root(), 4, false), Error::::InvalidReplacement, - Some(45302000) // only thing that matters for now is that it is NOT the full block. + Some(33777000) // only thing that matters for now is that it is NOT the full block. ); }); }