ethereum.forks.bpo5.vm.gasethereum.forks.amsterdam.vm.gas

Ethereum Virtual Machine (EVM) Gas.

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

Introduction

EVM gas constants and calculators.

StateGasCosts

EIP-8037 state-gas constants.

Kept separate from GasCosts because these carry a different unit: state-byte counts that convert into gas via COST_PER_STATE_BYTE.

class StateGasCosts:

COST_PER_STATE_BYTE

40
    COST_PER_STATE_BYTE: Final[StateGasPerByte] = StateGasPerByte(Uint(1530))

STATE_BYTES_PER_NEW_ACCOUNT

41
    STATE_BYTES_PER_NEW_ACCOUNT: Final[Uint] = Uint(120)

STATE_BYTES_PER_STORAGE_SET

42
    STATE_BYTES_PER_STORAGE_SET: Final[Uint] = Uint(64)

STATE_BYTES_PER_AUTH_BASE

43
    STATE_BYTES_PER_AUTH_BASE: Final[Uint] = Uint(23)

STORAGE_SET

44
    STORAGE_SET: Final[StateGas] = (
45
        STATE_BYTES_PER_STORAGE_SET * COST_PER_STATE_BYTE
46
    )

NEW_ACCOUNT

47
    NEW_ACCOUNT: Final[StateGas] = (
48
        STATE_BYTES_PER_NEW_ACCOUNT * COST_PER_STATE_BYTE
49
    )

AUTH_BASE

50
    AUTH_BASE: Final[StateGas] = (
51
        STATE_BYTES_PER_AUTH_BASE * COST_PER_STATE_BYTE
52
    )

GasCosts

Constant gas values for the EVM.

class GasCosts:

BASE

62
    BASE: Final[Uint] = Uint(2)

VERY_LOW

63
    VERY_LOW: Final[Uint] = Uint(3)

LOW

64
    LOW: Final[Uint] = Uint(5)

MID

65
    MID: Final[Uint] = Uint(8)

HIGH

66
    HIGH: Final[Uint] = Uint(10)

WARM_ACCESS

69
    WARM_ACCESS: Final[Uint] = Uint(100)

COLD_ACCOUNT_ACCESS

43
    COLD_ACCOUNT_ACCESS: Final[Uint] = Uint(2600)
70
    COLD_ACCOUNT_ACCESS: Final[Uint] = Uint(3000)

COLD_STORAGE_ACCESS

44
    COLD_STORAGE_ACCESS: Final[Uint] = Uint(2100)
71
    COLD_STORAGE_ACCESS: Final[Uint] = Uint(3000)

STORAGE_SET

47
    STORAGE_SET: Final[Uint] = Uint(20000)

COLD_STORAGE_WRITE

48
    COLD_STORAGE_WRITE: Final[Uint] = Uint(5000)

STORAGE_WRITE

74
    STORAGE_WRITE: Final[Uint] = Uint(10000)

CALL_VALUE

51
    CALL_VALUE: Final[Uint] = Uint(9000)
77
    CALL_VALUE: Final[Uint] = Uint(10300)

CALL_STIPEND

78
    CALL_STIPEND: Final[Uint] = Uint(2300)

NEW_ACCOUNT

53
    NEW_ACCOUNT: Final[Uint] = Uint(25000)

ACCOUNT_WRITE

79
    ACCOUNT_WRITE: Final[Uint] = Uint(8000)

CODE_DEPOSIT_PER_BYTE

82
    CODE_DEPOSIT_PER_BYTE: Final[Uint] = Uint(200)

CODE_INIT_PER_WORD

83
    CODE_INIT_PER_WORD: Final[Uint] = Uint(2)

AUTH_PER_EMPTY_ACCOUNT

60
    AUTH_PER_EMPTY_ACCOUNT: Final[int] = 25000

CREATE_ACCESS

84
    CREATE_ACCESS: Final[Uint] = ACCOUNT_WRITE + COLD_STORAGE_ACCESS

ZERO

87
    ZERO: Final[Uint] = Uint(0)

MEMORY_PER_WORD

88
    MEMORY_PER_WORD: Final[Uint] = Uint(3)

FAST_STEP

