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