ethereum.forks.london.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
    account = get_account(evm.message.tx_env.state, params.code_address)
288
    code = get_code(evm.message.tx_env.state, 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
    charge_gas(evm, gas_cost)
514
    if evm.message.is_static:
515
        raise WriteInStaticContext
516
517
    originator = evm.message.current_target
518
    beneficiary_balance = get_account(
519
        evm.message.tx_env.state, beneficiary
520
    ).balance
521
    originator_balance = get_account(
522
        evm.message.tx_env.state, originator
523
    ).balance
524
525
    # First Transfer to beneficiary
526
    set_account_balance(
527
        evm.message.tx_env.state,
528
        beneficiary,
529
        beneficiary_balance + originator_balance,
530
    )
531
    # Next, Zero the balance of the address being deleted (must come after
532
    # sending to beneficiary in case the contract named itself as the
533
    # beneficiary).
534
    set_account_balance(evm.message.tx_env.state, originator, U256(0))
535
536
    # register account for deletion
537
    evm.accounts_to_delete.add(originator)
538
539
    # mark beneficiary as touched
540
    if account_exists_and_is_empty(evm.message.tx_env.state, beneficiary):
541
        evm.touched_accounts.add(beneficiary)
542
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