ethereum.forks.dao_fork.vm.instructions.systemethereum.forks.tangerine_whistle.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
73
    create_message_gas = evm.gas_left
74
    evm.gas_left = Uint(0)
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 account_has_code_or_nonce(
98
        evm.message.tx_env.state, contract_address
99
    ) or account_has_storage(evm.message.tx_env.state, contract_address):
100
        increment_nonce(evm.message.tx_env.state, evm.message.current_target)
101
        push(evm.stack, U256(0))
102
    else:
103
        call_data = memory_read_bytes(
104
            evm.memory, memory_start_position, memory_size
105
        )
106
107
        increment_nonce(evm.message.tx_env.state, evm.message.current_target)
108
109
        child_message = Message(
110
            block_env=evm.message.block_env,
111
            tx_env=evm.message.tx_env,
112
            caller=evm.message.current_target,
113
            target=Bytes0(),
114
            gas=create_message_gas,
115
            value=endowment,
116
            data=b"",
117
            code=call_data,
118
            current_target=contract_address,
119
            depth=evm.message.depth + Uint(1),
120
            code_address=None,
121
            should_transfer_value=True,
122
            parent_evm=evm,
123
        )
124
        child_evm = process_create_message(child_message)
125
126
        if child_evm.error:
127
            incorporate_child_on_error(evm, child_evm)
128
            push(evm.stack, U256(0))
129
        else:
130
            incorporate_child_on_success(evm, child_evm)
131
            push(
132
                evm.stack, U256.from_be_bytes(child_evm.message.current_target)
133
            )
134
135
    # PROGRAM COUNTER
136
    evm.pc += Uint(1)

return_

Halts execution returning output data.

Parameters

evm : The current EVM frame.

def return_(evm: Evm) -> None:
140
    <snip>
149
    # STACK
150
    memory_start_position = pop(evm.stack)
151
    memory_size = pop(evm.stack)
152
153
    # GAS
154
    extend_memory = calculate_gas_extend_memory(
155
        evm.memory, [(memory_start_position, memory_size)]
156
    )
157
158
    charge_gas(evm, GasCosts.ZERO + extend_memory.cost)
159
160
    # OPERATION
161
    evm.memory += b"\x00" * extend_memory.expand_by
162
    evm.output = memory_read_bytes(
163
        evm.memory, memory_start_position, memory_size
164
    )
165
166
    evm.running = False
167
168
    # PROGRAM COUNTER
169
    pass

GenericCall

Parameters for the core logic of the CALL* family of opcodes.

172
@final
173
@dataclass
class GenericCall:

gas

179
    gas: Uint

value

180
    value: U256

caller

181
    caller: Address

to

182
    to: Address

code_address

183
    code_address: Address

should_transfer_value

184
    should_transfer_value: bool

memory_input_start_position

185
    memory_input_start_position: U256

memory_input_size

186
    memory_input_size: U256

memory_output_start_position

187
    memory_output_start_position: U256

memory_output_size

188
    memory_output_size: U256

generic_call

Perform the core logic of the CALL* family of opcodes.

def generic_call(evm: Evm, ​​params: GenericCall) -> None:
192
    <snip>
195
    from ...vm.interpreter import STACK_DEPTH_LIMIT, process_message
196
197
    if evm.message.depth + Uint(1) > STACK_DEPTH_LIMIT:
198
        evm.gas_left += params.gas
199
        push(evm.stack, U256(0))
200
        return
201
202
    call_data = memory_read_bytes(
203
        evm.memory,
204
        params.memory_input_start_position,
205
        params.memory_input_size,
206
    )
205
    account_to_call = get_account(
206
        evm.message.tx_env.state, params.code_address
207
    )
208
    code = get_code(evm.message.tx_env.state, account_to_call.code_hash)
207
    account = get_account(evm.message.tx_env.state, params.code_address)
208
    code = get_code(evm.message.tx_env.state, account.code_hash)
209
    child_message = Message(
210
        block_env=evm.message.block_env,
211
        tx_env=evm.message.tx_env,
212
        caller=params.caller,
213
        target=params.to,
214
        gas=params.gas,
215
        value=params.value,
216
        data=call_data,
217
        code=code,
218
        current_target=params.to,
219
        depth=evm.message.depth + Uint(1),
220
        code_address=params.code_address,
221
        should_transfer_value=params.should_transfer_value,
222
        parent_evm=evm,
223
    )
224
    child_evm = process_message(child_message)
225
226
    if child_evm.error:
