Skip to content

Commit 8efc2b3

Browse files
authored
Added receive_single_message_proof_with_dispatch benchmark (#1990)
* added receive_single_message_proof_with_dispatch benchmark * clippy
1 parent 6540f74 commit 8efc2b3

File tree

13 files changed

+276
-228
lines changed

13 files changed

+276
-228
lines changed

bin/millau/runtime/src/lib.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1007,7 +1007,7 @@ impl_runtime_apis! {
10071007
Runtime,
10081008
WithRialtoParachainsInstance,
10091009
WithRialtoParachainMessageBridge,
1010-
>(params)
1010+
>(params, xcm::v3::Junctions::Here)
10111011
}
10121012

10131013
fn prepare_message_delivery_proof(
@@ -1038,7 +1038,7 @@ impl_runtime_apis! {
10381038
Runtime,
10391039
RialtoGrandpaInstance,
10401040
WithRialtoMessageBridge,
1041-
>(params)
1041+
>(params, xcm::v3::Junctions::Here)
10421042
}
10431043

10441044
fn prepare_message_delivery_proof(

bin/millau/runtime/src/rialto_messages.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,7 @@ pub type FromRialtoMessageDispatch =
6767
bp_millau::Millau,
6868
bp_rialto::Rialto,
6969
crate::xcm_config::OnMillauBlobDispatcher,
70-
bridge_runtime_common::messages_xcm_extension::XcmRouterWeigher<crate::DbWeight>,
70+
(),
7171
>;
7272

7373
/// Maximal outbound payload size of Millau -> Rialto messages.

bin/millau/runtime/src/rialto_parachain_messages.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,7 @@ pub type FromRialtoParachainMessageDispatch =
6262
bp_millau::Millau,
6363
bp_rialto::Rialto,
6464
crate::xcm_config::OnMillauBlobDispatcher,
65-
bridge_runtime_common::messages_xcm_extension::XcmRouterWeigher<crate::DbWeight>,
65+
(),
6666
>;
6767

6868
/// Maximal outbound payload size of Millau -> RialtoParachain messages.

bin/millau/runtime/src/xcm_config.rs

Lines changed: 5 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -233,20 +233,16 @@ impl ExportXcm for ToRialtoOrRialtoParachainSwitchExporter {
233233
mod tests {
234234
use super::*;
235235
use crate::{
236-
rialto_messages::FromRialtoMessageDispatch,
237-
rialto_parachain_messages::FromRialtoParachainMessageDispatch, DbWeight,
238-
WithRialtoMessagesInstance, WithRialtoParachainMessagesInstance,
236+
rialto_messages::FromRialtoMessageDispatch, WithRialtoMessagesInstance,
237+
WithRialtoParachainMessagesInstance,
239238
};
240239
use bp_messages::{
241240
target_chain::{DispatchMessage, DispatchMessageData, MessageDispatch},
242241
LaneId, MessageKey,
243242
};
244-
use bridge_runtime_common::messages_xcm_extension::{
245-
XcmBlobMessageDispatchResult, XcmRouterWeigher,
246-
};
243+
use bridge_runtime_common::messages_xcm_extension::XcmBlobMessageDispatchResult;
247244
use codec::Encode;
248245
use pallet_bridge_messages::OutboundLanes;
249-
use sp_core::Get;
250246
use xcm_executor::XcmExecutor;
251247

252248
fn new_test_ext() -> sp_io::TestExternalities {
@@ -347,10 +343,7 @@ mod tests {
347343

348344
#[test]
349345
fn xcm_messages_from_rialto_are_dispatched() {
350-
let mut incoming_message = prepare_inbound_bridge_message();
351-
352-
let dispatch_weight = FromRialtoMessageDispatch::dispatch_weight(&mut incoming_message);
353-
assert_eq!(dispatch_weight, XcmRouterWeigher::<DbWeight>::get());
346+
let incoming_message = prepare_inbound_bridge_message();
354347

355348
// we care only about handing message to the XCM dispatcher, so we don't care about its
356349
// actual dispatch
@@ -364,11 +357,7 @@ mod tests {
364357

365358
#[test]
366359
fn xcm_messages_from_rialto_parachain_are_dispatched() {
367-
let mut incoming_message = prepare_inbound_bridge_message();
368-
369-
let dispatch_weight =
370-
FromRialtoParachainMessageDispatch::dispatch_weight(&mut incoming_message);
371-
assert_eq!(dispatch_weight, XcmRouterWeigher::<DbWeight>::get());
360+
let incoming_message = prepare_inbound_bridge_message();
372361

373362
// we care only about handing message to the XCM dispatcher, so we don't care about its
374363
// actual dispatch

bin/rialto-parachain/runtime/src/lib.rs

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -844,12 +844,10 @@ mod tests {
844844
LaneId, MessageKey,
845845
};
846846
use bridge_runtime_common::{
847-
integrity::check_additional_signed,
848-
messages_xcm_extension::{XcmBlobMessageDispatchResult, XcmRouterWeigher},
847+
integrity::check_additional_signed, messages_xcm_extension::XcmBlobMessageDispatchResult,
849848
};
850849
use codec::Encode;
851850
use pallet_bridge_messages::OutboundLanes;
852-
use sp_core::Get;
853851
use sp_runtime::generic::Era;
854852
use xcm_executor::XcmExecutor;
855853

@@ -914,10 +912,7 @@ mod tests {
914912
#[test]
915913
fn xcm_messages_from_millau_are_dispatched() {
916914
new_test_ext().execute_with(|| {
917-
let mut incoming_message = prepare_inbound_bridge_message();
918-
919-
let dispatch_weight = FromMillauMessageDispatch::dispatch_weight(&mut incoming_message);
920-
assert_eq!(dispatch_weight, XcmRouterWeigher::<()>::get());
915+
let incoming_message = prepare_inbound_bridge_message();
921916

922917
// we care only about handing message to the XCM dispatcher, so we don't care about its
923918
// actual dispatch

bin/rialto-parachain/runtime/src/millau_messages.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,7 @@ pub type FromMillauMessageDispatch =
6363
bp_rialto_parachain::RialtoParachain,
6464
bp_millau::Millau,
6565
crate::OnRialtoParachainBlobDispatcher,
66-
bridge_runtime_common::messages_xcm_extension::XcmRouterWeigher<()>,
66+
(),
6767
>;
6868

6969
/// Messages proof for Millau -> RialtoParachain messages.

bin/rialto/runtime/src/millau_messages.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,7 @@ pub type FromMillauMessageDispatch =
6060
bp_rialto::Rialto,
6161
bp_millau::Millau,
6262
crate::xcm_config::OnRialtoBlobDispatcher,
63-
bridge_runtime_common::messages_xcm_extension::XcmRouterWeigher<crate::DbWeight>,
63+
(),
6464
>;
6565

6666
/// Messages proof for Millau -> Rialto messages.

bin/rialto/runtime/src/xcm_config.rs

Lines changed: 3 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -189,18 +189,15 @@ mod tests {
189189
use super::*;
190190
use crate::{
191191
millau_messages::{FromMillauMessageDispatch, XCM_LANE},
192-
DbWeight, WithMillauMessagesInstance,
192+
WithMillauMessagesInstance,
193193
};
194194
use bp_messages::{
195195
target_chain::{DispatchMessage, DispatchMessageData, MessageDispatch},
196196
LaneId, MessageKey,
197197
};
198-
use bridge_runtime_common::messages_xcm_extension::{
199-
XcmBlobMessageDispatchResult, XcmRouterWeigher,
200-
};
198+
use bridge_runtime_common::messages_xcm_extension::XcmBlobMessageDispatchResult;
201199
use codec::Encode;
202200
use pallet_bridge_messages::OutboundLanes;
203-
use sp_core::Get;
204201
use xcm_executor::XcmExecutor;
205202

206203
fn new_test_ext() -> sp_io::TestExternalities {
@@ -263,10 +260,7 @@ mod tests {
263260

264261
#[test]
265262
fn xcm_messages_from_millau_are_dispatched() {
266-
let mut incoming_message = prepare_inbound_bridge_message();
267-
268-
let dispatch_weight = FromMillauMessageDispatch::dispatch_weight(&mut incoming_message);
269-
assert_eq!(dispatch_weight, XcmRouterWeigher::<DbWeight>::get());
263+
let incoming_message = prepare_inbound_bridge_message();
270264

271265
// we care only about handing message to the XCM dispatcher, so we don't care about its
272266
// actual dispatch

bin/runtime-common/src/messages_benchmarking.rs

Lines changed: 41 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,39 @@ use pallet_bridge_messages::benchmarking::{MessageDeliveryProofParams, MessagePr
4040
use sp_runtime::traits::{Header, Zero};
4141
use sp_std::prelude::*;
4242
use sp_trie::{trie_types::TrieDBMutBuilderV1, LayoutV1, MemoryDB, TrieMut};
43+
use xcm::v3::prelude::*;
44+
45+
/// Prepare inbound bridge message according to given message proof parameters.
46+
fn prepare_inbound_message(
47+
params: &MessageProofParams,
48+
destination: InteriorMultiLocation,
49+
) -> Vec<u8> {
50+
// we only care about **this** message size when message proof needs to be `Minimal`
51+
let expected_size = match params.size {
52+
StorageProofSize::Minimal(size) => size as usize,
53+
_ => 0,
54+
};
55+
56+
// if we don't need a correct message, then we may just return some random blob
57+
if !params.is_successful_dispatch_expected {
58+
return vec![0u8; expected_size]
59+
}
60+
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()
75+
}
4376

4477
/// Prepare proof of messages for the `receive_messages_proof` call.
4578
///
@@ -51,6 +84,7 @@ use sp_trie::{trie_types::TrieDBMutBuilderV1, LayoutV1, MemoryDB, TrieMut};
5184
/// function.
5285
pub fn prepare_message_proof_from_grandpa_chain<R, FI, B>(
5386
params: MessageProofParams,
87+
message_destination: InteriorMultiLocation,
5488
) -> (FromBridgedChainMessagesProof<HashOf<BridgedChain<B>>>, Weight)
5589
where
5690
R: pallet_bridge_grandpa::Config<FI, BridgedChain = UnderlyingChainOf<BridgedChain<B>>>,
@@ -61,12 +95,9 @@ where
6195
let (state_root, storage_proof) = prepare_messages_storage_proof::<B>(
6296
params.lane,
6397
params.message_nonces.clone(),
64-
params.outbound_lane_data,
98+
params.outbound_lane_data.clone(),
6599
params.size,
66-
match params.size {
67-
StorageProofSize::Minimal(ref size) => vec![0u8; *size as _],
68-
_ => vec![],
69-
},
100+
prepare_inbound_message(&params, message_destination),
70101
encode_all_messages,
71102
encode_lane_data,
72103
);
@@ -82,7 +113,7 @@ where
82113
nonces_start: *params.message_nonces.start(),
83114
nonces_end: *params.message_nonces.end(),
84115
},
85-
Weight::zero(),
116+
Weight::MAX / 1000,
86117
)
87118
}
88119

@@ -96,6 +127,7 @@ where
96127
/// `prepare_message_proof_from_grandpa_chain` function.
97128
pub fn prepare_message_proof_from_parachain<R, PI, B>(
98129
params: MessageProofParams,
130+
message_destination: InteriorMultiLocation,
99131
) -> (FromBridgedChainMessagesProof<HashOf<BridgedChain<B>>>, Weight)
100132
where
101133
R: pallet_bridge_parachains::Config<PI>,
@@ -107,12 +139,9 @@ where
107139
let (state_root, storage_proof) = prepare_messages_storage_proof::<B>(
108140
params.lane,
109141
params.message_nonces.clone(),
110-
params.outbound_lane_data,
142+
params.outbound_lane_data.clone(),
111143
params.size,
112-
match params.size {
113-
StorageProofSize::Minimal(ref size) => vec![0u8; *size as _],
114-
_ => vec![],
115-
},
144+
prepare_inbound_message(&params, message_destination),
116145
encode_all_messages,
117146
encode_lane_data,
118147
);
@@ -129,7 +158,7 @@ where
129158
nonces_start: *params.message_nonces.start(),
130159
nonces_end: *params.message_nonces.end(),
131160
},
132-
Weight::zero(),
161+
Weight::MAX / 1000,
133162
)
134163
}
135164

bin/runtime-common/src/messages_xcm_extension.rs

Lines changed: 17 additions & 53 deletions
Original file line numberDiff line numberDiff line change
@@ -28,43 +28,15 @@ use bp_messages::{
2828
};
2929
use bp_runtime::{messages::MessageDispatchResult, AccountIdOf, Chain};
3030
use codec::{Decode, Encode};
31-
use frame_support::{
32-
dispatch::Weight, traits::Get, weights::RuntimeDbWeight, CloneNoBound, EqNoBound,
33-
PartialEqNoBound,
34-
};
31+
use frame_support::{dispatch::Weight, CloneNoBound, EqNoBound, PartialEqNoBound};
32+
use pallet_bridge_messages::WeightInfoExt as MessagesPalletWeights;
3533
use scale_info::TypeInfo;
36-
use sp_std::marker::PhantomData;
34+
use sp_runtime::SaturatedConversion;
3735
use xcm_builder::{DispatchBlob, DispatchBlobError, HaulBlob, HaulBlobError};
3836

3937
/// Plain "XCM" payload, which we transfer through bridge
4038
pub type XcmAsPlainPayload = sp_std::prelude::Vec<u8>;
4139

42-
// TODO: below are just rough estimations. Other things also happen there (including hashing and so
43-
// on). Shall we do some benchmarking??? TODO: add proof_size component here
44-
// https://github.com/paritytech/parity-bridges-common/issues/1986
45-
46-
/// Simple weigher for incoming XCM dispatch at **bridge hubs** to use with
47-
/// `XcmBlobMessageDispatch`.
48-
///
49-
/// By our design, message at bridge hub is simply pushed to some other queue. This implementation
50-
/// is for this case only. If your runtime performs some other actions with incoming XCM messages,
51-
/// you shall use your own implementation.
52-
///
53-
/// If message is redirected to the relay chain, then `ParentAsUmp` is used and it roughly does
54-
/// 1 db read and 1 db write (in its `send_upward_message` method).
55-
///
56-
/// If message is redirected to some sibling parachain, then `XcmpQueue` is used and
57-
/// it roughly does 2 db reads and 2 db writes (in its `SendXcm` implementation).
58-
///
59-
/// The difference is not that big, so let's choose maximal.
60-
pub struct XcmRouterWeigher<T>(PhantomData<T>);
61-
62-
impl<T: Get<RuntimeDbWeight>> Get<Weight> for XcmRouterWeigher<T> {
63-
fn get() -> Weight {
64-
T::get().reads_writes(2, 2)
65-
}
66-
}
67-
6840
/// Message dispatch result type for single message
6941
#[derive(CloneNoBound, EqNoBound, PartialEqNoBound, Encode, Decode, Debug, TypeInfo)]
7042
pub enum XcmBlobMessageDispatchResult {
@@ -74,38 +46,35 @@ pub enum XcmBlobMessageDispatchResult {
7446
}
7547

7648
/// [`XcmBlobMessageDispatch`] is responsible for dispatching received messages
77-
pub struct XcmBlobMessageDispatch<
78-
SourceBridgeHubChain,
79-
TargetBridgeHubChain,
80-
DispatchBlob,
81-
DispatchBlobWeigher,
82-
> {
49+
pub struct XcmBlobMessageDispatch<SourceBridgeHubChain, TargetBridgeHubChain, DispatchBlob, Weights>
50+
{
8351
_marker: sp_std::marker::PhantomData<(
8452
SourceBridgeHubChain,
8553
TargetBridgeHubChain,
8654
DispatchBlob,
87-
DispatchBlobWeigher,
55+
Weights,
8856
)>,
8957
}
9058

9159
impl<
9260
SourceBridgeHubChain: Chain,
9361
TargetBridgeHubChain: Chain,
9462
BlobDispatcher: DispatchBlob,
95-
DispatchBlobWeigher: Get<Weight>,
63+
Weights: MessagesPalletWeights,
9664
> MessageDispatch<AccountIdOf<SourceBridgeHubChain>>
97-
for XcmBlobMessageDispatch<
98-
SourceBridgeHubChain,
99-
TargetBridgeHubChain,
100-
BlobDispatcher,
101-
DispatchBlobWeigher,
102-
>
65+
for XcmBlobMessageDispatch<SourceBridgeHubChain, TargetBridgeHubChain, BlobDispatcher, Weights>
10366
{
10467
type DispatchPayload = XcmAsPlainPayload;
10568
type DispatchLevelResult = XcmBlobMessageDispatchResult;
10669

107-
fn dispatch_weight(_message: &mut DispatchMessage<Self::DispatchPayload>) -> Weight {
108-
DispatchBlobWeigher::get()
70+
fn dispatch_weight(message: &mut DispatchMessage<Self::DispatchPayload>) -> Weight {
71+
match message.data.payload {
72+
Ok(ref payload) => {
73+
let payload_size = payload.encoded_size().saturated_into();
74+
Weights::message_dispatch_weight(payload_size)
75+
},
76+
Err(_) => Weight::zero(),
77+
}
10978
}
11079

11180
fn dispatch(
@@ -122,7 +91,6 @@ impl<
12291
message.key.nonce
12392
);
12493
return MessageDispatchResult {
125-
// TODO:check-parameter - setup uspent_weight? https://github.com/paritytech/polkadot/issues/6629
12694
unspent_weight: Weight::zero(),
12795
dispatch_level_result: XcmBlobMessageDispatchResult::InvalidPayload,
12896
}
@@ -158,11 +126,7 @@ impl<
158126
XcmBlobMessageDispatchResult::NotDispatched(e)
159127
},
160128
};
161-
MessageDispatchResult {
162-
// TODO:check-parameter - setup uspent_weight? https://github.com/paritytech/polkadot/issues/6629
163-
unspent_weight: Weight::zero(),
164-
dispatch_level_result,
165-
}
129+
MessageDispatchResult { unspent_weight: Weight::zero(), dispatch_level_result }
166130
}
167131
}
168132

0 commit comments

Comments
 (0)