Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 7 additions & 10 deletions polkadot/xcm/pallet-xcm/src/migration.rs
Original file line number Diff line number Diff line change
Expand Up @@ -225,7 +225,6 @@ pub mod data {

// check and migrate `Queries`
let queries_to_migrate = Queries::<T>::iter().filter_map(|(id, data)| {
weight.saturating_add(T::DbWeight::get().reads(1));
match data.try_migrate(required_xcm_version) {
Ok(Some(new_data)) => Some((id, new_data)),
Ok(None) => None,
Expand All @@ -248,13 +247,12 @@ pub mod data {
"Migrating `Queries`"
);
Queries::<T>::insert(id, new_data);
weight.saturating_add(T::DbWeight::get().writes(1));
weight.saturating_accrue(T::DbWeight::get().reads_writes(1, 1));
}

// check and migrate `LockedFungibles`
let locked_fungibles_to_migrate =
LockedFungibles::<T>::iter().filter_map(|(id, data)| {
weight.saturating_add(T::DbWeight::get().reads(1));
match data.try_migrate(required_xcm_version) {
Ok(Some(new_data)) => Some((id, new_data)),
Ok(None) => None,
Expand All @@ -277,13 +275,12 @@ pub mod data {
"Migrating `LockedFungibles`"
);
LockedFungibles::<T>::insert(id, new_data);
weight.saturating_add(T::DbWeight::get().writes(1));
weight.saturating_accrue(T::DbWeight::get().reads_writes(1, 1));
}

// check and migrate `RemoteLockedFungibles` - 1. step - just data
let remote_locked_fungibles_to_migrate =
RemoteLockedFungibles::<T>::iter().filter_map(|(id, data)| {
weight.saturating_add(T::DbWeight::get().reads(1));
match data.try_migrate(required_xcm_version) {
Ok(Some(new_data)) => Some((id, new_data)),
Ok(None) => None,
Expand All @@ -309,7 +306,7 @@ pub mod data {
"Migrating `RemoteLockedFungibles` data"
);
RemoteLockedFungibles::<T>::insert(id, new_data);
weight.saturating_add(T::DbWeight::get().writes(1));
weight.saturating_accrue(T::DbWeight::get().reads_writes(1, 1));
}

// check and migrate `RemoteLockedFungibles` - 2. step - key
Expand All @@ -335,7 +332,7 @@ pub mod data {
}
});
for (old_key, new_key) in remote_locked_fungibles_keys_to_migrate {
weight.saturating_add(T::DbWeight::get().reads(1));
weight.saturating_accrue(T::DbWeight::get().reads(1));
// make sure, that we don't override accidentally other data
if RemoteLockedFungibles::<T>::get(&new_key).is_some() {
tracing::error!(
Expand Down Expand Up @@ -366,12 +363,12 @@ pub mod data {
_,
_,
>(&old_key, &new_key);
weight.saturating_add(T::DbWeight::get().writes(1));
weight.saturating_accrue(T::DbWeight::get().writes(1));
}

// check and migrate `AuthorizedAliases`
let aliases_to_migrate = AuthorizedAliases::<T>::iter().filter_map(|(id, data)| {
weight.saturating_add(T::DbWeight::get().reads(1));
weight.saturating_accrue(T::DbWeight::get().reads(1));
match (&id, data, PhantomData::<T>).try_migrate(required_xcm_version) {
Ok(Some((new_id, new_data))) => Some((id, new_id, new_data)),
Ok(None) => None,
Expand Down Expand Up @@ -399,7 +396,7 @@ pub mod data {
count = count + 1;
}
// two writes per key, one to remove old entry, one to write new entry
weight.saturating_add(T::DbWeight::get().writes(count * 2));
weight.saturating_accrue(T::DbWeight::get().writes(count * 2));
}
}
}
Expand Down
2 changes: 1 addition & 1 deletion polkadot/xcm/xcm-executor/src/tests/mock.rs
Original file line number Diff line number Diff line change
Expand Up @@ -201,7 +201,7 @@ impl WeightTrader for TestTrader {
let amount = WeightToFee::weight_to_fee(&weight);
let required: Asset = (Here, amount).into();
let unused = payment.checked_sub(required).map_err(|_| XcmError::TooExpensive)?;
self.weight_bought_so_far.saturating_add(weight);
self.weight_bought_so_far.saturating_accrue(weight);
Ok(unused)
}

Expand Down
12 changes: 12 additions & 0 deletions prdoc/pr_10686.prdoc
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
title: 'Weight: Put `must_use` above some of the functions'
doc:
- audience: Runtime Dev
description: |-
Some functions return the modified weight and the user must use this or the changes are lost. This way the compiler informs the user.
crates:
- name: pallet-xcm
bump: patch
- name: staging-xcm-executor
bump: patch
- name: sp-weights
bump: patch
20 changes: 20 additions & 0 deletions substrate/primitives/weights/src/weight_v2.rs
Original file line number Diff line number Diff line change
Expand Up @@ -119,6 +119,7 @@ impl Weight {

/// Saturating [`Weight`] addition. Computes `self + rhs`, saturating at the numeric bounds of
/// all fields instead of overflowing.
#[must_use]
pub const fn saturating_add(self, rhs: Self) -> Self {
Self {
ref_time: self.ref_time.saturating_add(rhs.ref_time),
Expand All @@ -128,6 +129,7 @@ impl Weight {

/// Saturating [`Weight`] subtraction. Computes `self - rhs`, saturating at the numeric bounds
/// of all fields instead of overflowing.
#[must_use]
pub const fn saturating_sub(self, rhs: Self) -> Self {
Self {
ref_time: self.ref_time.saturating_sub(rhs.ref_time),
Expand All @@ -137,6 +139,7 @@ impl Weight {

/// Saturating [`Weight`] scalar multiplication. Computes `self.field * scalar` for all fields,
/// saturating at the numeric bounds of all fields instead of overflowing.
#[must_use]
pub const fn saturating_mul(self, scalar: u64) -> Self {
Self {
ref_time: self.ref_time.saturating_mul(scalar),
Expand All @@ -146,6 +149,7 @@ impl Weight {

/// Saturating [`Weight`] scalar division. Computes `self.field / scalar` for all fields,
/// saturating at the numeric bounds of all fields instead of overflowing.
#[must_use]
pub const fn saturating_div(self, scalar: u64) -> Self {
Self {
ref_time: self.ref_time.saturating_div(scalar),
Expand All @@ -155,6 +159,7 @@ impl Weight {

/// Saturating [`Weight`] scalar exponentiation. Computes `self.field.pow(exp)` for all fields,
/// saturating at the numeric bounds of all fields instead of overflowing.
#[must_use]
pub const fn saturating_pow(self, exp: u32) -> Self {
Self {
ref_time: self.ref_time.saturating_pow(exp),
Expand All @@ -173,6 +178,7 @@ impl Weight {
}

/// Checked [`Weight`] addition. Computes `self + rhs`, returning `None` if overflow occurred.
#[must_use]
pub const fn checked_add(&self, rhs: &Self) -> Option<Self> {
let ref_time = match self.ref_time.checked_add(rhs.ref_time) {
Some(t) => t,
Expand All @@ -187,6 +193,7 @@ impl Weight {

/// Checked [`Weight`] subtraction. Computes `self - rhs`, returning `None` if overflow
/// occurred.
#[must_use]
pub const fn checked_sub(&self, rhs: &Self) -> Option<Self> {
let ref_time = match self.ref_time.checked_sub(rhs.ref_time) {
Some(t) => t,
Expand All @@ -201,6 +208,7 @@ impl Weight {

/// Checked [`Weight`] scalar multiplication. Computes `self.field * scalar` for each field,
/// returning `None` if overflow occurred.
#[must_use]
pub const fn checked_mul(self, scalar: u64) -> Option<Self> {
let ref_time = match self.ref_time.checked_mul(scalar) {
Some(t) => t,
Expand All @@ -215,6 +223,7 @@ impl Weight {

/// Checked [`Weight`] scalar division. Computes `self.field / scalar` for each field, returning
/// `None` if overflow occurred.
#[must_use]
pub const fn checked_div(self, scalar: u64) -> Option<Self> {
let ref_time = match self.ref_time.checked_div(scalar) {
Some(t) => t,
Expand All @@ -236,6 +245,7 @@ impl Weight {
/// one non-zero component in `other`. The division for this particular component will then
/// yield the maximum value (e.g u64::MAX). This is because we assume not every operation and
/// hence each `Weight` will necessarily use each resource.
#[must_use]
pub const fn checked_div_per_component(self, other: &Self) -> Option<u64> {
let mut all_zero = true;
let ref_time = match self.ref_time.checked_div(other.ref_time) {
Expand Down Expand Up @@ -277,61 +287,71 @@ impl Weight {
/// Constant version of Add for `ref_time` component with u64.
///
/// Is only overflow safe when evaluated at compile-time.
#[must_use]
pub const fn add_ref_time(self, scalar: u64) -> Self {
Self { ref_time: self.ref_time + scalar, proof_size: self.proof_size }
}

/// Constant version of Add for `proof_size` component with u64.
///
/// Is only overflow safe when evaluated at compile-time.
#[must_use]
pub const fn add_proof_size(self, scalar: u64) -> Self {
Self { ref_time: self.ref_time, proof_size: self.proof_size + scalar }
}

/// Constant version of Sub for `ref_time` component with u64.
///
/// Is only overflow safe when evaluated at compile-time.
#[must_use]
pub const fn sub_ref_time(self, scalar: u64) -> Self {
Self { ref_time: self.ref_time - scalar, proof_size: self.proof_size }
}

/// Constant version of Sub for `proof_size` component with u64.
///
/// Is only overflow safe when evaluated at compile-time.
#[must_use]
pub const fn sub_proof_size(self, scalar: u64) -> Self {
Self { ref_time: self.ref_time, proof_size: self.proof_size - scalar }
}

/// Saturating version of Add for `ref_time` component with u64.
#[must_use]
pub const fn saturating_add_ref_time(self, scalar: u64) -> Self {
Self { ref_time: self.ref_time.saturating_add(scalar), proof_size: self.proof_size }
}

/// Saturating version of Add for `proof_size` component with u64.
#[must_use]
pub const fn saturating_add_proof_size(self, scalar: u64) -> Self {
Self { ref_time: self.ref_time, proof_size: self.proof_size.saturating_add(scalar) }
}

/// Saturating version of Sub for `ref_time` component with u64.
#[must_use]
pub const fn saturating_sub_ref_time(self, scalar: u64) -> Self {
Self { ref_time: self.ref_time.saturating_sub(scalar), proof_size: self.proof_size }
}

/// Saturating version of Sub for `proof_size` component with u64.
#[must_use]
pub const fn saturating_sub_proof_size(self, scalar: u64) -> Self {
Self { ref_time: self.ref_time, proof_size: self.proof_size.saturating_sub(scalar) }
}

/// Constant version of Div with u64.
///
/// Is only overflow safe when evaluated at compile-time.
#[must_use]
pub const fn div(self, scalar: u64) -> Self {
Self { ref_time: self.ref_time / scalar, proof_size: self.proof_size / scalar }
}

/// Constant version of Mul with u64.
///
/// Is only overflow safe when evaluated at compile-time.
#[must_use]
pub const fn mul(self, scalar: u64) -> Self {
Self { ref_time: self.ref_time * scalar, proof_size: self.proof_size * scalar }
}
Expand Down
Loading