ethereum.forks.prague.vm.gas

Ethereum Virtual Machine (EVM) Gas.

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

Introduction

EVM gas constants and calculators.

GasCosts

Constant gas values for the EVM.

class GasCosts:

BASE

35
    BASE = Uint(2)

VERY_LOW

36
    VERY_LOW = Uint(3)

LOW

37
    LOW = Uint(5)

MID

38
    MID = Uint(8)

HIGH

39
    HIGH = Uint(10)

WARM_ACCESS

42
    WARM_ACCESS = Uint(100)

COLD_ACCOUNT_ACCESS

43
    COLD_ACCOUNT_ACCESS = Uint(2600)

COLD_STORAGE_ACCESS

44
    COLD_STORAGE_ACCESS = Uint(2100)

STORAGE_SET

47
    STORAGE_SET = Uint(20000)

COLD_STORAGE_WRITE

48
    COLD_STORAGE_WRITE = Uint(5000)

CALL_VALUE

51
    CALL_VALUE = Uint(9000)

CALL_STIPEND

52
    CALL_STIPEND = Uint(2300)

NEW_ACCOUNT

53
    NEW_ACCOUNT = Uint(25000)

CODE_DEPOSIT_PER_BYTE

56
    CODE_DEPOSIT_PER_BYTE = Uint(200)

CODE_INIT_PER_WORD

57
    CODE_INIT_PER_WORD = Uint(2)

AUTH_PER_EMPTY_ACCOUNT

60
    AUTH_PER_EMPTY_ACCOUNT = 25000

ZERO

63
    ZERO = Uint(0)

MEMORY_PER_WORD

64
    MEMORY_PER_WORD = Uint(3)

FAST_STEP

65
    FAST_STEP = Uint(5)

REFUND_STORAGE_CLEAR

68
    REFUND_STORAGE_CLEAR = 4800

PRECOMPILE_ECRECOVER

71
    PRECOMPILE_ECRECOVER = Uint(3000)

PRECOMPILE_SHA256_BASE

72
    PRECOMPILE_SHA256_BASE = Uint(60)

PRECOMPILE_SHA256_PER_WORD

73
    PRECOMPILE_SHA256_PER_WORD = Uint(12)

PRECOMPILE_RIPEMD160_BASE

74
    PRECOMPILE_RIPEMD160_BASE = Uint(600)

PRECOMPILE_RIPEMD160_PER_WORD

75
    PRECOMPILE_RIPEMD160_PER_WORD = Uint(120)

PRECOMPILE_IDENTITY_BASE

76
    PRECOMPILE_IDENTITY_BASE = Uint(15)

PRECOMPILE_IDENTITY_PER_WORD

77
    PRECOMPILE_IDENTITY_PER_WORD = Uint(3)

PRECOMPILE_BLAKE2F_PER_ROUND

78
    PRECOMPILE_BLAKE2F_PER_ROUND = Uint(1)

PRECOMPILE_POINT_EVALUATION

79
    PRECOMPILE_POINT_EVALUATION = Uint(50000)

PRECOMPILE_BLS_G1ADD

80
    PRECOMPILE_BLS_G1ADD = Uint(375)

PRECOMPILE_BLS_G1MUL

81
    PRECOMPILE_BLS_G1MUL = Uint(12000)

PRECOMPILE_BLS_G1MAP

82
    PRECOMPILE_BLS_G1MAP = Uint(5500)

PRECOMPILE_BLS_G2ADD

83
    PRECOMPILE_BLS_G2ADD = Uint(600)

PRECOMPILE_BLS_G2MUL

84
    PRECOMPILE_BLS_G2MUL = Uint(22500)

PRECOMPILE_BLS_G2MAP

85
    PRECOMPILE_BLS_G2MAP = Uint(23800)

PRECOMPILE_ECADD

86
    PRECOMPILE_ECADD = Uint(150)

PRECOMPILE_ECMUL

87
    PRECOMPILE_ECMUL = Uint(6000)

PRECOMPILE_ECPAIRING_BASE

