Skip to content

Commit 20bd1fd

Browse files
svyatonikacatangiu
andauthored
Add weight of refund extension post_dispatch to the weights of messages pallet (paritytech#2089)
* add weight of refund extension post_dispatch to the weights of messages pallet * fixed tests and removed TODO * verify runtime overhead weight in integrity tests * add integrity tests to rialto-parachain runtime * refactor weights a bit * refund ext is disabled for Rialto <> Millau bridge * Update bin/runtime-common/src/integrity.rs Co-authored-by: Adrian Catangiu <[email protected]> * Update bin/runtime-common/src/integrity.rs Co-authored-by: Adrian Catangiu <[email protected]> * Update modules/relayers/src/weights_ext.rs Co-authored-by: Adrian Catangiu <[email protected]> --------- Co-authored-by: Adrian Catangiu <[email protected]>
1 parent 95dba66 commit 20bd1fd

File tree

12 files changed

+308
-30
lines changed

12 files changed

+308
-30
lines changed

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -599,7 +599,7 @@ generate_bridge_reject_obsolete_headers_and_messages! {
599599

600600
bp_runtime::generate_static_str_provider!(BridgeRefundRialtoPara2000Lane0Msgs);
601601
/// Signed extension that refunds relayers that are delivering messages from the Rialto parachain.
602-
pub type PriorityBoostPerMessage = ConstU64<699_683_285>;
602+
pub type PriorityBoostPerMessage = ConstU64<324_316_715>;
603603
pub type BridgeRefundRialtoParachainMessages = RefundBridgedParachainMessages<
604604
Runtime,
605605
RefundableParachain<WithRialtoParachainsInstance, RialtoParachainId>,

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

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ use bridge_runtime_common::{
2626
messages_xcm_extension::{XcmBlobHauler, XcmBlobHaulerAdapter},
2727
};
2828
use frame_support::{parameter_types, weights::Weight, RuntimeDebug};
29+
use pallet_bridge_relayers::WeightInfoExt as _;
2930
use xcm::latest::prelude::*;
3031
use xcm_builder::HaulBlobExporter;
3132

@@ -139,6 +140,14 @@ impl pallet_bridge_messages::WeightInfoExt for crate::weights::RialtoMessagesWei
139140
fn expected_extra_storage_proof_size() -> u32 {
140141
bp_rialto::EXTRA_STORAGE_PROOF_SIZE
141142
}
143+
144+
fn receive_messages_proof_overhead_from_runtime() -> Weight {
145+
pallet_bridge_relayers::weights::BridgeWeight::<Runtime>::receive_messages_proof_overhead_from_runtime()
146+
}
147+
148+
fn receive_messages_delivery_proof_overhead_from_runtime() -> Weight {
149+
pallet_bridge_relayers::weights::BridgeWeight::<Runtime>::receive_messages_delivery_proof_overhead_from_runtime()
150+
}
142151
}
143152

144153
#[cfg(test)]
@@ -161,6 +170,7 @@ mod tests {
161170
bp_rialto::EXTRA_STORAGE_PROOF_SIZE,
162171
bp_millau::MAX_UNREWARDED_RELAYERS_IN_CONFIRMATION_TX,
163172
bp_millau::MAX_UNCONFIRMED_MESSAGES_IN_CONFIRMATION_TX,
173+
false,
164174
);
165175
}
166176

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

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ use bridge_runtime_common::{
2828
messages_xcm_extension::{XcmBlobHauler, XcmBlobHaulerAdapter},
2929
};
3030
use frame_support::{parameter_types, weights::Weight, RuntimeDebug};
31+
use pallet_bridge_relayers::WeightInfoExt as _;
3132
use xcm::latest::prelude::*;
3233
use xcm_builder::HaulBlobExporter;
3334

