ethereum.istanbul.vm.instructions.systemethereum.muir_glacier.vm.instructions.system

Ethereum Virtual Machine (EVM) System Instructions ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

.. contents:: Table of Contents :backlinks: none :local:

Introduction

Implementations of the EVM system related instructions.

generic_create

Core logic used by the CREATE* family of opcodes.

def generic_create(evm: Evm, ​​endowment: U256, ​​contract_address: Address, ​​memory_start_position: U256, ​​memory_size: U256) -> None:
67
    """
68
    Core logic used by the `CREATE*` family of opcodes.
69
    """
70
    # This import causes a circular import error
71
    # if it's not moved inside this method
72
    from ...vm.interpreter import STACK_DEPTH_LIMIT, process_create_message
73
74
    call_data = memory_read_bytes(
75
        evm.memory, memory_start_position, memory_size
76
    )
77
78
    create_message_gas = max_message_call_gas(Uint(evm.gas_left))
79
    evm.gas_left -= create_message_gas
80
    if evm.message.is_static:
81
        raise WriteInStaticContext
82
    evm.return_data = b""
83
84
    sender_address = evm.message.current_target
85
    sender = get_account(evm.message.block_env.state, sender_address)
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
        evm.gas_left += create_message_gas
93
        push(evm.stack, U256(0))
94
        return
95
96
    if account_has_code_or_nonce(
97
        evm.message.block_env.state, contract_address
98
    ) or account_has_storage(evm.message.block_env.state, contract_address):
99
        increment_nonce(
100
            evm.message.block_env.state, evm.message.current_target
101
        )
102
        push(evm.stack, U256(0))
103
        return
104
105
    call_data = memory_read_bytes(
106
        evm.memory, memory_start_position, memory_size
107
    )
108
109
    increment_nonce(evm.message.block_env.state, evm.message.current_target)
110
111
    child_message = Message(
112
        block_env=evm.message.block_env,
113
        tx_env=evm.message.tx_env,
114
        caller=evm.message.current_target,
115
        target=Bytes0(),
116
        gas=create_message_gas,
117
        value=endowment,
118
        data=b"",
119
        code=call_data,
120
        current_target=contract_address,
121
        depth=evm.message.depth + Uint(1),
122
        code_address=None,
123
        should_transfer_value=True,
124
        is_static=False,
125
        parent_evm=evm,
126
    )
127
    child_evm = process_create_message(child_message)
128
129
    if child_evm.error:
130
        incorporate_child_on_error(evm, child_evm)
131
        evm.return_data = child_evm.output
132
        push(evm.stack, U256(0))
133
    else:
134
        incorporate_child_on_success(evm, child_evm)
135
        evm.return_data = b""
136
        push(evm.stack, U256.from_be_bytes(child_evm.message.current_target))

create

Creates a new account with associated code.

Parameters

evm : The current EVM frame.

def create(evm: Evm) -> None:
140
    """
141
    Creates a new account with associated code.
142
143
    Parameters
144
    ----------
145
    evm :
146
        The current EVM frame.
147
    """
148
    # STACK
149
    endowment = pop(evm.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, GAS_CREATE + extend_memory.cost)
159
160
    # OPERATION
161
    evm.memory += b"\x00" * extend_memory.expand_by
162
    contract_address = compute_contract_address(
163
        evm.message.current_target,
164
        get_account(
165
            evm.message.block_env.state, evm.message.current_target
166
        ).nonce,
167
    )
168
169
    generic_create(
170
        evm, endowment, contract_address, memory_start_position, memory_size
171
    )
172
173
    # PROGRAM COUNTER
174
    evm.pc += Uint(1)

create2

Creates a new account with associated code.

It's similar to CREATE opcode except that the address of new account depends on the init_code instead of the nonce of sender.

Parameters

evm : The current EVM frame.

def create2(evm: Evm) -> None:
178
    """
179
    Creates a new account with associated code.
180
181
    It's similar to CREATE opcode except that the address of new account
182
    depends on the init_code instead of the nonce of sender.
183
184
    Parameters
185
    ----------
186
    evm :
187
        The current EVM frame.
188
    """
189
    # STACK
190
    endowment = pop(evm.stack)
191
    memory_start_position = pop(evm.stack)
192
    memory_size = pop(evm.stack)
193
    salt = pop(evm.stack).to_be_bytes32()
194
195
    # GAS
196
    extend_memory = calculate_gas_extend_memory(
197
        evm.memory, [(memory_start_position, memory_size)]
198
    )
199
    call_data_words = ceil32(Uint(memory_size)) // Uint(32)
200
    charge_gas(
201
        evm,
202
        GAS_CREATE + GAS_KECCAK256_WORD * call_data_words + extend_memory.cost,
203
    )
204
205
    # OPERATION
