ethereum.tangerine_whistle.vm.instructions.systemethereum.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
    """
52
    Creates a new account with associated code.
53
54
    Parameters
55
    ----------
56
    evm :
57
        The current EVM frame.
58
    """
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, GAS_CREATE + 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.env.state, sender_address)
82
83
    contract_address = compute_contract_address(
84
        evm.message.current_target,
85
        get_account(evm.env.state, evm.message.current_target).nonce,
86
    )
87
88
    if (
89
        sender.balance < endowment
90
        or sender.nonce == Uint(2**64 - 1)
91
        or evm.message.depth + 1 > STACK_DEPTH_LIMIT
92
    ):
93
        push(evm.stack, U256(0))
94
        evm.gas_left += create_message_gas
95
    elif account_has_code_or_nonce(evm.env.state, contract_address):
96
        increment_nonce(evm.env.state, evm.message.current_target)
97
        push(evm.stack, U256(0))
98
    else:
99
        call_data = memory_read_bytes(
100
            evm.memory, memory_start_position, memory_size
101
        )
102
103
        increment_nonce(evm.env.state, evm.message.current_target)
104
105
        child_message = Message(
106
            caller=evm.message.current_target,
107
            target=Bytes0(),
108
            gas=create_message_gas,
109
            value=endowment,
110
            data=b"",
111
            code=call_data,
112
            current_target=contract_address,
113
            depth=evm.message.depth + 1,
114
            code_address=None,
115
            should_transfer_value=True,
116
            parent_evm=evm,
117
        )
118
        child_evm = process_create_message(child_message, evm.env)
119
120
        if child_evm.error:
121
            incorporate_child_on_error(evm, child_evm)
122
            push(evm.stack, U256(0))
123
        else:
124
            incorporate_child_on_success(evm, child_evm)
125
            push(
126
                evm.stack, U256.from_be_bytes(child_evm.message.current_target)
127
            )
128
129
    # PROGRAM COUNTER
130
    evm.pc += 1

return_

Halts execution returning output data.

Parameters

evm : The current EVM frame.

def return_(evm: Evm) -> None:
134
    """
135
    Halts execution returning output data.
136
137
    Parameters
138
    ----------
139
    evm :
140
        The current EVM frame.
141
    """
142
    # STACK
143
    memory_start_position = pop(evm.stack)
144
    memory_size = pop(evm.stack)
145
146
    # GAS
147
    extend_memory = calculate_gas_extend_memory(
148
        evm.memory, [(memory_start_position, memory_size)]
149
    )
150
151
    charge_gas(evm, GAS_ZERO + extend_memory.cost)
152
153
    # OPERATION
154
    evm.memory += b"\x00" * extend_memory.expand_by
155
    evm.output = memory_read_bytes(
156
        evm.memory, memory_start_position, memory_size
157
    )
158
159
    evm.running = False
160
161
    # PROGRAM COUNTER
162
    pass

generic_call

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

def generic_call(evm: Evm, ​​gas: Uint, ​​value: U256, ​​caller: Address, ​​to: Address, ​​code_address: Address, ​​should_transfer_value: bool, ​​memory_input_start_position: U256, ​​memory_input_size: U256, ​​memory_output_start_position: U256, ​​memory_output_size: U256) -> None:
178
    """
179
    Perform the core logic of the `CALL*` family of opcodes.
180
    """
181
    from ...vm.interpreter import STACK_DEPTH_LIMIT, process_message
182
183
    if evm.message.depth + 1 > STACK_DEPTH_LIMIT:
184
        evm.gas_left += gas
185
        push(evm.stack, U256(0))
186
        return
187
188
    call_data = memory_read_bytes(
189
        evm.memory, memory_input_start_position, memory_input_size
190
    )
191
    code = get_account(evm.env.state, code_address).code
192
    child_message = Message(
193
        caller=caller,
194
        target=to,
195
        gas=gas,
196
        value=value,
197
        data=call_data,
198
        code=code,
199
        current_target=to,
200
        depth=evm.message.depth + 1,
201
        code_address=code_address,
202
        should_transfer_value=should_transfer_value,
203
        parent_evm=evm,
204
    )
205
    child_evm = process_message(child_message, evm.env)
206
207
    if child_evm.error:
208
        incorporate_child_on_error(evm, child_evm)
209
        push(evm.stack, U256(0))