88
    PRECOMPILE_ECPAIRING_BASE = Uint(45000)

PRECOMPILE_ECPAIRING_PER_POINT

89
    PRECOMPILE_ECPAIRING_PER_POINT = Uint(34000)

PER_BLOB

92
    PER_BLOB = U64(2**17)

BLOB_TARGET_GAS_PER_BLOCK

93
    BLOB_TARGET_GAS_PER_BLOCK = U64(786432)

BLOB_MIN_GASPRICE

94
    BLOB_MIN_GASPRICE = Uint(1)

BLOB_BASE_FEE_UPDATE_FRACTION

95
    BLOB_BASE_FEE_UPDATE_FRACTION = Uint(5007716)

TX_BASE

98
    TX_BASE = Uint(21000)

TX_CREATE

99
    TX_CREATE = Uint(32000)

TX_DATA_TOKEN_STANDARD

100
    TX_DATA_TOKEN_STANDARD = Uint(4)

TX_DATA_TOKEN_FLOOR

101
    TX_DATA_TOKEN_FLOOR = Uint(10)

TX_ACCESS_LIST_ADDRESS

102
    TX_ACCESS_LIST_ADDRESS = Uint(2400)

TX_ACCESS_LIST_STORAGE_KEY

103
    TX_ACCESS_LIST_STORAGE_KEY = Uint(1900)

LIMIT_ADJUSTMENT_FACTOR

106
    LIMIT_ADJUSTMENT_FACTOR = Uint(1024)

LIMIT_MINIMUM

107
    LIMIT_MINIMUM = Uint(5000)

OPCODE_ADD

110
    OPCODE_ADD = VERY_LOW

OPCODE_SUB

111
    OPCODE_SUB = VERY_LOW

OPCODE_MUL

112
    OPCODE_MUL = LOW

OPCODE_DIV

113
    OPCODE_DIV = LOW

OPCODE_SDIV

114
    OPCODE_SDIV = LOW

OPCODE_MOD

115
    OPCODE_MOD = LOW

OPCODE_SMOD

116
    OPCODE_SMOD = LOW

OPCODE_ADDMOD

117
    OPCODE_ADDMOD = MID

OPCODE_MULMOD

118
    OPCODE_MULMOD = MID

OPCODE_SIGNEXTEND

119
    OPCODE_SIGNEXTEND = LOW

OPCODE_LT

120
    OPCODE_LT = VERY_LOW

OPCODE_GT

121
    OPCODE_GT = VERY_LOW

OPCODE_SLT

122
    OPCODE_SLT = VERY_LOW

OPCODE_SGT

123
    OPCODE_SGT = VERY_LOW

OPCODE_EQ

124
    OPCODE_EQ = VERY_LOW

OPCODE_ISZERO

125
    OPCODE_ISZERO = VERY_LOW

OPCODE_AND

126
    OPCODE_AND = VERY_LOW

OPCODE_OR

127
    OPCODE_OR = VERY_LOW

OPCODE_XOR

128
    OPCODE_XOR = VERY_LOW

OPCODE_NOT

129
    OPCODE_NOT = VERY_LOW

OPCODE_BYTE

130
    OPCODE_BYTE = VERY_LOW

OPCODE_SHL

131
    OPCODE_SHL = VERY_LOW

OPCODE_SHR

132
    OPCODE_SHR = VERY_LOW

OPCODE_SAR

133
    OPCODE_SAR = VERY_LOW

OPCODE_JUMP

134
    OPCODE_JUMP = MID

OPCODE_JUMPI

135
    OPCODE_JUMPI = HIGH

OPCODE_JUMPDEST

136
    OPCODE_JUMPDEST = Uint(1)

OPCODE_CALLDATALOAD

137
    OPCODE_CALLDATALOAD = VERY_LOW

OPCODE_BLOCKHASH

138
    OPCODE_BLOCKHASH = Uint(20)

OPCODE_COINBASE

139
    OPCODE_COINBASE = BASE

OPCODE_POP

140
    OPCODE_POP = BASE

