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

Bump Nominator Reward Limits #1668

Merged
7 commits merged into from
Sep 16, 2020
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion runtime/kusama/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -317,7 +317,7 @@ parameter_types! {
// 27 eras in which slashes can be cancelled (slightly less than 7 days).
pub const SlashDeferDuration: pallet_staking::EraIndex = 27;
pub const RewardCurve: &'static PiecewiseLinear<'static> = &REWARD_CURVE;
pub const MaxNominatorRewardedPerValidator: u32 = 64;
pub const MaxNominatorRewardedPerValidator: u32 = 128;
kianenigma marked this conversation as resolved.
Show resolved Hide resolved
// quarter of the last session will be for election.
pub const ElectionLookahead: BlockNumber = EPOCH_DURATION_IN_BLOCKS / 4;
pub const MaxIterations: u32 = 10;
Expand Down
2 changes: 1 addition & 1 deletion runtime/polkadot/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -326,7 +326,7 @@ parameter_types! {
pub const BondingDuration: pallet_staking::EraIndex = 28;
pub const SlashDeferDuration: pallet_staking::EraIndex = 27;
pub const RewardCurve: &'static PiecewiseLinear<'static> = &REWARD_CURVE;
pub const MaxNominatorRewardedPerValidator: u32 = 64;
pub const MaxNominatorRewardedPerValidator: u32 = 256;
// last 15 minutes of the last session will be for election.
pub const ElectionLookahead: BlockNumber = EPOCH_DURATION_IN_BLOCKS / 16;
pub const MaxIterations: u32 = 10;
Expand Down
88 changes: 69 additions & 19 deletions runtime/polkadot/tests/weights.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,10 +21,17 @@
//! weights in Substrate will change. Instead they are supposed to provide
//! some sort of indicator that calls we consider important (e.g pallet_balances::transfer)
//! have not suddenly changed from under us.
//!
//! Some of the tests in this crate print insightful logs. Run with:
//!
//! ```
//! $ cargo test -p polkadot-runtime -- --nocapture --test-threads=1
//! ```

use codec::Encode;
use frame_support::{
traits::ContainsLengthBound,
weights::{constants::*, GetDispatchInfo, Weight},
weights::{constants::*, GetDispatchInfo, Weight, DispatchInfo},
};
use keyring::AccountKeyring;
use polkadot_runtime::constants::currency::*;
Expand All @@ -40,10 +47,26 @@ use pallet_treasury::Call as TreasuryCall;

type DbWeight = <Runtime as frame_system::Trait>::DbWeight;


fn report_portion(name: &'static str, info: DispatchInfo, len: usize) {
let maximum_weight = <Runtime as frame_system::Trait>::MaximumBlockWeight::get();
let fee = sp_io::TestExternalities::new(Default::default()).execute_with(|| {
<pallet_transaction_payment::Module<Runtime>>::compute_fee(len as u32, &info, 0)
});

let portion = info.weight as f64 / maximum_weight as f64;

if portion > 0.5 {
panic!("Weight of some call seem to have exceeded half of the block. Probably something is wrong.");
}

println!("\nCall {} (with default args) takes {} of the block weight, pays {} in fee.", name, portion, fee);
}

#[test]
fn sanity_check_weight_per_time_constants_are_as_expected() {
// These values comes from Substrate, we want to make sure that if it
// ever changes we don't accidently break Polkadot
// ever changes we don't accidentally break Polkadot
assert_eq!(WEIGHT_PER_SECOND, 1_000_000_000_000);
assert_eq!(WEIGHT_PER_MILLIS, WEIGHT_PER_SECOND / 1000);
assert_eq!(WEIGHT_PER_MICROS, WEIGHT_PER_MILLIS / 1000);
Expand All @@ -54,37 +77,64 @@ fn sanity_check_weight_per_time_constants_are_as_expected() {
fn weight_of_staking_bond_is_correct() {
let controller: AccountId = AccountKeyring::Alice.into();

// #[weight = 67 * WEIGHT_PER_MICROS + T::DbWeight::get().reads_writes(5, 4)]
let expected_weight = 67 * WEIGHT_PER_MICROS + (DbWeight::get().read * 5) + (DbWeight::get().write * 4);
let weight = StakingCall::bond::<Runtime>(controller, 1 * DOLLARS, Default::default()).get_dispatch_info().weight;
// (144278000 as Weight)
// .saturating_add(DbWeight::get().reads(5 as Weight))
// .saturating_add(DbWeight::get().writes(4 as Weight))
let expected_weight = 144278000 + (DbWeight::get().read * 5) + (DbWeight::get().write * 4);
let call = StakingCall::bond::<Runtime>(controller, 1 * DOLLARS, Default::default());
let info = call.get_dispatch_info();

assert_eq!(weight, expected_weight);
assert_eq!(info.weight, expected_weight);
report_portion("staking_bond", info, call.encode().len())
}

#[test]
fn weight_of_staking_validate_is_correct() {
// #[weight = 17 * WEIGHT_PER_MICROS + T::DbWeight::get().reads_writes(2, 2)]
let expected_weight = 17 * WEIGHT_PER_MICROS + (DbWeight::get().read * 2) + (DbWeight::get().write * 2);
let weight = StakingCall::validate::<Runtime>(Default::default()).get_dispatch_info().weight;

assert_eq!(weight, expected_weight);
// (35539000 as Weight)
// .saturating_add(DbWeight::get().reads(2 as Weight))
// .saturating_add(DbWeight::get().writes(2 as Weight))
let expected_weight = 35539000 + (DbWeight::get().read * 2) + (DbWeight::get().write * 2);
let call = StakingCall::validate::<Runtime>(Default::default());
let info = call.get_dispatch_info();

assert_eq!(info.weight, expected_weight);
report_portion("staking_validate", info, call.encode().len())
}

#[test]
fn weight_of_staking_nominate_is_correct() {
let targets: Vec<AccountId> = vec![Default::default(), Default::default(), Default::default()];

// #[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))
// ]
// (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))
let db_weight = (DbWeight::get().read * 3) + (DbWeight::get().write * 2);
let targets_weight = (360 * WEIGHT_PER_NANOS).saturating_mul(targets.len() as Weight);
let targets_weight = (308 * WEIGHT_PER_NANOS).saturating_mul(targets.len() as Weight);

let expected_weight = db_weight.saturating_add(22 * WEIGHT_PER_MICROS).saturating_add(targets_weight);
let weight = StakingCall::nominate::<Runtime>(targets).get_dispatch_info().weight;
let expected_weight = db_weight.saturating_add(48596000).saturating_add(targets_weight);
let call = StakingCall::nominate::<Runtime>(targets);
let info = call.get_dispatch_info();

assert_eq!(weight, expected_weight);
assert_eq!(info.weight, expected_weight);
report_portion("staking_nominate", info, call.encode().len())
}

#[test]
fn weight_of_staking_payout_staker_is_correct() {
// (0 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)))
let call = StakingCall::payout_stakers::<Runtime>(Default::default(), 0u32);
let info = call.get_dispatch_info();

let n = <Runtime as pallet_staking::Trait>::MaxNominatorRewardedPerValidator::get() as Weight;
let mut expected_weight = (117324000 as Weight).saturating_mul(n as Weight);
expected_weight += (DbWeight::get().read * 5 * n) + (DbWeight::get().write * 3 * n);

assert_eq!(info.weight, expected_weight);
report_portion("staking_payout_stakers", info, call.encode().len())
}

#[test]
Expand Down