ethereum.forks.spurious_dragon.vm.instructions.system
Ethereum Virtual Machine (EVM) System Instructions.
.. contents:: Table of Contents :backlinks: none :local:
Introduction
Implementations of the EVM system related instructions.
create ¶
Creates a new account with associated code.
Parameters
evm : The current EVM frame.
def create(evm: Evm) -> None:
| 50 | <snip> |
|---|---|
| 59 | # This import causes a circular import error |
| 60 | # if it's not moved inside this method |
| 61 | from ...vm.interpreter import STACK_DEPTH_LIMIT, process_create_message |
| 62 | |
| 63 | # STACK |
| 64 | endowment = pop(evm.stack) |
| 65 | memory_start_position = pop(evm.stack) |
| 66 | memory_size = pop(evm.stack) |
| 67 | |
| 68 | # GAS |
| 69 | extend_memory = calculate_gas_extend_memory( |
| 70 | evm.memory, [(memory_start_position, memory_size)] |
| 71 | ) |
| 72 | |
| 73 | charge_gas(evm, GasCosts.OPCODE_CREATE_BASE + extend_memory.cost) |
| 74 | |
| 75 | create_message_gas = max_message_call_gas(Uint(evm.gas_left)) |
| 76 | evm.gas_left -= create_message_gas |
| 77 | |
| 78 | # OPERATION |
| 79 | evm.memory += b"\x00" * extend_memory.expand_by |
| 80 | sender_address = evm.message.current_target |
| 81 | sender = get_account(evm.message.tx_env.state, sender_address) |
| 82 | |
| 83 | contract_address = compute_contract_address( |
| 84 | evm.message.current_target, |
| 85 | get_account( |
| 86 | evm.message.tx_env.state, evm.message.current_target |
| 87 | ).nonce, |
| 88 | ) |
| 89 | |
| 90 | if ( |
| 91 | sender.balance < endowment |
| 92 | or sender.nonce == Uint(2**64 - 1) |
| 93 | or evm.message.depth + Uint(1) > STACK_DEPTH_LIMIT |
| 94 | ): |
| 95 | push(evm.stack, U256(0)) |
| 96 | evm.gas_left += create_message_gas |
| 97 | elif not account_deployable(evm.message.tx_env.state, contract_address): |
| 98 | increment_nonce(evm.message.tx_env.state, evm.message.current_target) |
| 99 | push(evm.stack, U256(0)) |
| 100 | else: |
| 101 | call_data = memory_read_bytes( |
| 102 | evm.memory, memory_start_position, memory_size |
| 103 | ) |
| 104 | |
| 105 | increment_nonce(evm.message.tx_env.state, evm.message.current_target) |
| 106 | |
| 107 | child_message = Message( |
| 108 | block_env=evm.message.block_env, |
| 109 | tx_env=evm.message.tx_env, |
| 110 | caller=evm.message.current_target, |
| 111 | target=Bytes0(), |
| 112 | gas=create_message_gas, |
| 113 | value=endowment, |
| 114 | data=b"", |
| 115 | code=call_data, |
| 116 | current_target=contract_address, |
| 117 | depth=evm.message.depth + Uint(1), |
| 118 | code_address=None, |
| 119 | should_transfer_value=True, |
| 120 | parent_evm=evm, |
| 121 | ) |
| 122 | child_evm = process_create_message(child_message) |
| 123 | |
| 124 | if child_evm.error: |
| 125 | incorporate_child_on_error(evm, child_evm) |
| 126 | push(evm.stack, U256(0)) |
| 127 | else: |
| 128 | incorporate_child_on_success(evm, child_evm) |
| 129 | push( |
| 130 | evm.stack, U256.from_be_bytes(child_evm.message.current_target) |
| 131 | ) |
| 132 | |
| 133 | # PROGRAM COUNTER |
| 134 | evm.pc += Uint(1) |
return_ ¶
Halts execution returning output data.
Parameters
evm : The current EVM frame.
def return_(evm: Evm) -> None:
| 138 | <snip> |
|---|---|
| 147 | # STACK |
| 148 | memory_start_position = pop(evm.stack) |
| 149 | memory_size = pop(evm.stack) |
| 150 | |
| 151 | # GAS |
| 152 | extend_memory = calculate_gas_extend_memory( |
| 153 | evm.memory, [(memory_start_position, memory_size)] |
| 154 | ) |
| 155 | |
| 156 | charge_gas(evm, GasCosts.ZERO + extend_memory.cost) |
| 157 | |
| 158 | # OPERATION |
| 159 | evm.memory += b"\x00" * extend_memory.expand_by |
| 160 | evm.output = memory_read_bytes( |
| 161 | evm.memory, memory_start_position, memory_size |
| 162 | ) |
| 163 | |
| 164 | evm.running = False |
| 165 | |
| 166 | # PROGRAM COUNTER |
| 167 | pass |
GenericCall ¶
Parameters for the core logic of the CALL* family of opcodes.
| 170 | @final |
|---|
| 171 | @dataclass |
|---|
class GenericCall:
gas¶
| 177 | gas: Uint |
|---|
value¶
| 178 | value: U256 |
|---|
caller¶
| 179 | caller: Address |
|---|
to¶
| 180 | to: Address |
|---|
code_address¶
| 181 | code_address: Address |
|---|
should_transfer_value¶
| 182 | should_transfer_value: bool |
|---|
memory_input_start_position¶
| 183 | memory_input_start_position: U256 |
|---|
memory_input_size¶
| 184 | memory_input_size: U256 |
|---|
memory_output_start_position¶
| 185 | memory_output_start_position: U256 |
|---|
memory_output_size¶
| 186 | memory_output_size: U256 |
|---|
generic_call ¶
Perform the core logic of the CALL* family of opcodes.
def generic_call(evm: Evm, params: GenericCall) -> None:
| 190 | <snip> |
|---|---|
| 193 | from ...vm.interpreter import STACK_DEPTH_LIMIT, process_message |
| 194 | |
| 195 | if evm.message.depth + Uint(1) > STACK_DEPTH_LIMIT: |
| 196 | evm.gas_left += params.gas |
| 197 | push(evm.stack, U256(0)) |
| 198 | return |
| 199 | |
| 200 | call_data = memory_read_bytes( |
| 201 | evm.memory, |
| 202 | params.memory_input_start_position, |
| 203 | params.memory_input_size, |
| 204 | ) |
| 205 | account = get_account(evm.message.tx_env.state, params.code_address) |
| 206 | code = get_code(evm.message.tx_env.state, account.code_hash) |
| 207 | child_message = Message( |
| 208 | block_env=evm.message.block_env, |
| 209 | tx_env=evm.message.tx_env, |
| 210 | caller=params.caller, |
| 211 | target=params.to, |
| 212 | gas=params.gas, |
| 213 | value=params.value, |
| 214 | data=call_data, |
| 215 | code=code, |
| 216 | current_target=params.to, |
| 217 | depth=evm.message.depth + Uint(1), |
| 218 | code_address=params.code_address, |
| 219 | should_transfer_value=params.should_transfer_value, |
| 220 | parent_evm=evm, |
| 221 | ) |
| 222 | child_evm = process_message(child_message) |
| 223 | |
| 224 | if child_evm.error: |
| 225 | incorporate_child_on_error(evm, child_evm) |
| 226 | push(evm.stack, U256(0)) |
| 227 | else: |
| 228 | incorporate_child_on_success(evm, child_evm) |
| 229 | push(evm.stack, U256(1)) |
| 230 | |
| 231 | actual_output_size = min( |
| 232 | params.memory_output_size, U256(len(child_evm.output)) |
| 233 | ) |
| 234 | memory_write( |
| 235 | evm.memory, |
| 236 | params.memory_output_start_position, |
| 237 | child_evm.output[:actual_output_size], |
| 238 | ) |
call ¶
Message-call into an account.
Parameters
evm : The current EVM frame.
def call(evm: Evm) -> None:
| 242 | <snip> |
|---|---|
| 251 | # STACK |
| 252 | gas = Uint(pop(evm.stack)) |
| 253 | to = to_address_masked(pop(evm.stack)) |
| 254 | value = pop(evm.stack) |
| 255 | memory_input_start_position = pop(evm.stack) |
| 256 | memory_input_size = pop(evm.stack) |
| 257 | memory_output_start_position = pop(evm.stack) |
| 258 | memory_output_size = pop(evm.stack) |
| 259 | |
| 260 | # GAS |
| 261 | extend_memory = calculate_gas_extend_memory( |
| 262 | evm.memory, |
| 263 | [ |
| 264 | (memory_input_start_position, memory_input_size), |
| 265 | (memory_output_start_position, memory_output_size), |
| 266 | ], |
| 267 | ) |
| 268 | |
| 269 | code_address = to |
| 270 | |
| 271 | create_gas_cost = GasCosts.NEW_ACCOUNT |
| 272 | if value == 0 or is_account_alive(evm.message.tx_env.state, to): |
| 273 | create_gas_cost = Uint(0) |
| 274 | transfer_gas_cost = Uint(0) if value == 0 else GasCosts.CALL_VALUE |
| 275 | message_call_gas = calculate_message_call_gas( |
| 276 | value, |
| 277 | gas, |
| 278 | Uint(evm.gas_left), |
| 279 | memory_cost=extend_memory.cost, |
| 280 | extra_gas=GasCosts.OPCODE_CALL_BASE |
| 281 | + create_gas_cost |
| 282 | + transfer_gas_cost, |
| 283 | ) |
| 284 | charge_gas(evm, message_call_gas.cost + extend_memory.cost) |
| 285 | |
| 286 | # OPERATION |
| 287 | evm.memory += b"\x00" * extend_memory.expand_by |
| 288 | sender_balance = get_account( |
| 289 | evm.message.tx_env.state, evm.message.current_target |
| 290 | ).balance |
| 291 | if sender_balance < value: |
| 292 | push(evm.stack, U256(0)) |
| 293 | evm.gas_left += message_call_gas.sub_call |
| 294 | else: |
| 295 | generic_call( |
| 296 | evm, |
| 297 | GenericCall( |
| 298 | gas=message_call_gas.sub_call, |
| 299 | value=value, |
| 300 | caller=evm.message.current_target, |
| 301 | to=to, |
| 302 | code_address=code_address, |
| 303 | should_transfer_value=True, |
| 304 | memory_input_start_position=memory_input_start_position, |
| 305 | memory_input_size=memory_input_size, |
| 306 | memory_output_start_position=memory_output_start_position, |
| 307 | memory_output_size=memory_output_size, |
| 308 | ), |
| 309 | ) |
| 310 | |
| 311 | # PROGRAM COUNTER |
| 312 | evm.pc += Uint(1) |
callcode ¶
Message-call into this account with alternative account’s code.
Parameters
evm : The current EVM frame.
def callcode(evm: Evm) -> None:
| 316 | <snip> |
|---|---|
| 325 | # STACK |
| 326 | gas = Uint(pop(evm.stack)) |
| 327 | code_address = to_address_masked(pop(evm.stack)) |
| 328 | value = pop(evm.stack) |
| 329 | memory_input_start_position = pop(evm.stack) |
| 330 | memory_input_size = pop(evm.stack) |
| 331 | memory_output_start_position = pop(evm.stack) |
| 332 | memory_output_size = pop(evm.stack) |
| 333 | |
| 334 | # GAS |
| 335 | to = evm.message.current_target |
| 336 | |
| 337 | extend_memory = calculate_gas_extend_memory( |
| 338 | evm.memory, |
| 339 | [ |
| 340 | (memory_input_start_position, memory_input_size), |
| 341 | (memory_output_start_position, memory_output_size), |
| 342 | ], |
| 343 | ) |
| 344 | transfer_gas_cost = Uint(0) if value == 0 else GasCosts.CALL_VALUE |
| 345 | message_call_gas = calculate_message_call_gas( |
| 346 | value, |
| 347 | gas, |
| 348 | Uint(evm.gas_left), |
| 349 | extend_memory.cost, |
| 350 | GasCosts.OPCODE_CALL_BASE + transfer_gas_cost, |
| 351 | ) |
| 352 | charge_gas(evm, message_call_gas.cost + extend_memory.cost) |
| 353 | |
| 354 | # OPERATION |
| 355 | evm.memory += b"\x00" * extend_memory.expand_by |
| 356 | sender_balance = get_account( |
| 357 | evm.message.tx_env.state, evm.message.current_target |
| 358 | ).balance |
| 359 | if sender_balance < value: |
| 360 | push(evm.stack, U256(0)) |
| 361 | evm.gas_left += message_call_gas.sub_call |
| 362 | else: |
| 363 | generic_call( |
| 364 | evm, |
| 365 | GenericCall( |
| 366 | gas=message_call_gas.sub_call, |
| 367 | value=value, |
| 368 | caller=evm.message.current_target, |
| 369 | to=to, |
| 370 | code_address=code_address, |
| 371 | should_transfer_value=True, |
| 372 | memory_input_start_position=memory_input_start_position, |
| 373 | memory_input_size=memory_input_size, |
| 374 | memory_output_start_position=memory_output_start_position, |
| 375 | memory_output_size=memory_output_size, |
| 376 | ), |
| 377 | ) |
| 378 | |
| 379 | # PROGRAM COUNTER |
| 380 | evm.pc += Uint(1) |
selfdestruct ¶
Halt execution and register account for later deletion.
Parameters
evm : The current EVM frame.
def selfdestruct(evm: Evm) -> None:
| 384 | <snip> |
|---|---|
| 393 | # STACK |
| 394 | beneficiary = to_address_masked(pop(evm.stack)) |
| 395 | |
| 396 | # GAS |
| 397 | gas_cost = GasCosts.OPCODE_SELFDESTRUCT_BASE |
| 398 | if ( |
| 399 | not is_account_alive(evm.message.tx_env.state, beneficiary) |
| 400 | and get_account( |
| 401 | evm.message.tx_env.state, evm.message.current_target |
| 402 | ).balance |
| 403 | != 0 |
| 404 | ): |
| 405 | gas_cost += GasCosts.OPCODE_SELFDESTRUCT_NEW_ACCOUNT |
| 406 | |
| 407 | originator = evm.message.current_target |
| 408 | |
| 409 | refunded_accounts = evm.accounts_to_delete |
| 410 | parent_evm = evm.message.parent_evm |
| 411 | while parent_evm is not None: |
| 412 | refunded_accounts.update(parent_evm.accounts_to_delete) |
| 413 | parent_evm = parent_evm.message.parent_evm |
| 414 | |
| 415 | if originator not in refunded_accounts: |
| 416 | evm.refund_counter += GasCosts.REFUND_SELF_DESTRUCT |
| 417 | |
| 418 | charge_gas(evm, gas_cost) |
| 419 | |
| 420 | beneficiary_balance = get_account( |
| 421 | evm.message.tx_env.state, beneficiary |
| 422 | ).balance |
| 423 | originator_balance = get_account( |
| 424 | evm.message.tx_env.state, originator |
| 425 | ).balance |
| 426 | |
| 427 | # First Transfer to beneficiary |
| 428 | set_account_balance( |
| 429 | evm.message.tx_env.state, |
| 430 | beneficiary, |
| 431 | beneficiary_balance + originator_balance, |
| 432 | ) |
| 433 | # Next, Zero the balance of the address being deleted (must come after |
| 434 | # sending to beneficiary in case the contract named itself as the |
| 435 | # beneficiary). |
| 436 | set_account_balance(evm.message.tx_env.state, originator, U256(0)) |
| 437 | |
| 438 | # register account for deletion |
| 439 | evm.accounts_to_delete.add(originator) |
| 440 | |
| 441 | # mark beneficiary as touched |
| 442 | if account_exists_and_is_empty(evm.message.tx_env.state, beneficiary): |
| 443 | evm.touched_accounts.add(beneficiary) |
| 444 | |
| 445 | # HALT the execution |
| 446 | evm.running = False |
| 447 | |
| 448 | # PROGRAM COUNTER |
| 449 | pass |
delegatecall ¶
Message-call into an account.
Parameters
evm : The current EVM frame.
def delegatecall(evm: Evm) -> None:
| 453 | <snip> |
|---|---|
| 462 | # STACK |
| 463 | gas = Uint(pop(evm.stack)) |
| 464 | code_address = to_address_masked(pop(evm.stack)) |
| 465 | memory_input_start_position = pop(evm.stack) |
| 466 | memory_input_size = pop(evm.stack) |
| 467 | memory_output_start_position = pop(evm.stack) |
| 468 | memory_output_size = pop(evm.stack) |
| 469 | |
| 470 | # GAS |
| 471 | extend_memory = calculate_gas_extend_memory( |
| 472 | evm.memory, |
| 473 | [ |
| 474 | (memory_input_start_position, memory_input_size), |
| 475 | (memory_output_start_position, memory_output_size), |
| 476 | ], |
| 477 | ) |
| 478 | message_call_gas = calculate_message_call_gas( |
| 479 | U256(0), |
| 480 | gas, |
| 481 | Uint(evm.gas_left), |
| 482 | extend_memory.cost, |
| 483 | GasCosts.OPCODE_CALL_BASE, |
| 484 | ) |
| 485 | charge_gas(evm, message_call_gas.cost + extend_memory.cost) |
| 486 | |
| 487 | # OPERATION |
| 488 | evm.memory += b"\x00" * extend_memory.expand_by |
| 489 | generic_call( |
| 490 | evm, |
| 491 | GenericCall( |
| 492 | gas=message_call_gas.sub_call, |
| 493 | value=evm.message.value, |
| 494 | caller=evm.message.caller, |
| 495 | to=evm.message.current_target, |
| 496 | code_address=code_address, |
| 497 | should_transfer_value=False, |
| 498 | memory_input_start_position=memory_input_start_position, |
| 499 | memory_input_size=memory_input_size, |
| 500 | memory_output_start_position=memory_output_start_position, |
| 501 | memory_output_size=memory_output_size, |
| 502 | ), |
| 503 | ) |
| 504 | |
| 505 | # PROGRAM COUNTER |
| 506 | evm.pc += Uint(1) |