ethereum.forks.dao_fork.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:
| 47 | <snip> |
|---|---|
| 56 | # This import causes a circular import error |
| 57 | # if it's not moved inside this method |
| 58 | from ...vm.interpreter import STACK_DEPTH_LIMIT, process_create_message |
| 59 | |
| 60 | # STACK |
| 61 | endowment = pop(evm.stack) |
| 62 | memory_start_position = pop(evm.stack) |
| 63 | memory_size = pop(evm.stack) |
| 64 | |
| 65 | # GAS |
| 66 | extend_memory = calculate_gas_extend_memory( |
| 67 | evm.memory, [(memory_start_position, memory_size)] |
| 68 | ) |
| 69 | |
| 70 | charge_gas(evm, GasCosts.OPCODE_CREATE_BASE + extend_memory.cost) |
| 71 | |
| 72 | create_message_gas = evm.gas_left |
| 73 | evm.gas_left = Uint(0) |
| 74 | |
| 75 | # OPERATION |
| 76 | evm.memory += b"\x00" * extend_memory.expand_by |
| 77 | sender_address = evm.message.current_target |
| 78 | sender = get_account(evm.message.tx_env.state, sender_address) |
| 79 | |
| 80 | contract_address = compute_contract_address( |
| 81 | evm.message.current_target, |
| 82 | get_account( |
| 83 | evm.message.tx_env.state, evm.message.current_target |
| 84 | ).nonce, |
| 85 | ) |
| 86 | |
| 87 | if ( |
| 88 | sender.balance < endowment |
| 89 | or sender.nonce == Uint(2**64 - 1) |
| 90 | or evm.message.depth + Uint(1) > STACK_DEPTH_LIMIT |
| 91 | ): |
| 92 | push(evm.stack, U256(0)) |
| 93 | evm.gas_left += create_message_gas |
| 94 | elif not account_deployable(evm.message.tx_env.state, contract_address): |
| 95 | increment_nonce(evm.message.tx_env.state, evm.message.current_target) |
| 96 | push(evm.stack, U256(0)) |
| 97 | else: |
| 98 | call_data = memory_read_bytes( |
| 99 | evm.memory, memory_start_position, memory_size |
| 100 | ) |
| 101 | |
| 102 | increment_nonce(evm.message.tx_env.state, evm.message.current_target) |
| 103 | |
| 104 | child_message = Message( |
| 105 | block_env=evm.message.block_env, |
| 106 | tx_env=evm.message.tx_env, |
| 107 | caller=evm.message.current_target, |
| 108 | target=Bytes0(), |
| 109 | gas=create_message_gas, |
| 110 | value=endowment, |
| 111 | data=b"", |
| 112 | code=call_data, |
| 113 | current_target=contract_address, |
| 114 | depth=evm.message.depth + Uint(1), |
| 115 | code_address=None, |
| 116 | should_transfer_value=True, |
| 117 | parent_evm=evm, |
| 118 | ) |
| 119 | child_evm = process_create_message(child_message) |
| 120 | |
| 121 | if child_evm.error: |
| 122 | incorporate_child_on_error(evm, child_evm) |
| 123 | push(evm.stack, U256(0)) |
| 124 | else: |
| 125 | incorporate_child_on_success(evm, child_evm) |
| 126 | push( |
| 127 | evm.stack, U256.from_be_bytes(child_evm.message.current_target) |
| 128 | ) |
| 129 | |
| 130 | # PROGRAM COUNTER |
| 131 | evm.pc += Uint(1) |
return_ ¶
Halts execution returning output data.
Parameters
evm : The current EVM frame.
def return_(evm: Evm) -> None:
| 135 | <snip> |
|---|---|
| 144 | # STACK |
| 145 | memory_start_position = pop(evm.stack) |
| 146 | memory_size = pop(evm.stack) |
| 147 | |
| 148 | # GAS |
| 149 | extend_memory = calculate_gas_extend_memory( |
| 150 | evm.memory, [(memory_start_position, memory_size)] |
| 151 | ) |
| 152 | |
| 153 | charge_gas(evm, GasCosts.ZERO + extend_memory.cost) |
| 154 | |
| 155 | # OPERATION |
| 156 | evm.memory += b"\x00" * extend_memory.expand_by |
| 157 | evm.output = memory_read_bytes( |
| 158 | evm.memory, memory_start_position, memory_size |
| 159 | ) |
| 160 | |
| 161 | evm.running = False |
| 162 | |
| 163 | # PROGRAM COUNTER |
| 164 | pass |
GenericCall ¶
Parameters for the core logic of the CALL* family of opcodes.
| 167 | @final |
|---|
| 168 | @dataclass |
|---|
class GenericCall:
gas¶
| 174 | gas: Uint |
|---|
value¶
| 175 | value: U256 |
|---|
caller¶
| 176 | caller: Address |
|---|
to¶
| 177 | to: Address |
|---|
code_address¶
| 178 | code_address: Address |
|---|
should_transfer_value¶
| 179 | should_transfer_value: bool |
|---|
memory_input_start_position¶
| 180 | memory_input_start_position: U256 |
|---|
memory_input_size¶
| 181 | memory_input_size: U256 |
|---|
memory_output_start_position¶
| 182 | memory_output_start_position: U256 |
|---|
memory_output_size¶
| 183 | memory_output_size: U256 |
|---|
generic_call ¶
Perform the core logic of the CALL* family of opcodes.
def generic_call(evm: Evm, params: GenericCall) -> None:
| 187 | <snip> |
|---|---|
| 190 | from ...vm.interpreter import STACK_DEPTH_LIMIT, process_message |
| 191 | |
| 192 | if evm.message.depth + Uint(1) > STACK_DEPTH_LIMIT: |
| 193 | evm.gas_left += params.gas |
| 194 | push(evm.stack, U256(0)) |
| 195 | return |
| 196 | |
| 197 | call_data = memory_read_bytes( |
| 198 | evm.memory, |
| 199 | params.memory_input_start_position, |
| 200 | params.memory_input_size, |
| 201 | ) |
| 202 | account_to_call = get_account( |
| 203 | evm.message.tx_env.state, params.code_address |
| 204 | ) |
| 205 | code = get_code(evm.message.tx_env.state, account_to_call.code_hash) |
| 206 | child_message = Message( |
| 207 | block_env=evm.message.block_env, |
| 208 | tx_env=evm.message.tx_env, |
| 209 | caller=params.caller, |
| 210 | target=params.to, |
| 211 | gas=params.gas, |
| 212 | value=params.value, |
| 213 | data=call_data, |
| 214 | code=code, |
| 215 | current_target=params.to, |
| 216 | depth=evm.message.depth + Uint(1), |
| 217 | code_address=params.code_address, |
| 218 | should_transfer_value=params.should_transfer_value, |
| 219 | parent_evm=evm, |
| 220 | ) |
| 221 | child_evm = process_message(child_message) |
| 222 | |
| 223 | if child_evm.error: |
| 224 | incorporate_child_on_error(evm, child_evm) |
| 225 | push(evm.stack, U256(0)) |
| 226 | else: |
| 227 | incorporate_child_on_success(evm, child_evm) |
| 228 | push(evm.stack, U256(1)) |
| 229 | |
| 230 | actual_output_size = min( |
| 231 | params.memory_output_size, U256(len(child_evm.output)) |
| 232 | ) |
| 233 | memory_write( |
| 234 | evm.memory, |
| 235 | params.memory_output_start_position, |
| 236 | child_evm.output[:actual_output_size], |
| 237 | ) |
call ¶
Message-call into an account.
Parameters
evm : The current EVM frame.
def call(evm: Evm) -> None:
| 241 | <snip> |
|---|---|
| 250 | # STACK |
| 251 | gas = Uint(pop(evm.stack)) |
| 252 | to = to_address_masked(pop(evm.stack)) |
| 253 | value = pop(evm.stack) |
| 254 | memory_input_start_position = pop(evm.stack) |
| 255 | memory_input_size = pop(evm.stack) |
| 256 | memory_output_start_position = pop(evm.stack) |
| 257 | memory_output_size = pop(evm.stack) |
| 258 | |
| 259 | # GAS |
| 260 | extend_memory = calculate_gas_extend_memory( |
| 261 | evm.memory, |
| 262 | [ |
| 263 | (memory_input_start_position, memory_input_size), |
| 264 | (memory_output_start_position, memory_output_size), |
| 265 | ], |
| 266 | ) |
| 267 | |
| 268 | code_address = to |
| 269 | |
| 270 | message_call_gas = calculate_message_call_gas( |
| 271 | evm.message.tx_env.state, gas, to, value |
| 272 | ) |
| 273 | charge_gas(evm, message_call_gas.cost + extend_memory.cost) |
| 274 | |
| 275 | # OPERATION |
| 276 | evm.memory += b"\x00" * extend_memory.expand_by |
| 277 | sender_balance = get_account( |
| 278 | evm.message.tx_env.state, evm.message.current_target |
| 279 | ).balance |
| 280 | if sender_balance < value: |
| 281 | push(evm.stack, U256(0)) |
| 282 | evm.gas_left += message_call_gas.sub_call |
| 283 | else: |
| 284 | generic_call( |
| 285 | evm, |
| 286 | GenericCall( |
| 287 | gas=message_call_gas.sub_call, |
| 288 | value=value, |
| 289 | caller=evm.message.current_target, |
| 290 | to=to, |
| 291 | code_address=code_address, |
| 292 | should_transfer_value=True, |
| 293 | memory_input_start_position=memory_input_start_position, |
| 294 | memory_input_size=memory_input_size, |
| 295 | memory_output_start_position=memory_output_start_position, |
| 296 | memory_output_size=memory_output_size, |
| 297 | ), |
| 298 | ) |
| 299 | |
| 300 | # PROGRAM COUNTER |
| 301 | 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:
| 305 | <snip> |
|---|---|
| 314 | # STACK |
| 315 | gas = Uint(pop(evm.stack)) |
| 316 | code_address = to_address_masked(pop(evm.stack)) |
| 317 | value = pop(evm.stack) |
| 318 | memory_input_start_position = pop(evm.stack) |
| 319 | memory_input_size = pop(evm.stack) |
| 320 | memory_output_start_position = pop(evm.stack) |
| 321 | memory_output_size = pop(evm.stack) |
| 322 | |
| 323 | # GAS |
| 324 | to = evm.message.current_target |
| 325 | |
| 326 | extend_memory = calculate_gas_extend_memory( |
| 327 | evm.memory, |
| 328 | [ |
| 329 | (memory_input_start_position, memory_input_size), |
| 330 | (memory_output_start_position, memory_output_size), |
| 331 | ], |
| 332 | ) |
| 333 | message_call_gas = calculate_message_call_gas( |
| 334 | evm.message.tx_env.state, gas, to, value |
| 335 | ) |
| 336 | charge_gas(evm, message_call_gas.cost + extend_memory.cost) |
| 337 | |
| 338 | # OPERATION |
| 339 | evm.memory += b"\x00" * extend_memory.expand_by |
| 340 | sender_balance = get_account( |
| 341 | evm.message.tx_env.state, evm.message.current_target |
| 342 | ).balance |
| 343 | if sender_balance < value: |
| 344 | push(evm.stack, U256(0)) |
| 345 | evm.gas_left += message_call_gas.sub_call |
| 346 | else: |
| 347 | generic_call( |
| 348 | evm, |
| 349 | GenericCall( |
| 350 | gas=message_call_gas.sub_call, |
| 351 | value=value, |
| 352 | caller=evm.message.current_target, |
| 353 | to=to, |
| 354 | code_address=code_address, |
| 355 | should_transfer_value=True, |
| 356 | memory_input_start_position=memory_input_start_position, |
| 357 | memory_input_size=memory_input_size, |
| 358 | memory_output_start_position=memory_output_start_position, |
| 359 | memory_output_size=memory_output_size, |
| 360 | ), |
| 361 | ) |
| 362 | |
| 363 | # PROGRAM COUNTER |
| 364 | evm.pc += Uint(1) |
selfdestruct ¶
Halt execution and register account for later deletion.
Parameters
evm : The current EVM frame.
def selfdestruct(evm: Evm) -> None:
| 368 | <snip> |
|---|---|
| 377 | # STACK |
| 378 | beneficiary = to_address_masked(pop(evm.stack)) |
| 379 | |
| 380 | # GAS |
| 381 | gas_cost = GasCosts.ZERO |
| 382 | |
| 383 | originator = evm.message.current_target |
| 384 | |
| 385 | refunded_accounts = evm.accounts_to_delete |
| 386 | parent_evm = evm.message.parent_evm |
| 387 | while parent_evm is not None: |
| 388 | refunded_accounts.update(parent_evm.accounts_to_delete) |
| 389 | parent_evm = parent_evm.message.parent_evm |
| 390 | |
| 391 | if originator not in refunded_accounts: |
| 392 | evm.refund_counter += int(GasCosts.REFUND_SELF_DESTRUCT) |
| 393 | |
| 394 | charge_gas(evm, gas_cost) |
| 395 | |
| 396 | # OPERATION |
| 397 | beneficiary_balance = get_account( |
| 398 | evm.message.tx_env.state, beneficiary |
| 399 | ).balance |
| 400 | originator_balance = get_account( |
| 401 | evm.message.tx_env.state, originator |
| 402 | ).balance |
| 403 | |
| 404 | # First Transfer to beneficiary |
| 405 | set_account_balance( |
| 406 | evm.message.tx_env.state, |
| 407 | beneficiary, |
| 408 | beneficiary_balance + originator_balance, |
| 409 | ) |
| 410 | # Next, Zero the balance of the address being deleted (must come after |
| 411 | # sending to beneficiary in case the contract named itself as the |
| 412 | # beneficiary). |
| 413 | set_account_balance(evm.message.tx_env.state, originator, U256(0)) |
| 414 | |
| 415 | # register account for deletion |
| 416 | evm.accounts_to_delete.add(originator) |
| 417 | |
| 418 | # HALT the execution |
| 419 | evm.running = False |
| 420 | |
| 421 | # PROGRAM COUNTER |
| 422 | pass |
delegatecall ¶
Message-call into an account.
Parameters
evm : The current EVM frame.
def delegatecall(evm: Evm) -> None:
| 426 | <snip> |
|---|---|
| 435 | # STACK |
| 436 | gas = Uint(pop(evm.stack)) |
| 437 | code_address = to_address_masked(pop(evm.stack)) |
| 438 | memory_input_start_position = pop(evm.stack) |
| 439 | memory_input_size = pop(evm.stack) |
| 440 | memory_output_start_position = pop(evm.stack) |
| 441 | memory_output_size = pop(evm.stack) |
| 442 | |
| 443 | # GAS |
| 444 | extend_memory = calculate_gas_extend_memory( |
| 445 | evm.memory, |
| 446 | [ |
| 447 | (memory_input_start_position, memory_input_size), |
| 448 | (memory_output_start_position, memory_output_size), |
| 449 | ], |
| 450 | ) |
| 451 | charge_gas(evm, GasCosts.OPCODE_CALL_BASE + gas + extend_memory.cost) |
| 452 | |
| 453 | # OPERATION |
| 454 | evm.memory += b"\x00" * extend_memory.expand_by |
| 455 | generic_call( |
| 456 | evm, |
| 457 | GenericCall( |
| 458 | gas=gas, |
| 459 | value=evm.message.value, |
| 460 | caller=evm.message.caller, |
| 461 | to=evm.message.current_target, |
| 462 | code_address=code_address, |
| 463 | should_transfer_value=False, |
| 464 | memory_input_start_position=memory_input_start_position, |
| 465 | memory_input_size=memory_input_size, |
| 466 | memory_output_start_position=memory_output_start_position, |
| 467 | memory_output_size=memory_output_size, |
| 468 | ), |
| 469 | ) |
| 470 | |
| 471 | # PROGRAM COUNTER |
| 472 | evm.pc += Uint(1) |