Move Staking Weights to T::WeightInfo (#7007)

* Fix the benchmarks

* Migrate staking to weightInfo

* Fix global benchmarks

* re-calculate the submit solution weight.

* Fix some refund.

* Get rid of all the extra parameters.

* Fix staking tests.

* new values from the bench machine.

* Fix some grumbles

* better macro

* Some better doc

* Move to interpreted wasm

* Make it work temporarily

* Final fix of default ones.

* Fix payout benchmarks

* Fix payout stuff

* One last fix

* use benchmarking machine for numbers

* update weight docs

Co-authored-by: Shawn Tabrizi <shawntabrizi@gmail.com>
This commit is contained in:
Kian Paimani
2020-09-16 14:25:31 +02:00
committed by GitHub
parent 7a075a990f
commit 83bde4c1e6
8 changed files with 660 additions and 378 deletions
+97 -232
View File
@@ -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::<ValidatorIndex>() <= size_of::<usize>());
static_assertions::const_assert!(size_of::<NominatorIndex>() <= size_of::<usize>());
static_assertions::const_assert!(size_of::<ValidatorIndex>() <= size_of::<u32>());
static_assertions::const_assert!(size_of::<NominatorIndex>() <= size_of::<u32>());
/// Maximum number of stakers that can be stored in a snapshot.
pub(crate) const MAX_VALIDATORS: usize = ValidatorIndex::max_value() as usize;
@@ -766,132 +769,31 @@ impl<T: Trait> SessionInterface<<T as frame_system::Trait>::AccountId> for T whe
}
}
pub 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<T: Trait>(
winners: &Vec<ValidatorIndex>,
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<T: Trait>(
winners: &Vec<ValidatorIndex>,
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::<T>(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(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;
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;
fn new_era(v: u32, n: u32, ) -> Weight;
fn do_slash(l: u32, ) -> 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<Call<Self>> {
@@ -1489,12 +1391,12 @@ 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
/// # </weight>
#[weight = 67 * WEIGHT_PER_MICROS + T::DbWeight::get().reads_writes(5, 4)]
#[weight = T::WeightInfo::bond()]
pub fn bond(origin,
controller: <T::Lookup as StaticLookup>::Source,
#[compact] value: BalanceOf<T>,
@@ -1558,12 +1460,11 @@ 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
/// # </weight>
#[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<T>) {
ensure!(Self::era_election_status().is_closed(), Error::<T>::CallNotAllowed);
let stash = ensure_signed(origin)?;
@@ -1609,12 +1510,12 @@ decl_module! {
/// `withdraw_unbonded`.
/// - One DB entry.
/// ----------
/// Base Weight: 50.34 µs
/// Weight: O(1)
/// 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>
#[weight = 50 * WEIGHT_PER_MICROS + T::DbWeight::get().reads_writes(4, 2)]
#[weight = T::WeightInfo::unbond()]
fn unbond(origin, #[compact] value: BalanceOf<T>) {
ensure!(Self::era_election_status().is_closed(), Error::<T>::CallNotAllowed);
let controller = ensure_signed(origin)?;
@@ -1663,25 +1564,18 @@ 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
/// - 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
/// 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,
/// [Origin Account], Locks, BalanceOf stash.
/// - Writes Each: SpanSlash * S
/// NOTE: Weight annotation is the kill scenario, we refund otherwise.
/// # </weight>
#[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::<T>::CallNotAllowed);
let controller = ensure_signed(origin)?;
@@ -1703,8 +1597,9 @@ 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.
Some(T::WeightInfo::withdraw_unbonded_update(num_slashing_spans))
};
// `old_total` should never be less than the new total because
@@ -1730,12 +1625,12 @@ 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
/// # </weight>
#[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::<T>::CallNotAllowed);
let controller = ensure_signed(origin)?;
@@ -1758,16 +1653,13 @@ 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
/// - Writes: Validators, Nominators
/// # </weight>
#[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<<T::Lookup as StaticLookup>::Source>) {
ensure!(Self::era_election_status().is_closed(), Error::<T>::CallNotAllowed);
let controller = ensure_signed(origin)?;
@@ -1802,12 +1694,12 @@ 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
/// # </weight>
#[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::<T>::CallNotAllowed);
let controller = ensure_signed(origin)?;
@@ -1826,12 +1718,12 @@ 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
/// # </weight>
#[weight = 11 * WEIGHT_PER_MICROS + T::DbWeight::get().reads_writes(1, 1)]
#[weight = T::WeightInfo::set_payee()]
fn set_payee(origin, payee: RewardDestination<T::AccountId>) {
let controller = ensure_signed(origin)?;
let ledger = Self::ledger(&controller).ok_or(Error::<T>::NotController)?;
@@ -1850,12 +1742,12 @@ 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
/// # </weight>
#[weight = 25 * WEIGHT_PER_MICROS + T::DbWeight::get().reads_writes(3, 3)]
#[weight = T::WeightInfo::set_controller()]
fn set_controller(origin, controller: <T::Lookup as StaticLookup>::Source) {
let stash = ensure_signed(origin)?;
let old_controller = Self::bonded(&stash).ok_or(Error::<T>::NotStash)?;
@@ -1876,10 +1768,10 @@ decl_module! {
/// The dispatch origin must be Root.
///
/// # <weight>
/// Base Weight: 1.717 µs
/// Weight: O(1)
/// Write: Validator Count
/// # </weight>
#[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 +1782,9 @@ decl_module! {
/// The dispatch origin must be Root.
///
/// # <weight>
/// Base Weight: 1.717 µs
/// Read/Write: Validator Count
/// Same as [`set_validator_count`].
/// # </weight>
#[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 +1795,9 @@ decl_module! {
/// The dispatch origin must be Root.
///
/// # <weight>
/// Base Weight: 1.717 µs
/// Read/Write: Validator Count
/// Same as [`set_validator_count`].
/// # </weight>
#[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);
@@ -1919,10 +1809,10 @@ decl_module! {
///
/// # <weight>
/// - No arguments.
/// - Base Weight: 1.857 µs
/// - Weight: O(1)
/// - Write: ForceEra
/// # </weight>
#[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);
@@ -1935,10 +1825,10 @@ decl_module! {
///
/// # <weight>
/// - No arguments.
/// - Base Weight: 1.959 µs
/// - Weight: O(1)
/// - Write ForceEra
/// # </weight>
#[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);
@@ -1950,16 +1840,12 @@ decl_module! {
///
/// # <weight>
/// - O(V)
/// - Base Weight: 2.208 + .006 * V µs
/// - Write: Invulnerables
/// # </weight>
#[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<T::AccountId>) {
#[weight = T::WeightInfo::set_invulnerables(invulnerables.len() as u32)]
fn set_invulnerables(origin, invulnerables: Vec<T::AccountId>) {
ensure_root(origin)?;
<Invulnerables<T>>::put(validators);
<Invulnerables<T>>::put(invulnerables);
}
/// Force a current staker to become completely unstaked, immediately.
@@ -1968,20 +1854,11 @@ decl_module! {
///
/// # <weight>
/// 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
/// # </weight>
#[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)?;
@@ -1997,10 +1874,10 @@ decl_module! {
/// The dispatch origin must be Root.
///
/// # <weight>
/// - Base Weight: 2.05 µs
/// - Weight: O(1)
/// - Write: ForceEra
/// # </weight>
#[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);
@@ -2016,14 +1893,10 @@ 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
/// # </weight>
#[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<u32>) {
T::SlashCancelOrigin::ensure_origin(origin)?;
@@ -2058,22 +1931,19 @@ 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)
/// - 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 alive stash account (Staked).
/// Paying even a dead controller is cheaper weight-wise. We don't do any refunds here.
/// # </weight>
#[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_alive_staked(T::MaxNominatorRewardedPerValidator::get())]
fn payout_stakers(origin, validator_stash: T::AccountId, era: EraIndex) -> DispatchResult {
ensure!(Self::era_election_status().is_closed(), Error::<T>::CallNotAllowed);
ensure_signed(origin)?;
@@ -2090,16 +1960,11 @@ 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
/// # </weight>
#[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<T>) -> DispatchResultWithPostInfo {
ensure!(Self::era_election_status().is_closed(), Error::<T>::CallNotAllowed);
let controller = ensure_signed(origin)?;
@@ -2129,19 +1994,14 @@ decl_module! {
///
/// # <weight>
/// - 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
/// - Clear Prefix Each: Era Stakers, EraStakersClipped, ErasValidatorPrefs
/// - Writes Each: ErasValidatorReward, ErasRewardPoints, ErasTotalStake, ErasStartSessionIndex
/// # </weight>
#[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,
@@ -2169,21 +2029,12 @@ decl_module! {
///
/// # <weight>
/// 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
/// - Writes Each: SpanSlash * S
/// # </weight>
#[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::<T>::FundedTarget);
Self::kill_stash(&stash, num_slashing_spans)?;
@@ -2235,9 +2086,16 @@ decl_module! {
/// minimized (to ensure less variance)
///
/// # <weight>
/// 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::weight_for_submit_solution::<T>(winners, compact, size)]
#[weight = T::WeightInfo::submit_solution_better(
size.validators.into(),
size.nominators.into(),
compact.len() as u32,
winners.len() as u32,
)]
pub fn submit_election_solution(
origin,
winners: Vec<ValidatorIndex>,
@@ -2266,7 +2124,12 @@ decl_module! {
/// # <weight>
/// See `crate::weight` module.
/// # </weight>
#[weight = weight::weight_for_submit_solution::<T>(winners, compact, size)]
#[weight = T::WeightInfo::submit_solution_better(
size.validators.into(),
size.nominators.into(),
compact.len() as u32,
winners.len() as u32,
)]
pub fn submit_election_solution_unsigned(
origin,
winners: Vec<ValidatorIndex>,
@@ -2580,13 +2443,16 @@ impl<T: Trait> Module<T> {
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::<T>(
&winners,
&compact_assignments,
&election_size,
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::<T>::OffchainElectionBogusWinnerCount,
);
// Check that the number of presented winners is sane. Most often we have more candidates
@@ -2744,7 +2610,7 @@ impl<T: Trait> Module<T> {
// emit event.
Self::deposit_event(RawEvent::SolutionStored(compute));
Ok(Some(adjusted_weight).into())
Ok(None.into())
}
/// Start a session potentially starting an era.
@@ -2862,7 +2728,6 @@ impl<T: Trait> Module<T> {
maybe_new_validators
}
/// Remove all the storage items associated with the election.
fn close_election_window() {
// Close window.