206
    evm.memory += b"\x00" * extend_memory.expand_by
207
    contract_address = compute_create2_contract_address(
208
        evm.message.current_target,
209
        salt,
210
        memory_read_bytes(evm.memory, memory_start_position, memory_size),
211
    )
212
213
    generic_create(
214
        evm, endowment, contract_address, memory_start_position, memory_size
215
    )
216
217
    # PROGRAM COUNTER
218
    evm.pc += Uint(1)

return_

Halts execution returning output data.

Parameters

evm : The current EVM frame.

def return_(evm: Evm) -> None:
222
    """
223
    Halts execution returning output data.
224
225
    Parameters
226
    ----------
227
    evm :
228
        The current EVM frame.
229
    """
230
    # STACK
231
    memory_start_position = pop(evm.stack)
232
    memory_size = pop(evm.stack)
233
234
    # GAS
235
    extend_memory = calculate_gas_extend_memory(
236
        evm.memory, [(memory_start_position, memory_size)]
237
    )
238
239
    charge_gas(evm, GAS_ZERO + extend_memory.cost)
240
241
    # OPERATION
242
    evm.memory += b"\x00" * extend_memory.expand_by
243
    evm.output = memory_read_bytes(
244
        evm.memory, memory_start_position, memory_size
245
    )
246
247
    evm.running = False
248
249
    # PROGRAM COUNTER
250
    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, ​​is_staticcall: bool, ​​memory_input_start_position: U256, ​​memory_input_size: U256, ​​memory_output_start_position: U256, ​​memory_output_size: U256) -> None:
267
    """
268
    Perform the core logic of the `CALL*` family of opcodes.
269
    """
270
    from ...vm.interpreter import STACK_DEPTH_LIMIT, process_message
271
272
    evm.return_data = b""
273
274
    if evm.message.depth + Uint(1) > STACK_DEPTH_LIMIT:
275
        evm.gas_left += gas
276
        push(evm.stack, U256(0))
277
        return
278
279
    call_data = memory_read_bytes(
280
        evm.memory, memory_input_start_position, memory_input_size
281
    )
282
    code = get_account(evm.message.block_env.state, code_address).code
283
    child_message = Message(
284
        block_env=evm.message.block_env,
285
        tx_env=evm.message.tx_env,
286
        caller=caller,
287
        target=to,
288
        gas=gas,
289
        value=value,
290
        data=call_data,
291
        code=code,
292
        current_target=to,
293
        depth=evm.message.depth + Uint(1),
294
        code_address=code_address,
295
        should_transfer_value=should_transfer_value,
296
        is_static=True if is_staticcall else evm.message.is_static,
297
        parent_evm=evm,
298
    )
299
    child_evm = process_message(child_message)
300
301
    if child_evm.error:
302
        incorporate_child_on_error(evm, child_evm)
303
        evm.return_data = child_evm.output
304
        push(evm.stack, U256(0))
305
    else:
306
        incorporate_child_on_success(evm, child_evm)
307
        evm.return_data = child_evm.output
308
        push(evm.stack, U256(1))
309
310
    actual_output_size = min(memory_output_size, U256(len(child_evm.output)))
311
    memory_write(
312
        evm.memory,
313
        memory_output_start_position,
314
        child_evm.output[:actual_output_size],
315
    )

call

Message-call into an account.

Parameters

evm : The current EVM frame.

def call(evm: Evm) -> None:
319
    """
320
    Message-call into an account.
321
322
    Parameters
323
    ----------
324
    evm :
325
        The current EVM frame.
326
    """
327
    # STACK
328
    gas = Uint(pop(evm.stack))
329
    to = to_address_masked(pop(evm.stack))
330
    value = pop(evm.stack)
331
    memory_input_start_position = pop(evm.stack)
332
    memory_input_size = pop(evm.stack)
333
    memory_output_start_position = pop(evm.stack)
334
    memory_output_size = pop(evm.stack)
335
336
    # GAS
337
    extend_memory = calculate_gas_extend_memory(
338
        evm.memory,
339
        [
340
            (memory_input_start_position, memory_input_size),
341
            (memory_output_start_position, memory_output_size),
342
        ],
343
    )
344
345
    code_address = to
346
347
    create_gas_cost = GAS_NEW_ACCOUNT
348
    if value == 0 or is_account_alive(evm.message.block_env.state, to):
349
        create_gas_cost = Uint(0)
350
    transfer_gas_cost = Uint(0) if value == 0 else GAS_CALL_VALUE
351
    message_call_gas = calculate_message_call_gas(
352
        value,
353
        gas,
354
        Uint(evm.gas_left),
355
        extend_memory.cost,
356
        GAS_CALL + create_gas_cost + transfer_gas_cost,
357
    )
358
    charge_gas(evm, message_call_gas.cost + extend_memory.cost)