210
    else:
211
        incorporate_child_on_success(evm, child_evm)
212
        push(evm.stack, U256(1))
213
214
    actual_output_size = min(memory_output_size, U256(len(child_evm.output)))
215
    memory_write(
216
        evm.memory,
217
        memory_output_start_position,
218
        child_evm.output[:actual_output_size],
219
    )

call

Message-call into an account.

Parameters

evm : The current EVM frame.

def call(evm: Evm) -> None:
223
    """
224
    Message-call into an account.
225
226
    Parameters
227
    ----------
228
    evm :
229
        The current EVM frame.
230
    """
231
    # STACK
232
    gas = Uint(pop(evm.stack))
233
    to = to_address(pop(evm.stack))
234
    value = pop(evm.stack)
235
    memory_input_start_position = pop(evm.stack)
236
    memory_input_size = pop(evm.stack)
237
    memory_output_start_position = pop(evm.stack)
238
    memory_output_size = pop(evm.stack)
239
240
    # GAS
241
    extend_memory = calculate_gas_extend_memory(
242
        evm.memory,
243
        [
244
            (memory_input_start_position, memory_input_size),
245
            (memory_output_start_position, memory_output_size),
246
        ],
247
    )
247
    _account_exists = account_exists(evm.env.state, to)
248
    create_gas_cost = Uint(0) if _account_exists else GAS_NEW_ACCOUNT
248
    create_gas_cost = (
249
        Uint(0)
250
        if value == 0 or is_account_alive(evm.env.state, to)
251
        else GAS_NEW_ACCOUNT
252
    )
253
    transfer_gas_cost = Uint(0) if value == 0 else GAS_CALL_VALUE
254
    message_call_gas = calculate_message_call_gas(
255
        value,
256
        gas,
257
        Uint(evm.gas_left),
258
        extend_memory.cost,
259
        GAS_CALL + create_gas_cost + transfer_gas_cost,
260
    )
261
    charge_gas(evm, message_call_gas.cost + extend_memory.cost)
262
263
    # OPERATION
264
    evm.memory += b"\x00" * extend_memory.expand_by
265
    sender_balance = get_account(
266
        evm.env.state, evm.message.current_target
267
    ).balance
268
    if sender_balance < value:
269
        push(evm.stack, U256(0))
270
        evm.gas_left += message_call_gas.stipend
271
    else:
272
        generic_call(
273
            evm,
274
            message_call_gas.stipend,
275
            value,
276
            evm.message.current_target,
277
            to,
278
            to,
279
            True,
280
            memory_input_start_position,
281
            memory_input_size,
282
            memory_output_start_position,
283
            memory_output_size,
284
        )
285
286
    # PROGRAM COUNTER
287
    evm.pc += 1

callcode

Message-call into this account with alternative account’s code.

Parameters

evm : The current EVM frame.

def callcode(evm: Evm) -> None:
291
    """
292
    Message-call into this account with alternative account’s code.
293
294
    Parameters
295
    ----------
296
    evm :
297
        The current EVM frame.
298
    """
299
    # STACK
300
    gas = Uint(pop(evm.stack))
301
    code_address = to_address(pop(evm.stack))
302
    value = pop(evm.stack)
303
    memory_input_start_position = pop(evm.stack)
304
    memory_input_size = pop(evm.stack)
305
    memory_output_start_position = pop(evm.stack)
306
    memory_output_size = pop(evm.stack)
307
308
    # GAS
309
    to = evm.message.current_target
310
311
    extend_memory = calculate_gas_extend_memory(
312
        evm.memory,
313
        [
314
            (memory_input_start_position, memory_input_size),
315
            (memory_output_start_position, memory_output_size),
316
        ],
317
    )
318
    transfer_gas_cost = Uint(0) if value == 0 else GAS_CALL_VALUE
319
    message_call_gas = calculate_message_call_gas(
320
        value,
321
        gas,
322
        Uint(evm.gas_left),
323
        extend_memory.cost,
324
        GAS_CALL + transfer_gas_cost,
325
    )
326
    charge_gas(evm, message_call_gas.cost + extend_memory.cost)
327
328
    # OPERATION
329
    evm.memory += b"\x00" * extend_memory.expand_by
330
    sender_balance = get_account(
331
        evm.env.state, evm.message.current_target
332
    ).balance
333
    if sender_balance < value:
