ethereum.shanghai.vm.gasethereum.cancun.vm.gas

Ethereum Virtual Machine (EVM) Gas ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

.. contents:: Table of Contents :backlinks: none :local:

Introduction

EVM gas constants and calculators.

GAS_JUMPDEST

26
GAS_JUMPDEST = Uint(1)

GAS_BASE

27
GAS_BASE = Uint(2)

GAS_VERY_LOW

28
GAS_VERY_LOW = Uint(3)

GAS_STORAGE_SET

29
GAS_STORAGE_SET = Uint(20000)

GAS_STORAGE_UPDATE

30
GAS_STORAGE_UPDATE = Uint(5000)

GAS_STORAGE_CLEAR_REFUND

31
GAS_STORAGE_CLEAR_REFUND = Uint(4800)

GAS_LOW

32
GAS_LOW = Uint(5)

GAS_MID

33
GAS_MID = Uint(8)

GAS_HIGH

34
GAS_HIGH = Uint(10)

GAS_EXPONENTIATION

35
GAS_EXPONENTIATION = Uint(10)

GAS_EXPONENTIATION_PER_BYTE

36
GAS_EXPONENTIATION_PER_BYTE = Uint(50)

GAS_MEMORY

37
GAS_MEMORY = Uint(3)

GAS_KECCAK256

38
GAS_KECCAK256 = Uint(30)

GAS_KECCAK256_WORD

39
GAS_KECCAK256_WORD = Uint(6)

GAS_COPY

40
GAS_COPY = Uint(3)

GAS_BLOCK_HASH

41
GAS_BLOCK_HASH = Uint(20)

GAS_LOG

42
GAS_LOG = Uint(375)

GAS_LOG_DATA

43
GAS_LOG_DATA = Uint(8)

GAS_LOG_TOPIC

44
GAS_LOG_TOPIC = Uint(375)

GAS_CREATE

45
GAS_CREATE = Uint(32000)

GAS_CODE_DEPOSIT

46
GAS_CODE_DEPOSIT = Uint(200)

GAS_ZERO

47
GAS_ZERO = Uint(0)

GAS_NEW_ACCOUNT

48
GAS_NEW_ACCOUNT = Uint(25000)

GAS_CALL_VALUE

49
GAS_CALL_VALUE = Uint(9000)

GAS_CALL_STIPEND

50
GAS_CALL_STIPEND = Uint(2300)

GAS_SELF_DESTRUCT

51
GAS_SELF_DESTRUCT = Uint(5000)

GAS_SELF_DESTRUCT_NEW_ACCOUNT

52
GAS_SELF_DESTRUCT_NEW_ACCOUNT = Uint(25000)

GAS_ECRECOVER

53
GAS_ECRECOVER = Uint(3000)

GAS_SHA256

54
GAS_SHA256 = Uint(60)

GAS_SHA256_WORD

55
GAS_SHA256_WORD = Uint(12)

GAS_RIPEMD160

56
GAS_RIPEMD160 = Uint(600)

GAS_RIPEMD160_WORD

57
GAS_RIPEMD160_WORD = Uint(120)

GAS_IDENTITY

58
GAS_IDENTITY = Uint(15)

GAS_IDENTITY_WORD

59
GAS_IDENTITY_WORD = Uint(3)

GAS_RETURN_DATA_COPY

60
GAS_RETURN_DATA_COPY = Uint(3)

GAS_FAST_STEP

61
GAS_FAST_STEP = Uint(5)

GAS_BLAKE2_PER_ROUND

62
GAS_BLAKE2_PER_ROUND = Uint(1)

GAS_COLD_SLOAD

63
GAS_COLD_SLOAD = Uint(2100)

GAS_COLD_ACCOUNT_ACCESS

64
GAS_COLD_ACCOUNT_ACCESS = Uint(2600)

GAS_WARM_ACCESS

65
GAS_WARM_ACCESS = Uint(100)