227
        incorporate_child_on_error(evm, child_evm)
228
        push(evm.stack, U256(0))
229
    else:
230
        incorporate_child_on_success(evm, child_evm)
231
        push(evm.stack, U256(1))
232
233
    actual_output_size = min(
234
        params.memory_output_size, U256(len(child_evm.output))
235
    )
236
    memory_write(
237
        evm.memory,
238
        params.memory_output_start_position,
239
        child_evm.output[:actual_output_size],
240
    )

call

Message-call into an account.

Parameters

evm : The current EVM frame.

def call(evm: Evm) -> None:
244
    <snip>
253
    # STACK
254
    gas = Uint(pop(evm.stack))
255
    to = to_address_masked(pop(evm.stack))
256
    value = pop(evm.stack)
257
    memory_input_start_position = pop(evm.stack)
258
    memory_input_size = pop(evm.stack)
259
    memory_output_start_position = pop(evm.stack)
260
    memory_output_size = pop(evm.stack)
261
262
    # GAS
263
    extend_memory = calculate_gas_extend_memory(
264
        evm.memory,
265
        [
266
            (memory_input_start_position, memory_input_size),
267
            (memory_output_start_position, memory_output_size),
268
        ],
269
    )
270
271
    code_address = to
272
273
    _account_exists = account_exists(evm.message.tx_env.state, to)
274
    create_gas_cost = Uint(0) if _account_exists else GasCosts.NEW_ACCOUNT
275
    transfer_gas_cost = Uint(0) if value == 0 else GasCosts.CALL_VALUE
276
    message_call_gas = calculate_message_call_gas(
274
        evm.message.tx_env.state, gas, to, value
277
        value,
278
        gas,
279
        Uint(evm.gas_left),
280
        memory_cost=extend_memory.cost,
281
        extra_gas=GasCosts.OPCODE_CALL_BASE
282
        + create_gas_cost
283
        + transfer_gas_cost,
284
    )
285
    charge_gas(evm, message_call_gas.cost + extend_memory.cost)
286
287
    # OPERATION
288
    evm.memory += b"\x00" * extend_memory.expand_by
289
    sender_balance = get_account(
290
        evm.message.tx_env.state, evm.message.current_target
291
    ).balance
292
    if sender_balance < value:
293
        push(evm.stack, U256(0))
294
        evm.gas_left += message_call_gas.sub_call
295
    else:
296
        generic_call(
297
            evm,
298
            GenericCall(
299
                gas=message_call_gas.sub_call,
300
                value=value,
301
                caller=evm.message.current_target,
302
                to=to,
303
                code_address=code_address,
304
                should_transfer_value=True,
305
                memory_input_start_position=memory_input_start_position,
306
                memory_input_size=memory_input_size,
307
                memory_output_start_position=memory_output_start_position,
308
                memory_output_size=memory_output_size,
309
            ),
310
        )
311
312
    # PROGRAM COUNTER
313
    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:
317
    <snip>
326
    # STACK
327
    gas = Uint(pop(evm.stack))
328
    code_address = to_address_masked(pop(evm.stack))
329
    value = pop(evm.stack)
330
    memory_input_start_position = pop(evm.stack)
331
    memory_input_size = pop(evm.stack)
332
    memory_output_start_position = pop(evm.stack)
333
    memory_output_size = pop(evm.stack)
334
335
    # GAS
336
    to = evm.message.current_target
337
338
    extend_memory = calculate_gas_extend_memory(
339
        evm.memory,
340
        [
341
            (memory_input_start_position, memory_input_size),
342
            (memory_output_start_position, memory_output_size),
343
        ],
344
    )
345
    transfer_gas_cost = Uint(0) if value == 0 else GasCosts.CALL_VALUE
346
    message_call_gas = calculate_message_call_gas(
337
        evm.message.tx_env.state, gas, to, value
347
        value,
348
        gas,
349
        Uint(evm.gas_left),
350
        extend_memory.cost,
351
        GasCosts.OPCODE_CALL_BASE + transfer_gas_cost,
352
    )
353
    charge_gas(evm, message_call_gas.cost + extend_memory.cost)
354
355
    # OPERATION
356
    evm.memory += b"\x00" * extend_memory.expand_by
357
    sender_balance = get_account(
358
        evm.message.tx_env.state, evm.message.current_target
359
    ).balance
360
    if sender_balance < value:
361
        push(evm.stack, U256(0))
362
        evm.gas_left += message_call_gas.sub_call
363
    else:
