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