GAS_INIT_CODE_WORD_COST

66
GAS_INIT_CODE_WORD_COST = 2

GAS_BLOBHASH_OPCODE

67
GAS_BLOBHASH_OPCODE = Uint(3)

GAS_POINT_EVALUATION

68
GAS_POINT_EVALUATION = Uint(50000)

TARGET_BLOB_GAS_PER_BLOCK

70
TARGET_BLOB_GAS_PER_BLOCK = U64(393216)

GAS_PER_BLOB

71
GAS_PER_BLOB = Uint(2**17)

MIN_BLOB_GASPRICE

72
MIN_BLOB_GASPRICE = Uint(1)

BLOB_GASPRICE_UPDATE_FRACTION

73
BLOB_GASPRICE_UPDATE_FRACTION = Uint(3338477)

ExtendMemory

Define the parameters for memory extension in opcodes

cost: ethereum.base_types.Uint The gas required to perform the extension expand_by: ethereum.base_types.Uint The size by which the memory will be extended

76
@dataclass
class ExtendMemory:

cost

87
    cost: Uint

expand_by

88
    expand_by: Uint

MessageCallGas

Define the gas cost and stipend for executing the call opcodes.

cost: ethereum.base_types.Uint The non-refundable portion of gas reserved for executing the call opcode. stipend: ethereum.base_types.Uint The portion of gas available to sub-calls that is refundable if not consumed

91
@dataclass
class MessageCallGas:

cost

104
    cost: Uint

stipend

105
    stipend: Uint

charge_gas

Subtracts amount from evm.gas_left.

Parameters

evm : The current EVM. amount : The amount of gas the current operation requires.

def charge_gas(evm: Evm, ​​amount: Uint) -> None:
109
    """
110
    Subtracts `amount` from `evm.gas_left`.
111
112
    Parameters
113
    ----------
114
    evm :
115
        The current EVM.
116
    amount :
117
        The amount of gas the current operation requires.
118
119
    """
120
    evm_trace(evm, GasAndRefund(amount))
121
122
    if evm.gas_left < amount:
123
        raise OutOfGasError
124
    else:
125
        evm.gas_left -= U256(amount)

calculate_memory_gas_cost

Calculates the gas cost for allocating memory to the smallest multiple of 32 bytes, such that the allocated size is at least as big as the given size.

Parameters

size_in_bytes : The size of the data in bytes.

Returns

total_gas_cost : ethereum.base_types.Uint The gas cost for storing data in memory.

def calculate_memory_gas_cost(size_in_bytes: Uint) -> Uint:
129
    """
130
    Calculates the gas cost for allocating memory
131
    to the smallest multiple of 32 bytes,
132
    such that the allocated size is at least as big as the given size.
133
134
    Parameters
135
    ----------
136
    size_in_bytes :
137
        The size of the data in bytes.
138
139
    Returns
140
    -------
141
    total_gas_cost : `ethereum.base_types.Uint`
142
        The gas cost for storing data in memory.
143
    """
144
    size_in_words = ceil32(size_in_bytes) // 32
145
    linear_cost = size_in_words * GAS_MEMORY
146
    quadratic_cost = size_in_words**2 // 512
147
    total_gas_cost = linear_cost + quadratic_cost
148
    try:
149
        return total_gas_cost
150
    except ValueError:
151
        raise OutOfGasError

calculate_gas_extend_memory

Calculates the gas amount to extend memory

Parameters

memory : Memory contents of the EVM. extensions: List of extensions to be made to the memory. Consists of a tuple of start position and size.

Returns

extend_memory: ExtendMemory

def calculate_gas_extend_memory(memory: bytearray, ​​extensions: List[Tuple[U256, U256]]) -> ExtendMemory:
157
    """
158
    Calculates the gas amount to extend memory
159
160
    Parameters
161
    ----------
162
    memory :
163
        Memory contents of the EVM.
164
    extensions:
165
        List of extensions to be made to the memory.
166
        Consists of a tuple of start position and size.
167
168
    Returns
169
    -------
170
    extend_memory: `ExtendMemory`
171
    """
