revm_context_interface/cfg/
gas.rs1use crate::{cfg::GasParams, transaction::AccessListItemTr as _, Transaction, TransactionType};
4use primitives::hardfork::SpecId;
5
6#[derive(Clone, Copy, Debug, Default, PartialEq, Eq, Hash)]
10#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
11pub struct GasTracker {
12 gas_limit: u64,
14 remaining: u64,
16 reservoir: u64,
19 state_gas_spent: u64,
21 refunded: i64,
23}
24
25impl GasTracker {
26 #[inline]
28 pub const fn new(gas_limit: u64, remaining: u64, reservoir: u64) -> Self {
29 Self {
30 gas_limit,
31 remaining,
32 reservoir,
33 state_gas_spent: 0,
34 refunded: 0,
35 }
36 }
37
38 #[inline]
40 pub const fn new_used_gas(gas_limit: u64, used_gas: u64, reservoir: u64) -> Self {
41 Self::new(gas_limit, gas_limit - used_gas, reservoir)
42 }
43
44 #[inline]
46 pub const fn limit(&self) -> u64 {
47 self.gas_limit
48 }
49
50 #[inline]
52 pub fn set_limit(&mut self, val: u64) {
53 self.gas_limit = val;
54 }
55
56 #[inline]
58 pub const fn remaining(&self) -> u64 {
59 self.remaining
60 }
61
62 #[inline]
64 pub fn set_remaining(&mut self, val: u64) {
65 self.remaining = val;
66 }
67
68 #[inline]
70 pub const fn reservoir(&self) -> u64 {
71 self.reservoir
72 }
73
74 #[inline]
76 pub fn set_reservoir(&mut self, val: u64) {
77 self.reservoir = val;
78 }
79
80 #[inline]
82 pub const fn state_gas_spent(&self) -> u64 {
83 self.state_gas_spent
84 }
85
86 #[inline]
88 pub fn set_state_gas_spent(&mut self, val: u64) {
89 self.state_gas_spent = val;
90 }
91
92 #[inline]
94 pub const fn refunded(&self) -> i64 {
95 self.refunded
96 }
97
98 #[inline]
100 pub fn set_refunded(&mut self, val: i64) {
101 self.refunded = val;
102 }
103
104 #[inline]
108 #[must_use = "In case of not enough gas, the interpreter should halt with an out-of-gas error"]
109 pub fn record_regular_cost(&mut self, cost: u64) -> bool {
110 if let Some(new_remaining) = self.remaining.checked_sub(cost) {
111 self.remaining = new_remaining;
112 return true;
113 }
114 false
115 }
116
117 #[inline]
125 #[must_use = "In case of not enough gas, the interpreter should halt with an out-of-gas error"]
126 pub fn record_state_cost(&mut self, cost: u64) -> bool {
127 if self.reservoir >= cost {
128 self.state_gas_spent = self.state_gas_spent.saturating_add(cost);
129 self.reservoir -= cost;
130 return true;
131 }
132
133 let spill = cost - self.reservoir;
134
135 let success = self.record_regular_cost(spill);
136 if success {
137 self.state_gas_spent = self.state_gas_spent.saturating_add(cost);
138 self.reservoir = 0;
139 }
140 success
141 }
142
143 #[inline]
145 pub fn record_refund(&mut self, refund: i64) {
146 self.refunded += refund;
147 }
148
149 #[inline]
151 pub fn erase_cost(&mut self, returned: u64) {
152 self.remaining += returned;
153 }
154
155 #[inline]
157 pub fn spend_all(&mut self) {
158 self.remaining = 0;
159 }
160}
161
162pub const ZERO: u64 = 0;
164pub const BASE: u64 = 2;
166
167pub const VERYLOW: u64 = 3;
169pub const DATA_LOADN_GAS: u64 = 3;
171
172pub const CONDITION_JUMP_GAS: u64 = 4;
174pub const RETF_GAS: u64 = 3;
176pub const DATA_LOAD_GAS: u64 = 4;
178
179pub const LOW: u64 = 5;
181pub const MID: u64 = 8;
183pub const HIGH: u64 = 10;
185pub const JUMPDEST: u64 = 1;
187pub const SELFDESTRUCT_REFUND: i64 = 24000;
189pub const CREATE: u64 = 32000;
191pub const CALLVALUE: u64 = 9000;
193pub const NEWACCOUNT: u64 = 25000;
195pub const EXP: u64 = 10;
197pub const MEMORY: u64 = 3;
199pub const LOG: u64 = 375;
201pub const LOGDATA: u64 = 8;
203pub const LOGTOPIC: u64 = 375;
205pub const KECCAK256: u64 = 30;
207pub const KECCAK256WORD: u64 = 6;
209pub const COPY: u64 = 3;
211pub const BLOCKHASH: u64 = 20;
213pub const CODEDEPOSIT: u64 = 200;
215
216pub const ISTANBUL_SLOAD_GAS: u64 = 800;
218pub const SSTORE_SET: u64 = 20000;
220pub const SSTORE_RESET: u64 = 5000;
222pub const REFUND_SSTORE_CLEARS: i64 = 15000;
224
225pub const STANDARD_TOKEN_COST: u64 = 4;
227pub const NON_ZERO_BYTE_DATA_COST: u64 = 68;
229pub const NON_ZERO_BYTE_MULTIPLIER: u64 = NON_ZERO_BYTE_DATA_COST / STANDARD_TOKEN_COST;
231pub const NON_ZERO_BYTE_DATA_COST_ISTANBUL: u64 = 16;
233pub const NON_ZERO_BYTE_MULTIPLIER_ISTANBUL: u64 =
235 NON_ZERO_BYTE_DATA_COST_ISTANBUL / STANDARD_TOKEN_COST;
236pub const TOTAL_COST_FLOOR_PER_TOKEN: u64 = 10;
238
239pub const EOF_CREATE_GAS: u64 = 32000;
241
242pub const ACCESS_LIST_ADDRESS: u64 = 2400;
245pub const ACCESS_LIST_STORAGE_KEY: u64 = 1900;
247
248pub const COLD_SLOAD_COST: u64 = 2100;
250pub const COLD_ACCOUNT_ACCESS_COST: u64 = 2600;
252pub const COLD_ACCOUNT_ACCESS_COST_ADDITIONAL: u64 =
254 COLD_ACCOUNT_ACCESS_COST - WARM_STORAGE_READ_COST;
255pub const WARM_STORAGE_READ_COST: u64 = 100;
257pub const WARM_SSTORE_RESET: u64 = SSTORE_RESET - COLD_SLOAD_COST;
259
260pub const INITCODE_WORD_COST: u64 = 2;
262
263pub const CALL_STIPEND: u64 = 2300;
265
266#[derive(Clone, Copy, Debug, Default, PartialEq, Eq)]
268#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
269pub struct InitialAndFloorGas {
270 pub initial_total_gas: u64,
272 pub initial_state_gas: u64,
278 pub floor_gas: u64,
281 pub eip7702_reservoir_refund: u64,
287}
288
289impl InitialAndFloorGas {
290 #[inline]
292 pub const fn new(initial_total_gas: u64, floor_gas: u64) -> Self {
293 Self {
294 initial_total_gas,
295 initial_state_gas: 0,
296 floor_gas,
297 eip7702_reservoir_refund: 0,
298 }
299 }
300
301 #[inline]
303 pub const fn new_with_state_gas(
304 initial_total_gas: u64,
305 initial_state_gas: u64,
306 floor_gas: u64,
307 ) -> Self {
308 Self {
309 initial_total_gas,
310 initial_state_gas,
311 floor_gas,
312 eip7702_reservoir_refund: 0,
313 }
314 }
315
316 #[inline]
321 pub const fn initial_regular_gas(&self) -> u64 {
322 self.initial_total_gas - self.initial_state_gas
323 }
324}
325
326pub fn calculate_initial_tx_gas(
334 spec_id: SpecId,
335 input: &[u8],
336 is_create: bool,
337 access_list_accounts: u64,
338 access_list_storages: u64,
339 authorization_list_num: u64,
340) -> InitialAndFloorGas {
341 GasParams::new_spec(spec_id).initial_tx_gas(
342 input,
343 is_create,
344 access_list_accounts,
345 access_list_storages,
346 authorization_list_num,
347 )
348}
349
350pub fn calculate_initial_tx_gas_for_tx(tx: impl Transaction, spec: SpecId) -> InitialAndFloorGas {
358 let mut accounts = 0;
359 let mut storages = 0;
360 if tx.tx_type() != TransactionType::Legacy {
362 (accounts, storages) = tx
363 .access_list()
364 .map(|al| {
365 al.fold((0, 0), |(mut num_accounts, mut num_storage_slots), item| {
366 num_accounts += 1;
367 num_storage_slots += item.storage_slots().count();
368
369 (num_accounts, num_storage_slots)
370 })
371 })
372 .unwrap_or_default();
373 }
374
375 calculate_initial_tx_gas(
376 spec,
377 tx.input(),
378 tx.kind().is_create(),
379 accounts as u64,
380 storages as u64,
381 tx.authorization_list_len() as u64,
382 )
383}
384
385#[inline]
387pub fn get_tokens_in_calldata_istanbul(input: &[u8]) -> u64 {
388 get_tokens_in_calldata(input, NON_ZERO_BYTE_MULTIPLIER_ISTANBUL)
389}
390
391#[inline]
393pub fn get_tokens_in_calldata(input: &[u8], non_zero_data_multiplier: u64) -> u64 {
394 let zero_data_len = input.iter().filter(|v| **v == 0).count() as u64;
395 let non_zero_data_len = input.len() as u64 - zero_data_len;
396 zero_data_len + non_zero_data_len * non_zero_data_multiplier
397}