Skip to content

Commit

Permalink
Run benchmarks for mock runtimes (paritytech#1996)
Browse files Browse the repository at this point in the history
* 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
  • Loading branch information
svyatonik authored and serban300 committed Apr 8, 2024
1 parent ffcd836 commit d463899
Show file tree
Hide file tree
Showing 10 changed files with 129 additions and 17 deletions.
2 changes: 2 additions & 0 deletions bridges/modules/grandpa/src/benchmarking.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
9 changes: 8 additions & 1 deletion bridges/modules/grandpa/src/mock.rs
Original file line number Diff line number Diff line change
Expand Up @@ -131,10 +131,17 @@ impl ChainWithGrandpa for TestBridgedChain {
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())
}

/// Return test within default test externalities context.
pub fn run_test<T>(test: impl FnOnce() -> T) -> T {
sp_io::TestExternalities::new(Default::default()).execute_with(test)
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)
Expand Down
4 changes: 2 additions & 2 deletions bridges/modules/messages/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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"]
Expand Down
4 changes: 3 additions & 1 deletion bridges/modules/messages/src/benchmarking.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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)]
Expand Down Expand Up @@ -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>() {
Expand Down
50 changes: 46 additions & 4 deletions bridges/modules/messages/src/mock.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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)
}
4 changes: 3 additions & 1 deletion bridges/modules/parachains/src/benchmarking.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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> {
Expand Down Expand Up @@ -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)
}
10 changes: 6 additions & 4 deletions bridges/modules/parachains/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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 {
Expand All @@ -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 {
Expand All @@ -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());
Expand Down Expand Up @@ -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 _,
Expand Down
39 changes: 38 additions & 1 deletion bridges/modules/parachains/src/mock.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down Expand Up @@ -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,
Expand Down
2 changes: 2 additions & 0 deletions bridges/modules/relayers/src/benchmarking.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
22 changes: 19 additions & 3 deletions bridges/modules/relayers/src/mock.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down Expand Up @@ -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)
}

0 comments on commit d463899

Please sign in to comment.