172
    size_to_extend = Uint(0)
173
    to_be_paid = Uint(0)
174
    current_size = Uint(len(memory))
175
    for start_position, size in extensions:
176
        if size == 0:
177
            continue
178
        before_size = ceil32(current_size)
179
        after_size = ceil32(Uint(start_position) + Uint(size))
180
        if after_size <= before_size:
181
            continue
182
183
        size_to_extend += after_size - before_size
184
        already_paid = calculate_memory_gas_cost(before_size)
185
        total_cost = calculate_memory_gas_cost(after_size)
186
        to_be_paid += total_cost - already_paid
187
188
        current_size = after_size
189
190
    return ExtendMemory(to_be_paid, size_to_extend)

calculate_message_call_gas

Calculates the MessageCallGas (cost and stipend) for executing call Opcodes.

Parameters

value: The amount of ETH that needs to be transferred. gas : The amount of gas provided to the message-call. gas_left : The amount of gas left in the current frame. memory_cost : The amount needed to extend the memory in the current frame. extra_gas : The amount of gas needed for transferring value + creating a new account inside a message call. call_stipend : The amount of stipend provided to a message call to execute code while transferring value(ETH).

Returns

message_call_gas: MessageCallGas

def calculate_message_call_gas(value: U256, ​​gas: Uint, ​​gas_left: Uint, ​​memory_cost: Uint, ​​extra_gas: Uint, ​​call_stipend: Uint) -> MessageCallGas:
201
    """
202
    Calculates the MessageCallGas (cost and stipend) for
203
    executing call Opcodes.
204
205
    Parameters
206
    ----------
207
    value:
208
        The amount of `ETH` that needs to be transferred.
209
    gas :
210
        The amount of gas provided to the message-call.
211
    gas_left :
212
        The amount of gas left in the current frame.
213
    memory_cost :
214
        The amount needed to extend the memory in the current frame.
215
    extra_gas :
216
        The amount of gas needed for transferring value + creating a new
217
        account inside a message call.
218
    call_stipend :
219
        The amount of stipend provided to a message call to execute code while
220
        transferring value(ETH).
221
222
    Returns
223
    -------
224
    message_call_gas: `MessageCallGas`
225
    """
226
    call_stipend = Uint(0) if value == 0 else call_stipend
227
    if gas_left < extra_gas + memory_cost:
228
        return MessageCallGas(gas + extra_gas, gas + call_stipend)
229
230
    gas = min(gas, max_message_call_gas(gas_left - memory_cost - extra_gas))
231
232
    return MessageCallGas(gas + extra_gas, gas + call_stipend)

max_message_call_gas

Calculates the maximum gas that is allowed for making a message call

Parameters

gas : The amount of gas provided to the message-call.

Returns

max_allowed_message_call_gas: ethereum.base_types.Uint The maximum gas allowed for making the message-call.

def max_message_call_gas(gas: Uint) -> Uint:
236
    """
237
    Calculates the maximum gas that is allowed for making a message call
238
239
    Parameters
240
    ----------
241
    gas :
242
        The amount of gas provided to the message-call.
243
244
    Returns
245
    -------
246
    max_allowed_message_call_gas: `ethereum.base_types.Uint`
247
        The maximum gas allowed for making the message-call.
248
    """
