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:
62
    <snip>
65
    # This import causes a circular import error
66
    # if it's not moved inside this method
67
    from ...vm.interpreter import STACK_DEPTH_LIMIT, process_create_message
68
69
    call_data = memory_read_bytes(
70
        evm.memory, memory_start_position, memory_size
71
    )
72
73
    create_message_gas = max_message_call_gas(Uint(evm.gas_left))
74
    evm.gas_left -= create_message_gas
75
    if evm.message.is_static:
76
        raise WriteInStaticContext
77
    evm.return_data = b""
78
79
    sender_address = evm.message.current_target
80
    sender = get_account(evm.message.tx_env.state, sender_address)
81
82
    if (
83
        sender.balance < endowment
84
        or sender.nonce == Uint(2**64 - 1)
85
        or evm.message.depth + Uint(1) > STACK_DEPTH_LIMIT
86
    ):
87
        evm.gas_left += create_message_gas
88
        push(evm.stack, U256(0))
89
        return
90
91
    evm.accessed_addresses.add(contract_address)
92
93
    if account_has_code_or_nonce(
94
        evm.message.tx_env.state, contract_address
95
    ) or account_has_storage(evm.message.tx_env.state, contract_address):
96
        increment_nonce(evm.message.tx_env.state, evm.message.current_target)
97
        push(evm.stack, U256(0))
98
        return
99
100
    increment_nonce(evm.message.tx_env.state, evm.message.current_target)
101
102
    child_message = Message(
103
        block_env=evm.message.block_env,
104
        tx_env=evm.message.tx_env,
105
        caller=evm.message.current_target,
106
        target=Bytes0(),
107
        gas=create_message_gas,
108
        value=endowment,
109
        data=b"",
110
        code=call_data,
111
        current_target=contract_address,
112
        depth=evm.message.depth + Uint(1),
113
        code_address=None,
114
        should_transfer_value=True,
115
        is_static=False,
116
        accessed_addresses=evm.accessed_addresses.copy(),
117
        accessed_storage_keys=evm.accessed_storage_keys.copy(),
118
        parent_evm=evm,
119
    )
120
    child_evm = process_create_message(child_message)
121
122
    if child_evm.error:
123
        incorporate_child_on_error(evm, child_evm)
124
        evm.return_data = child_evm.output
125
        push(evm.stack, U256(0))
126
    else:
127
        incorporate_child_on_success(evm, child_evm)
128
        evm.return_data = b""
129
        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:
133
    <snip>
142
    # STACK
143
    endowment = pop(evm.stack)
144
    memory_start_position = pop(evm.stack)
145
    memory_size = pop(evm.stack)
146
147
    # GAS
148
    extend_memory = calculate_gas_extend_memory(
149
        evm.memory, [(memory_start_position, memory_size)]
150
    )
151
152
    charge_gas(evm, GasCosts.OPCODE_CREATE_BASE + extend_memory.cost)
153
154
    # OPERATION
155
    evm.memory += b"\x00" * extend_memory.expand_by
156
    contract_address = compute_contract_address(
157
        evm.message.current_target,
158
        get_account(
159
            evm.message.tx_env.state, evm.message.current_target
160
        ).nonce,
161
    )
162
163
    generic_create(
164
        evm, endowment, contract_address, memory_start_position, memory_size
165
    )
166
167
    # PROGRAM COUNTER
168
    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:
172
    <snip>
184
    # STACK
185
    endowment = pop(evm.stack)
186
    memory_start_position = pop(evm.stack)
187
    memory_size = pop(evm.stack)
188
    salt = pop(evm.stack).to_be_bytes32()
189
190
    # GAS
191
    extend_memory = calculate_gas_extend_memory(
192
        evm.memory, [(memory_start_position, memory_size)]
193
    )
194
    call_data_words = ceil32(Uint(memory_size)) // Uint(32)
195
    charge_gas(
196
        evm,
197
        GasCosts.OPCODE_CREATE_BASE
198
        + GasCosts.OPCODE_KECCAK256_PER_WORD * call_data_words
199
        + extend_memory.cost,
200
    )
