Skip to main content

revm_context_interface/cfg/
gas.rs

1//! Gas constants and functions for gas calculation.
2
3use crate::{cfg::GasParams, transaction::AccessListItemTr as _, Transaction, TransactionType};
4use primitives::hardfork::SpecId;
5
6/// Tracker for gas during execution.
7///
8/// This is used to track the gas during execution.
9#[derive(Clone, Copy, Debug, Default, PartialEq, Eq, Hash)]
10#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
11pub struct GasTracker {
12    /// Gas Limit,
13    gas_limit: u64,
14    /// Regular gas remaining (`gas_left`). Reservoir is tracked separately.
15    remaining: u64,
16    /// State gas reservoir (gas exceeding TX_MAX_GAS_LIMIT). Starts as `execution_gas - min(execution_gas, regular_gas_budget)`.
17    /// When 0, all remaining gas is regular gas with hard cap at `TX_MAX_GAS_LIMIT`.
18    reservoir: u64,
19    /// Net state gas spent so far.
20    ///
21    /// Can be negative within a call frame when 0→x→0 storage restoration refills
22    /// more state gas than the frame itself has charged (the parent previously
23    /// charged the 0→x portion). The net is reconciled on frame return.
24    state_gas_spent: i64,
25    /// Cumulative reservoir refill amount from 0→x→0 storage restorations
26    /// performed by this frame (EIP-8037 issue #2). Tracked so that on
27    /// revert/halt the parent can subtract this inflation when propagating
28    /// the child's reservoir, without confusing it with legitimate reservoir
29    /// growth from grandchild halt/revert refunds.
30    refill_amount: u64,
31    /// Refunded gas. Used to refund the gas to the caller at the end of execution.
32    refunded: i64,
33}
34
35impl GasTracker {
36    /// Creates a new `GasTracker` with the given remaining gas and reservoir.
37    #[inline]
38    pub const fn new(gas_limit: u64, remaining: u64, reservoir: u64) -> Self {
39        Self {
40            gas_limit,
41            remaining,
42            reservoir,
43            state_gas_spent: 0,
44            refill_amount: 0,
45            refunded: 0,
46        }
47    }
48
49    /// Creates a new `GasTracker` with the given used gas and reservoir.
50    #[inline]
51    pub const fn new_used_gas(gas_limit: u64, used_gas: u64, reservoir: u64) -> Self {
52        Self::new(gas_limit, gas_limit - used_gas, reservoir)
53    }
54
55    /// Returns the gas limit.
56    #[inline]
57    pub const fn limit(&self) -> u64 {
58        self.gas_limit
59    }
60
61    /// Sets the gas limit.
62    #[inline]
63    pub const fn set_limit(&mut self, val: u64) {
64        self.gas_limit = val;
65    }
66
67    /// Returns the remaining gas.
68    #[inline]
69    pub const fn remaining(&self) -> u64 {
70        self.remaining
71    }
72
73    /// Sets the remaining gas.
74    #[inline]
75    pub const fn set_remaining(&mut self, val: u64) {
76        self.remaining = val;
77    }
78
79    /// Returns the reservoir gas.
80    #[inline]
81    pub const fn reservoir(&self) -> u64 {
82        self.reservoir
83    }
84
85    /// Sets the reservoir gas.
86    #[inline]
87    pub const fn set_reservoir(&mut self, val: u64) {
88        self.reservoir = val;
89    }
90
91    /// Returns the state gas spent.
92    #[inline]
93    pub const fn state_gas_spent(&self) -> i64 {
94        self.state_gas_spent
95    }
96
97    /// Sets the state gas spent.
98    #[inline]
99    pub const fn set_state_gas_spent(&mut self, val: i64) {
100        self.state_gas_spent = val;
101    }
102
103    /// Returns the refunded gas.
104    #[inline]
105    pub const fn refunded(&self) -> i64 {
106        self.refunded
107    }
108
109    /// Sets the refunded gas.
110    #[inline]
111    pub const fn set_refunded(&mut self, val: i64) {
112        self.refunded = val;
113    }
114
115    /// Records a regular gas cost.
116    ///
117    /// Deducts from `remaining`. Returns `false` if insufficient gas.
118    #[inline]
119    #[must_use = "In case of not enough gas, the interpreter should halt with an out-of-gas error"]
120    pub const fn record_regular_cost(&mut self, cost: u64) -> bool {
121        if let Some(new_remaining) = self.remaining.checked_sub(cost) {
122            self.remaining = new_remaining;
123            return true;
124        }
125        false
126    }
127
128    /// Records a state gas cost (EIP-8037 reservoir model).
129    ///
130    /// State gas charges deduct from the reservoir first. If the reservoir is exhausted,
131    /// remaining charges spill into `remaining` (requiring `remaining >= cost`).
132    /// Tracks state gas spent.
133    ///
134    /// Returns `false` if total remaining gas is insufficient.
135    #[inline]
136    #[must_use = "In case of not enough gas, the interpreter should halt with an out-of-gas error"]
137    pub const fn record_state_cost(&mut self, cost: u64) -> bool {
138        if self.reservoir >= cost {
139            self.state_gas_spent = self.state_gas_spent.saturating_add(cost as i64);
140            self.reservoir -= cost;
141            return true;
142        }
143
144        let spill = cost - self.reservoir;
145
146        let success = self.record_regular_cost(spill);
147        if success {
148            self.state_gas_spent = self.state_gas_spent.saturating_add(cost as i64);
149            self.reservoir = 0;
150        }
151        success
152    }
153
154    /// Refills the reservoir with state gas that is returned by 0→x→0 storage
155    /// restoration (EIP-8037 issue #2).
156    ///
157    /// Per the spec, when a storage slot is restored to its original zero value
158    /// within the same transaction, the state gas charged for the initial 0→x
159    /// transition is directly restored to the reservoir rather than routed
160    /// through the capped refund counter.
161    ///
162    /// `state_gas_spent` is decremented by the same amount and may become
163    /// negative if the matching 0→x charge was made by a parent frame. The
164    /// parent's total is reconciled on frame return.
165    #[inline]
166    pub const fn refill_reservoir(&mut self, amount: u64) {
167        self.reservoir = self.reservoir.saturating_add(amount);
168        self.state_gas_spent = self.state_gas_spent.saturating_sub(amount as i64);
169        self.refill_amount = self.refill_amount.saturating_add(amount);
170    }
171
172    /// Returns cumulative reservoir refill amount from 0→x→0 restorations
173    /// performed in this frame.
174    #[inline]
175    pub const fn refill_amount(&self) -> u64 {
176        self.refill_amount
177    }
178
179    /// Sets the refill amount.
180    #[inline]
181    pub const fn set_refill_amount(&mut self, val: u64) {
182        self.refill_amount = val;
183    }
184
185    /// Records a refund value.
186    #[inline]
187    pub const fn record_refund(&mut self, refund: i64) {
188        self.refunded += refund;
189    }
190
191    /// Erases a gas cost from remaining (returns gas from child frame).
192    #[inline]
193    pub const fn erase_cost(&mut self, returned: u64) {
194        self.remaining += returned;
195    }
196
197    /// Spends all remaining gas excluding the reservoir.
198    #[inline]
199    pub const fn spend_all(&mut self) {
200        self.remaining = 0;
201    }
202}
203
204/// Gas cost for operations that consume zero gas.
205pub const ZERO: u64 = 0;
206/// Base gas cost for basic operations.
207pub const BASE: u64 = 2;
208
209/// Gas cost for very low-cost operations.
210pub const VERYLOW: u64 = 3;
211/// Gas cost for DATALOADN instruction.
212pub const DATA_LOADN_GAS: u64 = 3;
213
214/// Gas cost for conditional jump instructions.
215pub const CONDITION_JUMP_GAS: u64 = 4;
216/// Gas cost for RETF instruction.
217pub const RETF_GAS: u64 = 3;
218/// Gas cost for DATALOAD instruction.
219pub const DATA_LOAD_GAS: u64 = 4;
220
221/// Gas cost for low-cost operations.
222pub const LOW: u64 = 5;
223/// Gas cost for medium-cost operations.
224pub const MID: u64 = 8;
225/// Gas cost for high-cost operations.
226pub const HIGH: u64 = 10;
227/// Gas cost for JUMPDEST instruction.
228pub const JUMPDEST: u64 = 1;
229/// Gas cost for REFUND SELFDESTRUCT instruction.
230pub const SELFDESTRUCT_REFUND: i64 = 24000;
231/// Gas cost for CREATE instruction.
232pub const CREATE: u64 = 32000;
233/// Additional gas cost when a call transfers value.
234pub const CALLVALUE: u64 = 9000;
235/// Gas cost for creating a new account.
236pub const NEWACCOUNT: u64 = 25000;
237/// Base gas cost for EXP instruction.
238pub const EXP: u64 = 10;
239/// Gas cost per word for memory operations.
240pub const MEMORY: u64 = 3;
241/// Base gas cost for LOG instructions.
242pub const LOG: u64 = 375;
243/// Gas cost per byte of data in LOG instructions.
244pub const LOGDATA: u64 = 8;
245/// Gas cost per topic in LOG instructions.
246pub const LOGTOPIC: u64 = 375;
247/// Base gas cost for KECCAK256 instruction.
248pub const KECCAK256: u64 = 30;
249/// Gas cost per word for KECCAK256 instruction.
250pub const KECCAK256WORD: u64 = 6;
251/// Gas cost per word for copy operations.
252pub const COPY: u64 = 3;
253/// Gas cost for BLOCKHASH instruction.
254pub const BLOCKHASH: u64 = 20;
255/// Gas cost per byte for code deposit during contract creation.
256pub const CODEDEPOSIT: u64 = 200;
257
258/// EIP-1884: Repricing for trie-size-dependent opcodes
259pub const ISTANBUL_SLOAD_GAS: u64 = 800;
260/// Gas cost for SSTORE when setting a storage slot from zero to non-zero.
261pub const SSTORE_SET: u64 = 20000;
262/// Gas cost for SSTORE when modifying an existing non-zero storage slot.
263pub const SSTORE_RESET: u64 = 5000;
264/// Gas refund for SSTORE when clearing a storage slot (setting to zero).
265pub const REFUND_SSTORE_CLEARS: i64 = 15000;
266
267/// The standard cost of calldata token.
268pub const STANDARD_TOKEN_COST: u64 = 4;
269/// The cost of a non-zero byte in calldata.
270pub const NON_ZERO_BYTE_DATA_COST: u64 = 68;
271/// The multiplier for a non zero byte in calldata.
272pub const NON_ZERO_BYTE_MULTIPLIER: u64 = NON_ZERO_BYTE_DATA_COST / STANDARD_TOKEN_COST;
273/// The cost of a non-zero byte in calldata adjusted by [EIP-2028](https://eips.ethereum.org/EIPS/eip-2028).
274pub const NON_ZERO_BYTE_DATA_COST_ISTANBUL: u64 = 16;
275/// The multiplier for a non zero byte in calldata adjusted by [EIP-2028](https://eips.ethereum.org/EIPS/eip-2028).
276pub const NON_ZERO_BYTE_MULTIPLIER_ISTANBUL: u64 =
277    NON_ZERO_BYTE_DATA_COST_ISTANBUL / STANDARD_TOKEN_COST;
278/// The cost floor per token as defined by [EIP-7623](https://eips.ethereum.org/EIPS/eip-7623).
279pub const TOTAL_COST_FLOOR_PER_TOKEN: u64 = 10;
280
281/// Gas cost for EOF CREATE instruction.
282pub const EOF_CREATE_GAS: u64 = 32000;
283
284// Berlin EIP-2929/EIP-2930 constants
285/// Gas cost for accessing an address in the access list (EIP-2930).
286pub const ACCESS_LIST_ADDRESS: u64 = 2400;
287/// Gas cost for accessing a storage key in the access list (EIP-2930).
288pub const ACCESS_LIST_STORAGE_KEY: u64 = 1900;
289
290/// Gas cost for SLOAD when accessing a cold storage slot (EIP-2929).
291pub const COLD_SLOAD_COST: u64 = 2100;
292/// Gas cost for accessing a cold account (EIP-2929).
293pub const COLD_ACCOUNT_ACCESS_COST: u64 = 2600;
294/// Additional gas cost for accessing a cold account.
295pub const COLD_ACCOUNT_ACCESS_COST_ADDITIONAL: u64 =
296    COLD_ACCOUNT_ACCESS_COST - WARM_STORAGE_READ_COST;
297/// Gas cost for reading from a warm storage slot (EIP-2929).
298pub const WARM_STORAGE_READ_COST: u64 = 100;
299/// Gas cost for SSTORE reset operation on a warm storage slot.
300pub const WARM_SSTORE_RESET: u64 = SSTORE_RESET - COLD_SLOAD_COST;
301
302/// EIP-3860 : Limit and meter initcode
303pub const INITCODE_WORD_COST: u64 = 2;
304
305/// Gas stipend provided to the recipient of a CALL with value transfer.
306pub const CALL_STIPEND: u64 = 2300;
307
308/// Init and floor gas from transaction
309#[derive(Clone, Copy, Debug, Default, PartialEq, Eq)]
310#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
311pub struct InitialAndFloorGas {
312    /// Regular (non-state) portion of the initial intrinsic gas.
313    ///
314    /// Under EIP-8037, this is the part constrained by `TX_MAX_GAS_LIMIT`;
315    /// state gas uses its own reservoir and is not subject to that cap.
316    pub initial_regular_gas: u64,
317    /// State gas component of the initial intrinsic gas.
318    /// Under EIP-8037, this includes:
319    /// - EIP-7702 auth list state gas (per-auth account creation + metadata costs)
320    /// - For CREATE transactions: `create_state_gas` (account creation + contract metadata)
321    /// - For CALL transactions: 0 (state gas is unpredictable at validation time)
322    pub initial_state_gas: u64,
323    /// EIP-7702 refund for existing authorities.
324    /// This is the refund given when an authorization is applied to an already existing account.
325    pub state_refund: u64,
326    /// If transaction is a Call and Prague is enabled
327    /// floor_gas is at least amount of gas that is going to be spent.
328    pub floor_gas: u64,
329}
330
331impl InitialAndFloorGas {
332    /***** Constructors *****/
333
334    /// Create a new InitialAndFloorGas instance.
335    #[inline]
336    pub const fn new(initial_regular_gas: u64, floor_gas: u64) -> Self {
337        Self {
338            initial_regular_gas,
339            initial_state_gas: 0,
340            state_refund: 0,
341            floor_gas,
342        }
343    }
344
345    /// Create a new InitialAndFloorGas instance with state gas tracking.
346    #[inline]
347    pub const fn new_with_state_gas(
348        initial_regular_gas: u64,
349        initial_state_gas: u64,
350        floor_gas: u64,
351    ) -> Self {
352        Self {
353            initial_regular_gas,
354            initial_state_gas,
355            state_refund: 0,
356            floor_gas,
357        }
358    }
359
360    /***** Simple getters *****/
361
362    /// Regular (non-state) portion of the initial intrinsic gas.
363    ///
364    /// Under EIP-8037, this is the part constrained by `TX_MAX_GAS_LIMIT`;
365    /// state gas uses its own reservoir and is not subject to that cap.
366    #[inline]
367    pub const fn initial_regular_gas(&self) -> u64 {
368        self.initial_regular_gas
369    }
370
371    /// State gas component of the initial intrinsic gas.
372    /// This is the state gas component of the initial intrinsic gas minus the EIP-7702 refund.
373    #[inline]
374    pub const fn initial_state_gas_final(&self) -> u64 {
375        self.initial_state_gas - self.state_refund
376    }
377
378    /// EIP-7623 floor gas.
379    #[inline]
380    pub const fn floor_gas(&self) -> u64 {
381        self.floor_gas
382    }
383
384    /// Total initial intrinsic gas: `initial_regular_gas + initial_state_gas`.
385    #[inline]
386    pub const fn initial_total_gas(&self) -> u64 {
387        self.initial_regular_gas + self.initial_state_gas_final()
388    }
389
390    /***** Simple setters *****/
391
392    /// Sets the `initial_regular_gas` field by mutable reference.
393    #[inline]
394    pub const fn set_initial_regular_gas(&mut self, initial_regular_gas: u64) {
395        self.initial_regular_gas = initial_regular_gas;
396    }
397
398    /// Sets the `initial_state_gas` field by mutable reference.
399    #[inline]
400    pub const fn set_initial_state_gas(&mut self, initial_state_gas: u64) {
401        self.initial_state_gas = initial_state_gas;
402    }
403
404    /// Sets the `floor_gas` field by mutable reference.
405    #[inline]
406    pub const fn set_floor_gas(&mut self, floor_gas: u64) {
407        self.floor_gas = floor_gas;
408    }
409
410    /***** Builder with_* methods *****/
411
412    /// Sets the `initial_regular_gas` field.
413    #[inline]
414    pub const fn with_initial_regular_gas(mut self, initial_regular_gas: u64) -> Self {
415        self.initial_regular_gas = initial_regular_gas;
416        self
417    }
418
419    /// Sets the `initial_state_gas` field.
420    #[inline]
421    pub const fn with_initial_state_gas(mut self, initial_state_gas: u64) -> Self {
422        self.initial_state_gas = initial_state_gas;
423        self
424    }
425
426    /// Sets the `floor_gas` field.
427    #[inline]
428    pub const fn with_floor_gas(mut self, floor_gas: u64) -> Self {
429        self.floor_gas = floor_gas;
430        self
431    }
432
433    /// Computes the regular gas budget and reservoir for the initial call frame.
434    ///
435    /// EIP-8037 reservoir model:
436    ///   execution_gas = tx.gas_limit - intrinsic_gas  (= gas_limit parameter)
437    ///   regular_gas_budget = min(execution_gas, TX_MAX_GAS_LIMIT - intrinsic_gas)
438    ///   reservoir = execution_gas - regular_gas_budget
439    ///
440    /// Initial state gas is then deducted from the reservoir (spilling into the
441    /// regular budget when the reservoir is insufficient), and the EIP-7702
442    /// refund for existing authorities is added back to the reservoir.
443    ///
444    /// On mainnet (state gas disabled), reservoir = 0 and gas_limit is unchanged.
445    ///
446    /// Returns `(gas_limit, reservoir)`.
447    pub fn initial_gas_and_reservoir(
448        &self,
449        tx_gas_limit: u64,
450        tx_gas_limit_cap: u64,
451    ) -> (u64, u64) {
452        let execution_gas = tx_gas_limit - self.initial_regular_gas();
453
454        // System calls pass InitialAndFloorGas with all zeros and should not be
455        // subject to the TX_MAX_GAS_LIMIT cap.
456        let tx_gas_limit_cap = if self.initial_total_gas() == 0 {
457            u64::MAX
458        } else {
459            tx_gas_limit_cap
460        };
461
462        let mut regular_gas_limit = core::cmp::min(tx_gas_limit, tx_gas_limit_cap)
463            .saturating_sub(self.initial_regular_gas());
464        let mut reservoir = execution_gas - regular_gas_limit;
465
466        // Deduct initial state gas from the reservoir. When the reservoir is
467        // insufficient, the deficit is charged from the regular gas budget.
468        if reservoir >= self.initial_state_gas {
469            reservoir -= self.initial_state_gas;
470        } else {
471            regular_gas_limit -= self.initial_state_gas - reservoir;
472            reservoir = 0;
473        }
474
475        // EIP-7702 state gas refund for existing authorities goes directly to
476        // the reservoir. In the Python spec, set_delegation adds this refund to
477        // state_gas_reservoir so it stays as state gas (not regular gas).
478        reservoir += self.state_refund;
479
480        (regular_gas_limit, reservoir)
481    }
482}
483
484/// Initial gas that is deducted for transaction to be included.
485/// Initial gas contains initial stipend gas, gas for access list and input data.
486///
487/// # Returns
488///
489/// - Intrinsic gas
490/// - Number of tokens in calldata
491pub fn calculate_initial_tx_gas(
492    spec_id: SpecId,
493    input: &[u8],
494    is_create: bool,
495    access_list_accounts: u64,
496    access_list_storages: u64,
497    authorization_list_num: u64,
498    cpsb: u64,
499) -> InitialAndFloorGas {
500    GasParams::new_spec(spec_id).initial_tx_gas(
501        input,
502        is_create,
503        access_list_accounts,
504        access_list_storages,
505        authorization_list_num,
506        cpsb,
507    )
508}
509
510/// Initial gas that is deducted for transaction to be included.
511/// Initial gas contains initial stipend gas, gas for access list and input data.
512///
513/// # Returns
514///
515/// - Intrinsic gas
516/// - Number of tokens in calldata
517pub fn calculate_initial_tx_gas_for_tx(
518    tx: impl Transaction,
519    spec: SpecId,
520    cpsb: u64,
521) -> InitialAndFloorGas {
522    let mut accounts = 0;
523    let mut storages = 0;
524    // legacy is only tx type that does not have access list.
525    if tx.tx_type() != TransactionType::Legacy {
526        (accounts, storages) = tx
527            .access_list()
528            .map(|al| {
529                al.fold((0, 0), |(mut num_accounts, mut num_storage_slots), item| {
530                    num_accounts += 1;
531                    num_storage_slots += item.storage_slots().count();
532
533                    (num_accounts, num_storage_slots)
534                })
535            })
536            .unwrap_or_default();
537    }
538
539    calculate_initial_tx_gas(
540        spec,
541        tx.input(),
542        tx.kind().is_create(),
543        accounts as u64,
544        storages as u64,
545        tx.authorization_list_len() as u64,
546        cpsb,
547    )
548}
549
550/// Retrieve the total number of tokens in calldata.
551#[inline]
552pub fn get_tokens_in_calldata_istanbul(input: &[u8]) -> u64 {
553    get_tokens_in_calldata(input, NON_ZERO_BYTE_MULTIPLIER_ISTANBUL)
554}
555
556/// Retrieve the total number of tokens in calldata.
557#[inline]
558pub fn get_tokens_in_calldata(input: &[u8], non_zero_data_multiplier: u64) -> u64 {
559    let zero_data_len = input.iter().filter(|v| **v == 0).count() as u64;
560    let non_zero_data_len = input.len() as u64 - zero_data_len;
561    zero_data_len + non_zero_data_len * non_zero_data_multiplier
562}