ethereum.forks.bpo5.vm.instructions.systemethereum.forks.amsterdam.vm.instructions.system
Ethereum Virtual Machine (EVM) System Instructions.
.. contents:: Table of Contents :backlinks: none :local:
Introduction
Implementations of the EVM system related instructions.
generic_create ¶
Core logic used by the CREATE* family of opcodes.
def generic_create(evm: Evm, endowment: U256, contract_address: Address, memory_start_position: U256, memory_size: U256) -> None:
| 73 | """ |
|---|---|
| 74 | Core logic used by the `CREATE*` family of opcodes. |
| 75 | """ |
| 76 | # This import causes a circular import error |
| 77 | # if it's not moved inside this method |
| 78 | from ...vm.interpreter import ( |
| 79 | MAX_INIT_CODE_SIZE, |
| 80 | STACK_DEPTH_LIMIT, |
| 81 | process_create_message, |
| 82 | ) |
| 83 | |
| 84 | # Check max init code size early before memory read |
| 85 | if memory_size > U256(MAX_INIT_CODE_SIZE): |
| 86 | raise OutOfGasError |
| 87 | |
| 88 | # Charge state gas for account creation (pay-before-execute). |
| 89 | # Refunded to the reservoir on any failure path below. |
| 90 | create_account_state_gas = ( |
| 91 | STATE_BYTES_PER_NEW_ACCOUNT * COST_PER_STATE_BYTE |
| 92 | ) |
| 93 | charge_state_gas(evm, create_account_state_gas) |
| 94 | |
| 95 | tx_state = evm.message.tx_env.state |
| 96 | |
| 97 | call_data = memory_read_bytes( |
| 98 | evm.memory, memory_start_position, memory_size |
| 99 | ) |
| 75 | if len(call_data) > MAX_INIT_CODE_SIZE: |
| 76 | raise OutOfGasError |
| 100 | |
| 101 | create_message_gas = max_message_call_gas(Uint(evm.gas_left)) |
| 102 | evm.gas_left -= create_message_gas |
| 80 | if evm.message.is_static: |
| 81 | raise WriteInStaticContext |
| 103 | |
| 104 | # Pass full reservoir to child (no 63/64 rule for state gas) |
| 105 | create_message_state_gas_reservoir = evm.state_gas_left |
| 106 | evm.state_gas_left = Uint(0) |
| 107 | |
| 108 | evm.return_data = b"" |
| 109 | |
| 110 | sender_address = evm.message.current_target |
| 85 | sender = get_account(evm.message.block_env.state, sender_address) |
| 111 | sender = get_account(tx_state, sender_address) |
| 112 | |
| 113 | if ( |
| 114 | sender.balance < endowment |
| 115 | or sender.nonce == Uint(2**64 - 1) |
| 116 | or evm.message.depth + Uint(1) > STACK_DEPTH_LIMIT |
| 117 | ): |
| 118 | evm.gas_left += create_message_gas |
| 119 | evm.state_gas_left += create_message_state_gas_reservoir |
| 120 | # No account created — refund state gas to reservoir. |
| 121 | credit_state_gas_refund(evm, create_account_state_gas) |
| 122 | push(evm.stack, U256(0)) |
| 123 | return |
| 124 | |
| 125 | evm.accessed_addresses.add(contract_address) |
| 126 | |
| 98 | if account_has_code_or_nonce( |
| 99 | evm.message.block_env.state, contract_address |
| 100 | ) or account_has_storage(evm.message.block_env.state, contract_address): |
| 101 | increment_nonce( |
| 102 | evm.message.block_env.state, evm.message.current_target |
| 103 | ) |
| 127 | if account_has_code_or_nonce( |
| 128 | tx_state, contract_address |
| 129 | ) or account_has_storage(tx_state, contract_address): |
| 130 | increment_nonce(tx_state, evm.message.current_target) |
| 131 | evm.regular_gas_used += create_message_gas |
| 132 | evm.state_gas_left += create_message_state_gas_reservoir |
| 133 | # Address collision — no account created, refund state gas. |
| 134 | credit_state_gas_refund(evm, create_account_state_gas) |
| 135 | push(evm.stack, U256(0)) |
| 136 | return |
| 137 | |
| 107 | increment_nonce(evm.message.block_env.state, evm.message.current_target) |
| 138 | increment_nonce(tx_state, evm.message.current_target) |
| 139 | |
| 140 | child_message = Message( |
| 141 | block_env=evm.message.block_env, |
| 142 | tx_env=evm.message.tx_env, |
| 143 | caller=evm.message.current_target, |
| 144 | target=Bytes0(), |
| 145 | gas=create_message_gas, |
| 146 | state_gas_reservoir=create_message_state_gas_reservoir, |
| 147 | value=endowment, |
| 148 | data=b"", |
| 149 | code=call_data, |
| 150 | current_target=contract_address, |
| 151 | depth=evm.message.depth + Uint(1), |
| 152 | code_address=None, |
| 153 | should_transfer_value=True, |
| 154 | is_static=False, |
| 155 | accessed_addresses=evm.accessed_addresses.copy(), |
| 156 | accessed_storage_keys=evm.accessed_storage_keys.copy(), |
| 157 | disable_precompiles=False, |
| 158 | parent_evm=evm, |
| 159 | ) |
| 160 | child_evm = process_create_message(child_message) |
| 161 | |
| 162 | if child_evm.error: |
| 163 | incorporate_child_on_error(evm, child_evm) |
| 164 | # No account created, refund parent's CREATE state gas. |
| 165 | credit_state_gas_refund(evm, create_account_state_gas) |
| 166 | evm.return_data = child_evm.output |
| 167 | push(evm.stack, U256(0)) |
| 168 | else: |
| 169 | incorporate_child_on_success(evm, child_evm) |
| 170 | evm.return_data = b"" |
| 171 | push(evm.stack, U256.from_be_bytes(child_evm.message.current_target)) |
create ¶
Creates a new account with associated code.
Parameters
evm : The current EVM frame.
def create(evm: Evm) -> None:
| 175 | """ |
|---|---|
| 176 | Creates a new account with associated code. |
| 177 | |
| 178 | Parameters |
| 179 | ---------- |
| 180 | evm : |
| 181 | The current EVM frame. |
| 182 | |
| 183 | """ |
| 184 | if evm.message.is_static: |
| 185 | raise WriteInStaticContext |
| 186 | |
| 187 | # STACK |
| 188 | endowment = pop(evm.stack) |
| 189 | memory_start_position = pop(evm.stack) |
| 190 | memory_size = pop(evm.stack) |
| 191 | |
| 192 | # GAS |
| 193 | extend_memory = calculate_gas_extend_memory( |
| 194 | evm.memory, [(memory_start_position, memory_size)] |
| 195 | ) |
| 196 | init_code_gas = init_code_cost(Uint(memory_size)) |
| 160 | |
| 161 | charge_gas( |
| 162 | evm, |
| 163 | GasCosts.OPCODE_CREATE_BASE + extend_memory.cost + init_code_gas, |
| 164 | ) |
| 197 | charge_gas(evm, REGULAR_GAS_CREATE + extend_memory.cost + init_code_gas) |
| 198 | |
| 199 | # OPERATION |
| 200 | evm.memory += b"\x00" * extend_memory.expand_by |
| 201 | contract_address = compute_contract_address( |
| 202 | evm.message.current_target, |
| 170 | get_account( |
| 171 | evm.message.block_env.state, evm.message.current_target |
| 203 | get_account( |
| 204 | evm.message.tx_env.state, evm.message.current_target |
| 205 | ).nonce, |
| 206 | ) |
| 207 | |
| 208 | generic_create( |
| 209 | evm, |
| 210 | endowment, |
| 211 | contract_address, |
| 212 | memory_start_position, |
| 213 | memory_size, |
| 214 | ) |
| 215 | |
| 216 | # PROGRAM COUNTER |
| 217 | evm.pc += Uint(1) |
create2 ¶
Creates a new account with associated code.
It's similar to the CREATE opcode except that the address of the new account depends on the init_code instead of the nonce of sender.
Parameters
evm : The current EVM frame.
def create2(evm: Evm) -> None:
| 221 | """ |
|---|---|
| 222 | Creates a new account with associated code. |
| 223 | |
| 224 | It's similar to the CREATE opcode except that the address of the new |
| 225 | account depends on the init_code instead of the nonce of sender. |
| 226 | |
| 227 | Parameters |
| 228 | ---------- |
| 229 | evm : |
| 230 | The current EVM frame. |
| 231 | |
| 232 | """ |
| 233 | if evm.message.is_static: |
| 234 | raise WriteInStaticContext |
| 235 | |
| 236 | # STACK |
| 237 | endowment = pop(evm.stack) |
| 238 | memory_start_position = pop(evm.stack) |
| 239 | memory_size = pop(evm.stack) |
| 240 | salt = pop(evm.stack).to_be_bytes32() |
| 241 | |
| 242 | # GAS |
| 243 | extend_memory = calculate_gas_extend_memory( |
| 244 | evm.memory, [(memory_start_position, memory_size)] |
| 245 | ) |
| 246 | call_data_words = ceil32(Uint(memory_size)) // Uint(32) |
| 247 | init_code_gas = init_code_cost(Uint(memory_size)) |
| 248 | charge_gas( |
| 249 | evm, |
| 214 | GasCosts.OPCODE_CREATE_BASE |
| 250 | REGULAR_GAS_CREATE |
| 251 | + GasCosts.OPCODE_KECCACK256_PER_WORD * call_data_words |
| 252 | + extend_memory.cost |
| 253 | + init_code_gas, |
| 254 | ) |
| 255 | |
| 256 | # OPERATION |
| 257 | evm.memory += b"\x00" * extend_memory.expand_by |
| 258 | contract_address = compute_create2_contract_address( |
| 259 | evm.message.current_target, |
| 260 | salt, |
| 261 | memory_read_bytes(evm.memory, memory_start_position, memory_size), |
| 262 | ) |
| 263 | |
| 264 | generic_create( |
| 265 | evm, |
| 266 | endowment, |
| 267 | contract_address, |
| 268 | memory_start_position, |
| 269 | memory_size, |
| 270 | ) |
| 271 | |
| 272 | # PROGRAM COUNTER |
| 273 | evm.pc += Uint(1) |
return_ ¶
Halts execution returning output data.
Parameters
evm : The current EVM frame.
def return_(evm: Evm) -> None:
| 277 | """ |
|---|---|
| 278 | Halts execution returning output data. |
| 279 | |
| 280 | Parameters |
| 281 | ---------- |
| 282 | evm : |
| 283 | The current EVM frame. |
| 284 | |
| 285 | """ |
| 286 | # STACK |
| 287 | memory_start_position = pop(evm.stack) |
| 288 | memory_size = pop(evm.stack) |
| 289 | |
| 290 | # GAS |
| 291 | extend_memory = calculate_gas_extend_memory( |
| 292 | evm.memory, [(memory_start_position, memory_size)] |
| 293 | ) |
| 294 | |
| 295 | charge_gas(evm, GasCosts.ZERO + extend_memory.cost) |
| 296 | |
| 297 | # OPERATION |
| 298 | evm.memory += b"\x00" * extend_memory.expand_by |
| 299 | evm.output = memory_read_bytes( |
| 300 | evm.memory, memory_start_position, memory_size |
| 301 | ) |
| 302 | |
| 303 | evm.running = False |
| 304 | |
| 305 | # PROGRAM COUNTER |
| 306 | pass |
generic_call ¶
Perform the core logic of the CALL* family of opcodes.
def generic_call(evm: Evm, gas: Uint, state_gas_reservoir: Uint, value: U256, caller: Address, to: Address, code_address: Address, should_transfer_value: bool, is_staticcall: bool, memory_input_start_position: U256, memory_input_size: U256, memory_output_start_position: U256, memory_output_size: U256, code: Bytes, disable_precompiles: bool) -> None:
| 326 | """ |
|---|---|
| 327 | Perform the core logic of the `CALL*` family of opcodes. |
| 328 | """ |
| 329 | from ...vm.interpreter import STACK_DEPTH_LIMIT, process_message |
| 330 | |
| 331 | evm.return_data = b"" |
| 332 | |
| 333 | if evm.message.depth + Uint(1) > STACK_DEPTH_LIMIT: |
| 334 | evm.gas_left += gas |
| 335 | evm.state_gas_left += state_gas_reservoir |
| 336 | push(evm.stack, U256(0)) |
| 337 | return |
| 338 | |
| 339 | call_data = memory_read_bytes( |
| 340 | evm.memory, memory_input_start_position, memory_input_size |
| 341 | ) |
| 342 | |
| 343 | child_message = Message( |
| 344 | block_env=evm.message.block_env, |
| 345 | tx_env=evm.message.tx_env, |
| 346 | caller=caller, |
| 347 | target=to, |
| 348 | gas=gas, |
| 349 | state_gas_reservoir=state_gas_reservoir, |
| 350 | value=value, |
| 351 | data=call_data, |
| 352 | code=code, |
| 353 | current_target=to, |
| 354 | depth=evm.message.depth + Uint(1), |
| 355 | code_address=code_address, |
| 356 | should_transfer_value=should_transfer_value, |
| 357 | is_static=True if is_staticcall else evm.message.is_static, |
| 358 | accessed_addresses=evm.accessed_addresses.copy(), |
| 359 | accessed_storage_keys=evm.accessed_storage_keys.copy(), |
| 360 | disable_precompiles=disable_precompiles, |
| 361 | parent_evm=evm, |
| 362 | ) |
| 363 | |
| 364 | child_evm = process_message(child_message) |
| 365 | |
| 366 | if child_evm.error: |
| 367 | incorporate_child_on_error(evm, child_evm) |
| 368 | evm.return_data = child_evm.output |
| 369 | push(evm.stack, U256(0)) |
| 370 | else: |
| 371 | incorporate_child_on_success(evm, child_evm) |
| 372 | evm.return_data = child_evm.output |
| 333 | push(evm.stack, U256(1)) |
| 373 | push(evm.stack, CALL_SUCCESS) |
| 374 | |
| 375 | actual_output_size = min(memory_output_size, U256(len(child_evm.output))) |
| 376 | memory_write( |
| 377 | evm.memory, |
| 378 | memory_output_start_position, |
| 379 | child_evm.output[:actual_output_size], |
| 380 | ) |
escrow_subcall_regular_gas ¶
Remove forwarded CALL* gas from the caller's regular gas usage.
CALL* forwards sub_call_gas to the child frame as temporary escrow.
Only gas actually burned by the child should be reintroduced via
incorporate_child_* child gas accounting.
def escrow_subcall_regular_gas(evm: Evm, sub_call_gas: Uint) -> None:
| 384 | """ |
|---|---|
| 385 | Remove forwarded CALL* gas from the caller's regular gas usage. |
| 386 | |
| 387 | CALL* forwards `sub_call_gas` to the child frame as temporary escrow. |
| 388 | Only gas actually burned by the child should be reintroduced via |
| 389 | `incorporate_child_*` child gas accounting. |
| 390 | """ |
| 391 | evm.regular_gas_used -= sub_call_gas |
call ¶
Message-call into an account.
Parameters
evm : The current EVM frame.
def call(evm: Evm) -> None:
| 395 | """ |
|---|---|
| 396 | Message-call into an account. |
| 397 | |
| 398 | Parameters |
| 399 | ---------- |
| 400 | evm : |
| 401 | The current EVM frame. |
| 402 | |
| 403 | """ |
| 404 | # STACK |
| 405 | gas = Uint(pop(evm.stack)) |
| 406 | to = to_address_masked(pop(evm.stack)) |
| 407 | value = pop(evm.stack) |
| 408 | memory_input_start_position = pop(evm.stack) |
| 409 | memory_input_size = pop(evm.stack) |
| 410 | memory_output_start_position = pop(evm.stack) |
| 411 | memory_output_size = pop(evm.stack) |
| 412 | |
| 413 | if evm.message.is_static and value != U256(0): |
| 414 | raise WriteInStaticContext |
| 415 | |
| 416 | # GAS |
| 417 | extend_memory = calculate_gas_extend_memory( |
| 418 | evm.memory, |
| 419 | [ |
| 420 | (memory_input_start_position, memory_input_size), |
| 421 | (memory_output_start_position, memory_output_size), |
| 422 | ], |
| 423 | ) |
| 424 | |
| 371 | if to in evm.accessed_addresses: |
| 372 | access_gas_cost = GasCosts.WARM_ACCESS |
| 425 | is_cold_access = to not in evm.accessed_addresses |
| 426 | if is_cold_access: |
| 427 | access_gas_cost = GasCosts.COLD_ACCOUNT_ACCESS |
| 428 | else: |
| 374 | evm.accessed_addresses.add(to) |
| 375 | access_gas_cost = GasCosts.COLD_ACCOUNT_ACCESS |
| 429 | access_gas_cost = GasCosts.WARM_ACCESS |
| 430 | |
| 377 | code_address = to |
| 431 | transfer_gas_cost = Uint(0) if value == 0 else GasCosts.CALL_VALUE |
| 432 | |
| 433 | # check static gas before state access |
| 434 | check_gas( |
| 435 | evm, |
| 436 | access_gas_cost + transfer_gas_cost + extend_memory.cost, |
| 437 | ) |
| 438 | |
| 439 | # STATE ACCESS |
| 440 | tx_state = evm.message.tx_env.state |
| 441 | if is_cold_access: |
| 442 | evm.accessed_addresses.add(to) |
| 443 | |
| 444 | extra_gas = access_gas_cost + transfer_gas_cost |
| 445 | ( |
| 379 | disable_precompiles, |
| 446 | is_delegated, |
| 447 | code_address, |
| 381 | code, |
| 382 | delegated_access_gas_cost, |
| 383 | ) = access_delegation(evm, code_address) |
| 384 | access_gas_cost += delegated_access_gas_cost |
| 448 | delegation_access_cost, |
| 449 | ) = calculate_delegation_cost(evm, to) |
| 450 | |
| 386 | create_gas_cost = GasCosts.NEW_ACCOUNT |
| 387 | if value == 0 or is_account_alive(evm.message.block_env.state, to): |
| 388 | create_gas_cost = Uint(0) |
| 389 | transfer_gas_cost = Uint(0) if value == 0 else GasCosts.CALL_VALUE |
| 451 | if is_delegated: |
| 452 | # check enough gas for delegation access |
| 453 | extra_gas += delegation_access_cost |
| 454 | check_gas(evm, extra_gas + extend_memory.cost) |
| 455 | if code_address not in evm.accessed_addresses: |
| 456 | evm.accessed_addresses.add(code_address) |
| 457 | |
| 458 | code_hash = get_account(tx_state, code_address).code_hash |
| 459 | code = get_code(tx_state, code_hash) |
| 460 | |
| 461 | # TODO: Consider consolidating charge_gas + charge_state_gas into |
| 462 | # a single gas charge to avoid duplicate EVM trace entries. |
| 463 | # Applies here and in create, create2, selfdestruct. See #2526. |
| 464 | charge_gas(evm, extra_gas + extend_memory.cost) |
| 465 | if value != 0 and not is_account_alive(tx_state, to): |
| 466 | charge_state_gas( |
| 467 | evm, STATE_BYTES_PER_NEW_ACCOUNT * COST_PER_STATE_BYTE |
| 468 | ) |
| 469 | |
| 470 | message_call_gas = calculate_message_call_gas( |
| 471 | value, |
| 472 | gas, |
| 473 | Uint(evm.gas_left), |
| 394 | extend_memory.cost, |
| 395 | access_gas_cost + create_gas_cost + transfer_gas_cost, |
| 474 | memory_cost=Uint(0), |
| 475 | extra_gas=Uint(0), |
| 476 | ) |
| 397 | charge_gas(evm, message_call_gas.cost + extend_memory.cost) |
| 398 | if evm.message.is_static and value != U256(0): |
| 399 | raise WriteInStaticContext |
| 400 | evm.memory += b"\x00" * extend_memory.expand_by |
| 401 | sender_balance = get_account( |
| 402 | evm.message.block_env.state, evm.message.current_target |
| 403 | ).balance |
| 477 | charge_gas(evm, message_call_gas.cost) |
| 478 | escrow_subcall_regular_gas(evm, message_call_gas.sub_call) |
| 479 | |
| 480 | # OPERATION |
| 481 | evm.memory += b"\x00" * extend_memory.expand_by |
| 482 | |
| 483 | # Pass full reservoir to child (no 63/64 rule for state gas) |
| 484 | call_state_gas_reservoir = evm.state_gas_left |
| 485 | evm.state_gas_left = Uint(0) |
| 486 | |
| 487 | sender_balance = get_account(tx_state, evm.message.current_target).balance |
| 488 | if sender_balance < value: |
| 489 | push(evm.stack, U256(0)) |
| 490 | evm.return_data = b"" |
| 407 | evm.gas_left += message_call_gas.sub_call |
| 491 | evm.gas_left += message_call_gas.sub_call |
| 492 | evm.state_gas_left += call_state_gas_reservoir |
| 493 | else: |
| 494 | generic_call( |
| 495 | evm, |
| 496 | message_call_gas.sub_call, |
| 497 | call_state_gas_reservoir, |
| 498 | value, |
| 499 | evm.message.current_target, |
| 500 | to, |
| 501 | code_address, |
| 502 | True, |
| 503 | False, |
| 504 | memory_input_start_position, |
| 505 | memory_input_size, |
| 506 | memory_output_start_position, |
| 507 | memory_output_size, |
| 508 | code, |
| 423 | disable_precompiles, |
| 509 | is_delegated, |
| 510 | ) |
| 511 | |
| 512 | # PROGRAM COUNTER |
| 513 | evm.pc += Uint(1) |
callcode ¶
Message-call into this account with alternative account’s code.Message-call into this account with alternative account's code.
Parameters
evm : The current EVM frame.
def callcode(evm: Evm) -> None:
| 517 | """ |
|---|---|
| 432 | Message-call into this account with alternative account’s code. |
| 518 | Message-call into this account with alternative account's code. |
| 519 | |
| 520 | Parameters |
| 521 | ---------- |
| 522 | evm : |
| 523 | The current EVM frame. |
| 524 | |
| 525 | """ |
| 526 | # STACK |
| 527 | gas = Uint(pop(evm.stack)) |
| 528 | code_address = to_address_masked(pop(evm.stack)) |
| 529 | value = pop(evm.stack) |
| 530 | memory_input_start_position = pop(evm.stack) |
| 531 | memory_input_size = pop(evm.stack) |
| 532 | memory_output_start_position = pop(evm.stack) |
| 533 | memory_output_size = pop(evm.stack) |
| 534 | |
| 535 | # GAS |
| 536 | to = evm.message.current_target |
| 537 | |
| 538 | extend_memory = calculate_gas_extend_memory( |
| 539 | evm.memory, |
| 540 | [ |
| 541 | (memory_input_start_position, memory_input_size), |
| 542 | (memory_output_start_position, memory_output_size), |
| 543 | ], |
| 544 | ) |
| 545 | |
| 460 | if code_address in evm.accessed_addresses: |
| 461 | access_gas_cost = GasCosts.WARM_ACCESS |
| 546 | is_cold_access = code_address not in evm.accessed_addresses |
| 547 | if is_cold_access: |
| 548 | access_gas_cost = GasCosts.COLD_ACCOUNT_ACCESS |
| 549 | else: |
| 463 | evm.accessed_addresses.add(code_address) |
| 464 | access_gas_cost = GasCosts.COLD_ACCOUNT_ACCESS |
| 465 | |
| 466 | ( |
| 467 | disable_precompiles, |
| 468 | code_address, |
| 469 | code, |
| 470 | delegated_access_gas_cost, |
| 471 | ) = access_delegation(evm, code_address) |
| 472 | access_gas_cost += delegated_access_gas_cost |
| 550 | access_gas_cost = GasCosts.WARM_ACCESS |
| 551 | |
| 552 | transfer_gas_cost = Uint(0) if value == 0 else GasCosts.CALL_VALUE |
| 553 | |
| 554 | # check static gas before state access |
| 555 | check_gas( |
| 556 | evm, |
| 557 | access_gas_cost + extend_memory.cost + transfer_gas_cost, |
| 558 | ) |
| 559 | |
| 560 | # STATE ACCESS |
| 561 | tx_state = evm.message.tx_env.state |
| 562 | if is_cold_access: |
| 563 | evm.accessed_addresses.add(code_address) |
| 564 | |
| 565 | extra_gas = access_gas_cost + transfer_gas_cost |
| 566 | ( |
| 567 | is_delegated, |
| 568 | code_address, |
| 569 | delegation_access_cost, |
| 570 | ) = calculate_delegation_cost(evm, code_address) |
| 571 | |
| 572 | if is_delegated: |
| 573 | # check enough gas for delegation access |
| 574 | extra_gas += delegation_access_cost |
| 575 | check_gas(evm, extra_gas + extend_memory.cost) |
| 576 | if code_address not in evm.accessed_addresses: |
| 577 | evm.accessed_addresses.add(code_address) |
| 578 | |
| 579 | code_hash = get_account(tx_state, code_address).code_hash |
| 580 | code = get_code(tx_state, code_hash) |
| 581 | |
| 582 | message_call_gas = calculate_message_call_gas( |
| 583 | value, |
| 584 | gas, |
| 585 | Uint(evm.gas_left), |
| 586 | extend_memory.cost, |
| 480 | access_gas_cost + transfer_gas_cost, |
| 587 | extra_gas, |
| 588 | ) |
| 589 | charge_gas(evm, message_call_gas.cost + extend_memory.cost) |
| 590 | escrow_subcall_regular_gas(evm, message_call_gas.sub_call) |
| 591 | |
| 592 | # OPERATION |
| 593 | evm.memory += b"\x00" * extend_memory.expand_by |
| 486 | sender_balance = get_account( |
| 487 | evm.message.block_env.state, evm.message.current_target |
| 488 | ).balance |
| 594 | |
| 595 | # Pass full reservoir to child (no 63/64 rule for state gas) |
| 596 | call_state_gas_reservoir = evm.state_gas_left |
| 597 | evm.state_gas_left = Uint(0) |
| 598 | |
| 599 | sender_balance = get_account(tx_state, evm.message.current_target).balance |
| 600 | |
| 601 | if sender_balance < value: |
| 602 | push(evm.stack, U256(0)) |
| 603 | evm.return_data = b"" |
| 492 | evm.gas_left += message_call_gas.sub_call |
| 604 | evm.gas_left += message_call_gas.sub_call |
| 605 | evm.state_gas_left += call_state_gas_reservoir |
| 606 | else: |
| 607 | generic_call( |
| 608 | evm, |
| 609 | message_call_gas.sub_call, |
| 610 | call_state_gas_reservoir, |
| 611 | value, |
| 612 | evm.message.current_target, |
| 613 | to, |
| 614 | code_address, |
| 615 | True, |
| 616 | False, |
| 617 | memory_input_start_position, |
| 618 | memory_input_size, |
| 619 | memory_output_start_position, |
| 620 | memory_output_size, |
| 621 | code, |
| 508 | disable_precompiles, |
| 622 | is_delegated, |
| 623 | ) |
| 624 | |
| 625 | # PROGRAM COUNTER |
| 626 | evm.pc += Uint(1) |
selfdestruct ¶
Halt execution and register account for later deletion.
Parameters
evm : The current EVM frame.
def selfdestruct(evm: Evm) -> None:
| 630 | """ |
|---|---|
| 631 | Halt execution and register account for later deletion. |
| 632 | |
| 633 | Parameters |
| 634 | ---------- |
| 635 | evm : |
| 636 | The current EVM frame. |
| 637 | |
| 638 | """ |
| 639 | if evm.message.is_static: |
| 640 | raise WriteInStaticContext |
| 641 | |
| 642 | # STACK |
| 643 | beneficiary = to_address_masked(pop(evm.stack)) |
| 644 | |
| 645 | # GAS |
| 646 | gas_cost = GasCosts.OPCODE_SELFDESTRUCT_BASE |
| 530 | if beneficiary not in evm.accessed_addresses: |
| 531 | evm.accessed_addresses.add(beneficiary) |
| 532 | gas_cost += GasCosts.COLD_ACCOUNT_ACCESS |
| 647 | |
| 534 | if ( |
| 535 | not is_account_alive(evm.message.block_env.state, beneficiary) |
| 536 | and get_account( |
| 537 | evm.message.block_env.state, evm.message.current_target |
| 538 | ).balance |
| 539 | != 0 |
| 540 | ): |
| 541 | gas_cost += GasCosts.OPCODE_SELFDESTRUCT_NEW_ACCOUNT |
| 648 | is_cold_access = beneficiary not in evm.accessed_addresses |
| 649 | if is_cold_access: |
| 650 | gas_cost += GasCosts.COLD_ACCOUNT_ACCESS |
| 651 | |
| 543 | charge_gas(evm, gas_cost) |
| 544 | if evm.message.is_static: |
| 545 | raise WriteInStaticContext |
| 652 | # check access gas cost before state access |
| 653 | check_gas(evm, gas_cost) |
| 654 | |
| 655 | # STATE ACCESS |
| 656 | tx_state = evm.message.tx_env.state |
| 657 | if is_cold_access: |
| 658 | evm.accessed_addresses.add(beneficiary) |
| 659 | |
| 660 | needs_state_gas = ( |
| 661 | not is_account_alive(tx_state, beneficiary) |
| 662 | and get_account(tx_state, evm.message.current_target).balance != 0 |
| 663 | ) |
| 664 | |
| 665 | # Charge regular gas before state gas so that a regular-gas OOG |
| 666 | # does not consume state gas that would inflate the parent's |
| 667 | # reservoir on frame failure. |
| 668 | charge_gas(evm, gas_cost) |
| 669 | if needs_state_gas: |
| 670 | charge_state_gas( |
| 671 | evm, STATE_BYTES_PER_NEW_ACCOUNT * COST_PER_STATE_BYTE |
| 672 | ) |
| 673 | |
| 674 | originator = evm.message.current_target |
| 548 | originator_balance = get_account( |
| 549 | evm.message.block_env.state, originator |
| 550 | ).balance |
| 675 | originator_balance = get_account(tx_state, originator).balance |
| 676 | |
| 552 | move_ether( |
| 553 | evm.message.block_env.state, |
| 554 | originator, |
| 555 | beneficiary, |
| 556 | originator_balance, |
| 557 | ) |
| 677 | # Transfer balance |
| 678 | move_ether(tx_state, originator, beneficiary, originator_balance) |
| 679 | |
| 680 | # EIP-7708: Emit appropriate log based on whether ETH is burned |
| 681 | # or transferred to a different account |
| 682 | if originator in tx_state.created_accounts and beneficiary == originator: |
| 683 | # Self-destruct to self in same tx burns the balance |
| 684 | emit_burn_log(evm, originator, originator_balance) |
| 685 | elif beneficiary != originator: |
| 686 | # Transfer to different beneficiary |
| 687 | emit_transfer_log(evm, originator, beneficiary, originator_balance) |
| 688 | |
| 689 | # register account for deletion only if it was created |
| 690 | # in the same transaction |
| 561 | if originator in evm.message.block_env.state.created_accounts: |
| 691 | if originator in tx_state.created_accounts: |
| 692 | # If beneficiary is the same as originator, then |
| 693 | # the ether is burnt. |
| 564 | set_account_balance(evm.message.block_env.state, originator, U256(0)) |
| 694 | set_account_balance(tx_state, originator, U256(0)) |
| 695 | evm.accounts_to_delete.add(originator) |
| 696 | |
| 697 | # HALT the execution |
| 698 | evm.running = False |
| 699 | |
| 700 | # PROGRAM COUNTER |
| 701 | pass |
delegatecall ¶
Message-call into an account.
Parameters
evm : The current EVM frame.
def delegatecall(evm: Evm) -> None:
| 705 | """ |
|---|---|
| 706 | Message-call into an account. |
| 707 | |
| 708 | Parameters |
| 709 | ---------- |
| 710 | evm : |
| 711 | The current EVM frame. |
| 712 | |
| 713 | """ |
| 714 | # STACK |
| 715 | gas = Uint(pop(evm.stack)) |
| 716 | code_address = to_address_masked(pop(evm.stack)) |
| 717 | memory_input_start_position = pop(evm.stack) |
| 718 | memory_input_size = pop(evm.stack) |
| 719 | memory_output_start_position = pop(evm.stack) |
| 720 | memory_output_size = pop(evm.stack) |
| 721 | |
| 722 | # GAS |
| 723 | extend_memory = calculate_gas_extend_memory( |
| 724 | evm.memory, |
| 725 | [ |
| 726 | (memory_input_start_position, memory_input_size), |
| 727 | (memory_output_start_position, memory_output_size), |
| 728 | ], |
| 729 | ) |
| 730 | |
| 601 | if code_address in evm.accessed_addresses: |
| 602 | access_gas_cost = GasCosts.WARM_ACCESS |
| 731 | is_cold_access = code_address not in evm.accessed_addresses |
| 732 | if is_cold_access: |
| 733 | access_gas_cost = GasCosts.COLD_ACCOUNT_ACCESS |
| 734 | else: |
| 604 | evm.accessed_addresses.add(code_address) |
| 605 | access_gas_cost = GasCosts.COLD_ACCOUNT_ACCESS |
| 735 | access_gas_cost = GasCosts.WARM_ACCESS |
| 736 | |
| 737 | # check static gas before state access |
| 738 | check_gas(evm, access_gas_cost + extend_memory.cost) |
| 739 | |
| 740 | # STATE ACCESS |
| 741 | tx_state = evm.message.tx_env.state |
| 742 | if is_cold_access: |
| 743 | evm.accessed_addresses.add(code_address) |
| 744 | |
| 745 | extra_gas = access_gas_cost |
| 746 | ( |
| 608 | disable_precompiles, |
| 747 | is_delegated, |
| 748 | code_address, |
| 610 | code, |
| 611 | delegated_access_gas_cost, |
| 612 | ) = access_delegation(evm, code_address) |
| 613 | access_gas_cost += delegated_access_gas_cost |
| 749 | delegation_access_cost, |
| 750 | ) = calculate_delegation_cost(evm, code_address) |
| 751 | |
| 752 | if is_delegated: |
| 753 | # check enough gas for delegation access |
| 754 | extra_gas += delegation_access_cost |
| 755 | check_gas(evm, extra_gas + extend_memory.cost) |
| 756 | if code_address not in evm.accessed_addresses: |
| 757 | evm.accessed_addresses.add(code_address) |
| 758 | |
| 759 | code_hash = get_account(tx_state, code_address).code_hash |
| 760 | code = get_code(tx_state, code_hash) |
| 761 | |
| 762 | message_call_gas = calculate_message_call_gas( |
| 616 | U256(0), gas, Uint(evm.gas_left), extend_memory.cost, access_gas_cost |
| 763 | U256(0), |
| 764 | gas, |
| 765 | Uint(evm.gas_left), |
| 766 | extend_memory.cost, |
| 767 | extra_gas, |
| 768 | ) |
| 769 | charge_gas(evm, message_call_gas.cost + extend_memory.cost) |
| 770 | escrow_subcall_regular_gas(evm, message_call_gas.sub_call) |
| 771 | |
| 772 | # OPERATION |
| 773 | evm.memory += b"\x00" * extend_memory.expand_by |
| 774 | |
| 775 | # Pass full reservoir to child (no 63/64 rule for state gas) |
| 776 | call_state_gas_reservoir = evm.state_gas_left |
| 777 | evm.state_gas_left = Uint(0) |
| 778 | |
| 779 | generic_call( |
| 780 | evm, |
| 781 | message_call_gas.sub_call, |
| 782 | call_state_gas_reservoir, |
| 783 | evm.message.value, |
| 784 | evm.message.caller, |
| 785 | evm.message.current_target, |
| 786 | code_address, |
| 787 | False, |
| 788 | False, |
| 789 | memory_input_start_position, |
| 790 | memory_input_size, |
| 791 | memory_output_start_position, |
| 792 | memory_output_size, |
| 793 | code, |
| 636 | disable_precompiles, |
| 794 | is_delegated, |
| 795 | ) |
| 796 | |
| 797 | # PROGRAM COUNTER |
| 798 | evm.pc += Uint(1) |
staticcall ¶
Message-call into an account.
Parameters
evm : The current EVM frame.
def staticcall(evm: Evm) -> None:
| 802 | """ |
|---|---|
| 803 | Message-call into an account. |
| 804 | |
| 805 | Parameters |
| 806 | ---------- |
| 807 | evm : |
| 808 | The current EVM frame. |
| 809 | |
| 810 | """ |
| 811 | # STACK |
| 812 | gas = Uint(pop(evm.stack)) |
| 813 | to = to_address_masked(pop(evm.stack)) |
| 814 | memory_input_start_position = pop(evm.stack) |
| 815 | memory_input_size = pop(evm.stack) |
| 816 | memory_output_start_position = pop(evm.stack) |
| 817 | memory_output_size = pop(evm.stack) |
| 818 | |
| 819 | # GAS |
| 820 | extend_memory = calculate_gas_extend_memory( |
| 821 | evm.memory, |
| 822 | [ |
| 823 | (memory_input_start_position, memory_input_size), |
| 824 | (memory_output_start_position, memory_output_size), |
| 825 | ], |
| 826 | ) |
| 827 | |
| 670 | if to in evm.accessed_addresses: |
| 671 | access_gas_cost = GasCosts.WARM_ACCESS |
| 828 | is_cold_access = to not in evm.accessed_addresses |
| 829 | if is_cold_access: |
| 830 | access_gas_cost = GasCosts.COLD_ACCOUNT_ACCESS |
| 831 | else: |
| 673 | evm.accessed_addresses.add(to) |
| 674 | access_gas_cost = GasCosts.COLD_ACCOUNT_ACCESS |
| 832 | access_gas_cost = GasCosts.WARM_ACCESS |
| 833 | |
| 676 | code_address = to |
| 834 | # check static gas before state access |
| 835 | check_gas(evm, access_gas_cost + extend_memory.cost) |
| 836 | |
| 837 | # STATE ACCESS |
| 838 | tx_state = evm.message.tx_env.state |
| 839 | if is_cold_access: |
| 840 | evm.accessed_addresses.add(to) |
| 841 | |
| 842 | extra_gas = access_gas_cost |
| 843 | ( |
| 678 | disable_precompiles, |
| 844 | is_delegated, |
| 845 | code_address, |
| 680 | code, |
| 681 | delegated_access_gas_cost, |
| 682 | ) = access_delegation(evm, code_address) |
| 683 | access_gas_cost += delegated_access_gas_cost |
| 846 | delegation_access_cost, |
| 847 | ) = calculate_delegation_cost(evm, to) |
| 848 | |
| 849 | if is_delegated: |
| 850 | # check enough gas for delegation access |
| 851 | extra_gas += delegation_access_cost |
| 852 | check_gas(evm, extra_gas + extend_memory.cost) |
| 853 | if code_address not in evm.accessed_addresses: |
| 854 | evm.accessed_addresses.add(code_address) |
| 855 | |
| 856 | code_hash = get_account(tx_state, code_address).code_hash |
| 857 | code = get_code(tx_state, code_hash) |
| 858 | |
| 859 | message_call_gas = calculate_message_call_gas( |
| 860 | U256(0), |
| 861 | gas, |
| 862 | Uint(evm.gas_left), |
| 863 | extend_memory.cost, |
| 690 | access_gas_cost, |
| 864 | extra_gas, |
| 865 | ) |
| 866 | charge_gas(evm, message_call_gas.cost + extend_memory.cost) |
| 867 | escrow_subcall_regular_gas(evm, message_call_gas.sub_call) |
| 868 | |
| 869 | # OPERATION |
| 870 | evm.memory += b"\x00" * extend_memory.expand_by |
| 871 | |
| 872 | # Pass full reservoir to child (no 63/64 rule for state gas) |
| 873 | call_state_gas_reservoir = evm.state_gas_left |
| 874 | evm.state_gas_left = Uint(0) |
| 875 | |
| 876 | generic_call( |
| 877 | evm, |
| 878 | message_call_gas.sub_call, |
| 879 | call_state_gas_reservoir, |
| 880 | U256(0), |
| 881 | evm.message.current_target, |
| 882 | to, |
| 883 | code_address, |
| 884 | True, |
| 885 | True, |
| 886 | memory_input_start_position, |
| 887 | memory_input_size, |
| 888 | memory_output_start_position, |
| 889 | memory_output_size, |
| 890 | code, |
| 710 | disable_precompiles, |
| 891 | is_delegated, |
| 892 | ) |
| 893 | |
| 894 | # PROGRAM COUNTER |
| 895 | evm.pc += Uint(1) |
revert ¶
Stop execution and revert state changes, without consuming all provided gas and also has the ability to return a reason.
Parameters
evm : The current EVM frame.
def revert(evm: Evm) -> None:
| 899 | """ |
|---|---|
| 900 | Stop execution and revert state changes, without consuming all provided gas |
| 901 | and also has the ability to return a reason. |
| 902 | |
| 903 | Parameters |
| 904 | ---------- |
| 905 | evm : |
| 906 | The current EVM frame. |
| 907 | |
| 908 | """ |
| 909 | # STACK |
| 910 | memory_start_index = pop(evm.stack) |
| 911 | size = pop(evm.stack) |
| 912 | |
| 913 | # GAS |
| 914 | extend_memory = calculate_gas_extend_memory( |
| 915 | evm.memory, [(memory_start_index, size)] |
| 916 | ) |
| 917 | |
| 918 | charge_gas(evm, extend_memory.cost) |
| 919 | |
| 920 | # OPERATION |
| 921 | evm.memory += b"\x00" * extend_memory.expand_by |
| 922 | output = memory_read_bytes(evm.memory, memory_start_index, size) |
| 923 | evm.output = Bytes(output) |
| 924 | raise Revert |
| 925 | |
| 926 | # PROGRAM COUNTER |
| 927 | # no-op |