ethereum.forks.gray_glacier.vm.instructions.systemethereum.forks.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
    """
149
    # STACK
150
    endowment = pop(evm.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, GAS_CREATE + extend_memory.cost)
160
161
    # OPERATION
162
    evm.memory += b"\x00" * extend_memory.expand_by
163
    contract_address = compute_contract_address(
164
        evm.message.current_target,
165
        get_account(
166
            evm.message.block_env.state, evm.message.current_target
167
        ).nonce,
168
    )
169
170
    generic_create(
171
        evm, endowment, contract_address, memory_start_position, memory_size
172
    )
173
174
    # PROGRAM COUNTER
175
    evm.pc += Uint(1)

create2

Creates a new account with associated code.

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

Parameters

evm : The current EVM frame.

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

return_

Halts execution returning output data.

Parameters

evm : The current EVM frame.

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

call

Message-call into an account.

Parameters

evm : The current EVM frame.

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

selfdestruct

Halt execution and register account for later deletion.

Parameters

evm : The current EVM frame.

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

delegatecall

Message-call into an account.

Parameters

evm : The current EVM frame.

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

staticcall

Message-call into an account.

Parameters

evm : The current EVM frame.

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