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,
163
        endowment,
164
        contract_address,
165
        memory_start_position,
166
        memory_size,
167
    )
168
169
    # PROGRAM COUNTER
170
    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:
174
    <snip>
186
    # STACK
187
    endowment = pop(evm.stack)
188
    memory_start_position = pop(evm.stack)
189
    memory_size = pop(evm.stack)
190
    salt = pop(evm.stack).to_be_bytes32()
191
192
    # GAS
193
    extend_memory = calculate_gas_extend_memory(
194
        evm.memory, [(memory_start_position, memory_size)]
195
    )
196
    call_data_words = ceil32(Uint(memory_size)) // Uint(32)
197
    charge_gas(
198
        evm,
199
        GasCosts.OPCODE_CREATE_BASE
200
        + GasCosts.OPCODE_KECCAK256_PER_WORD * call_data_words
201
        + extend_memory.cost,
202
    )
203
204
    # OPERATION
205
    evm.memory += b"\x00" * extend_memory.expand_by
206
    contract_address = compute_create2_contract_address(
207
        evm.message.current_target,
208
        salt,
209
        memory_read_bytes(evm.memory, memory_start_position, memory_size),
210
    )
211
212
    generic_create(
213
        evm,
214
        endowment,
215
        contract_address,
216
        memory_start_position,
217
        memory_size,
218
    )
219
220
    # PROGRAM COUNTER
221
    evm.pc += Uint(1)

return_

Halts execution returning output data.

Parameters

evm : The current EVM frame.

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

GenericCall

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

257
@final
258
@dataclass
class GenericCall:

gas

264
    gas: Uint

value

265
    value: U256

caller

266
    caller: Address

to

267
    to: Address

code_address

268
    code_address: Address

should_transfer_value

269
    should_transfer_value: bool

is_staticcall

270
    is_staticcall: bool

memory_input_start_position

271
    memory_input_start_position: U256

memory_input_size

272
    memory_input_size: U256

memory_output_start_position

273
    memory_output_start_position: U256

memory_output_size

274
    memory_output_size: U256

generic_call

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

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

call

Message-call into an account.

Parameters

evm : The current EVM frame.

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

selfdestruct

Halt execution and register account for later deletion.

Parameters

evm : The current EVM frame.

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

delegatecall

Message-call into an account.

Parameters

evm : The current EVM frame.

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

staticcall

Message-call into an account.

Parameters

evm : The current EVM frame.

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