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.
COST_PER_STATE_BYTE¶
| 29 | COST_PER_STATE_BYTE = Uint(1174) |
|---|
STATE_BYTES_PER_NEW_ACCOUNT¶
| 31 | STATE_BYTES_PER_NEW_ACCOUNT = Uint(112) |
|---|
STATE_BYTES_PER_STORAGE_SET¶
| 32 | STATE_BYTES_PER_STORAGE_SET = Uint(32) |
|---|
STATE_BYTES_PER_AUTH_BASE¶
| 33 | STATE_BYTES_PER_AUTH_BASE = Uint(23) |
|---|
PER_AUTH_BASE_COST¶
| 35 | PER_AUTH_BASE_COST = Uint(7500) |
|---|
REGULAR_GAS_CREATE¶
| 37 | REGULAR_GAS_CREATE = Uint(9000) |
|---|
GasCosts ¶
Constant gas values for the EVM.
class GasCosts:
BASE¶
| 47 | BASE = Uint(2) |
|---|
VERY_LOW¶
| 48 | VERY_LOW = Uint(3) |
|---|
LOW¶
| 49 | LOW = Uint(5) |
|---|
MID¶
| 50 | MID = Uint(8) |
|---|
HIGH¶
| 51 | HIGH = Uint(10) |
|---|
WARM_ACCESS¶
| 54 | WARM_ACCESS = Uint(100) |
|---|
COLD_ACCOUNT_ACCESS¶
| 55 | COLD_ACCOUNT_ACCESS = Uint(2600) |
|---|
COLD_STORAGE_ACCESS¶
| 56 | COLD_STORAGE_ACCESS = Uint(2100) |
|---|
STORAGE_SET¶
| 59 | STORAGE_SET = Uint(20000) |
|---|
COLD_STORAGE_WRITE¶
| 60 | COLD_STORAGE_WRITE = Uint(5000) |
|---|
CALL_VALUE¶
| 63 | CALL_VALUE = Uint(9000) |
|---|
CALL_STIPEND¶
| 64 | CALL_STIPEND = Uint(2300) |
|---|
NEW_ACCOUNT¶
| 65 | NEW_ACCOUNT = Uint(25000) |
|---|
CODE_DEPOSIT_PER_BYTE¶
| 68 | CODE_DEPOSIT_PER_BYTE = Uint(200) |
|---|
CODE_INIT_PER_WORD¶
| 69 | CODE_INIT_PER_WORD = Uint(2) |
|---|
AUTH_PER_EMPTY_ACCOUNT¶
| 72 | AUTH_PER_EMPTY_ACCOUNT = 25000 |
|---|
ZERO¶
| 75 | ZERO = Uint(0) |
|---|
MEMORY_PER_WORD¶
| 76 | MEMORY_PER_WORD = Uint(3) |
|---|
FAST_STEP¶
| 77 | FAST_STEP = Uint(5) |
|---|
REFUND_STORAGE_CLEAR¶
| 80 | REFUND_STORAGE_CLEAR = 4800 |
|---|
PRECOMPILE_ECRECOVER¶
| 83 | PRECOMPILE_ECRECOVER = Uint(3000) |
|---|
PRECOMPILE_P256VERIFY¶
| 84 | PRECOMPILE_P256VERIFY = Uint(6900) |
|---|
PRECOMPILE_SHA256_BASE¶
| 85 | PRECOMPILE_SHA256_BASE = Uint(60) |
|---|
PRECOMPILE_SHA256_PER_WORD¶
| 86 | PRECOMPILE_SHA256_PER_WORD = Uint(12) |
|---|
PRECOMPILE_RIPEMD160_BASE¶
| 87 | PRECOMPILE_RIPEMD160_BASE = Uint(600) |
|---|
PRECOMPILE_RIPEMD160_PER_WORD¶
| 88 | PRECOMPILE_RIPEMD160_PER_WORD = Uint(120) |
|---|
PRECOMPILE_IDENTITY_BASE¶
| 89 | PRECOMPILE_IDENTITY_BASE = Uint(15) |
|---|
PRECOMPILE_IDENTITY_PER_WORD¶
| 90 | PRECOMPILE_IDENTITY_PER_WORD = Uint(3) |
|---|
PRECOMPILE_BLAKE2F_PER_ROUND¶
| 91 | PRECOMPILE_BLAKE2F_PER_ROUND = Uint(1) |
|---|
PRECOMPILE_POINT_EVALUATION¶
| 92 | PRECOMPILE_POINT_EVALUATION = Uint(50000) |
|---|
PRECOMPILE_BLS_G1ADD¶
| 93 | PRECOMPILE_BLS_G1ADD = Uint(375) |
|---|
PRECOMPILE_BLS_G1MUL¶
| 94 | PRECOMPILE_BLS_G1MUL = Uint(12000) |
|---|
PRECOMPILE_BLS_G1MAP¶
| 95 | PRECOMPILE_BLS_G1MAP = Uint(5500) |
|---|
PRECOMPILE_BLS_G2ADD¶
| 96 | PRECOMPILE_BLS_G2ADD = Uint(600) |
|---|
PRECOMPILE_BLS_G2MUL¶
| 97 | PRECOMPILE_BLS_G2MUL = Uint(22500) |
|---|
PRECOMPILE_BLS_G2MAP¶
| 98 | PRECOMPILE_BLS_G2MAP = Uint(23800) |
|---|
PRECOMPILE_ECADD¶
| 99 | PRECOMPILE_ECADD = Uint(150) |
|---|
PRECOMPILE_ECMUL¶
| 100 | PRECOMPILE_ECMUL = Uint(6000) |
|---|
PRECOMPILE_ECPAIRING_BASE¶
| 101 | PRECOMPILE_ECPAIRING_BASE = Uint(45000) |
|---|
PRECOMPILE_ECPAIRING_PER_POINT¶
| 102 | PRECOMPILE_ECPAIRING_PER_POINT = Uint(34000) |
|---|
PER_BLOB¶
| 105 | PER_BLOB = U64(2**17) |
|---|
BLOB_SCHEDULE_TARGET¶
| 106 | BLOB_SCHEDULE_TARGET = U64(14) |
|---|
BLOB_TARGET_GAS_PER_BLOCK¶
| 107 | BLOB_TARGET_GAS_PER_BLOCK = PER_BLOB * BLOB_SCHEDULE_TARGET |
|---|
BLOB_BASE_COST¶
| 108 | BLOB_BASE_COST = Uint(2**13) |
|---|
BLOB_SCHEDULE_MAX¶
| 109 | BLOB_SCHEDULE_MAX = U64(21) |
|---|
BLOB_MIN_GASPRICE¶
| 110 | BLOB_MIN_GASPRICE = Uint(1) |
|---|
BLOB_BASE_FEE_UPDATE_FRACTION¶
| 111 | BLOB_BASE_FEE_UPDATE_FRACTION = Uint(11684671) |
|---|
BLOCK_ACCESS_LIST_ITEM¶
| 114 | BLOCK_ACCESS_LIST_ITEM = Uint(2000) |
|---|
TX_BASE¶
| 117 | TX_BASE = Uint(21000) |
|---|
TX_CREATE¶
| 118 | TX_CREATE = Uint(32000) |
|---|
TX_DATA_TOKEN_STANDARD¶
| 119 | TX_DATA_TOKEN_STANDARD = Uint(4) |
|---|
TX_DATA_TOKEN_FLOOR¶
| 105 | TX_DATA_TOKEN_FLOOR = Uint(10) |
|---|---|
| 120 | TX_DATA_TOKEN_FLOOR = Uint(16) |
TX_ACCESS_LIST_ADDRESS¶
| 121 | TX_ACCESS_LIST_ADDRESS = Uint(2400) |
|---|
TX_ACCESS_LIST_STORAGE_KEY¶
| 122 | TX_ACCESS_LIST_STORAGE_KEY = Uint(1900) |
|---|
LIMIT_ADJUSTMENT_FACTOR¶
| 125 | LIMIT_ADJUSTMENT_FACTOR = Uint(1024) |
|---|
LIMIT_MINIMUM¶
| 126 | LIMIT_MINIMUM = Uint(5000) |
|---|
OPCODE_ADD¶
| 129 | OPCODE_ADD = VERY_LOW |
|---|
OPCODE_SUB¶
| 130 | OPCODE_SUB = VERY_LOW |
|---|
OPCODE_MUL¶
| 131 | OPCODE_MUL = LOW |
|---|
OPCODE_DIV¶
| 132 | OPCODE_DIV = LOW |
|---|
OPCODE_SDIV¶
| 133 | OPCODE_SDIV = LOW |
|---|
OPCODE_MOD¶
| 134 | OPCODE_MOD = LOW |
|---|
OPCODE_SMOD¶
| 135 | OPCODE_SMOD = LOW |
|---|
OPCODE_ADDMOD¶
| 136 | OPCODE_ADDMOD = MID |
|---|
OPCODE_MULMOD¶
| 137 | OPCODE_MULMOD = MID |
|---|
OPCODE_SIGNEXTEND¶
| 138 | OPCODE_SIGNEXTEND = LOW |
|---|
OPCODE_LT¶
| 139 | OPCODE_LT = VERY_LOW |
|---|
OPCODE_GT¶
| 140 | OPCODE_GT = VERY_LOW |
|---|
OPCODE_SLT¶
| 141 | OPCODE_SLT = VERY_LOW |
|---|
OPCODE_SGT¶
| 142 | OPCODE_SGT = VERY_LOW |
|---|
OPCODE_EQ¶
| 143 | OPCODE_EQ = VERY_LOW |
|---|
OPCODE_ISZERO¶
| 144 | OPCODE_ISZERO = VERY_LOW |
|---|
OPCODE_AND¶
| 145 | OPCODE_AND = VERY_LOW |
|---|
OPCODE_OR¶
| 146 | OPCODE_OR = VERY_LOW |
|---|
OPCODE_XOR¶
| 147 | OPCODE_XOR = VERY_LOW |
|---|
OPCODE_NOT¶
| 148 | OPCODE_NOT = VERY_LOW |
|---|
OPCODE_BYTE¶
| 149 | OPCODE_BYTE = VERY_LOW |
|---|
OPCODE_SHL¶
| 150 | OPCODE_SHL = VERY_LOW |
|---|
OPCODE_SHR¶
| 151 | OPCODE_SHR = VERY_LOW |
|---|
OPCODE_SAR¶
| 152 | OPCODE_SAR = VERY_LOW |
|---|
OPCODE_CLZ¶
| 153 | OPCODE_CLZ = LOW |
|---|
OPCODE_JUMP¶
| 154 | OPCODE_JUMP = MID |
|---|
OPCODE_JUMPI¶
| 155 | OPCODE_JUMPI = HIGH |
|---|
OPCODE_JUMPDEST¶
| 156 | OPCODE_JUMPDEST = Uint(1) |
|---|
OPCODE_CALLDATALOAD¶
| 157 | OPCODE_CALLDATALOAD = VERY_LOW |
|---|
OPCODE_BLOCKHASH¶
| 158 | OPCODE_BLOCKHASH = Uint(20) |
|---|
OPCODE_COINBASE¶
| 159 | OPCODE_COINBASE = BASE |
|---|
OPCODE_POP¶
| 160 | OPCODE_POP = BASE |
|---|
OPCODE_MSIZE¶
| 161 | OPCODE_MSIZE = BASE |
|---|
OPCODE_PC¶
| 162 | OPCODE_PC = BASE |
|---|
OPCODE_GAS¶
| 163 | OPCODE_GAS = BASE |
|---|
OPCODE_ADDRESS¶
| 164 | OPCODE_ADDRESS = BASE |
|---|
OPCODE_ORIGIN¶
| 165 | OPCODE_ORIGIN = BASE |
|---|
OPCODE_CALLER¶
| 166 | OPCODE_CALLER = BASE |
|---|
OPCODE_CALLVALUE¶
| 167 | OPCODE_CALLVALUE = BASE |
|---|
OPCODE_CALLDATASIZE¶
| 168 | OPCODE_CALLDATASIZE = BASE |
|---|
OPCODE_CODESIZE¶
| 169 | OPCODE_CODESIZE = BASE |
|---|
OPCODE_GASPRICE¶
| 170 | OPCODE_GASPRICE = BASE |
|---|
OPCODE_TIMESTAMP¶
| 171 | OPCODE_TIMESTAMP = BASE |
|---|
OPCODE_NUMBER¶
| 172 | OPCODE_NUMBER = BASE |
|---|
OPCODE_GASLIMIT¶
| 173 | OPCODE_GASLIMIT = BASE |
|---|
OPCODE_PREVRANDAO¶
| 174 | OPCODE_PREVRANDAO = BASE |
|---|
OPCODE_RETURNDATASIZE¶
| 175 | OPCODE_RETURNDATASIZE = BASE |
|---|
OPCODE_CHAINID¶
| 176 | OPCODE_CHAINID = BASE |
|---|
OPCODE_BASEFEE¶
| 177 | OPCODE_BASEFEE = BASE |
|---|
OPCODE_BLOBBASEFEE¶
| 178 | OPCODE_BLOBBASEFEE = BASE |
|---|
OPCODE_SLOTNUM¶
| 179 | OPCODE_SLOTNUM = BASE |
|---|
OPCODE_BLOBHASH¶
| 180 | OPCODE_BLOBHASH = Uint(3) |
|---|
OPCODE_PUSH¶
| 181 | OPCODE_PUSH = VERY_LOW |
|---|
OPCODE_PUSH0¶
| 182 | OPCODE_PUSH0 = BASE |
|---|
OPCODE_DUP¶
| 183 | OPCODE_DUP = VERY_LOW |
|---|
OPCODE_SWAP¶
| 184 | OPCODE_SWAP = VERY_LOW |
|---|
OPCODE_DUPN¶
| 185 | OPCODE_DUPN = VERY_LOW |
|---|
OPCODE_SWAPN¶
| 186 | OPCODE_SWAPN = VERY_LOW |
|---|
OPCODE_EXCHANGE¶
| 187 | OPCODE_EXCHANGE = VERY_LOW |
|---|
OPCODE_RETURNDATACOPY_BASE¶
| 190 | OPCODE_RETURNDATACOPY_BASE = VERY_LOW |
|---|
OPCODE_RETURNDATACOPY_PER_WORD¶
| 191 | OPCODE_RETURNDATACOPY_PER_WORD = Uint(3) |
|---|
OPCODE_CALLDATACOPY_BASE¶
| 192 | OPCODE_CALLDATACOPY_BASE = VERY_LOW |
|---|
OPCODE_CODECOPY_BASE¶
| 193 | OPCODE_CODECOPY_BASE = VERY_LOW |
|---|
OPCODE_MCOPY_BASE¶
| 194 | OPCODE_MCOPY_BASE = VERY_LOW |
|---|
OPCODE_MLOAD_BASE¶
| 195 | OPCODE_MLOAD_BASE = VERY_LOW |
|---|
OPCODE_MSTORE_BASE¶
| 196 | OPCODE_MSTORE_BASE = VERY_LOW |
|---|
OPCODE_MSTORE8_BASE¶
| 197 | OPCODE_MSTORE8_BASE = VERY_LOW |
|---|
OPCODE_COPY_PER_WORD¶
| 198 | OPCODE_COPY_PER_WORD = Uint(3) |
|---|
OPCODE_CREATE_BASE¶
| 199 | OPCODE_CREATE_BASE = Uint(32000) |
|---|
OPCODE_EXP_BASE¶
| 200 | OPCODE_EXP_BASE = Uint(10) |
|---|
OPCODE_EXP_PER_BYTE¶
| 201 | OPCODE_EXP_PER_BYTE = Uint(50) |
|---|
OPCODE_KECCAK256_BASE¶
| 202 | OPCODE_KECCAK256_BASE = Uint(30) |
|---|
OPCODE_KECCACK256_PER_WORD¶
| 203 | OPCODE_KECCACK256_PER_WORD = Uint(6) |
|---|
OPCODE_LOG_BASE¶
| 204 | OPCODE_LOG_BASE = Uint(375) |
|---|
OPCODE_LOG_DATA_PER_BYTE¶
| 205 | OPCODE_LOG_DATA_PER_BYTE = Uint(8) |
|---|
OPCODE_LOG_TOPIC¶
| 206 | OPCODE_LOG_TOPIC = Uint(375) |
|---|
OPCODE_SELFDESTRUCT_BASE¶
| 207 | OPCODE_SELFDESTRUCT_BASE = Uint(5000) |
|---|
OPCODE_SELFDESTRUCT_NEW_ACCOUNT¶
| 208 | 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
| 211 | @dataclass |
|---|
class ExtendMemory:
cost¶
| 222 | cost: Uint |
|---|
expand_by¶
| 223 | 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.
| 226 | @dataclass |
|---|
class MessageCallGas:
cost¶
| 240 | cost: Uint |
|---|
sub_call¶
| 241 | 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:
| 245 | """ |
|---|---|
| 246 | Checks if `amount` gas is available without charging it. |
| 247 | Raises OutOfGasError if insufficient gas. |
| 248 | |
| 249 | Parameters |
| 250 | ---------- |
| 251 | evm : |
| 252 | The current EVM. |
| 253 | amount : |
| 254 | The amount of gas to check. |
| 255 | |
| 256 | """ |
| 257 | if evm.gas_left < amount: |
| 258 | 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:
| 262 | """ |
|---|---|
| 227 | Subtracts `amount` from `evm.gas_left`. |
| 263 | Subtracts `amount` from `evm.gas_left` (regular gas) and records usage. |
| 264 | |
| 265 | Parameters |
| 266 | ---------- |
| 267 | evm : |
| 268 | The current EVM. |
| 269 | amount : |
| 234 | The amount of gas the current operation requires. |
| 270 | The amount of regular gas the current operation requires. |
| 271 | |
| 272 | """ |
| 273 | evm_trace(evm, GasAndRefund(int(amount))) |
| 274 | |
| 275 | if evm.gas_left < amount: |
| 240 | raise OutOfGasError |
| 241 | else: |
| 242 | evm.gas_left -= amount |
| 276 | raise OutOfGasError |
| 277 | evm.gas_left -= amount |
| 278 | |
| 279 | 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. Records state gas usage.
Parameters
evm : The current EVM. amount : The amount of state gas the current operation requires.
def charge_state_gas(evm: Evm, amount: Uint) -> None:
| 283 | """ |
|---|---|
| 284 | Subtracts `amount` from the state gas reservoir, then from |
| 285 | `evm.gas_left` when the reservoir is empty. Records state gas usage. |
| 286 | |
| 287 | Parameters |
| 288 | ---------- |
| 289 | evm : |
| 290 | The current EVM. |
| 291 | amount : |
| 292 | The amount of state gas the current operation requires. |
| 293 | |
| 294 | """ |
| 295 | evm_trace(evm, StateGasAndRefund(int(amount))) |
| 296 | |
| 297 | if evm.state_gas_left >= amount: |
| 298 | evm.state_gas_left -= amount |
| 299 | elif evm.state_gas_left + evm.gas_left >= amount: |
| 300 | remainder = amount - evm.state_gas_left |
| 301 | evm.state_gas_left = Uint(0) |
| 302 | evm.gas_left -= remainder |
| 303 | else: |
| 304 | raise OutOfGasError |
| 305 | |
| 306 | evm.state_gas_used += 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:
| 310 | """ |
|---|---|
| 311 | Calculates the gas cost for allocating memory |
| 312 | to the smallest multiple of 32 bytes, |
| 313 | such that the allocated size is at least as big as the given size. |
| 314 | |
| 315 | Parameters |
| 316 | ---------- |
| 317 | size_in_bytes : |
| 318 | The size of the data in bytes. |
| 319 | |
| 320 | Returns |
| 321 | ------- |
| 322 | total_gas_cost : `ethereum.base_types.Uint` |
| 323 | The gas cost for storing data in memory. |
| 324 | |
| 325 | """ |
| 326 | size_in_words = ceil32(size_in_bytes) // Uint(32) |
| 327 | linear_cost = size_in_words * GasCosts.MEMORY_PER_WORD |
| 328 | quadratic_cost = size_in_words ** Uint(2) // Uint(512) |
| 329 | total_gas_cost = linear_cost + quadratic_cost |
| 330 | try: |
| 331 | return total_gas_cost |
| 332 | except ValueError as e: |
| 333 | 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:
| 339 | """ |
|---|---|
| 340 | Calculates the gas amount to extend memory. |
| 341 | |
| 342 | Parameters |
| 343 | ---------- |
| 344 | memory : |
| 345 | Memory contents of the EVM. |
| 346 | extensions: |
| 347 | List of extensions to be made to the memory. |
| 348 | Consists of a tuple of start position and size. |
| 349 | |
| 350 | Returns |
| 351 | ------- |
| 352 | extend_memory: `ExtendMemory` |
| 353 | |
| 354 | """ |
| 355 | size_to_extend = Uint(0) |
| 356 | to_be_paid = Uint(0) |
| 357 | current_size = ulen(memory) |
| 358 | for start_position, size in extensions: |
| 359 | if size == 0: |
| 360 | continue |
| 361 | before_size = ceil32(current_size) |
| 362 | after_size = ceil32(Uint(start_position) + Uint(size)) |
| 363 | if after_size <= before_size: |
| 364 | continue |
| 365 | |
| 366 | size_to_extend += after_size - before_size |
| 367 | already_paid = calculate_memory_gas_cost(before_size) |
| 368 | total_cost = calculate_memory_gas_cost(after_size) |
| 369 | to_be_paid += total_cost - already_paid |
| 370 | |
| 371 | current_size = after_size |
| 372 | |
| 373 | 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:
| 384 | """ |
|---|---|
| 385 | Calculates the MessageCallGas (cost and gas made available to the sub-call) |
| 386 | for executing call Opcodes. |
| 387 | |
| 388 | Parameters |
| 389 | ---------- |
| 390 | value: |
| 391 | The amount of `ETH` that needs to be transferred. |
| 392 | gas : |
| 393 | The amount of gas provided to the message-call. |
| 394 | gas_left : |
| 395 | The amount of gas left in the current frame. |
| 396 | memory_cost : |
| 397 | The amount needed to extend the memory in the current frame. |
| 398 | extra_gas : |
| 399 | The amount of gas needed for transferring value + creating a new |
| 400 | account inside a message call. |
| 401 | call_stipend : |
| 402 | The amount of stipend provided to a message call to execute code while |
| 403 | transferring value (ETH). |
| 404 | |
| 405 | Returns |
| 406 | ------- |
| 407 | message_call_gas: `MessageCallGas` |
| 408 | |
| 409 | """ |
| 410 | call_stipend = Uint(0) if value == 0 else call_stipend |
| 411 | if gas_left < extra_gas + memory_cost: |
| 412 | return MessageCallGas(gas + extra_gas, gas + call_stipend) |
| 413 | |
| 414 | gas = min(gas, max_message_call_gas(gas_left - memory_cost - extra_gas)) |
| 415 | |
| 416 | 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:
| 420 | """ |
|---|---|
| 421 | Calculates the maximum gas that is allowed for making a message call. |
| 422 | |
| 423 | Parameters |
| 424 | ---------- |
| 425 | gas : |
| 426 | The amount of gas provided to the message-call. |
| 427 | |
| 428 | Returns |
| 429 | ------- |
| 430 | max_allowed_message_call_gas: `ethereum.base_types.Uint` |
| 431 | The maximum gas allowed for making the message-call. |
| 432 | |
| 433 | """ |
| 434 | 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:
| 438 | """ |
|---|---|
| 439 | Calculates the gas to be charged for the init code in CREATE* |
| 440 | opcodes as well as create transactions. |
| 441 | |
| 442 | Parameters |
| 443 | ---------- |
| 444 | init_code_length : |
| 445 | The length of the init code provided to the opcode |
| 446 | or a create transaction |
| 447 | |
| 448 | Returns |
| 449 | ------- |
| 450 | init_code_gas: `ethereum.base_types.Uint` |
| 451 | The gas to be charged for the init code. |
| 452 | |
| 453 | """ |
| 454 | 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:
| 460 | """ |
|---|---|
| 461 | Calculates the excess blob gas for the current block based |
| 462 | on the gas used in the parent block. |
| 463 | |
| 464 | Parameters |
| 465 | ---------- |
| 466 | parent_header : |
| 467 | The parent block of the current block. |
| 468 | |
| 469 | Returns |
| 470 | ------- |
| 471 | excess_blob_gas: `ethereum.base_types.U64` |
| 472 | The excess blob gas for the current block. |
| 473 | |
| 474 | """ |
| 475 | # At the fork block, these are defined as zero. |
| 476 | excess_blob_gas = U64(0) |
| 477 | blob_gas_used = U64(0) |
| 478 | base_fee_per_gas = Uint(0) |
| 479 | |
| 480 | if isinstance(parent_header, Header): |
| 481 | # After the fork block, read them from the parent header. |
| 482 | excess_blob_gas = parent_header.excess_blob_gas |
| 483 | blob_gas_used = parent_header.blob_gas_used |
| 484 | base_fee_per_gas = parent_header.base_fee_per_gas |
| 485 | |
| 486 | parent_blob_gas = excess_blob_gas + blob_gas_used |
| 487 | if parent_blob_gas < GasCosts.BLOB_TARGET_GAS_PER_BLOCK: |
| 488 | return U64(0) |
| 489 | |
| 490 | target_blob_gas_price = Uint(GasCosts.PER_BLOB) |
| 491 | target_blob_gas_price *= calculate_blob_gas_price(excess_blob_gas) |
| 492 | |
| 493 | base_blob_tx_price = GasCosts.BLOB_BASE_COST * base_fee_per_gas |
| 494 | if base_blob_tx_price > target_blob_gas_price: |
| 495 | blob_schedule_delta = ( |
| 496 | GasCosts.BLOB_SCHEDULE_MAX - GasCosts.BLOB_SCHEDULE_TARGET |
| 497 | ) |
| 498 | return ( |
| 499 | excess_blob_gas |
| 500 | + blob_gas_used * blob_schedule_delta // GasCosts.BLOB_SCHEDULE_MAX |
| 501 | ) |
| 502 | |
| 503 | 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:
| 507 | """ |
|---|---|
| 508 | Calculate the total blob gas for a transaction. |
| 509 | |
| 510 | Parameters |
| 511 | ---------- |
| 512 | tx : |
| 513 | The transaction for which the blob gas is to be calculated. |
| 514 | |
| 515 | Returns |
| 516 | ------- |
| 517 | total_blob_gas: `ethereum.base_types.Uint` |
| 518 | The total blob gas for the transaction. |
| 519 | |
| 520 | """ |
| 521 | if isinstance(tx, BlobTransaction): |
| 522 | return GasCosts.PER_BLOB * U64(len(tx.blob_versioned_hashes)) |
| 523 | else: |
| 524 | 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:
| 528 | """ |
|---|---|
| 529 | Calculate the blob gasprice for a block. |
| 530 | |
| 531 | Parameters |
| 532 | ---------- |
| 533 | excess_blob_gas : |
| 534 | The excess blob gas for the block. |
| 535 | |
| 536 | Returns |
| 537 | ------- |
| 538 | blob_gasprice: `Uint` |
| 539 | The blob gasprice. |
| 540 | |
| 541 | """ |
| 542 | return taylor_exponential( |
| 543 | GasCosts.BLOB_MIN_GASPRICE, |
| 544 | Uint(excess_blob_gas), |
| 545 | GasCosts.BLOB_BASE_FEE_UPDATE_FRACTION, |
| 546 | ) |
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:
| 550 | """ |
|---|---|
| 551 | Calculate the blob data fee for a transaction. |
| 552 | |
| 553 | Parameters |
| 554 | ---------- |
| 555 | excess_blob_gas : |
| 556 | The excess_blob_gas for the execution. |
| 557 | tx : |
| 558 | The transaction for which the blob data fee is to be calculated. |
| 559 | |
| 560 | Returns |
| 561 | ------- |
| 562 | data_fee: `Uint` |
| 563 | The blob data fee. |
| 564 | |
| 565 | """ |
| 566 | return Uint(calculate_total_blob_gas(tx)) * calculate_blob_gas_price( |
| 567 | excess_blob_gas |
| 568 | ) |