ethereum.forks.istanbul.vm.instructions.systemethereum.forks.muir_glacier.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:
| 62 | <snip> |
|---|---|
| 65 | # This import causes a circular import error |
| 66 | # if it's not moved inside this method |
| 67 | from ...vm.interpreter import STACK_DEPTH_LIMIT, process_create_message |
| 68 | |
| 69 | call_data = memory_read_bytes( |
| 70 | evm.memory, memory_start_position, memory_size |
| 71 | ) |
| 72 | |
| 73 | create_message_gas = max_message_call_gas(Uint(evm.gas_left)) |
| 74 | evm.gas_left -= create_message_gas |
| 75 | if evm.message.is_static: |
| 76 | raise WriteInStaticContext |
| 77 | evm.return_data = b"" |
| 78 | |
| 79 | sender_address = evm.message.current_target |
| 80 | sender = get_account(evm.message.tx_env.state, sender_address) |
| 81 | |
| 82 | if ( |
| 83 | sender.balance < endowment |
| 84 | or sender.nonce == Uint(2**64 - 1) |
| 85 | or evm.message.depth + Uint(1) > STACK_DEPTH_LIMIT |
| 86 | ): |
| 87 | evm.gas_left += create_message_gas |
| 88 | push(evm.stack, U256(0)) |
| 89 | return |
| 90 | |
| 91 | if not account_deployable(evm.message.tx_env.state, contract_address): |
| 92 | increment_nonce(evm.message.tx_env.state, evm.message.current_target) |
| 93 | push(evm.stack, U256(0)) |
| 94 | return |
| 95 | |
| 96 | call_data = memory_read_bytes( |
| 97 | evm.memory, memory_start_position, memory_size |
| 98 | ) |
| 99 | |
| 100 | increment_nonce(evm.message.tx_env.state, evm.message.current_target) |
| 101 | |
| 102 | child_message = Message( |
| 103 | block_env=evm.message.block_env, |
| 104 | tx_env=evm.message.tx_env, |
| 105 | caller=evm.message.current_target, |
| 106 | target=Bytes0(), |
| 107 | gas=create_message_gas, |
| 108 | value=endowment, |
| 109 | data=b"", |
| 110 | code=call_data, |
| 111 | current_target=contract_address, |
| 112 | depth=evm.message.depth + Uint(1), |
| 113 | code_address=None, |
| 114 | should_transfer_value=True, |
| 115 | is_static=False, |
| 116 | parent_evm=evm, |
| 117 | ) |
| 118 | child_evm = process_create_message(child_message) |
| 119 | |
| 120 | if child_evm.error: |
| 121 | incorporate_child_on_error(evm, child_evm) |
| 122 | evm.return_data = child_evm.output |
| 123 | push(evm.stack, U256(0)) |
| 124 | else: |
| 125 | incorporate_child_on_success(evm, child_evm) |
| 126 | evm.return_data = b"" |
| 127 | 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:
| 131 | <snip> |
|---|---|
| 140 | # STACK |
| 141 | endowment = pop(evm.stack) |
| 142 | memory_start_position = pop(evm.stack) |
| 143 | memory_size = pop(evm.stack) |
| 144 | |
| 145 | # GAS |
| 146 | extend_memory = calculate_gas_extend_memory( |
| 147 | evm.memory, [(memory_start_position, memory_size)] |
| 148 | ) |
| 149 | |
| 150 | charge_gas(evm, GasCosts.OPCODE_CREATE_BASE + extend_memory.cost) |
| 151 | |
| 152 | # OPERATION |
| 153 | evm.memory += b"\x00" * extend_memory.expand_by |
| 154 | contract_address = compute_contract_address( |
| 155 | evm.message.current_target, |
| 156 | get_account( |
| 157 | evm.message.tx_env.state, evm.message.current_target |
| 158 | ).nonce, |
| 159 | ) |
| 160 | |
| 161 | generic_create( |
| 162 | evm, |
| 163 | endowment, |
| 164 | contract_address, |
| 165 | memory_start_position, |
| 166 | memory_size, |
| 167 | ) |
| 168 | |
| 169 | # PROGRAM COUNTER |
| 170 | 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:
| 174 | <snip> |
|---|---|
| 186 | # STACK |
| 187 | endowment = pop(evm.stack) |
| 188 | memory_start_position = pop(evm.stack) |
| 189 | memory_size = pop(evm.stack) |
| 190 | salt = pop(evm.stack).to_be_bytes32() |
| 191 | |
| 192 | # GAS |
| 193 | extend_memory = calculate_gas_extend_memory( |
| 194 | evm.memory, [(memory_start_position, memory_size)] |
| 195 | ) |
| 196 | call_data_words = ceil32(Uint(memory_size)) // Uint(32) |
| 197 | charge_gas( |
| 198 | evm, |
| 199 | GasCosts.OPCODE_CREATE_BASE |
| 200 | + GasCosts.OPCODE_KECCAK256_PER_WORD * call_data_words |
| 201 | + extend_memory.cost, |
| 202 | ) |
| 203 | |
| 204 | # OPERATION |
| 205 | evm.memory += b"\x00" * extend_memory.expand_by |
| 206 | contract_address = compute_create2_contract_address( |
| 207 | evm.message.current_target, |
| 208 | salt, |
| 209 | memory_read_bytes(evm.memory, memory_start_position, memory_size), |
| 210 | ) |
| 211 | |
| 212 | generic_create( |
| 213 | evm, |
| 214 | endowment, |
| 215 | contract_address, |
| 216 | memory_start_position, |
| 217 | memory_size, |
| 218 | ) |
| 219 | |
| 220 | # PROGRAM COUNTER |
| 221 | evm.pc += Uint(1) |
return_ ¶
Halts execution returning output data.
Parameters
evm : The current EVM frame.
def return_(evm: Evm) -> None:
| 225 | <snip> |
|---|---|
| 234 | # STACK |
| 235 | memory_start_position = pop(evm.stack) |
| 236 | memory_size = pop(evm.stack) |
| 237 | |
| 238 | # GAS |
| 239 | extend_memory = calculate_gas_extend_memory( |
| 240 | evm.memory, [(memory_start_position, memory_size)] |
| 241 | ) |
| 242 | |
| 243 | charge_gas(evm, GasCosts.ZERO + extend_memory.cost) |
| 244 | |
| 245 | # OPERATION |
| 246 | evm.memory += b"\x00" * extend_memory.expand_by |
| 247 | evm.output = memory_read_bytes( |
| 248 | evm.memory, memory_start_position, memory_size |
| 249 | ) |
| 250 | |
| 251 | evm.running = False |
| 252 | |
| 253 | # PROGRAM COUNTER |
| 254 | pass |
GenericCall ¶
Parameters for the core logic of the CALL* family of opcodes.
| 257 | @final |
|---|
| 258 | @dataclass |
|---|
class GenericCall:
gas¶
| 264 | gas: Uint |
|---|
value¶
| 265 | value: U256 |
|---|
caller¶
| 266 | caller: Address |
|---|
to¶
| 267 | to: Address |
|---|
code_address¶
| 268 | code_address: Address |
|---|
should_transfer_value¶
| 269 | should_transfer_value: bool |
|---|
is_staticcall¶
| 270 | is_staticcall: bool |
|---|
memory_input_start_position¶
| 271 | memory_input_start_position: U256 |
|---|
memory_input_size¶
| 272 | memory_input_size: U256 |
|---|
memory_output_start_position¶
| 273 | memory_output_start_position: U256 |
|---|
memory_output_size¶
| 274 | memory_output_size: U256 |
|---|
generic_call ¶
Perform the core logic of the CALL* family of opcodes.
def generic_call(evm: Evm, params: GenericCall) -> None:
| 278 | <snip> |
|---|---|
| 281 | from ...vm.interpreter import STACK_DEPTH_LIMIT, process_message |
| 282 | |
| 283 | evm.return_data = b"" |
| 284 | |
| 285 | if evm.message.depth + Uint(1) > STACK_DEPTH_LIMIT: |
| 286 | evm.gas_left += params.gas |
| 287 | push(evm.stack, U256(0)) |
| 288 | return |
| 289 | |
| 290 | call_data = memory_read_bytes( |
| 291 | evm.memory, |
| 292 | params.memory_input_start_position, |
| 293 | params.memory_input_size, |
| 294 | ) |
| 295 | code_account = get_account(evm.message.tx_env.state, params.code_address) |
| 296 | code = get_code(evm.message.tx_env.state, code_account.code_hash) |
| 297 | child_message = Message( |
| 298 | block_env=evm.message.block_env, |
| 299 | tx_env=evm.message.tx_env, |
| 300 | caller=params.caller, |
| 301 | target=params.to, |
| 302 | gas=params.gas, |
| 303 | value=params.value, |
| 304 | data=call_data, |
| 305 | code=code, |
| 306 | current_target=params.to, |
| 307 | depth=evm.message.depth + Uint(1), |
| 308 | code_address=params.code_address, |
| 309 | should_transfer_value=params.should_transfer_value, |
| 310 | is_static=params.is_staticcall or evm.message.is_static, |
| 311 | parent_evm=evm, |
| 312 | ) |
| 313 | child_evm = process_message(child_message) |
| 314 | |
| 315 | if child_evm.error: |
| 316 | incorporate_child_on_error(evm, child_evm) |
| 317 | evm.return_data = child_evm.output |
| 318 | push(evm.stack, U256(0)) |
| 319 | else: |
| 320 | incorporate_child_on_success(evm, child_evm) |
| 321 | evm.return_data = child_evm.output |
| 322 | push(evm.stack, U256(1)) |
| 323 | |
| 324 | actual_output_size = min( |
| 325 | params.memory_output_size, U256(len(child_evm.output)) |
| 326 | ) |
| 327 | memory_write( |
| 328 | evm.memory, |
| 329 | params.memory_output_start_position, |
| 330 | child_evm.output[:actual_output_size], |
| 331 | ) |
call ¶
Message-call into an account.
Parameters
evm : The current EVM frame.
def call(evm: Evm) -> None:
| 335 | <snip> |
|---|---|
| 344 | # STACK |
| 345 | gas = Uint(pop(evm.stack)) |
| 346 | to = to_address_masked(pop(evm.stack)) |
| 347 | value = pop(evm.stack) |
| 348 | memory_input_start_position = pop(evm.stack) |
| 349 | memory_input_size = pop(evm.stack) |
| 350 | memory_output_start_position = pop(evm.stack) |
| 351 | memory_output_size = pop(evm.stack) |
| 352 | |
| 353 | # GAS |
| 354 | extend_memory = calculate_gas_extend_memory( |
| 355 | evm.memory, |
| 356 | [ |
| 357 | (memory_input_start_position, memory_input_size), |
| 358 | (memory_output_start_position, memory_output_size), |
| 359 | ], |
| 360 | ) |
| 361 | |
| 362 | code_address = to |
| 363 | |
| 364 | create_gas_cost = GasCosts.NEW_ACCOUNT |
| 365 | if value == 0 or is_account_alive(evm.message.tx_env.state, to): |
| 366 | create_gas_cost = Uint(0) |
| 367 | transfer_gas_cost = Uint(0) if value == 0 else GasCosts.CALL_VALUE |
| 368 | message_call_gas = calculate_message_call_gas( |
| 369 | value, |
| 370 | gas, |
| 371 | Uint(evm.gas_left), |
| 372 | memory_cost=extend_memory.cost, |
| 373 | extra_gas=GasCosts.OPCODE_CALL_BASE |
| 374 | + create_gas_cost |
| 375 | + transfer_gas_cost, |
| 376 | ) |
| 377 | charge_gas(evm, message_call_gas.cost + extend_memory.cost) |
| 378 | if evm.message.is_static and value != U256(0): |
| 379 | raise WriteInStaticContext |
| 380 | evm.memory += b"\x00" * extend_memory.expand_by |
| 381 | sender_balance = get_account( |
| 382 | evm.message.tx_env.state, evm.message.current_target |
| 383 | ).balance |
| 384 | if sender_balance < value: |
| 385 | push(evm.stack, U256(0)) |
| 386 | evm.return_data = b"" |
| 387 | evm.gas_left += message_call_gas.sub_call |
| 388 | else: |
| 389 | generic_call( |
| 390 | evm, |
| 391 | GenericCall( |
| 392 | gas=message_call_gas.sub_call, |
| 393 | value=value, |
| 394 | caller=evm.message.current_target, |
| 395 | to=to, |
| 396 | code_address=code_address, |
| 397 | should_transfer_value=True, |
| 398 | is_staticcall=False, |
| 399 | memory_input_start_position=memory_input_start_position, |
| 400 | memory_input_size=memory_input_size, |
| 401 | memory_output_start_position=memory_output_start_position, |
| 402 | memory_output_size=memory_output_size, |
| 403 | ), |
| 404 | ) |
| 405 | |
| 406 | # PROGRAM COUNTER |
| 407 | evm.pc += Uint(1) |
callcode ¶
Message-call into this account with alternative account’s code.
Parameters
evm : The current EVM frame.
def callcode(evm: Evm) -> None:
| 411 | <snip> |
|---|---|
| 420 | # STACK |
| 421 | gas = Uint(pop(evm.stack)) |
| 422 | code_address = to_address_masked(pop(evm.stack)) |
| 423 | value = pop(evm.stack) |
| 424 | memory_input_start_position = pop(evm.stack) |
| 425 | memory_input_size = pop(evm.stack) |
| 426 | memory_output_start_position = pop(evm.stack) |
| 427 | memory_output_size = pop(evm.stack) |
| 428 | |
| 429 | # GAS |
| 430 | to = evm.message.current_target |
| 431 | |
| 432 | extend_memory = calculate_gas_extend_memory( |
| 433 | evm.memory, |
| 434 | [ |
| 435 | (memory_input_start_position, memory_input_size), |
| 436 | (memory_output_start_position, memory_output_size), |
| 437 | ], |
| 438 | ) |
| 439 | transfer_gas_cost = Uint(0) if value == 0 else GasCosts.CALL_VALUE |
| 440 | message_call_gas = calculate_message_call_gas( |
| 441 | value, |
| 442 | gas, |
| 443 | Uint(evm.gas_left), |
| 444 | extend_memory.cost, |
| 445 | GasCosts.OPCODE_CALL_BASE + transfer_gas_cost, |
| 446 | ) |
| 447 | charge_gas(evm, message_call_gas.cost + extend_memory.cost) |
| 448 | |
| 449 | # OPERATION |
| 450 | evm.memory += b"\x00" * extend_memory.expand_by |
| 451 | sender_balance = get_account( |
| 452 | evm.message.tx_env.state, evm.message.current_target |
| 453 | ).balance |
| 454 | if sender_balance < value: |
| 455 | push(evm.stack, U256(0)) |
| 456 | evm.return_data = b"" |
| 457 | evm.gas_left += message_call_gas.sub_call |
| 458 | else: |
| 459 | generic_call( |
| 460 | evm, |
| 461 | GenericCall( |
| 462 | gas=message_call_gas.sub_call, |
| 463 | value=value, |
| 464 | caller=evm.message.current_target, |
| 465 | to=to, |
| 466 | code_address=code_address, |
| 467 | should_transfer_value=True, |
| 468 | is_staticcall=False, |
| 469 | memory_input_start_position=memory_input_start_position, |
| 470 | memory_input_size=memory_input_size, |
| 471 | memory_output_start_position=memory_output_start_position, |
| 472 | memory_output_size=memory_output_size, |
| 473 | ), |
| 474 | ) |
| 475 | |
| 476 | # PROGRAM COUNTER |
| 477 | evm.pc += Uint(1) |
selfdestruct ¶
Halt execution and register account for later deletion.
Parameters
evm : The current EVM frame.
def selfdestruct(evm: Evm) -> None:
| 481 | <snip> |
|---|---|
| 490 | # STACK |
| 491 | beneficiary = to_address_masked(pop(evm.stack)) |
| 492 | |
| 493 | # GAS |
| 494 | gas_cost = GasCosts.OPCODE_SELFDESTRUCT_BASE |
| 495 | if ( |
| 496 | not is_account_alive(evm.message.tx_env.state, beneficiary) |
| 497 | and get_account( |
| 498 | evm.message.tx_env.state, evm.message.current_target |
| 499 | ).balance |
| 500 | != 0 |
| 501 | ): |
| 502 | gas_cost += GasCosts.OPCODE_SELFDESTRUCT_NEW_ACCOUNT |
| 503 | |
| 504 | originator = evm.message.current_target |
| 505 | |
| 506 | refunded_accounts = evm.accounts_to_delete |
| 507 | parent_evm = evm.message.parent_evm |
| 508 | while parent_evm is not None: |
| 509 | refunded_accounts.update(parent_evm.accounts_to_delete) |
| 510 | parent_evm = parent_evm.message.parent_evm |
| 511 | |
| 512 | if originator not in refunded_accounts: |
| 513 | evm.refund_counter += GasCosts.REFUND_SELF_DESTRUCT |
| 514 | |
| 515 | charge_gas(evm, gas_cost) |
| 516 | if evm.message.is_static: |
| 517 | raise WriteInStaticContext |
| 518 | |
| 519 | originator = evm.message.current_target |
| 520 | beneficiary_balance = get_account( |
| 521 | evm.message.tx_env.state, beneficiary |
| 522 | ).balance |
| 523 | originator_balance = get_account( |
| 524 | evm.message.tx_env.state, originator |
| 525 | ).balance |
| 526 | |
| 527 | # First Transfer to beneficiary |
| 528 | set_account_balance( |
| 529 | evm.message.tx_env.state, |
| 530 | beneficiary, |
| 531 | beneficiary_balance + originator_balance, |
| 532 | ) |
| 533 | # Next, Zero the balance of the address being deleted (must come after |
| 534 | # sending to beneficiary in case the contract named itself as the |
| 535 | # beneficiary). |
| 536 | set_account_balance(evm.message.tx_env.state, originator, U256(0)) |
| 537 | |
| 538 | # register account for deletion |
| 539 | evm.accounts_to_delete.add(originator) |
| 540 | |
| 541 | # mark beneficiary as touched |
| 542 | if account_exists_and_is_empty(evm.message.tx_env.state, beneficiary): |
| 543 | evm.touched_accounts.add(beneficiary) |
| 544 | |
| 545 | # HALT the execution |
| 546 | evm.running = False |
| 547 | |
| 548 | # PROGRAM COUNTER |
| 549 | pass |
delegatecall ¶
Message-call into an account.
Parameters
evm : The current EVM frame.
def delegatecall(evm: Evm) -> None:
| 553 | <snip> |
|---|---|
| 562 | # STACK |
| 563 | gas = Uint(pop(evm.stack)) |
| 564 | code_address = to_address_masked(pop(evm.stack)) |
| 565 | memory_input_start_position = pop(evm.stack) |
| 566 | memory_input_size = pop(evm.stack) |
| 567 | memory_output_start_position = pop(evm.stack) |
| 568 | memory_output_size = pop(evm.stack) |
| 569 | |
| 570 | # GAS |
| 571 | extend_memory = calculate_gas_extend_memory( |
| 572 | evm.memory, |
| 573 | [ |
| 574 | (memory_input_start_position, memory_input_size), |
| 575 | (memory_output_start_position, memory_output_size), |
| 576 | ], |
| 577 | ) |
| 578 | message_call_gas = calculate_message_call_gas( |
| 579 | U256(0), |
| 580 | gas, |
| 581 | Uint(evm.gas_left), |
| 582 | extend_memory.cost, |
| 583 | GasCosts.OPCODE_CALL_BASE, |
| 584 | ) |
| 585 | charge_gas(evm, message_call_gas.cost + extend_memory.cost) |
| 586 | |
| 587 | # OPERATION |
| 588 | evm.memory += b"\x00" * extend_memory.expand_by |
| 589 | generic_call( |
| 590 | evm, |
| 591 | GenericCall( |
| 592 | gas=message_call_gas.sub_call, |
| 593 | value=evm.message.value, |
| 594 | caller=evm.message.caller, |
| 595 | to=evm.message.current_target, |
| 596 | code_address=code_address, |
| 597 | should_transfer_value=False, |
| 598 | is_staticcall=False, |
| 599 | memory_input_start_position=memory_input_start_position, |
| 600 | memory_input_size=memory_input_size, |
| 601 | memory_output_start_position=memory_output_start_position, |
| 602 | memory_output_size=memory_output_size, |
| 603 | ), |
| 604 | ) |
| 605 | |
| 606 | # PROGRAM COUNTER |
| 607 | evm.pc += Uint(1) |
staticcall ¶
Message-call into an account.
Parameters
evm : The current EVM frame.
def staticcall(evm: Evm) -> None:
| 611 | <snip> |
|---|---|
| 620 | # STACK |
| 621 | gas = Uint(pop(evm.stack)) |
| 622 | to = to_address_masked(pop(evm.stack)) |
| 623 | memory_input_start_position = pop(evm.stack) |
| 624 | memory_input_size = pop(evm.stack) |
| 625 | memory_output_start_position = pop(evm.stack) |
| 626 | memory_output_size = pop(evm.stack) |
| 627 | |
| 628 | # GAS |
| 629 | extend_memory = calculate_gas_extend_memory( |
| 630 | evm.memory, |
| 631 | [ |
| 632 | (memory_input_start_position, memory_input_size), |
| 633 | (memory_output_start_position, memory_output_size), |
| 634 | ], |
| 635 | ) |
| 636 | |
| 637 | code_address = to |
| 638 | |
| 639 | message_call_gas = calculate_message_call_gas( |
| 640 | U256(0), |
| 641 | gas, |
| 642 | Uint(evm.gas_left), |
| 643 | extend_memory.cost, |
| 644 | GasCosts.OPCODE_CALL_BASE, |
| 645 | ) |
| 646 | charge_gas(evm, message_call_gas.cost + extend_memory.cost) |
| 647 | |
| 648 | # OPERATION |
| 649 | evm.memory += b"\x00" * extend_memory.expand_by |
| 650 | generic_call( |
| 651 | evm, |
| 652 | GenericCall( |
| 653 | gas=message_call_gas.sub_call, |
| 654 | value=U256(0), |
| 655 | caller=evm.message.current_target, |
| 656 | to=to, |
| 657 | code_address=code_address, |
| 658 | should_transfer_value=True, |
| 659 | is_staticcall=True, |
| 660 | memory_input_start_position=memory_input_start_position, |
| 661 | memory_input_size=memory_input_size, |
| 662 | memory_output_start_position=memory_output_start_position, |
| 663 | memory_output_size=memory_output_size, |
| 664 | ), |
| 665 | ) |
| 666 | |
| 667 | # PROGRAM COUNTER |
| 668 | 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:
| 672 | <snip> |
|---|---|
| 682 | # STACK |
| 683 | memory_start_index = pop(evm.stack) |
| 684 | size = pop(evm.stack) |
| 685 | |
| 686 | # GAS |
| 687 | extend_memory = calculate_gas_extend_memory( |
| 688 | evm.memory, [(memory_start_index, size)] |
| 689 | ) |
| 690 | |
| 691 | charge_gas(evm, extend_memory.cost) |
| 692 | |
| 693 | # OPERATION |
| 694 | evm.memory += b"\x00" * extend_memory.expand_by |
| 695 | output = memory_read_bytes(evm.memory, memory_start_index, size) |
| 696 | evm.output = Bytes(output) |
| 697 | raise Revert |
| 698 | |
| 699 | # PROGRAM COUNTER |
| 700 | # no-op |