364
        generic_call(
365
            evm,
366
            GenericCall(
367
                gas=message_call_gas.sub_call,
368
                value=value,
369
                caller=evm.message.current_target,
370
                to=to,
371
                code_address=code_address,
372
                should_transfer_value=True,
373
                memory_input_start_position=memory_input_start_position,
374
                memory_input_size=memory_input_size,
375
                memory_output_start_position=memory_output_start_position,
376
                memory_output_size=memory_output_size,
377
            ),
378
        )
379
380
    # PROGRAM COUNTER
381
    evm.pc += Uint(1)

selfdestruct

Halt execution and register account for later deletion.

Parameters

evm : The current EVM frame.

def selfdestruct(evm: Evm) -> None:
385
    <snip>
394
    # STACK
395
    beneficiary = to_address_masked(pop(evm.stack))
396
397
    # GAS
384
    gas_cost = GasCosts.ZERO
398
    gas_cost = GasCosts.OPCODE_SELFDESTRUCT_BASE
399
    if not account_exists(evm.message.tx_env.state, beneficiary):
400
        gas_cost += GasCosts.OPCODE_SELFDESTRUCT_NEW_ACCOUNT
401
402
    originator = evm.message.current_target
403
404
    refunded_accounts = evm.accounts_to_delete
405
    parent_evm = evm.message.parent_evm
406
    while parent_evm is not None:
407
        refunded_accounts.update(parent_evm.accounts_to_delete)
408
        parent_evm = parent_evm.message.parent_evm
409
410
    if originator not in refunded_accounts:
395
        evm.refund_counter += int(GasCosts.REFUND_SELF_DESTRUCT)
411
        evm.refund_counter += GasCosts.REFUND_SELF_DESTRUCT
412
413
    charge_gas(evm, gas_cost)
414
415
    # OPERATION
416
    beneficiary_balance = get_account(
417
        evm.message.tx_env.state, beneficiary
418
    ).balance
419
    originator_balance = get_account(
420
        evm.message.tx_env.state, originator
421
    ).balance
422
423
    # First Transfer to beneficiary
424
    set_account_balance(
425
        evm.message.tx_env.state,
426
        beneficiary,
427
        beneficiary_balance + originator_balance,
428
    )
429
    # Next, Zero the balance of the address being deleted (must come after
430
    # sending to beneficiary in case the contract named itself as the
431
    # beneficiary).
432
    set_account_balance(evm.message.tx_env.state, originator, U256(0))
433
434
    # register account for deletion
435
    evm.accounts_to_delete.add(originator)
436
437
    # HALT the execution
438
    evm.running = False
439
440
    # PROGRAM COUNTER
441
    pass

delegatecall

Message-call into an account.

Parameters

evm : The current EVM frame.

def delegatecall(evm: Evm) -> None:
445
    <snip>
454
    # STACK
455
    gas = Uint(pop(evm.stack))
456
    code_address = to_address_masked(pop(evm.stack))
457
    memory_input_start_position = pop(evm.stack)
458
    memory_input_size = pop(evm.stack)
459
    memory_output_start_position = pop(evm.stack)
460
    memory_output_size = pop(evm.stack)
461
462
    # GAS
463
    extend_memory = calculate_gas_extend_memory(
464
        evm.memory,
465
        [
466
            (memory_input_start_position, memory_input_size),
467
            (memory_output_start_position, memory_output_size),
468
        ],
469
    )
454
    charge_gas(evm, GasCosts.OPCODE_CALL_BASE + gas + extend_memory.cost)
470
    message_call_gas = calculate_message_call_gas(
471
        U256(0),
472
        gas,
473
        Uint(evm.gas_left),
474
        extend_memory.cost,
475
        GasCosts.OPCODE_CALL_BASE,
476
    )
477
    charge_gas(evm, message_call_gas.cost + extend_memory.cost)
478
479
    # OPERATION
480
    evm.memory += b"\x00" * extend_memory.expand_by
481
    generic_call(
482
        evm,
483
        GenericCall(
461
            gas=gas,
484
            gas=message_call_gas.sub_call,
485
            value=evm.message.value,
486
            caller=evm.message.caller,
487
            to=evm.message.current_target,
488
            code_address=code_address,
489
            should_transfer_value=False,
490
            memory_input_start_position=memory_input_start_position,
491
            memory_input_size=memory_input_size,
492
            memory_output_start_position=memory_output_start_position,
493
            memory_output_size=memory_output_size,
494
        ),
495
    )
496
497
    # PROGRAM COUNTER
498
    evm.pc += Uint(1)