ethereum.forks.bogota.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 ¶
Run the child-frame lifecycle for the CREATE* family of opcodes.
The opcode has already priced the operation itself; this function runs the lifecycle: preflight checks that abort without spawning, the destination access with its account-creation charge and collision check, the child's gas grant, the child frame itself, and the resolution of its outcome back into the creating frame.
def generic_create(evm: Evm, endowment: U256, contract_address: Address, memory_start_position: U256, memory_size: U256) -> None:
| 67 | <snip> |
|---|---|
| 76 | # These imports cause a circular import error |
| 77 | # if they're not moved inside this method |
| 78 | from ...vm.interpreter import STACK_DEPTH_LIMIT, process_create |
| 79 | from ...vm.runtime import get_valid_jump_destinations |
| 80 | |
| 81 | tx_state = evm.tx_env.state |
| 82 | |
| 83 | init_code = memory_read_bytes( |
| 84 | evm.memory, memory_start_position, memory_size |
| 85 | ) |
| 86 | |
| 87 | evm.return_data = b"" |
| 88 | |
| 89 | # PREFLIGHT |
| 90 | # Abort without spawning the child: nothing has been charged or |
| 91 | # withheld for it yet. |
| 92 | sender_address = evm.current_target |
| 93 | sender = get_account(tx_state, sender_address) |
| 94 | |
| 95 | if ( |
| 96 | sender.balance < endowment |
| 97 | or sender.nonce == Uint(2**64 - 1) |
| 98 | or evm.depth + Uint(1) > STACK_DEPTH_LIMIT |
| 99 | ): |
| 100 | push(evm.stack, U256(0)) |
| 101 | return |
| 102 | |
| 103 | # DESTINATION ACCESS |
| 104 | # The account-creation charge is decided by existence alone, |
| 105 | # independently of the collision outcome below. |
| 106 | evm.accessed_addresses.add(contract_address) |
| 107 | |
| 108 | new_account_charged = not is_account_alive(tx_state, contract_address) |
| 109 | if new_account_charged: |
| 110 | charge_state_gas(evm, StateGasCosts.NEW_ACCOUNT) |
| 111 | |
| 112 | # CHILD GRANT |
| 113 | # Withhold all but one 64th of the execution gas. |
| 114 | create_message_gas = withhold_create_gas(evm.gas_meter) |
| 115 | |
| 116 | # On a collision the child's execution-gas grant is consumed and no |
| 117 | # account is created. A collision target has code or a nonce, so |
| 118 | # the account-creation charge above was never taken. |
| 119 | if not account_deployable(tx_state, contract_address): |
| 120 | increment_nonce(tx_state, sender_address) |
| 121 | push(evm.stack, U256(0)) |
| 122 | return |
| 123 | |
| 124 | # In the reservoir model the whole reservoir rides along (no 63/64 |
| 125 | # rule for state gas) and is restored when the child returns. A |
| 126 | # frame transaction's state gas pool is frame-scoped on the frame |
| 127 | # context, so there is nothing to hand down: the child's meter |
| 128 | # carries no reservoir either. |
| 129 | if evm.gas_meter.reservoir is None: |
| 130 | child_reservoir = None |
| 131 | else: |
| 132 | create_message_state_gas_reservoir = drain_state_gas_reservoir( |
| 133 | evm.gas_meter |
| 134 | ) |
| 135 | child_reservoir = StateGasReservoir( |
| 136 | state_gas_left=create_message_state_gas_reservoir, |
| 137 | state_gas_baseline=create_message_state_gas_reservoir, |
| 138 | ) |
| 139 | |
| 140 | increment_nonce(tx_state, sender_address) |
| 141 | |
| 142 | # DISPATCH |
| 143 | |
| 144 | child_evm = Evm( |
| 145 | # Context |
| 146 | block_env=evm.block_env, |
| 147 | tx_env=evm.tx_env, |
| 148 | parent_evm=evm, |
| 149 | depth=evm.depth + Uint(1), |
| 150 | # Call Parameters |
| 151 | caller=evm.current_target, |
| 152 | current_target=contract_address, |
| 153 | value=endowment, |
| 154 | call_data=b"", |
| 155 | should_transfer_value=True, |
| 156 | is_static=False, |
| 157 | disable_precompiles=False, |
| 158 | # Code |
| 159 | code_address=None, |
| 160 | code=init_code, |
| 161 | valid_jump_destinations=get_valid_jump_destinations(init_code), |
| 162 | # Machine State |
| 163 | gas_meter=GasMeter( |
| 164 | gas_left=create_message_gas, |
| 165 | reservoir=child_reservoir, |
| 166 | ), |
| 167 | pc=Uint(0), |
| 168 | stack=[], |
| 169 | memory=bytearray(), |
| 170 | return_data=b"", |
| 171 | # Accrued Effects |
| 172 | logs=(), |
| 173 | accounts_to_delete=set(), |
| 174 | accessed_addresses=evm.accessed_addresses.copy(), |
| 175 | accessed_storage_keys=evm.accessed_storage_keys.copy(), |
| 176 | # Outcome |
| 177 | running=True, |
| 178 | output=b"", |
| 179 | error=None, |
| 180 | ) |
| 181 | child_evm = process_create(child_evm) |
| 182 | |
| 183 | # OUTCOME |
| 184 | # The child settled its own gas; absorb it and resolve the |
| 185 | # account-creation charge by the state's fate: it refills when a |
| 186 | # charged creation died with the failed child — to the meter's |
| 187 | # reservoir, or to the executing frame that paid it. |
| 188 | incorporate_child(evm, child_evm) |
| 189 | if child_evm.error: |
| 190 | if new_account_charged: |
| 191 | refund_state_gas(evm, StateGasCosts.NEW_ACCOUNT) |
| 192 | evm.return_data = child_evm.output |
| 193 | push(evm.stack, U256(0)) |
| 194 | else: |
| 195 | evm.return_data = b"" |
| 196 | push(evm.stack, U256.from_be_bytes(child_evm.current_target)) |
create ¶
Creates a new account with associated code.
Parameters
evm : The current EVM frame.
def create(evm: Evm) -> None:
| 200 | <snip> |
|---|---|
| 209 | # This import causes a circular import error |
| 210 | # if it's not moved inside this method |
| 211 | from ...vm.interpreter import MAX_INIT_CODE_SIZE |
| 212 | |
| 213 | if evm.is_static: |
| 214 | raise WriteInStaticContext |
| 215 | |
| 216 | # STACK |
| 217 | endowment = pop(evm.stack) |
| 218 | memory_start_position = pop(evm.stack) |
| 219 | memory_size = pop(evm.stack) |
| 220 | |
| 221 | # GAS |
| 222 | extend_memory = calculate_gas_extend_memory( |
| 223 | evm.memory, [(memory_start_position, memory_size)] |
| 224 | ) |
| 225 | init_code_gas = init_code_cost(Uint(memory_size)) |
| 226 | |
| 227 | charge_gas( |
| 228 | evm, |
| 229 | GasCosts.CREATE_ACCESS + extend_memory.cost + init_code_gas, |
| 230 | ) |
| 231 | |
| 232 | if memory_size > U256(MAX_INIT_CODE_SIZE): |
| 233 | raise OutOfGasError |
| 234 | |
| 235 | # OPERATION |
| 236 | evm.memory += b"\x00" * extend_memory.expand_by |
| 237 | contract_address = compute_contract_address( |
| 238 | evm.current_target, |
| 239 | get_account(evm.tx_env.state, evm.current_target).nonce, |
| 240 | ) |
| 241 | |
| 242 | generic_create( |
| 243 | evm, |
| 244 | endowment, |
| 245 | contract_address, |
| 246 | memory_start_position, |
| 247 | memory_size, |
| 248 | ) |
| 249 | |
| 250 | # PROGRAM COUNTER |
| 251 | 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:
| 255 | <snip> |
|---|---|
| 267 | # This import causes a circular import error |
| 268 | # if it's not moved inside this method |
| 269 | from ...vm.interpreter import MAX_INIT_CODE_SIZE |
| 270 | |
| 271 | if evm.is_static: |
| 272 | raise WriteInStaticContext |
| 273 | |
| 274 | # STACK |
| 275 | endowment = pop(evm.stack) |
| 276 | memory_start_position = pop(evm.stack) |
| 277 | memory_size = pop(evm.stack) |
| 278 | salt = pop(evm.stack).to_be_bytes32() |
| 279 | |
| 280 | # GAS |
| 281 | extend_memory = calculate_gas_extend_memory( |
| 282 | evm.memory, [(memory_start_position, memory_size)] |
| 283 | ) |
| 284 | call_data_words = ceil32(Uint(memory_size)) // Uint(32) |
| 285 | init_code_gas = init_code_cost(Uint(memory_size)) |
| 286 | charge_gas( |
| 287 | evm, |
| 288 | ExecutionGas( |
| 289 | GasCosts.CREATE_ACCESS |
| 290 | + GasCosts.OPCODE_KECCAK256_PER_WORD * call_data_words |
| 291 | + extend_memory.cost |
| 292 | + init_code_gas |
| 293 | ), |
| 294 | ) |
| 295 | |
| 296 | if memory_size > U256(MAX_INIT_CODE_SIZE): |
| 297 | raise OutOfGasError |
| 298 | |
| 299 | # OPERATION |
| 300 | evm.memory += b"\x00" * extend_memory.expand_by |
| 301 | contract_address = compute_create2_contract_address( |
| 302 | evm.current_target, |
| 303 | salt, |
| 304 | memory_read_bytes(evm.memory, memory_start_position, memory_size), |
| 305 | ) |
| 306 | |
| 307 | generic_create( |
| 308 | evm, |
| 309 | endowment, |
| 310 | contract_address, |
| 311 | memory_start_position, |
| 312 | memory_size, |
| 313 | ) |
| 314 | |
| 315 | # PROGRAM COUNTER |
| 316 | evm.pc += Uint(1) |
return_ ¶
Halts execution returning output data.
Parameters
evm : The current EVM frame.
def return_(evm: Evm) -> None:
| 320 | <snip> |
|---|---|
| 329 | # STACK |
| 330 | memory_start_position = pop(evm.stack) |
| 331 | memory_size = pop(evm.stack) |
| 332 | |
| 333 | # GAS |
| 334 | extend_memory = calculate_gas_extend_memory( |
| 335 | evm.memory, [(memory_start_position, memory_size)] |
| 336 | ) |
| 337 | |
| 338 | charge_gas(evm, GasCosts.ZERO + extend_memory.cost) |
| 339 | |
| 340 | # OPERATION |
| 341 | evm.memory += b"\x00" * extend_memory.expand_by |
| 342 | evm.output = memory_read_bytes( |
| 343 | evm.memory, memory_start_position, memory_size |
| 344 | ) |
| 345 | |
| 346 | evm.running = False |
| 347 | |
| 348 | # PROGRAM COUNTER |
| 349 | pass |
GenericCall ¶
Parameters for the core logic of the CALL* family of opcodes.
| 352 | @final |
|---|
| 353 | @dataclass |
|---|
class GenericCall:
gas¶
| 359 | gas: ExecutionGas |
|---|
value¶
| 360 | value: U256 |
|---|
caller¶
| 361 | caller: Address |
|---|
to¶
| 362 | to: Address |
|---|
code_address¶
| 363 | code_address: Address |
|---|
should_transfer_value¶
| 364 | should_transfer_value: bool |
|---|
is_staticcall¶
| 365 | is_staticcall: bool |
|---|
memory_input_start_position¶
| 366 | memory_input_start_position: U256 |
|---|
memory_input_size¶
| 367 | memory_input_size: U256 |
|---|
memory_output_start_position¶
| 368 | memory_output_start_position: U256 |
|---|
memory_output_size¶
| 369 | memory_output_size: U256 |
|---|
code¶
| 370 | code: Bytes |
|---|
disable_precompiles¶
| 371 | disable_precompiles: bool |
|---|
new_account_charged¶
| 372 | new_account_charged: bool = False |
|---|
insufficient_balance¶
True when the calling account cannot cover value; the call then
aborts in preflight without spawning the child frame.
| 373 | insufficient_balance: bool = False |
|---|
generic_call ¶
Run the child-frame lifecycle for the CALL* family of opcodes.
The opcode has already priced the call and withheld the child's execution gas grant; this function only runs the lifecycle: preflight checks that abort without spawning, the state dimension's hand-down, the child frame itself, and the resolution of its outcome back into the calling frame.
def generic_call(evm: Evm, params: GenericCall) -> None:
| 381 | <snip> |
|---|---|
| 390 | from ...vm.interpreter import STACK_DEPTH_LIMIT, process_call |
| 391 | from ...vm.runtime import get_valid_jump_destinations |
| 392 | |
| 393 | evm.return_data = b"" |
| 394 | |
| 395 | # PREFLIGHT |
| 396 | # Abort without spawning the child: the execution gas grant returns |
| 397 | # untouched and any account-creation charge refills — to the |
| 398 | # meter's reservoir, or to the executing frame that just paid it. |
| 399 | if evm.depth + Uint(1) > STACK_DEPTH_LIMIT or params.insufficient_balance: |
| 400 | restore_child_gas(evm.gas_meter, params.gas) |
| 401 | if params.new_account_charged: |
| 402 | refund_state_gas(evm, StateGasCosts.NEW_ACCOUNT) |
| 403 | push(evm.stack, U256(0)) |
| 404 | return |
| 405 | |
| 406 | # CHILD GRANT (STATE DIMENSION) |
| 407 | # In the reservoir model the whole reservoir rides along (no 63/64 |
| 408 | # rule for state gas) and is restored when the child returns. A |
| 409 | # frame transaction's state gas pool is frame-scoped on the frame |
| 410 | # context, so there is nothing to hand down: the child's meter |
| 411 | # carries no reservoir either. |
| 412 | if evm.gas_meter.reservoir is None: |
| 413 | child_reservoir = None |
| 414 | else: |
| 415 | call_state_gas_reservoir = drain_state_gas_reservoir(evm.gas_meter) |
| 416 | child_reservoir = StateGasReservoir( |
| 417 | state_gas_left=call_state_gas_reservoir, |
| 418 | state_gas_baseline=call_state_gas_reservoir, |
| 419 | ) |
| 420 | |
| 421 | # DISPATCH |
| 422 | call_data = memory_read_bytes( |
| 423 | evm.memory, |
| 424 | params.memory_input_start_position, |
| 425 | params.memory_input_size, |
| 426 | ) |
| 427 | |
| 428 | child_evm = Evm( |
| 429 | # Context |
| 430 | block_env=evm.block_env, |
| 431 | tx_env=evm.tx_env, |
| 432 | parent_evm=evm, |
| 433 | depth=evm.depth + Uint(1), |
| 434 | # Call Parameters |
| 435 | caller=params.caller, |
| 436 | current_target=params.to, |
| 437 | value=params.value, |
| 438 | call_data=call_data, |
| 439 | should_transfer_value=params.should_transfer_value, |
| 440 | is_static=params.is_staticcall or evm.is_static, |
| 441 | disable_precompiles=params.disable_precompiles, |
| 442 | # Code |
| 443 | code_address=params.code_address, |
| 444 | code=params.code, |
| 445 | valid_jump_destinations=get_valid_jump_destinations(params.code), |
| 446 | # Machine State |
| 447 | gas_meter=GasMeter( |
| 448 | gas_left=params.gas, |
| 449 | reservoir=child_reservoir, |
| 450 | ), |
| 451 | pc=Uint(0), |
| 452 | stack=[], |
| 453 | memory=bytearray(), |
| 454 | return_data=b"", |
| 455 | # Accrued Effects |
| 456 | logs=(), |
| 457 | accounts_to_delete=set(), |
| 458 | accessed_addresses=evm.accessed_addresses.copy(), |
| 459 | accessed_storage_keys=evm.accessed_storage_keys.copy(), |
| 460 | # Outcome |
| 461 | running=True, |
| 462 | output=b"", |
| 463 | error=None, |
| 464 | ) |
| 465 | |
| 466 | child_evm = process_call(child_evm) |
| 467 | |
| 468 | # OUTCOME |
| 469 | # The child settled its own gas; absorb it and resolve the |
| 470 | # account-creation charge by the state's fate: it refills when a |
| 471 | # charged creation died with the failed child. |
| 472 | incorporate_child(evm, child_evm) |
| 473 | evm.return_data = child_evm.output |
| 474 | if child_evm.error: |
| 475 | if params.new_account_charged: |
| 476 | refund_state_gas(evm, StateGasCosts.NEW_ACCOUNT) |
| 477 | push(evm.stack, U256(0)) |
| 478 | else: |
| 479 | push(evm.stack, CALL_SUCCESS) |
| 480 | |
| 481 | actual_output_size = min( |
| 482 | params.memory_output_size, U256(len(child_evm.output)) |
| 483 | ) |
| 484 | memory_write( |
| 485 | evm.memory, |
| 486 | params.memory_output_start_position, |
| 487 | child_evm.output[:actual_output_size], |
| 488 | ) |
call ¶
Message-call into an account.
Parameters
evm : The current EVM frame.
def call(evm: Evm) -> None:
| 492 | <snip> |
|---|---|
| 501 | # STACK |
| 502 | gas = ExecutionGas(Uint(pop(evm.stack))) |
| 503 | to = to_address_masked(pop(evm.stack)) |
| 504 | value = pop(evm.stack) |
| 505 | memory_input_start_position = pop(evm.stack) |
| 506 | memory_input_size = pop(evm.stack) |
| 507 | memory_output_start_position = pop(evm.stack) |
| 508 | memory_output_size = pop(evm.stack) |
| 509 | |
| 510 | if evm.is_static and value != U256(0): |
| 511 | raise WriteInStaticContext |
| 512 | |
| 513 | # GAS (STATE-INDEPENDENT) |
| 514 | # Price what is computable without touching state, and check it is |
| 515 | # affordable before any state access is performed. |
| 516 | extend_memory = calculate_gas_extend_memory( |
| 517 | evm.memory, |
| 518 | [ |
| 519 | (memory_input_start_position, memory_input_size), |
| 520 | (memory_output_start_position, memory_output_size), |
| 521 | ], |
| 522 | ) |
| 523 | |
| 524 | is_cold_access = to not in evm.accessed_addresses |
| 525 | if is_cold_access: |
| 526 | access_gas_cost = GasCosts.COLD_ACCOUNT_ACCESS |
| 527 | else: |
| 528 | access_gas_cost = GasCosts.WARM_ACCESS |
| 529 | |
| 530 | transfer_gas_cost = GasCosts.ZERO if value == 0 else GasCosts.CALL_VALUE |
| 531 | |
| 532 | check_gas( |
| 533 | evm, |
| 534 | access_gas_cost + transfer_gas_cost + extend_memory.cost, |
| 535 | ) |
| 536 | |
| 537 | # STATE ACCESS (STATE-DEPENDENT GAS) |
| 538 | # Perform the accesses and complete the state-dependent pricing -- |
| 539 | # a delegation adds its access cost -- then charge the execution |
| 540 | # gas. |
| 541 | tx_state = evm.tx_env.state |
| 542 | if is_cold_access: |
| 543 | evm.accessed_addresses.add(to) |
| 544 | |
| 545 | extra_gas = access_gas_cost + transfer_gas_cost |
| 546 | ( |
| 547 | is_delegated, |
| 548 | code_address, |
| 549 | delegation_access_cost, |
| 550 | ) = calculate_delegation_cost(evm, to) |
| 551 | |
| 552 | if is_delegated: |
| 553 | # check enough gas for delegation access |
| 554 | extra_gas += delegation_access_cost |
| 555 | check_gas(evm, extra_gas + extend_memory.cost) |
| 556 | if code_address not in evm.accessed_addresses: |
| 557 | evm.accessed_addresses.add(code_address) |
| 558 | |
| 559 | code_hash = get_account(tx_state, code_address).code_hash |
| 560 | code = get_code(tx_state, code_hash) |
| 561 | |
| 562 | charge_gas(evm, extra_gas + extend_memory.cost) |
| 563 | |
| 564 | # STATE GAS |
| 565 | # A value transfer that will create the recipient is charged by |
| 566 | # the frame whose opcode causes it; refilled in `generic_call` |
| 567 | # whenever the creation fails or never happens. |
| 568 | has_value = value != 0 |
| 569 | new_account_charged = has_value and not is_account_alive(tx_state, to) |
| 570 | if new_account_charged: |
| 571 | charge_state_gas(evm, StateGasCosts.NEW_ACCOUNT) |
| 572 | |
| 573 | # CHILD GRANT |
| 574 | # Computed after every charge above, so any state-gas spill has |
| 575 | # already thinned `gas_left`. The state dimension is handed down |
| 576 | # in `generic_call`. |
| 577 | message_call_gas = calculate_message_call_gas( |
| 578 | value, |
| 579 | gas, |
| 580 | evm.gas_meter.gas_left, |
| 581 | memory_cost=GasCosts.ZERO, |
| 582 | extra_gas=GasCosts.ZERO, |
| 583 | ) |
| 584 | charge_gas(evm, message_call_gas.cost) |
| 585 | |
| 586 | # OPERATION |
| 587 | evm.memory += b"\x00" * extend_memory.expand_by |
| 588 | sender_balance = get_account(tx_state, evm.current_target).balance |
| 589 | |
| 590 | generic_call( |
| 591 | evm, |
| 592 | GenericCall( |
| 593 | gas=message_call_gas.sub_call, |
| 594 | value=value, |
| 595 | caller=evm.current_target, |
| 596 | to=to, |
| 597 | code_address=code_address, |
| 598 | should_transfer_value=True, |
| 599 | is_staticcall=False, |
| 600 | memory_input_start_position=memory_input_start_position, |
| 601 | memory_input_size=memory_input_size, |
| 602 | memory_output_start_position=memory_output_start_position, |
| 603 | memory_output_size=memory_output_size, |
| 604 | code=code, |
| 605 | disable_precompiles=is_delegated, |
| 606 | new_account_charged=new_account_charged, |
| 607 | insufficient_balance=sender_balance < value, |
| 608 | ), |
| 609 | ) |
| 610 | |
| 611 | # PROGRAM COUNTER |
| 612 | 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:
| 616 | <snip> |
|---|---|
| 625 | # STACK |
| 626 | gas = ExecutionGas(Uint(pop(evm.stack))) |
| 627 | code_address = to_address_masked(pop(evm.stack)) |
| 628 | value = pop(evm.stack) |
| 629 | memory_input_start_position = pop(evm.stack) |
| 630 | memory_input_size = pop(evm.stack) |
| 631 | memory_output_start_position = pop(evm.stack) |
| 632 | memory_output_size = pop(evm.stack) |
| 633 | |
| 634 | # GAS (STATE-INDEPENDENT) |
| 635 | # Price what is computable without touching state, and check it is |
| 636 | # affordable before any state access is performed. |
| 637 | to = evm.current_target |
| 638 | |
| 639 | extend_memory = calculate_gas_extend_memory( |
| 640 | evm.memory, |
| 641 | [ |
| 642 | (memory_input_start_position, memory_input_size), |
| 643 | (memory_output_start_position, memory_output_size), |
| 644 | ], |
| 645 | ) |
| 646 | |
| 647 | is_cold_access = code_address not in evm.accessed_addresses |
| 648 | if is_cold_access: |
| 649 | access_gas_cost = GasCosts.COLD_ACCOUNT_ACCESS |
| 650 | else: |
| 651 | access_gas_cost = GasCosts.WARM_ACCESS |
| 652 | |
| 653 | transfer_gas_cost = GasCosts.ZERO if value == 0 else GasCosts.CALL_VALUE |
| 654 | |
| 655 | check_gas( |
| 656 | evm, |
| 657 | access_gas_cost + extend_memory.cost + transfer_gas_cost, |
| 658 | ) |
| 659 | |
| 660 | # STATE ACCESS (STATE-DEPENDENT GAS) |
| 661 | # Perform the accesses and complete the state-dependent pricing -- |
| 662 | # a delegation adds its access cost; the execution gas is charged |
| 663 | # with the child grant. |
| 664 | tx_state = evm.tx_env.state |
| 665 | if is_cold_access: |
| 666 | evm.accessed_addresses.add(code_address) |
| 667 | |
| 668 | extra_gas = access_gas_cost + transfer_gas_cost |
| 669 | ( |
| 670 | is_delegated, |
| 671 | code_address, |
| 672 | delegation_access_cost, |
| 673 | ) = calculate_delegation_cost(evm, code_address) |
| 674 | |
| 675 | if is_delegated: |
| 676 | # check enough gas for delegation access |
| 677 | extra_gas += delegation_access_cost |
| 678 | check_gas(evm, extra_gas + extend_memory.cost) |
| 679 | if code_address not in evm.accessed_addresses: |
| 680 | evm.accessed_addresses.add(code_address) |
| 681 | |
| 682 | code_hash = get_account(tx_state, code_address).code_hash |
| 683 | code = get_code(tx_state, code_hash) |
| 684 | |
| 685 | # CHILD GRANT |
| 686 | # Charge the call's cost and withhold the child's execution gas |
| 687 | # share in one step. The state dimension is handed down in |
| 688 | # `generic_call`. |
| 689 | message_call_gas = calculate_message_call_gas( |
| 690 | value, |
| 691 | gas, |
| 692 | evm.gas_meter.gas_left, |
| 693 | extend_memory.cost, |
| 694 | extra_gas, |
| 695 | ) |
| 696 | charge_gas(evm, message_call_gas.cost + extend_memory.cost) |
| 697 | |
| 698 | # OPERATION |
| 699 | evm.memory += b"\x00" * extend_memory.expand_by |
| 700 | sender_balance = get_account(tx_state, evm.current_target).balance |
| 701 | |
| 702 | generic_call( |
| 703 | evm, |
| 704 | GenericCall( |
| 705 | gas=message_call_gas.sub_call, |
| 706 | value=value, |
| 707 | caller=evm.current_target, |
| 708 | to=to, |
| 709 | code_address=code_address, |
| 710 | should_transfer_value=True, |
| 711 | is_staticcall=False, |
| 712 | memory_input_start_position=memory_input_start_position, |
| 713 | memory_input_size=memory_input_size, |
| 714 | memory_output_start_position=memory_output_start_position, |
| 715 | memory_output_size=memory_output_size, |
| 716 | code=code, |
| 717 | disable_precompiles=is_delegated, |
| 718 | insufficient_balance=sender_balance < value, |
| 719 | ), |
| 720 | ) |
| 721 | |
| 722 | # PROGRAM COUNTER |
| 723 | evm.pc += Uint(1) |
selfdestruct ¶
Halt execution and register account for later deletion.
Parameters
evm : The current EVM frame.
def selfdestruct(evm: Evm) -> None:
| 727 | <snip> |
|---|---|
| 736 | if evm.is_static: |
| 737 | raise WriteInStaticContext |
| 738 | |
| 739 | # STACK |
| 740 | beneficiary = to_address_masked(pop(evm.stack)) |
| 741 | |
| 742 | # GAS (STATE-INDEPENDENT) |
| 743 | # Price what is computable without touching state, and check it is |
| 744 | # affordable before any state access is performed. |
| 745 | gas_cost = GasCosts.OPCODE_SELFDESTRUCT_BASE |
| 746 | |
| 747 | is_cold_access = beneficiary not in evm.accessed_addresses |
| 748 | if is_cold_access: |
| 749 | gas_cost += GasCosts.COLD_ACCOUNT_ACCESS |
| 750 | |
| 751 | check_gas(evm, gas_cost) |
| 752 | |
| 753 | # STATE ACCESS (STATE-DEPENDENT GAS) |
| 754 | # Perform the access; the pricing completes with the state gas |
| 755 | # below. |
| 756 | tx_state = evm.tx_env.state |
| 757 | if is_cold_access: |
| 758 | evm.accessed_addresses.add(beneficiary) |
| 759 | |
| 760 | # STATE GAS |
| 761 | # A sweep that will create the beneficiary pays the account write |
| 762 | # and the creation, charged by the frame whose opcode causes it; |
| 763 | # it refills only through the frame's own rollback. |
| 764 | state_gas = StateGas(Uint(0)) |
| 765 | account_write_gas = GasCosts.ZERO |
| 766 | if ( |
| 767 | not is_account_alive(tx_state, beneficiary) |
| 768 | and get_account(tx_state, evm.current_target).balance != 0 |
| 769 | ): |
| 770 | state_gas = StateGasCosts.NEW_ACCOUNT |
| 771 | account_write_gas = GasCosts.ACCOUNT_WRITE |
| 772 | |
| 773 | # Charge execution gas before state gas so that an execution-gas |
| 774 | # OOG does not consume state gas that would inflate the parent's |
| 775 | # reservoir on frame failure. |
| 776 | charge_gas(evm, gas_cost + account_write_gas) |
| 777 | charge_state_gas(evm, state_gas) |
| 778 | |
| 779 | # OPERATION |
| 780 | originator = evm.current_target |
| 781 | originator_balance = get_account(tx_state, originator).balance |
| 782 | |
| 783 | # Transfer balance |
| 784 | move_ether(tx_state, originator, beneficiary, originator_balance) |
| 785 | |
| 786 | # Emit transfer log |
| 787 | if beneficiary != originator: |
| 788 | emit_transfer_log(evm, originator, beneficiary, originator_balance) |
| 789 | |
| 790 | # Register account for deletion iff created in same transaction |
| 791 | if originator in tx_state.created_accounts: |
| 792 | evm.accounts_to_delete.add(originator) |
| 793 | |
| 794 | # HALT the execution |
| 795 | evm.running = False |
| 796 | |
| 797 | # PROGRAM COUNTER |
| 798 | pass |
delegatecall ¶
Message-call into an account.
Parameters
evm : The current EVM frame.
def delegatecall(evm: Evm) -> None:
| 802 | <snip> |
|---|---|
| 811 | # STACK |
| 812 | gas = ExecutionGas(Uint(pop(evm.stack))) |
| 813 | code_address = 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 (STATE-INDEPENDENT) |
| 820 | # Price what is computable without touching state, and check it is |
| 821 | # affordable before any state access is performed. |
| 822 | extend_memory = calculate_gas_extend_memory( |
| 823 | evm.memory, |
| 824 | [ |
| 825 | (memory_input_start_position, memory_input_size), |
| 826 | (memory_output_start_position, memory_output_size), |
| 827 | ], |
| 828 | ) |
| 829 | |
| 830 | is_cold_access = code_address not in evm.accessed_addresses |
| 831 | if is_cold_access: |
| 832 | access_gas_cost = GasCosts.COLD_ACCOUNT_ACCESS |
| 833 | else: |
| 834 | access_gas_cost = GasCosts.WARM_ACCESS |
| 835 | |
| 836 | check_gas(evm, access_gas_cost + extend_memory.cost) |
| 837 | |
| 838 | # STATE ACCESS (STATE-DEPENDENT GAS) |
| 839 | # Perform the accesses and complete the state-dependent pricing -- |
| 840 | # a delegation adds its access cost; the execution gas is charged |
| 841 | # with the child grant. |
| 842 | if is_cold_access: |
| 843 | evm.accessed_addresses.add(code_address) |
| 844 | |
| 845 | extra_gas = access_gas_cost |
| 846 | ( |
| 847 | is_delegated, |
| 848 | code_address, |
| 849 | delegation_access_cost, |
| 850 | ) = calculate_delegation_cost(evm, code_address) |
| 851 | |
| 852 | if is_delegated: |
| 853 | # check enough gas for delegation access |
| 854 | extra_gas += delegation_access_cost |
| 855 | check_gas(evm, extra_gas + extend_memory.cost) |
| 856 | if code_address not in evm.accessed_addresses: |
| 857 | evm.accessed_addresses.add(code_address) |
| 858 | |
| 859 | tx_state = evm.tx_env.state |
| 860 | code_hash = get_account(tx_state, code_address).code_hash |
| 861 | code = get_code(tx_state, code_hash) |
| 862 | |
| 863 | # CHILD GRANT |
| 864 | # Charge the call's cost and withhold the child's execution gas |
| 865 | # share in one step. The state dimension is handed down in |
| 866 | # `generic_call`. |
| 867 | message_call_gas = calculate_message_call_gas( |
| 868 | U256(0), |
| 869 | gas, |
| 870 | evm.gas_meter.gas_left, |
| 871 | extend_memory.cost, |
| 872 | extra_gas, |
| 873 | ) |
| 874 | charge_gas(evm, message_call_gas.cost + extend_memory.cost) |
| 875 | |
| 876 | # OPERATION |
| 877 | evm.memory += b"\x00" * extend_memory.expand_by |
| 878 | generic_call( |
| 879 | evm, |
| 880 | GenericCall( |
| 881 | gas=message_call_gas.sub_call, |
| 882 | value=evm.value, |
| 883 | caller=evm.caller, |
| 884 | to=evm.current_target, |
| 885 | code_address=code_address, |
| 886 | should_transfer_value=False, |
| 887 | is_staticcall=False, |
| 888 | memory_input_start_position=memory_input_start_position, |
| 889 | memory_input_size=memory_input_size, |
| 890 | memory_output_start_position=memory_output_start_position, |
| 891 | memory_output_size=memory_output_size, |
| 892 | code=code, |
| 893 | disable_precompiles=is_delegated, |
| 894 | ), |
| 895 | ) |
| 896 | |
| 897 | # PROGRAM COUNTER |
| 898 | evm.pc += Uint(1) |
staticcall ¶
Message-call into an account.
Parameters
evm : The current EVM frame.
def staticcall(evm: Evm) -> None:
| 902 | <snip> |
|---|---|
| 911 | # STACK |
| 912 | gas = ExecutionGas(Uint(pop(evm.stack))) |
| 913 | to = to_address_masked(pop(evm.stack)) |
| 914 | memory_input_start_position = pop(evm.stack) |
| 915 | memory_input_size = pop(evm.stack) |
| 916 | memory_output_start_position = pop(evm.stack) |
| 917 | memory_output_size = pop(evm.stack) |
| 918 | |
| 919 | # GAS (STATE-INDEPENDENT) |
| 920 | # Price what is computable without touching state, and check it is |
| 921 | # affordable before any state access is performed. |
| 922 | extend_memory = calculate_gas_extend_memory( |
| 923 | evm.memory, |
| 924 | [ |
| 925 | (memory_input_start_position, memory_input_size), |
| 926 | (memory_output_start_position, memory_output_size), |
| 927 | ], |
| 928 | ) |
| 929 | |
| 930 | is_cold_access = to not in evm.accessed_addresses |
| 931 | if is_cold_access: |
| 932 | access_gas_cost = GasCosts.COLD_ACCOUNT_ACCESS |
| 933 | else: |
| 934 | access_gas_cost = GasCosts.WARM_ACCESS |
| 935 | |
| 936 | check_gas(evm, access_gas_cost + extend_memory.cost) |
| 937 | |
| 938 | # STATE ACCESS (STATE-DEPENDENT GAS) |
| 939 | # Perform the accesses and complete the state-dependent pricing -- |
| 940 | # a delegation adds its access cost; the execution gas is charged |
| 941 | # with the child grant. |
| 942 | if is_cold_access: |
| 943 | evm.accessed_addresses.add(to) |
| 944 | |
| 945 | extra_gas = access_gas_cost |
| 946 | ( |
| 947 | is_delegated, |
| 948 | code_address, |
| 949 | delegation_access_cost, |
| 950 | ) = calculate_delegation_cost(evm, to) |
| 951 | |
| 952 | if is_delegated: |
| 953 | # check enough gas for delegation access |
| 954 | extra_gas += delegation_access_cost |
| 955 | check_gas(evm, extra_gas + extend_memory.cost) |
| 956 | if code_address not in evm.accessed_addresses: |
| 957 | evm.accessed_addresses.add(code_address) |
| 958 | |
| 959 | tx_state = evm.tx_env.state |
| 960 | code_hash = get_account(tx_state, code_address).code_hash |
| 961 | code = get_code(tx_state, code_hash) |
| 962 | |
| 963 | # CHILD GRANT |
| 964 | # Charge the call's cost and withhold the child's execution gas |
| 965 | # share in one step. The state dimension is handed down in |
| 966 | # `generic_call`. |
| 967 | message_call_gas = calculate_message_call_gas( |
| 968 | U256(0), |
| 969 | gas, |
| 970 | evm.gas_meter.gas_left, |
| 971 | extend_memory.cost, |
| 972 | extra_gas, |
| 973 | ) |
| 974 | charge_gas(evm, message_call_gas.cost + extend_memory.cost) |
| 975 | |
| 976 | # OPERATION |
| 977 | evm.memory += b"\x00" * extend_memory.expand_by |
| 978 | generic_call( |
| 979 | evm, |
| 980 | GenericCall( |
| 981 | gas=message_call_gas.sub_call, |
| 982 | value=U256(0), |
| 983 | caller=evm.current_target, |
| 984 | to=to, |
| 985 | code_address=code_address, |
| 986 | should_transfer_value=True, |
| 987 | is_staticcall=True, |
| 988 | memory_input_start_position=memory_input_start_position, |
| 989 | memory_input_size=memory_input_size, |
| 990 | memory_output_start_position=memory_output_start_position, |
| 991 | memory_output_size=memory_output_size, |
| 992 | code=code, |
| 993 | disable_precompiles=is_delegated, |
| 994 | ), |
| 995 | ) |
| 996 | |
| 997 | # PROGRAM COUNTER |
| 998 | 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:
| 1002 | <snip> |
|---|---|
| 1012 | # STACK |
| 1013 | memory_start_index = pop(evm.stack) |
| 1014 | size = pop(evm.stack) |
| 1015 | |
| 1016 | # GAS |
| 1017 | extend_memory = calculate_gas_extend_memory( |
| 1018 | evm.memory, [(memory_start_index, size)] |
| 1019 | ) |
| 1020 | |
| 1021 | charge_gas(evm, extend_memory.cost) |
| 1022 | |
| 1023 | # OPERATION |
| 1024 | evm.memory += b"\x00" * extend_memory.expand_by |
| 1025 | output = memory_read_bytes(evm.memory, memory_start_index, size) |
| 1026 | evm.output = Bytes(output) |
| 1027 | raise Revert |
| 1028 | |
| 1029 | # PROGRAM COUNTER |
| 1030 | # no-op |