Skip to content

Commit 4be1e84

Browse files
authored
Run benchmarks for mock runtimes (paritytech#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
1 parent 2bf615f commit 4be1e84

File tree

10 files changed

+129
-17
lines changed

10 files changed

+129
-17
lines changed

modules/grandpa/src/benchmarking.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -136,4 +136,6 @@ benchmarks_instance_pallet! {
136136
// check that the header#0 has been pruned
137137
assert!(!<ImportedHeaders<T, I>>::contains_key(genesis_header.hash()));
138138
}
139+
140+
impl_benchmark_test_suite!(Pallet, crate::mock::new_test_ext(), crate::mock::TestRuntime)
139141
}

modules/grandpa/src/mock.rs

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -131,10 +131,17 @@ impl ChainWithGrandpa for TestBridgedChain {
131131
const AVERAGE_HEADER_SIZE_IN_JUSTIFICATION: u32 = 64;
132132
}
133133

134+
/// Return test externalities to use in tests.
135+
pub fn new_test_ext() -> sp_io::TestExternalities {
136+
sp_io::TestExternalities::new(Default::default())
137+
}
138+
139+
/// Return test within default test externalities context.
134140
pub fn run_test<T>(test: impl FnOnce() -> T) -> T {
135-
sp_io::TestExternalities::new(Default::default()).execute_with(test)
141+
new_test_ext().execute_with(test)
136142
}
137143

144+
/// Return test header with given number.
138145
pub fn test_header(num: TestNumber) -> TestHeader {
139146
// We wrap the call to avoid explicit type annotations in our tests
140147
bp_test_utils::test_header(num)

modules/messages/Cargo.toml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,9 +27,9 @@ sp-runtime = { git = "https://github.com/paritytech/substrate", branch = "master
2727
sp-std = { git = "https://github.com/paritytech/substrate", branch = "master", default-features = false }
2828

2929
[dev-dependencies]
30-
sp-io = { git = "https://github.com/paritytech/substrate", branch = "master" }
31-
pallet-balances = { git = "https://github.com/paritytech/substrate", branch = "master" }
3230
bp-test-utils = { path = "../../primitives/test-utils" }
31+
pallet-balances = { git = "https://github.com/paritytech/substrate", branch = "master" }
32+
sp-io = { git = "https://github.com/paritytech/substrate", branch = "master" }
3333

3434
[features]
3535
default = ["std"]

modules/messages/src/benchmarking.rs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ use sp_std::{ops::RangeInclusive, prelude::*};
3737
const SEED: u32 = 0;
3838

3939
/// Pallet we're benchmarking here.
40-
pub struct Pallet<T: Config<I>, I: 'static>(crate::Pallet<T, I>);
40+
pub struct Pallet<T: Config<I>, I: 'static = ()>(crate::Pallet<T, I>);
4141

4242
/// Benchmark-specific message proof parameters.
4343
#[derive(Debug)]
@@ -437,6 +437,8 @@ benchmarks_instance_pallet! {
437437
);
438438
assert!(T::is_message_successfully_dispatched(21));
439439
}
440+
441+
impl_benchmark_test_suite!(Pallet, crate::mock::new_test_ext(), crate::mock::TestRuntime)
440442
}
441443

442444
fn send_regular_message<T: Config<I>, I: 'static>() {

modules/messages/src/mock.rs

Lines changed: 46 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -170,6 +170,44 @@ impl Config for TestRuntime {
170170
type BridgedChainId = TestBridgedChainId;
171171
}
172172