OPCODE_MSIZE

141
    OPCODE_MSIZE = BASE

OPCODE_PC

142
    OPCODE_PC = BASE

OPCODE_GAS

143
    OPCODE_GAS = BASE

OPCODE_ADDRESS

144
    OPCODE_ADDRESS = BASE

OPCODE_ORIGIN

145
    OPCODE_ORIGIN = BASE

OPCODE_CALLER

146
    OPCODE_CALLER = BASE

OPCODE_CALLVALUE

147
    OPCODE_CALLVALUE = BASE

OPCODE_CALLDATASIZE

148
    OPCODE_CALLDATASIZE = BASE

OPCODE_CODESIZE

149
    OPCODE_CODESIZE = BASE

OPCODE_GASPRICE

150
    OPCODE_GASPRICE = BASE

OPCODE_TIMESTAMP

151
    OPCODE_TIMESTAMP = BASE

OPCODE_NUMBER

152
    OPCODE_NUMBER = BASE

OPCODE_GASLIMIT

153
    OPCODE_GASLIMIT = BASE

OPCODE_PREVRANDAO

154
    OPCODE_PREVRANDAO = BASE

OPCODE_RETURNDATASIZE

155
    OPCODE_RETURNDATASIZE = BASE

OPCODE_CHAINID

156
    OPCODE_CHAINID = BASE

OPCODE_BASEFEE

157
    OPCODE_BASEFEE = BASE

OPCODE_BLOBBASEFEE

158
    OPCODE_BLOBBASEFEE = BASE

OPCODE_BLOBHASH

159
    OPCODE_BLOBHASH = Uint(3)

OPCODE_PUSH

160
    OPCODE_PUSH = VERY_LOW

OPCODE_PUSH0

161
    OPCODE_PUSH0 = BASE

OPCODE_DUP

162
    OPCODE_DUP = VERY_LOW

OPCODE_SWAP

163
    OPCODE_SWAP = VERY_LOW

OPCODE_RETURNDATACOPY_BASE

166
    OPCODE_RETURNDATACOPY_BASE = VERY_LOW

OPCODE_RETURNDATACOPY_PER_WORD

167
    OPCODE_RETURNDATACOPY_PER_WORD = Uint(3)

OPCODE_CALLDATACOPY_BASE

168
    OPCODE_CALLDATACOPY_BASE = VERY_LOW

OPCODE_CODECOPY_BASE

169
    OPCODE_CODECOPY_BASE = VERY_LOW

OPCODE_MCOPY_BASE

170
    OPCODE_MCOPY_BASE = VERY_LOW

OPCODE_MLOAD_BASE

171
    OPCODE_MLOAD_BASE = VERY_LOW

OPCODE_MSTORE_BASE

172
    OPCODE_MSTORE_BASE = VERY_LOW

OPCODE_MSTORE8_BASE

173
    OPCODE_MSTORE8_BASE = VERY_LOW

OPCODE_COPY_PER_WORD

174
    OPCODE_COPY_PER_WORD = Uint(3)

OPCODE_CREATE_BASE

175
    OPCODE_CREATE_BASE = Uint(32000)

OPCODE_EXP_BASE

176
    OPCODE_EXP_BASE = Uint(10)

OPCODE_EXP_PER_BYTE

177
    OPCODE_EXP_PER_BYTE = Uint(50)

OPCODE_KECCAK256_BASE

178
    OPCODE_KECCAK256_BASE = Uint(30)

OPCODE_KECCACK256_PER_WORD

179
    OPCODE_KECCACK256_PER_WORD = Uint(6)

OPCODE_LOG_BASE

180
    OPCODE_LOG_BASE = Uint(375)

OPCODE_LOG_DATA_PER_BYTE

181
    OPCODE_LOG_DATA_PER_BYTE = Uint(8)

OPCODE_LOG_TOPIC

182
    OPCODE_LOG_TOPIC = Uint(375)

OPCODE_SELFDESTRUCT_BASE

