Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Deprecation] Remove sp_weights::OldWeight #3491

Merged
merged 7 commits into from
Feb 29, 2024
Merged
Show file tree
Hide file tree
Changes from 4 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
12 changes: 12 additions & 0 deletions prdoc/pr_3491.prdoc
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
# Schema: Polkadot SDK PRDoc Schema (prdoc) v1.0.0
# See doc at https://raw.githubusercontent.com/paritytech/polkadot-sdk/master/prdoc/schema_user.json

title: Remove Deprecated OldWeight

doc:
- audience: Runtime Dev
description: |
Removed deprecated sp_weights::OldWeight type. Use [`weight_v2::Weight`]
philoniare marked this conversation as resolved.
Show resolved Hide resolved

crates:
- name: frame-support
11 changes: 0 additions & 11 deletions substrate/frame/contracts/src/wasm/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1821,17 +1821,6 @@ mod tests {
assert!(weight_left.all_gt(actual_left), "gas_left must be greater than final");
}

/// Test that [`frame_support::weights::OldWeight`] en/decodes the same as our
/// [`crate::OldWeight`].
#[test]
fn old_weight_decode() {
#![allow(deprecated)]
let sp = frame_support::weights::OldWeight(42).encode();
let our = crate::OldWeight::decode(&mut &*sp).unwrap();

assert_eq!(our, 42);
}

const CODE_VALUE_TRANSFERRED: &str = r#"
(module
(import "seal0" "seal_value_transferred" (func $seal_value_transferred (param i32 i32)))
Expand Down
27 changes: 1 addition & 26 deletions substrate/primitives/weights/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,17 +18,14 @@
//! # Primitives for transaction weighting.

#![cfg_attr(not(feature = "std"), no_std)]
// TODO remove once `OldWeight` is gone. I dont know why this is needed, maybe by one of the macros
// of `OldWeight`.
#![allow(deprecated)]

extern crate self as sp_weights;

mod weight_meter;
mod weight_v2;

use bounded_collections::Get;
use codec::{CompactAs, Decode, Encode, MaxEncodedLen};
use codec::{Decode, Encode};
use scale_info::TypeInfo;
#[cfg(feature = "serde")]
use serde::{Deserialize, Serialize};
Expand All @@ -52,28 +49,6 @@ pub mod constants {
pub const WEIGHT_PROOF_SIZE_PER_KB: u64 = 1024;
}

/// The old weight type.
///
/// NOTE: This type exists purely for compatibility purposes! Use [`weight_v2::Weight`] in all other
/// cases.
#[derive(
Decode,
Encode,
CompactAs,
PartialEq,
Eq,
Clone,
Copy,
RuntimeDebug,
Default,
MaxEncodedLen,
TypeInfo,
)]
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
#[cfg_attr(feature = "serde", serde(transparent))]
#[deprecated(note = "Will be removed soon; use `Weight` instead.")]
pub struct OldWeight(pub u64);

/// The weight of database operations that the runtime can invoke.
///
/// NOTE: This is currently only measured in computational time, and will probably
Expand Down
81 changes: 34 additions & 47 deletions substrate/primitives/weights/src/weight_meter.rs
Original file line number Diff line number Diff line change
Expand Up @@ -118,13 +118,6 @@ impl WeightMeter {
debug_assert!(self.consumed.all_lte(self.limit), "Weight counter overflow");
}

/// Consume the given weight after checking that it can be consumed and return `true`. Otherwise
/// do nothing and return `false`.
#[deprecated(note = "Use `try_consume` instead. Will be removed after December 2023.")]
pub fn check_accrue(&mut self, w: Weight) -> bool {
self.try_consume(w).is_ok()
}

/// Consume the given weight after checking that it can be consumed.
///
/// Returns `Ok` if the weight can be consumed or otherwise an `Err`.
Expand All @@ -139,12 +132,6 @@ impl WeightMeter {
})
}

/// Check if the given weight can be consumed.
#[deprecated(note = "Use `can_consume` instead. Will be removed after December 2023.")]
pub fn can_accrue(&self, w: Weight) -> bool {
self.can_consume(w)
}

