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

return_

Halts execution returning output data.

Parameters

evm : The current EVM frame.

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

GenericCall

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

256
@final
257
@dataclass
class GenericCall:

gas

263
    gas: Uint

value

264
    value: U256

caller

265
    caller: Address

to

266
    to: Address

code_address

267
    code_address: Address

should_transfer_value

268
    should_transfer_value: bool

is_staticcall

269
    is_staticcall: bool

memory_input_start_position

270
    memory_input_start_position: U256

memory_input_size

271
    memory_input_size: U256

memory_output_start_position

272
    memory_output_start_position: U256

memory_output_size

273
    memory_output_size: U256

generic_call

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

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

call

Message-call into an account.

Parameters

evm : The current EVM frame.

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

selfdestruct

Halt execution and register account for later deletion.

Parameters

evm : The current EVM frame.

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

delegatecall

Message-call into an account.

Parameters

evm : The current EVM frame.

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

staticcall

Message-call into an account.

Parameters

evm : The current EVM frame.

def staticcall(​evm: Evm​) -> None:
621
    <snip>
630
    # STACK
631
    gas = Uint(pop(evm.stack))
632
    to = to_address_masked(pop(evm.stack))
633
    memory_input_start_position = pop(evm.stack)
634
    memory_input_size = pop(evm.stack)
635
    memory_output_start_position = pop(evm.stack)
636
    memory_output_size = pop(evm.stack)
637
638
    # GAS
639
    extend_memory = calculate_gas_extend_memory(
640
        evm.memory,
641
        [
642
            (memory_input_start_position, memory_input_size),
643
            (memory_output_start_position, memory_output_size),
644
        ],
645
    )
646
647
    if to in evm.accessed_addresses:
648
        access_gas_cost = GasCosts.WARM_ACCESS
649
    else:
650
        evm.accessed_addresses.add(to)
651
        access_gas_cost = GasCosts.COLD_ACCOUNT_ACCESS
652
653
    code_address = to
654
655
    message_call_gas = calculate_message_call_gas(
656
        U256(0),
657
        gas,
658
        Uint(evm.gas_left),
659
        extend_memory.cost,
660
        access_gas_cost,
661
    )
662
    charge_gas(evm, message_call_gas.cost + extend_memory.cost)
663
664
    # OPERATION
665
    evm.memory += b"\x00" * extend_memory.expand_by
666
    generic_call(
667
        evm,
668
        GenericCall(
669
            gas=message_call_gas.sub_call,
670
            value=U256(0),
671
            caller=evm.message.current_target,
672
            to=to,
673
            code_address=code_address,
674
            should_transfer_value=True,
675
            is_staticcall=True,
676
            memory_input_start_position=memory_input_start_position,
677
            memory_input_size=memory_input_size,
678
            memory_output_start_position=memory_output_start_position,
679
            memory_output_size=memory_output_size,
680
        ),
681
    )
682
683
    # PROGRAM COUNTER
684
    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:
688
    <snip>
698
    # STACK
699
    memory_start_index = pop(evm.stack)
700
    size = pop(evm.stack)
701
702
    # GAS
703
    extend_memory = calculate_gas_extend_memory(
704
        evm.memory, [(memory_start_index, size)]
705
    )
706
707
    charge_gas(evm, extend_memory.cost)
708
709
    # OPERATION
710
    evm.memory += b"\x00" * extend_memory.expand_by
711
    output = memory_read_bytes(evm.memory, memory_start_index, size)
712
    evm.output = Bytes(output)
713
    raise Revert
714
715
    # PROGRAM COUNTER
716
    # no-op