201
202
    # OPERATION
203
    evm.memory += b"\x00" * extend_memory.expand_by
204
    contract_address = compute_create2_contract_address(
205
        evm.message.current_target,
206
        salt,
207
        memory_read_bytes(evm.memory, memory_start_position, memory_size),
208
    )
209
210
    generic_create(
211
        evm, endowment, contract_address, memory_start_position, memory_size
212
    )
213
214
    # PROGRAM COUNTER
215
    evm.pc += Uint(1)

return_

Halts execution returning output data.

Parameters

evm : The current EVM frame.

def return_(evm: Evm) -> None:
219
    <snip>
228
    # STACK
229
    memory_start_position = pop(evm.stack)
230
    memory_size = pop(evm.stack)
231
232
    # GAS
233
    extend_memory = calculate_gas_extend_memory(
234
        evm.memory, [(memory_start_position, memory_size)]
235
    )
236
237
    charge_gas(evm, GasCosts.ZERO + extend_memory.cost)
238
239
    # OPERATION
240
    evm.memory += b"\x00" * extend_memory.expand_by
241
    evm.output = memory_read_bytes(
242
        evm.memory, memory_start_position, memory_size
243
    )
244
245
    evm.running = False
246
247
    # PROGRAM COUNTER
248
    pass

GenericCall

Parameters for the core logic of the CALL* family of opcodes.

251
@final
252
@dataclass
class GenericCall:

gas

258
    gas: Uint

value

259
    value: U256

caller

260
    caller: Address

to

261
    to: Address

code_address

262
    code_address: Address

should_transfer_value

263
    should_transfer_value: bool

is_staticcall

264
    is_staticcall: bool

memory_input_start_position

265
    memory_input_start_position: U256

memory_input_size

266
    memory_input_size: U256

memory_output_start_position

267
    memory_output_start_position: U256

memory_output_size

268
    memory_output_size: U256

generic_call

Perform the core logic of the CALL* family of opcodes.

def generic_call(evm: Evm, ​​params: GenericCall) -> None:
272
    <snip>
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 += params.gas
281
        push(evm.stack, U256(0))
282
        return
283
284
    call_data = memory_read_bytes(
285
        evm.memory,
286
        params.memory_input_start_position,
287
        params.memory_input_size,
288
    )
290
    account = get_account(evm.message.tx_env.state, params.code_address)
291
    code = get_code(evm.message.tx_env.state, account.code_hash)
289
    tx_state = evm.message.tx_env.state
290
    code = get_code(
291
        tx_state, get_account(tx_state, params.code_address).code_hash
292
    )
293
    child_message = Message(
294
        block_env=evm.message.block_env,
295
        tx_env=evm.message.tx_env,
296
        caller=params.caller,
297
        target=params.to,
298
        gas=params.gas,
299
        value=params.value,
300
        data=call_data,
301
        code=code,
302
        current_target=params.to,
303
        depth=evm.message.depth + Uint(1),
304
        code_address=params.code_address,
305
        should_transfer_value=params.should_transfer_value,
306
        is_static=params.is_staticcall or evm.message.is_static,
307
        accessed_addresses=evm.accessed_addresses.copy(),
308
        accessed_storage_keys=evm.accessed_storage_keys.copy(),
309
        parent_evm=evm,
310
    )
311
    child_evm = process_message(child_message)
312
313
    if child_evm.error:
314
        incorporate_child_on_error(evm, child_evm)
315
        evm.return_data = child_evm.output
316
        push(evm.stack, U256(0))
317
    else:
318
        incorporate_child_on_success(evm, child_evm)
319
        evm.return_data = child_evm.output
320
        push(evm.stack, U256(1))
321
322
    actual_output_size = min(
323
        params.memory_output_size, U256(len(child_evm.output))
324
    )
325
    memory_write(
326
        evm.memory,
327
        params.memory_output_start_position,
328
        child_evm.output[:actual_output_size],
329
    )

