ethereum.gray_glacier.vm.instructions.systemethereum.paris.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
    evm.accessed_addresses.add(contract_address)
97
98
    if account_has_code_or_nonce(
99
        evm.message.block_env.state, contract_address
100
    ) or account_has_storage(evm.message.block_env.state, contract_address):
101
        increment_nonce(
102
            evm.message.block_env.state, evm.message.current_target
103
        )
104
        push(evm.stack, U256(0))
105
        return
106
107
    increment_nonce(evm.message.block_env.state, evm.message.current_target)
108
109
    child_message = Message(
110
        block_env=evm.message.block_env,
111
        tx_env=evm.message.tx_env,
112
        caller=evm.message.current_target,
113
        target=Bytes0(),
114
        gas=create_message_gas,
115
        value=endowment,
116
        data=b"",
117
        code=call_data,
118
        current_target=contract_address,
119
        depth=evm.message.depth + Uint(1),
120
        code_address=None,
121
        should_transfer_value=True,
122
        is_static=False,
123
        accessed_addresses=evm.accessed_addresses.copy(),
124
        accessed_storage_keys=evm.accessed_storage_keys.copy(),
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
        accessed_addresses=evm.accessed_addresses.copy(),
298
        accessed_storage_keys=evm.accessed_storage_keys.copy(),
299
        parent_evm=evm,
300
    )
301
    child_evm = process_message(child_message)
302
303
    if child_evm.error:
304
        incorporate_child_on_error(evm, child_evm)
305
        evm.return_data = child_evm.output
306
        push(evm.stack, U256(0))
307
    else:
308
        incorporate_child_on_success(evm, child_evm)
309
        evm.return_data = child_evm.output
310
        push(evm.stack, U256(1))
311
312
    actual_output_size = min(memory_output_size, U256(len(child_evm.output)))
313
    memory_write(
314
        evm.memory,
315
        memory_output_start_position,
316
        child_evm.output[:actual_output_size],
317
    )

call

Message-call into an account.

Parameters

evm : The current EVM frame.

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

selfdestruct

Halt execution and register account for later deletion.

Parameters

evm : The current EVM frame.

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

delegatecall

Message-call into an account.

Parameters

evm : The current EVM frame.

def delegatecall(evm: Evm) -> None:
534
    """
535
    Message-call into an account.
536
537
    Parameters
538
    ----------
539
    evm :
540
        The current EVM frame.
541
    """
542
    # STACK
543
    gas = Uint(pop(evm.stack))
544
    code_address = to_address(pop(evm.stack))
545
    memory_input_start_position = pop(evm.stack)
546
    memory_input_size = pop(evm.stack)
547
    memory_output_start_position = pop(evm.stack)
548
    memory_output_size = pop(evm.stack)
549
550
    # GAS
551
    extend_memory = calculate_gas_extend_memory(
552
        evm.memory,
553
        [
554
            (memory_input_start_position, memory_input_size),
555
            (memory_output_start_position, memory_output_size),
556
        ],
557
    )
558
559
    if code_address in evm.accessed_addresses:
560
        access_gas_cost = GAS_WARM_ACCESS
561
    else:
562
        evm.accessed_addresses.add(code_address)
563
        access_gas_cost = GAS_COLD_ACCOUNT_ACCESS
564
565
    message_call_gas = calculate_message_call_gas(
566
        U256(0), gas, Uint(evm.gas_left), extend_memory.cost, access_gas_cost
567
    )
568
    charge_gas(evm, message_call_gas.cost + extend_memory.cost)
569
570
    # OPERATION
571
    evm.memory += b"\x00" * extend_memory.expand_by
572
    generic_call(
573
        evm,
574
        message_call_gas.sub_call,
575
        evm.message.value,
576
        evm.message.caller,
577
        evm.message.current_target,
578
        code_address,
579
        False,
580
        False,
581
        memory_input_start_position,
582
        memory_input_size,
583
        memory_output_start_position,
584
        memory_output_size,
585
    )
586
587
    # PROGRAM COUNTER
588
    evm.pc += Uint(1)

staticcall

Message-call into an account.

Parameters

evm : The current EVM frame.

def staticcall(evm: Evm) -> None:
592
    """
593
    Message-call into an account.
594
595
    Parameters
596
    ----------
597
    evm :
598
        The current EVM frame.
599
    """
600
    # STACK
601
    gas = Uint(pop(evm.stack))
602
    to = to_address(pop(evm.stack))
603
    memory_input_start_position = pop(evm.stack)
604
    memory_input_size = pop(evm.stack)
605
    memory_output_start_position = pop(evm.stack)
606
    memory_output_size = pop(evm.stack)
607
608
    # GAS
609
    extend_memory = calculate_gas_extend_memory(
610
        evm.memory,
611
        [
612
            (memory_input_start_position, memory_input_size),
613
            (memory_output_start_position, memory_output_size),
614
        ],
615
    )
616
617
    if to in evm.accessed_addresses:
618
        access_gas_cost = GAS_WARM_ACCESS
619
    else:
620
        evm.accessed_addresses.add(to)
621
        access_gas_cost = GAS_COLD_ACCOUNT_ACCESS
622
623
    code_address = to
624
625
    message_call_gas = calculate_message_call_gas(
626
        U256(0),
627
        gas,
628
        Uint(evm.gas_left),
629
        extend_memory.cost,
630
        access_gas_cost,
631
    )
632
    charge_gas(evm, message_call_gas.cost + extend_memory.cost)
633
634
    # OPERATION
635
    evm.memory += b"\x00" * extend_memory.expand_by
636
    generic_call(
637
        evm,
638
        message_call_gas.sub_call,
639
        U256(0),
640
        evm.message.current_target,
641
        to,
642
        code_address,
643
        True,
644
        True,
645
        memory_input_start_position,
646
        memory_input_size,
647
        memory_output_start_position,
648
        memory_output_size,
649
    )
650
651
    # PROGRAM COUNTER
652
    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:
656
    """
657
    Stop execution and revert state changes, without consuming all provided gas
658
    and also has the ability to return a reason
659
    Parameters
660
    ----------
661
    evm :
662
        The current EVM frame.
663
    """
664
    # STACK
665
    memory_start_index = pop(evm.stack)
666
    size = pop(evm.stack)
667
668
    # GAS
669
    extend_memory = calculate_gas_extend_memory(
670
        evm.memory, [(memory_start_index, size)]
671
    )
672
673
    charge_gas(evm, extend_memory.cost)
674
675
    # OPERATION
676
    evm.memory += b"\x00" * extend_memory.expand_by
677
    output = memory_read_bytes(evm.memory, memory_start_index, size)
678
    evm.output = Bytes(output)
679
    raise Revert