1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
use super::Weight;
use sp_arithmetic::Perbill;
#[derive(Debug, Clone)]
pub struct WeightMeter {
pub consumed: Weight,
pub limit: Weight,
}
impl WeightMeter {
pub fn from_limit(limit: Weight) -> Self {
Self { consumed: Weight::zero(), limit }
}
pub fn max_limit() -> Self {
Self::from_limit(Weight::MAX)
}
pub fn remaining(&self) -> Weight {
self.limit.saturating_sub(self.consumed)
}
pub fn consumed_ratio(&self) -> Perbill {
let time = Perbill::from_rational(self.consumed.ref_time(), self.limit.ref_time());
let pov = Perbill::from_rational(self.consumed.proof_size(), self.limit.proof_size());
time.max(pov)
}
pub fn defensive_saturating_accrue(&mut self, w: Weight) {
self.consumed.saturating_accrue(w);
debug_assert!(self.consumed.all_lte(self.limit), "Weight counter overflow");
}
pub fn check_accrue(&mut self, w: Weight) -> bool {
self.consumed.checked_add(&w).map_or(false, |test| {
if test.any_gt(self.limit) {
false
} else {
self.consumed = test;
true
}
})
}
pub fn can_accrue(&self, w: Weight) -> bool {
self.consumed.checked_add(&w).map_or(false, |t| t.all_lte(self.limit))
}
}
#[cfg(test)]
mod tests {
use crate::*;
#[test]
fn weight_meter_remaining_works() {
let mut meter = WeightMeter::from_limit(Weight::from_parts(10, 20));
assert!(meter.check_accrue(Weight::from_parts(5, 0)));
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.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.consumed, Weight::from_parts(10, 20));
assert_eq!(meter.remaining(), Weight::from_parts(0, 0));
}
#[test]
fn weight_meter_can_accrue_works() {
let meter = WeightMeter::from_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)));
}
#[test]
fn weight_meter_check_accrue_works() {
let mut meter = WeightMeter::from_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)));
}
#[test]
fn weight_meter_check_and_can_accrue_works() {
let mut meter = WeightMeter::max_limit();
assert!(meter.can_accrue(Weight::from_parts(u64::MAX, 0)));
assert!(meter.check_accrue(Weight::from_parts(u64::MAX, 0)));
assert!(meter.can_accrue(Weight::from_parts(0, u64::MAX)));
assert!(meter.check_accrue(Weight::from_parts(0, u64::MAX)));
assert!(!meter.can_accrue(Weight::from_parts(0, 1)));
assert!(!meter.check_accrue(Weight::from_parts(0, 1)));
assert!(!meter.can_accrue(Weight::from_parts(1, 0)));
assert!(!meter.check_accrue(Weight::from_parts(1, 0)));
assert!(meter.can_accrue(Weight::zero()));
assert!(meter.check_accrue(Weight::zero()));
}
#[test]
fn consumed_ratio_works() {
let mut meter = WeightMeter::from_limit(Weight::from_parts(10, 20));
assert!(meter.check_accrue(Weight::from_parts(5, 0)));
assert_eq!(meter.consumed_ratio(), Perbill::from_percent(50));
assert!(meter.check_accrue(Weight::from_parts(0, 12)));
assert_eq!(meter.consumed_ratio(), Perbill::from_percent(60));
assert!(meter.check_accrue(Weight::from_parts(2, 0)));
assert_eq!(meter.consumed_ratio(), Perbill::from_percent(70));
assert!(meter.check_accrue(Weight::from_parts(0, 4)));
assert_eq!(meter.consumed_ratio(), Perbill::from_percent(80));
assert!(meter.check_accrue(Weight::from_parts(3, 0)));
assert_eq!(meter.consumed_ratio(), Perbill::from_percent(100));
assert!(meter.check_accrue(Weight::from_parts(0, 4)));
assert_eq!(meter.consumed_ratio(), Perbill::from_percent(100));
}
}