1pub use context_interface::cfg::gas::*;
4
5#[derive(Clone, Copy, Debug, Default, PartialEq, Eq, Hash)]
18#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
19pub struct Gas {
20 tracker: GasTracker,
22 memory: MemoryGas,
24}
25
26impl Gas {
27 #[inline]
31 pub const fn new(limit: u64) -> Self {
32 Self {
33 tracker: GasTracker::new(limit, limit, 0),
34 memory: MemoryGas::new(),
35 }
36 }
37
38 #[inline]
40 pub const fn tracker(&self) -> &GasTracker {
41 &self.tracker
42 }
43
44 #[inline]
46 pub const fn tracker_mut(&mut self) -> &mut GasTracker {
47 &mut self.tracker
48 }
49
50 #[inline]
61 pub const fn new_with_regular_gas_and_reservoir(limit: u64, reservoir: u64) -> Self {
62 Self {
63 tracker: GasTracker::new(limit, limit, reservoir),
64 memory: MemoryGas::new(),
65 }
66 }
67
68 #[inline]
70 pub const fn new_spent_with_reservoir(limit: u64, reservoir: u64) -> Self {
71 Self {
72 tracker: GasTracker::new(limit, 0, reservoir),
73 memory: MemoryGas::new(),
74 }
75 }
76
77 #[inline]
79 pub const fn limit(&self) -> u64 {
80 self.tracker.limit()
81 }
82
83 #[inline]
85 pub const fn memory(&self) -> &MemoryGas {
86 &self.memory
87 }
88
89 #[inline]
91 pub const fn memory_mut(&mut self) -> &mut MemoryGas {
92 &mut self.memory
93 }
94
95 #[inline]
97 pub const fn refunded(&self) -> i64 {
98 self.tracker.refunded()
99 }
100
101 #[inline]
103 #[deprecated(
104 since = "32.0.0",
105 note = "After EIP-8037 gas is split on
106 regular and state gas, this method is no longer valid.
107 Use [`Gas::total_gas_spent`] instead"
108 )]
109 pub const fn spent(&self) -> u64 {
110 self.tracker
111 .limit()
112 .saturating_sub(self.tracker.remaining())
113 }
114
115 #[inline]
117 pub const fn total_gas_spent(&self) -> u64 {
118 self.tracker
119 .limit()
120 .saturating_sub(self.tracker.remaining())
121 }
122
123 #[inline]
125 pub const fn used(&self) -> u64 {
126 self.total_gas_spent()
127 .saturating_sub(self.refunded() as u64)
128 }
129
130 #[inline]
132 pub const fn spent_sub_refunded(&self) -> u64 {
133 self.total_gas_spent()
134 .saturating_sub(self.tracker.refunded() as u64)
135 }
136
137 #[inline]
139 pub const fn remaining(&self) -> u64 {
140 self.tracker.remaining()
141 }
142
143 #[inline]
145 pub const fn reservoir(&self) -> u64 {
146 self.tracker.reservoir()
147 }
148
149 #[inline]
151 pub const fn set_reservoir(&mut self, val: u64) {
152 self.tracker.set_reservoir(val);
153 }
154
155 #[inline]
157 pub const fn state_gas_spent(&self) -> u64 {
158 self.tracker.state_gas_spent()
159 }
160
161 #[inline]
163 pub const fn set_state_gas_spent(&mut self, val: u64) {
164 self.tracker.set_state_gas_spent(val);
165 }
166
167 #[inline]
169 pub const fn erase_cost(&mut self, returned: u64) {
170 self.tracker.erase_cost(returned);
171 }
172
173 #[inline]
180 pub const fn spend_all(&mut self) {
181 self.tracker.spend_all();
182 }
183
184 #[inline]
189 pub const fn record_refund(&mut self, refund: i64) {
190 self.tracker.record_refund(refund);
191 }
192
193 #[inline]
199 pub fn set_final_refund(&mut self, is_london: bool) {
200 let max_refund_quotient = if is_london { 5 } else { 2 };
201 let gas_used = self.total_gas_spent().saturating_sub(self.reservoir());
203 self.tracker
204 .set_refunded((self.refunded() as u64).min(gas_used / max_refund_quotient) as i64);
205 }
206
207 #[inline]
209 pub const fn set_refund(&mut self, refund: i64) {
210 self.tracker.set_refunded(refund);
211 }
212
213 #[inline]
215 pub const fn set_remaining(&mut self, remaining: u64) {
216 self.tracker.set_remaining(remaining);
217 }
218
219 #[inline]
221 pub const fn set_spent(&mut self, spent: u64) {
222 self.tracker
223 .set_remaining(self.tracker.limit().saturating_sub(spent));
224 }
225
226 #[inline]
234 #[must_use = "prefer using `gas!` instead to return an out-of-gas error on failure"]
235 #[deprecated(since = "32.0.0", note = "use record_regular_cost instead")]
236 pub const fn record_cost(&mut self, cost: u64) -> bool {
237 self.record_regular_cost(cost)
238 }
239
240 #[inline(always)]
246 #[must_use = "In case of not enough gas, the interpreter should halt with an out-of-gas error"]
247 pub const fn record_cost_unsafe(&mut self, cost: u64) -> bool {
248 let remaining = self.tracker.remaining();
249 let oog = remaining < cost;
250 self.tracker.set_remaining(remaining.wrapping_sub(cost));
251 oog
252 }
253
254 #[inline]
262 #[must_use = "In case of not enough gas, the interpreter should halt with an out-of-gas error"]
263 pub const fn record_state_cost(&mut self, cost: u64) -> bool {
264 self.tracker.record_state_cost(cost)
265 }
266
267 #[inline]
271 #[must_use = "In case of not enough gas, the interpreter should halt with an out-of-gas error"]
272 pub const fn record_regular_cost(&mut self, cost: u64) -> bool {
273 self.tracker.record_regular_cost(cost)
274 }
275}
276
277#[derive(Clone, Copy, Default, Debug, PartialEq, Eq, Hash)]
282#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
283pub struct MemoryGas {
284 pub words_num: usize,
286 pub expansion_cost: u64,
288}
289
290impl MemoryGas {
291 #[inline]
293 pub const fn new() -> Self {
294 Self {
295 words_num: 0,
296 expansion_cost: 0,
297 }
298 }
299
300 #[inline]
304 pub const fn set_words_num(
305 &mut self,
306 words_num: usize,
307 mut expansion_cost: u64,
308 ) -> Option<u64> {
309 self.words_num = words_num;
310 core::mem::swap(&mut self.expansion_cost, &mut expansion_cost);
311 self.expansion_cost.checked_sub(expansion_cost)
312 }
313}
314
315#[cfg(test)]
316mod tests {
317 use super::*;
318
319 #[test]
320 fn test_record_state_cost() {
321 let mut gas = Gas::new_with_regular_gas_and_reservoir(1000, 500);
323 assert!(gas.record_state_cost(200));
324 assert_eq!(
325 (gas.reservoir(), gas.remaining(), gas.state_gas_spent()),
326 (300, 1000, 200)
327 );
328
329 let mut gas = Gas::new_with_regular_gas_and_reservoir(1000, 500);
331 assert!(gas.record_state_cost(500));
332 assert_eq!(
333 (gas.reservoir(), gas.remaining(), gas.state_gas_spent()),
334 (0, 1000, 500)
335 );
336
337 let mut gas = Gas::new_with_regular_gas_and_reservoir(1000, 300);
339 assert!(gas.record_state_cost(500));
340 assert_eq!(
341 (gas.reservoir(), gas.remaining(), gas.state_gas_spent()),
342 (0, 800, 500)
343 );
344
345 let mut gas = Gas::new(1000);
347 assert!(gas.record_state_cost(200));
348 assert_eq!(
349 (gas.reservoir(), gas.remaining(), gas.state_gas_spent()),
350 (0, 800, 200)
351 );
352
353 let mut gas = Gas::new_with_regular_gas_and_reservoir(100, 50);
355 assert!(gas.record_state_cost(0));
356 assert_eq!(
357 (gas.reservoir(), gas.remaining(), gas.state_gas_spent()),
358 (50, 100, 0)
359 );
360
361 let mut gas = Gas::new_with_regular_gas_and_reservoir(100, 50);
363 assert!(!gas.record_state_cost(200));
364
365 let mut gas = Gas::new_with_regular_gas_and_reservoir(2000, 1000);
367 assert!(gas.record_state_cost(100));
368 assert!(gas.record_state_cost(200));
369 assert!(gas.record_state_cost(150));
370 assert_eq!(gas.state_gas_spent(), 450);
371
372 let mut gas = Gas::new_with_regular_gas_and_reservoir(500, 300);
374 assert!(gas.record_state_cost(150)); assert_eq!((gas.reservoir(), gas.remaining()), (150, 500));
376 assert!(gas.record_state_cost(200)); assert_eq!((gas.reservoir(), gas.remaining()), (0, 450));
378 assert!(gas.record_state_cost(100)); assert_eq!(
380 (gas.reservoir(), gas.remaining(), gas.state_gas_spent()),
381 (0, 350, 450)
382 );
383 }
384
385 #[test]
388 fn test_record_state_cost_oog_inflates_state_gas_spent() {
389 let mut gas = Gas::new(30);
391 assert!(!gas.record_state_cost(100));
392 assert_eq!(gas.state_gas_spent(), 0);
394
395 let mut gas = Gas::new_with_regular_gas_and_reservoir(30, 20);
398 assert!(!gas.record_state_cost(100));
399 assert_eq!(gas.state_gas_spent(), 0);
401 assert_eq!(gas.reservoir(), 20);
402 }
403
404 #[test]
406 fn test_record_state_cost_zero_remaining_with_reservoir() {
407 let mut gas = Gas::new_with_regular_gas_and_reservoir(0, 500);
409 assert!(gas.record_state_cost(200));
410 assert_eq!(
411 (gas.reservoir(), gas.remaining(), gas.state_gas_spent()),
412 (300, 0, 200)
413 );
414
415 assert!(gas.record_state_cost(300));
417 assert_eq!(
418 (gas.reservoir(), gas.remaining(), gas.state_gas_spent()),
419 (0, 0, 500)
420 );
421
422 assert!(!gas.record_state_cost(1));
424 }
425}