173+
#[cfg(feature = "runtime-benchmarks")]
174+
impl crate::benchmarking::Config<()> for TestRuntime {
175+
fn bench_lane_id() -> LaneId {
176+
TEST_LANE_ID
177+
}
178+
179+
fn prepare_message_proof(
180+
params: crate::benchmarking::MessageProofParams,
181+
) -> (TestMessagesProof, Weight) {
182+
// in mock run we only care about benchmarks correctness, not the benchmark results
183+
// => ignore size related arguments
184+
let (messages, total_dispatch_weight) =
185+
params.message_nonces.into_iter().map(|n| message(n, REGULAR_PAYLOAD)).fold(
186+
(Vec::new(), Weight::zero()),
187+
|(mut messages, total_dispatch_weight), message| {
188+
let weight = REGULAR_PAYLOAD.declared_weight;
189+
messages.push(message);
190+
(messages, total_dispatch_weight.saturating_add(weight))
191+
},
192+
);
193+
let mut proof: TestMessagesProof = Ok(messages).into();
194+
proof.result.as_mut().unwrap().get_mut(0).unwrap().1.lane_state = params.outbound_lane_data;
195+
(proof, total_dispatch_weight)
196+
}
197+
198+
fn prepare_message_delivery_proof(
199+
params: crate::benchmarking::MessageDeliveryProofParams<AccountId>,
200+
) -> TestMessagesDeliveryProof {
201+
// in mock run we only care about benchmarks correctness, not the benchmark results
202+
// => ignore size related arguments
203+
TestMessagesDeliveryProof(Ok((params.lane, params.inbound_lane_data)))
204+
}
205+
206+
fn is_relayer_rewarded(_relayer: &AccountId) -> bool {
207+
true
208+
}
209+
}
210+
173211
impl Size for TestPayload {
174212
fn size(&self) -> u32 {
175213
16 + self.extra.len() as u32
@@ -439,12 +477,16 @@ pub fn unrewarded_relayer(
439477
UnrewardedRelayer { relayer, messages: DeliveredMessages { begin, end } }
440478
}
441479

442-
/// Run pallet test.
443-
pub fn run_test<T>(test: impl FnOnce() -> T) -> T {
480+
/// Return test externalities to use in tests.
481+
pub fn new_test_ext() -> sp_io::TestExternalities {
444482
let mut t = frame_system::GenesisConfig::default().build_storage::<TestRuntime>().unwrap();
445483
pallet_balances::GenesisConfig::<TestRuntime> { balances: vec![(ENDOWED_ACCOUNT, 1_000_000)] }
446484
.assimilate_storage(&mut t)
447485
.unwrap();
448-
let mut ext = sp_io::TestExternalities::new(t);
449-
ext.execute_with(test)
486+
sp_io::TestExternalities::new(t)
487+
}
488+
489+
/// Run pallet test.
490+
pub fn run_test<T>(test: impl FnOnce() -> T) -> T {
491+
new_test_ext().execute_with(test)
450492
}

modules/parachains/src/benchmarking.rs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ use frame_system::RawOrigin;
2828
use sp_std::prelude::*;
2929

3030
/// Pallet we're benchmarking here.
31-
pub struct Pallet<T: Config<I>, I: 'static>(crate::Pallet<T, I>);
31+
pub struct Pallet<T: Config<I>, I: 'static = ()>(crate::Pallet<T, I>);
3232

3333
/// Trait that must be implemented by runtime to benchmark the parachains finality pallet.
3434
pub trait Config<I: 'static>: crate::Config<I> {
@@ -111,4 +111,6 @@ benchmarks_instance_pallet! {
111111
assert!(crate::Pallet::<T, I>::best_parachain_head(parachain).is_some());
112112
}
113113
}
114+
115+
impl_benchmark_test_suite!(Pallet, crate::mock::new_test_ext(), crate::mock::TestRuntime)
114116
}

modules/parachains/src/lib.rs

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -689,7 +689,7 @@ pub fn initialize_for_benchmarks<T: Config<I>, I: 'static, PC: Parachain<Hash =
689689
}
690690

