mirror of
https://github.com/pezkuwichain/pezkuwi-subxt.git
synced 2026-05-31 05:11:02 +00:00
Run benchmarks for mock runtimes (#1996)
* run benchmarks for pallet-bridge-grandpa mock runtime * run benchmarks for pallet-bridge-relayers mock runtime * run benchmarks for pallet-bridge-parachains mock runtime * run benchmarks for pallet-bridge-messages mock runtime * test benchmarks on mockj runtimes from CI * clippy and spelling
This commit is contained in:
committed by
Bastian Köcher
parent
88fb2befa0
commit
068f6f648b
@@ -136,4 +136,6 @@ benchmarks_instance_pallet! {
|
||||
// check that the header#0 has been pruned
|
||||
assert!(!<ImportedHeaders<T, I>>::contains_key(genesis_header.hash()));
|
||||
}
|
||||
|
||||
impl_benchmark_test_suite!(Pallet, crate::mock::new_test_ext(), crate::mock::TestRuntime)
|
||||
}
|
||||
|
||||
@@ -131,10 +131,17 @@ impl ChainWithGrandpa for TestBridgedChain {
|
||||
const AVERAGE_HEADER_SIZE_IN_JUSTIFICATION: u32 = 64;
|
||||
}
|
||||
|
||||
pub fn run_test<T>(test: impl FnOnce() -> T) -> T {
|
||||
sp_io::TestExternalities::new(Default::default()).execute_with(test)
|
||||
/// Return test externalities to use in tests.
|
||||
pub fn new_test_ext() -> sp_io::TestExternalities {
|
||||
sp_io::TestExternalities::new(Default::default())
|
||||
}
|
||||
|
||||
/// Return test within default test externalities context.
|
||||
pub fn run_test<T>(test: impl FnOnce() -> T) -> T {
|
||||
new_test_ext().execute_with(test)
|
||||
}
|
||||
|
||||
/// Return test header with given number.
|
||||
pub fn test_header(num: TestNumber) -> TestHeader {
|
||||
// We wrap the call to avoid explicit type annotations in our tests
|
||||
bp_test_utils::test_header(num)
|
||||
|
||||
@@ -27,9 +27,9 @@ sp-runtime = { git = "https://github.com/paritytech/substrate", branch = "master
|
||||
sp-std = { git = "https://github.com/paritytech/substrate", branch = "master", default-features = false }
|
||||
|
||||
[dev-dependencies]
|
||||
sp-io = { git = "https://github.com/paritytech/substrate", branch = "master" }
|
||||
pallet-balances = { git = "https://github.com/paritytech/substrate", branch = "master" }
|
||||
bp-test-utils = { path = "../../primitives/test-utils" }
|
||||
pallet-balances = { git = "https://github.com/paritytech/substrate", branch = "master" }
|
||||
sp-io = { git = "https://github.com/paritytech/substrate", branch = "master" }
|
||||
|
||||
[features]
|
||||
default = ["std"]
|
||||
|
||||
@@ -37,7 +37,7 @@ use sp_std::{ops::RangeInclusive, prelude::*};
|
||||
const SEED: u32 = 0;
|
||||
|
||||
/// Pallet we're benchmarking here.
|
||||
pub struct Pallet<T: Config<I>, I: 'static>(crate::Pallet<T, I>);
|
||||
pub struct Pallet<T: Config<I>, I: 'static = ()>(crate::Pallet<T, I>);
|
||||
|
||||
/// Benchmark-specific message proof parameters.
|
||||
#[derive(Debug)]
|
||||
@@ -437,6 +437,8 @@ benchmarks_instance_pallet! {
|
||||
);
|
||||
assert!(T::is_message_successfully_dispatched(21));
|
||||
}
|
||||
|
||||
impl_benchmark_test_suite!(Pallet, crate::mock::new_test_ext(), crate::mock::TestRuntime)
|
||||
}
|
||||
|
||||
fn send_regular_message<T: Config<I>, I: 'static>() {
|
||||
|
||||
@@ -170,6 +170,44 @@ impl Config for TestRuntime {
|
||||
type BridgedChainId = TestBridgedChainId;
|
||||
}
|
||||
|
||||
#[cfg(feature = "runtime-benchmarks")]
|
||||
impl crate::benchmarking::Config<()> for TestRuntime {
|
||||
fn bench_lane_id() -> LaneId {
|
||||
TEST_LANE_ID
|
||||
}
|
||||
|
||||
fn prepare_message_proof(
|
||||
params: crate::benchmarking::MessageProofParams,
|
||||
) -> (TestMessagesProof, Weight) {
|
||||
// in mock run we only care about benchmarks correctness, not the benchmark results
|
||||
// => ignore size related arguments
|
||||
let (messages, total_dispatch_weight) =
|
||||
params.message_nonces.into_iter().map(|n| message(n, REGULAR_PAYLOAD)).fold(
|
||||
(Vec::new(), Weight::zero()),
|
||||
|(mut messages, total_dispatch_weight), message| {
|
||||
let weight = REGULAR_PAYLOAD.declared_weight;
|
||||
messages.push(message);
|
||||
(messages, total_dispatch_weight.saturating_add(weight))
|
||||
},
|
||||
);
|
||||
let mut proof: TestMessagesProof = Ok(messages).into();
|
||||
proof.result.as_mut().unwrap().get_mut(0).unwrap().1.lane_state = params.outbound_lane_data;
|
||||
(proof, total_dispatch_weight)
|
||||
}
|
||||
|
||||
fn prepare_message_delivery_proof(
|
||||
params: crate::benchmarking::MessageDeliveryProofParams<AccountId>,
|
||||
) -> TestMessagesDeliveryProof {
|
||||
// in mock run we only care about benchmarks correctness, not the benchmark results
|
||||
// => ignore size related arguments
|
||||
TestMessagesDeliveryProof(Ok((params.lane, params.inbound_lane_data)))
|
||||
}
|
||||
|
||||
fn is_relayer_rewarded(_relayer: &AccountId) -> bool {
|
||||
true
|
||||
}
|
||||
}
|
||||
|
||||
impl Size for TestPayload {
|
||||
fn size(&self) -> u32 {
|
||||
16 + self.extra.len() as u32
|
||||
@@ -439,12 +477,16 @@ pub fn unrewarded_relayer(
|
||||
UnrewardedRelayer { relayer, messages: DeliveredMessages { begin, end } }
|
||||
}
|
||||
|
||||
/// Run pallet test.
|
||||
pub fn run_test<T>(test: impl FnOnce() -> T) -> T {
|
||||
/// Return test externalities to use in tests.
|
||||
pub fn new_test_ext() -> sp_io::TestExternalities {
|
||||
let mut t = frame_system::GenesisConfig::default().build_storage::<TestRuntime>().unwrap();
|
||||
pallet_balances::GenesisConfig::<TestRuntime> { balances: vec![(ENDOWED_ACCOUNT, 1_000_000)] }
|
||||
.assimilate_storage(&mut t)
|
||||
.unwrap();
|
||||
let mut ext = sp_io::TestExternalities::new(t);
|
||||
ext.execute_with(test)
|
||||
sp_io::TestExternalities::new(t)
|
||||
}
|
||||
|
||||
/// Run pallet test.
|
||||
pub fn run_test<T>(test: impl FnOnce() -> T) -> T {
|
||||
new_test_ext().execute_with(test)
|
||||
}
|
||||
|
||||
@@ -28,7 +28,7 @@ use frame_system::RawOrigin;
|
||||
use sp_std::prelude::*;
|
||||
|
||||
/// Pallet we're benchmarking here.
|
||||
pub struct Pallet<T: Config<I>, I: 'static>(crate::Pallet<T, I>);
|
||||
pub struct Pallet<T: Config<I>, I: 'static = ()>(crate::Pallet<T, I>);
|
||||
|
||||
/// Trait that must be implemented by runtime to benchmark the parachains finality pallet.
|
||||
pub trait Config<I: 'static>: crate::Config<I> {
|
||||
@@ -111,4 +111,6 @@ benchmarks_instance_pallet! {
|
||||
assert!(crate::Pallet::<T, I>::best_parachain_head(parachain).is_some());
|
||||
}
|
||||
}
|
||||
|
||||
impl_benchmark_test_suite!(Pallet, crate::mock::new_test_ext(), crate::mock::TestRuntime)
|
||||
}
|
||||
|
||||
@@ -689,7 +689,7 @@ pub fn initialize_for_benchmarks<T: Config<I>, I: 'static, PC: Parachain<Hash =
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
pub(crate) mod tests {
|
||||
use super::*;
|
||||
use crate::mock::{
|
||||
run_test, test_relay_header, BigParachainHeader, RegularParachainHasher,
|
||||
@@ -724,7 +724,7 @@ mod tests {
|
||||
type WeightInfo = <TestRuntime as Config>::WeightInfo;
|
||||
type DbWeight = <TestRuntime as frame_system::Config>::DbWeight;
|
||||
|
||||
fn initialize(state_root: RelayBlockHash) {
|
||||
pub(crate) fn initialize(state_root: RelayBlockHash) -> RelayBlockHash {
|
||||
pallet_bridge_grandpa::Pallet::<TestRuntime, BridgesGrandpaPalletInstance>::initialize(
|
||||
RuntimeOrigin::root(),
|
||||
bp_header_chain::InitializationData {
|
||||
@@ -738,6 +738,8 @@ mod tests {
|
||||
|
||||
System::<TestRuntime>::set_block_number(1);
|
||||
System::<TestRuntime>::reset_events();
|
||||
|
||||
test_relay_header(0, state_root).hash()
|
||||
}
|
||||
|
||||
fn proceed(num: RelayBlockNumber, state_root: RelayBlockHash) -> ParaHash {
|
||||
@@ -759,7 +761,7 @@ mod tests {
|
||||
hash
|
||||
}
|
||||
|
||||
fn prepare_parachain_heads_proof(
|
||||
pub(crate) fn prepare_parachain_heads_proof(
|
||||
heads: Vec<(u32, ParaHead)>,
|
||||
) -> (RelayBlockHash, ParaHeadsProof, Vec<(ParaId, ParaHash)>) {
|
||||
let mut parachains = Vec::with_capacity(heads.len());
|
||||
@@ -795,7 +797,7 @@ mod tests {
|
||||
}
|
||||
}
|
||||
|
||||
fn head_data(parachain: u32, head_number: u32) -> ParaHead {
|
||||
pub(crate) fn head_data(parachain: u32, head_number: u32) -> ParaHead {
|
||||
ParaHead(
|
||||
RegularParachainHeader::new(
|
||||
head_number as _,
|
||||
|
||||
@@ -228,6 +228,36 @@ impl pallet_bridge_parachains::Config for TestRuntime {
|
||||
type MaxParaHeadDataSize = ConstU32<MAXIMAL_PARACHAIN_HEAD_DATA_SIZE>;
|
||||
}
|
||||
|
||||
#[cfg(feature = "runtime-benchmarks")]
|
||||
impl pallet_bridge_parachains::benchmarking::Config<()> for TestRuntime {
|
||||
fn parachains() -> Vec<ParaId> {
|
||||
vec![
|
||||
ParaId(Parachain1::PARACHAIN_ID),
|
||||
ParaId(Parachain2::PARACHAIN_ID),
|
||||
ParaId(Parachain3::PARACHAIN_ID),
|
||||
]
|
||||
}
|
||||
|
||||
fn prepare_parachain_heads_proof(
|
||||
parachains: &[ParaId],
|
||||
_parachain_head_size: u32,
|
||||
_proof_size: bp_runtime::StorageProofSize,
|
||||
) -> (
|
||||
crate::RelayBlockNumber,
|
||||
crate::RelayBlockHash,
|
||||
bp_polkadot_core::parachains::ParaHeadsProof,
|
||||
Vec<(ParaId, bp_polkadot_core::parachains::ParaHash)>,
|
||||
) {
|
||||
// in mock run we only care about benchmarks correctness, not the benchmark results
|
||||
// => ignore size related arguments
|
||||
let (state_root, proof, parachains) = crate::tests::prepare_parachain_heads_proof(
|
||||
parachains.iter().map(|p| (p.0, crate::tests::head_data(p.0, 1))).collect(),
|
||||
);
|
||||
let relay_genesis_hash = crate::tests::initialize(state_root);
|
||||
(0, relay_genesis_hash, proof, parachains)
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Debug)]
|
||||
pub struct TestBridgedChain;
|
||||
|
||||
@@ -290,14 +320,21 @@ impl ChainWithGrandpa for OtherBridgedChain {
|
||||
const AVERAGE_HEADER_SIZE_IN_JUSTIFICATION: u32 = 64;
|
||||
}
|
||||
|
||||
/// Return test externalities to use in tests.
|
||||
pub fn new_test_ext() -> sp_io::TestExternalities {
|
||||
sp_io::TestExternalities::new(Default::default())
|
||||
}
|
||||
|
||||
/// Run pallet test.
|
||||
pub fn run_test<T>(test: impl FnOnce() -> T) -> T {
|
||||
sp_io::TestExternalities::new(Default::default()).execute_with(|| {
|
||||
new_test_ext().execute_with(|| {
|
||||
System::set_block_number(1);
|
||||
System::reset_events();
|
||||
test()
|
||||
})
|
||||
}
|
||||
|
||||
/// Return test relay chain header with given number.
|
||||
pub fn test_relay_header(
|
||||
num: crate::RelayBlockNumber,
|
||||
state_root: crate::RelayBlockHash,
|
||||
|
||||
@@ -54,4 +54,6 @@ benchmarks! {
|
||||
// payment logic, so we assume that if call has succeeded, the procedure has
|
||||
// also completed successfully
|
||||
}
|
||||
|
||||
impl_benchmark_test_suite!(Pallet, crate::mock::new_test_ext(), crate::mock::TestRuntime)
|
||||
}
|
||||
|
||||
@@ -99,6 +99,18 @@ impl pallet_bridge_relayers::Config for TestRuntime {
|
||||
type WeightInfo = ();
|
||||
}
|
||||
|
||||
#[cfg(feature = "runtime-benchmarks")]
|
||||
impl pallet_bridge_relayers::benchmarking::Config for TestRuntime {
|
||||
fn prepare_environment(account_params: RewardsAccountParams, reward: Balance) {
|
||||
use frame_support::traits::fungible::Mutate;
|
||||
let rewards_account =
|
||||
bp_relayers::PayRewardFromAccount::<Balances, AccountId>::rewards_account(
|
||||
account_params,
|
||||
);
|
||||
Balances::mint_into(&rewards_account, reward).unwrap();
|
||||
}
|
||||
}
|
||||
|
||||
/// Message lane that we're using in tests.
|
||||
pub const TEST_REWARDS_ACCOUNT_PARAMS: RewardsAccountParams =
|
||||
RewardsAccountParams::new(LaneId([0, 0, 0, 0]), *b"test", RewardsAccountOwner::ThisChain);
|
||||
@@ -127,9 +139,13 @@ impl PaymentProcedure<AccountId, Balance> for TestPaymentProcedure {
|
||||
}
|
||||
}
|
||||
|
||||
/// Return test externalities to use in tests.
|
||||
pub fn new_test_ext() -> sp_io::TestExternalities {
|
||||
let t = frame_system::GenesisConfig::default().build_storage::<TestRuntime>().unwrap();
|
||||
sp_io::TestExternalities::new(t)
|
||||
}
|
||||
|
||||
/// Run pallet test.
|
||||
pub fn run_test<T>(test: impl FnOnce() -> T) -> T {
|
||||
let t = frame_system::GenesisConfig::default().build_storage::<TestRuntime>().unwrap();
|
||||
let mut ext = sp_io::TestExternalities::new(t);
|
||||
ext.execute_with(test)
|
||||
new_test_ext().execute_with(test)
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user