ethereum.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:
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 + Uint(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 + Uint(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 += Uint(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 + Uint(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 + Uint(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
    )
248
    _account_exists = account_exists(evm.env.state, to)
249
    create_gas_cost = Uint(0) if _account_exists else GAS_NEW_ACCOUNT
250
    transfer_gas_cost = Uint(0) if value == 0 else GAS_CALL_VALUE
251
    message_call_gas = calculate_message_call_gas(
252
        value,
253
        gas,
254
        Uint(evm.gas_left),
255
        extend_memory.cost,
256
        GAS_CALL + create_gas_cost + transfer_gas_cost,
257
    )
258
    charge_gas(evm, message_call_gas.cost + extend_memory.cost)
259
260
    # OPERATION
261
    evm.memory += b"\x00" * extend_memory.expand_by
262
    sender_balance = get_account(
263
        evm.env.state, evm.message.current_target
264
    ).balance
265
    if sender_balance < value:
266
        push(evm.stack, U256(0))
267
        evm.gas_left += message_call_gas.stipend
268
    else:
269
        generic_call(
270
            evm,
271
            message_call_gas.stipend,
272
            value,
273
            evm.message.current_target,
274
            to,
275
            to,
276
            True,
277
            memory_input_start_position,
278
            memory_input_size,
279
            memory_output_start_position,
280
            memory_output_size,
281
        )
282
283
    # PROGRAM COUNTER
284
    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:
288
    """
289
    Message-call into this account with alternative account’s code.
290
291
    Parameters
292
    ----------
293
    evm :
294
        The current EVM frame.
295
    """
296
    # STACK
297
    gas = Uint(pop(evm.stack))
298
    code_address = to_address(pop(evm.stack))
299
    value = pop(evm.stack)
300
    memory_input_start_position = pop(evm.stack)
301
    memory_input_size = pop(evm.stack)
302
    memory_output_start_position = pop(evm.stack)
303
    memory_output_size = pop(evm.stack)
304
305
    # GAS
306
    to = evm.message.current_target
307
308
    extend_memory = calculate_gas_extend_memory(
309
        evm.memory,
310
        [
311
            (memory_input_start_position, memory_input_size),
312
            (memory_output_start_position, memory_output_size),
313
        ],
314
    )
315
    transfer_gas_cost = Uint(0) if value == 0 else GAS_CALL_VALUE
316
    message_call_gas = calculate_message_call_gas(
317
        value,
318
        gas,
319
        Uint(evm.gas_left),
320
        extend_memory.cost,
321
        GAS_CALL + transfer_gas_cost,
322
    )
323
    charge_gas(evm, message_call_gas.cost + extend_memory.cost)
324
325
    # OPERATION
326
    evm.memory += b"\x00" * extend_memory.expand_by
327
    sender_balance = get_account(
328
        evm.env.state, evm.message.current_target
329
    ).balance
330
    if sender_balance < value:
331
        push(evm.stack, U256(0))
332
        evm.gas_left += message_call_gas.stipend
333
    else:
334
        generic_call(
335
            evm,
336
            message_call_gas.stipend,
337
            value,
338
            evm.message.current_target,
339
            to,
340
            code_address,
341
            True,
342
            memory_input_start_position,
343
            memory_input_size,
344
            memory_output_start_position,
345
            memory_output_size,
346
        )
347
348
    # PROGRAM COUNTER
349
    evm.pc += Uint(1)

selfdestruct

Halt execution and register account for later deletion.

Parameters

evm : The current EVM frame.

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

delegatecall

Message-call into an account.

Parameters

evm : The current EVM frame.

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