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

Commit

Permalink
Co reducing fast-unstake bench time and more (#6552)
Browse files Browse the repository at this point in the history
* update stuff

* remove

* update

* update

* update weights

* fix tests

* update weights

* fix a few small things

* ".git/.scripts/commands/bench/bench.sh" runtime polkadot-dev pallet-fast-unstake

* ".git/.scripts/commands/bench/bench.sh" runtime polkadot-dev pallet-fast-unstake

* ".git/.scripts/commands/bench/bench.sh" runtime kusama-dev pallet-fast-unstake

* reduce batch size

* ".git/.scripts/commands/bench/bench.sh" runtime polkadot-dev pallet-fast-unstake

* update

* fix

* fix

* ".git/.scripts/commands/bench/bench.sh" runtime polkadot-dev pallet-fast-unstake

* update lockfile for {"substrate"}

* fmt

* Env gate migration try_fast_unstake_all

Signed-off-by: Oliver Tale-Yazdi <[email protected]>

---------

Signed-off-by: Oliver Tale-Yazdi <[email protected]>
Co-authored-by: command-bot <>
Co-authored-by: Oliver Tale-Yazdi <[email protected]>
  • Loading branch information
kianenigma and ggwpez authored Jan 27, 2023
1 parent 45f4155 commit 9433dc6
Show file tree
Hide file tree
Showing 10 changed files with 577 additions and 304 deletions.
408 changes: 220 additions & 188 deletions Cargo.lock

Large diffs are not rendered by default.

4 changes: 4 additions & 0 deletions runtime/common/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ sp-npos-elections = { git = "https://github.com/paritytech/substrate", branch =

pallet-authorship = { git = "https://github.com/paritytech/substrate", branch = "master", default-features = false }
pallet-balances = { git = "https://github.com/paritytech/substrate", branch = "master", default-features = false }
pallet-fast-unstake = { git = "https://github.com/paritytech/substrate", branch = "master", default-features = false }
pallet-session = { git = "https://github.com/paritytech/substrate", branch = "master", default-features = false }
frame-support = { git = "https://github.com/paritytech/substrate", branch = "master", default-features = false }
pallet-staking = { git = "https://github.com/paritytech/substrate", branch = "master", default-features = false }
Expand Down Expand Up @@ -84,6 +85,7 @@ std = [
"pallet-balances/std",
"pallet-beefy-mmr/std",
"pallet-session/std",
"pallet-fast-unstake/std",
"pallet-staking/std",
"pallet-staking-reward-fn/std",
"pallet-timestamp/std",
Expand Down Expand Up @@ -111,6 +113,7 @@ runtime-benchmarks = [
"runtime-parachains/runtime-benchmarks",
"pallet-babe/runtime-benchmarks",
"pallet-bags-list/runtime-benchmarks",
"pallet-fast-unstake/runtime-benchmarks"
]
try-runtime = [
"runtime-parachains/try-runtime",
Expand All @@ -122,4 +125,5 @@ try-runtime = [
"pallet-vesting/try-runtime",
"pallet-transaction-payment/try-runtime",
"pallet-treasury/try-runtime",
"pallet-fast-unstake/try-runtime",
]
2 changes: 2 additions & 0 deletions runtime/common/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,8 @@ pub mod purchase;
pub mod slot_range;
pub mod slots;
pub mod traits;
#[cfg(feature = "try-runtime")]
pub mod try_runtime;
pub mod xcm_sender;

#[cfg(test)]
Expand Down
107 changes: 107 additions & 0 deletions runtime/common/src/try_runtime.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,107 @@
// Copyright 2023 Parity Technologies (UK) Ltd.
// This file is part of Polkadot.

// Polkadot is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.

// Polkadot is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.

// You should have received a copy of the GNU General Public License
// along with Polkadot. If not, see <http://www.gnu.org/licenses/>.

//! Common try-runtime only tests for runtimes.

use frame_support::{
dispatch::RawOrigin,
traits::{Get, Hooks},
};
use pallet_fast_unstake::{Pallet as FastUnstake, *};
use pallet_staking::*;
use sp_std::{collections::btree_set::BTreeSet, prelude::*};

/// register all inactive nominators for fast-unstake, and progress until they have all been
/// processed.
pub fn migrate_all_inactive_nominators<T: pallet_fast_unstake::Config + pallet_staking::Config>()
where
<T as frame_system::Config>::RuntimeEvent: TryInto<pallet_fast_unstake::Event<T>>,
{
let mut unstaked_ok = 0;
let mut unstaked_err = 0;
let mut unstaked_slashed = 0;

let all_stakers = Ledger::<T>::iter().map(|(ctrl, l)| (ctrl, l.stash)).collect::<BTreeSet<_>>();
let mut all_exposed = BTreeSet::new();
ErasStakers::<T>::iter().for_each(|(_, val, expo)| {
all_exposed.insert(val);
all_exposed.extend(expo.others.iter().map(|ie| ie.who.clone()))
});

let eligible = all_stakers
.iter()
.filter_map(|(ctrl, stash)| all_exposed.contains(stash).then_some(ctrl))
.collect::<Vec<_>>();

log::info!(
target: "runtime::test",
"registering {} out of {} stakers for fast-unstake",
eligible.len(),
all_stakers.len()
);
for ctrl in eligible {
if let Err(why) =
FastUnstake::<T>::register_fast_unstake(RawOrigin::Signed(ctrl.clone()).into())
{
log::warn!(target: "runtime::test", "failed to register {:?} due to {:?}", ctrl, why);
}
}

log::info!(
target: "runtime::test",
"registered {} successfully, starting at {:?}.",
Queue::<T>::count(),
frame_system::Pallet::<T>::block_number(),
);
while Queue::<T>::count() != 0 || Head::<T>::get().is_some() {
let now = frame_system::Pallet::<T>::block_number();
let weight = <T as frame_system::Config>::BlockWeights::get().max_block;
let consumed = FastUnstake::<T>::on_idle(now, weight);
log::debug!(target: "runtime::test", "consumed {:?} ({})", consumed, consumed.ref_time() as f32 / weight.ref_time() as f32);

frame_system::Pallet::<T>::read_events_no_consensus()
.into_iter()
.map(|r| r.event)
.filter_map(|e| {
let maybe_fast_unstake_event: Option<pallet_fast_unstake::Event<T>> =
e.try_into().ok();
maybe_fast_unstake_event
})
.for_each(|e: pallet_fast_unstake::Event<T>| match e {
pallet_fast_unstake::Event::<T>::Unstaked { result, .. } =>
if result.is_ok() {
unstaked_ok += 1;
} else {
unstaked_err += 1
},
pallet_fast_unstake::Event::<T>::Slashed { .. } => unstaked_slashed += 1,
pallet_fast_unstake::Event::<T>::InternalError => unreachable!(),
_ => {},
});

if now % 100u32.into() == sp_runtime::traits::Zero::zero() {
log::info!(
target: "runtime::test",
"status: ok {}, err {}, slash {}",
unstaked_ok,
unstaked_err,
unstaked_slashed,
);
}

frame_system::Pallet::<T>::reset_events();
}
}
50 changes: 49 additions & 1 deletion runtime/kusama/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -607,8 +607,11 @@ impl pallet_fast_unstake::Config for Runtime {
EnsureRoot<AccountId>,
pallet_collective::EnsureProportionAtLeast<AccountId, CouncilCollective, 1, 2>,
>;
type WeightInfo = weights::pallet_fast_unstake::WeightInfo<Runtime>;
type Staking = Staking;
type MaxErasToCheckPerBlock = ConstU32<1>;
#[cfg(feature = "runtime-benchmarks")]
type MaxBackersPerValidator = MaxNominatorRewardedPerValidator;
type WeightInfo = weights::pallet_fast_unstake::WeightInfo<Runtime>;
}

parameter_types! {
Expand Down Expand Up @@ -2133,6 +2136,19 @@ mod multiplier_tests {
})
}

#[test]
fn fast_unstake_estimate() {
use pallet_fast_unstake::WeightInfo;
let block_time = BlockWeights::get().max_block.ref_time() as f32;
let on_idle = weights::pallet_fast_unstake::WeightInfo::<Runtime>::on_idle_check(
1000,
<Runtime as pallet_fast_unstake::Config>::BatchSize::get(),
)
.ref_time() as f32;
println!("ratio of block weight for full batch fast-unstake {}", on_idle / block_time);
assert!(on_idle / block_time <= 0.5f32)
}

#[test]
#[ignore]
fn multiplier_growth_simulator() {
Expand Down Expand Up @@ -2251,4 +2267,36 @@ mod remote_tests {
.unwrap();
ext.execute_with(|| Runtime::on_runtime_upgrade(UpgradeCheckSelect::PreAndPost));
}

#[tokio::test]
async fn try_fast_unstake_all() {
if var("RUN_MIGRATION_TESTS").is_err() {
return
}

sp_tracing::try_init_simple();
let transport: Transport =
var("WS").unwrap_or("wss://kusama-rpc.polkadot.io:443".to_string()).into();
let maybe_state_snapshot: Option<SnapshotConfig> = var("SNAP").map(|s| s.into()).ok();
let mut ext = Builder::<Block>::default()
.mode(if let Some(state_snapshot) = maybe_state_snapshot {
Mode::OfflineOrElseOnline(
OfflineConfig { state_snapshot: state_snapshot.clone() },
OnlineConfig {
transport,
state_snapshot: Some(state_snapshot),
..Default::default()
},
)
} else {
Mode::Online(OnlineConfig { transport, ..Default::default() })
})
.build()
.await
.unwrap();
ext.execute_with(|| {
pallet_fast_unstake::ErasToCheckPerBlock::<Runtime>::put(1);
runtime_common::try_runtime::migrate_all_inactive_nominators::<Runtime>()
});
}
}
92 changes: 51 additions & 41 deletions runtime/kusama/src/weights/pallet_fast_unstake.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,21 +16,23 @@
//! Autogenerated weights for `pallet_fast_unstake`
//!
//! THIS FILE WAS AUTO-GENERATED USING THE SUBSTRATE BENCHMARK CLI VERSION 4.0.0-dev
//! DATE: 2023-01-23, STEPS: `50`, REPEAT: 20, LOW RANGE: `[]`, HIGH RANGE: `[]`
//! HOSTNAME: `runner-b3zmxxc-project-163-concurrent-0`, CPU: `Intel(R) Xeon(R) CPU @ 2.60GHz`
//! DATE: 2023-01-15, STEPS: `50`, REPEAT: 20, LOW RANGE: `[]`, HIGH RANGE: `[]`
//! HOSTNAME: `bm3`, CPU: `Intel(R) Core(TM) i7-7700K CPU @ 4.20GHz`
//! EXECUTION: Some(Wasm), WASM-EXECUTION: Compiled, CHAIN: Some("kusama-dev"), DB CACHE: 1024

// Executed Command:
// ./target/production/polkadot
// /home/benchbot/cargo_target_dir/production/polkadot
// benchmark
// pallet
// --chain=kusama-dev
// --steps=50
// --repeat=20
// --pallet=pallet_fast_unstake
// --extrinsic=*
// --execution=wasm
// --wasm-execution=compiled
// --heap-pages=4096
// --json-file=/var/lib/gitlab-runner/builds/zyw4fam_/0/parity/mirrors/polkadot/.git/.artifacts/bench.json
// --pallet=pallet-fast-unstake
// --chain=kusama-dev
// --header=./file_header.txt
// --output=./runtime/kusama/src/weights/

Expand All @@ -46,40 +48,48 @@ pub struct WeightInfo<T>(PhantomData<T>);
impl<T: frame_system::Config> pallet_fast_unstake::WeightInfo for WeightInfo<T> {
// Storage: FastUnstake ErasToCheckPerBlock (r:1 w:0)
// Storage: Staking ValidatorCount (r:1 w:0)
// Storage: ElectionProviderMultiPhase CurrentPhase (r:1 w:0)
// Storage: FastUnstake Head (r:1 w:1)
// Storage: FastUnstake CounterForQueue (r:1 w:0)
// Storage: ElectionProviderMultiPhase CurrentPhase (r:1 w:0)
// Storage: Staking CurrentEra (r:1 w:0)
// Storage: Staking SlashingSpans (r:64 w:0)
// Storage: Staking Bonded (r:64 w:64)
// Storage: Staking Validators (r:64 w:0)
// Storage: Staking Nominators (r:64 w:0)
// Storage: System Account (r:64 w:64)
// Storage: Balances Locks (r:64 w:64)
// Storage: Staking Ledger (r:0 w:64)
// Storage: Staking Payee (r:0 w:64)
fn on_idle_unstake() -> Weight {
// Minimum execution time: 2_547_167 nanoseconds.
Weight::from_ref_time(2_569_186_000)
.saturating_add(T::DbWeight::get().reads(389))
.saturating_add(T::DbWeight::get().writes(321))
// Storage: Staking SlashingSpans (r:1 w:0)
// Storage: Staking Bonded (r:1 w:1)
// Storage: Staking Validators (r:1 w:0)
// Storage: Staking Nominators (r:1 w:0)
// Storage: System Account (r:1 w:1)
// Storage: Balances Locks (r:1 w:1)
// Storage: Staking Ledger (r:0 w:1)
// Storage: Staking Payee (r:0 w:1)
/// The range of component `b` is `[1, 64]`.
fn on_idle_unstake(b: u32, ) -> Weight {
// Minimum execution time: 77_225 nanoseconds.
Weight::from_ref_time(45_851_915)
// Standard Error: 27_205
.saturating_add(Weight::from_ref_time(34_256_381).saturating_mul(b.into()))
.saturating_add(T::DbWeight::get().reads(6))
.saturating_add(T::DbWeight::get().reads((6_u64).saturating_mul(b.into())))
.saturating_add(T::DbWeight::get().writes(1))
.saturating_add(T::DbWeight::get().writes((5_u64).saturating_mul(b.into())))
}
// Storage: FastUnstake ErasToCheckPerBlock (r:1 w:0)
// Storage: Staking ValidatorCount (r:1 w:0)
// Storage: ElectionProviderMultiPhase CurrentPhase (r:1 w:0)
// Storage: FastUnstake Head (r:1 w:1)
// Storage: FastUnstake Queue (r:65 w:64)
// Storage: FastUnstake CounterForQueue (r:1 w:1)
// Storage: FastUnstake CounterForQueue (r:1 w:0)
// Storage: ElectionProviderMultiPhase CurrentPhase (r:1 w:0)
// Storage: Staking CurrentEra (r:1 w:0)
// Storage: Staking ErasStakers (r:56 w:0)
/// The range of component `x` is `[28, 3584]`.
fn on_idle_check(x: u32, ) -> Weight {
// Minimum execution time: 25_160_452 nanoseconds.
Weight::from_ref_time(25_413_896_000)
// Standard Error: 556_413
.saturating_add(Weight::from_ref_time(700_987_201).saturating_mul(x.into()))
.saturating_add(T::DbWeight::get().reads(85))
.saturating_add(T::DbWeight::get().reads((1_u64).saturating_mul(x.into())))
.saturating_add(T::DbWeight::get().writes(66))
// Storage: Staking ErasStakers (r:2 w:0)
/// The range of component `v` is `[1, 256]`.
/// The range of component `b` is `[1, 64]`.
fn on_idle_check(v: u32, b: u32, ) -> Weight {
// Minimum execution time: 1_671_682 nanoseconds.
Weight::from_ref_time(1_683_909_000)
// Standard Error: 16_637_473
.saturating_add(Weight::from_ref_time(533_652_266).saturating_mul(v.into()))
// Standard Error: 66_568_424
.saturating_add(Weight::from_ref_time(2_085_678_765).saturating_mul(b.into()))
.saturating_add(T::DbWeight::get().reads(7))
.saturating_add(T::DbWeight::get().reads((1_u64).saturating_mul(v.into())))
.saturating_add(T::DbWeight::get().writes(1))
}
// Storage: FastUnstake ErasToCheckPerBlock (r:1 w:0)
// Storage: Staking Ledger (r:1 w:1)
Expand All @@ -89,33 +99,33 @@ impl<T: frame_system::Config> pallet_fast_unstake::WeightInfo for WeightInfo<T>
// Storage: Staking Validators (r:1 w:0)
// Storage: Staking Nominators (r:1 w:1)
// Storage: Staking CounterForNominators (r:1 w:1)
// Storage: VoterList ListNodes (r:2 w:2)
// Storage: VoterList ListNodes (r:1 w:1)
// Storage: VoterList ListBags (r:1 w:1)
// Storage: VoterList CounterForListNodes (r:1 w:1)
// Storage: Staking CurrentEra (r:1 w:0)
// Storage: Balances Locks (r:1 w:1)
// Storage: FastUnstake CounterForQueue (r:1 w:1)
fn register_fast_unstake() -> Weight {
// Minimum execution time: 156_147 nanoseconds.
Weight::from_ref_time(158_284_000)
.saturating_add(T::DbWeight::get().reads(15))
.saturating_add(T::DbWeight::get().writes(10))
// Minimum execution time: 106_509 nanoseconds.
Weight::from_ref_time(107_537_000)
.saturating_add(T::DbWeight::get().reads(14))
.saturating_add(T::DbWeight::get().writes(9))
}
// Storage: FastUnstake ErasToCheckPerBlock (r:1 w:0)
// Storage: Staking Ledger (r:1 w:0)
// Storage: FastUnstake Queue (r:1 w:1)
// Storage: FastUnstake Head (r:1 w:0)
// Storage: FastUnstake CounterForQueue (r:1 w:1)
fn deregister() -> Weight {
// Minimum execution time: 62_835 nanoseconds.
Weight::from_ref_time(65_108_000)
// Minimum execution time: 43_328 nanoseconds.
Weight::from_ref_time(43_904_000)
.saturating_add(T::DbWeight::get().reads(5))
.saturating_add(T::DbWeight::get().writes(2))
}
// Storage: FastUnstake ErasToCheckPerBlock (r:0 w:1)
fn control() -> Weight {
// Minimum execution time: 3_747 nanoseconds.
Weight::from_ref_time(4_132_000)
// Minimum execution time: 4_153 nanoseconds.
Weight::from_ref_time(4_337_000)
.saturating_add(T::DbWeight::get().writes(1))
}
}
Loading

0 comments on commit 9433dc6

Please sign in to comment.