89
    FAST_STEP: Final[Uint] = Uint(5)

REFUND_STORAGE_CLEAR

68
    REFUND_STORAGE_CLEAR: Final[int] = 4800
92
    REFUND_STORAGE_CLEAR: Final[int] = int(
93
        (STORAGE_WRITE + COLD_STORAGE_ACCESS) * Uint(4800) // Uint(5000)
94
    )

PRECOMPILE_ECRECOVER

97
    PRECOMPILE_ECRECOVER: Final[Uint] = Uint(3000)

PRECOMPILE_P256VERIFY

98
    PRECOMPILE_P256VERIFY: Final[Uint] = Uint(6900)

PRECOMPILE_SHA256_BASE

99
    PRECOMPILE_SHA256_BASE: Final[Uint] = Uint(60)

PRECOMPILE_SHA256_PER_WORD

100
    PRECOMPILE_SHA256_PER_WORD: Final[Uint] = Uint(12)

PRECOMPILE_RIPEMD160_BASE

101
    PRECOMPILE_RIPEMD160_BASE: Final[Uint] = Uint(600)

PRECOMPILE_RIPEMD160_PER_WORD

102
    PRECOMPILE_RIPEMD160_PER_WORD: Final[Uint] = Uint(120)

PRECOMPILE_IDENTITY_BASE

103
    PRECOMPILE_IDENTITY_BASE: Final[Uint] = Uint(15)

PRECOMPILE_IDENTITY_PER_WORD

104
    PRECOMPILE_IDENTITY_PER_WORD: Final[Uint] = Uint(3)

PRECOMPILE_BLAKE2F_PER_ROUND

105
    PRECOMPILE_BLAKE2F_PER_ROUND: Final[Uint] = Uint(1)

PRECOMPILE_POINT_EVALUATION

106
    PRECOMPILE_POINT_EVALUATION: Final[Uint] = Uint(50000)

PRECOMPILE_BLS_G1ADD

107
    PRECOMPILE_BLS_G1ADD: Final[Uint] = Uint(375)

PRECOMPILE_BLS_G1MUL

108
    PRECOMPILE_BLS_G1MUL: Final[Uint] = Uint(12000)

PRECOMPILE_BLS_G1MAP

109
    PRECOMPILE_BLS_G1MAP: Final[Uint] = Uint(5500)

PRECOMPILE_BLS_G2ADD

110
    PRECOMPILE_BLS_G2ADD: Final[Uint] = Uint(600)

PRECOMPILE_BLS_G2MUL

111
    PRECOMPILE_BLS_G2MUL: Final[Uint] = Uint(22500)

PRECOMPILE_BLS_G2MAP

112
    PRECOMPILE_BLS_G2MAP: Final[Uint] = Uint(23800)

PRECOMPILE_ECADD

113
    PRECOMPILE_ECADD: Final[Uint] = Uint(150)

PRECOMPILE_ECMUL

114
    PRECOMPILE_ECMUL: Final[Uint] = Uint(6000)

PRECOMPILE_ECPAIRING_BASE

115
    PRECOMPILE_ECPAIRING_BASE: Final[Uint] = Uint(45000)

PRECOMPILE_ECPAIRING_PER_POINT

116
    PRECOMPILE_ECPAIRING_PER_POINT: Final[Uint] = Uint(34000)

PER_BLOB

119
    PER_BLOB: Final[U64] = U64(2**17)

BLOB_SCHEDULE_TARGET

120
    BLOB_SCHEDULE_TARGET: Final[U64] = U64(14)

BLOB_TARGET_GAS_PER_BLOCK

121
    BLOB_TARGET_GAS_PER_BLOCK: Final[U64] = PER_BLOB * BLOB_SCHEDULE_TARGET

BLOB_BASE_COST

122
    BLOB_BASE_COST: Final[Uint] = Uint(2**13)

BLOB_SCHEDULE_MAX

123
    BLOB_SCHEDULE_MAX: Final[U64] = U64(21)

BLOB_MIN_GASPRICE

124
    BLOB_MIN_GASPRICE: Final[Uint] = Uint(1)

BLOB_BASE_FEE_UPDATE_FRACTION