@@ -142,6 +143,14 @@ impl pallet_bridge_messages::WeightInfoExt
142143
fn expected_extra_storage_proof_size() -> u32 {
143144
bp_rialto_parachain::EXTRA_STORAGE_PROOF_SIZE
144145
}
146+
147+
fn receive_messages_proof_overhead_from_runtime() -> Weight {
148+
pallet_bridge_relayers::weights::BridgeWeight::<Runtime>::receive_messages_proof_overhead_from_runtime()
149+
}
150+
151+
fn receive_messages_delivery_proof_overhead_from_runtime() -> Weight {
152+
pallet_bridge_relayers::weights::BridgeWeight::<Runtime>::receive_messages_delivery_proof_overhead_from_runtime()
153+
}
145154
}
146155

147156
#[cfg(test)]
@@ -167,6 +176,7 @@ mod tests {
167176
bp_rialto_parachain::EXTRA_STORAGE_PROOF_SIZE,
168177
bp_millau::MAX_UNREWARDED_RELAYERS_IN_CONFIRMATION_TX,
169178
bp_millau::MAX_UNCONFIRMED_MESSAGES_IN_CONFIRMATION_TX,
179+
true,
170180
);
171181
}
172182

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -154,6 +154,7 @@ mod tests {
154154
bp_millau::EXTRA_STORAGE_PROOF_SIZE,
155155
bp_rialto_parachain::MAX_UNREWARDED_RELAYERS_IN_CONFIRMATION_TX,
156156
bp_rialto_parachain::MAX_UNCONFIRMED_MESSAGES_IN_CONFIRMATION_TX,
157+
false,
157158
);
158159
}
159160

bridges/bin/rialto/runtime/src/millau_messages.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -154,6 +154,7 @@ mod tests {
154154
bp_millau::EXTRA_STORAGE_PROOF_SIZE,
155155
bp_rialto::MAX_UNREWARDED_RELAYERS_IN_CONFIRMATION_TX,
156156
bp_rialto::MAX_UNCONFIRMED_MESSAGES_IN_CONFIRMATION_TX,
157+
false,
157158
);
158159
}
159160

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

Lines changed: 29 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -24,8 +24,9 @@ use crate::{messages, messages::MessageBridge};
2424
use bp_messages::{InboundLaneData, MessageNonce};
2525
use bp_runtime::{Chain, ChainId};
2626
use codec::Encode;
27-
use frame_support::{storage::generator::StorageValue, traits::Get};
27+
use frame_support::{storage::generator::StorageValue, traits::Get, weights::Weight};
2828
use frame_system::limits;
29+
use pallet_bridge_messages::WeightInfoExt as _;
2930
use sp_runtime::traits::SignedExtension;
3031

3132
/// Macro that ensures that the runtime configuration and chain primitives crate are sharing
@@ -289,15 +290,25 @@ where
289290
}
290291