183
    OPCODE_SELFDESTRUCT_BASE = Uint(5000)

OPCODE_SELFDESTRUCT_NEW_ACCOUNT

184
    OPCODE_SELFDESTRUCT_NEW_ACCOUNT = Uint(25000)

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

187
@dataclass
class ExtendMemory:

cost

198
    cost: Uint

expand_by

199
    expand_by: Uint

MessageCallGas

Define the gas cost and gas given to the sub-call for executing the call opcodes.

cost: ethereum.base_types.Uint The gas required to execute the call opcode, excludes memory expansion costs. sub_call: ethereum.base_types.Uint The portion of gas available to sub-calls that is refundable if not consumed.

202
@dataclass
class MessageCallGas:

cost

216
    cost: Uint

sub_call

217
    sub_call: 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:
221
    """
222
    Subtracts `amount` from `evm.gas_left`.
223
224
    Parameters
225
    ----------
226
    evm :
227
        The current EVM.
228
    amount :
229
        The amount of gas the current operation requires.
230
231
    """
232
    evm_trace(evm, GasAndRefund(int(amount)))
233
234
    if evm.gas_left < amount:
235
        raise OutOfGasError
236
    else:
237
        evm.gas_left -= 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:
241
    """
242
    Calculates the gas cost for allocating memory
243
    to the smallest multiple of 32 bytes,
244
    such that the allocated size is at least as big as the given size.
245
246
    Parameters
247
    ----------
248
    size_in_bytes :
249
        The size of the data in bytes.
250
251
    Returns
252
    -------
253
    total_gas_cost : `ethereum.base_types.Uint`
254
        The gas cost for storing data in memory.
255
256
    """
257
    size_in_words = ceil32(size_in_bytes) // Uint(32)
258
    linear_cost = size_in_words * GasCosts.MEMORY_PER_WORD
259
    quadratic_cost = size_in_words ** Uint(2) // Uint(512)
260
    total_gas_cost = linear_cost + quadratic_cost
261
    try:
262
        return total_gas_cost
263
    except ValueError as e:
264
        raise OutOfGasError from e

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:
270
    """
271
    Calculates the gas amount to extend memory.
272
273
    Parameters
274
    ----------
275
    memory :
276
        Memory contents of the EVM.
277
    extensions:
278
        List of extensions to be made to the memory.
279
        Consists of a tuple of start position and size.
280
281
    Returns
282
    -------
283
    extend_memory: `ExtendMemory`
284
285
    """
286
    size_to_extend = Uint(0)
287
    to_be_paid = Uint(0)
288
    current_size = ulen(memory)
289
    for start_position, size in extensions:
290
        if size == 0:
291
            continue
292
        before_size = ceil32(current_size)
293
        after_size = ceil32(Uint(start_position) + Uint(size))
294
        if after_size <= before_size:
295
            continue
296
297
        size_to_extend += after_size - before_size
298
        already_paid = calculate_memory_gas_cost(before_size)
299
        total_cost = calculate_memory_gas_cost(after_size)
300
        to_be_paid += total_cost - already_paid
301
302
        current_size = after_size
303
304
    return ExtendMemory(to_be_paid, size_to_extend)

calculate_message_call_gas

Calculates the MessageCallGas (cost and gas made available to the sub-call) 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:
315
    """
316
    Calculates the MessageCallGas (cost and gas made available to the sub-call)
317
    for executing call Opcodes.
318
319
    Parameters
320
    ----------
321
    value:
322
        The amount of `ETH` that needs to be transferred.
323
    gas :
324
        The amount of gas provided to the message-call.
325
    gas_left :
326
        The amount of gas left in the current frame.
327
    memory_cost :
328
        The amount needed to extend the memory in the current frame.
329
    extra_gas :
330
        The amount of gas needed for transferring value + creating a new
331
        account inside a message call.
332
    call_stipend :
333
        The amount of stipend provided to a message call to execute code while
334
        transferring value (ETH).
335
336
    Returns
337
    -------
338
    message_call_gas: `MessageCallGas`
339
340
    """