/// Check if the given weight can be consumed.
pub fn can_consume(&self, w: Weight) -> bool {
self.consumed.checked_add(&w).map_or(false, |t| t.all_lte(self.limit))
Expand All @@ -165,80 +152,80 @@ mod tests {
fn weight_meter_remaining_works() {
let mut meter = WeightMeter::with_limit(Weight::from_parts(10, 20));

assert!(meter.check_accrue(Weight::from_parts(5, 0)));
assert_eq!(meter.try_consume(Weight::from_parts(5, 0)), Ok(()));
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

use assert_ok

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

yeah, I didn't want to add an unnecessary dependency, but I'm guessing it's fine to add it?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

As dev dependency yes, as normal dependency not (since its a primitive crate).
But its fine either way.

assert_eq!(meter.consumed, Weight::from_parts(5, 0));
assert_eq!(meter.remaining(), Weight::from_parts(5, 20));

assert!(meter.check_accrue(Weight::from_parts(2, 10)));
assert_eq!(meter.try_consume(Weight::from_parts(2, 10)), Ok(()));
assert_eq!(meter.consumed, Weight::from_parts(7, 10));
assert_eq!(meter.remaining(), Weight::from_parts(3, 10));

assert!(meter.check_accrue(Weight::from_parts(3, 10)));
assert_eq!(meter.try_consume(Weight::from_parts(3, 10)), Ok(()));
assert_eq!(meter.consumed, Weight::from_parts(10, 20));
assert_eq!(meter.remaining(), Weight::from_parts(0, 0));
}

#[test]
fn weight_meter_can_accrue_works() {
fn weight_meter_can_consume_works() {
let meter = WeightMeter::with_limit(Weight::from_parts(1, 1));

assert!(meter.can_accrue(Weight::from_parts(0, 0)));
assert!(meter.can_accrue(Weight::from_parts(1, 1)));
assert!(!meter.can_accrue(Weight::from_parts(0, 2)));
assert!(!meter.can_accrue(Weight::from_parts(2, 0)));
assert!(!meter.can_accrue(Weight::from_parts(2, 2)));
assert!(meter.can_consume(Weight::from_parts(0, 0)));
assert!(meter.can_consume(Weight::from_parts(1, 1)));
assert!(!meter.can_consume(Weight::from_parts(0, 2)));
assert!(!meter.can_consume(Weight::from_parts(2, 0)));
assert!(!meter.can_consume(Weight::from_parts(2, 2)));
}

#[test]
fn weight_meter_check_accrue_works() {
fn weight_meter_try_consume_works() {
let mut meter = WeightMeter::with_limit(Weight::from_parts(2, 2));

assert!(meter.check_accrue(Weight::from_parts(0, 0)));
assert!(meter.check_accrue(Weight::from_parts(1, 1)));
assert!(!meter.check_accrue(Weight::from_parts(0, 2)));
assert!(!meter.check_accrue(Weight::from_parts(2, 0)));
assert!(!meter.check_accrue(Weight::from_parts(2, 2)));
assert!(meter.check_accrue(Weight::from_parts(0, 1)));
assert!(meter.check_accrue(Weight::from_parts(1, 0)));
assert_eq!(meter.try_consume(Weight::from_parts(0, 0)), Ok(()));
assert_eq!(meter.try_consume(Weight::from_parts(1, 1)), Ok(()));
assert_eq!(meter.try_consume(Weight::from_parts(0, 2)), Err(()));
assert_eq!(meter.try_consume(Weight::from_parts(2, 0)), Err(()));
assert_eq!(meter.try_consume(Weight::from_parts(2, 2)), Err(()));
assert_eq!(meter.try_consume(Weight::from_parts(0, 1)), Ok(()));
assert_eq!(meter.try_consume(Weight::from_parts(1, 0)), Ok(()));
}

#[test]
fn weight_meter_check_and_can_accrue_works() {
fn weight_meter_check_and_can_consume_works() {
let mut meter = WeightMeter::new();

assert!(meter.can_accrue(Weight::from_parts(u64::MAX, 0)));
assert!(meter.check_accrue(Weight::from_parts(u64::MAX, 0)));
assert!(meter.can_consume(Weight::from_parts(u64::MAX, 0)));
assert_eq!(meter.try_consume(Weight::from_parts(u64::MAX, 0)), Ok(()));

assert!(meter.can_accrue(Weight::from_parts(0, u64::MAX)));
assert!(meter.check_accrue(Weight::from_parts(0, u64::MAX)));
assert!(meter.can_consume(Weight::from_parts(0, u64::MAX)));
assert_eq!(meter.try_consume(Weight::from_parts(0, u64::MAX)), Ok(()));

assert!(!meter.can_accrue(Weight::from_parts(0, 1)));
assert!(!meter.check_accrue(Weight::from_parts(0, 1)));
assert!(!meter.can_consume(Weight::from_parts(0, 1)));
assert_eq!(meter.try_consume(Weight::from_parts(0, 1)), Err(()));

assert!(!meter.can_accrue(Weight::from_parts(1, 0)));
assert!(!meter.check_accrue(Weight::from_parts(1, 0)));
assert!(!meter.can_consume(Weight::from_parts(1, 0)));
assert_eq!(meter.try_consume(Weight::from_parts(1, 0)), Err(()));

assert!(meter.can_accrue(Weight::zero()));
assert!(meter.check_accrue(Weight::zero()));
assert!(meter.can_consume(Weight::zero()));
assert_eq!(meter.try_consume(Weight::zero()), Ok(()));
}

#[test]
fn consumed_ratio_works() {
let mut meter = WeightMeter::with_limit(Weight::from_parts(10, 20));

assert!(meter.check_accrue(Weight::from_parts(5, 0)));
assert_eq!(meter.try_consume(Weight::from_parts(5, 0)), Ok(()));
assert_eq!(meter.consumed_ratio(), Perbill::from_percent(50));
assert!(meter.check_accrue(Weight::from_parts(0, 12)));
assert_eq!(meter.try_consume(Weight::from_parts(0, 12)), Ok(()));
assert_eq!(meter.consumed_ratio(), Perbill::from_percent(60));

assert!(meter.check_accrue(Weight::from_parts(2, 0)));
assert_eq!(meter.try_consume(Weight::from_parts(2, 0)), Ok(()));
assert_eq!(meter.consumed_ratio(), Perbill::from_percent(70));
assert!(meter.check_accrue(Weight::from_parts(0, 4)));
assert_eq!(meter.try_consume(Weight::from_parts(0, 4)), Ok(()));
assert_eq!(meter.consumed_ratio(), Perbill::from_percent(80));

assert!(meter.check_accrue(Weight::from_parts(3, 0)));
assert_eq!(meter.try_consume(Weight::from_parts(3, 0)), Ok(()));
assert_eq!(meter.consumed_ratio(), Perbill::from_percent(100));
assert!(meter.check_accrue(Weight::from_parts(0, 4)));
assert_eq!(meter.try_consume(Weight::from_parts(0, 4)), Ok(()));
assert_eq!(meter.consumed_ratio(), Perbill::from_percent(100));
}

Expand Down
Loading