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

Commit

Permalink
Browse files Browse the repository at this point in the history
  • Loading branch information
jiguantong committed Oct 27, 2022
1 parent 53c8373 commit 2a56055
Show file tree
Hide file tree
Showing 9 changed files with 60 additions and 42 deletions.
4 changes: 1 addition & 3 deletions modules/grandpa/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -118,9 +118,7 @@ pub mod pallet {
fn on_initialize(_n: T::BlockNumber) -> frame_support::weights::Weight {
<RequestCount<T, I>>::mutate(|count| *count = count.saturating_sub(1));

Weight::from_ref_time(0)
.saturating_add(T::DbWeight::get().reads(1))
.saturating_add(T::DbWeight::get().writes(1))
T::DbWeight::get().reads_writes(1, 1)
}
}

Expand Down
13 changes: 5 additions & 8 deletions modules/messages/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -357,7 +357,7 @@ pub mod pallet {
// on this lane. We can't dispatch lane messages out-of-order, so if declared
// weight is not enough, let's move to next lane
let dispatch_weight = T::MessageDispatch::dispatch_weight(&mut message);
if dispatch_weight.ref_time() > dispatch_weight_left.ref_time() {
if dispatch_weight.any_gt(dispatch_weight_left) {
log::trace!(
target: LOG_TARGET,
"Cannot dispatch any more messages on lane {:?}. Weight: declared={}, left={}",
Expand Down Expand Up @@ -396,10 +396,7 @@ pub mod pallet {
| ReceivalResult::TooManyUnconfirmedMessages => (dispatch_weight, true),
};

let unspent_weight =
sp_std::cmp::min_by(unspent_weight, dispatch_weight, |w1, w2| {
w1.ref_time().cmp(&w2.ref_time())
});
let unspent_weight = unspent_weight.min(dispatch_weight);
dispatch_weight_left -= dispatch_weight - unspent_weight;
actual_weight = actual_weight.saturating_sub(unspent_weight).saturating_sub(
// delivery call weight formula assumes that the fee is paid at
Expand All @@ -408,7 +405,7 @@ pub mod pallet {
if refund_pay_dispatch_fee {
T::WeightInfo::pay_inbound_dispatch_fee_overhead()
} else {
Weight::from_ref_time(0)
Weight::zero()
},
);
}
Expand Down Expand Up @@ -528,7 +525,7 @@ pub mod pallet {
let actual_callback_weight =
T::OnDeliveryConfirmed::on_messages_delivered(&lane_id, &confirmed_messages);
match preliminary_callback_overhead.checked_sub(&actual_callback_weight) {
Some(difference) if difference.ref_time() == 0 => (),
Some(difference) if difference.is_zero() => (),
Some(difference) => {
log::trace!(
target: LOG_TARGET,
Expand Down Expand Up @@ -846,7 +843,7 @@ fn send_message<T: Config<I>, I: 'static>(
T::WeightInfo::single_message_callback_overhead(T::DbWeight::get());
let actual_callback_weight = T::OnMessageAccepted::on_messages_accepted(&lane_id, &nonce);
match single_message_callback_overhead.checked_sub(&actual_callback_weight) {
Some(difference) if difference.ref_time() == 0 => (),
Some(difference) if difference.is_zero() => (),
Some(difference) => {
log::trace!(
target: LOG_TARGET,
Expand Down
39 changes: 19 additions & 20 deletions modules/messages/src/weights_ext.rs
Original file line number Diff line number Diff line change
Expand Up @@ -44,26 +44,26 @@ pub fn ensure_weights_are_correct<W: WeightInfoExt>(
db_weight: RuntimeDbWeight,
) {
// verify `send_message` weight components
assert_ne!(W::send_message_overhead(), Weight::from_ref_time(0));
assert_ne!(W::send_message_size_overhead(0), Weight::from_ref_time(0));
assert_ne!(W::send_message_overhead(), Weight::zero());
assert_ne!(W::send_message_size_overhead(0), Weight::zero());

// verify `receive_messages_proof` weight components
assert_ne!(W::receive_messages_proof_overhead(), Weight::from_ref_time(0));
assert_ne!(W::receive_messages_proof_messages_overhead(1), Weight::from_ref_time(0));
assert_ne!(W::receive_messages_proof_outbound_lane_state_overhead(), Weight::from_ref_time(0));
assert_ne!(W::storage_proof_size_overhead(1), Weight::from_ref_time(0));
assert_ne!(W::receive_messages_proof_overhead(), Weight::zero());
assert_ne!(W::receive_messages_proof_messages_overhead(1), Weight::zero());
assert_ne!(W::receive_messages_proof_outbound_lane_state_overhead(), Weight::zero());
assert_ne!(W::storage_proof_size_overhead(1), Weight::zero());

// verify that the hardcoded value covers `receive_messages_proof` weight
let actual_single_regular_message_delivery_tx_weight = W::receive_messages_proof_weight(
&PreComputedSize(
(EXPECTED_DEFAULT_MESSAGE_LENGTH + W::expected_extra_storage_proof_size()) as usize,
),
1,
Weight::from_ref_time(0),
Weight::zero(),
);
assert!(
actual_single_regular_message_delivery_tx_weight.ref_time()
<= expected_default_message_delivery_tx_weight.ref_time(),
actual_single_regular_message_delivery_tx_weight
.all_lte(expected_default_message_delivery_tx_weight),
"Default message delivery transaction weight {} is larger than expected weight {}",
actual_single_regular_message_delivery_tx_weight,
expected_default_message_delivery_tx_weight,
Expand All @@ -80,8 +80,8 @@ pub fn ensure_weights_are_correct<W: WeightInfoExt>(
);

// verify `receive_messages_delivery_proof` weight components
assert_ne!(W::receive_messages_delivery_proof_overhead(), Weight::from_ref_time(0));
assert_ne!(W::storage_proof_size_overhead(1), Weight::from_ref_time(0));
assert_ne!(W::receive_messages_delivery_proof_overhead(), Weight::zero());
assert_ne!(W::storage_proof_size_overhead(1), Weight::zero());

// `receive_messages_delivery_proof_messages_overhead` and
// `receive_messages_delivery_proof_relayers_overhead` may return zero if rewards are not paid
Expand All @@ -98,8 +98,8 @@ pub fn ensure_weights_are_correct<W: WeightInfoExt>(
db_weight,
);
assert!(
actual_messages_delivery_confirmation_tx_weight.ref_time()
<= expected_messages_delivery_confirmation_tx_weight.ref_time(),
actual_messages_delivery_confirmation_tx_weight
.all_lte(expected_messages_delivery_confirmation_tx_weight),
"Messages delivery confirmation transaction weight {} is larger than expected weight {}",
actual_messages_delivery_confirmation_tx_weight,
expected_messages_delivery_confirmation_tx_weight,
Expand All @@ -108,7 +108,7 @@ pub fn ensure_weights_are_correct<W: WeightInfoExt>(
// verify pay-dispatch-fee overhead for inbound messages
let actual_pay_inbound_dispatch_fee_weight = W::pay_inbound_dispatch_fee_overhead();
assert!(
actual_pay_inbound_dispatch_fee_weight.ref_time() <= expected_pay_inbound_dispatch_fee_weight.ref_time(),
actual_pay_inbound_dispatch_fee_weight.all_lte(expected_pay_inbound_dispatch_fee_weight),
"Weight {} of pay-dispatch-fee overhead for inbound messages is larger than expected weight {}",
actual_pay_inbound_dispatch_fee_weight,
expected_pay_inbound_dispatch_fee_weight,
Expand Down Expand Up @@ -142,7 +142,7 @@ pub fn ensure_able_to_receive_message<W: WeightInfoExt>(
max_incoming_message_dispatch_weight,
);
assert!(
max_delivery_transaction_dispatch_weight.ref_time() <= max_extrinsic_weight.ref_time(),
max_delivery_transaction_dispatch_weight.all_lte(max_extrinsic_weight),
"Weight of maximal message delivery transaction + {} is larger than maximal possible transaction weight {}",
max_delivery_transaction_dispatch_weight,
max_extrinsic_weight,
Expand Down Expand Up @@ -181,7 +181,7 @@ pub fn ensure_able_to_receive_confirmation<W: WeightInfoExt>(
db_weight,
);
assert!(
max_confirmation_transaction_dispatch_weight.ref_time() <= max_extrinsic_weight.ref_time(),
max_confirmation_transaction_dispatch_weight.all_lte(max_extrinsic_weight),
"Weight of maximal confirmation transaction {} is larger than maximal possible transaction weight {}",
max_confirmation_transaction_dispatch_weight,
max_extrinsic_weight,
Expand Down Expand Up @@ -264,15 +264,14 @@ pub trait WeightInfoExt: WeightInfo {

// and cost of calling `OnDeliveryConfirmed::on_messages_delivered()` for every confirmed
// message
let callback_overhead = relayers_state
.total_messages
.saturating_mul(Self::single_message_callback_overhead(db_weight).ref_time());
let callback_overhead = Self::single_message_callback_overhead(db_weight)
.saturating_mul(relayers_state.total_messages);

transaction_overhead
.saturating_add(messages_overhead)
.saturating_add(relayers_overhead)
.saturating_add(proof_size_overhead)
.saturating_add(Weight::from_ref_time(callback_overhead))
.saturating_add(callback_overhead)
}

// Functions that are used by extrinsics weights formulas.
Expand Down
2 changes: 1 addition & 1 deletion modules/parachains/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -701,7 +701,7 @@ mod tests {
let db_weight = <TestRuntime as frame_system::Config>::DbWeight::get();
WeightInfoOf::<TestRuntime, ()>::submit_parachain_heads_weight(db_weight, proof, 1)
.saturating_sub(if prune_expected {
Weight::from_ref_time(0)
Weight::zero()
} else {
WeightInfoOf::<TestRuntime, ()>::parachain_head_pruning_weight(db_weight)
})
Expand Down
6 changes: 3 additions & 3 deletions primitives/messages/src/source_chain.rs
Original file line number Diff line number Diff line change
Expand Up @@ -165,7 +165,7 @@ pub trait OnDeliveryConfirmed {
#[impl_trait_for_tuples::impl_for_tuples(30)]
impl OnDeliveryConfirmed for Tuple {
fn on_messages_delivered(lane: &LaneId, messages: &DeliveredMessages) -> Weight {
let mut total_weight = Weight::from_ref_time(0);
let mut total_weight = Weight::zero();
for_tuples!(
#(
total_weight = total_weight.saturating_add(Tuple::on_messages_delivered(lane, messages));
Expand All @@ -182,7 +182,7 @@ pub trait OnMessageAccepted {
}
impl OnMessageAccepted for () {
fn on_messages_accepted(_lane: &LaneId, _message: &MessageNonce) -> Weight {
Weight::from_ref_time(0)
Weight::zero()
}
}

Expand Down Expand Up @@ -226,7 +226,7 @@ impl<SenderOrigin, Balance, Payload> MessagesBridge<SenderOrigin, Balance, Paylo
_message: Payload,
_delivery_and_dispatch_fee: Balance,
) -> Result<SendMessageArtifacts, Self::Error> {
Ok(SendMessageArtifacts { nonce: 0, weight: Weight::from_ref_time(0) })
Ok(SendMessageArtifacts { nonce: 0, weight: Weight::zero() })
}
}

Expand Down
2 changes: 1 addition & 1 deletion primitives/messages/src/target_chain.rs
Original file line number Diff line number Diff line change
Expand Up @@ -178,7 +178,7 @@ impl<AccountId, Fee> MessageDispatch<AccountId, Fee> for ForbidInboundMessages {
) -> MessageDispatchResult {
MessageDispatchResult {
dispatch_result: false,
unspent_weight: Weight::from_ref_time(0),
unspent_weight: Weight::zero(),
dispatch_fee_paid_during_dispatch: false,
}
}
Expand Down
21 changes: 20 additions & 1 deletion primitives/runtime/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,8 @@ use num_traits::{CheckedSub, One};
use scale_info::TypeInfo;
// paritytech
use frame_support::{
log, pallet_prelude::DispatchResult, PalletError, RuntimeDebug, StorageHasher, StorageValue,
log, pallet_prelude::DispatchResult, weights::Weight, PalletError, RuntimeDebug, StorageHasher,
StorageValue,
};
use frame_system::RawOrigin;
use sp_core::{hash::H256, storage::StorageKey};
Expand Down Expand Up @@ -468,6 +469,24 @@ pub fn storage_value_key(pallet_prefix: &str, value_name: &str) -> StorageKey {
StorageKey(final_key)
}

/// All extra operations with weights that we need in bridges.
pub trait WeightExtraOps {
/// Checked division of individual components of two weights.
///
/// Divides components and returns minimal division result. Returns `None` if one
/// of `other` weight components is zero.
fn min_components_checked_div(&self, other: Weight) -> Option<u64>;
}

impl WeightExtraOps for Weight {
fn min_components_checked_div(&self, other: Weight) -> Option<u64> {
Some(sp_std::cmp::min(
self.ref_time().checked_div(other.ref_time())?,
self.proof_size().checked_div(other.proof_size())?,
))
}
}

#[cfg(test)]
mod tests {
use super::*;
Expand Down
13 changes: 9 additions & 4 deletions runtime-common/src/messages.rs
Original file line number Diff line number Diff line change
Expand Up @@ -638,7 +638,7 @@ pub mod target {
payload.weight = Some(weight);
weight
},
_ => Weight::from_ref_time(0),
_ => Weight::zero(),
}
}

Expand Down Expand Up @@ -674,17 +674,22 @@ pub mod target {
location,
xcm,
hash,
weight_limit.unwrap_or(Weight::from_ref_time(0)).ref_time(),
weight_limit.unwrap_or_else(Weight::zero).ref_time(),
weight_credit.ref_time(),
);
Ok(xcm_outcome)
};

let xcm_outcome = do_dispatch();
log::trace!(target: "runtime::bridge-dispatch", "Incoming message {:?} dispatched with result: {:?}", message_id, xcm_outcome);
log::trace!(
target: "runtime::bridge-dispatch",
"Incoming message {:?} dispatched with result: {:?}",
message_id,
xcm_outcome,
);
MessageDispatchResult {
dispatch_result: true,
unspent_weight: Weight::from_ref_time(0),
unspent_weight: Weight::zero(),
dispatch_fee_paid_during_dispatch: false,
}
}
Expand Down
2 changes: 1 addition & 1 deletion runtime-common/src/messages_benchmarking.rs
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,7 @@ where
nonces_start: *params.message_nonces.start(),
nonces_end: *params.message_nonces.end(),
},
Weight::from_ref_time(0),
Weight::zero(),
)
}

Expand Down

0 comments on commit 2a56055

Please sign in to comment.