359
    if evm.message.is_static and value != U256(0):
360
        raise WriteInStaticContext
361
    evm.memory += b"\x00" * extend_memory.expand_by
362
    sender_balance = get_account(
363
        evm.message.block_env.state, evm.message.current_target
364
    ).balance
365
    if sender_balance < value:
366
        push(evm.stack, U256(0))
367
        evm.return_data = b""
368
        evm.gas_left += message_call_gas.sub_call
369
    else:
370
        generic_call(
371
            evm,
372
            message_call_gas.sub_call,
373
            value,
374
            evm.message.current_target,
375
            to,
376
            code_address,
377
            True,
378
            False,
379
            memory_input_start_position,
380
            memory_input_size,
381
            memory_output_start_position,
382
            memory_output_size,
383
        )
384
385
    # PROGRAM COUNTER
386
    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:
390
    """
391
    Message-call into this account with alternative account’s code.
392
393
    Parameters
394
    ----------
395
    evm :
396
        The current EVM frame.
397
    """
398
    # STACK
399
    gas = Uint(pop(evm.stack))
400
    code_address = to_address_masked(pop(evm.stack))
401
    value = pop(evm.stack)
402
    memory_input_start_position = pop(evm.stack)
403
    memory_input_size = pop(evm.stack)
404
    memory_output_start_position = pop(evm.stack)
405
    memory_output_size = pop(evm.stack)
406
407
    # GAS
408
    to = evm.message.current_target
409
410
    extend_memory = calculate_gas_extend_memory(
411
        evm.memory,
412
        [
413
            (memory_input_start_position, memory_input_size),
414
            (memory_output_start_position, memory_output_size),
415
        ],
416
    )
417
    transfer_gas_cost = Uint(0) if value == 0 else GAS_CALL_VALUE
418
    message_call_gas = calculate_message_call_gas(
419
        value,
420
        gas,
421
        Uint(evm.gas_left),
422
        extend_memory.cost,
423
        GAS_CALL + transfer_gas_cost,
424
    )
425
    charge_gas(evm, message_call_gas.cost + extend_memory.cost)
426
427
    # OPERATION
428
    evm.memory += b"\x00" * extend_memory.expand_by
429
    sender_balance = get_account(
430
        evm.message.block_env.state, evm.message.current_target
431
    ).balance
432
    if sender_balance < value:
433
        push(evm.stack, U256(0))
434
        evm.return_data = b""
435
        evm.gas_left += message_call_gas.sub_call
436
    else:
437
        generic_call(
438
            evm,
439
            message_call_gas.sub_call,
440
            value,
441
            evm.message.current_target,
442
            to,
443
            code_address,
444
            True,
445
            False,
446
            memory_input_start_position,
447
            memory_input_size,
448
            memory_output_start_position,
449
            memory_output_size,
450
        )
451
452
    # PROGRAM COUNTER
453
    evm.pc += Uint(1)

selfdestruct

Halt execution and register account for later deletion.

Parameters

evm : The current EVM frame.

def selfdestruct(evm: Evm) -> None:
457
    """
458
    Halt execution and register account for later deletion.
459
460
    Parameters
461
    ----------
462
    evm :
463
        The current EVM frame.
464
    """
465
    # STACK
466
    beneficiary = to_address_masked(pop(evm.stack))
467
468
    # GAS
469
    gas_cost = GAS_SELF_DESTRUCT
470
    if (
471
        not is_account_alive(evm.message.block_env.state, beneficiary)
472
        and get_account(
473
            evm.message.block_env.state, evm.message.current_target
474
        ).balance
475
        != 0
476
    ):
477
        gas_cost += GAS_SELF_DESTRUCT_NEW_ACCOUNT
478
479
    originator = evm.message.current_target
480
481
    refunded_accounts = evm.accounts_to_delete
482
    parent_evm = evm.message.parent_evm
483
    while parent_evm is not None:
484
        refunded_accounts.update(parent_evm.accounts_to_delete)
485
        parent_evm = parent_evm.message.parent_evm
486
487
    if originator not in refunded_accounts:
488
        evm.refund_counter += REFUND_SELF_DESTRUCT
489
490
    charge_gas(evm, gas_cost)
491
    if evm.message.is_static:
492
        raise WriteInStaticContext
493
494
    originator = evm.message.current_target
495
    beneficiary_balance = get_account(
496
        evm.message.block_env.state, beneficiary
497
    ).balance
498
    originator_balance = get_account(
499
        evm.message.block_env.state, originator
500
    ).balance
501
502
    # First Transfer to beneficiary
503
    set_account_balance(
504
        evm.message.block_env.state,
505
        beneficiary,
506
        beneficiary_balance + originator_balance,
507
    )
508
    # Next, Zero the balance of the address being deleted (must come after
509
    # sending to beneficiary in case the contract named itself as the
