ethereum.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:
47
    """
48
    Creates a new account with associated code.
49
50
    Parameters
51
    ----------
52
    evm :
53
        The current EVM frame.
54
55
    """
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 = max_message_call_gas(Uint(evm.gas_left))
73
    evm.gas_left -= create_message_gas
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.block_env.state, sender_address)
79
80
    contract_address = compute_contract_address(
81
        evm.message.current_target,
82
        get_account(
83
            evm.message.block_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 account_has_code_or_nonce(
95
        evm.message.block_env.state, contract_address
96
    ) or account_has_storage(evm.message.block_env.state, contract_address):
97
        increment_nonce(
98
            evm.message.block_env.state, evm.message.current_target
99
        )
100
        push(evm.stack, U256(0))
101
    else:
102
        call_data = memory_read_bytes(
103
            evm.memory, memory_start_position, memory_size
104
        )
105
106
        increment_nonce(
107
            evm.message.block_env.state, evm.message.current_target
108
        )
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
    """
142
    Halts execution returning output data.
143
144
    Parameters
145
    ----------
146
    evm :
147
        The current EVM frame.
148
149
    """
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

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:
186
    """
187
    Perform the core logic of the `CALL*` family of opcodes.
188
    """
189
    from ...vm.interpreter import STACK_DEPTH_LIMIT, process_message
190
191
    if evm.message.depth + Uint(1) > STACK_DEPTH_LIMIT:
192
        evm.gas_left += gas
193
        push(evm.stack, U256(0))
194
        return
195
196
    call_data = memory_read_bytes(
197
        evm.memory, memory_input_start_position, memory_input_size
198
    )
199
    account = get_account(evm.message.block_env.state, code_address)
200
    code = get_code(evm.message.block_env.state, account.code_hash)
201
    child_message = Message(
202
        block_env=evm.message.block_env,
203
        tx_env=evm.message.tx_env,
204
        caller=caller,
205
        target=to,
206
        gas=gas,
207
        value=value,
208
        data=call_data,
209
        code=code,
210
        current_target=to,
211
        depth=evm.message.depth + Uint(1),
212
        code_address=code_address,
213
        should_transfer_value=should_transfer_value,
214
        parent_evm=evm,
215
    )
216
    child_evm = process_message(child_message)
217
218
    if child_evm.error:
219
        incorporate_child_on_error(evm, child_evm)
220
        push(evm.stack, U256(0))
221
    else:
222
        incorporate_child_on_success(evm, child_evm)
223
        push(evm.stack, U256(1))
224
225
    actual_output_size = min(memory_output_size, U256(len(child_evm.output)))
226
    memory_write(
227
        evm.memory,
228
        memory_output_start_position,
229
        child_evm.output[:actual_output_size],
230
    )

call

Message-call into an account.

Parameters

evm : The current EVM frame.

def call(evm: Evm) -> None:
234
    """
235
    Message-call into an account.
236
237
    Parameters
238
    ----------
239
    evm :
240
        The current EVM frame.
241
242
    """
243
    # STACK
244
    gas = Uint(pop(evm.stack))
245
    to = to_address_masked(pop(evm.stack))
246
    value = pop(evm.stack)
247
    memory_input_start_position = pop(evm.stack)
248
    memory_input_size = pop(evm.stack)
249
    memory_output_start_position = pop(evm.stack)
250
    memory_output_size = pop(evm.stack)
251
252
    # GAS
253
    extend_memory = calculate_gas_extend_memory(
254
        evm.memory,
255
        [
256
            (memory_input_start_position, memory_input_size),
257
            (memory_output_start_position, memory_output_size),
258
        ],
259
    )
260
261
    code_address = to
262
263
    _account_exists = account_exists(evm.message.block_env.state, to)
264
    create_gas_cost = Uint(0) if _account_exists else GasCosts.NEW_ACCOUNT
265
    transfer_gas_cost = Uint(0) if value == 0 else GasCosts.CALL_VALUE
266
    message_call_gas = calculate_message_call_gas(
267
        value,
268
        gas,
269
        Uint(evm.gas_left),
270
        extend_memory.cost,
271
        GasCosts.OPCODE_CALL_BASE + create_gas_cost + transfer_gas_cost,
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.block_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
            message_call_gas.sub_call,
287
            value,
288
            evm.message.current_target,
289
            to,
290
            code_address,
291
            True,
292
            memory_input_start_position,
293
            memory_input_size,
294
            memory_output_start_position,
295
            memory_output_size,
296
        )
297
298
    # PROGRAM COUNTER
299
    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:
303
    """
304
    Message-call into this account with alternative account’s code.
305
306
    Parameters
307
    ----------
308
    evm :
309
        The current EVM frame.
310
311
    """
312
    # STACK
313
    gas = Uint(pop(evm.stack))
314
    code_address = to_address_masked(pop(evm.stack))
315
    value = pop(evm.stack)
316
    memory_input_start_position = pop(evm.stack)
317
    memory_input_size = pop(evm.stack)
318
    memory_output_start_position = pop(evm.stack)
319
    memory_output_size = pop(evm.stack)
320
321
    # GAS
322
    to = evm.message.current_target
323
324
    extend_memory = calculate_gas_extend_memory(
325
        evm.memory,
326
        [
327
            (memory_input_start_position, memory_input_size),
328
            (memory_output_start_position, memory_output_size),
329
        ],
330
    )
331
    transfer_gas_cost = Uint(0) if value == 0 else GasCosts.CALL_VALUE
332
    message_call_gas = calculate_message_call_gas(
333
        value,
334
        gas,
335
        Uint(evm.gas_left),
336
        extend_memory.cost,
337
        GasCosts.OPCODE_CALL_BASE + transfer_gas_cost,
338
    )
339
    charge_gas(evm, message_call_gas.cost + extend_memory.cost)
340
341
    # OPERATION
342
    evm.memory += b"\x00" * extend_memory.expand_by
343
    sender_balance = get_account(
344
        evm.message.block_env.state, evm.message.current_target
345
    ).balance
346
    if sender_balance < value:
347
        push(evm.stack, U256(0))
348
        evm.gas_left += message_call_gas.sub_call
349
    else:
350
        generic_call(
351
            evm,
352
            message_call_gas.sub_call,
353
            value,
354
            evm.message.current_target,
355
            to,
356
            code_address,
357
            True,
358
            memory_input_start_position,
359
            memory_input_size,
360
            memory_output_start_position,
361
            memory_output_size,
362
        )
363
364
    # PROGRAM COUNTER
365
    evm.pc += Uint(1)

selfdestruct

Halt execution and register account for later deletion.

Parameters

evm : The current EVM frame.

def selfdestruct(evm: Evm) -> None:
369
    """
370
    Halt execution and register account for later deletion.
371
372
    Parameters
373
    ----------
374
    evm :
375
        The current EVM frame.
376
377
    """
378
    # STACK
379
    beneficiary = to_address_masked(pop(evm.stack))
380
381
    # GAS
382
    gas_cost = GasCosts.OPCODE_SELFDESTRUCT_BASE
383
    if not account_exists(evm.message.block_env.state, beneficiary):
384
        gas_cost += GasCosts.OPCODE_SELFDESTRUCT_NEW_ACCOUNT
385
386
    originator = evm.message.current_target
387
388
    refunded_accounts = evm.accounts_to_delete
389
    parent_evm = evm.message.parent_evm
390
    while parent_evm is not None:
391
        refunded_accounts.update(parent_evm.accounts_to_delete)
392
        parent_evm = parent_evm.message.parent_evm
393
394
    if originator not in refunded_accounts:
395
        evm.refund_counter += GasCosts.REFUND_SELF_DESTRUCT
396
397
    charge_gas(evm, gas_cost)
398
399
    # OPERATION
400
    beneficiary_balance = get_account(
401
        evm.message.block_env.state, beneficiary
402
    ).balance
403
    originator_balance = get_account(
404
        evm.message.block_env.state, originator
405
    ).balance
406
407
    # First Transfer to beneficiary
408
    set_account_balance(
409
        evm.message.block_env.state,
410
        beneficiary,
411
        beneficiary_balance + originator_balance,
412
    )
413
    # Next, Zero the balance of the address being deleted (must come after
414
    # sending to beneficiary in case the contract named itself as the
415
    # beneficiary).
416
    set_account_balance(evm.message.block_env.state, originator, U256(0))
417
418
    # register account for deletion
419
    evm.accounts_to_delete.add(originator)
420
421
    # HALT the execution
422
    evm.running = False
423
424
    # PROGRAM COUNTER
425
    pass

delegatecall

Message-call into an account.

Parameters

evm : The current EVM frame.

def delegatecall(evm: Evm) -> None:
429
    """
430
    Message-call into an account.
431
432
    Parameters
433
    ----------
434
    evm :
435
        The current EVM frame.
436
437
    """
438
    # STACK
439
    gas = Uint(pop(evm.stack))
440
    code_address = to_address_masked(pop(evm.stack))
441
    memory_input_start_position = pop(evm.stack)
442
    memory_input_size = pop(evm.stack)
443
    memory_output_start_position = pop(evm.stack)
444
    memory_output_size = pop(evm.stack)
445
446
    # GAS
447
    extend_memory = calculate_gas_extend_memory(
448
        evm.memory,
449
        [
450
            (memory_input_start_position, memory_input_size),
451
            (memory_output_start_position, memory_output_size),
452
        ],
453
    )
454
    message_call_gas = calculate_message_call_gas(
455
        U256(0),
456
        gas,
457
        Uint(evm.gas_left),
458
        extend_memory.cost,
459
        GasCosts.OPCODE_CALL_BASE,
460
    )
461
    charge_gas(evm, message_call_gas.cost + extend_memory.cost)
462
463
    # OPERATION
464
    evm.memory += b"\x00" * extend_memory.expand_by
465
    generic_call(
466
        evm,
467
        message_call_gas.sub_call,
468
        evm.message.value,
469
        evm.message.caller,
470
        evm.message.current_target,
471
        code_address,
472
        False,
473
        memory_input_start_position,
474
        memory_input_size,
475
        memory_output_start_position,
476
        memory_output_size,
477
    )
478
479
    # PROGRAM COUNTER
480
    evm.pc += Uint(1)