diff --git a/polkadot/xcm/pallet-xcm/src/migration.rs b/polkadot/xcm/pallet-xcm/src/migration.rs index 194bb1680ec4d..dec3252a9b91c 100644 --- a/polkadot/xcm/pallet-xcm/src/migration.rs +++ b/polkadot/xcm/pallet-xcm/src/migration.rs @@ -225,7 +225,6 @@ pub mod data { // check and migrate `Queries` let queries_to_migrate = Queries::::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, @@ -248,13 +247,12 @@ pub mod data { "Migrating `Queries`" ); Queries::::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::::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, @@ -277,13 +275,12 @@ pub mod data { "Migrating `LockedFungibles`" ); LockedFungibles::::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::::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, @@ -309,7 +306,7 @@ pub mod data { "Migrating `RemoteLockedFungibles` data" ); RemoteLockedFungibles::::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 @@ -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::::get(&new_key).is_some() { tracing::error!( @@ -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::::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::).try_migrate(required_xcm_version) { Ok(Some((new_id, new_data))) => Some((id, new_id, new_data)), Ok(None) => None, @@ -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)); } } } diff --git a/polkadot/xcm/xcm-executor/src/tests/mock.rs b/polkadot/xcm/xcm-executor/src/tests/mock.rs index 850629bef8c06..c8fb6dd9ebf39 100644 --- a/polkadot/xcm/xcm-executor/src/tests/mock.rs +++ b/polkadot/xcm/xcm-executor/src/tests/mock.rs @@ -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) } diff --git a/prdoc/pr_10686.prdoc b/prdoc/pr_10686.prdoc new file mode 100644 index 0000000000000..2aea0214290ee --- /dev/null +++ b/prdoc/pr_10686.prdoc @@ -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 diff --git a/substrate/primitives/weights/src/weight_v2.rs b/substrate/primitives/weights/src/weight_v2.rs index 790bb5ce41c38..f2c437b2c29f8 100644 --- a/substrate/primitives/weights/src/weight_v2.rs +++ b/substrate/primitives/weights/src/weight_v2.rs @@ -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), @@ -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), @@ -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), @@ -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), @@ -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), @@ -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 { let ref_time = match self.ref_time.checked_add(rhs.ref_time) { Some(t) => t, @@ -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 { let ref_time = match self.ref_time.checked_sub(rhs.ref_time) { Some(t) => t, @@ -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 { let ref_time = match self.ref_time.checked_mul(scalar) { Some(t) => t, @@ -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 { let ref_time = match self.ref_time.checked_div(scalar) { Some(t) => t, @@ -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 { let mut all_zero = true; let ref_time = match self.ref_time.checked_div(other.ref_time) { @@ -277,6 +287,7 @@ 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 } } @@ -284,6 +295,7 @@ impl Weight { /// 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 } } @@ -291,6 +303,7 @@ impl Weight { /// 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 } } @@ -298,26 +311,31 @@ impl Weight { /// 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) } } @@ -325,6 +343,7 @@ impl Weight { /// 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 } } @@ -332,6 +351,7 @@ impl Weight { /// 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 } }