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