Skip to main content

revm_context_interface/cfg/
gas.rs

1//! Gas constants and functions for gas calculation.
2
3use crate::{cfg::gas_params, cfg::GasParams, Transaction};
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    /// State gas drawn from regular gas (`remaining`) because the reservoir was
26    /// empty (EIP-8037's `state_gas_from_gas_left`).
27    ///
28    /// Incremented by [`Self::record_state_cost`] whenever a state-gas charge
29    /// spills out of the reservoir into regular gas. On frame rollback (revert or
30    /// halt) the spilled portion is credited back to `remaining` in last-in-
31    /// first-out order by [`Self::rollback_state_gas`]; on success it is
32    /// propagated to the parent frame so a later parent rollback can return it.
33    state_gas_spilled: u64,
34    /// Refunded gas. Used to refund the gas to the caller at the end of execution.
35    refunded: i64,
36}
37
38impl GasTracker {
39    /// Creates a new `GasTracker` with the given remaining gas and reservoir.
40    #[inline]
41    pub const fn new(gas_limit: u64, remaining: u64, reservoir: u64) -> Self {
42        Self {
43            gas_limit,
44            remaining,
45            reservoir,
46            state_gas_spent: 0,
47            state_gas_spilled: 0,
48            refunded: 0,
49        }
50    }
51
52    /// Creates a new `GasTracker` with the given used gas and reservoir.
53    /// Remaining gas saturates at zero when used gas exceeds the gas limit.
54    #[inline]
55    pub const fn new_used_gas(gas_limit: u64, used_gas: u64, reservoir: u64) -> Self {
56        Self::new(gas_limit, gas_limit.saturating_sub(used_gas), reservoir)
57    }
58
59    /// Returns the gas limit.
60    #[inline]
61    pub const fn limit(&self) -> u64 {
62        self.gas_limit
63    }
64
65    /// Sets the gas limit.
66    #[inline]
67    pub const fn set_limit(&mut self, val: u64) {
68        self.gas_limit = val;
69    }
70
71    /// Returns the remaining gas.
72    #[inline]
73    pub const fn remaining(&self) -> u64 {
74        self.remaining
75    }
76
77    /// Sets the remaining gas.
78    #[inline]
79    pub const fn set_remaining(&mut self, val: u64) {
80        self.remaining = val;
81    }
82
83    /// Returns the reservoir gas.
84    #[inline]
85    pub const fn reservoir(&self) -> u64 {
86        self.reservoir
87    }
88
89    /// Sets the reservoir gas.
90    #[inline]
91    pub const fn set_reservoir(&mut self, val: u64) {
92        self.reservoir = val;
93    }
94
95    /// Adopts a reservoir returned by a child frame and reconciles it with any
96    /// outstanding state gas spilled into regular gas.
97    ///
98    /// A successful child can refill state gas charged by an ancestor (for
99    /// example, by clearing a slot created by a sibling). Since the child does
100    /// not inherit the ancestor's [`Self::state_gas_spilled`] counter, that
101    /// refill initially lands in the child's reservoir. On return it must first
102    /// restore the parent's regular gas in last-in-first-out order; only the
103    /// excess remains in the reservoir.
104    ///
105    /// This only reconciles the funding pools. The child's signed
106    /// `state_gas_spent` has already accounted for the refill and is merged
107    /// separately by the frame handler.
108    #[inline]
109    pub const fn absorb_returned_reservoir(&mut self, reservoir: u64) {
110        let to_remaining = if reservoir < self.state_gas_spilled {
111            reservoir
112        } else {
113            self.state_gas_spilled
114        };
115        self.remaining = self.remaining.saturating_add(to_remaining);
116        self.state_gas_spilled -= to_remaining;
117        self.reservoir = reservoir - to_remaining;
118    }
119
120    /// Returns the state gas spent.
121    #[inline]
122    pub const fn state_gas_spent(&self) -> i64 {
123        self.state_gas_spent
124    }
125
126    /// Sets the state gas spent.
127    #[inline]
128    pub const fn set_state_gas_spent(&mut self, val: i64) {
129        self.state_gas_spent = val;
130    }
131
132    /// Returns the state gas drawn from regular gas (`remaining`) because the
133    /// reservoir was empty (EIP-8037's `state_gas_from_gas_left`).
134    #[inline]
135    pub const fn state_gas_spilled(&self) -> u64 {
136        self.state_gas_spilled
137    }
138
139    /// Sets the spilled state gas.
140    #[inline]
141    pub const fn set_state_gas_spilled(&mut self, val: u64) {
142        self.state_gas_spilled = val;
143    }
144
145    /// Adds `delta` to the spilled state gas, saturating.
146    ///
147    /// Used to merge a successful child frame's spilled state gas into this
148    /// (parent) frame so a later parent rollback can return it.
149    #[inline]
150    pub const fn add_state_gas_spilled(&mut self, delta: u64) {
151        self.state_gas_spilled = self.state_gas_spilled.saturating_add(delta);
152    }
153
154    /// Returns the refunded gas.
155    #[inline]
156    pub const fn refunded(&self) -> i64 {
157        self.refunded
158    }
159
160    /// Sets the refunded gas.
161    #[inline]
162    pub const fn set_refunded(&mut self, val: i64) {
163        self.refunded = val;
164    }
165
166    /// Records a regular gas cost.
167    ///
168    /// Deducts from `remaining`. Returns `false` if insufficient gas.
169    #[inline]
170    #[must_use = "In case of not enough gas, the interpreter should halt with an out-of-gas error"]
171    pub const fn record_regular_cost(&mut self, cost: u64) -> bool {
172        if let Some(new_remaining) = self.remaining.checked_sub(cost) {
173            self.remaining = new_remaining;
174            return true;
175        }
176        false
177    }
178
179    /// Records a state gas cost (EIP-8037 reservoir model).
180    ///
181    /// State gas charges deduct from the reservoir first. If the reservoir is exhausted,
182    /// remaining charges spill into `remaining` (requiring `remaining >= cost`).
183    /// Tracks state gas spent.
184    ///
185    /// Returns `false` if total remaining gas is insufficient.
186    #[inline]
187    #[must_use = "In case of not enough gas, the interpreter should halt with an out-of-gas error"]
188    pub const fn record_state_cost(&mut self, cost: u64) -> bool {
189        if self.reservoir >= cost {
190            self.state_gas_spent = self.state_gas_spent.saturating_add(cost as i64);
191            self.reservoir -= cost;
192            return true;
193        }
194
195        let spill = cost - self.reservoir;
196
197        let success = self.record_regular_cost(spill);
198        if success {
199            self.state_gas_spent = self.state_gas_spent.saturating_add(cost as i64);
200            self.state_gas_spilled = self.state_gas_spilled.saturating_add(spill);
201            self.reservoir = 0;
202        }
203        success
204    }
205
206    /// Rolls back this frame's state-gas charges on revert or exceptional halt
207    /// (EIP-8037).
208    ///
209    /// The state gas charged within the frame is refilled in last-in-first-out
210    /// order: the spilled portion is credited back to `remaining` (the pool
211    /// charged last) and the rest restores the reservoir to its frame-start
212    /// value. Concretely, `remaining` gains `state_gas_spilled` and the reservoir
213    /// becomes `reservoir + state_gas_spent - state_gas_spilled`, which is exactly
214    /// the reservoir the frame inherited. Both state-gas counters are then reset.
215    ///
216    /// On revert the resulting `remaining` (including the refilled spill) is
217    /// returned to the parent; on halt the caller additionally zeroes `remaining`
218    /// so the spilled gas is consumed while the reservoir is left untouched.
219    #[inline]
220    pub const fn rollback_state_gas(&mut self) {
221        self.reservoir = self
222            .reservoir
223            .saturating_add_signed(self.state_gas_spent)
224            .saturating_sub(self.state_gas_spilled);
225        self.remaining = self.remaining.saturating_add(self.state_gas_spilled);
226        self.state_gas_spent = 0;
227        self.state_gas_spilled = 0;
228    }
229
230    /// Refills the reservoir with state gas that is returned by 0→x→0 storage
231    /// restoration (EIP-8037 issue #2).
232    ///
233    /// Per the spec, when a storage slot is restored to its original zero value
234    /// within the same transaction, the state gas charged for the initial 0→x
235    /// transition is directly restored to the reservoir rather than routed
236    /// through the capped refund counter.
237    ///
238    /// `state_gas_spent` is decremented by the full `amount` and may become
239    /// negative if the matching 0→x charge was made by a parent frame (so this
240    /// frame's `state_gas_spilled` is zero and the whole refill lands in the
241    /// reservoir); the parent's total is reconciled on frame return.
242    ///
243    /// Because charges deduct from the reservoir first and from regular gas
244    /// (`remaining`) last, the refill credits the pool charged last first:
245    /// `remaining` is credited up to `state_gas_spilled` and any remainder tops
246    /// up the reservoir.
247    #[inline]
248    pub const fn refill_reservoir(&mut self, amount: u64) {
249        let to_remaining = if amount < self.state_gas_spilled {
250            amount
251        } else {
252            self.state_gas_spilled
253        };
254        self.remaining = self.remaining.saturating_add(to_remaining);
255        self.state_gas_spilled -= to_remaining;
256        self.reservoir = self.reservoir.saturating_add(amount - to_remaining);
257        self.state_gas_spent = self.state_gas_spent.saturating_sub(amount as i64);
258    }
259
260    /// Records a refund value.
261    #[inline]
262    pub const fn record_refund(&mut self, refund: i64) {
263        self.refunded += refund;
264    }
265
266    /// Erases a gas cost from remaining (returns gas from child frame).
267    #[inline]
268    pub const fn erase_cost(&mut self, returned: u64) {
269        self.remaining += returned;
270    }
271
272    /// Spends all remaining gas excluding the reservoir.
273    #[inline]
274    pub const fn spend_all(&mut self) {
275        self.remaining = 0;
276    }
277}
278
279#[cfg(test)]
280mod tests {
281    use super::GasTracker;
282
283    #[test]
284    fn new_used_gas_saturates_remaining_at_zero() {
285        assert_eq!(
286            GasTracker::new_used_gas(10, 9, 3),
287            GasTracker::new(10, 1, 3)
288        );
289        assert_eq!(
290            GasTracker::new_used_gas(10, 10, 3),
291            GasTracker::new(10, 0, 3)
292        );
293        assert_eq!(
294            GasTracker::new_used_gas(10, 11, 3),
295            GasTracker::new(10, 0, 3)
296        );
297    }
298
299    #[test]
300    fn returned_reservoir_restores_spilled_state_gas_first() {
301        let mut gas = GasTracker::new(1_000, 600, 0);
302        assert!(gas.record_state_cost(400));
303
304        gas.absorb_returned_reservoir(250);
305
306        assert_eq!(gas.remaining(), 450);
307        assert_eq!(gas.reservoir(), 0);
308        assert_eq!(gas.state_gas_spilled(), 150);
309        assert_eq!(gas.state_gas_spent(), 400);
310
311        gas.absorb_returned_reservoir(200);
312
313        assert_eq!(gas.remaining(), 600);
314        assert_eq!(gas.reservoir(), 50);
315        assert_eq!(gas.state_gas_spilled(), 0);
316        assert_eq!(gas.state_gas_spent(), 400);
317    }
318}
319
320/// Gas cost for operations that consume zero gas.
321pub const ZERO: u64 = 0;
322/// Base gas cost for basic operations.
323pub const BASE: u64 = 2;
324
325/// Gas cost for very low-cost operations.
326pub const VERYLOW: u64 = 3;
327/// Gas cost for DATALOADN instruction.
328pub const DATA_LOADN_GAS: u64 = 3;
329
330/// Gas cost for conditional jump instructions.
331pub const CONDITION_JUMP_GAS: u64 = 4;
332/// Gas cost for RETF instruction.
333pub const RETF_GAS: u64 = 3;
334/// Gas cost for DATALOAD instruction.
335pub const DATA_LOAD_GAS: u64 = 4;
336
337/// Gas cost for low-cost operations.
338pub const LOW: u64 = 5;
339/// Gas cost for medium-cost operations.
340pub const MID: u64 = 8;
341/// Gas cost for high-cost operations.
342pub const HIGH: u64 = 10;
343/// Gas cost for JUMPDEST instruction.
344pub const JUMPDEST: u64 = 1;
345/// Gas cost for REFUND SELFDESTRUCT instruction.
346pub const SELFDESTRUCT_REFUND: i64 = 24000;
347/// Gas cost for CREATE instruction.
348pub const CREATE: u64 = 32000;
349/// Additional gas cost when a call transfers value.
350pub const CALLVALUE: u64 = 9000;
351/// Gas cost for creating a new account.
352pub const NEWACCOUNT: u64 = 25000;
353/// Base gas cost for EXP instruction.
354pub const EXP: u64 = 10;
355/// Gas cost per word for memory operations.
356pub const MEMORY: u64 = 3;
357/// Base gas cost for LOG instructions.
358pub const LOG: u64 = 375;
359/// Gas cost per byte of data in LOG instructions.
360pub const LOGDATA: u64 = 8;
361/// Gas cost per topic in LOG instructions.
362pub const LOGTOPIC: u64 = 375;
363/// Base gas cost for KECCAK256 instruction.
364pub const KECCAK256: u64 = 30;
365/// Gas cost per word for KECCAK256 instruction.
366pub const KECCAK256WORD: u64 = 6;
367/// Gas cost per word for copy operations.
368pub const COPY: u64 = 3;
369/// Gas cost for BLOCKHASH instruction.
370pub const BLOCKHASH: u64 = 20;
371/// Gas cost per byte for code deposit during contract creation.
372pub const CODEDEPOSIT: u64 = 200;
373
374/// EIP-1884: Repricing for trie-size-dependent opcodes
375pub const ISTANBUL_SLOAD_GAS: u64 = 800;
376/// Gas cost for SSTORE when setting a storage slot from zero to non-zero.
377pub const SSTORE_SET: u64 = 20000;
378/// Gas cost for SSTORE when modifying an existing non-zero storage slot.
379pub const SSTORE_RESET: u64 = 5000;
380/// Gas refund for SSTORE when clearing a storage slot (setting to zero).
381pub const REFUND_SSTORE_CLEARS: i64 = 15000;
382
383/// The standard cost of calldata token.
384pub const STANDARD_TOKEN_COST: u64 = 4;
385/// The cost of a non-zero byte in calldata.
386pub const NON_ZERO_BYTE_DATA_COST: u64 = 68;
387/// The multiplier for a non zero byte in calldata.
388pub const NON_ZERO_BYTE_MULTIPLIER: u64 = NON_ZERO_BYTE_DATA_COST / STANDARD_TOKEN_COST;
389/// The cost of a non-zero byte in calldata adjusted by [EIP-2028](https://eips.ethereum.org/EIPS/eip-2028).
390pub const NON_ZERO_BYTE_DATA_COST_ISTANBUL: u64 = 16;
391/// The multiplier for a non zero byte in calldata adjusted by [EIP-2028](https://eips.ethereum.org/EIPS/eip-2028).
392pub const NON_ZERO_BYTE_MULTIPLIER_ISTANBUL: u64 =
393    NON_ZERO_BYTE_DATA_COST_ISTANBUL / STANDARD_TOKEN_COST;
394/// The cost floor per token as defined by [EIP-7623](https://eips.ethereum.org/EIPS/eip-7623).
395pub const TOTAL_COST_FLOOR_PER_TOKEN: u64 = 10;
396
397/// Gas cost for EOF CREATE instruction.
398pub const EOF_CREATE_GAS: u64 = 32000;
399
400// Berlin EIP-2929/EIP-2930 constants
401/// Gas cost for accessing an address in the access list (EIP-2930).
402pub const ACCESS_LIST_ADDRESS: u64 = 2400;
403/// Gas cost for accessing a storage key in the access list (EIP-2930).
404pub const ACCESS_LIST_STORAGE_KEY: u64 = 1900;
405
406/// Gas cost for SLOAD when accessing a cold storage slot (EIP-2929).
407pub const COLD_SLOAD_COST: u64 = 2100;
408/// Gas cost for accessing a cold account (EIP-2929).
409pub const COLD_ACCOUNT_ACCESS_COST: u64 = 2600;
410/// Additional gas cost for accessing a cold account.
411pub const COLD_ACCOUNT_ACCESS_COST_ADDITIONAL: u64 =
412    COLD_ACCOUNT_ACCESS_COST - WARM_STORAGE_READ_COST;
413/// Gas cost for reading from a warm storage slot (EIP-2929).
414pub const WARM_STORAGE_READ_COST: u64 = 100;
415/// Gas cost for SSTORE reset operation on a warm storage slot.
416pub const WARM_SSTORE_RESET: u64 = SSTORE_RESET - COLD_SLOAD_COST;
417
418/// EIP-3860 : Limit and meter initcode
419pub const INITCODE_WORD_COST: u64 = 2;
420
421/// Gas stipend provided to the recipient of a CALL with value transfer.
422pub const CALL_STIPEND: u64 = 2300;
423
424/// Init and floor gas from transaction
425#[derive(Clone, Copy, Debug, Default, PartialEq, Eq)]
426#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
427pub struct InitialAndFloorGas {
428    /// Regular (non-state) portion of the initial intrinsic gas.
429    ///
430    /// Under EIP-8037, this is the part constrained by `TX_MAX_GAS_LIMIT`;
431    /// state gas uses its own reservoir and is not subject to that cap.
432    pub initial_regular_gas: u64,
433    /// State gas charged at the intrinsic phase, before the first frame is
434    /// entered.
435    ///
436    /// The state-dependent charges of the EIP-2780 runtime gas phase are not
437    /// included here: they are recorded directly on the transaction-level gas.
438    pub initial_state_gas: u64,
439    /// If transaction is a Call and Prague is enabled
440    /// floor_gas is at least amount of gas that is going to be spent.
441    pub floor_gas: u64,
442}
443
444impl InitialAndFloorGas {
445    /***** Constructors *****/
446
447    /// Create a new InitialAndFloorGas instance.
448    #[inline]
449    pub const fn new(initial_regular_gas: u64, floor_gas: u64) -> Self {
450        Self {
451            initial_regular_gas,
452            initial_state_gas: 0,
453            floor_gas,
454        }
455    }
456
457    /// Create a new InitialAndFloorGas instance with state gas tracking.
458    #[inline]
459    pub const fn new_with_state_gas(
460        initial_regular_gas: u64,
461        initial_state_gas: u64,
462        floor_gas: u64,
463    ) -> Self {
464        Self {
465            initial_regular_gas,
466            initial_state_gas,
467            floor_gas,
468        }
469    }
470
471    /***** Simple getters *****/
472
473    /// Regular (non-state) portion of the initial intrinsic gas.
474    ///
475    /// Under EIP-8037, this is the part constrained by `TX_MAX_GAS_LIMIT`;
476    /// state gas uses its own reservoir and is not subject to that cap.
477    #[inline]
478    pub const fn initial_regular_gas(&self) -> u64 {
479        self.initial_regular_gas
480    }
481
482    /// State gas charged before the first frame is entered.
483    #[inline]
484    pub const fn initial_state_gas_final(&self) -> u64 {
485        self.initial_state_gas
486    }
487
488    /// EIP-7623 floor gas.
489    #[inline]
490    pub const fn floor_gas(&self) -> u64 {
491        self.floor_gas
492    }
493
494    /// Total initial intrinsic gas: `initial_regular_gas + initial_state_gas`.
495    #[inline]
496    pub const fn initial_total_gas(&self) -> u64 {
497        self.initial_regular_gas + self.initial_state_gas_final()
498    }
499
500    /***** Simple setters *****/
501
502    /// Sets the `initial_regular_gas` field by mutable reference.
503    #[inline]
504    pub const fn set_initial_regular_gas(&mut self, initial_regular_gas: u64) {
505        self.initial_regular_gas = initial_regular_gas;
506    }
507
508    /// Sets the `initial_state_gas` field by mutable reference.
509    #[inline]
510    pub const fn set_initial_state_gas(&mut self, initial_state_gas: u64) {
511        self.initial_state_gas = initial_state_gas;
512    }
513
514    /// Sets the `floor_gas` field by mutable reference.
515    #[inline]
516    pub const fn set_floor_gas(&mut self, floor_gas: u64) {
517        self.floor_gas = floor_gas;
518    }
519
520    /***** Builder with_* methods *****/
521
522    /// Sets the `initial_regular_gas` field.
523    #[inline]
524    pub const fn with_initial_regular_gas(mut self, initial_regular_gas: u64) -> Self {
525        self.initial_regular_gas = initial_regular_gas;
526        self
527    }
528
529    /// Sets the `initial_state_gas` field.
530    #[inline]
531    pub const fn with_initial_state_gas(mut self, initial_state_gas: u64) -> Self {
532        self.initial_state_gas = initial_state_gas;
533        self
534    }
535
536    /// Sets the `floor_gas` field.
537    #[inline]
538    pub const fn with_floor_gas(mut self, floor_gas: u64) -> Self {
539        self.floor_gas = floor_gas;
540        self
541    }
542
543    /// Computes the regular gas budget and reservoir for the initial call frame.
544    ///
545    /// EIP-8037 reservoir model:
546    ///   execution_gas = tx.gas_limit - intrinsic_gas  (= gas_limit parameter)
547    ///   regular_gas_budget = min(execution_gas, TX_MAX_GAS_LIMIT - intrinsic_gas)
548    ///   reservoir = execution_gas - regular_gas_budget
549    ///
550    /// Initial state gas is then deducted from the reservoir (spilling into the
551    /// regular budget when the reservoir is insufficient).
552    ///
553    /// On mainnet (state gas disabled), reservoir = 0 and gas_limit is unchanged.
554    ///
555    /// All subtractions saturate at zero: callers normally guarantee
556    /// `tx_gas_limit >= initial_total_gas` via validation, but if that invariant
557    /// is violated the result clamps to `(0, 0)` instead of underflowing.
558    ///
559    /// Returns `(gas_limit, reservoir)`.
560    pub fn initial_gas_and_reservoir(
561        &self,
562        tx_gas_limit: u64,
563        tx_gas_limit_cap: u64,
564    ) -> (u64, u64) {
565        let execution_gas = tx_gas_limit.saturating_sub(self.initial_regular_gas());
566
567        // System calls pass InitialAndFloorGas with all zeros and should not be
568        // subject to the TX_MAX_GAS_LIMIT cap.
569        let tx_gas_limit_cap = if self.initial_total_gas() == 0 {
570            u64::MAX
571        } else {
572            tx_gas_limit_cap
573        };
574
575        let mut regular_gas_limit = core::cmp::min(tx_gas_limit, tx_gas_limit_cap)
576            .saturating_sub(self.initial_regular_gas());
577        let mut reservoir = execution_gas.saturating_sub(regular_gas_limit);
578
579        // Deduct initial state gas from the reservoir. When the reservoir is
580        // insufficient, the deficit is charged from the regular gas budget.
581        if reservoir >= self.initial_state_gas {
582            reservoir -= self.initial_state_gas;
583        } else {
584            regular_gas_limit =
585                regular_gas_limit.saturating_sub(self.initial_state_gas - reservoir);
586            reservoir = 0;
587        }
588
589        (regular_gas_limit, reservoir)
590    }
591}
592
593/// Initial gas that is deducted for transaction to be included.
594/// Initial gas contains initial stipend gas, gas for access list and input data.
595///
596/// # Returns
597///
598/// - Intrinsic gas
599/// - Number of tokens in calldata
600#[allow(clippy::too_many_arguments)]
601pub fn calculate_initial_tx_gas(
602    spec_id: SpecId,
603    input: &[u8],
604    is_create: bool,
605    access_list_accounts: u64,
606    access_list_storages: u64,
607    authorization_list_num: u64,
608    eip2780: Option<gas_params::Eip2780TxInfo>,
609) -> InitialAndFloorGas {
610    GasParams::new_spec(spec_id).initial_tx_gas(
611        input,
612        is_create,
613        access_list_accounts,
614        access_list_storages,
615        authorization_list_num,
616        eip2780,
617    )
618}
619
620/// Initial gas that is deducted for transaction to be included.
621/// Initial gas contains initial stipend gas, gas for access list and input data.
622///
623/// # Returns
624///
625/// - Intrinsic gas
626/// - Number of tokens in calldata
627pub fn calculate_initial_tx_gas_for_tx(
628    tx: impl Transaction,
629    spec: SpecId,
630    eip2780: Option<gas_params::Eip2780TxInfo>,
631) -> InitialAndFloorGas {
632    GasParams::new_spec(spec).initial_tx_gas_for_tx(tx, eip2780)
633}
634
635/// Retrieve the total number of tokens in calldata.
636#[inline]
637pub fn get_tokens_in_calldata_istanbul(input: &[u8]) -> u64 {
638    get_tokens_in_calldata(input, NON_ZERO_BYTE_MULTIPLIER_ISTANBUL)
639}
640
641/// Retrieve the total number of tokens in calldata.
642#[inline]
643pub fn get_tokens_in_calldata(input: &[u8], non_zero_data_multiplier: u64) -> u64 {
644    let zero_data_len = input.iter().filter(|v| **v == 0).count() as u64;
645    let non_zero_data_len = input.len() as u64 - zero_data_len;
646    zero_data_len + non_zero_data_len * non_zero_data_multiplier
647}