291292
/// Check that the message lane weights are correct.
292-
pub fn check_message_lane_weights<C: Chain, T: frame_system::Config>(
293+
pub fn check_message_lane_weights<
294+
C: Chain,
295+
T: frame_system::Config + pallet_bridge_messages::Config,
296+
>(
293297
bridged_chain_extra_storage_proof_size: u32,
294298
this_chain_max_unrewarded_relayers: MessageNonce,
295299
this_chain_max_unconfirmed_messages: MessageNonce,
300+
// whether `RefundBridgedParachainMessages` extension is deployed at runtime and is used for
301+
// refunding this bridge transactions?
302+
//
303+
// in other words: pass true for all known production chains
304+
runtime_includes_refund_extension: bool,
296305
) {
297-
type Weights<T> = pallet_bridge_messages::weights::BridgeWeight<T>;
306+
type Weights<T> = <T as pallet_bridge_messages::Config>::WeightInfo;
298307

308+
// check basic weight assumptions
299309
pallet_bridge_messages::ensure_weights_are_correct::<Weights<T>>();
300310

311+
// check that weights allow us to receive messages
301312
let max_incoming_message_proof_size = bridged_chain_extra_storage_proof_size
302313
.saturating_add(messages::target::maximal_incoming_message_size(C::max_extrinsic_size()));
303314
pallet_bridge_messages::ensure_able_to_receive_message::<Weights<T>>(
@@ -307,6 +318,7 @@ pub fn check_message_lane_weights<C: Chain, T: frame_system::Config>(
307318
messages::target::maximal_incoming_message_dispatch_weight(C::max_extrinsic_weight()),
308319
);
309320

321+
// check that weights allow us to receive delivery confirmations
310322
let max_incoming_inbound_lane_data_proof_size =
311323
InboundLaneData::<()>::encoded_size_hint_u32(this_chain_max_unrewarded_relayers as _);
312324
pallet_bridge_messages::ensure_able_to_receive_confirmation::<Weights<T>>(
@@ -316,6 +328,20 @@ pub fn check_message_lane_weights<C: Chain, T: frame_system::Config>(
316328
this_chain_max_unrewarded_relayers,
317329
this_chain_max_unconfirmed_messages,
318330
);
331+
332+
// check that extra weights of delivery/confirmation transactions include the weight
333+
// of `RefundBridgedParachainMessages` operations. This signed extension assumes the worst case
334+
// (i.e. slashing if delivery transaction was invalid) and refunds some weight if
335+
// assumption was wrong (i.e. if we did refund instead of slashing). This check
336+
// ensures the extension will not refund weight when it doesn't need to (i.e. if pallet
337+
// weights do not account weights of refund extension).
338+
if runtime_includes_refund_extension {
339+
assert_ne!(Weights::<T>::receive_messages_proof_overhead_from_runtime(), Weight::zero());
340+
assert_ne!(
341+
Weights::<T>::receive_messages_delivery_proof_overhead_from_runtime(),
342+
Weight::zero()
343+
);
344+
}
319345
}
320346

321347
/// Check that the `AdditionalSigned` type of a wrapped runtime is the same as the one of the

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

Lines changed: 35 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,9 @@ use pallet_bridge_parachains::{
4040
BoundedBridgeGrandpaConfig, CallSubType as ParachainsCallSubType, Config as ParachainsConfig,
4141
RelayBlockNumber, SubmitParachainHeadsHelper, SubmitParachainHeadsInfo,
4242
};
43-
use pallet_bridge_relayers::{Config as RelayersConfig, Pallet as RelayersPallet};
43+
use pallet_bridge_relayers::{
44+
Config as RelayersConfig, Pallet as RelayersPallet, WeightInfoExt as _,
45+
};
4446
use pallet_transaction_payment::{Config as TransactionPaymentConfig, OnChargeTransaction};
4547
use pallet_utility::{Call as UtilityCall, Config as UtilityConfig, Pallet as UtilityPallet};
4648
use scale_info::TypeInfo;
@@ -445,11 +447,19 @@ where
445447

446448
// decrease post-dispatch weight/size using extra weight/size that we know now
447449
let post_info_len = len.saturating_sub(extra_size as usize);
448-
let mut post_info = *post_info;
449-
post_info.actual_weight =
450-
Some(post_info.actual_weight.unwrap_or(info.weight).saturating_sub(extra_weight));
450+
let mut post_info_weight =
451+
post_info.actual_weight.unwrap_or(info.weight).saturating_sub(extra_weight);
452+
453+
// let's also replace the weight of slashing relayer with the weight of rewarding relayer
454+
if call_info.is_receive_messages_proof_call() {
455+
post_info_weight = post_info_weight.saturating_sub(
456+
<Runtime as RelayersConfig>::WeightInfo::extra_weight_of_successful_receive_messages_proof_call(),
457+
);
458+
}
451459

452460
// compute the relayer refund
461+
let mut post_info = *post_info;
462+
post_info.actual_weight = Some(post_info_weight);
453463
let refund = Refund::compute_refund(info, &post_info, post_info_len, tip);
454464

455465
// we can finally reward relayer
@@ -1052,7 +1062,20 @@ mod tests {
10521062
assert_eq!(post_dispatch_result, Ok(()));
10531063
}
10541064

1055-
fn expected_reward() -> ThisChainBalance {
1065+
fn expected_delivery_reward() -> ThisChainBalance {
1066+
let mut post_dispatch_info = post_dispatch_info();
1067+
let extra_weight = <TestRuntime as RelayersConfig>::WeightInfo::extra_weight_of_successful_receive_messages_proof_call();
1068+
post_dispatch_info.actual_weight =
1069+
Some(dispatch_info().weight.saturating_sub(extra_weight));
1070+
pallet_transaction_payment::Pallet::<TestRuntime>::compute_actual_fee(
1071+
1024,
1072+
&dispatch_info(),
1073+
&post_dispatch_info,
1074+
Zero::zero(),
1075+
)
1076+
}
1077+
1078+
fn expected_confirmation_reward() -> ThisChainBalance {
10561079
pallet_transaction_payment::Pallet::<TestRuntime>::compute_actual_fee(
10571080
1024,
10581081
&dispatch_info(),
@@ -1449,7 +1472,7 @@ mod tests {
14491472

14501473
// without any size/weight refund: we expect regular reward
14511474
let pre_dispatch_data = all_finality_pre_dispatch_data();
1452-
let regular_reward = expected_reward();
1475+
let regular_reward = expected_delivery_reward();
14531476
run_post_dispatch(Some(pre_dispatch_data), Ok(()));
14541477
assert_eq!(
14551478
RelayersPallet::<TestRuntime>::relayer_reward(
@@ -1496,7 +1519,7 @@ mod tests {
14961519
relayer_account_at_this_chain(),
14971520
MsgProofsRewardsAccount::get()
14981521
),
1499-
Some(expected_reward()),
1522+
Some(expected_delivery_reward()),
15001523
);
15011524

15021525
run_post_dispatch(Some(all_finality_confirmation_pre_dispatch_data()), Ok(()));
@@ -1505,7 +1528,7 @@ mod tests {
15051528
relayer_account_at_this_chain(),
15061529
MsgDeliveryProofsRewardsAccount::get()
15071530
),
1508-
Some(expected_reward()),
1531+
Some(expected_confirmation_reward()),
15091532
);
15101533
});
15111534
}
@@ -1521,7 +1544,7 @@ mod tests {
15211544
relayer_account_at_this_chain(),
15221545
MsgProofsRewardsAccount::get()
15231546
),
1524-
Some(expected_reward()),
1547+
Some(expected_delivery_reward()),
15251548
);
15261549

15271550
run_post_dispatch(Some(parachain_finality_confirmation_pre_dispatch_data()), Ok(()));
@@ -1530,7 +1553,7 @@ mod tests {
15301553
relayer_account_at_this_chain(),
15311554
MsgDeliveryProofsRewardsAccount::get()
15321555
),
1533-
Some(expected_reward()),
1556+
Some(expected_confirmation_reward()),
15341557
);
15351558
});
15361559
}
@@ -1546,7 +1569,7 @@ mod tests {
15461569
relayer_account_at_this_chain(),
15471570
MsgProofsRewardsAccount::get()
15481571
),
1549-
Some(expected_reward()),
1572+
Some(expected_delivery_reward()),
15501573
);
15511574

