ethereum.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:
| 68 | """ |
|---|---|
| 69 | Core logic used by the `CREATE*` family of opcodes. |
| 70 | """ |
| 71 | # This import causes a circular import error |
| 72 | # if it's not moved inside this method |
| 73 | from ...vm.interpreter import ( |
| 74 | MAX_INIT_CODE_SIZE, |
| 75 | STACK_DEPTH_LIMIT, |
| 76 | process_create_message, |
| 77 | ) |
| 78 | |
| 79 | # Check static context first |
| 80 | if evm.message.is_static: |
| 81 | raise WriteInStaticContext |
| 82 | |
| 83 | # Check max init code size early before memory read |
| 84 | if memory_size > U256(MAX_INIT_CODE_SIZE): |
| 85 | raise OutOfGasError |
| 86 | |
| 87 | tx_state = evm.message.tx_env.state |
| 88 | |
| 89 | call_data = memory_read_bytes( |
| 90 | evm.memory, memory_start_position, memory_size |
| 91 | ) |
| 92 | |
| 93 | create_message_gas = max_message_call_gas(Uint(evm.gas_left)) |
| 94 | evm.gas_left -= create_message_gas |
| 95 | evm.return_data = b"" |
| 96 | |
| 97 | sender_address = evm.message.current_target |
| 98 | sender = get_account(tx_state, sender_address) |
| 99 | |
| 100 | if ( |
| 101 | sender.balance < endowment |
| 102 | or sender.nonce == Uint(2**64 - 1) |
| 103 | or evm.message.depth + Uint(1) > STACK_DEPTH_LIMIT |
| 104 | ): |
| 105 | evm.gas_left += create_message_gas |
| 106 | push(evm.stack, U256(0)) |
| 107 | return |
| 108 | |
| 109 | evm.accessed_addresses.add(contract_address) |
| 110 | |
| 111 | if account_has_code_or_nonce( |
| 112 | tx_state, contract_address |
| 113 | ) or account_has_storage(tx_state, contract_address): |
| 114 | increment_nonce(tx_state, evm.message.current_target) |
| 115 | push(evm.stack, U256(0)) |
| 116 | return |
| 117 | |
| 118 | increment_nonce(tx_state, evm.message.current_target) |
| 119 | |
| 120 | child_message = Message( |
| 121 | block_env=evm.message.block_env, |
| 122 | tx_env=evm.message.tx_env, |
| 123 | caller=evm.message.current_target, |
| 124 | target=Bytes0(), |
| 125 | gas=create_message_gas, |
| 126 | value=endowment, |
| 127 | data=b"", |
| 128 | code=call_data, |
| 129 | current_target=contract_address, |
| 130 | depth=evm.message.depth + Uint(1), |
| 131 | code_address=None, |
| 132 | should_transfer_value=True, |
| 133 | is_static=False, |
| 134 | accessed_addresses=evm.accessed_addresses.copy(), |
| 135 | accessed_storage_keys=evm.accessed_storage_keys.copy(), |
| 136 | disable_precompiles=False, |
| 137 | parent_evm=evm, |
| 138 | ) |
| 139 | child_evm = process_create_message(child_message) |
| 140 | |
| 141 | if child_evm.error: |
| 142 | incorporate_child_on_error(evm, child_evm) |
| 143 | evm.return_data = child_evm.output |
| 144 | push(evm.stack, U256(0)) |
| 145 | else: |
| 146 | incorporate_child_on_success(evm, child_evm) |
| 147 | evm.return_data = b"" |
| 148 | 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:
| 152 | """ |
|---|---|
| 153 | Creates a new account with associated code. |
| 154 | |
| 155 | Parameters |
| 156 | ---------- |
| 157 | evm : |
| 158 | The current EVM frame. |
| 159 | |
| 160 | """ |
| 161 | # STACK |
| 162 | endowment = pop(evm.stack) |
| 163 | memory_start_position = pop(evm.stack) |
| 164 | memory_size = pop(evm.stack) |
| 165 | |
| 166 | # GAS |
| 167 | extend_memory = calculate_gas_extend_memory( |
| 168 | evm.memory, [(memory_start_position, memory_size)] |
| 169 | ) |
| 170 | init_code_gas = init_code_cost(Uint(memory_size)) |
| 171 | |
| 172 | charge_gas( |
| 173 | evm, GasCosts.OPCODE_CREATE_BASE + extend_memory.cost + init_code_gas |
| 174 | ) |
| 175 | |
| 176 | # OPERATION |
| 177 | evm.memory += b"\x00" * extend_memory.expand_by |
| 178 | contract_address = compute_contract_address( |
| 179 | evm.message.current_target, |
| 180 | get_account( |
| 181 | evm.message.tx_env.state, evm.message.current_target |
| 182 | ).nonce, |
| 183 | ) |
| 184 | |
| 185 | generic_create( |
| 186 | evm, |
| 187 | endowment, |
| 188 | contract_address, |
| 189 | memory_start_position, |
| 190 | memory_size, |
| 191 | ) |
| 192 | |
| 193 | # PROGRAM COUNTER |
| 194 | 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:
| 198 | """ |
|---|---|
| 199 | Creates a new account with associated code. |
| 200 | |
| 201 | It's similar to the CREATE opcode except that the address of the new |
| 202 | account depends on the init_code instead of the nonce of sender. |
| 203 | |
| 204 | Parameters |
| 205 | ---------- |
| 206 | evm : |
| 207 | The current EVM frame. |
| 208 | |
| 209 | """ |
| 210 | # STACK |
| 211 | endowment = pop(evm.stack) |
| 212 | memory_start_position = pop(evm.stack) |
| 213 | memory_size = pop(evm.stack) |
| 214 | salt = pop(evm.stack).to_be_bytes32() |
| 215 | |
| 216 | # GAS |
| 217 | extend_memory = calculate_gas_extend_memory( |
| 218 | evm.memory, [(memory_start_position, memory_size)] |
| 219 | ) |
| 220 | call_data_words = ceil32(Uint(memory_size)) // Uint(32) |
| 221 | init_code_gas = init_code_cost(Uint(memory_size)) |
| 222 | charge_gas( |
| 223 | evm, |
| 224 | GasCosts.OPCODE_CREATE_BASE |
| 225 | + GasCosts.OPCODE_KECCACK256_PER_WORD * call_data_words |
| 226 | + extend_memory.cost |
| 227 | + init_code_gas, |
| 228 | ) |
| 229 | |
| 230 | # OPERATION |
| 231 | evm.memory += b"\x00" * extend_memory.expand_by |
| 232 | contract_address = compute_create2_contract_address( |
| 233 | evm.message.current_target, |
| 234 | salt, |
| 235 | memory_read_bytes(evm.memory, memory_start_position, memory_size), |
| 236 | ) |
| 237 | |
| 238 | generic_create( |
| 239 | evm, |
| 240 | endowment, |
| 241 | contract_address, |
| 242 | memory_start_position, |
| 243 | memory_size, |
| 244 | ) |
| 245 | |
| 246 | # PROGRAM COUNTER |
| 247 | evm.pc += Uint(1) |
return_ ¶
Halts execution returning output data.
Parameters
evm : The current EVM frame.
def return_(evm: Evm) -> None:
| 251 | """ |
|---|---|
| 252 | Halts execution returning output data. |
| 253 | |
| 254 | Parameters |
| 255 | ---------- |
| 256 | evm : |
| 257 | The current EVM frame. |
| 258 | |
| 259 | """ |
| 260 | # STACK |
| 261 | memory_start_position = pop(evm.stack) |
| 262 | memory_size = pop(evm.stack) |
| 263 | |
| 264 | # GAS |
| 265 | extend_memory = calculate_gas_extend_memory( |
| 266 | evm.memory, [(memory_start_position, memory_size)] |
| 267 | ) |
| 268 | |
| 269 | charge_gas(evm, GasCosts.ZERO + extend_memory.cost) |
| 270 | |
| 271 | # OPERATION |
| 272 | evm.memory += b"\x00" * extend_memory.expand_by |
| 273 | evm.output = memory_read_bytes( |
| 274 | evm.memory, memory_start_position, memory_size |
| 275 | ) |
| 276 | |
| 277 | evm.running = False |
| 278 | |
| 279 | # PROGRAM COUNTER |
| 280 | 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, code: Bytes, disable_precompiles: bool) -> None:
| 299 | """ |
|---|---|
| 300 | Perform the core logic of the `CALL*` family of opcodes. |
| 301 | """ |
| 302 | from ...vm.interpreter import STACK_DEPTH_LIMIT, process_message |
| 303 | |
| 304 | evm.return_data = b"" |
| 305 | |
| 306 | if evm.message.depth + Uint(1) > STACK_DEPTH_LIMIT: |
| 307 | evm.gas_left += gas |
| 308 | push(evm.stack, U256(0)) |
| 309 | return |
| 310 | |
| 311 | call_data = memory_read_bytes( |
| 312 | evm.memory, memory_input_start_position, memory_input_size |
| 313 | ) |
| 314 | |
| 315 | child_message = Message( |
| 316 | block_env=evm.message.block_env, |
| 317 | tx_env=evm.message.tx_env, |
| 318 | caller=caller, |
| 319 | target=to, |
| 320 | gas=gas, |
| 321 | value=value, |
| 322 | data=call_data, |
| 323 | code=code, |
| 324 | current_target=to, |
| 325 | depth=evm.message.depth + Uint(1), |
| 326 | code_address=code_address, |
| 327 | should_transfer_value=should_transfer_value, |
| 328 | is_static=True if is_staticcall else evm.message.is_static, |
| 329 | accessed_addresses=evm.accessed_addresses.copy(), |
| 330 | accessed_storage_keys=evm.accessed_storage_keys.copy(), |
| 331 | disable_precompiles=disable_precompiles, |
| 332 | parent_evm=evm, |
| 333 | ) |
| 334 | |
| 335 | child_evm = process_message(child_message) |
| 336 | |
| 337 | if child_evm.error: |
| 338 | incorporate_child_on_error(evm, child_evm) |
| 339 | evm.return_data = child_evm.output |
| 340 | push(evm.stack, U256(0)) |
| 341 | else: |
| 342 | incorporate_child_on_success(evm, child_evm) |
| 343 | evm.return_data = child_evm.output |
| 344 | push(evm.stack, CALL_SUCCESS) |
| 345 | |
| 346 | actual_output_size = min(memory_output_size, U256(len(child_evm.output))) |
| 347 | memory_write( |
| 348 | evm.memory, |
| 349 | memory_output_start_position, |
| 350 | child_evm.output[:actual_output_size], |
| 351 | ) |
call ¶
Message-call into an account.
Parameters
evm : The current EVM frame.
def call(evm: Evm) -> None:
| 355 | """ |
|---|---|
| 356 | Message-call into an account. |
| 357 | |
| 358 | Parameters |
| 359 | ---------- |
| 360 | evm : |
| 361 | The current EVM frame. |
| 362 | |
| 363 | """ |
| 364 | # STACK |
| 365 | gas = Uint(pop(evm.stack)) |
| 366 | to = to_address_masked(pop(evm.stack)) |
| 367 | value = pop(evm.stack) |
| 368 | memory_input_start_position = pop(evm.stack) |
| 369 | memory_input_size = pop(evm.stack) |
| 370 | memory_output_start_position = pop(evm.stack) |
| 371 | memory_output_size = pop(evm.stack) |
| 372 | |
| 373 | if evm.message.is_static and value != U256(0): |
| 374 | raise WriteInStaticContext |
| 375 | |
| 376 | # GAS |
| 377 | extend_memory = calculate_gas_extend_memory( |
| 378 | evm.memory, |
| 379 | [ |
| 380 | (memory_input_start_position, memory_input_size), |
| 381 | (memory_output_start_position, memory_output_size), |
| 382 | ], |
| 383 | ) |
| 384 | |
| 385 | is_cold_access = to not in evm.accessed_addresses |
| 386 | if is_cold_access: |
| 387 | access_gas_cost = GasCosts.COLD_ACCOUNT_ACCESS |
| 388 | else: |
| 389 | access_gas_cost = GasCosts.WARM_ACCESS |
| 390 | |
| 391 | transfer_gas_cost = Uint(0) if value == 0 else GasCosts.CALL_VALUE |
| 392 | |
| 393 | # check static gas before state access |
| 394 | check_gas( |
| 395 | evm, |
| 396 | access_gas_cost + transfer_gas_cost + extend_memory.cost, |
| 397 | ) |
| 398 | |
| 399 | # STATE ACCESS |
| 400 | tx_state = evm.message.tx_env.state |
| 401 | if is_cold_access: |
| 402 | evm.accessed_addresses.add(to) |
| 403 | |
| 404 | create_gas_cost = GasCosts.NEW_ACCOUNT |
| 405 | if value == 0 or is_account_alive(tx_state, to): |
| 406 | create_gas_cost = Uint(0) |
| 407 | |
| 408 | extra_gas = access_gas_cost + transfer_gas_cost + create_gas_cost |
| 409 | ( |
| 410 | is_delegated, |
| 411 | code_address, |
| 412 | delegation_access_cost, |
| 413 | ) = calculate_delegation_cost(evm, to) |
| 414 | |
| 415 | if is_delegated: |
| 416 | # check enough gas for delegation access |
| 417 | extra_gas += delegation_access_cost |
| 418 | check_gas(evm, extra_gas + extend_memory.cost) |
| 419 | if code_address not in evm.accessed_addresses: |
| 420 | evm.accessed_addresses.add(code_address) |
| 421 | |
| 422 | code_hash = get_account(tx_state, code_address).code_hash |
| 423 | code = get_code(tx_state, code_hash) |
| 424 | |
| 425 | message_call_gas = calculate_message_call_gas( |
| 426 | value, |
| 427 | gas, |
| 428 | Uint(evm.gas_left), |
| 429 | extend_memory.cost, |
| 430 | extra_gas, |
| 431 | ) |
| 432 | charge_gas(evm, message_call_gas.cost + extend_memory.cost) |
| 433 | |
| 434 | # OPERATION |
| 435 | evm.memory += b"\x00" * extend_memory.expand_by |
| 436 | sender_balance = get_account(tx_state, evm.message.current_target).balance |
| 437 | if sender_balance < value: |
| 438 | push(evm.stack, U256(0)) |
| 439 | evm.return_data = b"" |
| 440 | evm.gas_left += message_call_gas.sub_call |
| 441 | else: |
| 442 | generic_call( |
| 443 | evm, |
| 444 | message_call_gas.sub_call, |
| 445 | value, |
| 446 | evm.message.current_target, |
| 447 | to, |
| 448 | code_address, |
| 449 | True, |
| 450 | False, |
| 451 | memory_input_start_position, |
| 452 | memory_input_size, |
| 453 | memory_output_start_position, |
| 454 | memory_output_size, |
| 455 | code, |
| 456 | is_delegated, |
| 457 | ) |
| 458 | |
| 459 | # PROGRAM COUNTER |
| 460 | 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:
| 464 | """ |
|---|---|
| 465 | Message-call into this account with alternative account's code. |
| 466 | |
| 467 | Parameters |
| 468 | ---------- |
| 469 | evm : |
| 470 | The current EVM frame. |
| 471 | |
| 472 | """ |
| 473 | # STACK |
| 474 | gas = Uint(pop(evm.stack)) |
| 475 | code_address = to_address_masked(pop(evm.stack)) |
| 476 | value = pop(evm.stack) |
| 477 | memory_input_start_position = pop(evm.stack) |
| 478 | memory_input_size = pop(evm.stack) |
| 479 | memory_output_start_position = pop(evm.stack) |
| 480 | memory_output_size = pop(evm.stack) |
| 481 | |
| 482 | # GAS |
| 483 | to = evm.message.current_target |
| 484 | |
| 485 | extend_memory = calculate_gas_extend_memory( |
| 486 | evm.memory, |
| 487 | [ |
| 488 | (memory_input_start_position, memory_input_size), |
| 489 | (memory_output_start_position, memory_output_size), |
| 490 | ], |
| 491 | ) |
| 492 | |
| 493 | is_cold_access = code_address not in evm.accessed_addresses |
| 494 | if is_cold_access: |
| 495 | access_gas_cost = GasCosts.COLD_ACCOUNT_ACCESS |
| 496 | else: |
| 497 | access_gas_cost = GasCosts.WARM_ACCESS |
| 498 | |
| 499 | transfer_gas_cost = Uint(0) if value == 0 else GasCosts.CALL_VALUE |
| 500 | |
| 501 | # check static gas before state access |
| 502 | check_gas( |
| 503 | evm, |
| 504 | access_gas_cost + extend_memory.cost + transfer_gas_cost, |
| 505 | ) |
| 506 | |
| 507 | # STATE ACCESS |
| 508 | tx_state = evm.message.tx_env.state |
| 509 | if is_cold_access: |
| 510 | evm.accessed_addresses.add(code_address) |
| 511 | |
| 512 | extra_gas = access_gas_cost + transfer_gas_cost |
| 513 | ( |
| 514 | is_delegated, |
| 515 | code_address, |
| 516 | delegation_access_cost, |
| 517 | ) = calculate_delegation_cost(evm, code_address) |
| 518 | |
| 519 | if is_delegated: |
| 520 | # check enough gas for delegation access |
| 521 | extra_gas += delegation_access_cost |
| 522 | check_gas(evm, extra_gas + extend_memory.cost) |
| 523 | if code_address not in evm.accessed_addresses: |
| 524 | evm.accessed_addresses.add(code_address) |
| 525 | |
| 526 | code_hash = get_account(tx_state, code_address).code_hash |
| 527 | code = get_code(tx_state, code_hash) |
| 528 | |
| 529 | message_call_gas = calculate_message_call_gas( |
| 530 | value, |
| 531 | gas, |
| 532 | Uint(evm.gas_left), |
| 533 | extend_memory.cost, |
| 534 | extra_gas, |
| 535 | ) |
| 536 | charge_gas(evm, message_call_gas.cost + extend_memory.cost) |
| 537 | |
| 538 | # OPERATION |
| 539 | evm.memory += b"\x00" * extend_memory.expand_by |
| 540 | sender_balance = get_account(tx_state, evm.message.current_target).balance |
| 541 | |
| 542 | if sender_balance < value: |
| 543 | push(evm.stack, U256(0)) |
| 544 | evm.return_data = b"" |
| 545 | evm.gas_left += message_call_gas.sub_call |
| 546 | else: |
| 547 | generic_call( |
| 548 | evm, |
| 549 | message_call_gas.sub_call, |
| 550 | value, |
| 551 | evm.message.current_target, |
| 552 | to, |
| 553 | code_address, |
| 554 | True, |
| 555 | False, |
| 556 | memory_input_start_position, |
| 557 | memory_input_size, |
| 558 | memory_output_start_position, |
| 559 | memory_output_size, |
| 560 | code, |
| 561 | is_delegated, |
| 562 | ) |
| 563 | |
| 564 | # PROGRAM COUNTER |
| 565 | evm.pc += Uint(1) |
selfdestruct ¶
Halt execution and register account for later deletion.
Parameters
evm : The current EVM frame.
def selfdestruct(evm: Evm) -> None:
| 569 | """ |
|---|---|
| 570 | Halt execution and register account for later deletion. |
| 571 | |
| 572 | Parameters |
| 573 | ---------- |
| 574 | evm : |
| 575 | The current EVM frame. |
| 576 | |
| 577 | """ |
| 578 | if evm.message.is_static: |
| 579 | raise WriteInStaticContext |
| 580 | |
| 581 | # STACK |
| 582 | beneficiary = to_address_masked(pop(evm.stack)) |
| 583 | |
| 584 | # GAS |
| 585 | gas_cost = GasCosts.OPCODE_SELFDESTRUCT_BASE |
| 586 | |
| 587 | is_cold_access = beneficiary not in evm.accessed_addresses |
| 588 | if is_cold_access: |
| 589 | gas_cost += GasCosts.COLD_ACCOUNT_ACCESS |
| 590 | |
| 591 | # check access gas cost before state access |
| 592 | check_gas(evm, gas_cost) |
| 593 | |
| 594 | # STATE ACCESS |
| 595 | tx_state = evm.message.tx_env.state |
| 596 | if is_cold_access: |
| 597 | evm.accessed_addresses.add(beneficiary) |
| 598 | |
| 599 | if ( |
| 600 | not is_account_alive(tx_state, beneficiary) |
| 601 | and get_account(tx_state, evm.message.current_target).balance != 0 |
| 602 | ): |
| 603 | gas_cost += GasCosts.OPCODE_SELFDESTRUCT_NEW_ACCOUNT |
| 604 | |
| 605 | charge_gas(evm, gas_cost) |
| 606 | |
| 607 | originator = evm.message.current_target |
| 608 | originator_balance = get_account(tx_state, originator).balance |
| 609 | |
| 610 | # Transfer balance |
| 611 | move_ether(tx_state, originator, beneficiary, originator_balance) |
| 612 | |
| 613 | # Emit transfer or burn log |
| 614 | if originator in tx_state.created_accounts and beneficiary == originator: |
| 615 | emit_burn_log(evm, originator, originator_balance) |
| 616 | elif beneficiary != originator: |
| 617 | emit_transfer_log(evm, originator, beneficiary, originator_balance) |
| 618 | |
| 619 | # Register account for deletion iff created in same transaction |
| 620 | if originator in tx_state.created_accounts: |
| 621 | # If beneficiary and originator are the same then the ether is burnt. |
| 622 | set_account_balance(tx_state, originator, U256(0)) |
| 623 | evm.accounts_to_delete.add(originator) |
| 624 | |
| 625 | # HALT the execution |
| 626 | evm.running = False |
| 627 | |
| 628 | # PROGRAM COUNTER |
| 629 | pass |
delegatecall ¶
Message-call into an account.
Parameters
evm : The current EVM frame.
def delegatecall(evm: Evm) -> None:
| 633 | """ |
|---|---|
| 634 | Message-call into an account. |
| 635 | |
| 636 | Parameters |
| 637 | ---------- |
| 638 | evm : |
| 639 | The current EVM frame. |
| 640 | |
| 641 | """ |
| 642 | # STACK |
| 643 | gas = Uint(pop(evm.stack)) |
| 644 | code_address = to_address_masked(pop(evm.stack)) |
| 645 | memory_input_start_position = pop(evm.stack) |
| 646 | memory_input_size = pop(evm.stack) |
| 647 | memory_output_start_position = pop(evm.stack) |
| 648 | memory_output_size = pop(evm.stack) |
| 649 | |
| 650 | # GAS |
| 651 | extend_memory = calculate_gas_extend_memory( |
| 652 | evm.memory, |
| 653 | [ |
| 654 | (memory_input_start_position, memory_input_size), |
| 655 | (memory_output_start_position, memory_output_size), |
| 656 | ], |
| 657 | ) |
| 658 | |
| 659 | is_cold_access = code_address not in evm.accessed_addresses |
| 660 | if is_cold_access: |
| 661 | access_gas_cost = GasCosts.COLD_ACCOUNT_ACCESS |
| 662 | else: |
| 663 | access_gas_cost = GasCosts.WARM_ACCESS |
| 664 | |
| 665 | # check static gas before state access |
| 666 | check_gas(evm, access_gas_cost + extend_memory.cost) |
| 667 | |
| 668 | # STATE ACCESS |
| 669 | tx_state = evm.message.tx_env.state |
| 670 | if is_cold_access: |
| 671 | evm.accessed_addresses.add(code_address) |
| 672 | |
| 673 | extra_gas = access_gas_cost |
| 674 | ( |
| 675 | is_delegated, |
| 676 | code_address, |
| 677 | delegation_access_cost, |
| 678 | ) = calculate_delegation_cost(evm, code_address) |
| 679 | |
| 680 | if is_delegated: |
| 681 | # check enough gas for delegation access |
| 682 | extra_gas += delegation_access_cost |
| 683 | check_gas(evm, extra_gas + extend_memory.cost) |
| 684 | if code_address not in evm.accessed_addresses: |
| 685 | evm.accessed_addresses.add(code_address) |
| 686 | |
| 687 | code_hash = get_account(tx_state, code_address).code_hash |
| 688 | code = get_code(tx_state, code_hash) |
| 689 | |
| 690 | message_call_gas = calculate_message_call_gas( |
| 691 | U256(0), |
| 692 | gas, |
| 693 | Uint(evm.gas_left), |
| 694 | extend_memory.cost, |
| 695 | extra_gas, |
| 696 | ) |
| 697 | charge_gas(evm, message_call_gas.cost + extend_memory.cost) |
| 698 | |
| 699 | # OPERATION |
| 700 | evm.memory += b"\x00" * extend_memory.expand_by |
| 701 | generic_call( |
| 702 | evm, |
| 703 | message_call_gas.sub_call, |
| 704 | evm.message.value, |
| 705 | evm.message.caller, |
| 706 | evm.message.current_target, |
| 707 | code_address, |
| 708 | False, |
| 709 | False, |
| 710 | memory_input_start_position, |
| 711 | memory_input_size, |
| 712 | memory_output_start_position, |
| 713 | memory_output_size, |
| 714 | code, |
| 715 | is_delegated, |
| 716 | ) |
| 717 | |
| 718 | # PROGRAM COUNTER |
| 719 | evm.pc += Uint(1) |
staticcall ¶
Message-call into an account.
Parameters
evm : The current EVM frame.
def staticcall(evm: Evm) -> None:
| 723 | """ |
|---|---|
| 724 | Message-call into an account. |
| 725 | |
| 726 | Parameters |
| 727 | ---------- |
| 728 | evm : |
| 729 | The current EVM frame. |
| 730 | |
| 731 | """ |
| 732 | # STACK |
| 733 | gas = Uint(pop(evm.stack)) |
| 734 | to = to_address_masked(pop(evm.stack)) |
| 735 | memory_input_start_position = pop(evm.stack) |
| 736 | memory_input_size = pop(evm.stack) |
| 737 | memory_output_start_position = pop(evm.stack) |
| 738 | memory_output_size = pop(evm.stack) |
| 739 | |
| 740 | # GAS |
| 741 | extend_memory = calculate_gas_extend_memory( |
| 742 | evm.memory, |
| 743 | [ |
| 744 | (memory_input_start_position, memory_input_size), |
| 745 | (memory_output_start_position, memory_output_size), |
| 746 | ], |
| 747 | ) |
| 748 | |
| 749 | is_cold_access = to not in evm.accessed_addresses |
| 750 | if is_cold_access: |
| 751 | access_gas_cost = GasCosts.COLD_ACCOUNT_ACCESS |
| 752 | else: |
| 753 | access_gas_cost = GasCosts.WARM_ACCESS |
| 754 | |
| 755 | # check static gas before state access |
| 756 | check_gas(evm, access_gas_cost + extend_memory.cost) |
| 757 | |
| 758 | # STATE ACCESS |
| 759 | tx_state = evm.message.tx_env.state |
| 760 | if is_cold_access: |
| 761 | evm.accessed_addresses.add(to) |
| 762 | |
| 763 | extra_gas = access_gas_cost |
| 764 | ( |
| 765 | is_delegated, |
| 766 | code_address, |
| 767 | delegation_access_cost, |
| 768 | ) = calculate_delegation_cost(evm, to) |
| 769 | |
| 770 | if is_delegated: |
| 771 | # check enough gas for delegation access |
| 772 | extra_gas += delegation_access_cost |
| 773 | check_gas(evm, extra_gas + extend_memory.cost) |
| 774 | if code_address not in evm.accessed_addresses: |
| 775 | evm.accessed_addresses.add(code_address) |
| 776 | |
| 777 | code_hash = get_account(tx_state, code_address).code_hash |
| 778 | code = get_code(tx_state, code_hash) |
| 779 | |
| 780 | message_call_gas = calculate_message_call_gas( |
| 781 | U256(0), |
| 782 | gas, |
| 783 | Uint(evm.gas_left), |
| 784 | extend_memory.cost, |
| 785 | extra_gas, |
| 786 | ) |
| 787 | charge_gas(evm, message_call_gas.cost + extend_memory.cost) |
| 788 | |
| 789 | # OPERATION |
| 790 | evm.memory += b"\x00" * extend_memory.expand_by |
| 791 | generic_call( |
| 792 | evm, |
| 793 | message_call_gas.sub_call, |
| 794 | U256(0), |
| 795 | evm.message.current_target, |
| 796 | to, |
| 797 | code_address, |
| 798 | True, |
| 799 | True, |
| 800 | memory_input_start_position, |
| 801 | memory_input_size, |
| 802 | memory_output_start_position, |
| 803 | memory_output_size, |
| 804 | code, |
| 805 | is_delegated, |
| 806 | ) |
| 807 | |
| 808 | # PROGRAM COUNTER |
| 809 | 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:
| 813 | """ |
|---|---|
| 814 | Stop execution and revert state changes, without consuming all provided gas |
| 815 | and also has the ability to return a reason. |
| 816 | |
| 817 | Parameters |
| 818 | ---------- |
| 819 | evm : |
| 820 | The current EVM frame. |
| 821 | |
| 822 | """ |
| 823 | # STACK |
| 824 | memory_start_index = pop(evm.stack) |
| 825 | size = pop(evm.stack) |
| 826 | |
| 827 | # GAS |
| 828 | extend_memory = calculate_gas_extend_memory( |
| 829 | evm.memory, [(memory_start_index, size)] |
| 830 | ) |
| 831 | |
| 832 | charge_gas(evm, extend_memory.cost) |
| 833 | |
| 834 | # OPERATION |
| 835 | evm.memory += b"\x00" * extend_memory.expand_by |
| 836 | output = memory_read_bytes(evm.memory, memory_start_index, size) |
| 837 | evm.output = Bytes(output) |
| 838 | raise Revert |
| 839 | |
| 840 | # PROGRAM COUNTER |
| 841 | # no-op |