ethereum.forks.constantinople.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:
| 60 | """ |
|---|---|
| 61 | Core logic used by the `CREATE*` family of opcodes. |
| 62 | """ |
| 63 | # This import causes a circular import error |
| 64 | # if it's not moved inside this method |
| 65 | from ...vm.interpreter import STACK_DEPTH_LIMIT, process_create_message |
| 66 | |
| 67 | call_data = memory_read_bytes( |
| 68 | evm.memory, memory_start_position, memory_size |
| 69 | ) |
| 70 | |
| 71 | create_message_gas = max_message_call_gas(Uint(evm.gas_left)) |
| 72 | evm.gas_left -= create_message_gas |
| 73 | if evm.message.is_static: |
| 74 | raise WriteInStaticContext |
| 75 | evm.return_data = b"" |
| 76 | |
| 77 | sender_address = evm.message.current_target |
| 78 | sender = get_account(evm.message.block_env.state, sender_address) |
| 79 | |
| 80 | if ( |
| 81 | sender.balance < endowment |
| 82 | or sender.nonce == Uint(2**64 - 1) |
| 83 | or evm.message.depth + Uint(1) > STACK_DEPTH_LIMIT |
| 84 | ): |
| 85 | evm.gas_left += create_message_gas |
| 86 | push(evm.stack, U256(0)) |
| 87 | return |
| 88 | |
| 89 | if account_has_code_or_nonce( |
| 90 | evm.message.block_env.state, contract_address |
| 91 | ) or account_has_storage(evm.message.block_env.state, contract_address): |
| 92 | increment_nonce( |
| 93 | evm.message.block_env.state, evm.message.current_target |
| 94 | ) |
| 95 | push(evm.stack, U256(0)) |
| 96 | return |
| 97 | |
| 98 | call_data = memory_read_bytes( |
| 99 | evm.memory, memory_start_position, memory_size |
| 100 | ) |
| 101 | |
| 102 | increment_nonce(evm.message.block_env.state, evm.message.current_target) |
| 103 | |
| 104 | child_message = Message( |
| 105 | block_env=evm.message.block_env, |
| 106 | tx_env=evm.message.tx_env, |
| 107 | caller=evm.message.current_target, |
| 108 | target=Bytes0(), |
| 109 | gas=create_message_gas, |
| 110 | value=endowment, |
| 111 | data=b"", |
| 112 | code=call_data, |
| 113 | current_target=contract_address, |
| 114 | depth=evm.message.depth + Uint(1), |
| 115 | code_address=None, |
| 116 | should_transfer_value=True, |
| 117 | is_static=False, |
| 118 | parent_evm=evm, |
| 119 | ) |
| 120 | child_evm = process_create_message(child_message) |
| 121 | |
| 122 | if child_evm.error: |
| 123 | incorporate_child_on_error(evm, child_evm) |
| 124 | evm.return_data = child_evm.output |
| 125 | push(evm.stack, U256(0)) |
| 126 | else: |
| 127 | incorporate_child_on_success(evm, child_evm) |
| 128 | evm.return_data = b"" |
| 129 | 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:
| 133 | """ |
|---|---|
| 134 | Creates a new account with associated code. |
| 135 | |
| 136 | Parameters |
| 137 | ---------- |
| 138 | evm : |
| 139 | The current EVM frame. |
| 140 | |
| 141 | """ |
| 142 | # STACK |
| 143 | endowment = pop(evm.stack) |
| 144 | memory_start_position = pop(evm.stack) |
| 145 | memory_size = pop(evm.stack) |
| 146 | |
| 147 | # GAS |
| 148 | extend_memory = calculate_gas_extend_memory( |
| 149 | evm.memory, [(memory_start_position, memory_size)] |
| 150 | ) |
| 151 | |
| 152 | charge_gas(evm, GasCosts.OPCODE_CREATE_BASE + extend_memory.cost) |
| 153 | |
| 154 | # OPERATION |
| 155 | evm.memory += b"\x00" * extend_memory.expand_by |
| 156 | contract_address = compute_contract_address( |
| 157 | evm.message.current_target, |
| 158 | get_account( |
| 159 | evm.message.block_env.state, evm.message.current_target |
| 160 | ).nonce, |
| 161 | ) |
| 162 | |
| 163 | generic_create( |
| 164 | evm, endowment, contract_address, memory_start_position, memory_size |
| 165 | ) |
| 166 | |
| 167 | # PROGRAM COUNTER |
| 168 | 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:
| 172 | """ |
|---|---|
| 173 | Creates a new account with associated code. |
| 174 | |
| 175 | It's similar to the CREATE opcode except that the address of the new |
| 176 | account depends on the init_code instead of the nonce of sender. |
| 177 | |
| 178 | Parameters |
| 179 | ---------- |
| 180 | evm : |
| 181 | The current EVM frame. |
| 182 | |
| 183 | """ |
| 184 | # STACK |
| 185 | endowment = pop(evm.stack) |
| 186 | memory_start_position = pop(evm.stack) |
| 187 | memory_size = pop(evm.stack) |
| 188 | salt = pop(evm.stack).to_be_bytes32() |
| 189 | |
| 190 | # GAS |
| 191 | extend_memory = calculate_gas_extend_memory( |
| 192 | evm.memory, [(memory_start_position, memory_size)] |
| 193 | ) |
| 194 | call_data_words = ceil32(Uint(memory_size)) // Uint(32) |
| 195 | charge_gas( |
| 196 | evm, |
| 197 | GasCosts.OPCODE_CREATE_BASE |
| 198 | + GasCosts.OPCODE_KECCACK256_PER_WORD * call_data_words |
| 199 | + extend_memory.cost, |
| 200 | ) |
| 201 | |
| 202 | # OPERATION |
| 203 | evm.memory += b"\x00" * extend_memory.expand_by |
| 204 | contract_address = compute_create2_contract_address( |
| 205 | evm.message.current_target, |
| 206 | salt, |
| 207 | memory_read_bytes(evm.memory, memory_start_position, memory_size), |
| 208 | ) |
| 209 | |
| 210 | generic_create( |
| 211 | evm, endowment, contract_address, memory_start_position, memory_size |
| 212 | ) |
| 213 | |
| 214 | # PROGRAM COUNTER |
| 215 | evm.pc += Uint(1) |
return_ ¶
Halts execution returning output data.
Parameters
evm : The current EVM frame.
def return_(evm: Evm) -> None:
| 219 | """ |
|---|---|
| 220 | Halts execution returning output data. |
| 221 | |
| 222 | Parameters |
| 223 | ---------- |
| 224 | evm : |
| 225 | The current EVM frame. |
| 226 | |
| 227 | """ |
| 228 | # STACK |
| 229 | memory_start_position = pop(evm.stack) |
| 230 | memory_size = pop(evm.stack) |
| 231 | |
| 232 | # GAS |
| 233 | extend_memory = calculate_gas_extend_memory( |
| 234 | evm.memory, [(memory_start_position, memory_size)] |
| 235 | ) |
| 236 | |
| 237 | charge_gas(evm, GasCosts.ZERO + extend_memory.cost) |
| 238 | |
| 239 | # OPERATION |
| 240 | evm.memory += b"\x00" * extend_memory.expand_by |
| 241 | evm.output = memory_read_bytes( |
| 242 | evm.memory, memory_start_position, memory_size |
| 243 | ) |
| 244 | |
| 245 | evm.running = False |
| 246 | |
| 247 | # PROGRAM COUNTER |
| 248 | pass |
generic_call ¶
Perform the core logic of the CALL* family of opcodes.
def generic_call(evm: Evm, gas: 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) -> None:
| 265 | """ |
|---|---|
| 266 | Perform the core logic of the `CALL*` family of opcodes. |
| 267 | """ |
| 268 | from ...vm.interpreter import STACK_DEPTH_LIMIT, process_message |
| 269 | |
| 270 | evm.return_data = b"" |
| 271 | |
| 272 | if evm.message.depth + Uint(1) > STACK_DEPTH_LIMIT: |
| 273 | evm.gas_left += gas |
| 274 | push(evm.stack, U256(0)) |
| 275 | return |
| 276 | |
| 277 | call_data = memory_read_bytes( |
| 278 | evm.memory, memory_input_start_position, memory_input_size |
| 279 | ) |
| 280 | account = get_account(evm.message.block_env.state, code_address) |
| 281 | code = get_code(evm.message.block_env.state, account.code_hash) |
| 282 | child_message = Message( |
| 283 | block_env=evm.message.block_env, |
| 284 | tx_env=evm.message.tx_env, |
| 285 | caller=caller, |
| 286 | target=to, |
| 287 | gas=gas, |
| 288 | value=value, |
| 289 | data=call_data, |
| 290 | code=code, |
| 291 | current_target=to, |
| 292 | depth=evm.message.depth + Uint(1), |
| 293 | code_address=code_address, |
| 294 | should_transfer_value=should_transfer_value, |
| 295 | is_static=True if is_staticcall else evm.message.is_static, |
| 296 | parent_evm=evm, |
| 297 | ) |
| 298 | child_evm = process_message(child_message) |
| 299 | |
| 300 | if child_evm.error: |
| 301 | incorporate_child_on_error(evm, child_evm) |
| 302 | evm.return_data = child_evm.output |
| 303 | push(evm.stack, U256(0)) |
| 304 | else: |
| 305 | incorporate_child_on_success(evm, child_evm) |
| 306 | evm.return_data = child_evm.output |
| 307 | push(evm.stack, U256(1)) |
| 308 | |
| 309 | actual_output_size = min(memory_output_size, U256(len(child_evm.output))) |
| 310 | memory_write( |
| 311 | evm.memory, |
| 312 | memory_output_start_position, |
| 313 | child_evm.output[:actual_output_size], |
| 314 | ) |
call ¶
Message-call into an account.
Parameters
evm : The current EVM frame.
def call(evm: Evm) -> None:
| 318 | """ |
|---|---|
| 319 | Message-call into an account. |
| 320 | |
| 321 | Parameters |
| 322 | ---------- |
| 323 | evm : |
| 324 | The current EVM frame. |
| 325 | |
| 326 | """ |
| 327 | # STACK |
| 328 | gas = Uint(pop(evm.stack)) |
| 329 | to = to_address_masked(pop(evm.stack)) |
| 330 | value = pop(evm.stack) |
| 331 | memory_input_start_position = pop(evm.stack) |
| 332 | memory_input_size = pop(evm.stack) |
| 333 | memory_output_start_position = pop(evm.stack) |
| 334 | memory_output_size = pop(evm.stack) |
| 335 | |
| 336 | # GAS |
| 337 | extend_memory = calculate_gas_extend_memory( |
| 338 | evm.memory, |
| 339 | [ |
| 340 | (memory_input_start_position, memory_input_size), |
| 341 | (memory_output_start_position, memory_output_size), |
| 342 | ], |
| 343 | ) |
| 344 | |
| 345 | code_address = to |
| 346 | |
| 347 | create_gas_cost = GasCosts.NEW_ACCOUNT |
| 348 | if value == 0 or is_account_alive(evm.message.block_env.state, to): |
| 349 | create_gas_cost = Uint(0) |
| 350 | transfer_gas_cost = Uint(0) if value == 0 else GasCosts.CALL_VALUE |
| 351 | message_call_gas = calculate_message_call_gas( |
| 352 | value, |
| 353 | gas, |
| 354 | Uint(evm.gas_left), |
| 355 | extend_memory.cost, |
| 356 | GasCosts.OPCODE_CALL_BASE + create_gas_cost + transfer_gas_cost, |
| 357 | ) |
| 358 | charge_gas(evm, message_call_gas.cost + extend_memory.cost) |
| 359 | if evm.message.is_static and value != U256(0): |
| 360 | raise WriteInStaticContext |
| 361 | evm.memory += b"\x00" * extend_memory.expand_by |
| 362 | sender_balance = get_account( |
| 363 | evm.message.block_env.state, evm.message.current_target |
| 364 | ).balance |
| 365 | if sender_balance < value: |
| 366 | push(evm.stack, U256(0)) |
| 367 | evm.return_data = b"" |
| 368 | evm.gas_left += message_call_gas.sub_call |
| 369 | else: |
| 370 | generic_call( |
| 371 | evm, |
| 372 | message_call_gas.sub_call, |
| 373 | value, |
| 374 | evm.message.current_target, |
| 375 | to, |
| 376 | code_address, |
| 377 | True, |
| 378 | False, |
| 379 | memory_input_start_position, |
| 380 | memory_input_size, |
| 381 | memory_output_start_position, |
| 382 | memory_output_size, |
| 383 | ) |
| 384 | |
| 385 | # PROGRAM COUNTER |
| 386 | 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:
| 390 | """ |
|---|---|
| 391 | Message-call into this account with alternative account’s code. |
| 392 | |
| 393 | Parameters |
| 394 | ---------- |
| 395 | evm : |
| 396 | The current EVM frame. |
| 397 | |
| 398 | """ |
| 399 | # STACK |
| 400 | gas = Uint(pop(evm.stack)) |
| 401 | code_address = to_address_masked(pop(evm.stack)) |
| 402 | value = pop(evm.stack) |
| 403 | memory_input_start_position = pop(evm.stack) |
| 404 | memory_input_size = pop(evm.stack) |
| 405 | memory_output_start_position = pop(evm.stack) |
| 406 | memory_output_size = pop(evm.stack) |
| 407 | |
| 408 | # GAS |
| 409 | to = evm.message.current_target |
| 410 | |
| 411 | extend_memory = calculate_gas_extend_memory( |
| 412 | evm.memory, |
| 413 | [ |
| 414 | (memory_input_start_position, memory_input_size), |
| 415 | (memory_output_start_position, memory_output_size), |
| 416 | ], |
| 417 | ) |
| 418 | transfer_gas_cost = Uint(0) if value == 0 else GasCosts.CALL_VALUE |
| 419 | message_call_gas = calculate_message_call_gas( |
| 420 | value, |
| 421 | gas, |
| 422 | Uint(evm.gas_left), |
| 423 | extend_memory.cost, |
| 424 | GasCosts.OPCODE_CALL_BASE + transfer_gas_cost, |
| 425 | ) |
| 426 | charge_gas(evm, message_call_gas.cost + extend_memory.cost) |
| 427 | |
| 428 | # OPERATION |
| 429 | evm.memory += b"\x00" * extend_memory.expand_by |
| 430 | sender_balance = get_account( |
| 431 | evm.message.block_env.state, evm.message.current_target |
| 432 | ).balance |
| 433 | if sender_balance < value: |
| 434 | push(evm.stack, U256(0)) |
| 435 | evm.return_data = b"" |
| 436 | evm.gas_left += message_call_gas.sub_call |
| 437 | else: |
| 438 | generic_call( |
| 439 | evm, |
| 440 | message_call_gas.sub_call, |
| 441 | value, |
| 442 | evm.message.current_target, |
| 443 | to, |
| 444 | code_address, |
| 445 | True, |
| 446 | False, |
| 447 | memory_input_start_position, |
| 448 | memory_input_size, |
| 449 | memory_output_start_position, |
| 450 | memory_output_size, |
| 451 | ) |
| 452 | |
| 453 | # PROGRAM COUNTER |
| 454 | evm.pc += Uint(1) |
selfdestruct ¶
Halt execution and register account for later deletion.
Parameters
evm : The current EVM frame.
def selfdestruct(evm: Evm) -> None:
| 458 | """ |
|---|---|
| 459 | Halt execution and register account for later deletion. |
| 460 | |
| 461 | Parameters |
| 462 | ---------- |
| 463 | evm : |
| 464 | The current EVM frame. |
| 465 | |
| 466 | """ |
| 467 | # STACK |
| 468 | beneficiary = to_address_masked(pop(evm.stack)) |
| 469 | |
| 470 | # GAS |
| 471 | gas_cost = GasCosts.OPCODE_SELFDESTRUCT_BASE |
| 472 | if ( |
| 473 | not is_account_alive(evm.message.block_env.state, beneficiary) |
| 474 | and get_account( |
| 475 | evm.message.block_env.state, evm.message.current_target |
| 476 | ).balance |
| 477 | != 0 |
| 478 | ): |
| 479 | gas_cost += GasCosts.OPCODE_SELFDESTRUCT_NEW_ACCOUNT |
| 480 | |
| 481 | originator = evm.message.current_target |
| 482 | |
| 483 | refunded_accounts = evm.accounts_to_delete |
| 484 | parent_evm = evm.message.parent_evm |
| 485 | while parent_evm is not None: |
| 486 | refunded_accounts.update(parent_evm.accounts_to_delete) |
| 487 | parent_evm = parent_evm.message.parent_evm |
| 488 | |
| 489 | if originator not in refunded_accounts: |
| 490 | evm.refund_counter += GasCosts.REFUND_SELF_DESTRUCT |
| 491 | |
| 492 | charge_gas(evm, gas_cost) |
| 493 | if evm.message.is_static: |
| 494 | raise WriteInStaticContext |
| 495 | |
| 496 | originator = evm.message.current_target |
| 497 | beneficiary_balance = get_account( |
| 498 | evm.message.block_env.state, beneficiary |
| 499 | ).balance |
| 500 | originator_balance = get_account( |
| 501 | evm.message.block_env.state, originator |
| 502 | ).balance |
| 503 | |
| 504 | # First Transfer to beneficiary |
| 505 | set_account_balance( |
| 506 | evm.message.block_env.state, |
| 507 | beneficiary, |
| 508 | beneficiary_balance + originator_balance, |
| 509 | ) |
| 510 | # Next, Zero the balance of the address being deleted (must come after |
| 511 | # sending to beneficiary in case the contract named itself as the |
| 512 | # beneficiary). |
| 513 | set_account_balance(evm.message.block_env.state, originator, U256(0)) |
| 514 | |
| 515 | # register account for deletion |
| 516 | evm.accounts_to_delete.add(originator) |
| 517 | |
| 518 | # mark beneficiary as touched |
| 519 | if account_exists_and_is_empty(evm.message.block_env.state, beneficiary): |
| 520 | evm.touched_accounts.add(beneficiary) |
| 521 | |
| 522 | # HALT the execution |
| 523 | evm.running = False |
| 524 | |
| 525 | # PROGRAM COUNTER |
| 526 | pass |
delegatecall ¶
Message-call into an account.
Parameters
evm : The current EVM frame.
def delegatecall(evm: Evm) -> None:
| 530 | """ |
|---|---|
| 531 | Message-call into an account. |
| 532 | |
| 533 | Parameters |
| 534 | ---------- |
| 535 | evm : |
| 536 | The current EVM frame. |
| 537 | |
| 538 | """ |
| 539 | # STACK |
| 540 | gas = Uint(pop(evm.stack)) |
| 541 | code_address = to_address_masked(pop(evm.stack)) |
| 542 | memory_input_start_position = pop(evm.stack) |
| 543 | memory_input_size = pop(evm.stack) |
| 544 | memory_output_start_position = pop(evm.stack) |
| 545 | memory_output_size = pop(evm.stack) |
| 546 | |
| 547 | # GAS |
| 548 | extend_memory = calculate_gas_extend_memory( |
| 549 | evm.memory, |
| 550 | [ |
| 551 | (memory_input_start_position, memory_input_size), |
| 552 | (memory_output_start_position, memory_output_size), |
| 553 | ], |
| 554 | ) |
| 555 | message_call_gas = calculate_message_call_gas( |
| 556 | U256(0), |
| 557 | gas, |
| 558 | Uint(evm.gas_left), |
| 559 | extend_memory.cost, |
| 560 | GasCosts.OPCODE_CALL_BASE, |
| 561 | ) |
| 562 | charge_gas(evm, message_call_gas.cost + extend_memory.cost) |
| 563 | |
| 564 | # OPERATION |
| 565 | evm.memory += b"\x00" * extend_memory.expand_by |
| 566 | generic_call( |
| 567 | evm, |
| 568 | message_call_gas.sub_call, |
| 569 | evm.message.value, |
| 570 | evm.message.caller, |
| 571 | evm.message.current_target, |
| 572 | code_address, |
| 573 | False, |
| 574 | False, |
| 575 | memory_input_start_position, |
| 576 | memory_input_size, |
| 577 | memory_output_start_position, |
| 578 | memory_output_size, |
| 579 | ) |
| 580 | |
| 581 | # PROGRAM COUNTER |
| 582 | evm.pc += Uint(1) |
staticcall ¶
Message-call into an account.
Parameters
evm : The current EVM frame.
def staticcall(evm: Evm) -> None:
| 586 | """ |
|---|---|
| 587 | Message-call into an account. |
| 588 | |
| 589 | Parameters |
| 590 | ---------- |
| 591 | evm : |
| 592 | The current EVM frame. |
| 593 | |
| 594 | """ |
| 595 | # STACK |
| 596 | gas = Uint(pop(evm.stack)) |
| 597 | to = to_address_masked(pop(evm.stack)) |
| 598 | memory_input_start_position = pop(evm.stack) |
| 599 | memory_input_size = pop(evm.stack) |
| 600 | memory_output_start_position = pop(evm.stack) |
| 601 | memory_output_size = pop(evm.stack) |
| 602 | |
| 603 | # GAS |
| 604 | extend_memory = calculate_gas_extend_memory( |
| 605 | evm.memory, |
| 606 | [ |
| 607 | (memory_input_start_position, memory_input_size), |
| 608 | (memory_output_start_position, memory_output_size), |
| 609 | ], |
| 610 | ) |
| 611 | |
| 612 | code_address = to |
| 613 | |
| 614 | message_call_gas = calculate_message_call_gas( |
| 615 | U256(0), |
| 616 | gas, |
| 617 | Uint(evm.gas_left), |
| 618 | extend_memory.cost, |
| 619 | GasCosts.OPCODE_CALL_BASE, |
| 620 | ) |
| 621 | charge_gas(evm, message_call_gas.cost + extend_memory.cost) |
| 622 | |
| 623 | # OPERATION |
| 624 | evm.memory += b"\x00" * extend_memory.expand_by |
| 625 | generic_call( |
| 626 | evm, |
| 627 | message_call_gas.sub_call, |
| 628 | U256(0), |
| 629 | evm.message.current_target, |
| 630 | to, |
| 631 | code_address, |
| 632 | True, |
| 633 | True, |
| 634 | memory_input_start_position, |
| 635 | memory_input_size, |
| 636 | memory_output_start_position, |
| 637 | memory_output_size, |
| 638 | ) |
| 639 | |
| 640 | # PROGRAM COUNTER |
| 641 | 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:
| 645 | """ |
|---|---|
| 646 | Stop execution and revert state changes, without consuming all provided gas |
| 647 | and also has the ability to return a reason. |
| 648 | |
| 649 | Parameters |
| 650 | ---------- |
| 651 | evm : |
| 652 | The current EVM frame. |
| 653 | |
| 654 | """ |
| 655 | # STACK |
| 656 | memory_start_index = pop(evm.stack) |
| 657 | size = pop(evm.stack) |
| 658 | |
| 659 | # GAS |
| 660 | extend_memory = calculate_gas_extend_memory( |
| 661 | evm.memory, [(memory_start_index, size)] |
| 662 | ) |
| 663 | |
| 664 | charge_gas(evm, extend_memory.cost) |
| 665 | |
| 666 | # OPERATION |
| 667 | evm.memory += b"\x00" * extend_memory.expand_by |
| 668 | output = memory_read_bytes(evm.memory, memory_start_index, size) |
| 669 | evm.output = Bytes(output) |
| 670 | raise Revert |
| 671 | |
| 672 | # PROGRAM COUNTER |
| 673 | # no-op |