125
    BLOB_BASE_FEE_UPDATE_FRACTION: Final[Uint] = Uint(11684671)

BLOCK_ACCESS_LIST_ITEM

128
    BLOCK_ACCESS_LIST_ITEM: Final[Uint] = Uint(2000)

TX_BASE

102
    TX_BASE: Final[Uint] = Uint(21000)
131
    TX_BASE: Final[Uint] = Uint(12000)

TX_CREATE

132
    TX_CREATE: Final[Uint] = Uint(32000)

TX_VALUE_COST

133
    TX_VALUE_COST: Final[Uint] = Uint(4244)

TRANSFER_LOG_COST

134
    TRANSFER_LOG_COST: Final[Uint] = Uint(1756)

TX_DATA_TOKEN_STANDARD

135
    TX_DATA_TOKEN_STANDARD: Final[Uint] = Uint(4)

TX_DATA_TOKEN_FLOOR

105
    TX_DATA_TOKEN_FLOOR: Final[Uint] = Uint(10)
136
    TX_DATA_TOKEN_FLOOR: Final[Uint] = Uint(16)

TX_ACCESS_LIST_ADDRESS

106
    TX_ACCESS_LIST_ADDRESS: Final[Uint] = Uint(2400)
137
    TX_ACCESS_LIST_ADDRESS: Final[Uint] = COLD_ACCOUNT_ACCESS

TX_ACCESS_LIST_STORAGE_KEY

107
    TX_ACCESS_LIST_STORAGE_KEY: Final[Uint] = Uint(1900)
138
    TX_ACCESS_LIST_STORAGE_KEY: Final[Uint] = COLD_STORAGE_ACCESS

AUTH_TUPLE_BYTES

141
    AUTH_TUPLE_BYTES: Final[Uint] = Uint(101)

REGULAR_PER_AUTH_BASE_COST

142
    REGULAR_PER_AUTH_BASE_COST: Final[Uint] = (
143
        AUTH_TUPLE_BYTES * TX_DATA_TOKEN_FLOOR
144
        + PRECOMPILE_ECRECOVER
145
        + COLD_ACCOUNT_ACCESS
146
        + Uint(2) * WARM_ACCESS
147
    )

LIMIT_ADJUSTMENT_FACTOR

150
    LIMIT_ADJUSTMENT_FACTOR: Final[Uint] = Uint(1024)

LIMIT_MINIMUM

151
    LIMIT_MINIMUM: Final[Uint] = Uint(5000)

OPCODE_ADD

154
    OPCODE_ADD: Final[Uint] = VERY_LOW

OPCODE_SUB

155
    OPCODE_SUB: Final[Uint] = VERY_LOW

OPCODE_MUL

156
    OPCODE_MUL: Final[Uint] = LOW

OPCODE_DIV

157
    OPCODE_DIV: Final[Uint] = LOW

OPCODE_SDIV

158
    OPCODE_SDIV: Final[Uint] = LOW

OPCODE_MOD

159
    OPCODE_MOD: Final[Uint] = LOW

OPCODE_SMOD

160
    OPCODE_SMOD: Final[Uint] = LOW

OPCODE_ADDMOD

161
    OPCODE_ADDMOD: Final[Uint] = MID

OPCODE_MULMOD

162
    OPCODE_MULMOD: Final[Uint] = MID

OPCODE_SIGNEXTEND

163
    OPCODE_SIGNEXTEND: Final[Uint] = LOW

OPCODE_LT

164
    OPCODE_LT: Final[Uint] = VERY_LOW

OPCODE_GT

165
    OPCODE_GT: Final[Uint] = VERY_LOW

OPCODE_SLT

166
    OPCODE_SLT: Final[Uint] = VERY_LOW

OPCODE_SGT

167
    OPCODE_SGT: Final[Uint] = VERY_LOW

OPCODE_EQ

168
    OPCODE_EQ: Final[Uint] = VERY_LOW

OPCODE_ISZERO

169
    OPCODE_ISZERO: Final[Uint] = VERY_LOW

OPCODE_AND

170
    OPCODE_AND: Final[Uint] = VERY_LOW

OPCODE_OR

171
    OPCODE_OR: Final[Uint] = VERY_LOW

OPCODE_XOR