15521575
run_post_dispatch(Some(confirmation_pre_dispatch_data()), Ok(()));
@@ -1555,7 +1578,7 @@ mod tests {
15551578
relayer_account_at_this_chain(),
15561579
MsgDeliveryProofsRewardsAccount::get()
15571580
),
1558-
Some(expected_reward()),
1581+
Some(expected_confirmation_reward()),
15591582
);
15601583
});
15611584
}

bridges/modules/messages/src/weights_ext.rs

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -266,6 +266,27 @@ pub trait WeightInfoExt: WeightInfo {
266266
/// this value, we're going to charge relayer for that.
267267
fn expected_extra_storage_proof_size() -> u32;
268268

269+
// Our configuration assumes that the runtime has special signed extensions used to:
270+
//
271+
// 1) reject obsolete delivery and confirmation transactions;
272+
//
273+
// 2) refund transaction cost to relayer and register his rewards.
274+
//
275+
// The checks in (1) are trivial, so its computation weight may be ignored. And we only touch
276+
// storage values that are read during the call. So we may ignore the weight of this check.
277+
//
278+
// However, during (2) we read and update storage values of other pallets
279+
// (`pallet-bridge-relayers` and balances/assets pallet). So we need to add this weight to the
280+
// weight of our call. Hence two following methods.
281+
282+
/// Extra weight that is added to the `receive_messages_proof` call weight by signed extensions
283+
/// that are declared at runtime level.
284+
fn receive_messages_proof_overhead_from_runtime() -> Weight;
285+
286+
/// Extra weight that is added to the `receive_messages_delivery_proof` call weight by signed
287+
/// extensions that are declared at runtime level.
288+
fn receive_messages_delivery_proof_overhead_from_runtime() -> Weight;
289+
269290
// Functions that are directly mapped to extrinsics weights.
270291

271292
/// Weight of message delivery extrinsic.
@@ -276,6 +297,8 @@ pub trait WeightInfoExt: WeightInfo {
276297
) -> Weight {
277298
// basic components of extrinsic weight
278299
let transaction_overhead = Self::receive_messages_proof_overhead();
300+
let transaction_overhead_from_runtime =
301+
Self::receive_messages_proof_overhead_from_runtime();
279302
let outbound_state_delivery_weight =
280303
Self::receive_messages_proof_outbound_lane_state_overhead();
281304
let messages_delivery_weight =
@@ -292,6 +315,7 @@ pub trait WeightInfoExt: WeightInfo {
292315
);
293316

294317
transaction_overhead
318+
.saturating_add(transaction_overhead_from_runtime)
295319
.saturating_add(outbound_state_delivery_weight)
296320
.saturating_add(messages_delivery_weight)
297321
.saturating_add(messages_dispatch_weight)
@@ -305,6 +329,8 @@ pub trait WeightInfoExt: WeightInfo {
305329
) -> Weight {
306330
// basic components of extrinsic weight
307331
let transaction_overhead = Self::receive_messages_delivery_proof_overhead();
332+
let transaction_overhead_from_runtime =
333+
Self::receive_messages_delivery_proof_overhead_from_runtime();
308334
let messages_overhead =
309335
Self::receive_messages_delivery_proof_messages_overhead(relayers_state.total_messages);
310336
let relayers_overhead = Self::receive_messages_delivery_proof_relayers_overhead(
@@ -319,6 +345,7 @@ pub trait WeightInfoExt: WeightInfo {
319345
);
320346

321347
transaction_overhead
348+
.saturating_add(transaction_overhead_from_runtime)
322349
.saturating_add(messages_overhead)
323350
.saturating_add(relayers_overhead)
324351
.saturating_add(proof_size_overhead)
@@ -424,12 +451,28 @@ impl WeightInfoExt for () {
424451
fn expected_extra_storage_proof_size() -> u32 {
425452
EXTRA_STORAGE_PROOF_SIZE
426453
}
454+
455+
fn receive_messages_proof_overhead_from_runtime() -> Weight {
456+
Weight::zero()
457+
}
458+
459+
fn receive_messages_delivery_proof_overhead_from_runtime() -> Weight {
460+
Weight::zero()
461+
}
427462
}
428463

429464
impl<T: frame_system::Config> WeightInfoExt for crate::weights::BridgeWeight<T> {
430465
fn expected_extra_storage_proof_size() -> u32 {
431466
EXTRA_STORAGE_PROOF_SIZE
432467
}
468+
469+
fn receive_messages_proof_overhead_from_runtime() -> Weight {
470+
Weight::zero()
471+
}
472+
473+
fn receive_messages_delivery_proof_overhead_from_runtime() -> Weight {
474+
Weight::zero()
475+
}
433476
}
434477

435478
#[cfg(test)]

0 commit comments

Comments
 (0)