334
        push(evm.stack, U256(0))
335
        evm.gas_left += message_call_gas.stipend
336
    else:
337
        generic_call(
338
            evm,
339
            message_call_gas.stipend,
340
            value,
341
            evm.message.current_target,
342
            to,
343
            code_address,
344
            True,
345
            memory_input_start_position,
346
            memory_input_size,
347
            memory_output_start_position,
348
            memory_output_size,
349
        )
350
351
    # PROGRAM COUNTER
352
    evm.pc += 1

selfdestruct

Halt execution and register account for later deletion.

Parameters

evm : The current EVM frame.

def selfdestruct(evm: Evm) -> None:
356
    """
357
    Halt execution and register account for later deletion.
358
359
    Parameters
360
    ----------
361
    evm :
362
        The current EVM frame.
363
    """
364
    # STACK
365
    beneficiary = to_address(pop(evm.stack))
366
367
    # GAS
368
    gas_cost = GAS_SELF_DESTRUCT
365
    if not account_exists(evm.env.state, beneficiary):
369
    if (
370
        not is_account_alive(evm.env.state, beneficiary)
371
        and get_account(evm.env.state, evm.message.current_target).balance != 0
372
    ):
373
        gas_cost += GAS_SELF_DESTRUCT_NEW_ACCOUNT
374
375
    originator = evm.message.current_target
376
377
    refunded_accounts = evm.accounts_to_delete
378
    parent_evm = evm.message.parent_evm
379
    while parent_evm is not None:
380
        refunded_accounts.update(parent_evm.accounts_to_delete)
381
        parent_evm = parent_evm.message.parent_evm
382
383
    if originator not in refunded_accounts:
384
        evm.refund_counter += REFUND_SELF_DESTRUCT
385
386
    charge_gas(evm, gas_cost)
387
388
    # OPERATION
389
    beneficiary_balance = get_account(evm.env.state, beneficiary).balance
390
    originator_balance = get_account(evm.env.state, originator).balance
391
392
    # First Transfer to beneficiary
393
    set_account_balance(
394
        evm.env.state, beneficiary, beneficiary_balance + originator_balance
395
    )
396
    # Next, Zero the balance of the address being deleted (must come after
397
    # sending to beneficiary in case the contract named itself as the
398
    # beneficiary).
399
    set_account_balance(evm.env.state, originator, U256(0))
400
401
    # register account for deletion
402
    evm.accounts_to_delete.add(originator)
403
404
    # mark beneficiary as touched
405
    if account_exists_and_is_empty(evm.env.state, beneficiary):
406
        evm.touched_accounts.add(beneficiary)
407
408
    # HALT the execution
409
    evm.running = False
410
411
    # PROGRAM COUNTER
412
    pass

delegatecall

Message-call into an account.

Parameters

evm : The current EVM frame.

def delegatecall(evm: Evm) -> None:
416
    """
417
    Message-call into an account.
418
419
    Parameters
420
    ----------
421
    evm :
422
        The current EVM frame.
423
    """
424
    # STACK
425
    gas = Uint(pop(evm.stack))
426
    code_address = to_address(pop(evm.stack))
427
    memory_input_start_position = pop(evm.stack)
428
    memory_input_size = pop(evm.stack)
429
    memory_output_start_position = pop(evm.stack)
430
    memory_output_size = pop(evm.stack)
431
432
    # GAS
433
    extend_memory = calculate_gas_extend_memory(
434
        evm.memory,
435
        [
436
            (memory_input_start_position, memory_input_size),
437
            (memory_output_start_position, memory_output_size),
438
        ],
439
    )
440
    message_call_gas = calculate_message_call_gas(
441
        U256(0), gas, Uint(evm.gas_left), extend_memory.cost, GAS_CALL
442
    )
443
    charge_gas(evm, message_call_gas.cost + extend_memory.cost)
444
445
    # OPERATION
446
    evm.memory += b"\x00" * extend_memory.expand_by
447
    generic_call(
448
        evm,
449
        message_call_gas.stipend,
450
        evm.message.value,
451
        evm.message.caller,
452
        evm.message.current_target,
453
        code_address,
454
        False,
455
        memory_input_start_position,
456
        memory_input_size,
457
        memory_output_start_position,
458
        memory_output_size,
459
    )
460
461
    # PROGRAM COUNTER
462
    evm.pc += 1