172
    OPCODE_XOR: Final[Uint] = VERY_LOW

OPCODE_NOT

173
    OPCODE_NOT: Final[Uint] = VERY_LOW

OPCODE_BYTE

174
    OPCODE_BYTE: Final[Uint] = VERY_LOW

OPCODE_SHL

175
    OPCODE_SHL: Final[Uint] = VERY_LOW

OPCODE_SHR

176
    OPCODE_SHR: Final[Uint] = VERY_LOW

OPCODE_SAR

177
    OPCODE_SAR: Final[Uint] = VERY_LOW

OPCODE_CLZ

178
    OPCODE_CLZ: Final[Uint] = LOW

OPCODE_JUMP

179
    OPCODE_JUMP: Final[Uint] = MID

OPCODE_JUMPI

180
    OPCODE_JUMPI: Final[Uint] = HIGH

OPCODE_JUMPDEST

181
    OPCODE_JUMPDEST: Final[Uint] = Uint(1)

OPCODE_CALLDATALOAD

182
    OPCODE_CALLDATALOAD: Final[Uint] = VERY_LOW

OPCODE_BLOCKHASH

183
    OPCODE_BLOCKHASH: Final[Uint] = Uint(20)

OPCODE_COINBASE

184
    OPCODE_COINBASE: Final[Uint] = BASE

OPCODE_POP

185
    OPCODE_POP: Final[Uint] = BASE

OPCODE_MSIZE

186
    OPCODE_MSIZE: Final[Uint] = BASE

OPCODE_PC

187
    OPCODE_PC: Final[Uint] = BASE

OPCODE_GAS

188
    OPCODE_GAS: Final[Uint] = BASE

OPCODE_ADDRESS

189
    OPCODE_ADDRESS: Final[Uint] = BASE

OPCODE_ORIGIN

190
    OPCODE_ORIGIN: Final[Uint] = BASE

OPCODE_CALLER

191
    OPCODE_CALLER: Final[Uint] = BASE

OPCODE_CALLVALUE

192
    OPCODE_CALLVALUE: Final[Uint] = BASE

OPCODE_CALLDATASIZE

193
    OPCODE_CALLDATASIZE: Final[Uint] = BASE

OPCODE_CODESIZE

194
    OPCODE_CODESIZE: Final[Uint] = BASE

OPCODE_GASPRICE

195
    OPCODE_GASPRICE: Final[Uint] = BASE

OPCODE_TIMESTAMP

196
    OPCODE_TIMESTAMP: Final[Uint] = BASE

OPCODE_NUMBER

197
    OPCODE_NUMBER: Final[Uint] = BASE

OPCODE_GASLIMIT

198
    OPCODE_GASLIMIT: Final[Uint] = BASE

OPCODE_PREVRANDAO

199
    OPCODE_PREVRANDAO: Final[Uint] = BASE

OPCODE_RETURNDATASIZE

200
    OPCODE_RETURNDATASIZE: Final[Uint] = BASE

OPCODE_CHAINID

201
    OPCODE_CHAINID: Final[Uint] = BASE

OPCODE_BASEFEE

202
    OPCODE_BASEFEE: Final[Uint] = BASE

OPCODE_BLOBBASEFEE

203
    OPCODE_BLOBBASEFEE: Final[Uint] = BASE

OPCODE_SLOTNUM

204
    OPCODE_SLOTNUM: Final[Uint] = BASE

OPCODE_BLOBHASH

205
    OPCODE_BLOBHASH: Final[Uint] = Uint(3)

OPCODE_PUSH

206
    OPCODE_PUSH: Final[Uint] = VERY_LOW

OPCODE_PUSH0

207
    OPCODE_PUSH0: Final[Uint] = BASE

OPCODE_DUP

208
    OPCODE_DUP: Final[Uint] = VERY_LOW

OPCODE_SWAP

209
    OPCODE_SWAP: Final[Uint] = VERY_LOW

OPCODE_DUPN

210
    OPCODE_DUPN: Final[Uint] = VERY_LOW

OPCODE_SWAPN

211
    OPCODE_SWAPN: Final[Uint] = VERY_LOW

OPCODE_EXCHANGE

