ethereum.forks.berlin.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 not account_deployable(evm.message.tx_env.state, contract_address):
94
        increment_nonce(evm.message.tx_env.state, evm.message.current_target)
95
        push(evm.stack, U256(0))
96
        return
97
98
    increment_nonce(evm.message.tx_env.state, evm.message.current_target)
99
100
    child_message = Message(
101
        block_env=evm.message.block_env,
102
        tx_env=evm.message.tx_env,
103
        caller=evm.message.current_target,
104
        target=Bytes0(),
105
        gas=create_message_gas,
106
        value=endowment,
107
        data=b"",
108
        code=call_data,
109
        current_target=contract_address,
110
        depth=evm.message.depth + Uint(1),
111
        code_address=None,
112
        should_transfer_value=True,
113
        is_static=False,
114
        accessed_addresses=evm.accessed_addresses.copy(),
115
        accessed_storage_keys=evm.accessed_storage_keys.copy(),
116
        parent_evm=evm,
117
    )
118
    child_evm = process_create_message(child_message)
119
120
    if child_evm.error:
121
        incorporate_child_on_error(evm, child_evm)
122
        evm.return_data = child_evm.output
123
        push(evm.stack, U256(0))
124
    else:
125
        incorporate_child_on_success(evm, child_evm)
126
        evm.return_data = b""
127
        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:
131
    <snip>
140
    # STACK
141
    endowment = pop(evm.stack)
142
    memory_start_position = pop(evm.stack)
143
    memory_size = pop(evm.stack)
144
145
    # GAS
146
    extend_memory = calculate_gas_extend_memory(
147
        evm.memory, [(memory_start_position, memory_size)]
148
    )
149
150
    charge_gas(evm, GasCosts.OPCODE_CREATE_BASE + extend_memory.cost)
151
152
    # OPERATION
153
    evm.memory += b"\x00" * extend_memory.expand_by
154
    contract_address = compute_contract_address(
155
        evm.message.current_target,
156
        get_account(
157
            evm.message.tx_env.state, evm.message.current_target
158
        ).nonce,
159
    )
160
161
    generic_create(
162
        evm, endowment, contract_address, memory_start_position, memory_size
163
    )
164
165
    # PROGRAM COUNTER
166
    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:
170
    <snip>
182
    # STACK
183
    endowment = pop(evm.stack)
184
    memory_start_position = pop(evm.stack)
185
    memory_size = pop(evm.stack)
186
    salt = pop(evm.stack).to_be_bytes32()
187
188
    # GAS
189
    extend_memory = calculate_gas_extend_memory(
190
        evm.memory, [(memory_start_position, memory_size)]
191
    )
192
    call_data_words = ceil32(Uint(memory_size)) // Uint(32)
193
    charge_gas(
194
        evm,
195
        GasCosts.OPCODE_CREATE_BASE
196
        + GasCosts.OPCODE_KECCAK256_PER_WORD * call_data_words
197
        + extend_memory.cost,
198
    )
199
200
    # OPERATION
201
    evm.memory += b"\x00" * extend_memory.expand_by
202
    contract_address = compute_create2_contract_address(
203
        evm.message.current_target,
204
        salt,
205
        memory_read_bytes(evm.memory, memory_start_position, memory_size),
206
    )
207
208
    generic_create(
209
        evm, endowment, contract_address, memory_start_position, memory_size
210
    )
211
212
    # PROGRAM COUNTER
213
    evm.pc += Uint(1)

return_

Halts execution returning output data.

Parameters

evm : The current EVM frame.

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

GenericCall

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

249
@final
250
@dataclass
class GenericCall:

gas

256
    gas: Uint

value

257
    value: U256

caller

258
    caller: Address

to

259
    to: Address

code_address

260
    code_address: Address

should_transfer_value

261
    should_transfer_value: bool

is_staticcall

262
    is_staticcall: bool

memory_input_start_position

263
    memory_input_start_position: U256

memory_input_size

264
    memory_input_size: U256

memory_output_start_position

265
    memory_output_start_position: U256

memory_output_size

266
    memory_output_size: U256

generic_call

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

def generic_call(evm: Evm, ​​params: GenericCall) -> None:
270
    <snip>
273
    from ...vm.interpreter import STACK_DEPTH_LIMIT, process_message
274
275
    evm.return_data = b""
276
277
    if evm.message.depth + Uint(1) > STACK_DEPTH_LIMIT:
278
        evm.gas_left += params.gas
279
        push(evm.stack, U256(0))
280
        return
281
282
    call_data = memory_read_bytes(
283
        evm.memory,
284
        params.memory_input_start_position,
285
        params.memory_input_size,
286
    )
287
    _code_account = get_account(evm.message.tx_env.state, params.code_address)
288
    code = get_code(evm.message.tx_env.state, _code_account.code_hash)
289
    child_message = Message(
290
        block_env=evm.message.block_env,
291
        tx_env=evm.message.tx_env,
292
        caller=params.caller,
293
        target=params.to,
294
        gas=params.gas,
295
        value=params.value,
296
        data=call_data,
297
        code=code,
298
        current_target=params.to,
299
        depth=evm.message.depth + Uint(1),
300
        code_address=params.code_address,
301
        should_transfer_value=params.should_transfer_value,
302
        is_static=params.is_staticcall or 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(
319
        params.memory_output_size, U256(len(child_evm.output))
320
    )
321
    memory_write(
322
        evm.memory,
323
        params.memory_output_start_position,
324
        child_evm.output[:actual_output_size],
325
    )

call

Message-call into an account.

Parameters

evm : The current EVM frame.

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

selfdestruct

Halt execution and register account for later deletion.

Parameters

evm : The current EVM frame.

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

delegatecall

Message-call into an account.

Parameters

evm : The current EVM frame.

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

staticcall

Message-call into an account.

Parameters

evm : The current EVM frame.

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