249
    return gas - (gas // 64)

init_code_cost

Calculates the gas to be charged for the init code in CREAT* opcodes as well as create transactions.

Parameters

init_code_length : The length of the init code provided to the opcode or a create transaction

Returns

init_code_gas: ethereum.base_types.Uint The gas to be charged for the init code.

def init_code_cost(init_code_length: Uint) -> Uint:
253
    """
254
    Calculates the gas to be charged for the init code in CREAT*
255
    opcodes as well as create transactions.
256
257
    Parameters
258
    ----------
259
    init_code_length :
260
        The length of the init code provided to the opcode
261
        or a create transaction
262
263
    Returns
264
    -------
265
    init_code_gas: `ethereum.base_types.Uint`
266
        The gas to be charged for the init code.
267
    """
268
    return GAS_INIT_CODE_WORD_COST * ceil32(init_code_length) // 32

calculate_excess_blob_gas

Calculated the excess blob gas for the current block based on the gas used in the parent block.

Parameters

parent_header : The parent block of the current block.

Returns

excess_blob_gas: ethereum.base_types.U64 The excess blob gas for the current block.

def calculate_excess_blob_gas(parent_header: Header) -> U64:
272
    """
273
    Calculated the excess blob gas for the current block based
274
    on the gas used in the parent block.
275
276
    Parameters
277
    ----------
278
    parent_header :
279
        The parent block of the current block.
280
281
    Returns
282
    -------
283
    excess_blob_gas: `ethereum.base_types.U64`
284
        The excess blob gas for the current block.
285
    """
286
    parent_blob_gas = (
287
        parent_header.excess_blob_gas + parent_header.blob_gas_used
288
    )
289
    if parent_blob_gas < TARGET_BLOB_GAS_PER_BLOCK:
290
        return U64(0)
291
    else:
292
        return parent_blob_gas - TARGET_BLOB_GAS_PER_BLOCK

calculate_total_blob_gas

Calculate the total blob gas for a transaction.

Parameters

tx : The transaction for which the blob gas is to be calculated.

Returns

total_blob_gas: ethereum.base_types.Uint The total blob gas for the transaction.

def calculate_total_blob_gas(tx: Transaction) -> Uint:
296
    """
297
    Calculate the total blob gas for a transaction.
298
299
    Parameters
300
    ----------
301
    tx :
302
        The transaction for which the blob gas is to be calculated.
303
304
    Returns
305
    -------
306
    total_blob_gas: `ethereum.base_types.Uint`
307
        The total blob gas for the transaction.
308
    """
309
    if isinstance(tx, BlobTransaction):
310
        return GAS_PER_BLOB * len(tx.blob_versioned_hashes)
311
    else:
312
        return Uint(0)

calculate_blob_gas_price

Calculate the blob gasprice for a block.

Parameters

excess_blob_gas : The excess blob gas for the block.

Returns

blob_gasprice: Uint The blob gasprice.

def calculate_blob_gas_price(excess_blob_gas: U64) -> Uint:
316
    """
317
    Calculate the blob gasprice for a block.
318
319
    Parameters
320
    ----------
321
    excess_blob_gas :
322
        The excess blob gas for the block.
323
324
    Returns
325
    -------
326
    blob_gasprice: `Uint`
327
        The blob gasprice.
328
    """
329
    return taylor_exponential(
330
        MIN_BLOB_GASPRICE,
331
        Uint(excess_blob_gas),
332
        BLOB_GASPRICE_UPDATE_FRACTION,
333
    )

calculate_data_fee

Calculate the blob data fee for a transaction.

Parameters

excess_blob_gas : The excess_blob_gas for the execution. tx : The transaction for which the blob data fee is to be calculated.

Returns

data_fee: Uint The blob data fee.

def calculate_data_fee(excess_blob_gas: U64, ​​tx: Transaction) -> Uint:
337
    """
338
    Calculate the blob data fee for a transaction.
339
340
    Parameters
341
    ----------
342
    excess_blob_gas :
343
        The excess_blob_gas for the execution.
344
    tx :
345
        The transaction for which the blob data fee is to be calculated.
346
347
    Returns
348
    -------
349
    data_fee: `Uint`
350
        The blob data fee.
351
    """
352
    return calculate_total_blob_gas(tx) * calculate_blob_gas_price(
353
        excess_blob_gas
354
    )