691691
#[cfg(test)]
692-
mod tests {
692+
pub(crate) mod tests {
693693
use super::*;
694694
use crate::mock::{
695695
run_test, test_relay_header, BigParachainHeader, RegularParachainHasher,
@@ -724,7 +724,7 @@ mod tests {
724724
type WeightInfo = <TestRuntime as Config>::WeightInfo;
725725
type DbWeight = <TestRuntime as frame_system::Config>::DbWeight;
726726

727-
fn initialize(state_root: RelayBlockHash) {
727+
pub(crate) fn initialize(state_root: RelayBlockHash) -> RelayBlockHash {
728728
pallet_bridge_grandpa::Pallet::<TestRuntime, BridgesGrandpaPalletInstance>::initialize(
729729
RuntimeOrigin::root(),
730730
bp_header_chain::InitializationData {
@@ -738,6 +738,8 @@ mod tests {
738738

739739
System::<TestRuntime>::set_block_number(1);
740740
System::<TestRuntime>::reset_events();
741+
742+
test_relay_header(0, state_root).hash()
741743
}
742744

743745
fn proceed(num: RelayBlockNumber, state_root: RelayBlockHash) -> ParaHash {
@@ -759,7 +761,7 @@ mod tests {
759761
hash
760762
}
761763

762-
fn prepare_parachain_heads_proof(
764+
pub(crate) fn prepare_parachain_heads_proof(
763765
heads: Vec<(u32, ParaHead)>,
764766
) -> (RelayBlockHash, ParaHeadsProof, Vec<(ParaId, ParaHash)>) {
765767
let mut parachains = Vec::with_capacity(heads.len());
@@ -795,7 +797,7 @@ mod tests {
795797
}
796798
}
797799

798-
fn head_data(parachain: u32, head_number: u32) -> ParaHead {
800+
pub(crate) fn head_data(parachain: u32, head_number: u32) -> ParaHead {
799801
ParaHead(
800802
RegularParachainHeader::new(
801803
head_number as _,

modules/parachains/src/mock.rs

Lines changed: 38 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -228,6 +228,36 @@ impl pallet_bridge_parachains::Config for TestRuntime {
228228
type MaxParaHeadDataSize = ConstU32<MAXIMAL_PARACHAIN_HEAD_DATA_SIZE>;
229229
}
230230

231+
#[cfg(feature = "runtime-benchmarks")]
232+
impl pallet_bridge_parachains::benchmarking::Config<()> for TestRuntime {
233+
fn parachains() -> Vec<ParaId> {
234+
vec![
235+
ParaId(Parachain1::PARACHAIN_ID),
236+
ParaId(Parachain2::PARACHAIN_ID),
237+
ParaId(Parachain3::PARACHAIN_ID),
238+
]
239+
}
240+
241+
fn prepare_parachain_heads_proof(
242+
parachains: &[ParaId],
243+
_parachain_head_size: u32,
244+
_proof_size: bp_runtime::StorageProofSize,
245+
) -> (
246+
crate::RelayBlockNumber,
247+
crate::RelayBlockHash,
248+
bp_polkadot_core::parachains::ParaHeadsProof,
249+
Vec<(ParaId, bp_polkadot_core::parachains::ParaHash)>,
250+
) {
251+
// in mock run we only care about benchmarks correctness, not the benchmark results
252+
// => ignore size related arguments
253+
let (state_root, proof, parachains) = crate::tests::prepare_parachain_heads_proof(
254+
parachains.iter().map(|p| (p.0, crate::tests::head_data(p.0, 1))).collect(),
255+
);
256+
let relay_genesis_hash = crate::tests::initialize(state_root);
257+
(0, relay_genesis_hash, proof, parachains)
258+
}
259+
}
260+
231261
#[derive(Debug)]
232262
pub struct TestBridgedChain;
233263

@@ -290,14 +320,21 @@ impl ChainWithGrandpa for OtherBridgedChain {
290320
const AVERAGE_HEADER_SIZE_IN_JUSTIFICATION: u32 = 64;
291321
}
292322

323+
/// Return test externalities to use in tests.
324+
pub fn new_test_ext() -> sp_io::TestExternalities {
325+
sp_io::TestExternalities::new(Default::default())
326+
}
327+
328+
/// Run pallet test.
293329
pub fn run_test<T>(test: impl FnOnce() -> T) -> T {
294-
sp_io::TestExternalities::new(Default::default()).execute_with(|| {
330+
new_test_ext().execute_with(|| {
295331
System::set_block_number(1);
296332
System::reset_events();
297333
test()
298334
})
299335
}
300336

337+
/// Return test relay chain header with given number.
301338
pub fn test_relay_header(
302339
num: crate::RelayBlockNumber,
303340
state_root: crate::RelayBlockHash,

modules/relayers/src/benchmarking.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,4 +54,6 @@ benchmarks! {
5454
// payment logic, so we assume that if call has succeeded, the procedure has
5555
// also completed successfully
5656
}
57+
58+
impl_benchmark_test_suite!(Pallet, crate::mock::new_test_ext(), crate::mock::TestRuntime)
5759
}

modules/relayers/src/mock.rs

Lines changed: 19 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -99,6 +99,18 @@ impl pallet_bridge_relayers::Config for TestRuntime {
9999
type WeightInfo = ();
100100
}
101101

102+
#[cfg(feature = "runtime-benchmarks")]
103+
impl pallet_bridge_relayers::benchmarking::Config for TestRuntime {
104+
fn prepare_environment(account_params: RewardsAccountParams, reward: Balance) {
105+
use frame_support::traits::fungible::Mutate;
106+
let rewards_account =
107+
bp_relayers::PayRewardFromAccount::<Balances, AccountId>::rewards_account(
108+
account_params,
109+
);
110+
Balances::mint_into(&rewards_account, reward).unwrap();
111+
}
112+
}
113+
102114
/// Message lane that we're using in tests.
103115
pub const TEST_REWARDS_ACCOUNT_PARAMS: RewardsAccountParams =
104116
RewardsAccountParams::new(LaneId([0, 0, 0, 0]), *b"test", RewardsAccountOwner::ThisChain);
@@ -127,9 +139,13 @@ impl PaymentProcedure<AccountId, Balance> for TestPaymentProcedure {
127139
}
128140
}
129141

142+
/// Return test externalities to use in tests.
143+
pub fn new_test_ext() -> sp_io::TestExternalities {
144+
let t = frame_system::GenesisConfig::default().build_storage::<TestRuntime>().unwrap();
145+
sp_io::TestExternalities::new(t)
146+
}
147+
130148
/// Run pallet test.
131149
pub fn run_test<T>(test: impl FnOnce() -> T) -> T {
132-
let t = frame_system::GenesisConfig::default().build_storage::<TestRuntime>().unwrap();
133-
let mut ext = sp_io::TestExternalities::new(t);
134-
ext.execute_with(test)
150+
new_test_ext().execute_with(test)
135151
}

0 commit comments

Comments
 (0)