Skip to content
This repository was archived by the owner on Nov 15, 2023. It is now read-only.
Closed
Changes from 1 commit
Commits
Show all changes
31 commits
Select commit Hold shift + click to select a range
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
Prev Previous commit
Next Next commit
Add helpers
Signed-off-by: Oliver Tale-Yazdi <[email protected]>
  • Loading branch information
ggwpez committed Jan 18, 2023
commit efcf8e38840a3cb06ac0a1b78830d1f6540e5477
54 changes: 54 additions & 0 deletions primitives/weights/src/limit.rs
Original file line number Diff line number Diff line change
Expand Up @@ -122,6 +122,10 @@ impl WeightLimit {
self.ref_time.is_none()
}

pub fn is_any_zero(&self) -> bool {
self.ref_time == Some(0) || self.proof_size == Some(0)
}

/// Whether the proof is unlimited.
pub const fn is_proof_unlimited(&self) -> bool {
self.proof_size.is_none()
Expand Down Expand Up @@ -199,6 +203,23 @@ impl WeightLimit {
self.proof_size.map_or(false, |limit| limit < weight.proof_size)
}

/// Compare `self` with `other` and return `true` if all limits of `self` are greater-or-equal.
pub fn all_above_or_equal(&self, other: &Self) -> bool {
self.ref_time.map_or(true, |limit| limit >= other.ref_time.unwrap_or(u64::MAX)) &&
self.proof_size
.map_or(true, |limit| limit >= other.proof_size.unwrap_or(u64::MAX))
}

pub fn all_less_or_equal(&self, other: &Self) -> bool {
self.ref_time.map_or_else(
|| other.ref_time.is_none(),
|limit| other.ref_time.map_or(true, |other_limit| limit <= other_limit),
) && self.proof_size.map_or_else(
|| other.proof_size.is_none(),
|limit| other.proof_size.map_or(true, |other_limit| limit <= other_limit),
)
}

/// Uses the exact value for each *limited* component and `w` for each unlimited one.
///
/// # Example
Expand Down Expand Up @@ -269,6 +290,27 @@ impl WeightLimit {
pub fn check_contains(&self, weight: Weight) -> Result<(), ()> {
self.contains(weight).then(|| ()).ok_or(())
}

pub fn map_proof_limit<F>(self, f: F) -> Self
where
F: FnOnce(Option<u64>) -> Option<u64>,
{
Self { ref_time: self.ref_time, proof_size: f(self.proof_size) }
}

pub fn map_time_limit<F>(self, f: F) -> Self
where
F: FnOnce(Option<u64>) -> Option<u64>,
{
Self { ref_time: f(self.ref_time), proof_size: self.proof_size }
}

pub fn map_limits<F>(self, f: F) -> Self
where
F: Fn(Option<u64>) -> Option<u64>,
{
Self { ref_time: f(self.ref_time), proof_size: f(self.proof_size) }
}
}

impl From<Weight> for WeightLimit {
Expand Down Expand Up @@ -614,6 +656,18 @@ mod tests {
assert!(!L::from_proof_limit(10).check_contains(Weight::from_parts(0, 11)).is_ok());
}

#[test]
fn all_less_or_equal_works() {
assert!(L::UNLIMITED.all_less_or_equal(&L::UNLIMITED));
assert!(L::NOTHING.all_less_or_equal(&L::UNLIMITED));

let l = L::from_some_limits(10, 5);
let m = L::from_some_limits(5, 10);
assert!(l.all_less_or_equal(&l));
assert!(!l.all_less_or_equal(&m));
assert!(!m.all_less_or_equal(&l));
}

/// Some known-good weight pairs.
fn good_values() -> Vec<(Weight, Weight)> {
let c = vec![0, 1, 1000, u64::MAX - 1, u64::MAX];
Expand Down