510
    # beneficiary).
511
    set_account_balance(evm.message.block_env.state, originator, U256(0))
512
513
    # register account for deletion
514
    evm.accounts_to_delete.add(originator)
515
516
    # mark beneficiary as touched
517
    if account_exists_and_is_empty(evm.message.block_env.state, beneficiary):
518
        evm.touched_accounts.add(beneficiary)
519
520
    # HALT the execution
521
    evm.running = False
522
523
    # PROGRAM COUNTER
524
    pass

delegatecall

Message-call into an account.

Parameters

evm : The current EVM frame.

def delegatecall(evm: Evm) -> None:
528
    """
529
    Message-call into an account.
530
531
    Parameters
532
    ----------
533
    evm :
534
        The current EVM frame.
535
    """
536
    # STACK
537
    gas = Uint(pop(evm.stack))
538
    code_address = to_address_masked(pop(evm.stack))
539
    memory_input_start_position = pop(evm.stack)
540
    memory_input_size = pop(evm.stack)
541
    memory_output_start_position = pop(evm.stack)
542
    memory_output_size = pop(evm.stack)
543
544
    # GAS
545
    extend_memory = calculate_gas_extend_memory(
546
        evm.memory,
547
        [
548
            (memory_input_start_position, memory_input_size),
549
            (memory_output_start_position, memory_output_size),
550
        ],
551
    )
552
    message_call_gas = calculate_message_call_gas(
553
        U256(0), gas, Uint(evm.gas_left), extend_memory.cost, GAS_CALL
554
    )
555
    charge_gas(evm, message_call_gas.cost + extend_memory.cost)
556
557
    # OPERATION
558
    evm.memory += b"\x00" * extend_memory.expand_by
559
    generic_call(
560
        evm,
561
        message_call_gas.sub_call,
562
        evm.message.value,
563
        evm.message.caller,
564
        evm.message.current_target,
565
        code_address,
566
        False,
567
        False,
568
        memory_input_start_position,
569
        memory_input_size,
570
        memory_output_start_position,
571
        memory_output_size,
572
    )
573
574
    # PROGRAM COUNTER
575
    evm.pc += Uint(1)

staticcall

Message-call into an account.

Parameters

evm : The current EVM frame.

def staticcall(evm: Evm) -> None:
579
    """
580
    Message-call into an account.
581
582
    Parameters
583
    ----------
584
    evm :
585
        The current EVM frame.
586
    """
587
    # STACK
588
    gas = Uint(pop(evm.stack))
589
    to = to_address_masked(pop(evm.stack))
590
    memory_input_start_position = pop(evm.stack)
591
    memory_input_size = pop(evm.stack)
592
    memory_output_start_position = pop(evm.stack)
593
    memory_output_size = pop(evm.stack)
594
595
    # GAS
596
    extend_memory = calculate_gas_extend_memory(
597
        evm.memory,
598
        [
599
            (memory_input_start_position, memory_input_size),
600
            (memory_output_start_position, memory_output_size),
601
        ],
602
    )
603
604
    code_address = to
605
606
    message_call_gas = calculate_message_call_gas(
607
        U256(0),
608
        gas,
609
        Uint(evm.gas_left),
610
        extend_memory.cost,
611
        GAS_CALL,
612
    )
613
    charge_gas(evm, message_call_gas.cost + extend_memory.cost)
614
615
    # OPERATION
616
    evm.memory += b"\x00" * extend_memory.expand_by
617
    generic_call(
618
        evm,
619
        message_call_gas.sub_call,
620
        U256(0),
621
        evm.message.current_target,
622
        to,
623
        code_address,
624
        True,
625
        True,
626
        memory_input_start_position,
627
        memory_input_size,
628
        memory_output_start_position,
629
        memory_output_size,
630
    )
631
632
    # PROGRAM COUNTER
633
    evm.pc += Uint(1)

revert

Stop execution and revert state changes, without consuming all provided gas and also has the ability to return a reason Parameters

evm : The current EVM frame.

def revert(evm: Evm) -> None:
637
    """
638
    Stop execution and revert state changes, without consuming all provided gas
639
    and also has the ability to return a reason
640
    Parameters
641
    ----------
642
    evm :
643
        The current EVM frame.
644
    """
645
    # STACK
646
    memory_start_index = pop(evm.stack)
647
    size = pop(evm.stack)
648
649
    # GAS
650
    extend_memory = calculate_gas_extend_memory(
651
        evm.memory, [(memory_start_index, size)]
652
    )
653
654
    charge_gas(evm, extend_memory.cost)
655
656
    # OPERATION
657
    evm.memory += b"\x00" * extend_memory.expand_by
658
    output = memory_read_bytes(evm.memory, memory_start_index, size)
659
    evm.output = Bytes(output)
660
    raise Revert