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