Skip to content

Commit 98ee19c

Browse files
bkonturbkchr
authored andcommitted
Fix benchmark with new XCM::V3 MAX_INSTRUCTIONS_TO_DECODE (#2514)
* Fix benchmark with new XCM::V3 MAX_INSTRUCTIONS_TO_DECODE * Small refactor * `ClearOrigin` replaced by ExpectPallet * Doc * Spellcheck * Fixes and pr reviews
1 parent 9216b78 commit 98ee19c

File tree

3 files changed

+71
-25
lines changed

3 files changed

+71
-25
lines changed

bridges/bin/millau/runtime/src/lib.rs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1036,6 +1036,7 @@ impl_runtime_apis! {
10361036
];
10371037

10381038
use bridge_runtime_common::messages_benchmarking::{
1039+
generate_xcm_builder_bridge_message_sample,
10391040
prepare_message_delivery_proof_from_grandpa_chain,
10401041
prepare_message_delivery_proof_from_parachain,
10411042
prepare_message_proof_from_grandpa_chain,
@@ -1070,7 +1071,7 @@ impl_runtime_apis! {
10701071
Runtime,
10711072
WithRialtoParachainsInstance,
10721073
WithRialtoParachainMessageBridge,
1073-
>(params, xcm::v3::Junctions::Here)
1074+
>(params, generate_xcm_builder_bridge_message_sample(xcm::v3::Junctions::Here))
10741075
}
10751076

10761077
fn prepare_message_delivery_proof(
@@ -1101,7 +1102,7 @@ impl_runtime_apis! {
11011102
Runtime,
11021103
RialtoGrandpaInstance,
11031104
WithRialtoMessageBridge,
1104-
>(params, xcm::v3::Junctions::Here)
1105+
>(params, generate_xcm_builder_bridge_message_sample(xcm::v3::Junctions::Here))
11051106
}
11061107

11071108
fn prepare_message_delivery_proof(

bridges/bin/runtime-common/src/messages_benchmarking.rs

Lines changed: 66 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ use crate::{
2929
},
3030
};
3131

32-
use bp_messages::storage_keys;
32+
use bp_messages::{storage_keys, MessagePayload};
3333
use bp_polkadot_core::parachains::ParaHash;
3434
use bp_runtime::{
3535
record_all_trie_keys, Chain, Parachain, RawStorageProof, StorageProofSize, UnderlyingChainOf,
@@ -45,8 +45,8 @@ use xcm::v3::prelude::*;
4545
/// Prepare inbound bridge message according to given message proof parameters.
4646
fn prepare_inbound_message(
4747
params: &MessageProofParams,
48-
destination: InteriorMultiLocation,
49-
) -> Vec<u8> {
48+
successful_dispatch_message_generator: impl Fn(usize) -> MessagePayload,
49+
) -> MessagePayload {
5050
// we only care about **this** message size when message proof needs to be `Minimal`
5151
let expected_size = match params.size {
5252
StorageProofSize::Minimal(size) => size as usize,
@@ -58,20 +58,15 @@ fn prepare_inbound_message(
5858
return vec![0u8; expected_size]
5959
}
6060

61-
// else let's prepare successful message. For XCM bridge hubs, it is the message that
62-
// will be pushed further to some XCM queue (XCMP/UMP)
63-
let location = xcm::VersionedInteriorMultiLocation::V3(destination);
64-
let location_encoded_size = location.encoded_size();
65-
66-
// we don't need to be super-precise with `expected_size` here
67-
let xcm_size = expected_size.saturating_sub(location_encoded_size);
68-
let xcm = xcm::VersionedXcm::<()>::V3(vec![Instruction::ClearOrigin; xcm_size].into());
69-
70-
// this is the `BridgeMessage` from polkadot xcm builder, but it has no constructor
71-
// or public fields, so just tuple
72-
// (double encoding, because `.encode()` is called on original Xcm BLOB when it is pushed
73-
// to the storage)
74-
(location, xcm).encode().encode()
61+
// else let's prepare successful message.
62+
let msg = successful_dispatch_message_generator(expected_size);
63+
assert!(
64+
msg.len() >= expected_size,
65+
"msg.len(): {} does not match expected_size: {}",
66+
expected_size,
67+
msg.len()
68+
);
69+
msg
7570
}
7671

7772
/// Prepare proof of messages for the `receive_messages_proof` call.
@@ -84,7 +79,7 @@ fn prepare_inbound_message(
8479
/// function.
8580
pub fn prepare_message_proof_from_grandpa_chain<R, FI, B>(
8681
params: MessageProofParams,
87-
message_destination: InteriorMultiLocation,
82+
message_generator: impl Fn(usize) -> MessagePayload,
8883
) -> (FromBridgedChainMessagesProof<HashOf<BridgedChain<B>>>, Weight)
8984
where
9085
R: pallet_bridge_grandpa::Config<FI, BridgedChain = UnderlyingChainOf<BridgedChain<B>>>,
@@ -97,7 +92,7 @@ where
9792
params.message_nonces.clone(),
9893
params.outbound_lane_data.clone(),
9994
params.size,
100-
prepare_inbound_message(&params, message_destination),
95+
prepare_inbound_message(&params, message_generator),
10196
encode_all_messages,
10297
encode_lane_data,
10398
);
@@ -127,7 +122,7 @@ where
127122
/// `prepare_message_proof_from_grandpa_chain` function.
128123
pub fn prepare_message_proof_from_parachain<R, PI, B>(
129124
params: MessageProofParams,
130-
message_destination: InteriorMultiLocation,
125+
message_generator: impl Fn(usize) -> MessagePayload,
131126
) -> (FromBridgedChainMessagesProof<HashOf<BridgedChain<B>>>, Weight)
132127
where
133128
R: pallet_bridge_parachains::Config<PI>,
@@ -141,7 +136,7 @@ where
141136
params.message_nonces.clone(),
142137
params.outbound_lane_data.clone(),
143138
params.size,
144-
prepare_inbound_message(&params, message_destination),
139+
prepare_inbound_message(&params, message_generator),
145140
encode_all_messages,
146141
encode_lane_data,
147142
);
@@ -291,3 +286,53 @@ where
291286
pallet_bridge_parachains::initialize_for_benchmarks::<R, PI, PC>(bridged_header);
292287
(bridged_block_number, bridged_header_hash)
293288
}
289+
290+
/// Returns callback which generates `BridgeMessage` from Polkadot XCM builder based on
291+
/// `expected_message_size` for benchmark.
292+
pub fn generate_xcm_builder_bridge_message_sample(
293+
destination: InteriorMultiLocation,
294+
) -> impl Fn(usize) -> MessagePayload {
295+
move |expected_message_size| -> MessagePayload {
296+
// For XCM bridge hubs, it is the message that
297+
// will be pushed further to some XCM queue (XCMP/UMP)
298+
let location = xcm::VersionedInteriorMultiLocation::V3(destination);
299+
let location_encoded_size = location.encoded_size();
300+
301+
// we don't need to be super-precise with `expected_size` here
302+
let xcm_size = expected_message_size.saturating_sub(location_encoded_size);
303+
let xcm_data_size = xcm_size.saturating_sub(
304+
// minus empty instruction size
305+
xcm::v3::Instruction::<()>::ExpectPallet {
306+
index: 0,
307+
name: vec![],
308+
module_name: vec![],
309+
crate_major: 0,
310+
min_crate_minor: 0,
311+
}
312+
.encoded_size(),
313+
);
314+
315+
log::trace!(
316+
target: "runtime::bridge-benchmarks",
317+
"generate_xcm_builder_bridge_message_sample with expected_message_size: {}, location_encoded_size: {}, xcm_size: {}, xcm_data_size: {}",
318+
expected_message_size, location_encoded_size, xcm_size, xcm_data_size,
319+
);
320+
321+
let xcm = xcm::VersionedXcm::<()>::V3(
322+
vec![xcm::v3::Instruction::<()>::ExpectPallet {
323+
index: 0,
324+
name: vec![42; xcm_data_size],
325+
module_name: vec![],
326+
crate_major: 0,
327+
min_crate_minor: 0,
328+
}]
329+
.into(),
330+
);
331+
332+
// this is the `BridgeMessage` from polkadot xcm builder, but it has no constructor
333+
// or public fields, so just tuple
334+
// (double encoding, because `.encode()` is called on original Xcm BLOB when it is pushed
335+
// to the storage)
336+
(location, xcm).encode().encode()
337+
}
338+
}

bridges/modules/messages/src/weights_ext.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -29,8 +29,8 @@ pub const EXPECTED_DEFAULT_MESSAGE_LENGTH: u32 = 128;
2929
/// calls we're checking here would fit 1KB.
3030
const SIGNED_EXTENSIONS_SIZE: u32 = 1024;
3131

32-
/// Number of extra bytes (excluding size of storage value itself) of storage proof, built at
33-
/// Rialto chain. This mostly depends on number of entries (and their density) in the storage trie.
32+
/// Number of extra bytes (excluding size of storage value itself) of storage proof.
33+
/// This mostly depends on number of entries (and their density) in the storage trie.
3434
/// Some reserve is reserved to account future chain growth.
3535
pub const EXTRA_STORAGE_PROOF_SIZE: u32 = 1024;
3636

0 commit comments

Comments
 (0)