call

Message-call into an account.

Parameters

evm : The current EVM frame.

def call(evm: Evm) -> None:
333
    <snip>
342
    # STACK
343
    gas = Uint(pop(evm.stack))
344
    to = to_address_masked(pop(evm.stack))
345
    value = pop(evm.stack)
346
    memory_input_start_position = pop(evm.stack)
347
    memory_input_size = pop(evm.stack)
348
    memory_output_start_position = pop(evm.stack)
349
    memory_output_size = pop(evm.stack)
350
351
    # GAS
352
    extend_memory = calculate_gas_extend_memory(
353
        evm.memory,
354
        [
355
            (memory_input_start_position, memory_input_size),
356
            (memory_output_start_position, memory_output_size),
357
        ],
358
    )
359
360
    if to in evm.accessed_addresses:
361
        access_gas_cost = GasCosts.WARM_ACCESS
362
    else:
363
        evm.accessed_addresses.add(to)
364
        access_gas_cost = GasCosts.COLD_ACCOUNT_ACCESS
365
366
    code_address = to
367
368
    create_gas_cost = GasCosts.NEW_ACCOUNT
369
    if value == 0 or is_account_alive(evm.message.tx_env.state, to):
370
        create_gas_cost = Uint(0)
371
    transfer_gas_cost = Uint(0) if value == 0 else GasCosts.CALL_VALUE
372
    message_call_gas = calculate_message_call_gas(
373
        value,
374
        gas,
375
        Uint(evm.gas_left),
376
        memory_cost=extend_memory.cost,
377
        extra_gas=access_gas_cost + create_gas_cost + transfer_gas_cost,
378
    )
379
    charge_gas(evm, message_call_gas.cost + extend_memory.cost)
380
    if evm.message.is_static and value != U256(0):
381
        raise WriteInStaticContext
382
    evm.memory += b"\x00" * extend_memory.expand_by
383
    sender_balance = get_account(
384
        evm.message.tx_env.state, evm.message.current_target
385
    ).balance
386
    if sender_balance < value:
387
        push(evm.stack, U256(0))
388
        evm.return_data = b""
389
        evm.gas_left += message_call_gas.sub_call
390
    else:
391
        generic_call(
392
            evm,
393
            GenericCall(
394
                gas=message_call_gas.sub_call,
395
                value=value,
396
                caller=evm.message.current_target,
397
                to=to,
398
                code_address=code_address,
399
                should_transfer_value=True,
400
                is_staticcall=False,
401
                memory_input_start_position=memory_input_start_position,
402
                memory_input_size=memory_input_size,
403
                memory_output_start_position=memory_output_start_position,
404
                memory_output_size=memory_output_size,
405
            ),
406
        )
407
408
    # PROGRAM COUNTER
409
    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:
413
    <snip>
422
    # STACK
423
    gas = Uint(pop(evm.stack))
424
    code_address = to_address_masked(pop(evm.stack))
425
    value = pop(evm.stack)
426
    memory_input_start_position = pop(evm.stack)
427
    memory_input_size = pop(evm.stack)
428
    memory_output_start_position = pop(evm.stack)
429
    memory_output_size = pop(evm.stack)
430
431
    # GAS
432
    to = evm.message.current_target
433
434
    extend_memory = calculate_gas_extend_memory(
435
        evm.memory,
436
        [
437
            (memory_input_start_position, memory_input_size),
438
            (memory_output_start_position, memory_output_size),
439
        ],
440
    )
441
442
    if code_address in evm.accessed_addresses:
443
        access_gas_cost = GasCosts.WARM_ACCESS
444
    else:
445
        evm.accessed_addresses.add(code_address)
446
        access_gas_cost = GasCosts.COLD_ACCOUNT_ACCESS
447
448
    transfer_gas_cost = Uint(0) if value == 0 else GasCosts.CALL_VALUE
449
    message_call_gas = calculate_message_call_gas(
450
        value,
451
        gas,
452
        Uint(evm.gas_left),
453
        extend_memory.cost,
454
        access_gas_cost + transfer_gas_cost,
455
    )
