mirror of
https://github.com/pezkuwichain/pezkuwi-subxt.git
synced 2026-06-14 04:01:10 +00:00
Generate Unit Tests for Benchmarks (#5527)
* Update to latest staking * generate tests for benchmarking * add tests, fix warnings * starting on democracy * impl_benchmark_tests * Way more readable * add test feature flag (does this work?) * Fix `successful_origin` impl * democracry benchmark tests * Fix example benchmarks, add tests * identity benchmark tests * Update im-online benchmark tests * try to add session benchmarking tests (problem with mock) * staking and timestamp * add test for treasury, issue with dynamic contains * utility * Vesting * test instead of check * hide until we figure out what is wrong * add docs * close code * Create custom mock for session-pallet-benchmarking * Use refcell pattern * make un-pub * test-linux-stable includes `runtime-benchmarks` feature * Revert "test-linux-stable includes `runtime-benchmarks` feature" This reverts commit a2dab38abd18ac3eb8a6220e4a00e687740bd38c. * run tests in `--release` * undo balance change * build wasm
This commit is contained in:
@@ -13,9 +13,19 @@ sp-std = { version = "2.0.0-alpha.5", default-features = false, path = "../../..
|
||||
sp-runtime = { version = "2.0.0-alpha.5", default-features = false, path = "../../../primitives/runtime" }
|
||||
frame-system = { version = "2.0.0-alpha.5", default-features = false, path = "../../system" }
|
||||
frame-benchmarking = { version = "2.0.0-alpha.5", default-features = false, path = "../../benchmarking" }
|
||||
frame-support = { version = "2.0.0-alpha.5", default-features = false, path = "../../support" }
|
||||
pallet-staking = { version = "2.0.0-alpha.5", default-features = false, features = ["runtime-benchmarks"], path = "../../staking" }
|
||||
pallet-session = { version = "2.0.0-alpha.5", default-features = false, path = "../../session" }
|
||||
|
||||
[dev-dependencies]
|
||||
serde = { version = "1.0.101" }
|
||||
codec = { package = "parity-scale-codec", version = "1.3.0", features = ["derive"] }
|
||||
sp-core = { version = "2.0.0-alpha.5", path = "../../../primitives/core" }
|
||||
pallet-staking-reward-curve = { version = "2.0.0-alpha.5", path = "../../staking/reward-curve" }
|
||||
sp-io ={ path = "../../../primitives/io", version = "2.0.0-alpha.5"}
|
||||
pallet-timestamp = { version = "2.0.0-alpha.5", path = "../../timestamp" }
|
||||
pallet-balances = { version = "2.0.0-alpha.5", path = "../../balances" }
|
||||
|
||||
[features]
|
||||
default = ["std"]
|
||||
std = [
|
||||
@@ -23,6 +33,7 @@ std = [
|
||||
"sp-runtime/std",
|
||||
"frame-system/std",
|
||||
"frame-benchmarking/std",
|
||||
"frame-support/std",
|
||||
"pallet-staking/std",
|
||||
"pallet-session/std",
|
||||
]
|
||||
|
||||
@@ -19,6 +19,8 @@
|
||||
|
||||
#![cfg_attr(not(feature = "std"), no_std)]
|
||||
|
||||
mod mock;
|
||||
|
||||
use sp_std::prelude::*;
|
||||
use sp_std::vec;
|
||||
|
||||
@@ -42,16 +44,33 @@ benchmarks! {
|
||||
|
||||
set_keys {
|
||||
let n in 1 .. MAX_NOMINATIONS as u32;
|
||||
let validator = create_validator_with_nominators::<T>(n, MAX_NOMINATIONS as u32)?;
|
||||
let v_stash = create_validator_with_nominators::<T>(n, MAX_NOMINATIONS as u32)?;
|
||||
let v_controller = pallet_staking::Module::<T>::bonded(&v_stash).ok_or("not stash")?;
|
||||
let keys = T::Keys::default();
|
||||
let proof: Vec<u8> = vec![0,1,2,3];
|
||||
}: _(RawOrigin::Signed(validator), keys, proof)
|
||||
}: _(RawOrigin::Signed(v_controller), keys, proof)
|
||||
|
||||
purge_keys {
|
||||
let n in 1 .. MAX_NOMINATIONS as u32;
|
||||
let validator = create_validator_with_nominators::<T>(n, MAX_NOMINATIONS as u32)?;
|
||||
let v_stash = create_validator_with_nominators::<T>(n, MAX_NOMINATIONS as u32)?;
|
||||
let v_controller = pallet_staking::Module::<T>::bonded(&v_stash).ok_or("not stash")?;
|
||||
let keys = T::Keys::default();
|
||||
let proof: Vec<u8> = vec![0,1,2,3];
|
||||
Session::<T>::set_keys(RawOrigin::Signed(validator.clone()).into(), keys, proof)?;
|
||||
}: _(RawOrigin::Signed(validator))
|
||||
Session::<T>::set_keys(RawOrigin::Signed(v_controller.clone()).into(), keys, proof)?;
|
||||
}: _(RawOrigin::Signed(v_controller))
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::*;
|
||||
use crate::mock::{new_test_ext, Test};
|
||||
use frame_support::assert_ok;
|
||||
|
||||
#[test]
|
||||
fn test_benchmarks() {
|
||||
new_test_ext().execute_with(|| {
|
||||
assert_ok!(test_benchmark_set_keys::<Test>());
|
||||
assert_ok!(test_benchmark_purge_keys::<Test>());
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
@@ -0,0 +1,184 @@
|
||||
// Copyright 2020 Parity Technologies (UK) Ltd.
|
||||
// This file is part of Substrate.
|
||||
|
||||
// Substrate 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.
|
||||
|
||||
// Substrate 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 Substrate. If not, see <http://www.gnu.org/licenses/>.
|
||||
|
||||
//! Mock file for staking fuzzing.
|
||||
|
||||
#![cfg(test)]
|
||||
|
||||
use sp_runtime::traits::{Convert, SaturatedConversion, IdentityLookup};
|
||||
use frame_support::{impl_outer_origin, impl_outer_dispatch, parameter_types};
|
||||
|
||||
type AccountId = u64;
|
||||
type AccountIndex = u32;
|
||||
type BlockNumber = u64;
|
||||
type Balance = u64;
|
||||
|
||||
type System = frame_system::Module<Test>;
|
||||
type Balances = pallet_balances::Module<Test>;
|
||||
type Staking = pallet_staking::Module<Test>;
|
||||
type Session = pallet_session::Module<Test>;
|
||||
|
||||
impl_outer_origin! {
|
||||
pub enum Origin for Test where system = frame_system {}
|
||||
}
|
||||
|
||||
impl_outer_dispatch! {
|
||||
pub enum Call for Test where origin: Origin {
|
||||
pallet_staking::Staking,
|
||||
}
|
||||
}
|
||||
|
||||
pub struct CurrencyToVoteHandler;
|
||||
impl Convert<u64, u64> for CurrencyToVoteHandler {
|
||||
fn convert(x: u64) -> u64 {
|
||||
x
|
||||
}
|
||||
}
|
||||
impl Convert<u128, u64> for CurrencyToVoteHandler {
|
||||
fn convert(x: u128) -> u64 {
|
||||
x.saturated_into()
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Clone, Eq, PartialEq, Debug)]
|
||||
pub struct Test;
|
||||
|
||||
impl frame_system::Trait for Test {
|
||||
type Origin = Origin;
|
||||
type Index = AccountIndex;
|
||||
type BlockNumber = BlockNumber;
|
||||
type Call = Call;
|
||||
type Hash = sp_core::H256;
|
||||
type Hashing = ::sp_runtime::traits::BlakeTwo256;
|
||||
type AccountId = AccountId;
|
||||
type Lookup = IdentityLookup<Self::AccountId>;
|
||||
type Header = sp_runtime::testing::Header;
|
||||
type Event = ();
|
||||
type BlockHashCount = ();
|
||||
type MaximumBlockWeight = ();
|
||||
type AvailableBlockRatio = ();
|
||||
type MaximumBlockLength = ();
|
||||
type Version = ();
|
||||
type ModuleToIndex = ();
|
||||
type AccountData = pallet_balances::AccountData<u64>;
|
||||
type OnNewAccount = ();
|
||||
type OnKilledAccount = (Balances,);
|
||||
}
|
||||
parameter_types! {
|
||||
pub const ExistentialDeposit: Balance = 10;
|
||||
}
|
||||
impl pallet_balances::Trait for Test {
|
||||
type Balance = Balance;
|
||||
type Event = ();
|
||||
type DustRemoval = ();
|
||||
type ExistentialDeposit = ExistentialDeposit;
|
||||
type AccountStore = System;
|
||||
}
|
||||
|
||||
parameter_types! {
|
||||
pub const MinimumPeriod: u64 = 5;
|
||||
}
|
||||
impl pallet_timestamp::Trait for Test {
|
||||
type Moment = u64;
|
||||
type OnTimestampSet = ();
|
||||
type MinimumPeriod = MinimumPeriod;
|
||||
}
|
||||
impl pallet_session::historical::Trait for Test {
|
||||
type FullIdentification = pallet_staking::Exposure<AccountId, Balance>;
|
||||
type FullIdentificationOf = pallet_staking::ExposureOf<Test>;
|
||||
}
|
||||
|
||||
sp_runtime::impl_opaque_keys! {
|
||||
pub struct SessionKeys {
|
||||
pub foo: sp_runtime::testing::UintAuthorityId,
|
||||
}
|
||||
}
|
||||
|
||||
pub struct TestSessionHandler;
|
||||
impl pallet_session::SessionHandler<AccountId> for TestSessionHandler {
|
||||
const KEY_TYPE_IDS: &'static [sp_runtime::KeyTypeId] = &[];
|
||||
|
||||
fn on_genesis_session<Ks: sp_runtime::traits::OpaqueKeys>(_validators: &[(AccountId, Ks)]) {}
|
||||
|
||||
fn on_new_session<Ks: sp_runtime::traits::OpaqueKeys>(
|
||||
_: bool,
|
||||
_: &[(AccountId, Ks)],
|
||||
_: &[(AccountId, Ks)],
|
||||
) {}
|
||||
|
||||
fn on_disabled(_: usize) {}
|
||||
}
|
||||
|
||||
impl pallet_session::Trait for Test {
|
||||
type SessionManager = pallet_session::historical::NoteHistoricalRoot<Test, Staking>;
|
||||
type Keys = SessionKeys;
|
||||
type ShouldEndSession = pallet_session::PeriodicSessions<(), ()>;
|
||||
type NextSessionRotation = pallet_session::PeriodicSessions<(), ()>;
|
||||
type SessionHandler = TestSessionHandler;
|
||||
type Event = ();
|
||||
type ValidatorId = AccountId;
|
||||
type ValidatorIdOf = pallet_staking::StashOf<Test>;
|
||||
type DisabledValidatorsThreshold = ();
|
||||
}
|
||||
pallet_staking_reward_curve::build! {
|
||||
const I_NPOS: sp_runtime::curve::PiecewiseLinear<'static> = curve!(
|
||||
min_inflation: 0_025_000,
|
||||
max_inflation: 0_100_000,
|
||||
ideal_stake: 0_500_000,
|
||||
falloff: 0_050_000,
|
||||
max_piece_count: 40,
|
||||
test_precision: 0_005_000,
|
||||
);
|
||||
}
|
||||
parameter_types! {
|
||||
pub const RewardCurve: &'static sp_runtime::curve::PiecewiseLinear<'static> = &I_NPOS;
|
||||
pub const MaxNominatorRewardedPerValidator: u32 = 64;
|
||||
}
|
||||
|
||||
pub type Extrinsic = sp_runtime::testing::TestXt<Call, ()>;
|
||||
type SubmitTransaction = frame_system::offchain::TransactionSubmitter<
|
||||
sp_runtime::testing::UintAuthorityId,
|
||||
Test,
|
||||
Extrinsic,
|
||||
>;
|
||||
|
||||
impl pallet_staking::Trait for Test {
|
||||
type Currency = Balances;
|
||||
type UnixTime = pallet_timestamp::Module<Self>;
|
||||
type CurrencyToVote = CurrencyToVoteHandler;
|
||||
type RewardRemainder = ();
|
||||
type Event = ();
|
||||
type Slash = ();
|
||||
type Reward = ();
|
||||
type SessionsPerEra = ();
|
||||
type SlashDeferDuration = ();
|
||||
type SlashCancelOrigin = frame_system::EnsureRoot<Self::AccountId>;
|
||||
type BondingDuration = ();
|
||||
type SessionInterface = Self;
|
||||
type RewardCurve = RewardCurve;
|
||||
type NextNewSession = Session;
|
||||
type ElectionLookahead = ();
|
||||
type Call = Call;
|
||||
type SubmitTransaction = SubmitTransaction;
|
||||
type MaxNominatorRewardedPerValidator = MaxNominatorRewardedPerValidator;
|
||||
}
|
||||
|
||||
impl crate::Trait for Test {}
|
||||
|
||||
pub fn new_test_ext() -> sp_io::TestExternalities {
|
||||
let t = frame_system::GenesisConfig::default().build_storage::<Test>().unwrap();
|
||||
sp_io::TestExternalities::new(t)
|
||||
}
|
||||
@@ -150,6 +150,16 @@ pub fn reset_before_session_end_called() {
|
||||
BEFORE_SESSION_END_CALLED.with(|b| *b.borrow_mut() = false);
|
||||
}
|
||||
|
||||
pub fn new_test_ext() -> sp_io::TestExternalities {
|
||||
let mut t = frame_system::GenesisConfig::default().build_storage::<Test>().unwrap();
|
||||
GenesisConfig::<Test> {
|
||||
keys: NEXT_VALIDATORS.with(|l|
|
||||
l.borrow().iter().cloned().map(|i| (i, i, UintAuthorityId(i).into())).collect()
|
||||
),
|
||||
}.assimilate_storage(&mut t).unwrap();
|
||||
sp_io::TestExternalities::new(t)
|
||||
}
|
||||
|
||||
#[derive(Clone, Eq, PartialEq)]
|
||||
pub struct Test;
|
||||
|
||||
|
||||
@@ -21,21 +21,11 @@ use frame_support::{traits::OnInitialize, assert_ok};
|
||||
use sp_core::crypto::key_types::DUMMY;
|
||||
use sp_runtime::testing::UintAuthorityId;
|
||||
use mock::{
|
||||
NEXT_VALIDATORS, SESSION_CHANGED, TEST_SESSION_CHANGED, authorities, force_new_session,
|
||||
set_next_validators, set_session_length, session_changed, Test, Origin, System, Session,
|
||||
reset_before_session_end_called, before_session_end_called,
|
||||
SESSION_CHANGED, TEST_SESSION_CHANGED, authorities, force_new_session,
|
||||
set_next_validators, set_session_length, session_changed, Origin, System, Session,
|
||||
reset_before_session_end_called, before_session_end_called, new_test_ext,
|
||||
};
|
||||
|
||||
fn new_test_ext() -> sp_io::TestExternalities {
|
||||
let mut t = frame_system::GenesisConfig::default().build_storage::<Test>().unwrap();
|
||||
GenesisConfig::<Test> {
|
||||
keys: NEXT_VALIDATORS.with(|l|
|
||||
l.borrow().iter().cloned().map(|i| (i, i, UintAuthorityId(i).into())).collect()
|
||||
),
|
||||
}.assimilate_storage(&mut t).unwrap();
|
||||
sp_io::TestExternalities::new(t)
|
||||
}
|
||||
|
||||
fn initialize_block(block: u64) {
|
||||
SESSION_CHANGED.with(|l| *l.borrow_mut() = false);
|
||||
System::set_block_number(block);
|
||||
|
||||
Reference in New Issue
Block a user