1use codec::{Decode, DecodeWithMemTracking, Encode, MaxEncodedLen};
19use core::ops::{Add, AddAssign, Div, Mul, Sub, SubAssign};
20use sp_arithmetic::traits::{Bounded, CheckedAdd, CheckedSub, Zero};
21
22use super::*;
23
24#[derive(
25 Encode,
26 Decode,
27 DecodeWithMemTracking,
28 MaxEncodedLen,
29 TypeInfo,
30 Eq,
31 PartialEq,
32 Copy,
33 Clone,
34 Debug,
35 Default,
36)]
37#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
38#[cfg_attr(feature = "json-schema", derive(schemars::JsonSchema))]
39pub struct Weight {
40 #[codec(compact)]
41 ref_time: u64,
43 #[codec(compact)]
44 proof_size: u64,
46}
47
48impl Weight {
49 pub const fn set_ref_time(mut self, c: u64) -> Self {
51 self.ref_time = c;
52 self
53 }
54
55 pub const fn set_proof_size(mut self, c: u64) -> Self {
57 self.proof_size = c;
58 self
59 }
60
61 pub const fn ref_time(&self) -> u64 {
63 self.ref_time
64 }
65
66 pub const fn proof_size(&self) -> u64 {
68 self.proof_size
69 }
70
71 pub fn ref_time_mut(&mut self) -> &mut u64 {
73 &mut self.ref_time
74 }
75
76 pub fn proof_size_mut(&mut self) -> &mut u64 {
78 &mut self.proof_size
79 }
80
81 pub const MAX: Self = Self { ref_time: u64::MAX, proof_size: u64::MAX };
83
84 pub fn min(&self, other: Self) -> Self {
86 Self {
87 ref_time: self.ref_time.min(other.ref_time),
88 proof_size: self.proof_size.min(other.proof_size),
89 }
90 }
91
92 pub fn max(&self, other: Self) -> Self {
94 Self {
95 ref_time: self.ref_time.max(other.ref_time),
96 proof_size: self.proof_size.max(other.proof_size),
97 }
98 }
99
100 pub fn try_add(&self, other: &Self, limit: &Self) -> Option<Self> {
102 let total = self.checked_add(other)?;
103 if total.any_gt(*limit) {
104 None
105 } else {
106 Some(total)
107 }
108 }
109
110 pub const fn from_parts(ref_time: u64, proof_size: u64) -> Self {
112 Self { ref_time, proof_size }
113 }
114
115 pub const fn from_all(value: u64) -> Self {
117 Self { ref_time: value, proof_size: value }
118 }
119
120 pub const fn saturating_add(self, rhs: Self) -> Self {
123 Self {
124 ref_time: self.ref_time.saturating_add(rhs.ref_time),
125 proof_size: self.proof_size.saturating_add(rhs.proof_size),
126 }
127 }
128
129 pub const fn saturating_sub(self, rhs: Self) -> Self {
132 Self {
133 ref_time: self.ref_time.saturating_sub(rhs.ref_time),
134 proof_size: self.proof_size.saturating_sub(rhs.proof_size),
135 }
136 }
137
138 pub const fn saturating_mul(self, scalar: u64) -> Self {
141 Self {
142 ref_time: self.ref_time.saturating_mul(scalar),
143 proof_size: self.proof_size.saturating_mul(scalar),
144 }
145 }
146
147 pub const fn saturating_div(self, scalar: u64) -> Self {
150 Self {
151 ref_time: self.ref_time.saturating_div(scalar),
152 proof_size: self.proof_size.saturating_div(scalar),
153 }
154 }
155
156 pub const fn saturating_pow(self, exp: u32) -> Self {
159 Self {
160 ref_time: self.ref_time.saturating_pow(exp),
161 proof_size: self.proof_size.saturating_pow(exp),
162 }
163 }
164
165 pub fn saturating_accrue(&mut self, amount: Self) {
167 *self = self.saturating_add(amount);
168 }
169
170 pub fn saturating_reduce(&mut self, amount: Self) {
172 *self = self.saturating_sub(amount);
173 }
174
175 pub const fn checked_add(&self, rhs: &Self) -> Option<Self> {
177 let ref_time = match self.ref_time.checked_add(rhs.ref_time) {
178 Some(t) => t,
179 None => return None,
180 };
181 let proof_size = match self.proof_size.checked_add(rhs.proof_size) {
182 Some(s) => s,
183 None => return None,
184 };
185 Some(Self { ref_time, proof_size })
186 }
187
188 pub const fn checked_sub(&self, rhs: &Self) -> Option<Self> {
191 let ref_time = match self.ref_time.checked_sub(rhs.ref_time) {
192 Some(t) => t,
193 None => return None,
194 };
195 let proof_size = match self.proof_size.checked_sub(rhs.proof_size) {
196 Some(s) => s,
197 None => return None,
198 };
199 Some(Self { ref_time, proof_size })
200 }
201
202 pub const fn checked_mul(self, scalar: u64) -> Option<Self> {
205 let ref_time = match self.ref_time.checked_mul(scalar) {
206 Some(t) => t,
207 None => return None,
208 };
209 let proof_size = match self.proof_size.checked_mul(scalar) {
210 Some(s) => s,
211 None => return None,
212 };
213 Some(Self { ref_time, proof_size })
214 }
215
216 pub const fn checked_div(self, scalar: u64) -> Option<Self> {
219 let ref_time = match self.ref_time.checked_div(scalar) {
220 Some(t) => t,
221 None => return None,
222 };
223 let proof_size = match self.proof_size.checked_div(scalar) {
224 Some(s) => s,
225 None => return None,
226 };
227 Some(Self { ref_time, proof_size })
228 }
229
230 pub const fn checked_div_per_component(self, other: &Self) -> Option<u64> {
240 let mut all_zero = true;
241 let ref_time = match self.ref_time.checked_div(other.ref_time) {
242 Some(ref_time) => {
243 all_zero = false;
244 ref_time
245 },
246 None => u64::MAX,
247 };
248 let proof_size = match self.proof_size.checked_div(other.proof_size) {
249 Some(proof_size) => {
250 all_zero = false;
251 proof_size
252 },
253 None => u64::MAX,
254 };
255 if all_zero {
256 None
257 } else {
258 Some(if ref_time < proof_size { ref_time } else { proof_size })
259 }
260 }
261
262 pub fn checked_accrue(&mut self, amount: Self) -> Option<()> {
264 self.checked_add(&amount).map(|new_self| *self = new_self)
265 }
266
267 pub fn checked_reduce(&mut self, amount: Self) -> Option<()> {
269 self.checked_sub(&amount).map(|new_self| *self = new_self)
270 }
271
272 pub const fn zero() -> Self {
274 Self { ref_time: 0, proof_size: 0 }
275 }
276
277 pub const fn add_ref_time(self, scalar: u64) -> Self {
281 Self { ref_time: self.ref_time + scalar, proof_size: self.proof_size }
282 }
283
284 pub const fn add_proof_size(self, scalar: u64) -> Self {
288 Self { ref_time: self.ref_time, proof_size: self.proof_size + scalar }
289 }
290
291 pub const fn sub_ref_time(self, scalar: u64) -> Self {
295 Self { ref_time: self.ref_time - scalar, proof_size: self.proof_size }
296 }
297
298 pub const fn sub_proof_size(self, scalar: u64) -> Self {
302 Self { ref_time: self.ref_time, proof_size: self.proof_size - scalar }
303 }
304
305 pub const fn div(self, scalar: u64) -> Self {
309 Self { ref_time: self.ref_time / scalar, proof_size: self.proof_size / scalar }
310 }
311
312 pub const fn mul(self, scalar: u64) -> Self {
316 Self { ref_time: self.ref_time * scalar, proof_size: self.proof_size * scalar }
317 }
318
319 pub const fn any_gt(self, other: Self) -> bool {
322 self.ref_time > other.ref_time || self.proof_size > other.proof_size
323 }
324
325 pub const fn all_gt(self, other: Self) -> bool {
328 self.ref_time > other.ref_time && self.proof_size > other.proof_size
329 }
330
331 pub const fn any_lt(self, other: Self) -> bool {
334 self.ref_time < other.ref_time || self.proof_size < other.proof_size
335 }
336
337 pub const fn all_lt(self, other: Self) -> bool {
340 self.ref_time < other.ref_time && self.proof_size < other.proof_size
341 }
342
343 pub const fn any_gte(self, other: Self) -> bool {
346 self.ref_time >= other.ref_time || self.proof_size >= other.proof_size
347 }
348
349 pub const fn all_gte(self, other: Self) -> bool {
352 self.ref_time >= other.ref_time && self.proof_size >= other.proof_size
353 }
354
355 pub const fn any_lte(self, other: Self) -> bool {
358 self.ref_time <= other.ref_time || self.proof_size <= other.proof_size
359 }
360
361 pub const fn all_lte(self, other: Self) -> bool {
364 self.ref_time <= other.ref_time && self.proof_size <= other.proof_size
365 }
366
367 pub const fn any_eq(self, other: Self) -> bool {
370 self.ref_time == other.ref_time || self.proof_size == other.proof_size
371 }
372
373 }
375
376impl Zero for Weight {
377 fn zero() -> Self {
378 Self::zero()
379 }
380
381 fn is_zero(&self) -> bool {
382 self == &Self::zero()
383 }
384}
385
386impl Add for Weight {
387 type Output = Self;
388 fn add(self, rhs: Self) -> Self {
389 Self {
390 ref_time: self.ref_time + rhs.ref_time,
391 proof_size: self.proof_size + rhs.proof_size,
392 }
393 }
394}
395
396impl Sub for Weight {
397 type Output = Self;
398 fn sub(self, rhs: Self) -> Self {
399 Self {
400 ref_time: self.ref_time - rhs.ref_time,
401 proof_size: self.proof_size - rhs.proof_size,
402 }
403 }
404}
405
406impl<T> Mul<T> for Weight
407where
408 T: Mul<u64, Output = u64> + Copy,
409{
410 type Output = Self;
411 fn mul(self, b: T) -> Self {
412 Self { ref_time: b * self.ref_time, proof_size: b * self.proof_size }
413 }
414}
415
416#[cfg(any(test, feature = "std"))]
417impl From<u64> for Weight {
418 fn from(value: u64) -> Self {
419 Self::from_parts(value, value)
420 }
421}
422
423#[cfg(any(test, feature = "std"))]
424impl From<(u64, u64)> for Weight {
425 fn from(value: (u64, u64)) -> Self {
426 Self::from_parts(value.0, value.1)
427 }
428}
429
430macro_rules! weight_mul_per_impl {
431 ($($t:ty),* $(,)?) => {
432 $(
433 impl Mul<Weight> for $t {
434 type Output = Weight;
435 fn mul(self, b: Weight) -> Weight {
436 Weight {
437 ref_time: self * b.ref_time,
438 proof_size: self * b.proof_size,
439 }
440 }
441 }
442 )*
443 }
444}
445weight_mul_per_impl!(
446 sp_arithmetic::Percent,
447 sp_arithmetic::PerU16,
448 sp_arithmetic::Permill,
449 sp_arithmetic::Perbill,
450 sp_arithmetic::Perquintill,
451);
452
453macro_rules! weight_mul_primitive_impl {
454 ($($t:ty),* $(,)?) => {
455 $(
456 impl Mul<Weight> for $t {
457 type Output = Weight;
458 fn mul(self, b: Weight) -> Weight {
459 Weight {
460 ref_time: u64::from(self) * b.ref_time,
461 proof_size: u64::from(self) * b.proof_size,
462 }
463 }
464 }
465 )*
466 }
467}
468weight_mul_primitive_impl!(u8, u16, u32, u64);
469
470impl<T> Div<T> for Weight
471where
472 u64: Div<T, Output = u64>,
473 T: Copy,
474{
475 type Output = Self;
476 fn div(self, b: T) -> Self {
477 Self { ref_time: self.ref_time / b, proof_size: self.proof_size / b }
478 }
479}
480
481impl CheckedAdd for Weight {
482 fn checked_add(&self, rhs: &Self) -> Option<Self> {
483 self.checked_add(rhs)
484 }
485}
486
487impl CheckedSub for Weight {
488 fn checked_sub(&self, rhs: &Self) -> Option<Self> {
489 self.checked_sub(rhs)
490 }
491}
492
493impl core::fmt::Display for Weight {
494 fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
495 write!(f, "Weight(ref_time: {}, proof_size: {})", self.ref_time, self.proof_size)
496 }
497}
498
499impl Bounded for Weight {
500 fn min_value() -> Self {
501 Zero::zero()
502 }
503 fn max_value() -> Self {
504 Self::MAX
505 }
506}
507
508impl AddAssign for Weight {
509 fn add_assign(&mut self, other: Self) {
510 *self = Self {
511 ref_time: self.ref_time + other.ref_time,
512 proof_size: self.proof_size + other.proof_size,
513 };
514 }
515}
516
517impl SubAssign for Weight {
518 fn sub_assign(&mut self, other: Self) {
519 *self = Self {
520 ref_time: self.ref_time - other.ref_time,
521 proof_size: self.proof_size - other.proof_size,
522 };
523 }
524}
525
526#[cfg(test)]
527mod tests {
528 use super::*;
529
530 #[test]
531 fn is_zero_works() {
532 assert!(Weight::zero().is_zero());
533 assert!(!Weight::from_parts(1, 0).is_zero());
534 assert!(!Weight::from_parts(0, 1).is_zero());
535 assert!(!Weight::MAX.is_zero());
536 }
537
538 #[test]
539 fn from_parts_works() {
540 assert_eq!(Weight::from_parts(0, 0), Weight { ref_time: 0, proof_size: 0 });
541 assert_eq!(Weight::from_parts(5, 5), Weight { ref_time: 5, proof_size: 5 });
542 assert_eq!(
543 Weight::from_parts(u64::MAX, u64::MAX),
544 Weight { ref_time: u64::MAX, proof_size: u64::MAX }
545 );
546 }
547
548 #[test]
549 fn from_all_works() {
550 assert_eq!(Weight::from_all(0), Weight::from_parts(0, 0));
551 assert_eq!(Weight::from_all(5), Weight::from_parts(5, 5));
552 assert_eq!(Weight::from_all(u64::MAX), Weight::from_parts(u64::MAX, u64::MAX));
553 }
554
555 #[test]
556 fn from_u64_works() {
557 assert_eq!(Weight::from_all(0), 0_u64.into());
558 assert_eq!(Weight::from_all(123), 123_u64.into());
559 assert_eq!(Weight::from_all(u64::MAX), u64::MAX.into());
560 }
561
562 #[test]
563 fn from_u64_pair_works() {
564 assert_eq!(Weight::from_parts(0, 1), (0, 1).into());
565 assert_eq!(Weight::from_parts(123, 321), (123u64, 321u64).into());
566 assert_eq!(Weight::from_parts(u64::MAX, 0), (u64::MAX, 0).into());
567 }
568
569 #[test]
570 fn saturating_reduce_works() {
571 let mut weight = Weight::from_parts(10, 20);
572 weight.saturating_reduce(Weight::from_all(5));
573 assert_eq!(weight, Weight::from_parts(5, 15));
574 weight.saturating_reduce(Weight::from_all(5));
575 assert_eq!(weight, Weight::from_parts(0, 10));
576 weight.saturating_reduce(Weight::from_all(11));
577 assert!(weight.is_zero());
578 weight.saturating_reduce(Weight::from_all(u64::MAX));
579 assert!(weight.is_zero());
580 }
581
582 #[test]
583 fn checked_accrue_works() {
584 let mut weight = Weight::from_parts(10, 20);
585 assert!(weight.checked_accrue(Weight::from_all(2)).is_some());
586 assert_eq!(weight, Weight::from_parts(12, 22));
587 assert!(weight.checked_accrue(Weight::from_parts(u64::MAX, 0)).is_none());
588 assert!(weight.checked_accrue(Weight::from_parts(0, u64::MAX)).is_none());
589 assert_eq!(weight, Weight::from_parts(12, 22));
590 assert!(weight
591 .checked_accrue(Weight::from_parts(u64::MAX - 12, u64::MAX - 22))
592 .is_some());
593 assert_eq!(weight, Weight::MAX);
594 assert!(weight.checked_accrue(Weight::from_parts(1, 0)).is_none());
595 assert!(weight.checked_accrue(Weight::from_parts(0, 1)).is_none());
596 assert_eq!(weight, Weight::MAX);
597 }
598
599 #[test]
600 fn checked_reduce_works() {
601 let mut weight = Weight::from_parts(10, 20);
602 assert!(weight.checked_reduce(Weight::from_all(2)).is_some());
603 assert_eq!(weight, Weight::from_parts(8, 18));
604 assert!(weight.checked_reduce(Weight::from_parts(9, 0)).is_none());
605 assert!(weight.checked_reduce(Weight::from_parts(0, 19)).is_none());
606 assert_eq!(weight, Weight::from_parts(8, 18));
607 assert!(weight.checked_reduce(Weight::from_parts(8, 0)).is_some());
608 assert_eq!(weight, Weight::from_parts(0, 18));
609 assert!(weight.checked_reduce(Weight::from_parts(0, 18)).is_some());
610 assert!(weight.is_zero());
611 }
612
613 #[test]
614 fn checked_div_per_component_works() {
615 assert_eq!(
616 Weight::from_parts(10, 20).checked_div_per_component(&Weight::from_parts(2, 10)),
617 Some(2)
618 );
619 assert_eq!(
620 Weight::from_parts(10, 200).checked_div_per_component(&Weight::from_parts(2, 10)),
621 Some(5)
622 );
623 assert_eq!(
624 Weight::from_parts(10, 200).checked_div_per_component(&Weight::from_parts(1, 10)),
625 Some(10)
626 );
627 assert_eq!(
628 Weight::from_parts(10, 200).checked_div_per_component(&Weight::from_parts(2, 1)),
629 Some(5)
630 );
631 assert_eq!(
632 Weight::from_parts(10, 200).checked_div_per_component(&Weight::from_parts(0, 10)),
633 Some(20)
634 );
635 assert_eq!(
636 Weight::from_parts(10, 200).checked_div_per_component(&Weight::from_parts(1, 0)),
637 Some(10)
638 );
639 assert_eq!(
640 Weight::from_parts(0, 200).checked_div_per_component(&Weight::from_parts(2, 3)),
641 Some(0)
642 );
643 assert_eq!(
644 Weight::from_parts(10, 0).checked_div_per_component(&Weight::from_parts(2, 3)),
645 Some(0)
646 );
647 assert_eq!(
648 Weight::from_parts(10, 200).checked_div_per_component(&Weight::from_parts(0, 0)),
649 None,
650 );
651 assert_eq!(
652 Weight::from_parts(0, 0).checked_div_per_component(&Weight::from_parts(0, 0)),
653 None,
654 );
655 }
656}