212
    OPCODE_EXCHANGE: Final[Uint] = VERY_LOW

OPCODE_TLOAD

213
    OPCODE_TLOAD: Final[Uint] = Uint(100)

OPCODE_TSTORE

214
    OPCODE_TSTORE: Final[Uint] = Uint(100)

OPCODE_RETURNDATACOPY_BASE

217
    OPCODE_RETURNDATACOPY_BASE: Final[Uint] = VERY_LOW

OPCODE_RETURNDATACOPY_PER_WORD

218
    OPCODE_RETURNDATACOPY_PER_WORD: Final[Uint] = Uint(3)

OPCODE_CALLDATACOPY_BASE

219
    OPCODE_CALLDATACOPY_BASE: Final[Uint] = VERY_LOW

OPCODE_CODECOPY_BASE

220
    OPCODE_CODECOPY_BASE: Final[Uint] = VERY_LOW

OPCODE_MCOPY_BASE

221
    OPCODE_MCOPY_BASE: Final[Uint] = VERY_LOW

OPCODE_MLOAD_BASE

222
    OPCODE_MLOAD_BASE: Final[Uint] = VERY_LOW

OPCODE_MSTORE_BASE

223
    OPCODE_MSTORE_BASE: Final[Uint] = VERY_LOW

OPCODE_MSTORE8_BASE

224
    OPCODE_MSTORE8_BASE: Final[Uint] = VERY_LOW

OPCODE_COPY_PER_WORD

225
    OPCODE_COPY_PER_WORD: Final[Uint] = Uint(3)

OPCODE_CREATE_BASE

180
    OPCODE_CREATE_BASE: Final[Uint] = Uint(32000)

OPCODE_EXP_BASE

226
    OPCODE_EXP_BASE: Final[Uint] = Uint(10)

OPCODE_EXP_PER_BYTE

227
    OPCODE_EXP_PER_BYTE: Final[Uint] = Uint(50)

OPCODE_KECCAK256_BASE

228
    OPCODE_KECCAK256_BASE: Final[Uint] = Uint(30)

OPCODE_KECCAK256_PER_WORD

229
    OPCODE_KECCAK256_PER_WORD: Final[Uint] = Uint(6)

OPCODE_LOG_BASE

230
    OPCODE_LOG_BASE: Final[Uint] = Uint(375)

OPCODE_LOG_DATA_PER_BYTE

231
    OPCODE_LOG_DATA_PER_BYTE: Final[Uint] = Uint(8)

OPCODE_LOG_TOPIC

232
    OPCODE_LOG_TOPIC: Final[Uint] = Uint(375)

OPCODE_SELFDESTRUCT_BASE

233
    OPCODE_SELFDESTRUCT_BASE: Final[Uint] = Uint(5000)

OPCODE_SELFDESTRUCT_NEW_ACCOUNT

189
    OPCODE_SELFDESTRUCT_NEW_ACCOUNT: Final[Uint] = 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

236
@final
237
@dataclass
class ExtendMemory:

cost

248
    cost: Uint

expand_by

249
    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.

252
@final
253
@dataclass
class MessageCallGas:

cost

267
    cost: Uint

sub_call

268
    sub_call: Uint

check_gas

Checks if amount gas is available without charging it. Raises OutOfGasError if insufficient gas.

Parameters

evm : The current EVM. amount : The amount of gas to check.

def check_gas(evm: Evm, ​​amount: Uint) -> None:
272
    <snip>
284
    if evm.gas_left < amount:
285
        raise OutOfGasError

charge_gas

Subtracts amount from evm.gas_left. (regular gas) and records usage.

Parameters

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

def charge_gas(evm: Evm, ​​amount: Uint) -> None:
289
    <snip>
300
    evm_trace(evm, GasAndRefund(int(amount)))
301
302
    if evm.gas_left < amount:
242
        raise OutOfGasError
243
    else:
244
        evm.gas_left -= amount
303
        raise OutOfGasError
304
    evm.gas_left -= amount
305
306
    evm.regular_gas_used += amount

charge_state_gas

Subtracts amount from the state gas reservoir, then from evm.gas_left when the reservoir is empty, tracking any spill.

Parameters

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

