@@ -29,7 +29,7 @@ use crate::{
2929 } ,
3030} ;
3131
32- use bp_messages:: storage_keys;
32+ use bp_messages:: { storage_keys, MessagePayload } ;
3333use bp_polkadot_core:: parachains:: ParaHash ;
3434use 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.
4646fn 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.
8580pub 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 )
8984where
9085 R : pallet_bridge_grandpa:: Config < FI , BridgedChain = UnderlyingChainOf < BridgedChain < B > > > ,
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.
128123pub 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 )
132127where
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+ }
0 commit comments