Skip to content
This repository was archived by the owner on Feb 29, 2024. It is now read-only.

Commit 2a56055

Browse files
committed
1 parent 53c8373 commit 2a56055

File tree

9 files changed

+60
-42
lines changed

9 files changed

+60
-42
lines changed

modules/grandpa/src/lib.rs

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -118,9 +118,7 @@ pub mod pallet {
118118
fn on_initialize(_n: T::BlockNumber) -> frame_support::weights::Weight {
119119
<RequestCount<T, I>>::mutate(|count| *count = count.saturating_sub(1));
120120

121-
Weight::from_ref_time(0)
122-
.saturating_add(T::DbWeight::get().reads(1))
123-
.saturating_add(T::DbWeight::get().writes(1))
121+
T::DbWeight::get().reads_writes(1, 1)
124122
}
125123
}
126124

modules/messages/src/lib.rs

Lines changed: 5 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -357,7 +357,7 @@ pub mod pallet {
357357
// on this lane. We can't dispatch lane messages out-of-order, so if declared
358358
// weight is not enough, let's move to next lane
359359
let dispatch_weight = T::MessageDispatch::dispatch_weight(&mut message);
360-
if dispatch_weight.ref_time() > dispatch_weight_left.ref_time() {
360+
if dispatch_weight.any_gt(dispatch_weight_left) {
361361
log::trace!(
362362
target: LOG_TARGET,
363363
"Cannot dispatch any more messages on lane {:?}. Weight: declared={}, left={}",
@@ -396,10 +396,7 @@ pub mod pallet {
396396
| ReceivalResult::TooManyUnconfirmedMessages => (dispatch_weight, true),
397397
};
398398

399-
let unspent_weight =
400-
sp_std::cmp::min_by(unspent_weight, dispatch_weight, |w1, w2| {
401-
w1.ref_time().cmp(&w2.ref_time())
402-
});
399+
let unspent_weight = unspent_weight.min(dispatch_weight);
403400
dispatch_weight_left -= dispatch_weight - unspent_weight;
404401
actual_weight = actual_weight.saturating_sub(unspent_weight).saturating_sub(
405402
// delivery call weight formula assumes that the fee is paid at
@@ -408,7 +405,7 @@ pub mod pallet {
408405
if refund_pay_dispatch_fee {
409406
T::WeightInfo::pay_inbound_dispatch_fee_overhead()
410407
} else {
411-
Weight::from_ref_time(0)
408+
Weight::zero()
412409
},
413410
);
414411
}
@@ -528,7 +525,7 @@ pub mod pallet {
528525
let actual_callback_weight =
529526
T::OnDeliveryConfirmed::on_messages_delivered(&lane_id, &confirmed_messages);
530527
match preliminary_callback_overhead.checked_sub(&actual_callback_weight) {
531-
Some(difference) if difference.ref_time() == 0 => (),
528+
Some(difference) if difference.is_zero() => (),
532529
Some(difference) => {
533530
log::trace!(
534531
target: LOG_TARGET,
@@ -846,7 +843,7 @@ fn send_message<T: Config<I>, I: 'static>(
846843
T::WeightInfo::single_message_callback_overhead(T::DbWeight::get());
847844
let actual_callback_weight = T::OnMessageAccepted::on_messages_accepted(&lane_id, &nonce);
848845
match single_message_callback_overhead.checked_sub(&actual_callback_weight) {
849-
Some(difference) if difference.ref_time() == 0 => (),
846+
Some(difference) if difference.is_zero() => (),
850847
Some(difference) => {
851848
log::trace!(
852849
target: LOG_TARGET,

modules/messages/src/weights_ext.rs

Lines changed: 19 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -44,26 +44,26 @@ pub fn ensure_weights_are_correct<W: WeightInfoExt>(
4444
db_weight: RuntimeDbWeight,
4545
) {
4646
// verify `send_message` weight components
47-
assert_ne!(W::send_message_overhead(), Weight::from_ref_time(0));
48-
assert_ne!(W::send_message_size_overhead(0), Weight::from_ref_time(0));
47+
assert_ne!(W::send_message_overhead(), Weight::zero());
48+
assert_ne!(W::send_message_size_overhead(0), Weight::zero());
4949

5050
// verify `receive_messages_proof` weight components
51-
assert_ne!(W::receive_messages_proof_overhead(), Weight::from_ref_time(0));
52-
assert_ne!(W::receive_messages_proof_messages_overhead(1), Weight::from_ref_time(0));
53-
assert_ne!(W::receive_messages_proof_outbound_lane_state_overhead(), Weight::from_ref_time(0));
54-
assert_ne!(W::storage_proof_size_overhead(1), Weight::from_ref_time(0));
51+
assert_ne!(W::receive_messages_proof_overhead(), Weight::zero());
52+
assert_ne!(W::receive_messages_proof_messages_overhead(1), Weight::zero());
53+
assert_ne!(W::receive_messages_proof_outbound_lane_state_overhead(), Weight::zero());
54+
assert_ne!(W::storage_proof_size_overhead(1), Weight::zero());
5555

5656
// verify that the hardcoded value covers `receive_messages_proof` weight
5757
let actual_single_regular_message_delivery_tx_weight = W::receive_messages_proof_weight(
5858
&PreComputedSize(
5959
(EXPECTED_DEFAULT_MESSAGE_LENGTH + W::expected_extra_storage_proof_size()) as usize,
6060
),
6161
1,
62-
Weight::from_ref_time(0),
62+
Weight::zero(),
6363
);
6464
assert!(
65-
actual_single_regular_message_delivery_tx_weight.ref_time()
66-
<= expected_default_message_delivery_tx_weight.ref_time(),
65+
actual_single_regular_message_delivery_tx_weight
66+
.all_lte(expected_default_message_delivery_tx_weight),
6767
"Default message delivery transaction weight {} is larger than expected weight {}",
6868
actual_single_regular_message_delivery_tx_weight,
6969
expected_default_message_delivery_tx_weight,
@@ -80,8 +80,8 @@ pub fn ensure_weights_are_correct<W: WeightInfoExt>(
8080
);
8181

8282
// verify `receive_messages_delivery_proof` weight components
83-
assert_ne!(W::receive_messages_delivery_proof_overhead(), Weight::from_ref_time(0));
84-
assert_ne!(W::storage_proof_size_overhead(1), Weight::from_ref_time(0));
83+
assert_ne!(W::receive_messages_delivery_proof_overhead(), Weight::zero());
84+
assert_ne!(W::storage_proof_size_overhead(1), Weight::zero());
8585

8686
// `receive_messages_delivery_proof_messages_overhead` and
8787
// `receive_messages_delivery_proof_relayers_overhead` may return zero if rewards are not paid
@@ -98,8 +98,8 @@ pub fn ensure_weights_are_correct<W: WeightInfoExt>(
9898
db_weight,
9999
);
100100
assert!(
101-
actual_messages_delivery_confirmation_tx_weight.ref_time()
102-
<= expected_messages_delivery_confirmation_tx_weight.ref_time(),
101+
actual_messages_delivery_confirmation_tx_weight
102+
.all_lte(expected_messages_delivery_confirmation_tx_weight),
103103
"Messages delivery confirmation transaction weight {} is larger than expected weight {}",
104104
actual_messages_delivery_confirmation_tx_weight,
105105
expected_messages_delivery_confirmation_tx_weight,
@@ -108,7 +108,7 @@ pub fn ensure_weights_are_correct<W: WeightInfoExt>(
108108
// verify pay-dispatch-fee overhead for inbound messages
109109
let actual_pay_inbound_dispatch_fee_weight = W::pay_inbound_dispatch_fee_overhead();
110110
assert!(
111-
actual_pay_inbound_dispatch_fee_weight.ref_time() <= expected_pay_inbound_dispatch_fee_weight.ref_time(),
111+
actual_pay_inbound_dispatch_fee_weight.all_lte(expected_pay_inbound_dispatch_fee_weight),
112112
"Weight {} of pay-dispatch-fee overhead for inbound messages is larger than expected weight {}",
113113
actual_pay_inbound_dispatch_fee_weight,
114114
expected_pay_inbound_dispatch_fee_weight,
@@ -142,7 +142,7 @@ pub fn ensure_able_to_receive_message<W: WeightInfoExt>(
142142
max_incoming_message_dispatch_weight,
143143
);
144144
assert!(
145-
max_delivery_transaction_dispatch_weight.ref_time() <= max_extrinsic_weight.ref_time(),
145+
max_delivery_transaction_dispatch_weight.all_lte(max_extrinsic_weight),
146146
"Weight of maximal message delivery transaction + {} is larger than maximal possible transaction weight {}",
147147
max_delivery_transaction_dispatch_weight,
148148
max_extrinsic_weight,
@@ -181,7 +181,7 @@ pub fn ensure_able_to_receive_confirmation<W: WeightInfoExt>(
181181
db_weight,
182182
);
183183
assert!(
184-
max_confirmation_transaction_dispatch_weight.ref_time() <= max_extrinsic_weight.ref_time(),
184+
max_confirmation_transaction_dispatch_weight.all_lte(max_extrinsic_weight),
185185
"Weight of maximal confirmation transaction {} is larger than maximal possible transaction weight {}",
186186
max_confirmation_transaction_dispatch_weight,
187187
max_extrinsic_weight,
@@ -264,15 +264,14 @@ pub trait WeightInfoExt: WeightInfo {
264264

265265
// and cost of calling `OnDeliveryConfirmed::on_messages_delivered()` for every confirmed
266266
// message
267-
let callback_overhead = relayers_state
268-
.total_messages
269-
.saturating_mul(Self::single_message_callback_overhead(db_weight).ref_time());
267+
let callback_overhead = Self::single_message_callback_overhead(db_weight)
268+
.saturating_mul(relayers_state.total_messages);
270269

271270
transaction_overhead
272271
.saturating_add(messages_overhead)
273272
.saturating_add(relayers_overhead)
274273
.saturating_add(proof_size_overhead)
275-
.saturating_add(Weight::from_ref_time(callback_overhead))
274+
.saturating_add(callback_overhead)
276275
}
277276

278277
// Functions that are used by extrinsics weights formulas.

modules/parachains/src/lib.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -701,7 +701,7 @@ mod tests {
701701
let db_weight = <TestRuntime as frame_system::Config>::DbWeight::get();
702702
WeightInfoOf::<TestRuntime, ()>::submit_parachain_heads_weight(db_weight, proof, 1)
703703
.saturating_sub(if prune_expected {
704-
Weight::from_ref_time(0)
704+
Weight::zero()
705705
} else {
706706
WeightInfoOf::<TestRuntime, ()>::parachain_head_pruning_weight(db_weight)
707707
})

primitives/messages/src/source_chain.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -165,7 +165,7 @@ pub trait OnDeliveryConfirmed {
165165
#[impl_trait_for_tuples::impl_for_tuples(30)]
166166
impl OnDeliveryConfirmed for Tuple {
167167
fn on_messages_delivered(lane: &LaneId, messages: &DeliveredMessages) -> Weight {
168-
let mut total_weight = Weight::from_ref_time(0);
168+
let mut total_weight = Weight::zero();
169169
for_tuples!(
170170
#(
171171
total_weight = total_weight.saturating_add(Tuple::on_messages_delivered(lane, messages));
@@ -182,7 +182,7 @@ pub trait OnMessageAccepted {
182182
}
183183
impl OnMessageAccepted for () {
184184
fn on_messages_accepted(_lane: &LaneId, _message: &MessageNonce) -> Weight {
185-
Weight::from_ref_time(0)
185+
Weight::zero()
186186
}
187187
}
188188

@@ -226,7 +226,7 @@ impl<SenderOrigin, Balance, Payload> MessagesBridge<SenderOrigin, Balance, Paylo
226226
_message: Payload,
227227
_delivery_and_dispatch_fee: Balance,
228228
) -> Result<SendMessageArtifacts, Self::Error> {
229-
Ok(SendMessageArtifacts { nonce: 0, weight: Weight::from_ref_time(0) })
229+
Ok(SendMessageArtifacts { nonce: 0, weight: Weight::zero() })
230230
}
231231
}
232232

primitives/messages/src/target_chain.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -178,7 +178,7 @@ impl<AccountId, Fee> MessageDispatch<AccountId, Fee> for ForbidInboundMessages {
178178
) -> MessageDispatchResult {
179179
MessageDispatchResult {
180180
dispatch_result: false,
181-
unspent_weight: Weight::from_ref_time(0),
181+
unspent_weight: Weight::zero(),
182182
dispatch_fee_paid_during_dispatch: false,
183183
}
184184
}

primitives/runtime/src/lib.rs

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,8 @@ use num_traits::{CheckedSub, One};
4343
use scale_info::TypeInfo;
4444
// paritytech
4545
use frame_support::{
46-
log, pallet_prelude::DispatchResult, PalletError, RuntimeDebug, StorageHasher, StorageValue,
46+
log, pallet_prelude::DispatchResult, weights::Weight, PalletError, RuntimeDebug, StorageHasher,
47+
StorageValue,
4748
};
4849
use frame_system::RawOrigin;
4950
use sp_core::{hash::H256, storage::StorageKey};
@@ -468,6 +469,24 @@ pub fn storage_value_key(pallet_prefix: &str, value_name: &str) -> StorageKey {
468469
StorageKey(final_key)
469470
}
470471

472+
/// All extra operations with weights that we need in bridges.
473+
pub trait WeightExtraOps {
474+
/// Checked division of individual components of two weights.
475+
///
476+
/// Divides components and returns minimal division result. Returns `None` if one
477+
/// of `other` weight components is zero.
478+
fn min_components_checked_div(&self, other: Weight) -> Option<u64>;
479+
}
480+
481+
impl WeightExtraOps for Weight {
482+
fn min_components_checked_div(&self, other: Weight) -> Option<u64> {
483+
Some(sp_std::cmp::min(
484+
self.ref_time().checked_div(other.ref_time())?,
485+
self.proof_size().checked_div(other.proof_size())?,
486+
))
487+
}
488+
}
489+
471490
#[cfg(test)]
472491
mod tests {
473492
use super::*;

runtime-common/src/messages.rs

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -638,7 +638,7 @@ pub mod target {
638638
payload.weight = Some(weight);
639639
weight
640640
},
641-
_ => Weight::from_ref_time(0),
641+
_ => Weight::zero(),
642642
}
643643
}
644644

@@ -674,17 +674,22 @@ pub mod target {
674674
location,
675675
xcm,
676676
hash,
677-
weight_limit.unwrap_or(Weight::from_ref_time(0)).ref_time(),
677+
weight_limit.unwrap_or_else(Weight::zero).ref_time(),
678678
weight_credit.ref_time(),
679679
);
680680
Ok(xcm_outcome)
681681
};
682682

683683
let xcm_outcome = do_dispatch();
684-
log::trace!(target: "runtime::bridge-dispatch", "Incoming message {:?} dispatched with result: {:?}", message_id, xcm_outcome);
684+
log::trace!(
685+
target: "runtime::bridge-dispatch",
686+
"Incoming message {:?} dispatched with result: {:?}",
687+
message_id,
688+
xcm_outcome,
689+
);
685690
MessageDispatchResult {
686691
dispatch_result: true,
687-
unspent_weight: Weight::from_ref_time(0),
692+
unspent_weight: Weight::zero(),
688693
dispatch_fee_paid_during_dispatch: false,
689694
}
690695
}

runtime-common/src/messages_benchmarking.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -93,7 +93,7 @@ where
9393
nonces_start: *params.message_nonces.start(),
9494
nonces_end: *params.message_nonces.end(),
9595
},
96-
Weight::from_ref_time(0),
96+
Weight::zero(),
9797
)
9898
}
9999

0 commit comments

Comments
 (0)