From 8ae474785b93809c93bc8364721c7346cc44dc98 Mon Sep 17 00:00:00 2001 From: kianenigma Date: Wed, 2 Sep 2020 11:14:03 +0200 Subject: [PATCH 01/19] Fix the benchmarks --- frame/staking/src/benchmarking.rs | 18 ++++++++++++++---- frame/staking/src/lib.rs | 3 ++- 2 files changed, 16 insertions(+), 5 deletions(-) diff --git a/frame/staking/src/benchmarking.rs b/frame/staking/src/benchmarking.rs index 156b2f81c8429..c528ed32b1365 100644 --- a/frame/staking/src/benchmarking.rs +++ b/frame/staking/src/benchmarking.rs @@ -280,13 +280,18 @@ benchmarks! { let validator = create_validator_with_nominators::(n, T::MaxNominatorRewardedPerValidator::get() as u32, true)?; let current_era = CurrentEra::get().unwrap(); + // set the commission for this particular era as well. + >::insert(current_era, validator.clone(), >::validators(&validator)); + let caller = whitelisted_caller(); let balance_before = T::Currency::free_balance(&validator); }: _(RawOrigin::Signed(caller), validator.clone(), current_era) verify { - // Validator has been paid! let balance_after = T::Currency::free_balance(&validator); - assert!(balance_before < balance_after); + assert!( + balance_before < balance_after, + "Balance of stash {:?} should have increased after payout.", validator, + ); } payout_stakers_alive_controller { @@ -294,13 +299,18 @@ benchmarks! { let validator = create_validator_with_nominators::(n, T::MaxNominatorRewardedPerValidator::get() as u32, false)?; let current_era = CurrentEra::get().unwrap(); + // set the commission for this particular era as well. + >::insert(current_era, validator.clone(), >::validators(&validator)); + let caller = whitelisted_caller(); let balance_before = T::Currency::free_balance(&validator); }: payout_stakers(RawOrigin::Signed(caller), validator.clone(), current_era) verify { - // Validator has been paid! let balance_after = T::Currency::free_balance(&validator); - assert!(balance_before < balance_after); + assert!( + balance_before < balance_after, + "Balance of stash {:?} should have increased after payout.", validator, + ); } rebond { diff --git a/frame/staking/src/lib.rs b/frame/staking/src/lib.rs index a15b7ac5d7248..8b9a79c32a671 100644 --- a/frame/staking/src/lib.rs +++ b/frame/staking/src/lib.rs @@ -2175,7 +2175,8 @@ decl_module! { /// - Writes: Bonded, Slashing Spans (if S > 0), Ledger, Payee, Validators, Nominators, Stash Account, Locks /// - Writes Each: SpanSlash * S /// # - #[weight = T::DbWeight::get().reads_writes(4, 7) + #[weight = + T::DbWeight::get().reads_writes(4, 7) .saturating_add(76 * WEIGHT_PER_MICROS) .saturating_add( WEIGHT_PER_MICROS.saturating_mul(2).saturating_mul(Weight::from(*num_slashing_spans)) From 31d69bce6641924be50a05f55a0c727e1345ef59 Mon Sep 17 00:00:00 2001 From: kianenigma Date: Wed, 2 Sep 2020 14:24:28 +0200 Subject: [PATCH 02/19] Migrate staking to weightInfo --- frame/staking/src/benchmarking.rs | 32 ++-- frame/staking/src/default_weights.rs | 249 +++++++++++++++++++++++++++ frame/staking/src/lib.rs | 206 ++++++++-------------- 3 files changed, 335 insertions(+), 152 deletions(-) create mode 100644 frame/staking/src/default_weights.rs diff --git a/frame/staking/src/benchmarking.rs b/frame/staking/src/benchmarking.rs index c528ed32b1365..68ed64e30c7b6 100644 --- a/frame/staking/src/benchmarking.rs +++ b/frame/staking/src/benchmarking.rs @@ -441,13 +441,13 @@ benchmarks! { // It builds a solution with `w` winners composed of nominated validators randomly nominated, // `a` assignment with MAX_NOMINATIONS. submit_solution_initial { - // number of validator intent - let v in 1000 .. 2000; - // number of nominator intent - let n in 1000 .. 2000; + // number of validator intention. + let v in 200 .. 400; + // number of nominator intention. + let n in 500 .. 1000; // number of assignments. Basically, number of active nominators. - let a in 200 .. 500; - // number of winners, also ValidatorCount + let a in 200 .. 400; + // number of winners, also ValidatorCount. let w in 16 .. 100; ensure!(w as usize >= MAX_NOMINATIONS, "doesn't support lower value"); @@ -503,13 +503,13 @@ benchmarks! { // same as submit_solution_initial but we place a very weak solution on chian first. submit_solution_better { - // number of validator intent - let v in 1000 .. 2000; - // number of nominator intent - let n in 1000 .. 2000; + // number of validator intention. + let v in 200 .. 400; + // number of nominator intention. + let n in 500 .. 1000; // number of assignments. Basically, number of active nominators. - let a in 200 .. 500; - // number of winners, also ValidatorCount + let a in 200 .. 400; + // number of winners, also ValidatorCount. let w in 16 .. 100; ensure!(w as usize >= MAX_NOMINATIONS, "doesn't support lower value"); @@ -587,10 +587,10 @@ benchmarks! { // This will be early rejected based on the score. submit_solution_weaker { - // number of validator intent - let v in 1000 .. 2000; - // number of nominator intent - let n in 1000 .. 2000; + // number of validator intention. + let v in 200 .. 400; + // number of nominator intention. + let n in 500 .. 1000; create_validators_with_nominators_for_era::(v, n, MAX_NOMINATIONS, false, None)?; diff --git a/frame/staking/src/default_weights.rs b/frame/staking/src/default_weights.rs new file mode 100644 index 0000000000000..9afae2edafb0a --- /dev/null +++ b/frame/staking/src/default_weights.rs @@ -0,0 +1,249 @@ + +// This file is part of Substrate. + +// Copyright (C) 2017-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. + +//! Default weights of pallet-staking. +//! 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 crate::WeightInfo for WeightInfo { + // WARNING! Some components were not used: ["u"] + fn bond() -> Weight { + (65788000 as Weight) + .saturating_add(DbWeight::get().reads(6 as Weight)) + .saturating_add(DbWeight::get().writes(5 as Weight)) + } + // WARNING! Some components were not used: ["u"] + fn bond_extra() -> Weight { + (53598000 as Weight) + .saturating_add(DbWeight::get().reads(5 as Weight)) + .saturating_add(DbWeight::get().writes(3 as Weight)) + } + // WARNING! Some components were not used: ["u"] + fn unbond() -> Weight { + (48977000 as Weight) + .saturating_add(DbWeight::get().reads(5 as Weight)) + .saturating_add(DbWeight::get().writes(3 as Weight)) + } + fn withdraw_unbonded_update(s: u32, ) -> Weight { + (49008000 as Weight) + .saturating_add((30000 as Weight).saturating_mul(s as Weight)) + .saturating_add(DbWeight::get().reads(5 as Weight)) + .saturating_add(DbWeight::get().writes(3 as Weight)) + } + fn withdraw_unbonded_kill(s: u32, ) -> Weight { + (69924000 as Weight) + .saturating_add((1554000 as Weight).saturating_mul(s as Weight)) + .saturating_add(DbWeight::get().reads(7 as Weight)) + .saturating_add(DbWeight::get().writes(8 as Weight)) + .saturating_add(DbWeight::get().writes((1 as Weight).saturating_mul(s as Weight))) + } + // WARNING! Some components were not used: ["u"] + fn validate() -> Weight { + (15848000 as Weight) + .saturating_add(DbWeight::get().reads(2 as Weight)) + .saturating_add(DbWeight::get().writes(2 as Weight)) + } + fn nominate(n: u32, ) -> Weight { + (20625000 as Weight) + .saturating_add((357000 as Weight).saturating_mul(n as Weight)) + .saturating_add(DbWeight::get().reads(3 as Weight)) + .saturating_add(DbWeight::get().writes(2 as Weight)) + } + // WARNING! Some components were not used: ["u"] + fn chill() -> Weight { + (17159000 as Weight) + .saturating_add(DbWeight::get().reads(2 as Weight)) + .saturating_add(DbWeight::get().writes(2 as Weight)) + } + // WARNING! Some components were not used: ["u"] + fn set_payee() -> Weight { + (10970000 as Weight) + .saturating_add(DbWeight::get().reads(1 as Weight)) + .saturating_add(DbWeight::get().writes(1 as Weight)) + } + // WARNING! Some components were not used: ["u"] + fn set_controller() -> Weight { + (23826000 as Weight) + .saturating_add(DbWeight::get().reads(3 as Weight)) + .saturating_add(DbWeight::get().writes(3 as Weight)) + } + // WARNING! Some components were not used: ["c"] + fn set_validator_count() -> Weight { + (1962000 as Weight) + .saturating_add(DbWeight::get().writes(1 as Weight)) + } + // WARNING! Some components were not used: ["i"] + fn force_no_eras() -> Weight { + (2000000 as Weight) + .saturating_add(DbWeight::get().writes(1 as Weight)) + } + // WARNING! Some components were not used: ["i"] + fn force_new_era() -> Weight { + (2000000 as Weight) + .saturating_add(DbWeight::get().writes(1 as Weight)) + } + // WARNING! Some components were not used: ["i"] + fn force_new_era_always() -> Weight { + (2000000 as Weight) + .saturating_add(DbWeight::get().writes(1 as Weight)) + } + fn set_invulnerables(v: u32, ) -> Weight { + (1955000 as Weight) + .saturating_add((4000 as Weight).saturating_mul(v as Weight)) + .saturating_add(DbWeight::get().writes(1 as Weight)) + } + fn force_unstake(s: u32, ) -> Weight { + (38402000 as Weight) + .saturating_add((1711000 as Weight).saturating_mul(s as Weight)) + .saturating_add(DbWeight::get().reads(4 as Weight)) + .saturating_add(DbWeight::get().writes(8 as Weight)) + .saturating_add(DbWeight::get().writes((1 as Weight).saturating_mul(s as Weight))) + } + fn cancel_deferred_slash(s: u32, ) -> Weight { + (410638000 as Weight) + .saturating_add((710000 as Weight).saturating_mul(s as Weight)) + .saturating_add(DbWeight::get().reads(1 as Weight)) + .saturating_add(DbWeight::get().writes(1 as Weight)) + } + fn payout_stakers(n: u32, ) -> Weight { + (31790000 as Weight) + .saturating_add((40680000 as Weight).saturating_mul(n as Weight)) + .saturating_add(DbWeight::get().reads(5 as Weight)) + .saturating_add(DbWeight::get().reads((3 as Weight).saturating_mul(n as Weight))) + .saturating_add(DbWeight::get().writes(1 as Weight)) + .saturating_add(DbWeight::get().writes((1 as Weight).saturating_mul(n as Weight))) + } + fn payout_stakers_alive_controller(n: u32, ) -> Weight { + (0 as Weight) + .saturating_add((55362000 as Weight).saturating_mul(n as Weight)) + .saturating_add(DbWeight::get().reads(1 as Weight)) + .saturating_add(DbWeight::get().reads((5 as Weight).saturating_mul(n as Weight))) + .saturating_add(DbWeight::get().writes((3 as Weight).saturating_mul(n as Weight))) + } + fn rebond(l: u32, ) -> Weight { + (35025000 as Weight) + .saturating_add((28000 as Weight).saturating_mul(l as Weight)) + .saturating_add(DbWeight::get().reads(4 as Weight)) + .saturating_add(DbWeight::get().writes(3 as Weight)) + } + fn set_history_depth(e: u32, ) -> Weight { + (0 as Weight) + .saturating_add((43889000 as Weight).saturating_mul(e as Weight)) + .saturating_add(DbWeight::get().reads(2 as Weight)) + .saturating_add(DbWeight::get().writes(4 as Weight)) + .saturating_add(DbWeight::get().writes((7 as Weight).saturating_mul(e as Weight))) + } + fn reap_stash(s: u32, ) -> Weight { + (69698000 as Weight) + .saturating_add((1680000 as Weight).saturating_mul(s as Weight)) + .saturating_add(DbWeight::get().reads(4 as Weight)) + .saturating_add(DbWeight::get().writes(8 as Weight)) + .saturating_add(DbWeight::get().writes((1 as Weight).saturating_mul(s as Weight))) + } + fn new_era(v: u32, n: u32, ) -> Weight { + (0 as Weight) + .saturating_add((462131000 as Weight).saturating_mul(v as Weight)) + .saturating_add((75480000 as Weight).saturating_mul(n as Weight)) + .saturating_add(DbWeight::get().reads(10 as Weight)) + .saturating_add(DbWeight::get().reads((4 as Weight).saturating_mul(v as Weight))) + .saturating_add(DbWeight::get().reads((3 as Weight).saturating_mul(n as Weight))) + .saturating_add(DbWeight::get().writes(8 as Weight)) + .saturating_add(DbWeight::get().writes((3 as Weight).saturating_mul(v as Weight))) + } + // WARNING! Some components were not used: ["l"] + fn do_slash() -> Weight { + (47783000 as Weight) + .saturating_add(DbWeight::get().reads(3 as Weight)) + .saturating_add(DbWeight::get().writes(2 as Weight)) + } + fn payout_all(v: u32, n: u32, ) -> Weight { + (0 as Weight) + .saturating_add((1841741000 as Weight).saturating_mul(v as Weight)) + .saturating_add((181891000 as Weight).saturating_mul(n as Weight)) + .saturating_add(DbWeight::get().reads((15 as Weight).saturating_mul(v as Weight))) + .saturating_add(DbWeight::get().reads((5 as Weight).saturating_mul(n as Weight))) + .saturating_add(DbWeight::get().writes((8 as Weight).saturating_mul(v as Weight))) + .saturating_add(DbWeight::get().writes((3 as Weight).saturating_mul(n as Weight))) + } + fn submit_solution_initial(v: u32, n: u32, a: u32, w: u32, ) -> Weight { + (0 as Weight) + .saturating_add((762000 as Weight).saturating_mul(v as Weight)) + .saturating_add((868000 as Weight).saturating_mul(n as Weight)) + .saturating_add((66802000 as Weight).saturating_mul(a as Weight)) + .saturating_add((6474000 as Weight).saturating_mul(w as Weight)) + .saturating_add(DbWeight::get().reads(6 as Weight)) + .saturating_add(DbWeight::get().reads((4 as Weight).saturating_mul(a as Weight))) + .saturating_add(DbWeight::get().reads((1 as Weight).saturating_mul(w as Weight))) + .saturating_add(DbWeight::get().writes(2 as Weight)) + } + fn submit_solution_better(v: u32, n: u32, a: u32, w: u32, ) -> Weight { + (0 as Weight) + .saturating_add((784000 as Weight).saturating_mul(v as Weight)) + .saturating_add((640000 as Weight).saturating_mul(n as Weight)) + .saturating_add((68830000 as Weight).saturating_mul(a as Weight)) + .saturating_add((8181000 as Weight).saturating_mul(w as Weight)) + .saturating_add(DbWeight::get().reads(6 as Weight)) + .saturating_add(DbWeight::get().reads((4 as Weight).saturating_mul(a as Weight))) + .saturating_add(DbWeight::get().reads((1 as Weight).saturating_mul(w as Weight))) + .saturating_add(DbWeight::get().writes(2 as Weight)) + } + fn submit_solution_weaker(v: u32, n: u32, ) -> Weight { + (11244000 as Weight) + .saturating_add((28000 as Weight).saturating_mul(v as Weight)) + .saturating_add((12000 as Weight).saturating_mul(n as Weight)) + .saturating_add(DbWeight::get().reads(3 as Weight)) + } +} + + +#[cfg(feature = "std")] +impl crate::WeightInfo for () { + fn bond() -> Weight { 1_000_000_000 } + fn bond_extra() -> Weight { 1_000_000_000 } + fn unbond() -> Weight { 1_000_000_000 } + fn withdraw_unbonded_update(_s: u32, ) -> Weight { 1_000_000_000 } + fn withdraw_unbonded_kill(_s: u32, ) -> Weight { 1_000_000_000 } + fn validate() -> Weight { 1_000_000_000 } + fn nominate(n: u32) -> Weight { 1_000_000_000 } + fn chill() -> Weight { 1_000_000_000 } + fn set_payee() -> Weight { 1_000_000_000 } + fn set_controller() -> Weight { 1_000_000_000 } + fn set_validator_count() -> Weight { 1_000_000_000 } + fn force_no_eras() -> Weight { 1_000_000_000 } + fn force_new_era() -> Weight { 1_000_000_000 } + fn force_new_era_always() -> Weight { 1_000_000_000 } + fn set_invulnerables(_v: u32, ) -> Weight { 1_000_000_000 } + fn force_unstake(_s: u32, ) -> Weight { 1_000_000_000 } + fn cancel_deferred_slash(_s: u32, ) -> Weight { 1_000_000_000 } + fn payout_stakers(_n: u32, ) -> Weight { 1_000_000_000 } + fn payout_stakers_alive_controller(_n: u32, ) -> Weight { 1_000_000_000 } + fn rebond(_l: u32, ) -> Weight { 1_000_000_000 } + fn set_history_depth(_e: u32, ) -> Weight { 1_000_000_000 } + fn reap_stash(_s: u32, ) -> Weight { 1_000_000_000 } + fn new_era(_v: u32, _n: u32, ) -> Weight { 1_000_000_000 } + fn do_slash() -> Weight { 1_000_000_000 } + fn payout_all(_v: u32, _n: u32, ) -> Weight { 1_000_000_000 } + fn submit_solution_initial(_v: u32, _n: u32, _a: u32, _w: u32, ) -> Weight { 1_000_000_000 } + fn submit_solution_better(_v: u32, _n: u32, _a: u32, _w: u32, ) -> Weight { 1_000_000_000 } + fn submit_solution_weaker(_v: u32, _n: u32, ) -> Weight { 1_000_000_000 } +} diff --git a/frame/staking/src/lib.rs b/frame/staking/src/lib.rs index 8b9a79c32a671..abd9dfbee9d6d 100644 --- a/frame/staking/src/lib.rs +++ b/frame/staking/src/lib.rs @@ -279,6 +279,7 @@ pub mod benchmarking; pub mod slashing; pub mod offchain_election; pub mod inflation; +pub mod default_weights; use sp_std::{ result, @@ -356,6 +357,8 @@ pub type ValidatorIndex = u16; // Ensure the size of both ValidatorIndex and NominatorIndex. They both need to be well below usize. static_assertions::const_assert!(size_of::() <= size_of::()); static_assertions::const_assert!(size_of::() <= size_of::()); +static_assertions::const_assert!(size_of::() <= size_of::()); +static_assertions::const_assert!(size_of::() <= size_of::()); /// Maximum number of stakers that can be stored in a snapshot. pub(crate) const MAX_VALIDATORS: usize = ValidatorIndex::max_value() as usize; @@ -766,7 +769,7 @@ impl SessionInterface<::AccountId> for T whe } } -pub mod weight { +mod weight { use super::*; /// All weight notes are pertaining to the case of a better solution, in which we execute @@ -833,20 +836,20 @@ pub mod weight { } pub trait WeightInfo { - fn bond(u: u32, ) -> Weight; - fn bond_extra(u: u32, ) -> Weight; - fn unbond(u: u32, ) -> Weight; + fn bond() -> Weight; + fn bond_extra() -> Weight; + fn unbond() -> Weight; fn withdraw_unbonded_update(s: u32, ) -> Weight; fn withdraw_unbonded_kill(s: u32, ) -> Weight; - fn validate(u: u32, ) -> Weight; + fn validate() -> Weight; fn nominate(n: u32, ) -> Weight; - fn chill(u: u32, ) -> Weight; - fn set_payee(u: u32, ) -> Weight; - fn set_controller(u: u32, ) -> Weight; - fn set_validator_count(c: u32, ) -> Weight; - fn force_no_eras(i: u32, ) -> Weight; - fn force_new_era(i: u32, ) -> Weight; - fn force_new_era_always(i: u32, ) -> Weight; + fn chill() -> Weight; + fn set_payee() -> Weight; + fn set_controller() -> Weight; + fn set_validator_count() -> Weight; + fn force_no_eras() -> Weight; + fn force_new_era() -> Weight; + fn force_new_era_always() -> Weight; fn set_invulnerables(v: u32, ) -> Weight; fn force_unstake(s: u32, ) -> Weight; fn cancel_deferred_slash(s: u32, ) -> Weight; @@ -856,44 +859,13 @@ pub trait WeightInfo { fn set_history_depth(e: u32, ) -> Weight; fn reap_stash(s: u32, ) -> Weight; fn new_era(v: u32, n: u32, ) -> Weight; - fn do_slash(l: u32, ) -> Weight; + fn do_slash() -> Weight; fn payout_all(v: u32, n: u32, ) -> Weight; fn submit_solution_initial(v: u32, n: u32, a: u32, w: u32, ) -> Weight; fn submit_solution_better(v: u32, n: u32, a: u32, w: u32, ) -> Weight; fn submit_solution_weaker(v: u32, n: u32, ) -> Weight; } -impl WeightInfo for () { - fn bond(_u: u32, ) -> Weight { 1_000_000_000 } - fn bond_extra(_u: u32, ) -> Weight { 1_000_000_000 } - fn unbond(_u: u32, ) -> Weight { 1_000_000_000 } - fn withdraw_unbonded_update(_s: u32, ) -> Weight { 1_000_000_000 } - fn withdraw_unbonded_kill(_s: u32, ) -> Weight { 1_000_000_000 } - fn validate(_u: u32, ) -> Weight { 1_000_000_000 } - fn nominate(_n: u32, ) -> Weight { 1_000_000_000 } - fn chill(_u: u32, ) -> Weight { 1_000_000_000 } - fn set_payee(_u: u32, ) -> Weight { 1_000_000_000 } - fn set_controller(_u: u32, ) -> Weight { 1_000_000_000 } - fn set_validator_count(_c: u32, ) -> Weight { 1_000_000_000 } - fn force_no_eras(_i: u32, ) -> Weight { 1_000_000_000 } - fn force_new_era(_i: u32, ) -> Weight { 1_000_000_000 } - fn force_new_era_always(_i: u32, ) -> Weight { 1_000_000_000 } - fn set_invulnerables(_v: u32, ) -> Weight { 1_000_000_000 } - fn force_unstake(_s: u32, ) -> Weight { 1_000_000_000 } - fn cancel_deferred_slash(_s: u32, ) -> Weight { 1_000_000_000 } - fn payout_stakers(_n: u32, ) -> Weight { 1_000_000_000 } - fn payout_stakers_alive_controller(_n: u32, ) -> Weight { 1_000_000_000 } - fn rebond(_l: u32, ) -> Weight { 1_000_000_000 } - fn set_history_depth(_e: u32, ) -> Weight { 1_000_000_000 } - fn reap_stash(_s: u32, ) -> Weight { 1_000_000_000 } - fn new_era(_v: u32, _n: u32, ) -> Weight { 1_000_000_000 } - fn do_slash(_l: u32, ) -> Weight { 1_000_000_000 } - fn payout_all(_v: u32, _n: u32, ) -> Weight { 1_000_000_000 } - fn submit_solution_initial(_v: u32, _n: u32, _a: u32, _w: u32, ) -> Weight { 1_000_000_000 } - fn submit_solution_better(_v: u32, _n: u32, _a: u32, _w: u32, ) -> Weight { 1_000_000_000 } - fn submit_solution_weaker(_v: u32, _n: u32, ) -> Weight { 1_000_000_000 } -} - pub trait Trait: frame_system::Trait + SendTransactionTypes> { /// The staking balance. type Currency: LockableCurrency; @@ -1494,7 +1466,7 @@ decl_module! { /// - Read: Bonded, Ledger, [Origin Account], Current Era, History Depth, Locks /// - Write: Bonded, Payee, [Origin Account], Locks, Ledger /// # - #[weight = 67 * WEIGHT_PER_MICROS + T::DbWeight::get().reads_writes(5, 4)] + #[weight = T::WeightInfo::bond()] pub fn bond(origin, controller: ::Source, #[compact] value: BalanceOf, @@ -1563,7 +1535,7 @@ decl_module! { /// - Read: Era Election Status, Bonded, Ledger, [Origin Account], Locks /// - Write: [Origin Account], Locks, Ledger /// # - #[weight = 55 * WEIGHT_PER_MICROS + T::DbWeight::get().reads_writes(4, 2)] + #[weight = T::WeightInfo::bond_extra()] fn bond_extra(origin, #[compact] max_additional: BalanceOf) { ensure!(Self::era_election_status().is_closed(), Error::::CallNotAllowed); let stash = ensure_signed(origin)?; @@ -1614,7 +1586,7 @@ decl_module! { /// - Read: Era Election Status, Ledger, Current Era, Locks, [Origin Account] /// - Write: [Origin Account], Locks, Ledger /// - #[weight = 50 * WEIGHT_PER_MICROS + T::DbWeight::get().reads_writes(4, 2)] + #[weight = T::WeightInfo::unbond()] fn unbond(origin, #[compact] value: BalanceOf) { ensure!(Self::era_election_status().is_closed(), Error::::CallNotAllowed); let controller = ensure_signed(origin)?; @@ -1673,15 +1645,7 @@ decl_module! { /// - Writes Each: SpanSlash * S /// NOTE: Weight annotation is the kill scenario, we refund otherwise. /// # - #[weight = T::DbWeight::get().reads_writes(6, 6) - .saturating_add(80 * WEIGHT_PER_MICROS) - .saturating_add( - (2 * WEIGHT_PER_MICROS).saturating_mul(Weight::from(*num_slashing_spans)) - ) - .saturating_add(T::DbWeight::get().writes(Weight::from(*num_slashing_spans))) - // if slashing spans is non-zero, add 1 more write - .saturating_add(T::DbWeight::get().writes(Weight::from(*num_slashing_spans).min(1))) - ] + #[weight = T::WeightInfo::withdraw_unbonded_kill(*num_slashing_spans)] fn withdraw_unbonded(origin, num_slashing_spans: u32) -> DispatchResultWithPostInfo { ensure!(Self::era_election_status().is_closed(), Error::::CallNotAllowed); let controller = ensure_signed(origin)?; @@ -1703,8 +1667,14 @@ decl_module! { } else { // This was the consequence of a partial unbond. just update the ledger and move on. Self::update_ledger(&controller, &ledger); - // This is only an update, so we use less overall weight - Some(50 * WEIGHT_PER_MICROS + T::DbWeight::get().reads_writes(4, 2)) + + // This is only an update, so we use less overall weight. + // Note: we are re-calculating weights here which is not performant per se but it is + // accurate and maintainable. The calculation themselve are trivial and O(1). + Some( + T::WeightInfo::withdraw_unbonded_kill(num_slashing_spans) + .saturating_sub(T::WeightInfo::withdraw_unbonded_update(num_slashing_spans)) + ) }; // `old_total` should never be less than the new total because @@ -1735,7 +1705,7 @@ decl_module! { /// - Read: Era Election Status, Ledger /// - Write: Nominators, Validators /// # - #[weight = 17 * WEIGHT_PER_MICROS + T::DbWeight::get().reads_writes(2, 2)] + #[weight = T::WeightInfo::validate()] pub fn validate(origin, prefs: ValidatorPrefs) { ensure!(Self::era_election_status().is_closed(), Error::::CallNotAllowed); let controller = ensure_signed(origin)?; @@ -1764,10 +1734,7 @@ decl_module! { /// - Reads: Era Election Status, Ledger, Current Era /// - Writes: Validators, Nominators /// # - #[weight = T::DbWeight::get().reads_writes(3, 2) - .saturating_add(22 * WEIGHT_PER_MICROS) - .saturating_add((360 * WEIGHT_PER_NANOS).saturating_mul(targets.len() as Weight)) - ] + #[weight = T::WeightInfo::nominate(targets.len() as u32)] pub fn nominate(origin, targets: Vec<::Source>) { ensure!(Self::era_election_status().is_closed(), Error::::CallNotAllowed); let controller = ensure_signed(origin)?; @@ -1807,7 +1774,7 @@ decl_module! { /// - Read: EraElectionStatus, Ledger /// - Write: Validators, Nominators /// # - #[weight = 16 * WEIGHT_PER_MICROS + T::DbWeight::get().reads_writes(2, 2)] + #[weight = T::WeightInfo::chill()] fn chill(origin) { ensure!(Self::era_election_status().is_closed(), Error::::CallNotAllowed); let controller = ensure_signed(origin)?; @@ -1831,7 +1798,7 @@ decl_module! { /// - Read: Ledger /// - Write: Payee /// # - #[weight = 11 * WEIGHT_PER_MICROS + T::DbWeight::get().reads_writes(1, 1)] + #[weight = T::WeightInfo::set_payee()] fn set_payee(origin, payee: RewardDestination) { let controller = ensure_signed(origin)?; let ledger = Self::ledger(&controller).ok_or(Error::::NotController)?; @@ -1855,7 +1822,7 @@ decl_module! { /// - Read: Bonded, Ledger New Controller, Ledger Old Controller /// - Write: Bonded, Ledger New Controller, Ledger Old Controller /// # - #[weight = 25 * WEIGHT_PER_MICROS + T::DbWeight::get().reads_writes(3, 3)] + #[weight = T::WeightInfo::set_controller()] fn set_controller(origin, controller: ::Source) { let stash = ensure_signed(origin)?; let old_controller = Self::bonded(&stash).ok_or(Error::::NotStash)?; @@ -1879,7 +1846,7 @@ decl_module! { /// Base Weight: 1.717 µs /// Write: Validator Count /// # - #[weight = 2 * WEIGHT_PER_MICROS + T::DbWeight::get().writes(1)] + #[weight = T::WeightInfo::set_validator_count()] fn set_validator_count(origin, #[compact] new: u32) { ensure_root(origin)?; ValidatorCount::put(new); @@ -1890,10 +1857,9 @@ decl_module! { /// The dispatch origin must be Root. /// /// # - /// Base Weight: 1.717 µs - /// Read/Write: Validator Count + /// Same as [`set_validator_count`]. /// # - #[weight = 2 * WEIGHT_PER_MICROS + T::DbWeight::get().reads_writes(1, 1)] + #[weight = T::WeightInfo::set_validator_count()] fn increase_validator_count(origin, #[compact] additional: u32) { ensure_root(origin)?; ValidatorCount::mutate(|n| *n += additional); @@ -1904,10 +1870,9 @@ decl_module! { /// The dispatch origin must be Root. /// /// # - /// Base Weight: 1.717 µs - /// Read/Write: Validator Count + /// Same as [`set_validator_count`]. /// # - #[weight = 2 * WEIGHT_PER_MICROS + T::DbWeight::get().reads_writes(1, 1)] + #[weight = T::WeightInfo::set_validator_count()] fn scale_validator_count(origin, factor: Percent) { ensure_root(origin)?; ValidatorCount::mutate(|n| *n += factor * *n); @@ -1922,7 +1887,7 @@ decl_module! { /// - Base Weight: 1.857 µs /// - Write: ForceEra /// # - #[weight = 2 * WEIGHT_PER_MICROS + T::DbWeight::get().writes(1)] + #[weight = T::WeightInfo::force_no_eras()] fn force_no_eras(origin) { ensure_root(origin)?; ForceEra::put(Forcing::ForceNone); @@ -1938,7 +1903,7 @@ decl_module! { /// - Base Weight: 1.959 µs /// - Write ForceEra /// # - #[weight = 2 * WEIGHT_PER_MICROS + T::DbWeight::get().writes(1)] + #[weight = T::WeightInfo::force_new_era()] fn force_new_era(origin) { ensure_root(origin)?; ForceEra::put(Forcing::ForceNew); @@ -1953,13 +1918,10 @@ decl_module! { /// - Base Weight: 2.208 + .006 * V µs /// - Write: Invulnerables /// # - #[weight = T::DbWeight::get().writes(1) - .saturating_add(2 * WEIGHT_PER_MICROS) - .saturating_add((6 * WEIGHT_PER_NANOS).saturating_mul(validators.len() as Weight)) - ] - fn set_invulnerables(origin, validators: Vec) { + #[weight = T::WeightInfo::set_invulnerables(invulnerables.len() as u32)] + fn set_invulnerables(origin, invulnerables: Vec) { ensure_root(origin)?; - >::put(validators); + >::put(invulnerables); } /// Force a current staker to become completely unstaked, immediately. @@ -1973,15 +1935,7 @@ decl_module! { /// Writes: Bonded, Slashing Spans (if S > 0), Ledger, Payee, Validators, Nominators, Account, Locks /// Writes Each: SpanSlash * S /// # - #[weight = T::DbWeight::get().reads_writes(4, 7) - .saturating_add(53 * WEIGHT_PER_MICROS) - .saturating_add( - WEIGHT_PER_MICROS.saturating_mul(2).saturating_mul(Weight::from(*num_slashing_spans)) - ) - .saturating_add(T::DbWeight::get().writes(Weight::from(*num_slashing_spans))) - // if slashing spans is non-zero, add 1 more write - .saturating_add(T::DbWeight::get().writes(Weight::from(*num_slashing_spans > 0))) - ] + #[weight = T::WeightInfo::force_unstake(*num_slashing_spans)] fn force_unstake(origin, stash: T::AccountId, num_slashing_spans: u32) { ensure_root(origin)?; @@ -2000,7 +1954,7 @@ decl_module! { /// - Base Weight: 2.05 µs /// - Write: ForceEra /// # - #[weight = 2 * WEIGHT_PER_MICROS + T::DbWeight::get().writes(1)] + #[weight = T::WeightInfo::force_new_era_always()] fn force_new_era_always(origin) { ensure_root(origin)?; ForceEra::put(Forcing::ForceAlways); @@ -2020,10 +1974,7 @@ decl_module! { /// - Read: Unapplied Slashes /// - Write: Unapplied Slashes /// # - #[weight = T::DbWeight::get().reads_writes(1, 1) - .saturating_add(5_870 * WEIGHT_PER_MICROS) - .saturating_add((35 * WEIGHT_PER_MICROS).saturating_mul(slash_indices.len() as Weight)) - ] + #[weight = T::WeightInfo::cancel_deferred_slash(slash_indices.len() as u32)] fn cancel_deferred_slash(origin, era: EraIndex, slash_indices: Vec) { T::SlashCancelOrigin::ensure_origin(origin)?; @@ -2066,14 +2017,11 @@ decl_module! { /// ErasStakersClipped, ErasRewardPoints, ErasValidatorPrefs (8 items) /// - Read Each: Bonded, Ledger, Payee, Locks, System Account (5 items) /// - Write Each: System Account, Locks, Ledger (3 items) + /// + /// NOTE: weights are assuming that payouts are made to dead controller accounts. + /// Currently we don't refund otherwise. /// # - #[weight = - 120 * WEIGHT_PER_MICROS - + 54 * WEIGHT_PER_MICROS * Weight::from(T::MaxNominatorRewardedPerValidator::get()) - + T::DbWeight::get().reads(7) - + T::DbWeight::get().reads(5) * Weight::from(T::MaxNominatorRewardedPerValidator::get() + 1) - + T::DbWeight::get().writes(3) * Weight::from(T::MaxNominatorRewardedPerValidator::get() + 1) - ] + #[weight = T::WeightInfo::payout_stakers(T::MaxNominatorRewardedPerValidator::get())] fn payout_stakers(origin, validator_stash: T::AccountId, era: EraIndex) -> DispatchResult { ensure!(Self::era_election_status().is_closed(), Error::::CallNotAllowed); ensure_signed(origin)?; @@ -2095,11 +2043,7 @@ decl_module! { /// - Reads: EraElectionStatus, Ledger, Locks, [Origin Account] /// - Writes: [Origin Account], Locks, Ledger /// # - #[weight = - 35 * WEIGHT_PER_MICROS - + 50 * WEIGHT_PER_NANOS * (MAX_UNLOCKING_CHUNKS as Weight) - + T::DbWeight::get().reads_writes(3, 2) - ] + #[weight = T::WeightInfo::rebond(MAX_UNLOCKING_CHUNKS as u32)] fn rebond(origin, #[compact] value: BalanceOf) -> DispatchResultWithPostInfo { ensure!(Self::era_election_status().is_closed(), Error::::CallNotAllowed); let controller = ensure_signed(origin)?; @@ -2136,12 +2080,7 @@ decl_module! { /// - Clear Prefix Each: Era Stakers, EraStakersClipped, ErasValidatorPrefs /// - Writes Each: ErasValidatorReward, ErasRewardPoints, ErasTotalStake, ErasStartSessionIndex /// # - #[weight = { - let items = Weight::from(*_era_items_deleted); - T::DbWeight::get().reads_writes(2, 1) - .saturating_add(T::DbWeight::get().reads_writes(items, items)) - - }] + #[weight = T::WeightInfo::set_history_depth(*_era_items_deleted)] fn set_history_depth(origin, #[compact] new_history_depth: EraIndex, #[compact] _era_items_deleted: u32, @@ -2175,16 +2114,7 @@ decl_module! { /// - Writes: Bonded, Slashing Spans (if S > 0), Ledger, Payee, Validators, Nominators, Stash Account, Locks /// - Writes Each: SpanSlash * S /// # - #[weight = - T::DbWeight::get().reads_writes(4, 7) - .saturating_add(76 * WEIGHT_PER_MICROS) - .saturating_add( - WEIGHT_PER_MICROS.saturating_mul(2).saturating_mul(Weight::from(*num_slashing_spans)) - ) - .saturating_add(T::DbWeight::get().writes(Weight::from(*num_slashing_spans))) - // if slashing spans is non-zero, add 1 more write - .saturating_add(T::DbWeight::get().writes(Weight::from(*num_slashing_spans).min(1))) - ] + #[weight = T::WeightInfo::reap_stash(*num_slashing_spans)] fn reap_stash(_origin, stash: T::AccountId, num_slashing_spans: u32) { ensure!(T::Currency::total_balance(&stash).is_zero(), Error::::FundedTarget); Self::kill_stash(&stash, num_slashing_spans)?; @@ -2236,9 +2166,16 @@ decl_module! { /// minimized (to ensure less variance) /// /// # - /// See `crate::weight` module. + /// The transaction is assumed to be the longest path, a better solution. + /// - Initial solution is almost the same. + /// - Worse solution is retraced in pre-dispatch-checks which sets its own weight. /// # - #[weight = weight::weight_for_submit_solution::(winners, compact, size)] + #[weight = T::WeightInfo::submit_solution_better( + size.validators as u32, + size.validators as u32, + compact.len() as u32, + winners.len() as u32, + )] pub fn submit_election_solution( origin, winners: Vec, @@ -2267,7 +2204,12 @@ decl_module! { /// # /// See `crate::weight` module. /// # - #[weight = weight::weight_for_submit_solution::(winners, compact, size)] + #[weight = T::WeightInfo::submit_solution_better( + size.validators as u32, + size.validators as u32, + compact.len() as u32, + winners.len() as u32, + )] pub fn submit_election_solution_unsigned( origin, winners: Vec, @@ -2581,14 +2523,7 @@ impl Module { election_size: ElectionSize, ) -> DispatchResultWithPostInfo { // Do the basic checks. era, claimed score and window open. - Self::pre_dispatch_checks(claimed_score, era)?; - // the weight that we will refund in case of a correct submission. We compute this now - // because the data needed for it will be consumed further down. - let adjusted_weight = weight::weight_for_correct_submit_solution::( - &winners, - &compact_assignments, - &election_size, - ); + let _ = Self::pre_dispatch_checks(claimed_score, era)?; // Check that the number of presented winners is sane. Most often we have more candidates // than we need. Then it should be `Self::validator_count()`. Else it should be all the @@ -2745,7 +2680,7 @@ impl Module { // emit event. Self::deposit_event(RawEvent::SolutionStored(compute)); - Ok(Some(adjusted_weight).into()) + Ok(None.into()) } /// Start a session potentially starting an era. @@ -2863,7 +2798,6 @@ impl Module { maybe_new_validators } - /// Remove all the storage items associated with the election. fn close_election_window() { // Close window. From 44add7050f865db114c5f22ec795d5aae7a96640 Mon Sep 17 00:00:00 2001 From: kianenigma Date: Wed, 2 Sep 2020 14:55:04 +0200 Subject: [PATCH 03/19] Fix global benchmarks --- bin/node/runtime/src/lib.rs | 2 +- frame/staking/src/default_weights.rs | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/bin/node/runtime/src/lib.rs b/bin/node/runtime/src/lib.rs index 54dea704bd7f6..3ad52b16c2b59 100644 --- a/bin/node/runtime/src/lib.rs +++ b/bin/node/runtime/src/lib.rs @@ -454,7 +454,7 @@ impl pallet_staking::Trait for Runtime { type MinSolutionScoreBump = MinSolutionScoreBump; type MaxNominatorRewardedPerValidator = MaxNominatorRewardedPerValidator; type UnsignedPriority = StakingUnsignedPriority; - type WeightInfo = (); + type WeightInfo = pallet_staking::default_weights::WeightInfo; } parameter_types! { diff --git a/frame/staking/src/default_weights.rs b/frame/staking/src/default_weights.rs index 9afae2edafb0a..d2f1dc120b092 100644 --- a/frame/staking/src/default_weights.rs +++ b/frame/staking/src/default_weights.rs @@ -224,7 +224,7 @@ impl crate::WeightInfo for () { fn withdraw_unbonded_update(_s: u32, ) -> Weight { 1_000_000_000 } fn withdraw_unbonded_kill(_s: u32, ) -> Weight { 1_000_000_000 } fn validate() -> Weight { 1_000_000_000 } - fn nominate(n: u32) -> Weight { 1_000_000_000 } + fn nominate(_n: u32) -> Weight { 1_000_000_000 } fn chill() -> Weight { 1_000_000_000 } fn set_payee() -> Weight { 1_000_000_000 } fn set_controller() -> Weight { 1_000_000_000 } From 0147878b1208f0835794bda5b3162b0518c13343 Mon Sep 17 00:00:00 2001 From: kianenigma Date: Thu, 3 Sep 2020 17:08:22 +0200 Subject: [PATCH 04/19] re-calculate the submit solution weight. --- frame/staking/src/benchmarking.rs | 37 ++++++-- frame/staking/src/default_weights.rs | 58 ++++++------ frame/staking/src/lib.rs | 80 +++------------- primitives/npos-elections/compact/src/lib.rs | 97 +++++++++++++++++--- primitives/npos-elections/src/tests.rs | 71 +++++++++++++- 5 files changed, 223 insertions(+), 120 deletions(-) diff --git a/frame/staking/src/benchmarking.rs b/frame/staking/src/benchmarking.rs index 68ed64e30c7b6..48a20a4140f8d 100644 --- a/frame/staking/src/benchmarking.rs +++ b/frame/staking/src/benchmarking.rs @@ -436,18 +436,17 @@ benchmarks! { } } - // This benchmark create `v` validators intent, `n` nominators intent, each nominator nominate - // MAX_NOMINATIONS in the set of the first `w` validators. - // It builds a solution with `w` winners composed of nominated validators randomly nominated, - // `a` assignment with MAX_NOMINATIONS. + // This benchmark create `v` validators intent, `n` nominators intent, in total creating `e` + // edges. submit_solution_initial { - // number of validator intention. + // number of validator intention. This will be equal to `ElectionSize::validators`. let v in 200 .. 400; - // number of nominator intention. + // number of nominator intention. This will be equal to `ElectionSize::nominators`. let n in 500 .. 1000; - // number of assignments. Basically, number of active nominators. + // number of assignments. Basically, number of active nominators. This will be equal to + // `compact.len()`. let a in 200 .. 400; - // number of winners, also ValidatorCount. + // number of winners, also ValidatorCount. This will be equal to `winner.len()`. let w in 16 .. 100; ensure!(w as usize >= MAX_NOMINATIONS, "doesn't support lower value"); @@ -476,6 +475,13 @@ benchmarks! { size ) = offchain_election::prepare_submission::(assignments, winners, false).unwrap(); + assert_eq!( + winners.len(), compact.unique_targets().len(), + "unique targets ({}) and winners ({}) count not same. This solution is not valid.", + compact.unique_targets().len(), + winners.len(), + ); + // needed for the solution to be accepted >::put(ElectionStatus::Open(T::BlockNumber::from(1u32))); @@ -502,6 +508,7 @@ benchmarks! { } // same as submit_solution_initial but we place a very weak solution on chian first. + #[extra] submit_solution_better { // number of validator intention. let v in 200 .. 400; @@ -540,6 +547,13 @@ benchmarks! { size ) = offchain_election::prepare_submission::(assignments, winners, false).unwrap(); + assert_eq!( + winners.len(), compact.unique_targets().len(), + "unique targets ({}) and winners ({}) count not same. This solution is not valid.", + compact.unique_targets().len(), + winners.len(), + ); + // needed for the solution to be accepted >::put(ElectionStatus::Open(T::BlockNumber::from(1u32))); @@ -586,6 +600,7 @@ benchmarks! { } // This will be early rejected based on the score. + #[extra] submit_solution_weaker { // number of validator intention. let v in 200 .. 400; @@ -609,6 +624,12 @@ benchmarks! { // submit a seq-phragmen with all the good stuff on chain. { let (winners, compact, score, size) = get_seq_phragmen_solution::(true); + assert_eq!( + winners.len(), compact.unique_targets().len(), + "unique targets ({}) and winners ({}) count not same. This solution is not valid.", + compact.unique_targets().len(), + winners.len(), + ); assert!( >::submit_election_solution( RawOrigin::Signed(caller.clone()).into(), diff --git a/frame/staking/src/default_weights.rs b/frame/staking/src/default_weights.rs index d2f1dc120b092..6edb697806ee1 100644 --- a/frame/staking/src/default_weights.rs +++ b/frame/staking/src/default_weights.rs @@ -215,35 +215,37 @@ impl crate::WeightInfo for WeightInfo { } } +#[cfg(feature = "std")] +const DEFAULT_WEIGHT: Weight = 1_000_000_000; #[cfg(feature = "std")] impl crate::WeightInfo for () { - fn bond() -> Weight { 1_000_000_000 } - fn bond_extra() -> Weight { 1_000_000_000 } - fn unbond() -> Weight { 1_000_000_000 } - fn withdraw_unbonded_update(_s: u32, ) -> Weight { 1_000_000_000 } - fn withdraw_unbonded_kill(_s: u32, ) -> Weight { 1_000_000_000 } - fn validate() -> Weight { 1_000_000_000 } - fn nominate(_n: u32) -> Weight { 1_000_000_000 } - fn chill() -> Weight { 1_000_000_000 } - fn set_payee() -> Weight { 1_000_000_000 } - fn set_controller() -> Weight { 1_000_000_000 } - fn set_validator_count() -> Weight { 1_000_000_000 } - fn force_no_eras() -> Weight { 1_000_000_000 } - fn force_new_era() -> Weight { 1_000_000_000 } - fn force_new_era_always() -> Weight { 1_000_000_000 } - fn set_invulnerables(_v: u32, ) -> Weight { 1_000_000_000 } - fn force_unstake(_s: u32, ) -> Weight { 1_000_000_000 } - fn cancel_deferred_slash(_s: u32, ) -> Weight { 1_000_000_000 } - fn payout_stakers(_n: u32, ) -> Weight { 1_000_000_000 } - fn payout_stakers_alive_controller(_n: u32, ) -> Weight { 1_000_000_000 } - fn rebond(_l: u32, ) -> Weight { 1_000_000_000 } - fn set_history_depth(_e: u32, ) -> Weight { 1_000_000_000 } - fn reap_stash(_s: u32, ) -> Weight { 1_000_000_000 } - fn new_era(_v: u32, _n: u32, ) -> Weight { 1_000_000_000 } - fn do_slash() -> Weight { 1_000_000_000 } - fn payout_all(_v: u32, _n: u32, ) -> Weight { 1_000_000_000 } - fn submit_solution_initial(_v: u32, _n: u32, _a: u32, _w: u32, ) -> Weight { 1_000_000_000 } - fn submit_solution_better(_v: u32, _n: u32, _a: u32, _w: u32, ) -> Weight { 1_000_000_000 } - fn submit_solution_weaker(_v: u32, _n: u32, ) -> Weight { 1_000_000_000 } + fn bond() -> Weight { DEFAULT_WEIGHT } + fn bond_extra() -> Weight { DEFAULT_WEIGHT } + fn unbond() -> Weight { DEFAULT_WEIGHT } + fn withdraw_unbonded_update(_s: u32, ) -> Weight { DEFAULT_WEIGHT } + fn withdraw_unbonded_kill(_s: u32, ) -> Weight { DEFAULT_WEIGHT } + fn validate() -> Weight { DEFAULT_WEIGHT } + fn nominate(_n: u32) -> Weight { DEFAULT_WEIGHT } + fn chill() -> Weight { DEFAULT_WEIGHT } + fn set_payee() -> Weight { DEFAULT_WEIGHT } + fn set_controller() -> Weight { DEFAULT_WEIGHT } + fn set_validator_count() -> Weight { DEFAULT_WEIGHT } + fn force_no_eras() -> Weight { DEFAULT_WEIGHT } + fn force_new_era() -> Weight { DEFAULT_WEIGHT } + fn force_new_era_always() -> Weight { DEFAULT_WEIGHT } + fn set_invulnerables(_v: u32, ) -> Weight { DEFAULT_WEIGHT } + fn force_unstake(_s: u32, ) -> Weight { DEFAULT_WEIGHT } + fn cancel_deferred_slash(_s: u32, ) -> Weight { DEFAULT_WEIGHT } + fn payout_stakers(_n: u32, ) -> Weight { DEFAULT_WEIGHT } + fn payout_stakers_alive_controller(_n: u32, ) -> Weight { DEFAULT_WEIGHT } + fn rebond(_l: u32, ) -> Weight { DEFAULT_WEIGHT } + fn set_history_depth(_e: u32, ) -> Weight { DEFAULT_WEIGHT } + fn reap_stash(_s: u32, ) -> Weight { DEFAULT_WEIGHT } + fn new_era(_v: u32, _n: u32, ) -> Weight { DEFAULT_WEIGHT } + fn do_slash() -> Weight { DEFAULT_WEIGHT } + fn payout_all(_v: u32, _n: u32, ) -> Weight { DEFAULT_WEIGHT } + fn submit_solution_initial(_v: u32, _n: u32, _a: u32, _w: u32, ) -> Weight { DEFAULT_WEIGHT } + fn submit_solution_better(_v: u32, _n: u32, _a: u32, _w: u32, ) -> Weight { DEFAULT_WEIGHT } + fn submit_solution_weaker(_v: u32, _n: u32, ) -> Weight { DEFAULT_WEIGHT } } diff --git a/frame/staking/src/lib.rs b/frame/staking/src/lib.rs index abd9dfbee9d6d..2a1cf6e4f156d 100644 --- a/frame/staking/src/lib.rs +++ b/frame/staking/src/lib.rs @@ -769,72 +769,6 @@ impl SessionInterface<::AccountId> for T whe } } -mod weight { - use super::*; - - /// All weight notes are pertaining to the case of a better solution, in which we execute - /// the longest code path. - /// Weight: 0 + (0.63 μs * v) + (0.36 μs * n) + (96.53 μs * a ) + (8 μs * w ) with: - /// * v validators in snapshot validators, - /// * n nominators in snapshot nominators, - /// * a assignment in the submitted solution - /// * w winners in the submitted solution - /// - /// State reads: - /// - Initial checks: - /// - ElectionState, CurrentEra, QueuedScore - /// - SnapshotValidators.len() + SnapShotNominators.len() - /// - ValidatorCount - /// - SnapshotValidators - /// - SnapshotNominators - /// - Iterate over nominators: - /// - compact.len() * Nominators(who) - /// - (non_self_vote_edges) * SlashingSpans - /// - For `assignment_ratio_to_staked`: Basically read the staked value of each stash. - /// - (winners.len() + compact.len()) * (Ledger + Bonded) - /// - TotalIssuance (read a gzillion times potentially, but well it is cached.) - /// - State writes: - /// - QueuedElected, QueuedScore - pub fn weight_for_submit_solution( - winners: &Vec, - compact: &CompactAssignments, - size: &ElectionSize, - ) -> Weight { - (630 * WEIGHT_PER_NANOS).saturating_mul(size.validators as Weight) - .saturating_add((360 * WEIGHT_PER_NANOS).saturating_mul(size.nominators as Weight)) - .saturating_add((96 * WEIGHT_PER_MICROS).saturating_mul(compact.len() as Weight)) - .saturating_add((8 * WEIGHT_PER_MICROS).saturating_mul(winners.len() as Weight)) - // Initial checks - .saturating_add(T::DbWeight::get().reads(8)) - // Nominators - .saturating_add(T::DbWeight::get().reads(compact.len() as Weight)) - // SlashingSpans (upper bound for invalid solution) - .saturating_add(T::DbWeight::get().reads(compact.edge_count() as Weight)) - // `assignment_ratio_to_staked` - .saturating_add(T::DbWeight::get().reads(2 * ((winners.len() + compact.len()) as Weight))) - .saturating_add(T::DbWeight::get().reads(1)) - // write queued score and elected - .saturating_add(T::DbWeight::get().writes(2)) - } - - /// Weight of `submit_solution` in case of a correct submission. - /// - /// refund: we charged compact.len() * read(1) for SlashingSpans. A valid solution only reads - /// winners.len(). - pub fn weight_for_correct_submit_solution( - winners: &Vec, - compact: &CompactAssignments, - size: &ElectionSize, - ) -> Weight { - // NOTE: for consistency, we re-compute the original weight to maintain their relation and - // prevent any foot-guns. - let original_weight = weight_for_submit_solution::(winners, compact, size); - original_weight - .saturating_sub(T::DbWeight::get().reads(compact.edge_count() as Weight)) - .saturating_add(T::DbWeight::get().reads(winners.len() as Weight)) - } -} - pub trait WeightInfo { fn bond() -> Weight; fn bond_extra() -> Weight; @@ -2172,7 +2106,7 @@ decl_module! { /// # #[weight = T::WeightInfo::submit_solution_better( size.validators as u32, - size.validators as u32, + size.nominators as u32, compact.len() as u32, winners.len() as u32, )] @@ -2206,7 +2140,7 @@ decl_module! { /// # #[weight = T::WeightInfo::submit_solution_better( size.validators as u32, - size.validators as u32, + size.nominators as u32, compact.len() as u32, winners.len() as u32, )] @@ -2525,6 +2459,16 @@ impl Module { // Do the basic checks. era, claimed score and window open. let _ = Self::pre_dispatch_checks(claimed_score, era)?; + // before we read any further state, we check that the unique targets in compact is same as + // compact. is a all in-memory check and easy to do. Moreover, it ensures that the solution + // is not full of bogus edges that can cause lots of reads to SlashingSpans. Thus, we can + // assume that the storage access of this function is always O(|winners|), not + // O(|compact.edge_count()|). + ensure!( + compact_assignments.unique_targets().len() == winners.len(), + Error::::OffchainElectionBogusWinnerCount, + ); + // Check that the number of presented winners is sane. Most often we have more candidates // than we need. Then it should be `Self::validator_count()`. Else it should be all the // candidates. diff --git a/primitives/npos-elections/compact/src/lib.rs b/primitives/npos-elections/compact/src/lib.rs index 03526d17981f9..6b1d9f7c96a23 100644 --- a/primitives/npos-elections/compact/src/lib.rs +++ b/primitives/npos-elections/compact/src/lib.rs @@ -150,21 +150,9 @@ fn struct_def( }).collect::(); - let len_impl = (1..=count).map(|c| { - let field_name = field_name_for(c); - quote!( - all_len = all_len.saturating_add(self.#field_name.len()); - ) - }).collect::(); - - let edge_count_impl = (1..count).map(|c| { - let field_name = field_name_for(c); - quote!( - all_edges = all_edges.saturating_add( - self.#field_name.len().saturating_mul(#c as usize) - ); - ) - }).collect::(); + let len_impl = len_impl(count); + let edge_count_impl = edge_count_impl(count); + let unique_targets_impl = unique_targets_impl(count); let derives_and_maybe_compact_encoding = if compact_encoding { // custom compact encoding. @@ -209,6 +197,26 @@ fn struct_def( all_edges } + /// Get the number of unique targets in the whole struct. + /// + /// Once presented with a list of winners, this set and the set of winners must be + /// equal. + /// + /// The resulting indices are sorted. + pub fn unique_targets(&self) -> Vec<#target_type> { + let mut all_targets: Vec<#target_type> = Vec::with_capacity(self.average_edge_count()); + let mut maybe_insert_target = |t: #target_type| { + match all_targets.binary_search(&t) { + Ok(_) => (), + Err(pos) => all_targets.insert(pos, t) + } + }; + + #unique_targets_impl + + all_targets + } + /// Get the average edge count. pub fn average_edge_count(&self) -> usize { self.edge_count().checked_div(self.len()).unwrap_or(0) @@ -217,6 +225,65 @@ fn struct_def( )) } +fn len_impl(count: usize) -> TokenStream2 { + (1..=count).map(|c| { + let field_name = field_name_for(c); + quote!( + all_len = all_len.saturating_add(self.#field_name.len()); + ) + }).collect::() +} + +fn edge_count_impl(count: usize) -> TokenStream2 { + (1..=count).map(|c| { + let field_name = field_name_for(c); + quote!( + all_edges = all_edges.saturating_add( + self.#field_name.len().saturating_mul(#c as usize) + ); + ) + }).collect::() +} + +fn unique_targets_impl(count: usize) -> TokenStream2 { + let unique_targets_impl_single = { + let field_name = field_name_for(1); + quote! { + self.#field_name.iter().for_each(|(_, t)| { + maybe_insert_target(*t); + }); + } + }; + + let unique_targets_impl_double = { + let field_name = field_name_for(2); + quote! { + self.#field_name.iter().for_each(|(_, (t1, _), t2)| { + maybe_insert_target(*t1); + maybe_insert_target(*t2); + }); + } + }; + + let unique_targets_impl_rest = (3..=count).map(|c| { + let field_name = field_name_for(c); + quote! { + self.#field_name.iter().for_each(|(_, inners, t_last)| { + inners.iter().for_each(|(t, _)| { + maybe_insert_target(*t); + }); + maybe_insert_target(*t_last); + }); + } + }).collect::(); + + quote! { + #unique_targets_impl_single + #unique_targets_impl_double + #unique_targets_impl_rest + } +} + fn imports() -> Result { if std::env::var("CARGO_PKG_NAME").unwrap() == "sp-npos-elections" { Ok(quote! { diff --git a/primitives/npos-elections/src/tests.rs b/primitives/npos-elections/src/tests.rs index 8e99d2222e885..d1769acd08144 100644 --- a/primitives/npos-elections/src/tests.rs +++ b/primitives/npos-elections/src/tests.rs @@ -1002,6 +1002,7 @@ mod solution_type { ); assert_eq!(compact.len(), 4); assert_eq!(compact.edge_count(), 2 + 4); + assert_eq!(compact.unique_targets(), vec![10, 11, 20, 40, 50, 51]); } #[test] @@ -1097,6 +1098,11 @@ mod solution_type { } ); + assert_eq!( + compacted.unique_targets(), + vec![0, 1, 2, 3, 4, 5, 6, 7, 8], + ); + let voter_at = |a: u32| -> Option { voters.get(>::try_into(a).unwrap()).cloned() }; @@ -1110,6 +1116,69 @@ mod solution_type { ); } + #[test] + fn unique_targets_len_edge_count_works() { + const ACC: TestAccuracy = TestAccuracy::from_percent(10); + + // we don't really care about voters here so all duplicates. This is not invalid per se. + let compact = TestSolutionCompact { + votes1: vec![(99, 1), (99, 2)], + votes2: vec![ + (99, (3, ACC.clone()), 7), + (99, (4, ACC.clone()), 8), + ], + votes3: vec![ + (99, [(11, ACC.clone()), (12, ACC.clone())], 13), + ], + // ensure the last one is also counted. + votes16: vec![ + ( + 99, + [ + (66, ACC.clone()), + (66, ACC.clone()), + (66, ACC.clone()), + (66, ACC.clone()), + (66, ACC.clone()), + (66, ACC.clone()), + (66, ACC.clone()), + (66, ACC.clone()), + (66, ACC.clone()), + (66, ACC.clone()), + (66, ACC.clone()), + (66, ACC.clone()), + (66, ACC.clone()), + (66, ACC.clone()), + (66, ACC.clone()), + ], + 67, + ) + ], + ..Default::default() + }; + + assert_eq!(compact.unique_targets(), vec![1, 2, 3, 4, 7, 8, 11, 12, 13, 66, 67]); + assert_eq!(compact.edge_count(), 2 + (2 * 2) + 3 + 16); + assert_eq!(compact.len(), 6); + + // this one has some duplicates. + let compact = TestSolutionCompact { + votes1: vec![(99, 1), (99, 1)], + votes2: vec![ + (99, (3, ACC.clone()), 7), + (99, (4, ACC.clone()), 8), + ], + votes3: vec![ + (99, [(11, ACC.clone()), (11, ACC.clone())], 13), + ], + ..Default::default() + }; + + assert_eq!(compact.unique_targets(), vec![1, 3, 4, 7, 8, 11, 13]); + assert_eq!(compact.edge_count(), 2 + (2 * 2) + 3); + assert_eq!(compact.len(), 5); + } + #[test] fn compact_into_assignment_must_report_overflow() { // in votes2 @@ -1165,7 +1234,7 @@ mod solution_type { assert_eq!(compacted.unwrap_err(), PhragmenError::CompactTargetOverflow); } - #[test] + #[test] fn zero_target_count_is_ignored() { let voters = vec![1 as AccountId, 2]; let targets = vec![10 as AccountId, 11]; From e874300de17b8702b7068805202c5d4041608b72 Mon Sep 17 00:00:00 2001 From: kianenigma Date: Fri, 4 Sep 2020 10:16:58 +0200 Subject: [PATCH 05/19] Fix some refund. --- bin/node/runtime/src/lib.rs | 2 +- frame/staking/src/default_weights.rs | 38 +--------------------------- frame/staking/src/lib.rs | 7 +---- 3 files changed, 3 insertions(+), 44 deletions(-) diff --git a/bin/node/runtime/src/lib.rs b/bin/node/runtime/src/lib.rs index 3ad52b16c2b59..54dea704bd7f6 100644 --- a/bin/node/runtime/src/lib.rs +++ b/bin/node/runtime/src/lib.rs @@ -454,7 +454,7 @@ impl pallet_staking::Trait for Runtime { type MinSolutionScoreBump = MinSolutionScoreBump; type MaxNominatorRewardedPerValidator = MaxNominatorRewardedPerValidator; type UnsignedPriority = StakingUnsignedPriority; - type WeightInfo = pallet_staking::default_weights::WeightInfo; + type WeightInfo = (); } parameter_types! { diff --git a/frame/staking/src/default_weights.rs b/frame/staking/src/default_weights.rs index 6edb697806ee1..6240d9769c2ee 100644 --- a/frame/staking/src/default_weights.rs +++ b/frame/staking/src/default_weights.rs @@ -24,8 +24,7 @@ use frame_support::weights::{Weight, constants::RocksDbWeight as DbWeight}; -pub struct WeightInfo; -impl crate::WeightInfo for WeightInfo { +impl crate::WeightInfo for () { // WARNING! Some components were not used: ["u"] fn bond() -> Weight { (65788000 as Weight) @@ -214,38 +213,3 @@ impl crate::WeightInfo for WeightInfo { .saturating_add(DbWeight::get().reads(3 as Weight)) } } - -#[cfg(feature = "std")] -const DEFAULT_WEIGHT: Weight = 1_000_000_000; - -#[cfg(feature = "std")] -impl crate::WeightInfo for () { - fn bond() -> Weight { DEFAULT_WEIGHT } - fn bond_extra() -> Weight { DEFAULT_WEIGHT } - fn unbond() -> Weight { DEFAULT_WEIGHT } - fn withdraw_unbonded_update(_s: u32, ) -> Weight { DEFAULT_WEIGHT } - fn withdraw_unbonded_kill(_s: u32, ) -> Weight { DEFAULT_WEIGHT } - fn validate() -> Weight { DEFAULT_WEIGHT } - fn nominate(_n: u32) -> Weight { DEFAULT_WEIGHT } - fn chill() -> Weight { DEFAULT_WEIGHT } - fn set_payee() -> Weight { DEFAULT_WEIGHT } - fn set_controller() -> Weight { DEFAULT_WEIGHT } - fn set_validator_count() -> Weight { DEFAULT_WEIGHT } - fn force_no_eras() -> Weight { DEFAULT_WEIGHT } - fn force_new_era() -> Weight { DEFAULT_WEIGHT } - fn force_new_era_always() -> Weight { DEFAULT_WEIGHT } - fn set_invulnerables(_v: u32, ) -> Weight { DEFAULT_WEIGHT } - fn force_unstake(_s: u32, ) -> Weight { DEFAULT_WEIGHT } - fn cancel_deferred_slash(_s: u32, ) -> Weight { DEFAULT_WEIGHT } - fn payout_stakers(_n: u32, ) -> Weight { DEFAULT_WEIGHT } - fn payout_stakers_alive_controller(_n: u32, ) -> Weight { DEFAULT_WEIGHT } - fn rebond(_l: u32, ) -> Weight { DEFAULT_WEIGHT } - fn set_history_depth(_e: u32, ) -> Weight { DEFAULT_WEIGHT } - fn reap_stash(_s: u32, ) -> Weight { DEFAULT_WEIGHT } - fn new_era(_v: u32, _n: u32, ) -> Weight { DEFAULT_WEIGHT } - fn do_slash() -> Weight { DEFAULT_WEIGHT } - fn payout_all(_v: u32, _n: u32, ) -> Weight { DEFAULT_WEIGHT } - fn submit_solution_initial(_v: u32, _n: u32, _a: u32, _w: u32, ) -> Weight { DEFAULT_WEIGHT } - fn submit_solution_better(_v: u32, _n: u32, _a: u32, _w: u32, ) -> Weight { DEFAULT_WEIGHT } - fn submit_solution_weaker(_v: u32, _n: u32, ) -> Weight { DEFAULT_WEIGHT } -} diff --git a/frame/staking/src/lib.rs b/frame/staking/src/lib.rs index 2a1cf6e4f156d..bd2516c9063f7 100644 --- a/frame/staking/src/lib.rs +++ b/frame/staking/src/lib.rs @@ -1603,12 +1603,7 @@ decl_module! { Self::update_ledger(&controller, &ledger); // This is only an update, so we use less overall weight. - // Note: we are re-calculating weights here which is not performant per se but it is - // accurate and maintainable. The calculation themselve are trivial and O(1). - Some( - T::WeightInfo::withdraw_unbonded_kill(num_slashing_spans) - .saturating_sub(T::WeightInfo::withdraw_unbonded_update(num_slashing_spans)) - ) + Some(T::WeightInfo::withdraw_unbonded_update(num_slashing_spans)) }; // `old_total` should never be less than the new total because From 74bfea08317d838289641c608ec59b9aaf5190ba Mon Sep 17 00:00:00 2001 From: kianenigma Date: Fri, 4 Sep 2020 10:56:42 +0200 Subject: [PATCH 06/19] Get rid of all the extra parameters. --- frame/staking/src/benchmarking.rs | 42 ++++++++++++---------------- frame/staking/src/default_weights.rs | 26 +---------------- frame/staking/src/lib.rs | 3 -- 3 files changed, 19 insertions(+), 52 deletions(-) diff --git a/frame/staking/src/benchmarking.rs b/frame/staking/src/benchmarking.rs index 48a20a4140f8d..7490c53ce9bb9 100644 --- a/frame/staking/src/benchmarking.rs +++ b/frame/staking/src/benchmarking.rs @@ -100,16 +100,14 @@ pub fn create_validator_with_nominators( Ok(v_stash) } +const USER_SEED: u32 = 999666; + benchmarks! { - _{ - // User account seed - let u in 0 .. 1000 => (); - } + _{} bond { - let u in ...; - let stash = create_funded_user::("stash", u, 100); - let controller = create_funded_user::("controller", u, 100); + let stash = create_funded_user::("stash", USER_SEED, 100); + let controller = create_funded_user::("controller", USER_SEED, 100); let controller_lookup: ::Source = T::Lookup::unlookup(controller.clone()); let reward_destination = RewardDestination::Staked; let amount = T::Currency::minimum_balance() * 10.into(); @@ -120,8 +118,7 @@ benchmarks! { } bond_extra { - let u in ...; - let (stash, controller) = create_stash_controller::(u, 100)?; + let (stash, controller) = create_stash_controller::(USER_SEED, 100)?; 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; @@ -133,8 +130,7 @@ benchmarks! { } unbond { - let u in ...; - let (_, controller) = create_stash_controller::(u, 100)?; + let (_, controller) = create_stash_controller::(USER_SEED, 100)?; 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; @@ -180,8 +176,7 @@ benchmarks! { } validate { - let u in ...; - let (stash, controller) = create_stash_controller::(u, 100)?; + let (stash, controller) = create_stash_controller::(USER_SEED, 100)?; let prefs = ValidatorPrefs::default(); }: _(RawOrigin::Signed(controller), prefs) verify { @@ -199,13 +194,11 @@ benchmarks! { } chill { - let u in ...; - let (_, controller) = create_stash_controller::(u, 100)?; + let (_, controller) = create_stash_controller::(USER_SEED, 100)?; }: _(RawOrigin::Signed(controller)) set_payee { - let u in ...; - let (stash, controller) = create_stash_controller::(u, 100)?; + let (stash, controller) = create_stash_controller::(USER_SEED, 100)?; assert_eq!(Payee::::get(&stash), RewardDestination::Staked); }: _(RawOrigin::Signed(controller), RewardDestination::Controller) verify { @@ -213,9 +206,8 @@ benchmarks! { } set_controller { - let u in ...; - let (stash, _) = create_stash_controller::(u, 100)?; - let new_controller = create_funded_user::("new_controller", u, 100); + let (stash, _) = create_stash_controller::(USER_SEED, 100)?; + let new_controller = create_funded_user::("new_controller", USER_SEED, 100); let new_controller_lookup = T::Lookup::unlookup(new_controller.clone()); }: _(RawOrigin::Signed(stash), new_controller_lookup) verify { @@ -229,13 +221,13 @@ benchmarks! { assert_eq!(ValidatorCount::get(), c); } - force_no_eras { let i in 0 .. 1; }: _(RawOrigin::Root) + force_no_eras {}: _(RawOrigin::Root) verify { assert_eq!(ForceEra::get(), Forcing::ForceNone); } - force_new_era {let i in 0 .. 1; }: _(RawOrigin::Root) + force_new_era {}: _(RawOrigin::Root) verify { assert_eq!(ForceEra::get(), Forcing::ForceNew); } - force_new_era_always { let i in 0 .. 1; }: _(RawOrigin::Root) + force_new_era_always {}: _(RawOrigin::Root) verify { assert_eq!(ForceEra::get(), Forcing::ForceAlways); } // Worst case scenario, the list of invulnerables is very long. @@ -315,7 +307,7 @@ benchmarks! { rebond { let l in 1 .. MAX_UNLOCKING_CHUNKS as u32; - let (_, controller) = create_stash_controller::(u, 100)?; + let (_, controller) = create_stash_controller::(USER_SEED, 100)?; let mut staking_ledger = Ledger::::get(controller.clone()).unwrap(); let unlock_chunk = UnlockChunk::> { value: 1.into(), @@ -372,6 +364,7 @@ benchmarks! { assert!(validators.len() == v as usize); } + #[extra] do_slash { let l in 1 .. MAX_UNLOCKING_CHUNKS as u32; let (stash, controller) = create_stash_controller::(0, 100)?; @@ -438,6 +431,7 @@ benchmarks! { // This benchmark create `v` validators intent, `n` nominators intent, in total creating `e` // edges. + #[extra] submit_solution_initial { // number of validator intention. This will be equal to `ElectionSize::validators`. let v in 200 .. 400; diff --git a/frame/staking/src/default_weights.rs b/frame/staking/src/default_weights.rs index 6240d9769c2ee..49bfa695a82ba 100644 --- a/frame/staking/src/default_weights.rs +++ b/frame/staking/src/default_weights.rs @@ -1,4 +1,3 @@ - // This file is part of Substrate. // Copyright (C) 2017-2020 Parity Technologies (UK) Ltd. @@ -169,12 +168,6 @@ impl crate::WeightInfo for () { .saturating_add(DbWeight::get().writes(8 as Weight)) .saturating_add(DbWeight::get().writes((3 as Weight).saturating_mul(v as Weight))) } - // WARNING! Some components were not used: ["l"] - fn do_slash() -> Weight { - (47783000 as Weight) - .saturating_add(DbWeight::get().reads(3 as Weight)) - .saturating_add(DbWeight::get().writes(2 as Weight)) - } fn payout_all(v: u32, n: u32, ) -> Weight { (0 as Weight) .saturating_add((1841741000 as Weight).saturating_mul(v as Weight)) @@ -184,7 +177,7 @@ impl crate::WeightInfo for () { .saturating_add(DbWeight::get().writes((8 as Weight).saturating_mul(v as Weight))) .saturating_add(DbWeight::get().writes((3 as Weight).saturating_mul(n as Weight))) } - fn submit_solution_initial(v: u32, n: u32, a: u32, w: u32, ) -> Weight { + fn submit_solution_better(v: u32, n: u32, a: u32, w: u32, ) -> Weight { (0 as Weight) .saturating_add((762000 as Weight).saturating_mul(v as Weight)) .saturating_add((868000 as Weight).saturating_mul(n as Weight)) @@ -195,21 +188,4 @@ impl crate::WeightInfo for () { .saturating_add(DbWeight::get().reads((1 as Weight).saturating_mul(w as Weight))) .saturating_add(DbWeight::get().writes(2 as Weight)) } - fn submit_solution_better(v: u32, n: u32, a: u32, w: u32, ) -> Weight { - (0 as Weight) - .saturating_add((784000 as Weight).saturating_mul(v as Weight)) - .saturating_add((640000 as Weight).saturating_mul(n as Weight)) - .saturating_add((68830000 as Weight).saturating_mul(a as Weight)) - .saturating_add((8181000 as Weight).saturating_mul(w as Weight)) - .saturating_add(DbWeight::get().reads(6 as Weight)) - .saturating_add(DbWeight::get().reads((4 as Weight).saturating_mul(a as Weight))) - .saturating_add(DbWeight::get().reads((1 as Weight).saturating_mul(w as Weight))) - .saturating_add(DbWeight::get().writes(2 as Weight)) - } - fn submit_solution_weaker(v: u32, n: u32, ) -> Weight { - (11244000 as Weight) - .saturating_add((28000 as Weight).saturating_mul(v as Weight)) - .saturating_add((12000 as Weight).saturating_mul(n as Weight)) - .saturating_add(DbWeight::get().reads(3 as Weight)) - } } diff --git a/frame/staking/src/lib.rs b/frame/staking/src/lib.rs index bd2516c9063f7..a8a5b253fca19 100644 --- a/frame/staking/src/lib.rs +++ b/frame/staking/src/lib.rs @@ -793,11 +793,8 @@ pub trait WeightInfo { fn set_history_depth(e: u32, ) -> Weight; fn reap_stash(s: u32, ) -> Weight; fn new_era(v: u32, n: u32, ) -> Weight; - fn do_slash() -> Weight; fn payout_all(v: u32, n: u32, ) -> Weight; - fn submit_solution_initial(v: u32, n: u32, a: u32, w: u32, ) -> Weight; fn submit_solution_better(v: u32, n: u32, a: u32, w: u32, ) -> Weight; - fn submit_solution_weaker(v: u32, n: u32, ) -> Weight; } pub trait Trait: frame_system::Trait + SendTransactionTypes> { From 73ae89668d8de9506315e01df0bd7063b5fefd0f Mon Sep 17 00:00:00 2001 From: kianenigma Date: Fri, 4 Sep 2020 13:55:05 +0200 Subject: [PATCH 07/19] Fix staking tests. --- frame/staking/src/tests.rs | 57 ++++++++++++++++++++++++++++++++++---- 1 file changed, 51 insertions(+), 6 deletions(-) diff --git a/frame/staking/src/tests.rs b/frame/staking/src/tests.rs index d27654d1feae6..0f5d08a3a8ccc 100644 --- a/frame/staking/src/tests.rs +++ b/frame/staking/src/tests.rs @@ -3615,7 +3615,7 @@ mod offchain_election { // A validator index which is out of bound ExtBuilder::default() .offchain_election_ext() - .validator_count(4) + .validator_count(2) .has_stakers(false) .build() .execute_with(|| { @@ -3627,7 +3627,7 @@ mod offchain_election { let (mut compact, winners, score) = prepare_submission_with(true, 2, |_| {}); // index 4 doesn't exist. - compact.votes1.push((3, 4)); + compact.votes1.iter_mut().for_each(|(_, vidx)| if *vidx == 1 { *vidx = 4 }); // The error type sadly cannot be more specific now. assert_noop!( @@ -3688,12 +3688,57 @@ mod offchain_election { assert_eq!(Staking::snapshot_nominators().unwrap().len(), 5 + 4); assert_eq!(Staking::snapshot_validators().unwrap().len(), 4); + let (compact, winners, score) = prepare_submission_with(true, 2, |a| { + // swap all 11 and 41s in the distribution with non-winners. Note that it is + // important that the count of winners and the count of unique targets remain + // valid. + a.iter_mut().for_each(| StakedAssignment { who, distribution } | + distribution.iter_mut().for_each(|(t, _)| { + if *t == 41 { *t = 31 } else { *t = 21 } + // if it is self vote, correct that. + if *who == 41 { *who = 31 } + if *who == 11 { *who = 21 } + }) + ); + }); + + assert_noop!( + submit_solution( + Origin::signed(10), + winners, + compact, + score, + ), + Error::::OffchainElectionBogusNomination, + ); + }) + } + + #[test] + fn offchain_election_unique_target_count_is_checked() { + // Number of unique targets and and winners.len must match. + ExtBuilder::default() + .offchain_election_ext() + .validator_count(2) // we select only 2. + .has_stakers(false) + .build() + .execute_with(|| { + build_offchain_election_test_ext(); + run_to_block(12); + + assert_eq!(Staking::snapshot_nominators().unwrap().len(), 5 + 4); + assert_eq!(Staking::snapshot_validators().unwrap().len(), 4); + let (compact, winners, score) = prepare_submission_with(true, 2, |a| { a.iter_mut() .find(|x| x.who == 5) - // all 3 cannot be among the winners. Although, all of them are validator - // candidates. - .map(|x| x.distribution = vec![(21, 50), (41, 30), (31, 20)]); + // just add any new target. + .map(|x| { + // old value. + assert_eq!(x.distribution, vec![(41, 100)]); + // new value. + x.distribution = vec![(21, 50), (41, 50)] + }); }); assert_noop!( @@ -3703,7 +3748,7 @@ mod offchain_election { compact, score, ), - Error::::OffchainElectionBogusEdge, + Error::::OffchainElectionBogusWinnerCount, ); }) } From 4c94916bb762c51952963205383ee81f50d46f7b Mon Sep 17 00:00:00 2001 From: kianenigma Date: Fri, 4 Sep 2020 14:28:33 +0200 Subject: [PATCH 08/19] new values from the bench machine. --- frame/staking/src/benchmarking.rs | 54 ++++++++--------- frame/staking/src/default_weights.rs | 88 ++++++++++++---------------- 2 files changed, 66 insertions(+), 76 deletions(-) diff --git a/frame/staking/src/benchmarking.rs b/frame/staking/src/benchmarking.rs index 7490c53ce9bb9..91290691bc433 100644 --- a/frame/staking/src/benchmarking.rs +++ b/frame/staking/src/benchmarking.rs @@ -364,33 +364,6 @@ benchmarks! { assert!(validators.len() == v as usize); } - #[extra] - do_slash { - let l in 1 .. MAX_UNLOCKING_CHUNKS as u32; - let (stash, controller) = create_stash_controller::(0, 100)?; - let mut staking_ledger = Ledger::::get(controller.clone()).unwrap(); - let unlock_chunk = UnlockChunk::> { - value: 1.into(), - era: EraIndex::zero(), - }; - for _ in 0 .. l { - staking_ledger.unlocking.push(unlock_chunk.clone()) - } - Ledger::::insert(controller, staking_ledger); - let slash_amount = T::Currency::minimum_balance() * 10.into(); - let balance_before = T::Currency::free_balance(&stash); - }: { - crate::slashing::do_slash::( - &stash, - slash_amount, - &mut BalanceOf::::zero(), - &mut NegativeImbalanceOf::::zero() - ); - } verify { - let balance_after = T::Currency::free_balance(&stash); - assert!(balance_before > balance_after); - } - payout_all { let v in 1 .. 10; let n in 1 .. 100; @@ -429,6 +402,33 @@ benchmarks! { } } + #[extra] + do_slash { + let l in 1 .. MAX_UNLOCKING_CHUNKS as u32; + let (stash, controller) = create_stash_controller::(0, 100)?; + let mut staking_ledger = Ledger::::get(controller.clone()).unwrap(); + let unlock_chunk = UnlockChunk::> { + value: 1.into(), + era: EraIndex::zero(), + }; + for _ in 0 .. l { + staking_ledger.unlocking.push(unlock_chunk.clone()) + } + Ledger::::insert(controller, staking_ledger); + let slash_amount = T::Currency::minimum_balance() * 10.into(); + let balance_before = T::Currency::free_balance(&stash); + }: { + crate::slashing::do_slash::( + &stash, + slash_amount, + &mut BalanceOf::::zero(), + &mut NegativeImbalanceOf::::zero() + ); + } verify { + let balance_after = T::Currency::free_balance(&stash); + assert!(balance_before > balance_after); + } + // This benchmark create `v` validators intent, `n` nominators intent, in total creating `e` // edges. #[extra] diff --git a/frame/staking/src/default_weights.rs b/frame/staking/src/default_weights.rs index 49bfa695a82ba..b0b568c9108d6 100644 --- a/frame/staking/src/default_weights.rs +++ b/frame/staking/src/default_weights.rs @@ -24,108 +24,98 @@ use frame_support::weights::{Weight, constants::RocksDbWeight as DbWeight}; impl crate::WeightInfo for () { - // WARNING! Some components were not used: ["u"] fn bond() -> Weight { - (65788000 as Weight) + (148930000 as Weight) .saturating_add(DbWeight::get().reads(6 as Weight)) .saturating_add(DbWeight::get().writes(5 as Weight)) } - // WARNING! Some components were not used: ["u"] fn bond_extra() -> Weight { - (53598000 as Weight) + (113370000 as Weight) .saturating_add(DbWeight::get().reads(5 as Weight)) .saturating_add(DbWeight::get().writes(3 as Weight)) } - // WARNING! Some components were not used: ["u"] fn unbond() -> Weight { - (48977000 as Weight) + (102041000 as Weight) .saturating_add(DbWeight::get().reads(5 as Weight)) .saturating_add(DbWeight::get().writes(3 as Weight)) } fn withdraw_unbonded_update(s: u32, ) -> Weight { - (49008000 as Weight) - .saturating_add((30000 as Weight).saturating_mul(s as Weight)) + (102396000 as Weight) + .saturating_add((69000 as Weight).saturating_mul(s as Weight)) .saturating_add(DbWeight::get().reads(5 as Weight)) .saturating_add(DbWeight::get().writes(3 as Weight)) } fn withdraw_unbonded_kill(s: u32, ) -> Weight { - (69924000 as Weight) - .saturating_add((1554000 as Weight).saturating_mul(s as Weight)) + (168850000 as Weight) + .saturating_add((6968000 as Weight).saturating_mul(s as Weight)) .saturating_add(DbWeight::get().reads(7 as Weight)) .saturating_add(DbWeight::get().writes(8 as Weight)) .saturating_add(DbWeight::get().writes((1 as Weight).saturating_mul(s as Weight))) } - // WARNING! Some components were not used: ["u"] fn validate() -> Weight { - (15848000 as Weight) + (36647000 as Weight) .saturating_add(DbWeight::get().reads(2 as Weight)) .saturating_add(DbWeight::get().writes(2 as Weight)) } fn nominate(n: u32, ) -> Weight { - (20625000 as Weight) - .saturating_add((357000 as Weight).saturating_mul(n as Weight)) + (47419000 as Weight) + .saturating_add((914000 as Weight).saturating_mul(n as Weight)) .saturating_add(DbWeight::get().reads(3 as Weight)) .saturating_add(DbWeight::get().writes(2 as Weight)) } - // WARNING! Some components were not used: ["u"] fn chill() -> Weight { - (17159000 as Weight) + (36017000 as Weight) .saturating_add(DbWeight::get().reads(2 as Weight)) .saturating_add(DbWeight::get().writes(2 as Weight)) } - // WARNING! Some components were not used: ["u"] fn set_payee() -> Weight { - (10970000 as Weight) + (24557000 as Weight) .saturating_add(DbWeight::get().reads(1 as Weight)) .saturating_add(DbWeight::get().writes(1 as Weight)) } - // WARNING! Some components were not used: ["u"] fn set_controller() -> Weight { - (23826000 as Weight) + (53165000 as Weight) .saturating_add(DbWeight::get().reads(3 as Weight)) .saturating_add(DbWeight::get().writes(3 as Weight)) } // WARNING! Some components were not used: ["c"] fn set_validator_count() -> Weight { - (1962000 as Weight) + (5281000 as Weight) .saturating_add(DbWeight::get().writes(1 as Weight)) } - // WARNING! Some components were not used: ["i"] fn force_no_eras() -> Weight { - (2000000 as Weight) + (6003000 as Weight) .saturating_add(DbWeight::get().writes(1 as Weight)) } - // WARNING! Some components were not used: ["i"] fn force_new_era() -> Weight { - (2000000 as Weight) + (6043000 as Weight) .saturating_add(DbWeight::get().writes(1 as Weight)) } - // WARNING! Some components were not used: ["i"] fn force_new_era_always() -> Weight { - (2000000 as Weight) + (6096000 as Weight) .saturating_add(DbWeight::get().writes(1 as Weight)) } fn set_invulnerables(v: u32, ) -> Weight { - (1955000 as Weight) - .saturating_add((4000 as Weight).saturating_mul(v as Weight)) + (6334000 as Weight) + .saturating_add((9000 as Weight).saturating_mul(v as Weight)) .saturating_add(DbWeight::get().writes(1 as Weight)) } fn force_unstake(s: u32, ) -> Weight { - (38402000 as Weight) - .saturating_add((1711000 as Weight).saturating_mul(s as Weight)) + (120299000 as Weight) + .saturating_add((6962000 as Weight).saturating_mul(s as Weight)) .saturating_add(DbWeight::get().reads(4 as Weight)) .saturating_add(DbWeight::get().writes(8 as Weight)) .saturating_add(DbWeight::get().writes((1 as Weight).saturating_mul(s as Weight))) } fn cancel_deferred_slash(s: u32, ) -> Weight { - (410638000 as Weight) - .saturating_add((710000 as Weight).saturating_mul(s as Weight)) + (5389294000 as Weight) + .saturating_add((34593000 as Weight).saturating_mul(s as Weight)) .saturating_add(DbWeight::get().reads(1 as Weight)) .saturating_add(DbWeight::get().writes(1 as Weight)) } fn payout_stakers(n: u32, ) -> Weight { - (31790000 as Weight) - .saturating_add((40680000 as Weight).saturating_mul(n as Weight)) + (40610000 as Weight) + .saturating_add((94430000 as Weight).saturating_mul(n as Weight)) .saturating_add(DbWeight::get().reads(5 as Weight)) .saturating_add(DbWeight::get().reads((3 as Weight).saturating_mul(n as Weight))) .saturating_add(DbWeight::get().writes(1 as Weight)) @@ -133,35 +123,35 @@ impl crate::WeightInfo for () { } fn payout_stakers_alive_controller(n: u32, ) -> Weight { (0 as Weight) - .saturating_add((55362000 as Weight).saturating_mul(n as Weight)) + .saturating_add((119601000 as Weight).saturating_mul(n as Weight)) .saturating_add(DbWeight::get().reads(1 as Weight)) .saturating_add(DbWeight::get().reads((5 as Weight).saturating_mul(n as Weight))) .saturating_add(DbWeight::get().writes((3 as Weight).saturating_mul(n as Weight))) } fn rebond(l: u32, ) -> Weight { - (35025000 as Weight) - .saturating_add((28000 as Weight).saturating_mul(l as Weight)) + (71994000 as Weight) + .saturating_add((167000 as Weight).saturating_mul(l as Weight)) .saturating_add(DbWeight::get().reads(4 as Weight)) .saturating_add(DbWeight::get().writes(3 as Weight)) } fn set_history_depth(e: u32, ) -> Weight { (0 as Weight) - .saturating_add((43889000 as Weight).saturating_mul(e as Weight)) + .saturating_add((53251000 as Weight).saturating_mul(e as Weight)) .saturating_add(DbWeight::get().reads(2 as Weight)) .saturating_add(DbWeight::get().writes(4 as Weight)) .saturating_add(DbWeight::get().writes((7 as Weight).saturating_mul(e as Weight))) } fn reap_stash(s: u32, ) -> Weight { - (69698000 as Weight) - .saturating_add((1680000 as Weight).saturating_mul(s as Weight)) + (149165000 as Weight) + .saturating_add((6980000 as Weight).saturating_mul(s as Weight)) .saturating_add(DbWeight::get().reads(4 as Weight)) .saturating_add(DbWeight::get().writes(8 as Weight)) .saturating_add(DbWeight::get().writes((1 as Weight).saturating_mul(s as Weight))) } fn new_era(v: u32, n: u32, ) -> Weight { (0 as Weight) - .saturating_add((462131000 as Weight).saturating_mul(v as Weight)) - .saturating_add((75480000 as Weight).saturating_mul(n as Weight)) + .saturating_add((1470601000 as Weight).saturating_mul(v as Weight)) + .saturating_add((187200000 as Weight).saturating_mul(n as Weight)) .saturating_add(DbWeight::get().reads(10 as Weight)) .saturating_add(DbWeight::get().reads((4 as Weight).saturating_mul(v as Weight))) .saturating_add(DbWeight::get().reads((3 as Weight).saturating_mul(n as Weight))) @@ -170,8 +160,8 @@ impl crate::WeightInfo for () { } fn payout_all(v: u32, n: u32, ) -> Weight { (0 as Weight) - .saturating_add((1841741000 as Weight).saturating_mul(v as Weight)) - .saturating_add((181891000 as Weight).saturating_mul(n as Weight)) + .saturating_add((6796630000 as Weight).saturating_mul(v as Weight)) + .saturating_add((582776000 as Weight).saturating_mul(n as Weight)) .saturating_add(DbWeight::get().reads((15 as Weight).saturating_mul(v as Weight))) .saturating_add(DbWeight::get().reads((5 as Weight).saturating_mul(n as Weight))) .saturating_add(DbWeight::get().writes((8 as Weight).saturating_mul(v as Weight))) @@ -179,10 +169,10 @@ impl crate::WeightInfo for () { } fn submit_solution_better(v: u32, n: u32, a: u32, w: u32, ) -> Weight { (0 as Weight) - .saturating_add((762000 as Weight).saturating_mul(v as Weight)) - .saturating_add((868000 as Weight).saturating_mul(n as Weight)) - .saturating_add((66802000 as Weight).saturating_mul(a as Weight)) - .saturating_add((6474000 as Weight).saturating_mul(w as Weight)) + .saturating_add((1488000 as Weight).saturating_mul(v as Weight)) + .saturating_add((719000 as Weight).saturating_mul(n as Weight)) + .saturating_add((211407000 as Weight).saturating_mul(a as Weight)) + .saturating_add((10786000 as Weight).saturating_mul(w as Weight)) .saturating_add(DbWeight::get().reads(6 as Weight)) .saturating_add(DbWeight::get().reads((4 as Weight).saturating_mul(a as Weight))) .saturating_add(DbWeight::get().reads((1 as Weight).saturating_mul(w as Weight))) From 8521ab07b4994ef802ba4cdc737a92c18cb325bf Mon Sep 17 00:00:00 2001 From: kianenigma Date: Tue, 8 Sep 2020 15:18:57 +0200 Subject: [PATCH 09/19] Fix some grumbles --- frame/staking/src/benchmarking.rs | 36 +++++++++++++++++++----------- frame/staking/src/lib.rs | 8 +++---- frame/staking/src/testing_utils.rs | 8 +++++-- 3 files changed, 33 insertions(+), 19 deletions(-) diff --git a/frame/staking/src/benchmarking.rs b/frame/staking/src/benchmarking.rs index 91290691bc433..a15fa679dd518 100644 --- a/frame/staking/src/benchmarking.rs +++ b/frame/staking/src/benchmarking.rs @@ -29,6 +29,13 @@ const MAX_SPANS: u32 = 100; const MAX_VALIDATORS: u32 = 1000; const MAX_SLASHES: u32 = 1000; +macro_rules! do_whitelist { + ($acc:ident) => { + let caller_key = frame_system::Account::::hashed_key_for(&$acc); + frame_benchmarking::benchmarking::add_to_whitelist(caller_key.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) { @@ -111,6 +118,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); }: _(RawOrigin::Signed(stash.clone()), controller_lookup, amount, reward_destination) verify { assert!(Bonded::::contains_key(stash)); @@ -122,6 +130,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); }: _(RawOrigin::Signed(stash), max_additional) verify { let ledger = Ledger::::get(&controller).ok_or("ledger not created after")?; @@ -134,6 +143,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); }: _(RawOrigin::Signed(controller.clone()), amount) verify { let ledger = Ledger::::get(&controller).ok_or("ledger not created after")?; @@ -152,6 +162,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); }: withdraw_unbonded(RawOrigin::Signed(controller.clone()), s) verify { let ledger = Ledger::::get(&controller).ok_or("ledger not created after")?; @@ -170,6 +181,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); }: withdraw_unbonded(RawOrigin::Signed(controller.clone()), s) verify { assert!(!Ledger::::contains_key(controller)); @@ -178,6 +190,7 @@ benchmarks! { validate { let (stash, controller) = create_stash_controller::(USER_SEED, 100)?; let prefs = ValidatorPrefs::default(); + do_whitelist!(controller); }: _(RawOrigin::Signed(controller), prefs) verify { assert!(Validators::::contains_key(stash)); @@ -188,6 +201,7 @@ benchmarks! { let n in 1 .. MAX_NOMINATIONS as u32; let (stash, controller) = create_stash_controller::(n + 1, 100)?; let validators = create_validators::(n, 100)?; + do_whitelist!(controller); }: _(RawOrigin::Signed(controller), validators) verify { assert!(Nominators::::contains_key(stash)); @@ -195,11 +209,13 @@ benchmarks! { chill { let (_, controller) = create_stash_controller::(USER_SEED, 100)?; + do_whitelist!(controller); }: _(RawOrigin::Signed(controller)) set_payee { let (stash, controller) = create_stash_controller::(USER_SEED, 100)?; assert_eq!(Payee::::get(&stash), RewardDestination::Staked); + do_whitelist!(controller); }: _(RawOrigin::Signed(controller), RewardDestination::Controller) verify { assert_eq!(Payee::::get(&stash), RewardDestination::Controller); @@ -209,6 +225,7 @@ benchmarks! { let (stash, _) = create_stash_controller::(USER_SEED, 100)?; let new_controller = create_funded_user::("new_controller", USER_SEED, 100); let new_controller_lookup = T::Lookup::unlookup(new_controller.clone()); + do_whitelist!(stash); }: _(RawOrigin::Signed(stash), new_controller_lookup) verify { assert!(Ledger::::contains_key(&new_controller)); @@ -318,6 +335,7 @@ benchmarks! { } Ledger::::insert(controller.clone(), staking_ledger.clone()); let original_bonded: BalanceOf = staking_ledger.active; + do_whitelist!(controller); }: _(RawOrigin::Signed(controller.clone()), (l + 100).into()) verify { let ledger = Ledger::::get(&controller).ok_or("ledger not created after")?; @@ -348,6 +366,7 @@ benchmarks! { let (stash, controller) = create_stash_controller::(0, 100)?; add_slashing_spans::(&stash, s); T::Currency::make_free_balance_be(&stash, 0.into()); + do_whitelist!(controller); }: _(RawOrigin::Signed(controller), stash.clone(), s) verify { assert!(!Bonded::::contains_key(&stash)); @@ -481,10 +500,7 @@ benchmarks! { let era = >::current_era().unwrap_or(0); let caller: T::AccountId = account("caller", n, SEED); - - // Whitelist caller account from further DB operations. - let caller_key = frame_system::Account::::hashed_key_for(&caller); - frame_benchmarking::benchmarking::add_to_whitelist(caller_key.into()); + do_whitelist!(caller); }: { let result = >::submit_election_solution( RawOrigin::Signed(caller.clone()).into(), @@ -553,10 +569,7 @@ benchmarks! { let era = >::current_era().unwrap_or(0); let caller: T::AccountId = account("caller", n, SEED); - - // Whitelist caller account from further DB operations. - let caller_key = frame_system::Account::::hashed_key_for(&caller); - frame_benchmarking::benchmarking::add_to_whitelist(caller_key.into()); + do_whitelist!(caller); // submit a very bad solution on-chain { @@ -608,12 +621,9 @@ benchmarks! { // needed for the solution to be accepted >::put(ElectionStatus::Open(T::BlockNumber::from(1u32))); - let caller: T::AccountId = account("caller", n, SEED); let era = >::current_era().unwrap_or(0); - - // Whitelist caller account from further DB operations. - let caller_key = frame_system::Account::::hashed_key_for(&caller); - frame_benchmarking::benchmarking::add_to_whitelist(caller_key.into()); + let caller: T::AccountId = account("caller", n, SEED); + do_whitelist!(caller); // submit a seq-phragmen with all the good stuff on chain. { diff --git a/frame/staking/src/lib.rs b/frame/staking/src/lib.rs index a8a5b253fca19..19631bf58279f 100644 --- a/frame/staking/src/lib.rs +++ b/frame/staking/src/lib.rs @@ -2097,8 +2097,8 @@ decl_module! { /// - Worse solution is retraced in pre-dispatch-checks which sets its own weight. /// # #[weight = T::WeightInfo::submit_solution_better( - size.validators as u32, - size.nominators as u32, + size.validators.into(), + size.nominators.into(), compact.len() as u32, winners.len() as u32, )] @@ -2131,8 +2131,8 @@ decl_module! { /// See `crate::weight` module. /// # #[weight = T::WeightInfo::submit_solution_better( - size.validators as u32, - size.nominators as u32, + size.validators.into(), + size.nominators.into(), compact.len() as u32, winners.len() as u32, )] diff --git a/frame/staking/src/testing_utils.rs b/frame/staking/src/testing_utils.rs index 02acd135e63e4..cacad27070084 100644 --- a/frame/staking/src/testing_utils.rs +++ b/frame/staking/src/testing_utils.rs @@ -20,7 +20,7 @@ use crate::*; use crate::Module as Staking; -use frame_benchmarking::{account}; +use frame_benchmarking::account; use frame_system::RawOrigin; use sp_io::hashing::blake2_256; use rand_chacha::{rand_core::{RngCore, SeedableRng}, ChaChaRng}; @@ -29,7 +29,11 @@ use sp_npos_elections::*; const SEED: u32 = 0; /// Grab a funded user. -pub fn create_funded_user(string: &'static str, n: u32, balance_factor: u32) -> T::AccountId { +pub fn create_funded_user( + string: &'static str, + n: u32, + balance_factor: u32, +) -> T::AccountId { let user = account(string, n, SEED); let balance = T::Currency::minimum_balance() * balance_factor.into(); T::Currency::make_free_balance_be(&user, balance); From cd310221486ef1916cdd35543f6adf1aedd36638 Mon Sep 17 00:00:00 2001 From: kianenigma Date: Wed, 9 Sep 2020 09:33:26 +0200 Subject: [PATCH 10/19] better macro --- frame/staking/src/benchmarking.rs | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/frame/staking/src/benchmarking.rs b/frame/staking/src/benchmarking.rs index a15fa679dd518..01af555b03c1b 100644 --- a/frame/staking/src/benchmarking.rs +++ b/frame/staking/src/benchmarking.rs @@ -31,8 +31,9 @@ const MAX_SLASHES: u32 = 1000; macro_rules! do_whitelist { ($acc:ident) => { - let caller_key = frame_system::Account::::hashed_key_for(&$acc); - frame_benchmarking::benchmarking::add_to_whitelist(caller_key.into()); + frame_benchmarking::benchmarking::add_to_whitelist( + frame_system::Account::::hashed_key_for(&$acc).into() + ); } } From 6f98b80346960e45659c7491f4d273e382837849 Mon Sep 17 00:00:00 2001 From: kianenigma Date: Wed, 9 Sep 2020 17:06:00 +0200 Subject: [PATCH 11/19] Some better doc --- frame/staking/src/lib.rs | 10 ++++++---- 1 file changed, 6 insertions(+), 4 deletions(-) diff --git a/frame/staking/src/lib.rs b/frame/staking/src/lib.rs index 19631bf58279f..1c7c828e99ac2 100644 --- a/frame/staking/src/lib.rs +++ b/frame/staking/src/lib.rs @@ -1514,8 +1514,8 @@ decl_module! { /// ---------- /// Base Weight: 50.34 µs /// DB Weight: - /// - Read: Era Election Status, Ledger, Current Era, Locks, [Origin Account] - /// - Write: [Origin Account], Locks, Ledger + /// - Read: EraElectionStatus, Ledger, CurrentEra, Locks, BalanceOf Stash, + /// - Write: Locks, Ledger, BalanceOf Stash, /// #[weight = T::WeightInfo::unbond()] fn unbond(origin, #[compact] value: BalanceOf) { @@ -1571,8 +1571,10 @@ decl_module! { /// - Reads: EraElectionStatus, Ledger, Current Era, Locks, [Origin Account] /// - Writes: [Origin Account], Locks, Ledger /// Kill: 79.41 + 2.366 * S µs - /// - Reads: EraElectionStatus, Ledger, Current Era, Bonded, Slashing Spans, [Origin Account], Locks - /// - Writes: Bonded, Slashing Spans (if S > 0), Ledger, Payee, Validators, Nominators, [Origin Account], Locks + /// - Reads: EraElectionStatus, Ledger, Current Era, Bonded, Slashing Spans, [Origin + /// Account], Locks, BalanceOf stash + /// - Writes: Bonded, Slashing Spans (if S > 0), Ledger, Payee, Validators, Nominators, + /// [Origin Account], Locks, BalanceOf stash. /// - Writes Each: SpanSlash * S /// NOTE: Weight annotation is the kill scenario, we refund otherwise. /// # From 58720e09009143c1fbc10912f778165cb4e019ee Mon Sep 17 00:00:00 2001 From: kianenigma Date: Wed, 9 Sep 2020 17:29:02 +0200 Subject: [PATCH 12/19] Move to interpreted wasm --- frame/staking/src/benchmarking.rs | 1 - frame/staking/src/default_weights.rs | 91 ++++++++++++---------------- 2 files changed, 40 insertions(+), 52 deletions(-) diff --git a/frame/staking/src/benchmarking.rs b/frame/staking/src/benchmarking.rs index 01af555b03c1b..a39837712abe7 100644 --- a/frame/staking/src/benchmarking.rs +++ b/frame/staking/src/benchmarking.rs @@ -519,7 +519,6 @@ benchmarks! { } // same as submit_solution_initial but we place a very weak solution on chian first. - #[extra] submit_solution_better { // number of validator intention. let v in 200 .. 400; diff --git a/frame/staking/src/default_weights.rs b/frame/staking/src/default_weights.rs index b0b568c9108d6..f05c3b05aa045 100644 --- a/frame/staking/src/default_weights.rs +++ b/frame/staking/src/default_weights.rs @@ -24,98 +24,98 @@ use frame_support::weights::{Weight, constants::RocksDbWeight as DbWeight}; impl crate::WeightInfo for () { - fn bond() -> Weight { - (148930000 as Weight) - .saturating_add(DbWeight::get().reads(6 as Weight)) - .saturating_add(DbWeight::get().writes(5 as Weight)) + fn bond() -> Weight { + (412198000 as Weight) + .saturating_add(DbWeight::get().reads(5 as Weight)) + .saturating_add(DbWeight::get().writes(4 as Weight)) } fn bond_extra() -> Weight { - (113370000 as Weight) - .saturating_add(DbWeight::get().reads(5 as Weight)) - .saturating_add(DbWeight::get().writes(3 as Weight)) + (317637000 as Weight) + .saturating_add(DbWeight::get().reads(4 as Weight)) + .saturating_add(DbWeight::get().writes(2 as Weight)) } fn unbond() -> Weight { - (102041000 as Weight) + (277418000 as Weight) .saturating_add(DbWeight::get().reads(5 as Weight)) .saturating_add(DbWeight::get().writes(3 as Weight)) } fn withdraw_unbonded_update(s: u32, ) -> Weight { - (102396000 as Weight) - .saturating_add((69000 as Weight).saturating_mul(s as Weight)) + (282986000 as Weight) + .saturating_add((45000 as Weight).saturating_mul(s as Weight)) .saturating_add(DbWeight::get().reads(5 as Weight)) .saturating_add(DbWeight::get().writes(3 as Weight)) } fn withdraw_unbonded_kill(s: u32, ) -> Weight { - (168850000 as Weight) - .saturating_add((6968000 as Weight).saturating_mul(s as Weight)) + (471338000 as Weight) + .saturating_add((20118000 as Weight).saturating_mul(s as Weight)) .saturating_add(DbWeight::get().reads(7 as Weight)) .saturating_add(DbWeight::get().writes(8 as Weight)) .saturating_add(DbWeight::get().writes((1 as Weight).saturating_mul(s as Weight))) } fn validate() -> Weight { - (36647000 as Weight) + (99599000 as Weight) .saturating_add(DbWeight::get().reads(2 as Weight)) .saturating_add(DbWeight::get().writes(2 as Weight)) } fn nominate(n: u32, ) -> Weight { - (47419000 as Weight) - .saturating_add((914000 as Weight).saturating_mul(n as Weight)) + (125965000 as Weight) + .saturating_add((5544000 as Weight).saturating_mul(n as Weight)) .saturating_add(DbWeight::get().reads(3 as Weight)) .saturating_add(DbWeight::get().writes(2 as Weight)) } fn chill() -> Weight { - (36017000 as Weight) + (97291000 as Weight) .saturating_add(DbWeight::get().reads(2 as Weight)) .saturating_add(DbWeight::get().writes(2 as Weight)) } fn set_payee() -> Weight { - (24557000 as Weight) + (73810000 as Weight) .saturating_add(DbWeight::get().reads(1 as Weight)) .saturating_add(DbWeight::get().writes(1 as Weight)) } fn set_controller() -> Weight { - (53165000 as Weight) + (153200000 as Weight) .saturating_add(DbWeight::get().reads(3 as Weight)) .saturating_add(DbWeight::get().writes(3 as Weight)) } // WARNING! Some components were not used: ["c"] fn set_validator_count() -> Weight { - (5281000 as Weight) + (10913000 as Weight) .saturating_add(DbWeight::get().writes(1 as Weight)) } fn force_no_eras() -> Weight { - (6003000 as Weight) + (13148000 as Weight) .saturating_add(DbWeight::get().writes(1 as Weight)) } fn force_new_era() -> Weight { - (6043000 as Weight) + (13215000 as Weight) .saturating_add(DbWeight::get().writes(1 as Weight)) } fn force_new_era_always() -> Weight { - (6096000 as Weight) + (13150000 as Weight) .saturating_add(DbWeight::get().writes(1 as Weight)) } fn set_invulnerables(v: u32, ) -> Weight { - (6334000 as Weight) - .saturating_add((9000 as Weight).saturating_mul(v as Weight)) + (14525000 as Weight) + .saturating_add((224000 as Weight).saturating_mul(v as Weight)) .saturating_add(DbWeight::get().writes(1 as Weight)) } fn force_unstake(s: u32, ) -> Weight { - (120299000 as Weight) - .saturating_add((6962000 as Weight).saturating_mul(s as Weight)) + (345510000 as Weight) + .saturating_add((20063000 as Weight).saturating_mul(s as Weight)) .saturating_add(DbWeight::get().reads(4 as Weight)) .saturating_add(DbWeight::get().writes(8 as Weight)) .saturating_add(DbWeight::get().writes((1 as Weight).saturating_mul(s as Weight))) } fn cancel_deferred_slash(s: u32, ) -> Weight { - (5389294000 as Weight) - .saturating_add((34593000 as Weight).saturating_mul(s as Weight)) + (260000722000 as Weight) + .saturating_add((1678139000 as Weight).saturating_mul(s as Weight)) .saturating_add(DbWeight::get().reads(1 as Weight)) .saturating_add(DbWeight::get().writes(1 as Weight)) } fn payout_stakers(n: u32, ) -> Weight { - (40610000 as Weight) - .saturating_add((94430000 as Weight).saturating_mul(n as Weight)) + (65430000 as Weight) + .saturating_add((275913000 as Weight).saturating_mul(n as Weight)) .saturating_add(DbWeight::get().reads(5 as Weight)) .saturating_add(DbWeight::get().reads((3 as Weight).saturating_mul(n as Weight))) .saturating_add(DbWeight::get().writes(1 as Weight)) @@ -123,35 +123,35 @@ impl crate::WeightInfo for () { } fn payout_stakers_alive_controller(n: u32, ) -> Weight { (0 as Weight) - .saturating_add((119601000 as Weight).saturating_mul(n as Weight)) + .saturating_add((373671000 as Weight).saturating_mul(n as Weight)) .saturating_add(DbWeight::get().reads(1 as Weight)) .saturating_add(DbWeight::get().reads((5 as Weight).saturating_mul(n as Weight))) .saturating_add(DbWeight::get().writes((3 as Weight).saturating_mul(n as Weight))) } fn rebond(l: u32, ) -> Weight { - (71994000 as Weight) - .saturating_add((167000 as Weight).saturating_mul(l as Weight)) + (209660000 as Weight) + .saturating_add((2307000 as Weight).saturating_mul(l as Weight)) .saturating_add(DbWeight::get().reads(4 as Weight)) .saturating_add(DbWeight::get().writes(3 as Weight)) } fn set_history_depth(e: u32, ) -> Weight { (0 as Weight) - .saturating_add((53251000 as Weight).saturating_mul(e as Weight)) + .saturating_add((92748000 as Weight).saturating_mul(e as Weight)) .saturating_add(DbWeight::get().reads(2 as Weight)) .saturating_add(DbWeight::get().writes(4 as Weight)) .saturating_add(DbWeight::get().writes((7 as Weight).saturating_mul(e as Weight))) } fn reap_stash(s: u32, ) -> Weight { - (149165000 as Weight) - .saturating_add((6980000 as Weight).saturating_mul(s as Weight)) + (396133000 as Weight) + .saturating_add((19951000 as Weight).saturating_mul(s as Weight)) .saturating_add(DbWeight::get().reads(4 as Weight)) .saturating_add(DbWeight::get().writes(8 as Weight)) .saturating_add(DbWeight::get().writes((1 as Weight).saturating_mul(s as Weight))) } fn new_era(v: u32, n: u32, ) -> Weight { (0 as Weight) - .saturating_add((1470601000 as Weight).saturating_mul(v as Weight)) - .saturating_add((187200000 as Weight).saturating_mul(n as Weight)) + .saturating_add((20017645000 as Weight).saturating_mul(v as Weight)) + .saturating_add((1812349000 as Weight).saturating_mul(n as Weight)) .saturating_add(DbWeight::get().reads(10 as Weight)) .saturating_add(DbWeight::get().reads((4 as Weight).saturating_mul(v as Weight))) .saturating_add(DbWeight::get().reads((3 as Weight).saturating_mul(n as Weight))) @@ -160,22 +160,11 @@ impl crate::WeightInfo for () { } fn payout_all(v: u32, n: u32, ) -> Weight { (0 as Weight) - .saturating_add((6796630000 as Weight).saturating_mul(v as Weight)) - .saturating_add((582776000 as Weight).saturating_mul(n as Weight)) + .saturating_add((24211976000 as Weight).saturating_mul(v as Weight)) + .saturating_add((2022572000 as Weight).saturating_mul(n as Weight)) .saturating_add(DbWeight::get().reads((15 as Weight).saturating_mul(v as Weight))) .saturating_add(DbWeight::get().reads((5 as Weight).saturating_mul(n as Weight))) .saturating_add(DbWeight::get().writes((8 as Weight).saturating_mul(v as Weight))) .saturating_add(DbWeight::get().writes((3 as Weight).saturating_mul(n as Weight))) } - fn submit_solution_better(v: u32, n: u32, a: u32, w: u32, ) -> Weight { - (0 as Weight) - .saturating_add((1488000 as Weight).saturating_mul(v as Weight)) - .saturating_add((719000 as Weight).saturating_mul(n as Weight)) - .saturating_add((211407000 as Weight).saturating_mul(a as Weight)) - .saturating_add((10786000 as Weight).saturating_mul(w as Weight)) - .saturating_add(DbWeight::get().reads(6 as Weight)) - .saturating_add(DbWeight::get().reads((4 as Weight).saturating_mul(a as Weight))) - .saturating_add(DbWeight::get().reads((1 as Weight).saturating_mul(w as Weight))) - .saturating_add(DbWeight::get().writes(2 as Weight)) - } } From b9bd6eae398d3298598a4ecb25c614151582eae6 Mon Sep 17 00:00:00 2001 From: kianenigma Date: Wed, 9 Sep 2020 17:29:51 +0200 Subject: [PATCH 13/19] Make it work temporarily --- frame/staking/src/default_weights.rs | 2 ++ 1 file changed, 2 insertions(+) diff --git a/frame/staking/src/default_weights.rs b/frame/staking/src/default_weights.rs index f05c3b05aa045..1bda2ec24d5b2 100644 --- a/frame/staking/src/default_weights.rs +++ b/frame/staking/src/default_weights.rs @@ -167,4 +167,6 @@ impl crate::WeightInfo for () { .saturating_add(DbWeight::get().writes((8 as Weight).saturating_mul(v as Weight))) .saturating_add(DbWeight::get().writes((3 as Weight).saturating_mul(n as Weight))) } + + fn submit_solution_better(v: u32, n: u32, a: u32, w: u32, ) -> Weight { 0 } } From ac745526290489d53ecdd4e418e86a921d33fa15 Mon Sep 17 00:00:00 2001 From: kianenigma Date: Thu, 10 Sep 2020 13:53:50 +0200 Subject: [PATCH 14/19] Final fix of default ones. --- frame/staking/src/default_weights.rs | 85 +++++++++++++++------------- 1 file changed, 47 insertions(+), 38 deletions(-) diff --git a/frame/staking/src/default_weights.rs b/frame/staking/src/default_weights.rs index 1bda2ec24d5b2..6e7283ea708a2 100644 --- a/frame/staking/src/default_weights.rs +++ b/frame/staking/src/default_weights.rs @@ -24,98 +24,98 @@ use frame_support::weights::{Weight, constants::RocksDbWeight as DbWeight}; impl crate::WeightInfo for () { - fn bond() -> Weight { - (412198000 as Weight) + fn bond() -> Weight { + (408578000 as Weight) .saturating_add(DbWeight::get().reads(5 as Weight)) .saturating_add(DbWeight::get().writes(4 as Weight)) } fn bond_extra() -> Weight { - (317637000 as Weight) + (316693000 as Weight) .saturating_add(DbWeight::get().reads(4 as Weight)) .saturating_add(DbWeight::get().writes(2 as Weight)) } fn unbond() -> Weight { - (277418000 as Weight) + (276194000 as Weight) .saturating_add(DbWeight::get().reads(5 as Weight)) .saturating_add(DbWeight::get().writes(3 as Weight)) } fn withdraw_unbonded_update(s: u32, ) -> Weight { - (282986000 as Weight) - .saturating_add((45000 as Weight).saturating_mul(s as Weight)) + (281330000 as Weight) + .saturating_add((46000 as Weight).saturating_mul(s as Weight)) .saturating_add(DbWeight::get().reads(5 as Weight)) .saturating_add(DbWeight::get().writes(3 as Weight)) } fn withdraw_unbonded_kill(s: u32, ) -> Weight { - (471338000 as Weight) - .saturating_add((20118000 as Weight).saturating_mul(s as Weight)) + (474310000 as Weight) + .saturating_add((19672000 as Weight).saturating_mul(s as Weight)) .saturating_add(DbWeight::get().reads(7 as Weight)) .saturating_add(DbWeight::get().writes(8 as Weight)) .saturating_add(DbWeight::get().writes((1 as Weight).saturating_mul(s as Weight))) } fn validate() -> Weight { - (99599000 as Weight) + (99283000 as Weight) .saturating_add(DbWeight::get().reads(2 as Weight)) .saturating_add(DbWeight::get().writes(2 as Weight)) } fn nominate(n: u32, ) -> Weight { - (125965000 as Weight) - .saturating_add((5544000 as Weight).saturating_mul(n as Weight)) + (126479000 as Weight) + .saturating_add((5329000 as Weight).saturating_mul(n as Weight)) .saturating_add(DbWeight::get().reads(3 as Weight)) .saturating_add(DbWeight::get().writes(2 as Weight)) } fn chill() -> Weight { - (97291000 as Weight) + (97447000 as Weight) .saturating_add(DbWeight::get().reads(2 as Weight)) .saturating_add(DbWeight::get().writes(2 as Weight)) } fn set_payee() -> Weight { - (73810000 as Weight) + (74199000 as Weight) .saturating_add(DbWeight::get().reads(1 as Weight)) .saturating_add(DbWeight::get().writes(1 as Weight)) } fn set_controller() -> Weight { - (153200000 as Weight) + (153911000 as Weight) .saturating_add(DbWeight::get().reads(3 as Weight)) .saturating_add(DbWeight::get().writes(3 as Weight)) } // WARNING! Some components were not used: ["c"] fn set_validator_count() -> Weight { - (10913000 as Weight) + (10890000 as Weight) .saturating_add(DbWeight::get().writes(1 as Weight)) } fn force_no_eras() -> Weight { - (13148000 as Weight) + (13143000 as Weight) .saturating_add(DbWeight::get().writes(1 as Weight)) } fn force_new_era() -> Weight { - (13215000 as Weight) + (13112000 as Weight) .saturating_add(DbWeight::get().writes(1 as Weight)) } fn force_new_era_always() -> Weight { - (13150000 as Weight) + (13108000 as Weight) .saturating_add(DbWeight::get().writes(1 as Weight)) } fn set_invulnerables(v: u32, ) -> Weight { - (14525000 as Weight) - .saturating_add((224000 as Weight).saturating_mul(v as Weight)) + (14140000 as Weight) + .saturating_add((218000 as Weight).saturating_mul(v as Weight)) .saturating_add(DbWeight::get().writes(1 as Weight)) } fn force_unstake(s: u32, ) -> Weight { - (345510000 as Weight) - .saturating_add((20063000 as Weight).saturating_mul(s as Weight)) + (344355000 as Weight) + .saturating_add((19704000 as Weight).saturating_mul(s as Weight)) .saturating_add(DbWeight::get().reads(4 as Weight)) .saturating_add(DbWeight::get().writes(8 as Weight)) .saturating_add(DbWeight::get().writes((1 as Weight).saturating_mul(s as Weight))) } fn cancel_deferred_slash(s: u32, ) -> Weight { - (260000722000 as Weight) - .saturating_add((1678139000 as Weight).saturating_mul(s as Weight)) + (260009398000 as Weight) + .saturating_add((1675144000 as Weight).saturating_mul(s as Weight)) .saturating_add(DbWeight::get().reads(1 as Weight)) .saturating_add(DbWeight::get().writes(1 as Weight)) } fn payout_stakers(n: u32, ) -> Weight { - (65430000 as Weight) - .saturating_add((275913000 as Weight).saturating_mul(n as Weight)) + (71905000 as Weight) + .saturating_add((274832000 as Weight).saturating_mul(n as Weight)) .saturating_add(DbWeight::get().reads(5 as Weight)) .saturating_add(DbWeight::get().reads((3 as Weight).saturating_mul(n as Weight))) .saturating_add(DbWeight::get().writes(1 as Weight)) @@ -123,35 +123,35 @@ impl crate::WeightInfo for () { } fn payout_stakers_alive_controller(n: u32, ) -> Weight { (0 as Weight) - .saturating_add((373671000 as Weight).saturating_mul(n as Weight)) + .saturating_add((371585000 as Weight).saturating_mul(n as Weight)) .saturating_add(DbWeight::get().reads(1 as Weight)) .saturating_add(DbWeight::get().reads((5 as Weight).saturating_mul(n as Weight))) .saturating_add(DbWeight::get().writes((3 as Weight).saturating_mul(n as Weight))) } fn rebond(l: u32, ) -> Weight { - (209660000 as Weight) - .saturating_add((2307000 as Weight).saturating_mul(l as Weight)) + (208619000 as Weight) + .saturating_add((2271000 as Weight).saturating_mul(l as Weight)) .saturating_add(DbWeight::get().reads(4 as Weight)) .saturating_add(DbWeight::get().writes(3 as Weight)) } fn set_history_depth(e: u32, ) -> Weight { (0 as Weight) - .saturating_add((92748000 as Weight).saturating_mul(e as Weight)) + .saturating_add((93111000 as Weight).saturating_mul(e as Weight)) .saturating_add(DbWeight::get().reads(2 as Weight)) .saturating_add(DbWeight::get().writes(4 as Weight)) .saturating_add(DbWeight::get().writes((7 as Weight).saturating_mul(e as Weight))) } fn reap_stash(s: u32, ) -> Weight { - (396133000 as Weight) - .saturating_add((19951000 as Weight).saturating_mul(s as Weight)) + (394410000 as Weight) + .saturating_add((19600000 as Weight).saturating_mul(s as Weight)) .saturating_add(DbWeight::get().reads(4 as Weight)) .saturating_add(DbWeight::get().writes(8 as Weight)) .saturating_add(DbWeight::get().writes((1 as Weight).saturating_mul(s as Weight))) } fn new_era(v: u32, n: u32, ) -> Weight { (0 as Weight) - .saturating_add((20017645000 as Weight).saturating_mul(v as Weight)) - .saturating_add((1812349000 as Weight).saturating_mul(n as Weight)) + .saturating_add((19861198000 as Weight).saturating_mul(v as Weight)) + .saturating_add((1792877000 as Weight).saturating_mul(n as Weight)) .saturating_add(DbWeight::get().reads(10 as Weight)) .saturating_add(DbWeight::get().reads((4 as Weight).saturating_mul(v as Weight))) .saturating_add(DbWeight::get().reads((3 as Weight).saturating_mul(n as Weight))) @@ -160,13 +160,22 @@ impl crate::WeightInfo for () { } fn payout_all(v: u32, n: u32, ) -> Weight { (0 as Weight) - .saturating_add((24211976000 as Weight).saturating_mul(v as Weight)) - .saturating_add((2022572000 as Weight).saturating_mul(n as Weight)) + .saturating_add((23897487000 as Weight).saturating_mul(v as Weight)) + .saturating_add((1997765000 as Weight).saturating_mul(n as Weight)) .saturating_add(DbWeight::get().reads((15 as Weight).saturating_mul(v as Weight))) .saturating_add(DbWeight::get().reads((5 as Weight).saturating_mul(n as Weight))) .saturating_add(DbWeight::get().writes((8 as Weight).saturating_mul(v as Weight))) .saturating_add(DbWeight::get().writes((3 as Weight).saturating_mul(n as Weight))) } - - fn submit_solution_better(v: u32, n: u32, a: u32, w: u32, ) -> Weight { 0 } + fn submit_solution_better(v: u32, n: u32, a: u32, w: u32, ) -> Weight { + (1278000000 as Weight) + .saturating_add((10599000 as Weight).saturating_mul(v as Weight)) + .saturating_add((5384000 as Weight).saturating_mul(n as Weight)) + .saturating_add((1016715000 as Weight).saturating_mul(a as Weight)) + .saturating_add((113107000 as Weight).saturating_mul(w as Weight)) + .saturating_add(DbWeight::get().reads(6 as Weight)) + .saturating_add(DbWeight::get().reads((4 as Weight).saturating_mul(a as Weight))) + .saturating_add(DbWeight::get().reads((1 as Weight).saturating_mul(w as Weight))) + .saturating_add(DbWeight::get().writes(2 as Weight)) + } } From 3751af5eb7fe96d2545d1aebde7e08e960af9fa2 Mon Sep 17 00:00:00 2001 From: kianenigma Date: Fri, 11 Sep 2020 09:32:26 +0200 Subject: [PATCH 15/19] Fix payout benchmarks --- frame/session/benchmarking/src/lib.rs | 11 +++-- frame/staking/src/benchmarking.rs | 66 +++++++++++++++++---------- frame/staking/src/testing_utils.rs | 23 ++++++---- 3 files changed, 64 insertions(+), 36 deletions(-) diff --git a/frame/session/benchmarking/src/lib.rs b/frame/session/benchmarking/src/lib.rs index cc471893356d5..ee66223fc0b04 100644 --- a/frame/session/benchmarking/src/lib.rs +++ b/frame/session/benchmarking/src/lib.rs @@ -35,7 +35,7 @@ use frame_system::RawOrigin; use pallet_session::{historical::Module as Historical, Module as Session, *}; use pallet_staking::{ benchmarking::create_validator_with_nominators, testing_utils::create_validators, - MAX_NOMINATIONS, + MAX_NOMINATIONS, RewardDestination, }; use sp_runtime::traits::{One, StaticLookup}; @@ -55,7 +55,12 @@ benchmarks! { set_keys { let n in 1 .. MAX_NOMINATIONS as u32; - let v_stash = create_validator_with_nominators::(n, MAX_NOMINATIONS as u32, false)?; + let v_stash = create_validator_with_nominators::( + n, + MAX_NOMINATIONS as u32, + false, + RewardDestination::Staked, + )?; let v_controller = pallet_staking::Module::::bonded(&v_stash).ok_or("not stash")?; let keys = T::Keys::default(); let proof: Vec = vec![0,1,2,3]; @@ -63,7 +68,7 @@ benchmarks! { purge_keys { let n in 1 .. MAX_NOMINATIONS as u32; - let v_stash = create_validator_with_nominators::(n, MAX_NOMINATIONS as u32, false)?; + let v_stash = create_validator_with_nominators::(n, MAX_NOMINATIONS as u32, false, RewardDestination::Staked)?; let v_controller = pallet_staking::Module::::bonded(&v_stash).ok_or("not stash")?; let keys = T::Keys::default(); let proof: Vec = vec![0,1,2,3]; diff --git a/frame/staking/src/benchmarking.rs b/frame/staking/src/benchmarking.rs index a39837712abe7..d1b097e4c2a10 100644 --- a/frame/staking/src/benchmarking.rs +++ b/frame/staking/src/benchmarking.rs @@ -59,11 +59,12 @@ pub fn create_validator_with_nominators( n: u32, upper_bound: u32, dead: bool, + destination: RewardDestination ) -> Result { let mut points_total = 0; let mut points_individual = Vec::new(); - let (v_stash, v_controller) = create_stash_controller::(0, 100)?; + let (v_stash, v_controller) = create_stash_controller::(0, 100, destination.clone())?; let validator_prefs = ValidatorPrefs { commission: Perbill::from_percent(50), }; @@ -76,9 +77,9 @@ pub fn create_validator_with_nominators( // Give the validator n nominators, but keep total users in the system the same. for i in 0 .. upper_bound { let (_n_stash, n_controller) = if !dead { - create_stash_controller::(u32::max_value() - i, 100)? + create_stash_controller::(u32::max_value() - i, 100, destination.clone())? } else { - create_stash_and_dead_controller::(u32::max_value() - i, 100)? + create_stash_and_dead_controller::(u32::max_value() - i, 100, destination.clone())? }; if i < n { Staking::::nominate(RawOrigin::Signed(n_controller.clone()).into(), vec![stash_lookup.clone()])?; @@ -127,7 +128,7 @@ benchmarks! { } bond_extra { - let (stash, controller) = create_stash_controller::(USER_SEED, 100)?; + let (stash, controller) = create_stash_controller::(USER_SEED, 100, Default::default())?; 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; @@ -140,7 +141,7 @@ benchmarks! { } unbond { - let (_, controller) = create_stash_controller::(USER_SEED, 100)?; + let (_, controller) = create_stash_controller::(USER_SEED, 100, Default::default())?; 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; @@ -156,7 +157,7 @@ benchmarks! { withdraw_unbonded_update { // Slashing Spans let s in 0 .. MAX_SPANS; - let (stash, controller) = create_stash_controller::(0, 100)?; + let (stash, controller) = create_stash_controller::(0, 100, Default::default())?; add_slashing_spans::(&stash, s); let amount = T::Currency::minimum_balance() * 5.into(); // Half of total Staking::::unbond(RawOrigin::Signed(controller.clone()).into(), amount)?; @@ -175,7 +176,7 @@ benchmarks! { withdraw_unbonded_kill { // Slashing Spans let s in 0 .. MAX_SPANS; - let (stash, controller) = create_stash_controller::(0, 100)?; + let (stash, controller) = create_stash_controller::(0, 100, Default::default())?; add_slashing_spans::(&stash, s); let amount = T::Currency::minimum_balance() * 10.into(); Staking::::unbond(RawOrigin::Signed(controller.clone()).into(), amount)?; @@ -189,7 +190,7 @@ benchmarks! { } validate { - let (stash, controller) = create_stash_controller::(USER_SEED, 100)?; + let (stash, controller) = create_stash_controller::(USER_SEED, 100, Default::default())?; let prefs = ValidatorPrefs::default(); do_whitelist!(controller); }: _(RawOrigin::Signed(controller), prefs) @@ -200,7 +201,7 @@ benchmarks! { // Worst case scenario, MAX_NOMINATIONS nominate { let n in 1 .. MAX_NOMINATIONS as u32; - let (stash, controller) = create_stash_controller::(n + 1, 100)?; + let (stash, controller) = create_stash_controller::(n + 1, 100, Default::default())?; let validators = create_validators::(n, 100)?; do_whitelist!(controller); }: _(RawOrigin::Signed(controller), validators) @@ -209,12 +210,12 @@ benchmarks! { } chill { - let (_, controller) = create_stash_controller::(USER_SEED, 100)?; + let (_, controller) = create_stash_controller::(USER_SEED, 100, Default::default())?; do_whitelist!(controller); }: _(RawOrigin::Signed(controller)) set_payee { - let (stash, controller) = create_stash_controller::(USER_SEED, 100)?; + let (stash, controller) = create_stash_controller::(USER_SEED, 100, Default::default())?; assert_eq!(Payee::::get(&stash), RewardDestination::Staked); do_whitelist!(controller); }: _(RawOrigin::Signed(controller), RewardDestination::Controller) @@ -223,7 +224,7 @@ benchmarks! { } set_controller { - let (stash, _) = create_stash_controller::(USER_SEED, 100)?; + 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); @@ -263,7 +264,7 @@ benchmarks! { force_unstake { // Slashing Spans let s in 0 .. MAX_SPANS; - let (stash, controller) = create_stash_controller::(0, 100)?; + let (stash, controller) = create_stash_controller::(0, 100, Default::default())?; add_slashing_spans::(&stash, s); }: _(RawOrigin::Root, stash, s) verify { @@ -285,28 +286,40 @@ benchmarks! { assert_eq!(UnappliedSlashes::::get(&era).len(), (MAX_SLASHES - s) as usize); } - payout_stakers { + payout_stakers_dead_controller { let n in 1 .. T::MaxNominatorRewardedPerValidator::get() as u32; - let validator = create_validator_with_nominators::(n, T::MaxNominatorRewardedPerValidator::get() as u32, true)?; + let validator = create_validator_with_nominators::( + n, + T::MaxNominatorRewardedPerValidator::get() as u32, + true, + RewardDestination::Controller, + )?; let current_era = CurrentEra::get().unwrap(); // set the commission for this particular era as well. >::insert(current_era, validator.clone(), >::validators(&validator)); let caller = whitelisted_caller(); - let balance_before = T::Currency::free_balance(&validator); - }: _(RawOrigin::Signed(caller), validator.clone(), current_era) + let validator_controller = >::get(&validator).unwrap(); + let balance_before = T::Currency::free_balance(&validator_controller); + }: payout_stakers(RawOrigin::Signed(caller), validator.clone(), current_era) verify { - let balance_after = T::Currency::free_balance(&validator); + let balance_after = T::Currency::free_balance(&validator_controller); assert!( balance_before < balance_after, - "Balance of stash {:?} should have increased after payout.", validator, + "Balance of controller {:?} should have increased after payout.", + validator, ); } - payout_stakers_alive_controller { + payout_stakers_alive_staked { let n in 1 .. T::MaxNominatorRewardedPerValidator::get() as u32; - let validator = create_validator_with_nominators::(n, T::MaxNominatorRewardedPerValidator::get() as u32, false)?; + let validator = create_validator_with_nominators::( + n, + T::MaxNominatorRewardedPerValidator::get() as u32, + false, + RewardDestination::Staked, + )?; let current_era = CurrentEra::get().unwrap(); // set the commission for this particular era as well. @@ -319,13 +332,14 @@ benchmarks! { let balance_after = T::Currency::free_balance(&validator); assert!( balance_before < balance_after, - "Balance of stash {:?} should have increased after payout.", validator, + "Balance of stash {:?} should have increased after payout.", + validator, ); } rebond { let l in 1 .. MAX_UNLOCKING_CHUNKS as u32; - let (_, controller) = create_stash_controller::(USER_SEED, 100)?; + let (_, controller) = create_stash_controller::(USER_SEED, 100, Default::default())?; let mut staking_ledger = Ledger::::get(controller.clone()).unwrap(); let unlock_chunk = UnlockChunk::> { value: 1.into(), @@ -364,7 +378,7 @@ benchmarks! { reap_stash { let s in 1 .. MAX_SPANS; - let (stash, controller) = create_stash_controller::(0, 100)?; + 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); @@ -425,7 +439,7 @@ benchmarks! { #[extra] do_slash { let l in 1 .. MAX_UNLOCKING_CHUNKS as u32; - let (stash, controller) = create_stash_controller::(0, 100)?; + let (stash, controller) = create_stash_controller::(0, 100, Default::default())?; let mut staking_ledger = Ledger::::get(controller.clone()).unwrap(); let unlock_chunk = UnlockChunk::> { value: 1.into(), @@ -697,6 +711,7 @@ mod tests { n, ::MaxNominatorRewardedPerValidator::get() as u32, false, + RewardDestination::Staked, ).unwrap(); let current_era = CurrentEra::get().unwrap(); @@ -718,6 +733,7 @@ mod tests { n, ::MaxNominatorRewardedPerValidator::get() as u32, false, + RewardDestination::Staked, ).unwrap(); // Add 20 slashing spans diff --git a/frame/staking/src/testing_utils.rs b/frame/staking/src/testing_utils.rs index cacad27070084..6354014232d50 100644 --- a/frame/staking/src/testing_utils.rs +++ b/frame/staking/src/testing_utils.rs @@ -43,30 +43,36 @@ pub fn create_funded_user( } /// Create a stash and controller pair. -pub fn create_stash_controller(n: u32, balance_factor: u32) +pub fn create_stash_controller( + n: u32, + balance_factor: u32, + destination: RewardDestination, +) -> Result<(T::AccountId, T::AccountId), &'static str> { let stash = create_funded_user::("stash", n, balance_factor); let controller = create_funded_user::("controller", n, balance_factor); let controller_lookup: ::Source = T::Lookup::unlookup(controller.clone()); - let reward_destination = RewardDestination::Staked; let amount = T::Currency::minimum_balance() * (balance_factor / 10).max(1).into(); - Staking::::bond(RawOrigin::Signed(stash.clone()).into(), controller_lookup, amount, reward_destination)?; + Staking::::bond(RawOrigin::Signed(stash.clone()).into(), controller_lookup, amount, destination)?; return Ok((stash, controller)) } /// Create a stash and controller pair, where the controller is dead, and payouts go to controller. /// This is used to test worst case payout scenarios. -pub fn create_stash_and_dead_controller(n: u32, balance_factor: u32) +pub fn create_stash_and_dead_controller( + n: u32, + balance_factor: u32, + destination: RewardDestination, +) -> Result<(T::AccountId, T::AccountId), &'static str> { let stash = create_funded_user::("stash", n, balance_factor); // controller has no funds let controller = create_funded_user::("controller", n, 0); let controller_lookup: ::Source = T::Lookup::unlookup(controller.clone()); - let reward_destination = RewardDestination::Controller; let amount = T::Currency::minimum_balance() * (balance_factor / 10).max(1).into(); - Staking::::bond(RawOrigin::Signed(stash.clone()).into(), controller_lookup, amount, reward_destination)?; + Staking::::bond(RawOrigin::Signed(stash.clone()).into(), controller_lookup, amount, destination)?; return Ok((stash, controller)) } @@ -77,7 +83,7 @@ pub fn create_validators( ) -> Result::Source>, &'static str> { let mut validators: Vec<::Source> = Vec::with_capacity(max as usize); for i in 0 .. max { - let (stash, controller) = create_stash_controller::(i, balance_factor)?; + let (stash, controller) = create_stash_controller::(i, balance_factor, RewardDestination::Staked)?; let validator_prefs = ValidatorPrefs { commission: Perbill::from_percent(50), }; @@ -114,7 +120,7 @@ pub fn create_validators_with_nominators_for_era( // Create validators for i in 0 .. validators { let balance_factor = if randomize_stake { rng.next_u32() % 255 + 10 } else { 100u32 }; - let (v_stash, v_controller) = create_stash_controller::(i, balance_factor)?; + let (v_stash, v_controller) = create_stash_controller::(i, balance_factor, RewardDestination::Staked)?; let validator_prefs = ValidatorPrefs { commission: Perbill::from_percent(50), }; @@ -132,6 +138,7 @@ pub fn create_validators_with_nominators_for_era( let (_n_stash, n_controller) = create_stash_controller::( u32::max_value() - j, balance_factor, + RewardDestination::Staked, )?; // Have them randomly validate From 2eb0245dda60f96cfec1b41f12f0a4f96f05f6a9 Mon Sep 17 00:00:00 2001 From: kianenigma Date: Fri, 11 Sep 2020 14:15:32 +0200 Subject: [PATCH 16/19] Fix payout stuff --- frame/staking/src/benchmarking.rs | 5 +++-- frame/staking/src/default_weights.rs | 9 --------- frame/staking/src/lib.rs | 1 - 3 files changed, 3 insertions(+), 12 deletions(-) diff --git a/frame/staking/src/benchmarking.rs b/frame/staking/src/benchmarking.rs index d1b097e4c2a10..ecac00218ae92 100644 --- a/frame/staking/src/benchmarking.rs +++ b/frame/staking/src/benchmarking.rs @@ -398,6 +398,7 @@ benchmarks! { assert!(validators.len() == v as usize); } + #[extra] payout_all { let v in 1 .. 10; let n in 1 .. 100; @@ -794,8 +795,8 @@ mod tests { assert_ok!(test_benchmark_set_invulnerables::()); assert_ok!(test_benchmark_force_unstake::()); assert_ok!(test_benchmark_cancel_deferred_slash::()); - assert_ok!(test_benchmark_payout_stakers::()); - assert_ok!(test_benchmark_payout_stakers_alive_controller::()); + assert_ok!(test_benchmark_payout_stakers_dead_controller::()); + assert_ok!(test_benchmark_payout_stakers_alive_staked::()); assert_ok!(test_benchmark_rebond::()); assert_ok!(test_benchmark_set_history_depth::()); assert_ok!(test_benchmark_reap_stash::()); diff --git a/frame/staking/src/default_weights.rs b/frame/staking/src/default_weights.rs index 6e7283ea708a2..007fa540d3dec 100644 --- a/frame/staking/src/default_weights.rs +++ b/frame/staking/src/default_weights.rs @@ -158,15 +158,6 @@ impl crate::WeightInfo for () { .saturating_add(DbWeight::get().writes(8 as Weight)) .saturating_add(DbWeight::get().writes((3 as Weight).saturating_mul(v as Weight))) } - fn payout_all(v: u32, n: u32, ) -> Weight { - (0 as Weight) - .saturating_add((23897487000 as Weight).saturating_mul(v as Weight)) - .saturating_add((1997765000 as Weight).saturating_mul(n as Weight)) - .saturating_add(DbWeight::get().reads((15 as Weight).saturating_mul(v as Weight))) - .saturating_add(DbWeight::get().reads((5 as Weight).saturating_mul(n as Weight))) - .saturating_add(DbWeight::get().writes((8 as Weight).saturating_mul(v as Weight))) - .saturating_add(DbWeight::get().writes((3 as Weight).saturating_mul(n as Weight))) - } fn submit_solution_better(v: u32, n: u32, a: u32, w: u32, ) -> Weight { (1278000000 as Weight) .saturating_add((10599000 as Weight).saturating_mul(v as Weight)) diff --git a/frame/staking/src/lib.rs b/frame/staking/src/lib.rs index 1c7c828e99ac2..7a48406dfeb81 100644 --- a/frame/staking/src/lib.rs +++ b/frame/staking/src/lib.rs @@ -793,7 +793,6 @@ pub trait WeightInfo { fn set_history_depth(e: u32, ) -> Weight; fn reap_stash(s: u32, ) -> Weight; fn new_era(v: u32, n: u32, ) -> Weight; - fn payout_all(v: u32, n: u32, ) -> Weight; fn submit_solution_better(v: u32, n: u32, a: u32, w: u32, ) -> Weight; } From 462690a96a90b0a65736acd975a5bee6cc7709b6 Mon Sep 17 00:00:00 2001 From: kianenigma Date: Fri, 11 Sep 2020 14:49:42 +0200 Subject: [PATCH 17/19] One last fix --- frame/staking/src/default_weights.rs | 85 ++++++++++++++-------------- frame/staking/src/lib.rs | 10 ++-- 2 files changed, 47 insertions(+), 48 deletions(-) diff --git a/frame/staking/src/default_weights.rs b/frame/staking/src/default_weights.rs index 007fa540d3dec..868a150bfe84a 100644 --- a/frame/staking/src/default_weights.rs +++ b/frame/staking/src/default_weights.rs @@ -24,134 +24,133 @@ use frame_support::weights::{Weight, constants::RocksDbWeight as DbWeight}; impl crate::WeightInfo for () { - fn bond() -> Weight { - (408578000 as Weight) + fn bond() -> Weight { + (410765000 as Weight) .saturating_add(DbWeight::get().reads(5 as Weight)) .saturating_add(DbWeight::get().writes(4 as Weight)) } fn bond_extra() -> Weight { - (316693000 as Weight) + (317886000 as Weight) .saturating_add(DbWeight::get().reads(4 as Weight)) .saturating_add(DbWeight::get().writes(2 as Weight)) } fn unbond() -> Weight { - (276194000 as Weight) + (277650000 as Weight) .saturating_add(DbWeight::get().reads(5 as Weight)) .saturating_add(DbWeight::get().writes(3 as Weight)) } fn withdraw_unbonded_update(s: u32, ) -> Weight { - (281330000 as Weight) - .saturating_add((46000 as Weight).saturating_mul(s as Weight)) + (282302000 as Weight) + .saturating_add((72000 as Weight).saturating_mul(s as Weight)) .saturating_add(DbWeight::get().reads(5 as Weight)) .saturating_add(DbWeight::get().writes(3 as Weight)) } fn withdraw_unbonded_kill(s: u32, ) -> Weight { - (474310000 as Weight) - .saturating_add((19672000 as Weight).saturating_mul(s as Weight)) + (472897000 as Weight) + .saturating_add((20054000 as Weight).saturating_mul(s as Weight)) .saturating_add(DbWeight::get().reads(7 as Weight)) .saturating_add(DbWeight::get().writes(8 as Weight)) .saturating_add(DbWeight::get().writes((1 as Weight).saturating_mul(s as Weight))) } fn validate() -> Weight { - (99283000 as Weight) + (99792000 as Weight) .saturating_add(DbWeight::get().reads(2 as Weight)) .saturating_add(DbWeight::get().writes(2 as Weight)) } fn nominate(n: u32, ) -> Weight { - (126479000 as Weight) - .saturating_add((5329000 as Weight).saturating_mul(n as Weight)) + (126734000 as Weight) + .saturating_add((5386000 as Weight).saturating_mul(n as Weight)) .saturating_add(DbWeight::get().reads(3 as Weight)) .saturating_add(DbWeight::get().writes(2 as Weight)) } fn chill() -> Weight { - (97447000 as Weight) + (97637000 as Weight) .saturating_add(DbWeight::get().reads(2 as Weight)) .saturating_add(DbWeight::get().writes(2 as Weight)) } fn set_payee() -> Weight { - (74199000 as Weight) + (74185000 as Weight) .saturating_add(DbWeight::get().reads(1 as Weight)) .saturating_add(DbWeight::get().writes(1 as Weight)) } fn set_controller() -> Weight { - (153911000 as Weight) + (152821000 as Weight) .saturating_add(DbWeight::get().reads(3 as Weight)) .saturating_add(DbWeight::get().writes(3 as Weight)) } // WARNING! Some components were not used: ["c"] fn set_validator_count() -> Weight { - (10890000 as Weight) + (10844000 as Weight) .saturating_add(DbWeight::get().writes(1 as Weight)) } fn force_no_eras() -> Weight { - (13143000 as Weight) + (13105000 as Weight) .saturating_add(DbWeight::get().writes(1 as Weight)) } fn force_new_era() -> Weight { - (13112000 as Weight) + (13011000 as Weight) .saturating_add(DbWeight::get().writes(1 as Weight)) } fn force_new_era_always() -> Weight { - (13108000 as Weight) + (13023000 as Weight) .saturating_add(DbWeight::get().writes(1 as Weight)) } fn set_invulnerables(v: u32, ) -> Weight { - (14140000 as Weight) - .saturating_add((218000 as Weight).saturating_mul(v as Weight)) + (14084000 as Weight) + .saturating_add((225000 as Weight).saturating_mul(v as Weight)) .saturating_add(DbWeight::get().writes(1 as Weight)) } fn force_unstake(s: u32, ) -> Weight { - (344355000 as Weight) - .saturating_add((19704000 as Weight).saturating_mul(s as Weight)) + (346943000 as Weight) + .saturating_add((20004000 as Weight).saturating_mul(s as Weight)) .saturating_add(DbWeight::get().reads(4 as Weight)) .saturating_add(DbWeight::get().writes(8 as Weight)) .saturating_add(DbWeight::get().writes((1 as Weight).saturating_mul(s as Weight))) } fn cancel_deferred_slash(s: u32, ) -> Weight { - (260009398000 as Weight) - .saturating_add((1675144000 as Weight).saturating_mul(s as Weight)) + (259957575000 as Weight) + .saturating_add((1675368000 as Weight).saturating_mul(s as Weight)) .saturating_add(DbWeight::get().reads(1 as Weight)) .saturating_add(DbWeight::get().writes(1 as Weight)) } - fn payout_stakers(n: u32, ) -> Weight { - (71905000 as Weight) - .saturating_add((274832000 as Weight).saturating_mul(n as Weight)) - .saturating_add(DbWeight::get().reads(5 as Weight)) + fn payout_stakers_dead_controller(n: u32, ) -> Weight { + (0 as Weight) + .saturating_add((276999000 as Weight).saturating_mul(n as Weight)) + .saturating_add(DbWeight::get().reads(4 as Weight)) .saturating_add(DbWeight::get().reads((3 as Weight).saturating_mul(n as Weight))) - .saturating_add(DbWeight::get().writes(1 as Weight)) .saturating_add(DbWeight::get().writes((1 as Weight).saturating_mul(n as Weight))) } - fn payout_stakers_alive_controller(n: u32, ) -> Weight { + fn payout_stakers_alive_staked(n: u32, ) -> Weight { (0 as Weight) - .saturating_add((371585000 as Weight).saturating_mul(n as Weight)) + .saturating_add((373451000 as Weight).saturating_mul(n as Weight)) .saturating_add(DbWeight::get().reads(1 as Weight)) .saturating_add(DbWeight::get().reads((5 as Weight).saturating_mul(n as Weight))) .saturating_add(DbWeight::get().writes((3 as Weight).saturating_mul(n as Weight))) } fn rebond(l: u32, ) -> Weight { - (208619000 as Weight) - .saturating_add((2271000 as Weight).saturating_mul(l as Weight)) + (210408000 as Weight) + .saturating_add((2272000 as Weight).saturating_mul(l as Weight)) .saturating_add(DbWeight::get().reads(4 as Weight)) .saturating_add(DbWeight::get().writes(3 as Weight)) } fn set_history_depth(e: u32, ) -> Weight { (0 as Weight) - .saturating_add((93111000 as Weight).saturating_mul(e as Weight)) + .saturating_add((94035000 as Weight).saturating_mul(e as Weight)) .saturating_add(DbWeight::get().reads(2 as Weight)) .saturating_add(DbWeight::get().writes(4 as Weight)) .saturating_add(DbWeight::get().writes((7 as Weight).saturating_mul(e as Weight))) } fn reap_stash(s: u32, ) -> Weight { - (394410000 as Weight) - .saturating_add((19600000 as Weight).saturating_mul(s as Weight)) + (396612000 as Weight) + .saturating_add((19958000 as Weight).saturating_mul(s as Weight)) .saturating_add(DbWeight::get().reads(4 as Weight)) .saturating_add(DbWeight::get().writes(8 as Weight)) .saturating_add(DbWeight::get().writes((1 as Weight).saturating_mul(s as Weight))) } fn new_era(v: u32, n: u32, ) -> Weight { (0 as Weight) - .saturating_add((19861198000 as Weight).saturating_mul(v as Weight)) - .saturating_add((1792877000 as Weight).saturating_mul(n as Weight)) + .saturating_add((20113089000 as Weight).saturating_mul(v as Weight)) + .saturating_add((1815337000 as Weight).saturating_mul(n as Weight)) .saturating_add(DbWeight::get().reads(10 as Weight)) .saturating_add(DbWeight::get().reads((4 as Weight).saturating_mul(v as Weight))) .saturating_add(DbWeight::get().reads((3 as Weight).saturating_mul(n as Weight))) @@ -159,11 +158,11 @@ impl crate::WeightInfo for () { .saturating_add(DbWeight::get().writes((3 as Weight).saturating_mul(v as Weight))) } fn submit_solution_better(v: u32, n: u32, a: u32, w: u32, ) -> Weight { - (1278000000 as Weight) - .saturating_add((10599000 as Weight).saturating_mul(v as Weight)) - .saturating_add((5384000 as Weight).saturating_mul(n as Weight)) - .saturating_add((1016715000 as Weight).saturating_mul(a as Weight)) - .saturating_add((113107000 as Weight).saturating_mul(w as Weight)) + (1609354000 as Weight) + .saturating_add((10579000 as Weight).saturating_mul(v as Weight)) + .saturating_add((5164000 as Weight).saturating_mul(n as Weight)) + .saturating_add((1027784000 as Weight).saturating_mul(a as Weight)) + .saturating_add((116974000 as Weight).saturating_mul(w as Weight)) .saturating_add(DbWeight::get().reads(6 as Weight)) .saturating_add(DbWeight::get().reads((4 as Weight).saturating_mul(a as Weight))) .saturating_add(DbWeight::get().reads((1 as Weight).saturating_mul(w as Weight))) diff --git a/frame/staking/src/lib.rs b/frame/staking/src/lib.rs index 7a48406dfeb81..b1565e1aa5f46 100644 --- a/frame/staking/src/lib.rs +++ b/frame/staking/src/lib.rs @@ -787,8 +787,8 @@ pub trait WeightInfo { fn set_invulnerables(v: u32, ) -> Weight; fn force_unstake(s: u32, ) -> Weight; fn cancel_deferred_slash(s: u32, ) -> Weight; - fn payout_stakers(n: u32, ) -> Weight; - fn payout_stakers_alive_controller(n: u32, ) -> Weight; + fn payout_stakers_alive_staked(n: u32, ) -> Weight; + fn payout_stakers_dead_controller(n: u32, ) -> Weight; fn rebond(l: u32, ) -> Weight; fn set_history_depth(e: u32, ) -> Weight; fn reap_stash(s: u32, ) -> Weight; @@ -1945,10 +1945,10 @@ decl_module! { /// - Read Each: Bonded, Ledger, Payee, Locks, System Account (5 items) /// - Write Each: System Account, Locks, Ledger (3 items) /// - /// NOTE: weights are assuming that payouts are made to dead controller accounts. - /// Currently we don't refund otherwise. + /// NOTE: weights are assuming that payouts are made to alive stash account (Staked). + /// Paying even a dead controller is cheaper weight-wise. We don't do any refunds here. /// # - #[weight = T::WeightInfo::payout_stakers(T::MaxNominatorRewardedPerValidator::get())] + #[weight = T::WeightInfo::payout_stakers_alive_staked(T::MaxNominatorRewardedPerValidator::get())] fn payout_stakers(origin, validator_stash: T::AccountId, era: EraIndex) -> DispatchResult { ensure!(Self::era_election_status().is_closed(), Error::::CallNotAllowed); ensure_signed(origin)?; From 813c13604d6cf59a58bdf1a38a331b292f190947 Mon Sep 17 00:00:00 2001 From: Shawn Tabrizi Date: Tue, 15 Sep 2020 01:12:16 +0200 Subject: [PATCH 18/19] use benchmarking machine for numbers --- frame/staking/src/benchmarking.rs | 6 +-- frame/staking/src/default_weights.rs | 78 ++++++++++++++-------------- 2 files changed, 41 insertions(+), 43 deletions(-) diff --git a/frame/staking/src/benchmarking.rs b/frame/staking/src/benchmarking.rs index ecac00218ae92..afda58db4672f 100644 --- a/frame/staking/src/benchmarking.rs +++ b/frame/staking/src/benchmarking.rs @@ -234,10 +234,10 @@ benchmarks! { } set_validator_count { - let c in 0 .. MAX_VALIDATORS; - }: _(RawOrigin::Root, c) + let validator_count = MAX_VALIDATORS; + }: _(RawOrigin::Root, validator_count) verify { - assert_eq!(ValidatorCount::get(), c); + assert_eq!(ValidatorCount::get(), validator_count); } force_no_eras {}: _(RawOrigin::Root) diff --git a/frame/staking/src/default_weights.rs b/frame/staking/src/default_weights.rs index 868a150bfe84a..fa5a05f63824e 100644 --- a/frame/staking/src/default_weights.rs +++ b/frame/staking/src/default_weights.rs @@ -24,133 +24,131 @@ use frame_support::weights::{Weight, constants::RocksDbWeight as DbWeight}; impl crate::WeightInfo for () { - fn bond() -> Weight { - (410765000 as Weight) + fn bond() -> Weight { + (144278000 as Weight) .saturating_add(DbWeight::get().reads(5 as Weight)) .saturating_add(DbWeight::get().writes(4 as Weight)) } fn bond_extra() -> Weight { - (317886000 as Weight) + (110715000 as Weight) .saturating_add(DbWeight::get().reads(4 as Weight)) .saturating_add(DbWeight::get().writes(2 as Weight)) } fn unbond() -> Weight { - (277650000 as Weight) + (99840000 as Weight) .saturating_add(DbWeight::get().reads(5 as Weight)) .saturating_add(DbWeight::get().writes(3 as Weight)) } fn withdraw_unbonded_update(s: u32, ) -> Weight { - (282302000 as Weight) - .saturating_add((72000 as Weight).saturating_mul(s as Weight)) + (100728000 as Weight) + .saturating_add((63000 as Weight).saturating_mul(s as Weight)) .saturating_add(DbWeight::get().reads(5 as Weight)) .saturating_add(DbWeight::get().writes(3 as Weight)) } fn withdraw_unbonded_kill(s: u32, ) -> Weight { - (472897000 as Weight) - .saturating_add((20054000 as Weight).saturating_mul(s as Weight)) + (168879000 as Weight) + .saturating_add((6666000 as Weight).saturating_mul(s as Weight)) .saturating_add(DbWeight::get().reads(7 as Weight)) .saturating_add(DbWeight::get().writes(8 as Weight)) .saturating_add(DbWeight::get().writes((1 as Weight).saturating_mul(s as Weight))) } fn validate() -> Weight { - (99792000 as Weight) + (35539000 as Weight) .saturating_add(DbWeight::get().reads(2 as Weight)) .saturating_add(DbWeight::get().writes(2 as Weight)) } fn nominate(n: u32, ) -> Weight { - (126734000 as Weight) - .saturating_add((5386000 as Weight).saturating_mul(n as Weight)) + (48596000 as Weight) + .saturating_add((308000 as Weight).saturating_mul(n as Weight)) .saturating_add(DbWeight::get().reads(3 as Weight)) .saturating_add(DbWeight::get().writes(2 as Weight)) } fn chill() -> Weight { - (97637000 as Weight) + (35144000 as Weight) .saturating_add(DbWeight::get().reads(2 as Weight)) .saturating_add(DbWeight::get().writes(2 as Weight)) } fn set_payee() -> Weight { - (74185000 as Weight) + (24255000 as Weight) .saturating_add(DbWeight::get().reads(1 as Weight)) .saturating_add(DbWeight::get().writes(1 as Weight)) } fn set_controller() -> Weight { - (152821000 as Weight) + (52294000 as Weight) .saturating_add(DbWeight::get().reads(3 as Weight)) .saturating_add(DbWeight::get().writes(3 as Weight)) } - // WARNING! Some components were not used: ["c"] fn set_validator_count() -> Weight { - (10844000 as Weight) + (5185000 as Weight) .saturating_add(DbWeight::get().writes(1 as Weight)) } fn force_no_eras() -> Weight { - (13105000 as Weight) + (5907000 as Weight) .saturating_add(DbWeight::get().writes(1 as Weight)) } fn force_new_era() -> Weight { - (13011000 as Weight) + (5917000 as Weight) .saturating_add(DbWeight::get().writes(1 as Weight)) } fn force_new_era_always() -> Weight { - (13023000 as Weight) + (5952000 as Weight) .saturating_add(DbWeight::get().writes(1 as Weight)) } fn set_invulnerables(v: u32, ) -> Weight { - (14084000 as Weight) - .saturating_add((225000 as Weight).saturating_mul(v as Weight)) + (6324000 as Weight) + .saturating_add((9000 as Weight).saturating_mul(v as Weight)) .saturating_add(DbWeight::get().writes(1 as Weight)) } fn force_unstake(s: u32, ) -> Weight { - (346943000 as Weight) - .saturating_add((20004000 as Weight).saturating_mul(s as Weight)) + (119691000 as Weight) + .saturating_add((6681000 as Weight).saturating_mul(s as Weight)) .saturating_add(DbWeight::get().reads(4 as Weight)) .saturating_add(DbWeight::get().writes(8 as Weight)) .saturating_add(DbWeight::get().writes((1 as Weight).saturating_mul(s as Weight))) } fn cancel_deferred_slash(s: u32, ) -> Weight { - (259957575000 as Weight) - .saturating_add((1675368000 as Weight).saturating_mul(s as Weight)) + (5820201000 as Weight) + .saturating_add((34672000 as Weight).saturating_mul(s as Weight)) .saturating_add(DbWeight::get().reads(1 as Weight)) .saturating_add(DbWeight::get().writes(1 as Weight)) } fn payout_stakers_dead_controller(n: u32, ) -> Weight { (0 as Weight) - .saturating_add((276999000 as Weight).saturating_mul(n as Weight)) + .saturating_add((92486000 as Weight).saturating_mul(n as Weight)) .saturating_add(DbWeight::get().reads(4 as Weight)) .saturating_add(DbWeight::get().reads((3 as Weight).saturating_mul(n as Weight))) .saturating_add(DbWeight::get().writes((1 as Weight).saturating_mul(n as Weight))) } fn payout_stakers_alive_staked(n: u32, ) -> Weight { (0 as Weight) - .saturating_add((373451000 as Weight).saturating_mul(n as Weight)) - .saturating_add(DbWeight::get().reads(1 as Weight)) + .saturating_add((117324000 as Weight).saturating_mul(n as Weight)) .saturating_add(DbWeight::get().reads((5 as Weight).saturating_mul(n as Weight))) .saturating_add(DbWeight::get().writes((3 as Weight).saturating_mul(n as Weight))) } fn rebond(l: u32, ) -> Weight { - (210408000 as Weight) - .saturating_add((2272000 as Weight).saturating_mul(l as Weight)) + (71316000 as Weight) + .saturating_add((142000 as Weight).saturating_mul(l as Weight)) .saturating_add(DbWeight::get().reads(4 as Weight)) .saturating_add(DbWeight::get().writes(3 as Weight)) } fn set_history_depth(e: u32, ) -> Weight { (0 as Weight) - .saturating_add((94035000 as Weight).saturating_mul(e as Weight)) + .saturating_add((51901000 as Weight).saturating_mul(e as Weight)) .saturating_add(DbWeight::get().reads(2 as Weight)) .saturating_add(DbWeight::get().writes(4 as Weight)) .saturating_add(DbWeight::get().writes((7 as Weight).saturating_mul(e as Weight))) } fn reap_stash(s: u32, ) -> Weight { - (396612000 as Weight) - .saturating_add((19958000 as Weight).saturating_mul(s as Weight)) + (147166000 as Weight) + .saturating_add((6661000 as Weight).saturating_mul(s as Weight)) .saturating_add(DbWeight::get().reads(4 as Weight)) .saturating_add(DbWeight::get().writes(8 as Weight)) .saturating_add(DbWeight::get().writes((1 as Weight).saturating_mul(s as Weight))) } fn new_era(v: u32, n: u32, ) -> Weight { (0 as Weight) - .saturating_add((20113089000 as Weight).saturating_mul(v as Weight)) - .saturating_add((1815337000 as Weight).saturating_mul(n as Weight)) + .saturating_add((1440459000 as Weight).saturating_mul(v as Weight)) + .saturating_add((182580000 as Weight).saturating_mul(n as Weight)) .saturating_add(DbWeight::get().reads(10 as Weight)) .saturating_add(DbWeight::get().reads((4 as Weight).saturating_mul(v as Weight))) .saturating_add(DbWeight::get().reads((3 as Weight).saturating_mul(n as Weight))) @@ -158,11 +156,11 @@ impl crate::WeightInfo for () { .saturating_add(DbWeight::get().writes((3 as Weight).saturating_mul(v as Weight))) } fn submit_solution_better(v: u32, n: u32, a: u32, w: u32, ) -> Weight { - (1609354000 as Weight) - .saturating_add((10579000 as Weight).saturating_mul(v as Weight)) - .saturating_add((5164000 as Weight).saturating_mul(n as Weight)) - .saturating_add((1027784000 as Weight).saturating_mul(a as Weight)) - .saturating_add((116974000 as Weight).saturating_mul(w as Weight)) + (0 as Weight) + .saturating_add((964000 as Weight).saturating_mul(v as Weight)) + .saturating_add((432000 as Weight).saturating_mul(n as Weight)) + .saturating_add((204294000 as Weight).saturating_mul(a as Weight)) + .saturating_add((9546000 as Weight).saturating_mul(w as Weight)) .saturating_add(DbWeight::get().reads(6 as Weight)) .saturating_add(DbWeight::get().reads((4 as Weight).saturating_mul(a as Weight))) .saturating_add(DbWeight::get().reads((1 as Weight).saturating_mul(w as Weight))) From 4a91a58a3f44c64196c7c298ef2a6f68c504101b Mon Sep 17 00:00:00 2001 From: Shawn Tabrizi Date: Tue, 15 Sep 2020 01:25:03 +0200 Subject: [PATCH 19/19] update weight docs --- frame/staking/src/lib.rs | 41 +++++++++++++++++----------------------- 1 file changed, 17 insertions(+), 24 deletions(-) diff --git a/frame/staking/src/lib.rs b/frame/staking/src/lib.rs index 3aec82a9fe161..7061832b0460c 100644 --- a/frame/staking/src/lib.rs +++ b/frame/staking/src/lib.rs @@ -1391,7 +1391,7 @@ decl_module! { /// NOTE: Two of the storage writes (`Self::bonded`, `Self::payee`) are _never_ cleaned /// unless the `origin` falls below _existential deposit_ and gets removed as dust. /// ------------------ - /// Base Weight: 67.87 µs + /// Weight: O(1) /// DB Weight: /// - Read: Bonded, Ledger, [Origin Account], Current Era, History Depth, Locks /// - Write: Bonded, Payee, [Origin Account], Locks, Ledger @@ -1460,7 +1460,6 @@ decl_module! { /// - O(1). /// - One DB entry. /// ------------ - /// Base Weight: 54.88 µs /// DB Weight: /// - Read: Era Election Status, Bonded, Ledger, [Origin Account], Locks /// - Write: [Origin Account], Locks, Ledger @@ -1511,7 +1510,7 @@ decl_module! { /// `withdraw_unbonded`. /// - One DB entry. /// ---------- - /// Base Weight: 50.34 µs + /// Weight: O(1) /// DB Weight: /// - Read: EraElectionStatus, Ledger, CurrentEra, Locks, BalanceOf Stash, /// - Write: Locks, Ledger, BalanceOf Stash, @@ -1565,11 +1564,10 @@ decl_module! { /// - Writes are limited to the `origin` account key. /// --------------- /// Complexity O(S) where S is the number of slashing spans to remove - /// Base Weight: - /// Update: 50.52 + .028 * S µs + /// Update: /// - Reads: EraElectionStatus, Ledger, Current Era, Locks, [Origin Account] /// - Writes: [Origin Account], Locks, Ledger - /// Kill: 79.41 + 2.366 * S µs + /// Kill: /// - Reads: EraElectionStatus, Ledger, Current Era, Bonded, Slashing Spans, [Origin /// Account], Locks, BalanceOf stash /// - Writes: Bonded, Slashing Spans (if S > 0), Ledger, Payee, Validators, Nominators, @@ -1627,7 +1625,7 @@ decl_module! { /// - Contains a limited number of reads. /// - Writes are limited to the `origin` account key. /// ----------- - /// Base Weight: 17.13 µs + /// Weight: O(1) /// DB Weight: /// - Read: Era Election Status, Ledger /// - Write: Nominators, Validators @@ -1655,7 +1653,7 @@ decl_module! { /// which is capped at CompactAssignments::LIMIT (MAX_NOMINATIONS). /// - Both the reads and writes follow a similar pattern. /// --------- - /// Base Weight: 22.34 + .36 * N µs + /// Weight: O(N) /// where N is the number of targets /// DB Weight: /// - Reads: Era Election Status, Ledger, Current Era @@ -1696,7 +1694,7 @@ decl_module! { /// - Contains one read. /// - Writes are limited to the `origin` account key. /// -------- - /// Base Weight: 16.53 µs + /// Weight: O(1) /// DB Weight: /// - Read: EraElectionStatus, Ledger /// - Write: Validators, Nominators @@ -1720,7 +1718,7 @@ decl_module! { /// - Contains a limited number of reads. /// - Writes are limited to the `origin` account key. /// --------- - /// - Base Weight: 11.33 µs + /// - Weight: O(1) /// - DB Weight: /// - Read: Ledger /// - Write: Payee @@ -1744,7 +1742,7 @@ decl_module! { /// - Contains a limited number of reads. /// - Writes are limited to the `origin` account key. /// ---------- - /// Base Weight: 25.22 µs + /// Weight: O(1) /// DB Weight: /// - Read: Bonded, Ledger New Controller, Ledger Old Controller /// - Write: Bonded, Ledger New Controller, Ledger Old Controller @@ -1770,7 +1768,7 @@ decl_module! { /// The dispatch origin must be Root. /// /// # - /// Base Weight: 1.717 µs + /// Weight: O(1) /// Write: Validator Count /// # #[weight = T::WeightInfo::set_validator_count()] @@ -1811,7 +1809,7 @@ decl_module! { /// /// # /// - No arguments. - /// - Base Weight: 1.857 µs + /// - Weight: O(1) /// - Write: ForceEra /// # #[weight = T::WeightInfo::force_no_eras()] @@ -1827,7 +1825,7 @@ decl_module! { /// /// # /// - No arguments. - /// - Base Weight: 1.959 µs + /// - Weight: O(1) /// - Write ForceEra /// # #[weight = T::WeightInfo::force_new_era()] @@ -1842,7 +1840,6 @@ decl_module! { /// /// # /// - O(V) - /// - Base Weight: 2.208 + .006 * V µs /// - Write: Invulnerables /// # #[weight = T::WeightInfo::set_invulnerables(invulnerables.len() as u32)] @@ -1857,7 +1854,6 @@ decl_module! { /// /// # /// O(S) where S is the number of slashing spans to be removed - /// Base Weight: 53.07 + 2.365 * S µs /// Reads: Bonded, Slashing Spans, Account, Locks /// Writes: Bonded, Slashing Spans (if S > 0), Ledger, Payee, Validators, Nominators, Account, Locks /// Writes Each: SpanSlash * S @@ -1878,7 +1874,7 @@ decl_module! { /// The dispatch origin must be Root. /// /// # - /// - Base Weight: 2.05 µs + /// - Weight: O(1) /// - Write: ForceEra /// # #[weight = T::WeightInfo::force_new_era_always()] @@ -1897,7 +1893,6 @@ decl_module! { /// Complexity: O(U + S) /// with U unapplied slashes weighted with U=1000 /// and S is the number of slash indices to be canceled. - /// - Base: 5870 + 34.61 * S µs /// - Read: Unapplied Slashes /// - Write: Unapplied Slashes /// # @@ -1936,9 +1931,9 @@ decl_module! { /// - Contains a limited number of reads and writes. /// ----------- /// N is the Number of payouts for the validator (including the validator) - /// Base Weight: - /// - Reward Destination Staked: 110 + 54.2 * N µs (Median Slopes) - /// - Reward Destination Controller (Creating): 120 + 41.95 * N µs (Median Slopes) + /// Weight: + /// - Reward Destination Staked: O(N) + /// - Reward Destination Controller (Creating): O(N) /// DB Weight: /// - Read: EraElectionStatus, CurrentEra, HistoryDepth, ErasValidatorReward, /// ErasStakersClipped, ErasRewardPoints, ErasValidatorPrefs (8 items) @@ -1965,7 +1960,6 @@ decl_module! { /// - Bounded by `MAX_UNLOCKING_CHUNKS`. /// - Storage changes: Can't increase storage, only decrease it. /// --------------- - /// - Base Weight: 34.51 µs * .048 L µs /// - DB Weight: /// - Reads: EraElectionStatus, Ledger, Locks, [Origin Account] /// - Writes: [Origin Account], Locks, Ledger @@ -2000,7 +1994,7 @@ decl_module! { /// /// # /// - E: Number of history depths removed, i.e. 10 -> 7 = 3 - /// - Base Weight: 29.13 * E µs + /// - Weight: O(E) /// - DB Weight: /// - Reads: Current Era, History Depth /// - Writes: History Depth @@ -2035,7 +2029,6 @@ decl_module! { /// /// # /// Complexity: O(S) where S is the number of slashing spans on the account. - /// Base Weight: 75.94 + 2.396 * S µs /// DB Weight: /// - Reads: Stash Account, Bonded, Slashing Spans, Locks /// - Writes: Bonded, Slashing Spans (if S > 0), Ledger, Payee, Validators, Nominators, Stash Account, Locks