def charge_state_gas(evm: Evm, ​​amount: StateGas) -> None:
310
    <snip>
324
    evm_trace(evm, StateGasAndRefund(int(amount)))
325
326
    if evm.state_gas_left >= amount:
327
        evm.state_gas_left -= amount
328
    elif evm.state_gas_left + evm.gas_left >= amount:
329
        remainder = amount - evm.state_gas_left
330
        evm.state_gas_left = Uint(0)
331
        evm.gas_left -= remainder
332
        evm.state_gas_spilled += remainder
333
    else:
334
        raise OutOfGasError

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:
338
    <snip>
354
    size_in_words = ceil32(size_in_bytes) // Uint(32)
355
    linear_cost = size_in_words * GasCosts.MEMORY_PER_WORD
356
    quadratic_cost = size_in_words ** Uint(2) // Uint(512)
357
    total_gas_cost = linear_cost + quadratic_cost
358
    try:
359
        return total_gas_cost
360
    except ValueError as e:
361
        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:
367
    <snip>
383
    size_to_extend = Uint(0)
384
    to_be_paid = Uint(0)
385
    current_size = ulen(memory)
386
    for start_position, size in extensions:
387
        if size == 0:
388
            continue
389
        before_size = ceil32(current_size)
390
        after_size = ceil32(Uint(start_position) + Uint(size))
391
        if after_size <= before_size:
392
            continue
393
394
        size_to_extend += after_size - before_size
395
        already_paid = calculate_memory_gas_cost(before_size)
396
        total_cost = calculate_memory_gas_cost(after_size)
397
        to_be_paid += total_cost - already_paid
398
399
        current_size = after_size
400
401
    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:
412
    <snip>
438
    call_stipend = Uint(0) if value == 0 else call_stipend
439
    if gas_left < extra_gas + memory_cost:
440
        return MessageCallGas(gas + extra_gas, gas + call_stipend)
441
442
    gas = min(gas, max_message_call_gas(gas_left - memory_cost - extra_gas))
443
444
    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:
448
    <snip>
462
    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:
466
    <snip>
482
    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: HeaderHeader | PreviousHeader) -> U64:
488
    <snip>
503
    # At the fork block, these are defined as zero.
504
    excess_blob_gas = U64(0)
505
    blob_gas_used = U64(0)
506
    base_fee_per_gas = Uint(0)
507
508
    if isinstance(parent_header, Header):
509
        # After the fork block, read them from the parent header.
510
        excess_blob_gas = parent_header.excess_blob_gas
511
        blob_gas_used = parent_header.blob_gas_used
512
        base_fee_per_gas = parent_header.base_fee_per_gas
513
514
    parent_blob_gas = excess_blob_gas + blob_gas_used
515
    if parent_blob_gas < GasCosts.BLOB_TARGET_GAS_PER_BLOCK:
516
        return U64(0)
517
518
    target_blob_gas_price = Uint(GasCosts.PER_BLOB)
519
    target_blob_gas_price *= calculate_blob_gas_price(excess_blob_gas)
520
521
    base_blob_tx_price = GasCosts.BLOB_BASE_COST * base_fee_per_gas
522
    if base_blob_tx_price > target_blob_gas_price:
523
        blob_schedule_delta = (
524
            GasCosts.BLOB_SCHEDULE_MAX - GasCosts.BLOB_SCHEDULE_TARGET
525
        )
526
        return (
527
            excess_blob_gas
528
            + blob_gas_used * blob_schedule_delta // GasCosts.BLOB_SCHEDULE_MAX
529
        )
530
531
    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:
535
    <snip>
549
    if isinstance(tx, BlobTransaction):
550
        return GasCosts.PER_BLOB * U64(len(tx.blob_versioned_hashes))
551
    else:
552
        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:
556
    <snip>
570
    return taylor_exponential(
571
        GasCosts.BLOB_MIN_GASPRICE,
572
        Uint(excess_blob_gas),
573
        GasCosts.BLOB_BASE_FEE_UPDATE_FRACTION,
574
    )

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:
578
    <snip>
594
    return Uint(calculate_total_blob_gas(tx)) * calculate_blob_gas_price(
595
        excess_blob_gas
596
    )