mirror of
https://github.com/pezkuwichain/pezkuwi-subxt.git
synced 2026-06-24 13:41:06 +00:00
0c69651830
* Base features and traits. * pallet and unsigned phase * Undo bad formattings. * some formatting cleanup. * Small self-cleanup. * Make it all build * self-review * Some doc tests. * Some changes from other PR * Fix session test * Update Cargo.lock * Update frame/election-provider-multi-phase/src/lib.rs Co-authored-by: Guillaume Thiolliere <gui.thiolliere@gmail.com> * Some review comments * Rename + make encode/decode * Do an assert as well, just in case. * Fix build * Update frame/election-provider-multi-phase/src/unsigned.rs Co-authored-by: Guillaume Thiolliere <gui.thiolliere@gmail.com> * Las comment * fix staking fuzzer. * cargo run --release --features=runtime-benchmarks --manifest-path=bin/node/cli/Cargo.toml -- benchmark --chain=dev --steps=50 --repeat=20 --pallet=pallet_election_provider_multi_phase --extrinsic=* --execution=wasm --wasm-execution=compiled --heap-pages=4096 --output=./frame/election-provider-multi-phase/src/weights.rs --template=./.maintain/frame-weight-template.hbs * Add one last layer of feasibility check as well. * Last fixes to benchmarks * Some more docs. * cargo run --release --features=runtime-benchmarks --manifest-path=bin/node/cli/Cargo.toml -- benchmark --chain=dev --steps=50 --repeat=20 --pallet=pallet_election_provider_multi_phase --extrinsic=* --execution=wasm --wasm-execution=compiled --heap-pages=4096 --output=./frame/election-provider-multi-phase/src/weights.rs --template=./.maintain/frame-weight-template.hbs * cargo run --release --features=runtime-benchmarks --manifest-path=bin/node/cli/Cargo.toml -- benchmark --chain=dev --steps=50 --repeat=20 --pallet=pallet_election_provider_multi_phase --extrinsic=* --execution=wasm --wasm-execution=compiled --heap-pages=4096 --output=./frame/election-provider-multi-phase/src/weights.rs --template=./.maintain/frame-weight-template.hbs * Some nits * It all works * Some self cleanup * Update frame/staking/src/lib.rs Co-authored-by: Peter Goodspeed-Niklaus <coriolinus@users.noreply.github.com> * remove most todos. * Round of self-review. * Fix migration * clean macro * Revert wrong merge * remove fuzzer stuff. * Self review * Update frame/staking/src/lib.rs Co-authored-by: Guillaume Thiolliere <gui.thiolliere@gmail.com> * review comments * add logs * Add tests to demonstrate the capacity of the snapshot. * Replace upgrade * Last touches * Fix benchmakrs * cargo run --release --features=runtime-benchmarks --manifest-path=bin/node/cli/Cargo.toml -- benchmark --chain=dev --steps=50 --repeat=20 --pallet=pallet_staking --extrinsic=* --execution=wasm --wasm-execution=compiled --heap-pages=4096 --output=./frame/staking/src/weights.rs --template=./.maintain/frame-weight-template.hbs * cargo run --release --features=runtime-benchmarks --manifest-path=bin/node/cli/Cargo.toml -- benchmark --chain=dev --steps=50 --repeat=20 --pallet=pallet_election_provider_multi_phase --extrinsic=* --execution=wasm --wasm-execution=compiled --heap-pages=4096 --output=./frame/election-provider-multi-phase/src/weights.rs --template=./.maintain/frame-weight-template.hbs * remove unused stuff * Fix tests. Co-authored-by: Shawn Tabrizi <shawntabrizi@gmail.com> Co-authored-by: Guillaume Thiolliere <gui.thiolliere@gmail.com> Co-authored-by: Parity Benchmarking Bot <admin@parity.io> Co-authored-by: Peter Goodspeed-Niklaus <coriolinus@users.noreply.github.com>
178 lines
6.7 KiB
Rust
178 lines
6.7 KiB
Rust
// This file is part of Substrate.
|
|
|
|
// Copyright (C) 2020-2021 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.
|
|
|
|
//! Testing utils for staking. Provides some common functions to setup staking state, such as
|
|
//! bonding validators, nominators, and generating different types of solutions.
|
|
|
|
use crate::*;
|
|
use crate::Module as Staking;
|
|
use frame_benchmarking::account;
|
|
use frame_system::RawOrigin;
|
|
use sp_io::hashing::blake2_256;
|
|
use rand_chacha::{rand_core::{RngCore, SeedableRng}, ChaChaRng};
|
|
|
|
const SEED: u32 = 0;
|
|
|
|
/// This function removes all validators and nominators from storage.
|
|
pub fn clear_validators_and_nominators<T: Config>() {
|
|
Validators::<T>::remove_all();
|
|
Nominators::<T>::remove_all();
|
|
}
|
|
|
|
/// Grab a funded user.
|
|
pub fn create_funded_user<T: Config>(
|
|
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);
|
|
// ensure T::CurrencyToVote will work correctly.
|
|
T::Currency::issue(balance);
|
|
user
|
|
}
|
|
|
|
/// Create a stash and controller pair.
|
|
pub fn create_stash_controller<T: Config>(
|
|
n: u32,
|
|
balance_factor: u32,
|
|
destination: RewardDestination<T::AccountId>,
|
|
)
|
|
-> Result<(T::AccountId, T::AccountId), &'static str>
|
|
{
|
|
let stash = create_funded_user::<T>("stash", n, balance_factor);
|
|
let controller = create_funded_user::<T>("controller", n, balance_factor);
|
|
let controller_lookup: <T::Lookup as StaticLookup>::Source = T::Lookup::unlookup(controller.clone());
|
|
let amount = T::Currency::minimum_balance() * (balance_factor / 10).max(1).into();
|
|
Staking::<T>::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<T: Config>(
|
|
n: u32,
|
|
balance_factor: u32,
|
|
destination: RewardDestination<T::AccountId>,
|
|
)
|
|
-> Result<(T::AccountId, T::AccountId), &'static str>
|
|
{
|
|
let stash = create_funded_user::<T>("stash", n, balance_factor);
|
|
// controller has no funds
|
|
let controller = create_funded_user::<T>("controller", n, 0);
|
|
let controller_lookup: <T::Lookup as StaticLookup>::Source = T::Lookup::unlookup(controller.clone());
|
|
let amount = T::Currency::minimum_balance() * (balance_factor / 10).max(1).into();
|
|
Staking::<T>::bond(RawOrigin::Signed(stash.clone()).into(), controller_lookup, amount, destination)?;
|
|
return Ok((stash, controller))
|
|
}
|
|
|
|
/// create `max` validators.
|
|
pub fn create_validators<T: Config>(
|
|
max: u32,
|
|
balance_factor: u32,
|
|
) -> Result<Vec<<T::Lookup as StaticLookup>::Source>, &'static str> {
|
|
let mut validators: Vec<<T::Lookup as StaticLookup>::Source> = Vec::with_capacity(max as usize);
|
|
for i in 0 .. max {
|
|
let (stash, controller) = create_stash_controller::<T>(i, balance_factor, RewardDestination::Staked)?;
|
|
let validator_prefs = ValidatorPrefs {
|
|
commission: Perbill::from_percent(50),
|
|
.. Default::default()
|
|
};
|
|
Staking::<T>::validate(RawOrigin::Signed(controller).into(), validator_prefs)?;
|
|
let stash_lookup: <T::Lookup as StaticLookup>::Source = T::Lookup::unlookup(stash);
|
|
validators.push(stash_lookup);
|
|
}
|
|
Ok(validators)
|
|
}
|
|
|
|
/// This function generates validators and nominators who are randomly nominating
|
|
/// `edge_per_nominator` random validators (until `to_nominate` if provided).
|
|
///
|
|
/// NOTE: This function will remove any existing validators or nominators to ensure
|
|
/// we are working with a clean state.
|
|
///
|
|
/// Parameters:
|
|
/// - `validators`: number of bonded validators
|
|
/// - `nominators`: number of bonded nominators.
|
|
/// - `edge_per_nominator`: number of edge (vote) per nominator.
|
|
/// - `randomize_stake`: whether to randomize the stakes.
|
|
/// - `to_nominate`: if `Some(n)`, only the first `n` bonded validator are voted upon. Else, all of
|
|
/// them are considered and `edge_per_nominator` random validators are voted for.
|
|
///
|
|
/// Return the validators chosen to be nominated.
|
|
pub fn create_validators_with_nominators_for_era<T: Config>(
|
|
validators: u32,
|
|
nominators: u32,
|
|
edge_per_nominator: usize,
|
|
randomize_stake: bool,
|
|
to_nominate: Option<u32>,
|
|
) -> Result<Vec<<T::Lookup as StaticLookup>::Source>, &'static str> {
|
|
clear_validators_and_nominators::<T>();
|
|
|
|
let mut validators_stash: Vec<<T::Lookup as StaticLookup>::Source>
|
|
= Vec::with_capacity(validators as usize);
|
|
let mut rng = ChaChaRng::from_seed(SEED.using_encoded(blake2_256));
|
|
|
|
// 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::<T>(i, balance_factor, RewardDestination::Staked)?;
|
|
let validator_prefs = ValidatorPrefs {
|
|
commission: Perbill::from_percent(50),
|
|
.. Default::default()
|
|
};
|
|
Staking::<T>::validate(RawOrigin::Signed(v_controller.clone()).into(), validator_prefs)?;
|
|
let stash_lookup: <T::Lookup as StaticLookup>::Source = T::Lookup::unlookup(v_stash.clone());
|
|
validators_stash.push(stash_lookup.clone());
|
|
}
|
|
|
|
let to_nominate = to_nominate.unwrap_or(validators_stash.len() as u32) as usize;
|
|
let validator_chosen = validators_stash[0..to_nominate].to_vec();
|
|
|
|
// Create nominators
|
|
for j in 0 .. nominators {
|
|
let balance_factor = if randomize_stake { rng.next_u32() % 255 + 10 } else { 100u32 };
|
|
let (_n_stash, n_controller) = create_stash_controller::<T>(
|
|
u32::max_value() - j,
|
|
balance_factor,
|
|
RewardDestination::Staked,
|
|
)?;
|
|
|
|
// Have them randomly validate
|
|
let mut available_validators = validator_chosen.clone();
|
|
let mut selected_validators: Vec<<T::Lookup as StaticLookup>::Source> =
|
|
Vec::with_capacity(edge_per_nominator);
|
|
|
|
for _ in 0 .. validators.min(edge_per_nominator as u32) {
|
|
let selected = rng.next_u32() as usize % available_validators.len();
|
|
let validator = available_validators.remove(selected);
|
|
selected_validators.push(validator);
|
|
}
|
|
Staking::<T>::nominate(RawOrigin::Signed(n_controller.clone()).into(), selected_validators)?;
|
|
}
|
|
|
|
ValidatorCount::put(validators);
|
|
|
|
Ok(validator_chosen)
|
|
}
|
|
|
|
/// get the current era.
|
|
pub fn current_era<T: Config>() -> EraIndex {
|
|
<Module<T>>::current_era().unwrap_or(0)
|
|
}
|