456
    charge_gas(evm, message_call_gas.cost + extend_memory.cost)
457
458
    # OPERATION
459
    evm.memory += b"\x00" * extend_memory.expand_by
460
    sender_balance = get_account(
461
        evm.message.tx_env.state, evm.message.current_target
462
    ).balance
463
    if sender_balance < value:
464
        push(evm.stack, U256(0))
465
        evm.return_data = b""
466
        evm.gas_left += message_call_gas.sub_call
467
    else:
468
        generic_call(
469
            evm,
470
            GenericCall(
471
                gas=message_call_gas.sub_call,
472
                value=value,
473
                caller=evm.message.current_target,
474
                to=to,
475
                code_address=code_address,
476
                should_transfer_value=True,
477
                is_staticcall=False,
478
                memory_input_start_position=memory_input_start_position,
479
                memory_input_size=memory_input_size,
480
                memory_output_start_position=memory_output_start_position,
481
                memory_output_size=memory_output_size,
482
            ),
483
        )
484
485
    # PROGRAM COUNTER
486
    evm.pc += Uint(1)

selfdestruct

Halt execution and register account for later deletion.

Parameters

evm : The current EVM frame.

def selfdestruct(evm: Evm) -> None:
490
    <snip>
499
    # STACK
500
    beneficiary = to_address_masked(pop(evm.stack))
501
502
    # GAS
503
    gas_cost = GasCosts.OPCODE_SELFDESTRUCT_BASE
504
    if beneficiary not in evm.accessed_addresses:
505
        evm.accessed_addresses.add(beneficiary)
506
        gas_cost += GasCosts.COLD_ACCOUNT_ACCESS
507
508
    if (
509
        not is_account_alive(evm.message.tx_env.state, beneficiary)
510
        and get_account(
511
            evm.message.tx_env.state, evm.message.current_target
512
        ).balance
513
        != 0
514
    ):
515
        gas_cost += GasCosts.OPCODE_SELFDESTRUCT_NEW_ACCOUNT
516
517
    charge_gas(evm, gas_cost)
518
    if evm.message.is_static:
519
        raise WriteInStaticContext
520
521
    originator = evm.message.current_target
522
    beneficiary_balance = get_account(
523
        evm.message.tx_env.state, beneficiary
524
    ).balance
525
    originator_balance = get_account(
526
        evm.message.tx_env.state, originator
527
    ).balance
528
529
    # First Transfer to beneficiary
530
    set_account_balance(
531
        evm.message.tx_env.state,
532
        beneficiary,
533
        beneficiary_balance + originator_balance,
534
    )
535
    # Next, Zero the balance of the address being deleted (must come after
536
    # sending to beneficiary in case the contract named itself as the
537
    # beneficiary).
538
    set_account_balance(evm.message.tx_env.state, originator, U256(0))
539
540
    # register account for deletion
541
    evm.accounts_to_delete.add(originator)
542
542
    # mark beneficiary as touched
543
    if account_exists_and_is_empty(evm.message.tx_env.state, beneficiary):
544
        evm.touched_accounts.add(beneficiary)
545
543
    # HALT the execution
544
    evm.running = False
545
546
    # PROGRAM COUNTER
547
    pass

delegatecall

Message-call into an account.

Parameters

evm : The current EVM frame.

def delegatecall(evm: Evm) -> None:
551
    <snip>
560
    # STACK
561
    gas = Uint(pop(evm.stack))
562
    code_address = to_address_masked(pop(evm.stack))
563
    memory_input_start_position = pop(evm.stack)
564
    memory_input_size = pop(evm.stack)
565
    memory_output_start_position = pop(evm.stack)
566
    memory_output_size = pop(evm.stack)
567
568
    # GAS
569
    extend_memory = calculate_gas_extend_memory(
570
        evm.memory,
571
        [
572
            (memory_input_start_position, memory_input_size),
573
            (memory_output_start_position, memory_output_size),
574
        ],
575
    )
