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:
| 79 | """ |
|---|---|
| 80 | Core logic used by the `CREATE*` family of opcodes. |
| 81 | """ |
| 82 | # This import causes a circular import error |
| 83 | # if it's not moved inside this method |
| 84 | from ...vm.interpreter import ( |
| 85 | MAX_INIT_CODE_SIZE, |
| 86 | STACK_DEPTH_LIMIT, |
| 87 | process_create_message, |
| 88 | ) |
| 89 | |
| 90 | # Check static context first |
| 91 | if evm.message.is_static: |
| 92 | raise WriteInStaticContext |
| 93 | |
| 94 | # Check max init code size early before memory read |
| 95 | if memory_size > U256(MAX_INIT_CODE_SIZE): |
| 96 | raise OutOfGasError |
| 97 | |
| 98 | state = evm.message.block_env.state |
| 99 | |
| 100 | call_data = memory_read_bytes( |
| 101 | evm.memory, memory_start_position, memory_size |
| 102 | ) |
| 83 | if len(call_data) > MAX_INIT_CODE_SIZE: |
| 84 | raise OutOfGasError |
| 103 | |
| 104 | create_message_gas = max_message_call_gas(Uint(evm.gas_left)) |
| 105 | evm.gas_left -= create_message_gas |
| 88 | if evm.message.is_static: |
| 89 | raise WriteInStaticContext |
| 106 | evm.return_data = b"" |
| 107 | |
| 108 | sender_address = evm.message.current_target |
| 93 | sender = get_account(evm.message.block_env.state, sender_address) |
| 109 | sender = get_account(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 | push(evm.stack, U256(0)) |
| 118 | return |
| 119 | |
| 120 | evm.accessed_addresses.add(contract_address) |
| 121 | |
| 122 | track_address(evm.state_changes, contract_address) |
| 123 | if account_has_code_or_nonce( |
| 107 | evm.message.block_env.state, contract_address |
| 108 | ) or account_has_storage(evm.message.block_env.state, contract_address): |
| 109 | increment_nonce( |
| 110 | evm.message.block_env.state, evm.message.current_target |
| 124 | state, contract_address |
| 125 | ) or account_has_storage(state, contract_address): |
| 126 | increment_nonce(state, evm.message.current_target) |
| 127 | nonce_after = get_account(state, evm.message.current_target).nonce |
| 128 | track_nonce_change( |
| 129 | evm.state_changes, |
| 130 | evm.message.current_target, |
| 131 | U64(nonce_after), |
| 132 | ) |
| 133 | push(evm.stack, U256(0)) |
| 134 | return |
| 135 | |
| 115 | increment_nonce(evm.message.block_env.state, evm.message.current_target) |
| 136 | # Track nonce increment for CREATE |
| 137 | increment_nonce(state, evm.message.current_target) |
| 138 | nonce_after = get_account(state, evm.message.current_target).nonce |
| 139 | track_nonce_change( |
| 140 | evm.state_changes, |
| 141 | evm.message.current_target, |
| 142 | U64(nonce_after), |
| 143 | ) |
| 144 | |
| 145 | # Create call frame as child of parent EVM's frame |
| 146 | child_state_changes = create_child_frame(evm.state_changes) |
| 147 | |
| 148 | child_message = Message( |
| 149 | block_env=evm.message.block_env, |
| 150 | tx_env=evm.message.tx_env, |
| 151 | caller=evm.message.current_target, |
| 152 | target=Bytes0(), |
| 153 | gas=create_message_gas, |
| 154 | value=endowment, |
| 155 | data=b"", |
| 156 | code=call_data, |
| 157 | current_target=contract_address, |
| 158 | depth=evm.message.depth + Uint(1), |
| 159 | code_address=None, |
| 160 | should_transfer_value=True, |
| 161 | is_static=False, |
| 162 | accessed_addresses=evm.accessed_addresses.copy(), |
| 163 | accessed_storage_keys=evm.accessed_storage_keys.copy(), |
| 164 | disable_precompiles=False, |
| 165 | parent_evm=evm, |
| 166 | is_create=True, |
| 167 | state_changes=child_state_changes, |
| 168 | ) |
| 169 | child_evm = process_create_message(child_message) |
| 170 | |
| 171 | if child_evm.error: |
| 172 | incorporate_child_on_error(evm, child_evm) |
| 173 | evm.return_data = child_evm.output |
| 174 | push(evm.stack, U256(0)) |
| 175 | else: |
| 176 | incorporate_child_on_success(evm, child_evm) |
| 177 | evm.return_data = b"" |
| 178 | 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:
| 182 | """ |
|---|---|
| 183 | Creates a new account with associated code. |
| 184 | |
| 185 | Parameters |
| 186 | ---------- |
| 187 | evm : |
| 188 | The current EVM frame. |
| 189 | |
| 190 | """ |
| 191 | # STACK |
| 192 | endowment = pop(evm.stack) |
| 193 | memory_start_position = pop(evm.stack) |
| 194 | memory_size = pop(evm.stack) |
| 195 | |
| 196 | # GAS |
| 197 | extend_memory = calculate_gas_extend_memory( |
| 198 | evm.memory, [(memory_start_position, memory_size)] |
| 199 | ) |
| 200 | init_code_gas = init_code_cost(Uint(memory_size)) |
| 201 | |
| 202 | charge_gas(evm, GAS_CREATE + extend_memory.cost + init_code_gas) |
| 203 | |
| 204 | # OPERATION |
| 205 | evm.memory += b"\x00" * extend_memory.expand_by |
| 206 | contract_address = compute_contract_address( |
| 207 | evm.message.current_target, |
| 208 | get_account( |
| 209 | evm.message.block_env.state, evm.message.current_target |
| 210 | ).nonce, |
| 211 | ) |
| 212 | |
| 213 | generic_create( |
| 214 | evm, |
| 215 | endowment, |
| 216 | contract_address, |
| 217 | memory_start_position, |
| 218 | memory_size, |
| 219 | ) |
| 220 | |
| 221 | # PROGRAM COUNTER |
| 222 | 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:
| 226 | """ |
|---|---|
| 227 | Creates a new account with associated code. |
| 228 | |
| 229 | It's similar to the CREATE opcode except that the address of the new |
| 230 | account depends on the init_code instead of the nonce of sender. |
| 231 | |
| 232 | Parameters |
| 233 | ---------- |
| 234 | evm : |
| 235 | The current EVM frame. |
| 236 | |
| 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, |
| 252 | GAS_CREATE |
| 253 | + GAS_KECCAK256_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 | """ |
|---|---|
| 280 | Halts execution returning output data. |
| 281 | |
| 282 | Parameters |
| 283 | ---------- |
| 284 | evm : |
| 285 | The current EVM frame. |
| 286 | |
| 287 | """ |
| 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, GAS_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 |
generic_call
Perform the core logic of the CALL* family of opcodes.
def generic_call(evm: Evm, gas: Uint, value: U256, caller: Address, to: Address, code_address: Address, should_transfer_value: bool, is_staticcall: bool, memory_input_start_position: U256, memory_input_size: U256, memory_output_start_position: U256, memory_output_size: U256, code: Bytes, disable_precompiles: bool) -> None:
| 327 | """ |
|---|---|
| 328 | Perform the core logic of the `CALL*` family of opcodes. |
| 329 | """ |
| 330 | from ...vm.interpreter import STACK_DEPTH_LIMIT, process_message |
| 331 | |
| 332 | evm.return_data = b"" |
| 333 | |
| 334 | if evm.message.depth + Uint(1) > STACK_DEPTH_LIMIT: |
| 335 | evm.gas_left += gas |
| 336 | push(evm.stack, U256(0)) |
| 337 | return |
| 338 | |
| 339 | call_data = memory_read_bytes( |
| 340 | evm.memory, memory_input_start_position, memory_input_size |
| 341 | ) |
| 342 | |
| 343 | # Create call frame as child of parent EVM's frame |
| 344 | child_state_changes = create_child_frame(evm.state_changes) |
| 345 | |
| 346 | child_message = Message( |
| 347 | block_env=evm.message.block_env, |
| 348 | tx_env=evm.message.tx_env, |
| 349 | caller=caller, |
| 350 | target=to, |
| 351 | gas=gas, |
| 352 | value=value, |
| 353 | data=call_data, |
| 354 | code=code, |
| 355 | current_target=to, |
| 356 | depth=evm.message.depth + Uint(1), |
| 357 | code_address=code_address, |
| 358 | should_transfer_value=should_transfer_value, |
| 359 | is_static=True if is_staticcall else evm.message.is_static, |
| 360 | accessed_addresses=evm.accessed_addresses.copy(), |
| 361 | accessed_storage_keys=evm.accessed_storage_keys.copy(), |
| 362 | disable_precompiles=disable_precompiles, |
| 363 | parent_evm=evm, |
| 364 | is_create=False, |
| 365 | state_changes=child_state_changes, |
| 366 | ) |
| 367 | |
| 368 | child_evm = process_message(child_message) |
| 369 | |
| 370 | if child_evm.error: |
| 371 | incorporate_child_on_error(evm, child_evm) |
| 372 | evm.return_data = child_evm.output |
| 373 | push(evm.stack, U256(0)) |
| 374 | else: |
| 375 | incorporate_child_on_success(evm, child_evm) |
| 376 | evm.return_data = child_evm.output |
| 377 | push(evm.stack, U256(1)) |
| 378 | |
| 379 | actual_output_size = min(memory_output_size, U256(len(child_evm.output))) |
| 380 | memory_write( |
| 381 | evm.memory, |
| 382 | memory_output_start_position, |
| 383 | child_evm.output[:actual_output_size], |
| 384 | ) |
call
Message-call into an account.
Parameters
evm : The current EVM frame.
def call(evm: Evm) -> None:
| 388 | """ |
|---|---|
| 389 | Message-call into an account. |
| 390 | |
| 391 | Parameters |
| 392 | ---------- |
| 393 | evm : |
| 394 | The current EVM frame. |
| 395 | |
| 396 | """ |
| 397 | # STACK |
| 398 | gas = Uint(pop(evm.stack)) |
| 399 | to = to_address_masked(pop(evm.stack)) |
| 400 | value = pop(evm.stack) |
| 401 | memory_input_start_position = pop(evm.stack) |
| 402 | memory_input_size = pop(evm.stack) |
| 403 | memory_output_start_position = pop(evm.stack) |
| 404 | memory_output_size = pop(evm.stack) |
| 405 | |
| 406 | if evm.message.is_static and value != U256(0): |
| 407 | raise WriteInStaticContext |
| 408 | |
| 409 | # GAS |
| 410 | extend_memory = calculate_gas_extend_memory( |
| 411 | evm.memory, |
| 412 | [ |
| 413 | (memory_input_start_position, memory_input_size), |
| 414 | (memory_output_start_position, memory_output_size), |
| 415 | ], |
| 416 | ) |
| 417 | |
| 376 | if to in evm.accessed_addresses: |
| 377 | access_gas_cost = GAS_WARM_ACCESS |
| 418 | is_cold_access = to not in evm.accessed_addresses |
| 419 | if is_cold_access: |
| 420 | access_gas_cost = GAS_COLD_ACCOUNT_ACCESS |
| 421 | else: |
| 379 | evm.accessed_addresses.add(to) |
| 380 | access_gas_cost = GAS_COLD_ACCOUNT_ACCESS |
| 422 | access_gas_cost = GAS_WARM_ACCESS |
| 423 | |
| 382 | code_address = to |
| 383 | ( |
| 384 | disable_precompiles, |
| 385 | code_address, |
| 386 | code, |
| 387 | delegated_access_gas_cost, |
| 388 | ) = access_delegation(evm, code_address) |
| 389 | access_gas_cost += delegated_access_gas_cost |
| 424 | transfer_gas_cost = Uint(0) if value == 0 else GAS_CALL_VALUE |
| 425 | |
| 426 | # check static gas before state access |
| 427 | check_gas( |
| 428 | evm, |
| 429 | access_gas_cost + transfer_gas_cost + extend_memory.cost, |
| 430 | ) |
| 431 | |
| 432 | # STATE ACCESS |
| 433 | state = evm.message.block_env.state |
| 434 | if is_cold_access: |
| 435 | evm.accessed_addresses.add(to) |
| 436 | |
| 437 | create_gas_cost = GAS_NEW_ACCOUNT |
| 392 | if value == 0 or is_account_alive(evm.message.block_env.state, to): |
| 438 | if value == 0 or is_account_alive(state, to): |
| 439 | create_gas_cost = Uint(0) |
| 394 | transfer_gas_cost = Uint(0) if value == 0 else GAS_CALL_VALUE |
| 440 | |
| 441 | extra_gas = access_gas_cost + transfer_gas_cost + create_gas_cost |
| 442 | ( |
| 443 | is_delegated, |
| 444 | code_address, |
| 445 | delegation_access_cost, |
| 446 | ) = calculate_delegation_cost(evm, to) |
| 447 | |
| 448 | if is_delegated: |
| 449 | # check enough gas for delegation access |
| 450 | extra_gas += delegation_access_cost |
| 451 | check_gas(evm, extra_gas + extend_memory.cost) |
| 452 | track_address(evm.state_changes, code_address) |
| 453 | if code_address not in evm.accessed_addresses: |
| 454 | evm.accessed_addresses.add(code_address) |
| 455 | |
| 456 | code = get_account(state, code_address).code |
| 457 | |
| 458 | message_call_gas = calculate_message_call_gas( |
| 459 | value, |
| 460 | gas, |
| 461 | Uint(evm.gas_left), |
| 462 | extend_memory.cost, |
| 400 | access_gas_cost + create_gas_cost + transfer_gas_cost, |
| 463 | extra_gas, |
| 464 | ) |
| 465 | charge_gas(evm, message_call_gas.cost + extend_memory.cost) |
| 403 | if evm.message.is_static and value != U256(0): |
| 404 | raise WriteInStaticContext |
| 466 | |
| 467 | evm.memory += b"\x00" * extend_memory.expand_by |
| 406 | sender_balance = get_account( |
| 407 | evm.message.block_env.state, evm.message.current_target |
| 408 | ).balance |
| 468 | sender_balance = get_account(state, evm.message.current_target).balance |
| 469 | if sender_balance < value: |
| 470 | push(evm.stack, U256(0)) |
| 471 | evm.return_data = b"" |
| 472 | evm.gas_left += message_call_gas.sub_call |
| 473 | else: |
| 474 | generic_call( |
| 475 | evm, |
| 476 | message_call_gas.sub_call, |
| 477 | value, |
| 478 | evm.message.current_target, |
| 479 | to, |
| 480 | code_address, |
| 481 | True, |
| 482 | False, |
| 483 | memory_input_start_position, |
| 484 | memory_input_size, |
| 485 | memory_output_start_position, |
| 486 | memory_output_size, |
| 487 | code, |
| 428 | disable_precompiles, |
| 488 | is_delegated, |
| 489 | ) |
| 490 | |
| 491 | # PROGRAM COUNTER |
| 492 | 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:
| 496 | """ |
|---|---|
| 437 | Message-call into this account with alternative account’s code. |
| 497 | Message-call into this account with alternative account's code. |
| 498 | |
| 499 | Parameters |
| 500 | ---------- |
| 501 | evm : |
| 502 | The current EVM frame. |
| 503 | |
| 504 | """ |
| 505 | # STACK |
| 506 | gas = Uint(pop(evm.stack)) |
| 507 | code_address = to_address_masked(pop(evm.stack)) |
| 508 | value = pop(evm.stack) |
| 509 | memory_input_start_position = pop(evm.stack) |
| 510 | memory_input_size = pop(evm.stack) |
| 511 | memory_output_start_position = pop(evm.stack) |
| 512 | memory_output_size = pop(evm.stack) |
| 513 | |
| 514 | # GAS |
| 515 | to = evm.message.current_target |
| 516 | |
| 517 | extend_memory = calculate_gas_extend_memory( |
| 518 | evm.memory, |
| 519 | [ |
| 520 | (memory_input_start_position, memory_input_size), |
| 521 | (memory_output_start_position, memory_output_size), |
| 522 | ], |
| 523 | ) |
| 524 | |
| 465 | if code_address in evm.accessed_addresses: |
| 466 | access_gas_cost = GAS_WARM_ACCESS |
| 525 | is_cold_access = code_address not in evm.accessed_addresses |
| 526 | if is_cold_access: |
| 527 | access_gas_cost = GAS_COLD_ACCOUNT_ACCESS |
| 528 | else: |
| 468 | evm.accessed_addresses.add(code_address) |
| 469 | access_gas_cost = GAS_COLD_ACCOUNT_ACCESS |
| 470 | |
| 471 | ( |
| 472 | disable_precompiles, |
| 473 | code_address, |
| 474 | code, |
| 475 | delegated_access_gas_cost, |
| 476 | ) = access_delegation(evm, code_address) |
| 477 | access_gas_cost += delegated_access_gas_cost |
| 529 | access_gas_cost = GAS_WARM_ACCESS |
| 530 | |
| 531 | transfer_gas_cost = Uint(0) if value == 0 else GAS_CALL_VALUE |
| 532 | |
| 533 | # check static gas before state access |
| 534 | check_gas( |
| 535 | evm, |
| 536 | access_gas_cost + extend_memory.cost + transfer_gas_cost, |
| 537 | ) |
| 538 | |
| 539 | # STATE ACCESS |
| 540 | state = evm.message.block_env.state |
| 541 | if is_cold_access: |
| 542 | evm.accessed_addresses.add(code_address) |
| 543 | |
| 544 | extra_gas = access_gas_cost + transfer_gas_cost |
| 545 | ( |
| 546 | is_delegated, |
| 547 | code_address, |
| 548 | delegation_access_cost, |
| 549 | ) = calculate_delegation_cost(evm, code_address) |
| 550 | |
| 551 | if is_delegated: |
| 552 | # check enough gas for delegation access |
| 553 | extra_gas += delegation_access_cost |
| 554 | check_gas(evm, extra_gas + extend_memory.cost) |
| 555 | track_address(evm.state_changes, code_address) |
| 556 | if code_address not in evm.accessed_addresses: |
| 557 | evm.accessed_addresses.add(code_address) |
| 558 | |
| 559 | code = get_account(state, code_address).code |
| 560 | |
| 561 | message_call_gas = calculate_message_call_gas( |
| 562 | value, |
| 563 | gas, |
| 564 | Uint(evm.gas_left), |
| 565 | extend_memory.cost, |
| 485 | access_gas_cost + transfer_gas_cost, |
| 566 | extra_gas, |
| 567 | ) |
| 568 | charge_gas(evm, message_call_gas.cost + extend_memory.cost) |
| 569 | |
| 570 | # OPERATION |
| 571 | evm.memory += b"\x00" * extend_memory.expand_by |
| 572 | sender_balance = get_account( |
| 573 | evm.message.block_env.state, evm.message.current_target |
| 574 | ).balance |
| 575 | |
| 576 | # EIP-7928: For CALLCODE with value transfer, capture pre-balance |
| 577 | # in transaction frame. CALLCODE transfers value from/to current_target |
| 578 | # (same address), affecting current storage context, not child frame |
| 579 | if value != 0 and sender_balance >= value: |
| 580 | capture_pre_balance( |
| 581 | evm.message.tx_env.state_changes, |
| 582 | evm.message.current_target, |
| 583 | sender_balance, |
| 584 | ) |
| 585 | |
| 586 | if sender_balance < value: |
| 587 | push(evm.stack, U256(0)) |
| 588 | evm.return_data = b"" |
| 589 | evm.gas_left += message_call_gas.sub_call |
| 590 | else: |
| 591 | generic_call( |
| 592 | evm, |
| 593 | message_call_gas.sub_call, |
| 594 | value, |
| 595 | evm.message.current_target, |
| 596 | to, |
| 597 | code_address, |
| 598 | True, |
| 599 | False, |
| 600 | memory_input_start_position, |
| 601 | memory_input_size, |
| 602 | memory_output_start_position, |
| 603 | memory_output_size, |
| 604 | code, |
| 513 | disable_precompiles, |
| 605 | is_delegated, |
| 606 | ) |
| 607 | |
| 608 | # PROGRAM COUNTER |
| 609 | evm.pc += Uint(1) |
selfdestruct
Halt execution and register account for later deletion.
Parameters
evm : The current EVM frame.
def selfdestruct(evm: Evm) -> None:
| 613 | """ |
|---|---|
| 614 | Halt execution and register account for later deletion. |
| 615 | |
| 616 | Parameters |
| 617 | ---------- |
| 618 | evm : |
| 619 | The current EVM frame. |
| 620 | |
| 621 | """ |
| 622 | if evm.message.is_static: |
| 623 | raise WriteInStaticContext |
| 624 | |
| 625 | # STACK |
| 626 | beneficiary = to_address_masked(pop(evm.stack)) |
| 627 | |
| 628 | # GAS |
| 629 | gas_cost = GAS_SELF_DESTRUCT |
| 535 | if beneficiary not in evm.accessed_addresses: |
| 536 | evm.accessed_addresses.add(beneficiary) |
| 537 | gas_cost += GAS_COLD_ACCOUNT_ACCESS |
| 630 | |
| 631 | is_cold_access = beneficiary not in evm.accessed_addresses |
| 632 | if is_cold_access: |
| 633 | gas_cost += GAS_COLD_ACCOUNT_ACCESS |
| 634 | |
| 635 | # check access gas cost before state access |
| 636 | check_gas(evm, gas_cost) |
| 637 | |
| 638 | # STATE ACCESS |
| 639 | state = evm.message.block_env.state |
| 640 | if is_cold_access: |
| 641 | evm.accessed_addresses.add(beneficiary) |
| 642 | |
| 643 | track_address(evm.state_changes, beneficiary) |
| 644 | |
| 645 | if ( |
| 540 | not is_account_alive(evm.message.block_env.state, beneficiary) |
| 541 | and get_account( |
| 542 | evm.message.block_env.state, evm.message.current_target |
| 543 | ).balance |
| 544 | != 0 |
| 646 | not is_account_alive(state, beneficiary) |
| 647 | and get_account(state, evm.message.current_target).balance != 0 |
| 648 | ): |
| 649 | gas_cost += GAS_SELF_DESTRUCT_NEW_ACCOUNT |
| 650 | |
| 651 | charge_gas(evm, gas_cost) |
| 549 | if evm.message.is_static: |
| 550 | raise WriteInStaticContext |
| 652 | |
| 653 | state = evm.message.block_env.state |
| 654 | originator = evm.message.current_target |
| 553 | originator_balance = get_account( |
| 554 | evm.message.block_env.state, originator |
| 555 | ).balance |
| 655 | originator_balance = get_account(state, originator).balance |
| 656 | beneficiary_balance = get_account(state, beneficiary).balance |
| 657 | |
| 557 | move_ether( |
| 558 | evm.message.block_env.state, |
| 658 | # Get tracking context |
| 659 | tx_frame = evm.message.tx_env.state_changes |
| 660 | |
| 661 | # Capture pre-balances for net-zero filtering |
| 662 | track_address(evm.state_changes, originator) |
| 663 | capture_pre_balance(tx_frame, originator, originator_balance) |
| 664 | capture_pre_balance(tx_frame, beneficiary, beneficiary_balance) |
| 665 | |
| 666 | # Transfer balance |
| 667 | move_ether(state, originator, beneficiary, originator_balance) |
| 668 | |
| 669 | # Track balance changes |
| 670 | originator_new_balance = get_account(state, originator).balance |
| 671 | beneficiary_new_balance = get_account(state, beneficiary).balance |
| 672 | track_balance_change( |
| 673 | evm.state_changes, |
| 674 | originator, |
| 675 | originator_new_balance, |
| 676 | ) |
| 677 | track_balance_change( |
| 678 | evm.state_changes, |
| 679 | beneficiary, |
| 561 | originator_balance, |
| 680 | beneficiary_new_balance, |
| 681 | ) |
| 682 | |
| 683 | # register account for deletion only if it was created |
| 684 | # in the same transaction |
| 566 | if originator in evm.message.block_env.state.created_accounts: |
| 685 | if originator in state.created_accounts: |
| 686 | # If beneficiary is the same as originator, then |
| 687 | # the ether is burnt. |
| 569 | set_account_balance(evm.message.block_env.state, originator, U256(0)) |
| 688 | set_account_balance(state, originator, U256(0)) |
| 689 | track_balance_change(evm.state_changes, originator, U256(0)) |
| 690 | evm.accounts_to_delete.add(originator) |
| 691 | |
| 692 | # HALT the execution |
| 693 | evm.running = False |
| 694 | |
| 695 | # PROGRAM COUNTER |
| 696 | pass |
delegatecall
Message-call into an account.
Parameters
evm : The current EVM frame.
def delegatecall(evm: Evm) -> None:
| 700 | """ |
|---|---|
| 701 | Message-call into an account. |
| 702 | |
| 703 | Parameters |
| 704 | ---------- |
| 705 | evm : |
| 706 | The current EVM frame. |
| 707 | |
| 708 | """ |
| 709 | # STACK |
| 710 | gas = Uint(pop(evm.stack)) |
| 711 | code_address = to_address_masked(pop(evm.stack)) |
| 712 | memory_input_start_position = pop(evm.stack) |
| 713 | memory_input_size = pop(evm.stack) |
| 714 | memory_output_start_position = pop(evm.stack) |
| 715 | memory_output_size = pop(evm.stack) |
| 716 | |
| 717 | # GAS |
| 718 | extend_memory = calculate_gas_extend_memory( |
| 719 | evm.memory, |
| 720 | [ |
| 721 | (memory_input_start_position, memory_input_size), |
| 722 | (memory_output_start_position, memory_output_size), |
| 723 | ], |
| 724 | ) |
| 725 | |
| 606 | if code_address in evm.accessed_addresses: |
| 607 | access_gas_cost = GAS_WARM_ACCESS |
| 726 | is_cold_access = code_address not in evm.accessed_addresses |
| 727 | if is_cold_access: |
| 728 | access_gas_cost = GAS_COLD_ACCOUNT_ACCESS |
| 729 | else: |
| 609 | evm.accessed_addresses.add(code_address) |
| 610 | access_gas_cost = GAS_COLD_ACCOUNT_ACCESS |
| 730 | access_gas_cost = GAS_WARM_ACCESS |
| 731 | |
| 732 | # check static gas before state access |
| 733 | check_gas(evm, access_gas_cost + extend_memory.cost) |
| 734 | |
| 735 | # STATE ACCESS |
| 736 | state = evm.message.block_env.state |
| 737 | if is_cold_access: |
| 738 | evm.accessed_addresses.add(code_address) |
| 739 | |
| 740 | extra_gas = access_gas_cost |
| 741 | ( |
| 613 | disable_precompiles, |
| 742 | is_delegated, |
| 743 | code_address, |
| 615 | code, |
| 616 | delegated_access_gas_cost, |
| 617 | ) = access_delegation(evm, code_address) |
| 618 | access_gas_cost += delegated_access_gas_cost |
| 744 | delegation_access_cost, |
| 745 | ) = calculate_delegation_cost(evm, code_address) |
| 746 | |
| 747 | if is_delegated: |
| 748 | # check enough gas for delegation access |
| 749 | extra_gas += delegation_access_cost |
| 750 | check_gas(evm, extra_gas + extend_memory.cost) |
| 751 | track_address(evm.state_changes, code_address) |
| 752 | if code_address not in evm.accessed_addresses: |
| 753 | evm.accessed_addresses.add(code_address) |
| 754 | |
| 755 | code = get_account(state, code_address).code |
| 756 | |
| 757 | message_call_gas = calculate_message_call_gas( |
| 621 | U256(0), gas, Uint(evm.gas_left), extend_memory.cost, access_gas_cost |
| 758 | U256(0), |
| 759 | gas, |
| 760 | Uint(evm.gas_left), |
| 761 | extend_memory.cost, |
| 762 | extra_gas, |
| 763 | ) |
| 764 | charge_gas(evm, message_call_gas.cost + extend_memory.cost) |
| 765 | |
| 766 | # OPERATION |
| 767 | evm.memory += b"\x00" * extend_memory.expand_by |
| 768 | generic_call( |
| 769 | evm, |
| 770 | message_call_gas.sub_call, |
| 771 | evm.message.value, |
| 772 | evm.message.caller, |
| 773 | evm.message.current_target, |
| 774 | code_address, |
| 775 | False, |
| 776 | False, |
| 777 | memory_input_start_position, |
| 778 | memory_input_size, |
| 779 | memory_output_start_position, |
| 780 | memory_output_size, |
| 781 | code, |
| 641 | disable_precompiles, |
| 782 | is_delegated, |
| 783 | ) |
| 784 | |
| 785 | # PROGRAM COUNTER |
| 786 | evm.pc += Uint(1) |
staticcall
Message-call into an account.
Parameters
evm : The current EVM frame.
def staticcall(evm: Evm) -> None:
| 790 | """ |
|---|---|
| 791 | Message-call into an account. |
| 792 | |
| 793 | Parameters |
| 794 | ---------- |
| 795 | evm : |
| 796 | The current EVM frame. |
| 797 | |
| 798 | """ |
| 799 | # STACK |
| 800 | gas = Uint(pop(evm.stack)) |
| 801 | to = to_address_masked(pop(evm.stack)) |
| 802 | memory_input_start_position = pop(evm.stack) |
| 803 | memory_input_size = pop(evm.stack) |
| 804 | memory_output_start_position = pop(evm.stack) |
| 805 | memory_output_size = pop(evm.stack) |
| 806 | |
| 807 | # GAS |
| 808 | extend_memory = calculate_gas_extend_memory( |
| 809 | evm.memory, |
| 810 | [ |
| 811 | (memory_input_start_position, memory_input_size), |
| 812 | (memory_output_start_position, memory_output_size), |
| 813 | ], |
| 814 | ) |
| 815 | |
| 675 | if to in evm.accessed_addresses: |
| 676 | access_gas_cost = GAS_WARM_ACCESS |
| 816 | is_cold_access = to not in evm.accessed_addresses |
| 817 | if is_cold_access: |
| 818 | access_gas_cost = GAS_COLD_ACCOUNT_ACCESS |
| 819 | else: |
| 678 | evm.accessed_addresses.add(to) |
| 679 | access_gas_cost = GAS_COLD_ACCOUNT_ACCESS |
| 820 | access_gas_cost = GAS_WARM_ACCESS |
| 821 | |
| 681 | code_address = to |
| 822 | # check static gas before state access |
| 823 | check_gas(evm, access_gas_cost + extend_memory.cost) |
| 824 | |
| 825 | # STATE ACCESS |
| 826 | state = evm.message.block_env.state |
| 827 | if is_cold_access: |
| 828 | evm.accessed_addresses.add(to) |
| 829 | |
| 830 | extra_gas = access_gas_cost |
| 831 | ( |
| 683 | disable_precompiles, |
| 832 | is_delegated, |
| 833 | code_address, |
| 685 | code, |
| 686 | delegated_access_gas_cost, |
| 687 | ) = access_delegation(evm, code_address) |
| 688 | access_gas_cost += delegated_access_gas_cost |
| 834 | delegation_access_cost, |
| 835 | ) = calculate_delegation_cost(evm, to) |
| 836 | |
| 837 | if is_delegated: |
| 838 | # check enough gas for delegation access |
| 839 | extra_gas += delegation_access_cost |
| 840 | check_gas(evm, extra_gas + extend_memory.cost) |
| 841 | track_address(evm.state_changes, code_address) |
| 842 | if code_address not in evm.accessed_addresses: |
| 843 | evm.accessed_addresses.add(code_address) |
| 844 | |
| 845 | code = get_account(state, code_address).code |
| 846 | |
| 847 | message_call_gas = calculate_message_call_gas( |
| 848 | U256(0), |
| 849 | gas, |
| 850 | Uint(evm.gas_left), |
| 851 | extend_memory.cost, |
| 695 | access_gas_cost, |
| 852 | extra_gas, |
| 853 | ) |
| 854 | charge_gas(evm, message_call_gas.cost + extend_memory.cost) |
| 855 | |
| 856 | # OPERATION |
| 857 | evm.memory += b"\x00" * extend_memory.expand_by |
| 858 | generic_call( |
| 859 | evm, |
| 860 | message_call_gas.sub_call, |
| 861 | U256(0), |
| 862 | evm.message.current_target, |
| 863 | to, |
| 864 | code_address, |
| 865 | True, |
| 866 | True, |
| 867 | memory_input_start_position, |
| 868 | memory_input_size, |
| 869 | memory_output_start_position, |
| 870 | memory_output_size, |
| 871 | code, |
| 715 | disable_precompiles, |
| 872 | is_delegated, |
| 873 | ) |
| 874 | |
| 875 | # PROGRAM COUNTER |
| 876 | 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:
| 880 | """ |
|---|---|
| 881 | Stop execution and revert state changes, without consuming all provided gas |
| 882 | and also has the ability to return a reason. |
| 883 | |
| 884 | Parameters |
| 885 | ---------- |
| 886 | evm : |
| 887 | The current EVM frame. |
| 888 | |
| 889 | """ |
| 890 | # STACK |
| 891 | memory_start_index = pop(evm.stack) |
| 892 | size = pop(evm.stack) |
| 893 | |
| 894 | # GAS |
| 895 | extend_memory = calculate_gas_extend_memory( |
| 896 | evm.memory, [(memory_start_index, size)] |
| 897 | ) |
| 898 | |
| 899 | charge_gas(evm, extend_memory.cost) |
| 900 | |
| 901 | # OPERATION |
| 902 | evm.memory += b"\x00" * extend_memory.expand_by |
| 903 | output = memory_read_bytes(evm.memory, memory_start_index, size) |
| 904 | evm.output = Bytes(output) |
| 905 | raise Revert |