341
    call_stipend = Uint(0) if value == 0 else call_stipend
342
    if gas_left < extra_gas + memory_cost:
343
        return MessageCallGas(gas + extra_gas, gas + call_stipend)
344
345
    gas = min(gas, max_message_call_gas(gas_left - memory_cost - extra_gas))
346
347
    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:
351
    """
352
    Calculates the maximum gas that is allowed for making a message call.
353
354
    Parameters
355
    ----------
356
    gas :
357
        The amount of gas provided to the message-call.
358
359
    Returns
360
    -------
361
    max_allowed_message_call_gas: `ethereum.base_types.Uint`
362
        The maximum gas allowed for making the message-call.
363
364
    """
365
    return gas - (gas // Uint(64))

init_code_cost

Calculates the gas to be charged for the init code in CREATE* 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:
369
    """
370
    Calculates the gas to be charged for the init code in CREATE*
371
    opcodes as well as create transactions.
372
373
    Parameters
374
    ----------
375
    init_code_length :
376
        The length of the init code provided to the opcode
377
        or a create transaction
378
379
    Returns
380
    -------
381
    init_code_gas: `ethereum.base_types.Uint`
382
        The gas to be charged for the init code.
383
384
    """
385
    return GasCosts.CODE_INIT_PER_WORD * ceil32(init_code_length) // Uint(32)

calculate_excess_blob_gas

Calculates 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:
389
    """
390
    Calculates the excess blob gas for the current block based
391
    on the gas used in the parent block.
392
393
    Parameters
394
    ----------
395
    parent_header :
396
        The parent block of the current block.
397
398
    Returns
399
    -------
400
    excess_blob_gas: `ethereum.base_types.U64`
401
        The excess blob gas for the current block.
402
403
    """
404
    # At the fork block, these are defined as zero.
405
    excess_blob_gas = U64(0)
406
    blob_gas_used = U64(0)
407
408
    if isinstance(parent_header, Header):
409
        # After the fork block, read them from the parent header.
410
        excess_blob_gas = parent_header.excess_blob_gas
411
        blob_gas_used = parent_header.blob_gas_used
412
413
    parent_blob_gas = excess_blob_gas + blob_gas_used
414
    if parent_blob_gas < GasCosts.BLOB_TARGET_GAS_PER_BLOCK:
415
        return U64(0)
416
417
    return parent_blob_gas - GasCosts.BLOB_TARGET_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) -> U64:
421
    """
422
    Calculate the total blob gas for a transaction.
423
424
    Parameters
425
    ----------
426
    tx :
427
        The transaction for which the blob gas is to be calculated.
428
429
    Returns
430
    -------
431
    total_blob_gas: `ethereum.base_types.Uint`
432
        The total blob gas for the transaction.
433
434
    """
435
    if isinstance(tx, BlobTransaction):
436
        return GasCosts.PER_BLOB * U64(len(tx.blob_versioned_hashes))
437
    else:
438
        return U64(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:
442
    """
443
    Calculate the blob gasprice for a block.
444
445
    Parameters
446
    ----------
447
    excess_blob_gas :
448
        The excess blob gas for the block.
449
450
    Returns
451
    -------
452
    blob_gasprice: `Uint`
453
        The blob gasprice.
454
455
    """
456
    return taylor_exponential(
457
        GasCosts.BLOB_MIN_GASPRICE,
458
        Uint(excess_blob_gas),
459
        GasCosts.BLOB_BASE_FEE_UPDATE_FRACTION,
460
    )

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:
464
    """
465
    Calculate the blob data fee for a transaction.
466
467
    Parameters
468
    ----------
469
    excess_blob_gas :
470
        The excess_blob_gas for the execution.
471
    tx :
472
        The transaction for which the blob data fee is to be calculated.
473
474
    Returns
475
    -------
476
    data_fee: `Uint`
477
        The blob data fee.
478
479
    """
480
    return Uint(calculate_total_blob_gas(tx)) * calculate_blob_gas_price(
481
        excess_blob_gas
482
    )