576
577
    if code_address in evm.accessed_addresses:
578
        access_gas_cost = GasCosts.WARM_ACCESS
579
    else:
580
        evm.accessed_addresses.add(code_address)
581
        access_gas_cost = GasCosts.COLD_ACCOUNT_ACCESS
582
583
    message_call_gas = calculate_message_call_gas(
584
        U256(0), gas, Uint(evm.gas_left), extend_memory.cost, access_gas_cost
585
    )
586
    charge_gas(evm, message_call_gas.cost + extend_memory.cost)
587
588
    # OPERATION
589
    evm.memory += b"\x00" * extend_memory.expand_by
590
    generic_call(
591
        evm,
592
        GenericCall(
593
            gas=message_call_gas.sub_call,
594
            value=evm.message.value,
595
            caller=evm.message.caller,
596
            to=evm.message.current_target,
597
            code_address=code_address,
598
            should_transfer_value=False,
599
            is_staticcall=False,
600
            memory_input_start_position=memory_input_start_position,
601
            memory_input_size=memory_input_size,
602
            memory_output_start_position=memory_output_start_position,
603
            memory_output_size=memory_output_size,
604
        ),
605
    )
606
607
    # PROGRAM COUNTER
608
    evm.pc += Uint(1)

staticcall

Message-call into an account.

Parameters

evm : The current EVM frame.

def staticcall(evm: Evm) -> None:
612
    <snip>
621
    # STACK
622
    gas = Uint(pop(evm.stack))
623
    to = to_address_masked(pop(evm.stack))
624
    memory_input_start_position = pop(evm.stack)
625
    memory_input_size = pop(evm.stack)
626
    memory_output_start_position = pop(evm.stack)
627
    memory_output_size = pop(evm.stack)
628
629
    # GAS
630
    extend_memory = calculate_gas_extend_memory(
631
        evm.memory,
632
        [
633
            (memory_input_start_position, memory_input_size),
634
            (memory_output_start_position, memory_output_size),
635
        ],
636
    )
637
638
    if to in evm.accessed_addresses:
639
        access_gas_cost = GasCosts.WARM_ACCESS
640
    else:
641
        evm.accessed_addresses.add(to)
642
        access_gas_cost = GasCosts.COLD_ACCOUNT_ACCESS
643
644
    code_address = to
645
646
    message_call_gas = calculate_message_call_gas(
647
        U256(0),
648
        gas,
649
        Uint(evm.gas_left),
650
        extend_memory.cost,
651
        access_gas_cost,
652
    )
653
    charge_gas(evm, message_call_gas.cost + extend_memory.cost)
654
655
    # OPERATION
656
    evm.memory += b"\x00" * extend_memory.expand_by
657
    generic_call(
658
        evm,
659
        GenericCall(
660
            gas=message_call_gas.sub_call,
661
            value=U256(0),
662
            caller=evm.message.current_target,
663
            to=to,
664
            code_address=code_address,
665
            should_transfer_value=True,
666
            is_staticcall=True,
667
            memory_input_start_position=memory_input_start_position,
668
            memory_input_size=memory_input_size,
669
            memory_output_start_position=memory_output_start_position,
670
            memory_output_size=memory_output_size,
671
        ),
672
    )
673
674
    # PROGRAM COUNTER
675
    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:
679
    <snip>
689
    # STACK
690
    memory_start_index = pop(evm.stack)
691
    size = pop(evm.stack)
692
693
    # GAS
694
    extend_memory = calculate_gas_extend_memory(
695
        evm.memory, [(memory_start_index, size)]
696
    )
697
698
    charge_gas(evm, extend_memory.cost)
699
700
    # OPERATION
701
    evm.memory += b"\x00" * extend_memory.expand_by
702
    output = memory_read_bytes(evm.memory, memory_start_index, size)
703
    evm.output = Bytes(output)
704
    raise Revert
705
706
    # PROGRAM COUNTER
707
    # no-op