ethereum.forks.tangerine_whistle.vm.instructions.systemethereum.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:
51
    <snip>
60
    # This import causes a circular import error
61
    # if it's not moved inside this method
62
    from ...vm.interpreter import STACK_DEPTH_LIMIT, process_create_message
63
64
    # STACK
65
    endowment = pop(evm.stack)
66
    memory_start_position = pop(evm.stack)
67
    memory_size = pop(evm.stack)
68
69
    # GAS
70
    extend_memory = calculate_gas_extend_memory(
71
        evm.memory, [(memory_start_position, memory_size)]
72
    )
73
74
    charge_gas(evm, GasCosts.OPCODE_CREATE_BASE + extend_memory.cost)
75
76
    create_message_gas = max_message_call_gas(Uint(evm.gas_left))
77
    evm.gas_left -= create_message_gas
78
79
    # OPERATION
80
    evm.memory += b"\x00" * extend_memory.expand_by
81
    sender_address = evm.message.current_target
82
    sender = get_account(evm.message.tx_env.state, sender_address)
83
84
    contract_address = compute_contract_address(
85
        evm.message.current_target,
86
        get_account(
87
            evm.message.tx_env.state, evm.message.current_target
88
        ).nonce,
89
    )
90
91
    if (
92
        sender.balance < endowment
93
        or sender.nonce == Uint(2**64 - 1)
94
        or evm.message.depth + Uint(1) > STACK_DEPTH_LIMIT
95
    ):
96
        push(evm.stack, U256(0))
97
        evm.gas_left += create_message_gas
98
    elif account_has_code_or_nonce(
99
        evm.message.tx_env.state, contract_address
100
    ) or account_has_storage(evm.message.tx_env.state, contract_address):
101
        increment_nonce(evm.message.tx_env.state, evm.message.current_target)
102
        push(evm.stack, U256(0))
103
    else:
104
        call_data = memory_read_bytes(
105
            evm.memory, memory_start_position, memory_size
106
        )
107
108
        increment_nonce(evm.message.tx_env.state, evm.message.current_target)
109
110
        child_message = Message(
111
            block_env=evm.message.block_env,
112
            tx_env=evm.message.tx_env,
113
            caller=evm.message.current_target,
114
            target=Bytes0(),
115
            gas=create_message_gas,
116
            value=endowment,
117
            data=b"",
118
            code=call_data,
119
            current_target=contract_address,
120
            depth=evm.message.depth + Uint(1),
121
            code_address=None,
122
            should_transfer_value=True,
123
            parent_evm=evm,
124
        )
125
        child_evm = process_create_message(child_message)
126
127
        if child_evm.error:
128
            incorporate_child_on_error(evm, child_evm)
129
            push(evm.stack, U256(0))
130
        else:
131
            incorporate_child_on_success(evm, child_evm)
132
            push(
133
                evm.stack, U256.from_be_bytes(child_evm.message.current_target)
134
            )
135
136
    # PROGRAM COUNTER
137
    evm.pc += Uint(1)

return_

Halts execution returning output data.

Parameters

evm : The current EVM frame.

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

GenericCall

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

173
@final
174
@dataclass
class GenericCall:

gas

180
    gas: Uint

value

181
    value: U256

caller

182
    caller: Address

to

183
    to: Address

code_address

184
    code_address: Address

should_transfer_value

185
    should_transfer_value: bool

memory_input_start_position

186
    memory_input_start_position: U256

memory_input_size

187
    memory_input_size: U256

memory_output_start_position

188
    memory_output_start_position: U256

memory_output_size

189
    memory_output_size: U256

generic_call

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

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

call

Message-call into an account.

Parameters

evm : The current EVM frame.

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

selfdestruct

Halt execution and register account for later deletion.

Parameters

evm : The current EVM frame.

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

delegatecall

Message-call into an account.

Parameters

evm : The current EVM frame.

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