Skip to main content

revm_interpreter/
gas.rs

1//! EVM gas calculation utilities.
2
3pub use context_interface::cfg::gas::*;
4
5/// Represents the state of gas during execution.
6///
7/// Implements the EIP-8037 reservoir model for dual-limit gas accounting:
8/// - `remaining`: regular gas left (`gas_left`). Does NOT include `reservoir`.
9/// - `reservoir`: state gas pool (separate from `remaining`). Starts as `execution_gas - gas_left`.
10/// - `state_gas_spent`: tracks total state gas spent
11///
12/// **Regular gas charges** (`record_cost`): deduct from `remaining`, checked against `remaining`.
13/// **State gas charges** (`record_state_cost`): deduct from `reservoir` first; when exhausted, spill into `remaining`.
14/// Total gas available = `remaining` + `reservoir`.
15///
16/// On mainnet (no state gas), `reservoir = 0` so all gas is regular gas and behavior is unchanged.
17#[derive(Clone, Copy, Debug, Default, PartialEq, Eq, Hash)]
18#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
19pub struct Gas {
20    /// Tracker for gas during execution.
21    tracker: GasTracker,
22    /// Memoisation of values for memory expansion cost.
23    memory: MemoryGas,
24}
25
26impl Gas {
27    /// Creates a new `Gas` struct with the given gas limit.
28    ///
29    /// Sets `reservoir = 0` so all gas is regular gas (standard mainnet behavior).
30    #[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    /// Returns the tracker for gas during execution.
39    #[inline]
40    pub const fn tracker(&self) -> &GasTracker {
41        &self.tracker
42    }
43
44    /// Returns the mutable tracker for gas during execution.
45    #[inline]
46    pub const fn tracker_mut(&mut self) -> &mut GasTracker {
47        &mut self.tracker
48    }
49
50    /// Creates a new `Gas` struct with a regular gas budget and reservoir (EIP-8037 reservoir model).
51    ///
52    /// Following the EIP-8037 spec:
53    /// - `remaining = limit` (regular gas available, i.e. `gas_left`)
54    /// - `reservoir` = state gas pool (separate from `remaining`)
55    /// - Total gas available = `remaining + reservoir = limit + reservoir`
56    ///
57    /// # Arguments
58    /// * `limit`: regular gas budget (capped execution gas, i.e. `gas_left`)
59    /// * `reservoir`: state gas pool (execution gas exceeding the regular gas cap)
60    #[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    /// Creates a new `Gas` struct with the given gas limit, but without any gas remaining.
69    #[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    /// Returns the gas limit.
78    #[inline]
79    pub const fn limit(&self) -> u64 {
80        self.tracker.limit()
81    }
82
83    /// Returns the memory gas.
84    #[inline]
85    pub const fn memory(&self) -> &MemoryGas {
86        &self.memory
87    }
88
89    /// Returns the memory gas.
90    #[inline]
91    pub const fn memory_mut(&mut self) -> &mut MemoryGas {
92        &mut self.memory
93    }
94
95    /// Returns the total amount of gas that was refunded.
96    #[inline]
97    pub const fn refunded(&self) -> i64 {
98        self.tracker.refunded()
99    }
100
101    /// Returns the total amount of gas spent.
102    #[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    /// Returns the regular gas spent.
116    #[inline]
117    pub const fn total_gas_spent(&self) -> u64 {
118        self.tracker
119            .limit()
120            .saturating_sub(self.tracker.remaining())
121    }
122
123    /// Returns the final amount of gas used by subtracting the refund from spent gas.
124    #[inline]
125    pub const fn used(&self) -> u64 {
126        self.total_gas_spent()
127            .saturating_sub(self.refunded() as u64)
128    }
129
130    /// Returns the total amount of gas spent, minus the refunded gas.
131    #[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    /// Returns the amount of gas remaining.
138    #[inline]
139    pub const fn remaining(&self) -> u64 {
140        self.tracker.remaining()
141    }
142
143    /// Returns the state gas reservoir.
144    #[inline]
145    pub const fn reservoir(&self) -> u64 {
146        self.tracker.reservoir()
147    }
148
149    /// Sets the state gas reservoir (used when propagating from child frame).
150    #[inline]
151    pub const fn set_reservoir(&mut self, val: u64) {
152        self.tracker.set_reservoir(val);
153    }
154
155    /// Returns total state gas spent so far.
156    ///
157    /// Can be negative within a call frame when 0→x→0 storage restoration
158    /// refills more state gas than this frame charged (see
159    /// [`GasTracker::refill_reservoir`]).
160    #[inline]
161    pub const fn state_gas_spent(&self) -> i64 {
162        self.tracker.state_gas_spent()
163    }
164
165    /// Sets the total state gas spent (used when propagating from child frame).
166    #[inline]
167    pub const fn set_state_gas_spent(&mut self, val: i64) {
168        self.tracker.set_state_gas_spent(val);
169    }
170
171    /// Refills the reservoir with state gas returned by 0→x→0 storage restoration.
172    ///
173    /// See [`GasTracker::refill_reservoir`].
174    #[inline]
175    pub const fn refill_reservoir(&mut self, amount: u64) {
176        self.tracker.refill_reservoir(amount);
177    }
178
179    /// Erases a gas cost from remaining (returns gas from child frame).
180    #[inline]
181    pub const fn erase_cost(&mut self, returned: u64) {
182        self.tracker.erase_cost(returned);
183    }
184
185    /// Spends all remaining gas excluding the reservoir.
186    ///
187    /// On exceptional halt, the remaining gas must be zeroed
188    /// to prevent state operations from succeeding via remaining gas.
189    ///
190    /// Note that this does not affect the reservoir.
191    #[inline]
192    pub const fn spend_all(&mut self) {
193        self.tracker.spend_all();
194    }
195
196    /// Records a refund value.
197    ///
198    /// `refund` can be negative but `self.refunded` should always be positive
199    /// at the end of transact.
200    #[inline]
201    pub const fn record_refund(&mut self, refund: i64) {
202        self.tracker.record_refund(refund);
203    }
204
205    /// Set a refund value for final refund.
206    ///
207    /// Max refund value is limited to Nth part (depending of fork) of gas spend.
208    ///
209    /// Related to EIP-3529: Reduction in refunds
210    #[inline]
211    pub fn set_final_refund(&mut self, is_london: bool) {
212        let max_refund_quotient = if is_london { 5 } else { 2 };
213        // EIP-8037: gas_used = total_gas_spent - reservoir (reservoir is unused state gas)
214        let gas_used = self.total_gas_spent().saturating_sub(self.reservoir());
215        self.tracker
216            .set_refunded((self.refunded() as u64).min(gas_used / max_refund_quotient) as i64);
217    }
218
219    /// Set a refund value. This overrides the current refund value.
220    #[inline]
221    pub const fn set_refund(&mut self, refund: i64) {
222        self.tracker.set_refunded(refund);
223    }
224
225    /// Set a remaining value. This overrides the current remaining value.
226    #[inline]
227    pub const fn set_remaining(&mut self, remaining: u64) {
228        self.tracker.set_remaining(remaining);
229    }
230
231    /// Set a spent value. This overrides the current spent value.
232    #[inline]
233    pub const fn set_spent(&mut self, spent: u64) {
234        self.tracker
235            .set_remaining(self.tracker.limit().saturating_sub(spent));
236    }
237
238    /// Records a regular gas cost (EIP-8037 reservoir model).
239    ///
240    /// Deducts from `remaining` and checks against implicit `gas_left` budget.
241    /// Regular gas charges cannot draw from the reservoir.
242    ///
243    /// Returns `false` if the regular gas limit is exceeded.
244    /// On failure, values contain wrapped (invalid) state — callers must not read after OOG.
245    #[inline]
246    #[must_use = "prefer using `gas!` instead to return an out-of-gas error on failure"]
247    #[deprecated(since = "32.0.0", note = "use record_regular_cost instead")]
248    pub const fn record_cost(&mut self, cost: u64) -> bool {
249        self.record_regular_cost(cost)
250    }
251
252    /// Records an explicit cost without bounds checking (unsafe path).
253    ///
254    /// Returns `true` if the gas limit is exceeded. Values wrap on underflow.
255    /// Only the regular gas check is meaningful here; total remaining can underflow
256    /// without consequence if the caller handles it.
257    #[inline(always)]
258    #[must_use = "In case of not enough gas, the interpreter should halt with an out-of-gas error"]
259    pub const fn record_cost_unsafe(&mut self, cost: u64) -> bool {
260        let remaining = self.tracker.remaining();
261        let oog = remaining < cost;
262        self.tracker.set_remaining(remaining.wrapping_sub(cost));
263        oog
264    }
265
266    /// Records a state gas cost (EIP-8037 reservoir model).
267    ///
268    /// State gas charges deduct from the reservoir first. If the reservoir is exhausted,
269    /// remaining charges spill into `gas_left` (requiring total `remaining >= cost`).
270    /// Tracks state gas spent.
271    ///
272    /// Returns `false` if total remaining gas is insufficient.
273    #[inline]
274    #[must_use = "In case of not enough gas, the interpreter should halt with an out-of-gas error"]
275    pub const fn record_state_cost(&mut self, cost: u64) -> bool {
276        self.tracker.record_state_cost(cost)
277    }
278
279    /// Deducts from `remaining` only (used for child frame gas forwarding).
280    /// Does not affect reservoir or regular gas budget.
281    /// Used for forwarding gas to child frames.
282    #[inline]
283    #[must_use = "In case of not enough gas, the interpreter should halt with an out-of-gas error"]
284    pub const fn record_regular_cost(&mut self, cost: u64) -> bool {
285        self.tracker.record_regular_cost(cost)
286    }
287}
288
289/// Utility struct that speeds up calculation of memory expansion
290/// It contains the current memory length and its memory expansion cost.
291///
292/// It allows us to split gas accounting from memory structure.
293#[derive(Clone, Copy, Default, Debug, PartialEq, Eq, Hash)]
294#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
295pub struct MemoryGas {
296    /// Current memory length
297    pub words_num: usize,
298    /// Current memory expansion cost
299    pub expansion_cost: u64,
300}
301
302impl MemoryGas {
303    /// Creates a new `MemoryGas` instance with zero memory allocation.
304    #[inline]
305    pub const fn new() -> Self {
306        Self {
307            words_num: 0,
308            expansion_cost: 0,
309        }
310    }
311
312    /// Sets the number of words and the expansion cost.
313    ///
314    /// Returns the difference between the new and old expansion cost.
315    #[inline]
316    pub const fn set_words_num(
317        &mut self,
318        words_num: usize,
319        mut expansion_cost: u64,
320    ) -> Option<u64> {
321        self.words_num = words_num;
322        core::mem::swap(&mut self.expansion_cost, &mut expansion_cost);
323        self.expansion_cost.checked_sub(expansion_cost)
324    }
325}
326
327#[cfg(test)]
328mod tests {
329    use super::*;
330
331    #[test]
332    fn test_record_state_cost() {
333        // Test 1: Cost from reservoir only
334        let mut gas = Gas::new_with_regular_gas_and_reservoir(1000, 500);
335        assert!(gas.record_state_cost(200));
336        assert_eq!(
337            (gas.reservoir(), gas.remaining(), gas.state_gas_spent()),
338            (300, 1000, 200)
339        );
340
341        // Test 2: Exhaust reservoir exactly
342        let mut gas = Gas::new_with_regular_gas_and_reservoir(1000, 500);
343        assert!(gas.record_state_cost(500));
344        assert_eq!(
345            (gas.reservoir(), gas.remaining(), gas.state_gas_spent()),
346            (0, 1000, 500)
347        );
348
349        // Test 3: Spill to remaining (reservoir < cost)
350        let mut gas = Gas::new_with_regular_gas_and_reservoir(1000, 300);
351        assert!(gas.record_state_cost(500));
352        assert_eq!(
353            (gas.reservoir(), gas.remaining(), gas.state_gas_spent()),
354            (0, 800, 500)
355        );
356
357        // Test 4: No reservoir (mainnet standard)
358        let mut gas = Gas::new(1000);
359        assert!(gas.record_state_cost(200));
360        assert_eq!(
361            (gas.reservoir(), gas.remaining(), gas.state_gas_spent()),
362            (0, 800, 200)
363        );
364
365        // Test 5: Zero cost
366        let mut gas = Gas::new_with_regular_gas_and_reservoir(100, 50);
367        assert!(gas.record_state_cost(0));
368        assert_eq!(
369            (gas.reservoir(), gas.remaining(), gas.state_gas_spent()),
370            (50, 100, 0)
371        );
372
373        // Test 6: Out of gas (cost > remaining + reservoir)
374        let mut gas = Gas::new_with_regular_gas_and_reservoir(100, 50);
375        assert!(!gas.record_state_cost(200));
376
377        // Test 7: Multiple operations accumulate state_gas_spent
378        let mut gas = Gas::new_with_regular_gas_and_reservoir(2000, 1000);
379        assert!(gas.record_state_cost(100));
380        assert!(gas.record_state_cost(200));
381        assert!(gas.record_state_cost(150));
382        assert_eq!(gas.state_gas_spent(), 450);
383
384        // Test 8: Complex scenario exhausting reservoir then remaining
385        let mut gas = Gas::new_with_regular_gas_and_reservoir(500, 300);
386        assert!(gas.record_state_cost(150)); // 150 from reservoir
387        assert_eq!((gas.reservoir(), gas.remaining()), (150, 500));
388        assert!(gas.record_state_cost(200)); // 150 from reservoir, 50 from remaining
389        assert_eq!((gas.reservoir(), gas.remaining()), (0, 450));
390        assert!(gas.record_state_cost(100)); // 100 from remaining
391        assert_eq!(
392            (gas.reservoir(), gas.remaining(), gas.state_gas_spent()),
393            (0, 350, 450)
394        );
395    }
396
397    /// A.1: Verify state_gas_spent is incremented even after failed record_state_cost.
398    /// On OOG, state_gas_spent is NOT incremented and reservoir is unchanged.
399    #[test]
400    fn test_record_state_cost_oog_inflates_state_gas_spent() {
401        // remaining=30, reservoir=0, cost=100 → OOG
402        let mut gas = Gas::new(30);
403        assert!(!gas.record_state_cost(100));
404        // On OOG, state_gas_spent is NOT incremented (operation failed)
405        assert_eq!(gas.state_gas_spent(), 0);
406
407        // With reservoir partially covering: reservoir=20, remaining=30, cost=100
408        // spill = 100 - 20 = 80, remaining(30) < 80 → OOG
409        let mut gas = Gas::new_with_regular_gas_and_reservoir(30, 20);
410        assert!(!gas.record_state_cost(100));
411        // On OOG, state_gas_spent is NOT incremented and reservoir is unchanged
412        assert_eq!(gas.state_gas_spent(), 0);
413        assert_eq!(gas.reservoir(), 20);
414    }
415
416    /// Refill reservoir restores state gas in-place (EIP-8037 0→x→0 restoration).
417    ///
418    /// The refill decrements `state_gas_spent` and may drive it negative if the
419    /// matching charge was made by a parent frame.
420    #[test]
421    fn test_refill_reservoir() {
422        // Simple case: charge then refill within the same frame.
423        let mut gas = Gas::new_with_regular_gas_and_reservoir(1000, 500);
424        assert!(gas.record_state_cost(200));
425        assert_eq!(
426            (gas.reservoir(), gas.remaining(), gas.state_gas_spent()),
427            (300, 1000, 200)
428        );
429        gas.refill_reservoir(200);
430        assert_eq!(
431            (gas.reservoir(), gas.remaining(), gas.state_gas_spent()),
432            (500, 1000, 0)
433        );
434
435        // Child-frame case: refill without a prior charge makes state_gas_spent
436        // negative. The parent's matching +charge is reconciled on return.
437        let mut gas = Gas::new_with_regular_gas_and_reservoir(1000, 100);
438        gas.refill_reservoir(300);
439        assert_eq!(
440            (gas.reservoir(), gas.remaining(), gas.state_gas_spent()),
441            (400, 1000, -300)
442        );
443    }
444
445    /// A.3: State gas with zero regular remaining but non-zero reservoir.
446    #[test]
447    fn test_record_state_cost_zero_remaining_with_reservoir() {
448        // remaining=0, reservoir=500: state gas draws entirely from reservoir
449        let mut gas = Gas::new_with_regular_gas_and_reservoir(0, 500);
450        assert!(gas.record_state_cost(200));
451        assert_eq!(
452            (gas.reservoir(), gas.remaining(), gas.state_gas_spent()),
453            (300, 0, 200)
454        );
455
456        // Exhaust reservoir exactly
457        assert!(gas.record_state_cost(300));
458        assert_eq!(
459            (gas.reservoir(), gas.remaining(), gas.state_gas_spent()),
460            (0, 0, 500)
461        );
462
463        // Now any cost → OOG (both remaining and reservoir are 0)
464        assert!(!gas.record_state_cost(1));
465    }
466}