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/// Gas cost for operations that consume zero gas.
7pub const ZERO: u64 = 0;
8/// Base gas cost for basic operations.
9pub const BASE: u64 = 2;
10
11/// Gas cost for very low-cost operations.
12pub const VERYLOW: u64 = 3;
13/// Gas cost for DATALOADN instruction.
14pub const DATA_LOADN_GAS: u64 = 3;
15
16/// Gas cost for conditional jump instructions.
17pub const CONDITION_JUMP_GAS: u64 = 4;
18/// Gas cost for RETF instruction.
19pub const RETF_GAS: u64 = 3;
20/// Gas cost for DATALOAD instruction.
21pub const DATA_LOAD_GAS: u64 = 4;
22
23/// Gas cost for low-cost operations.
24pub const LOW: u64 = 5;
25/// Gas cost for medium-cost operations.
26pub const MID: u64 = 8;
27/// Gas cost for high-cost operations.
28pub const HIGH: u64 = 10;
29/// Gas cost for JUMPDEST instruction.
30pub const JUMPDEST: u64 = 1;
31/// Gas cost for REFUND SELFDESTRUCT instruction.
32pub const SELFDESTRUCT_REFUND: i64 = 24000;
33/// Gas cost for CREATE instruction.
34pub const CREATE: u64 = 32000;
35/// Additional gas cost when a call transfers value.
36pub const CALLVALUE: u64 = 9000;
37/// Gas cost for creating a new account.
38pub const NEWACCOUNT: u64 = 25000;
39/// Base gas cost for EXP instruction.
40pub const EXP: u64 = 10;
41/// Gas cost per word for memory operations.
42pub const MEMORY: u64 = 3;
43/// Base gas cost for LOG instructions.
44pub const LOG: u64 = 375;
45/// Gas cost per byte of data in LOG instructions.
46pub const LOGDATA: u64 = 8;
47/// Gas cost per topic in LOG instructions.
48pub const LOGTOPIC: u64 = 375;
49/// Base gas cost for KECCAK256 instruction.
50pub const KECCAK256: u64 = 30;
51/// Gas cost per word for KECCAK256 instruction.
52pub const KECCAK256WORD: u64 = 6;
53/// Gas cost per word for copy operations.
54pub const COPY: u64 = 3;
55/// Gas cost for BLOCKHASH instruction.
56pub const BLOCKHASH: u64 = 20;
57/// Gas cost per byte for code deposit during contract creation.
58pub const CODEDEPOSIT: u64 = 200;
59
60/// EIP-1884: Repricing for trie-size-dependent opcodes
61pub const ISTANBUL_SLOAD_GAS: u64 = 800;
62/// Gas cost for SSTORE when setting a storage slot from zero to non-zero.
63pub const SSTORE_SET: u64 = 20000;
64/// Gas cost for SSTORE when modifying an existing non-zero storage slot.
65pub const SSTORE_RESET: u64 = 5000;
66/// Gas refund for SSTORE when clearing a storage slot (setting to zero).
67pub const REFUND_SSTORE_CLEARS: i64 = 15000;
68
69/// The standard cost of calldata token.
70pub const STANDARD_TOKEN_COST: u64 = 4;
71/// The cost of a non-zero byte in calldata.
72pub const NON_ZERO_BYTE_DATA_COST: u64 = 68;
73/// The multiplier for a non zero byte in calldata.
74pub const NON_ZERO_BYTE_MULTIPLIER: u64 = NON_ZERO_BYTE_DATA_COST / STANDARD_TOKEN_COST;
75/// The cost of a non-zero byte in calldata adjusted by [EIP-2028](https://eips.ethereum.org/EIPS/eip-2028).
76pub const NON_ZERO_BYTE_DATA_COST_ISTANBUL: u64 = 16;
77/// The multiplier for a non zero byte in calldata adjusted by [EIP-2028](https://eips.ethereum.org/EIPS/eip-2028).
78pub const NON_ZERO_BYTE_MULTIPLIER_ISTANBUL: u64 =
79    NON_ZERO_BYTE_DATA_COST_ISTANBUL / STANDARD_TOKEN_COST;
80/// The cost floor per token as defined by EIP-2028.
81pub const TOTAL_COST_FLOOR_PER_TOKEN: u64 = 10;
82
83/// Gas cost for EOF CREATE instruction.
84pub const EOF_CREATE_GAS: u64 = 32000;
85
86// Berlin EIP-2929/EIP-2930 constants
87/// Gas cost for accessing an address in the access list (EIP-2930).
88pub const ACCESS_LIST_ADDRESS: u64 = 2400;
89/// Gas cost for accessing a storage key in the access list (EIP-2930).
90pub const ACCESS_LIST_STORAGE_KEY: u64 = 1900;
91
92/// Gas cost for SLOAD when accessing a cold storage slot (EIP-2929).
93pub const COLD_SLOAD_COST: u64 = 2100;
94/// Gas cost for accessing a cold account (EIP-2929).
95pub const COLD_ACCOUNT_ACCESS_COST: u64 = 2600;
96/// Additional gas cost for accessing a cold account.
97pub const COLD_ACCOUNT_ACCESS_COST_ADDITIONAL: u64 =
98    COLD_ACCOUNT_ACCESS_COST - WARM_STORAGE_READ_COST;
99/// Gas cost for reading from a warm storage slot (EIP-2929).
100pub const WARM_STORAGE_READ_COST: u64 = 100;
101/// Gas cost for SSTORE reset operation on a warm storage slot.
102pub const WARM_SSTORE_RESET: u64 = SSTORE_RESET - COLD_SLOAD_COST;
103
104/// EIP-3860 : Limit and meter initcode
105pub const INITCODE_WORD_COST: u64 = 2;
106
107/// Gas stipend provided to the recipient of a CALL with value transfer.
108pub const CALL_STIPEND: u64 = 2300;
109
110/// Init and floor gas from transaction
111#[derive(Clone, Copy, Debug, Default, PartialEq, Eq)]
112#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
113pub struct InitialAndFloorGas {
114    /// Initial gas for transaction.
115    pub initial_gas: u64,
116    /// If transaction is a Call and Prague is enabled
117    /// floor_gas is at least amount of gas that is going to be spent.
118    pub floor_gas: u64,
119}
120
121impl InitialAndFloorGas {
122    /// Create a new InitialAndFloorGas instance.
123    #[inline]
124    pub const fn new(initial_gas: u64, floor_gas: u64) -> Self {
125        Self {
126            initial_gas,
127            floor_gas,
128        }
129    }
130}
131
132/// Initial gas that is deducted for transaction to be included.
133/// Initial gas contains initial stipend gas, gas for access list and input data.
134///
135/// # Returns
136///
137/// - Intrinsic gas
138/// - Number of tokens in calldata
139pub fn calculate_initial_tx_gas(
140    spec_id: SpecId,
141    input: &[u8],
142    is_create: bool,
143    access_list_accounts: u64,
144    access_list_storages: u64,
145    authorization_list_num: u64,
146) -> InitialAndFloorGas {
147    GasParams::new_spec(spec_id).initial_tx_gas(
148        input,
149        is_create,
150        access_list_accounts,
151        access_list_storages,
152        authorization_list_num,
153    )
154}
155
156/// Initial gas that is deducted for transaction to be included.
157/// Initial gas contains initial stipend gas, gas for access list and input data.
158///
159/// # Returns
160///
161/// - Intrinsic gas
162/// - Number of tokens in calldata
163pub fn calculate_initial_tx_gas_for_tx(tx: impl Transaction, spec: SpecId) -> InitialAndFloorGas {
164    let mut accounts = 0;
165    let mut storages = 0;
166    // legacy is only tx type that does not have access list.
167    if tx.tx_type() != TransactionType::Legacy {
168        (accounts, storages) = tx
169            .access_list()
170            .map(|al| {
171                al.fold((0, 0), |(mut num_accounts, mut num_storage_slots), item| {
172                    num_accounts += 1;
173                    num_storage_slots += item.storage_slots().count();
174
175                    (num_accounts, num_storage_slots)
176                })
177            })
178            .unwrap_or_default();
179    }
180
181    calculate_initial_tx_gas(
182        spec,
183        tx.input(),
184        tx.kind().is_create(),
185        accounts as u64,
186        storages as u64,
187        tx.authorization_list_len() as u64,
188    )
189}
190
191/// Retrieve the total number of tokens in calldata.
192#[inline]
193pub fn get_tokens_in_calldata_istanbul(input: &[u8]) -> u64 {
194    get_tokens_in_calldata(input, NON_ZERO_BYTE_MULTIPLIER_ISTANBUL)
195}
196
197/// Retrieve the total number of tokens in calldata.
198#[inline]
199pub fn get_tokens_in_calldata(input: &[u8], non_zero_data_multiplier: u64) -> u64 {
200    let zero_data_len = input.iter().filter(|v| **v == 0).count() as u64;
201    let non_zero_data_len = input.len() as u64 - zero_data_len;
202    zero_data_len + non_zero_data_len * non_zero_data_multiplier
203}