ethereum.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:
67
    """
68
    Core logic used by the `CREATE*` family of opcodes.
69
    """
70
    # This import causes a circular import error
71
    # if it's not moved inside this method
72
    from ...vm.interpreter import STACK_DEPTH_LIMIT, process_create_message
73
74
    evm.accessed_addresses.add(contract_address)
75
76
    create_message_gas = max_message_call_gas(Uint(evm.gas_left))
77
    evm.gas_left -= create_message_gas
78
    if evm.message.is_static:
79
        raise WriteInStaticContext
80
    evm.return_data = b""
81
82
    sender_address = evm.message.current_target
83
    sender = get_account(evm.env.state, sender_address)
84
85
    if (
86
        sender.balance < endowment
87
        or sender.nonce == Uint(2**64 - 1)
88
        or evm.message.depth + Uint(1) > STACK_DEPTH_LIMIT
89
    ):
90
        evm.gas_left += create_message_gas
91
        push(evm.stack, U256(0))
92
        return
93
94
    if account_has_code_or_nonce(evm.env.state, contract_address):
95
        increment_nonce(evm.env.state, evm.message.current_target)
96
        push(evm.stack, U256(0))
97
        return
98
99
    call_data = memory_read_bytes(
100
        evm.memory, memory_start_position, memory_size
101
    )
102
103
    increment_nonce(evm.env.state, evm.message.current_target)
104
105
    child_message = Message(
106
        caller=evm.message.current_target,
107
        target=Bytes0(),
108
        gas=create_message_gas,
109
        value=endowment,
110
        data=b"",
111
        code=call_data,
112
        current_target=contract_address,
113
        depth=evm.message.depth + Uint(1),
114
        code_address=None,
115
        should_transfer_value=True,
116
        is_static=False,
117
        accessed_addresses=evm.accessed_addresses.copy(),
118
        accessed_storage_keys=evm.accessed_storage_keys.copy(),
119
        parent_evm=evm,
120
    )
121
    child_evm = process_create_message(child_message, evm.env)
122
123
    if child_evm.error:
124
        incorporate_child_on_error(evm, child_evm)
125
        evm.return_data = child_evm.output
126
        push(evm.stack, U256(0))
127
    else:
128
        incorporate_child_on_success(evm, child_evm)
129
        evm.return_data = b""
130
        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:
134
    """
135
    Creates a new account with associated code.
136
137
    Parameters
138
    ----------
139
    evm :
140
        The current EVM frame.
141
    """
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, GAS_CREATE + 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(evm.env.state, evm.message.current_target).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 CREATE opcode except that the address of 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
    """
171
    Creates a new account with associated code.
172
173
    It's similar to CREATE opcode except that the address of new account
174
    depends on the init_code instead of the nonce of sender.
175
176
    Parameters
177
    ----------
178
    evm :
179
        The current EVM frame.
180
    """
181
    # STACK
182
    endowment = pop(evm.stack)
183
    memory_start_position = pop(evm.stack)
184
    memory_size = pop(evm.stack)
185
    salt = pop(evm.stack).to_be_bytes32()
186
187
    # GAS
188
    extend_memory = calculate_gas_extend_memory(
189
        evm.memory, [(memory_start_position, memory_size)]
190
    )
191
    call_data_words = ceil32(Uint(memory_size)) // Uint(32)
192
    charge_gas(
193
        evm,
194
        GAS_CREATE + GAS_KECCAK256_WORD * call_data_words + extend_memory.cost,
195
    )
196
197
    # OPERATION
198
    evm.memory += b"\x00" * extend_memory.expand_by
199
    contract_address = compute_create2_contract_address(
200
        evm.message.current_target,
201
        salt,
202
        memory_read_bytes(evm.memory, memory_start_position, memory_size),
203
    )
204
205
    generic_create(
206
        evm, endowment, contract_address, memory_start_position, memory_size
207
    )
208
209
    # PROGRAM COUNTER
210
    evm.pc += Uint(1)

return_

Halts execution returning output data.

Parameters

evm : The current EVM frame.

def return_(evm: Evm) -> None:
214
    """
215
    Halts execution returning output data.
216
217
    Parameters
218
    ----------
219
    evm :
220
        The current EVM frame.
221
    """
222
    # STACK
223
    memory_start_position = pop(evm.stack)
224
    memory_size = pop(evm.stack)
225
226
    # GAS
227
    extend_memory = calculate_gas_extend_memory(
228
        evm.memory, [(memory_start_position, memory_size)]
229
    )
230
231
    charge_gas(evm, GAS_ZERO + extend_memory.cost)
232
233
    # OPERATION
234
    evm.memory += b"\x00" * extend_memory.expand_by
235
    evm.output = memory_read_bytes(
236
        evm.memory, memory_start_position, memory_size
237
    )
238
239
    evm.running = False
240
241
    # PROGRAM COUNTER
242
    pass

generic_call

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

def generic_call(evm: Evm, ​​gas: Uint, ​​value: U256, ​​caller: Address, ​​to: Address, ​​code_address: Address, ​​should_transfer_value: bool, ​​is_staticcall: bool, ​​memory_input_start_position: U256, ​​memory_input_size: U256, ​​memory_output_start_position: U256, ​​memory_output_size: U256) -> None:
259
    """
260
    Perform the core logic of the `CALL*` family of opcodes.
261
    """
262
    from ...vm.interpreter import STACK_DEPTH_LIMIT, process_message
263
264
    evm.return_data = b""
265
266
    if evm.message.depth + Uint(1) > STACK_DEPTH_LIMIT:
267
        evm.gas_left += gas
268
        push(evm.stack, U256(0))
269
        return
270
271
    call_data = memory_read_bytes(
272
        evm.memory, memory_input_start_position, memory_input_size
273
    )
274
    code = get_account(evm.env.state, code_address).code
275
    child_message = Message(
276
        caller=caller,
277
        target=to,
278
        gas=gas,
279
        value=value,
280
        data=call_data,
281
        code=code,
282
        current_target=to,
283
        depth=evm.message.depth + Uint(1),
284
        code_address=code_address,
285
        should_transfer_value=should_transfer_value,
286
        is_static=True if is_staticcall else evm.message.is_static,
287
        accessed_addresses=evm.accessed_addresses.copy(),
288
        accessed_storage_keys=evm.accessed_storage_keys.copy(),
289
        parent_evm=evm,
290
    )
291
    child_evm = process_message(child_message, evm.env)
292
293
    if child_evm.error:
294
        incorporate_child_on_error(evm, child_evm)
295
        evm.return_data = child_evm.output
296
        push(evm.stack, U256(0))
297
    else:
298
        incorporate_child_on_success(evm, child_evm)
299
        evm.return_data = child_evm.output
300
        push(evm.stack, U256(1))
301
302
    actual_output_size = min(memory_output_size, U256(len(child_evm.output)))
303
    memory_write(
304
        evm.memory,
305
        memory_output_start_position,
306
        child_evm.output[:actual_output_size],
307
    )

call

Message-call into an account.

Parameters

evm : The current EVM frame.

def call(evm: Evm) -> None:
311
    """
312
    Message-call into an account.
313
314
    Parameters
315
    ----------
316
    evm :
317
        The current EVM frame.
318
    """
319
    # STACK
320
    gas = Uint(pop(evm.stack))
321
    to = to_address(pop(evm.stack))
322
    value = pop(evm.stack)
323
    memory_input_start_position = pop(evm.stack)
324
    memory_input_size = pop(evm.stack)
325
    memory_output_start_position = pop(evm.stack)
326
    memory_output_size = pop(evm.stack)
327
328
    # GAS
329
    extend_memory = calculate_gas_extend_memory(
330
        evm.memory,
331
        [
332
            (memory_input_start_position, memory_input_size),
333
            (memory_output_start_position, memory_output_size),
334
        ],
335
    )
336
337
    if to in evm.accessed_addresses:
338
        access_gas_cost = GAS_WARM_ACCESS
339
    else:
340
        evm.accessed_addresses.add(to)
341
        access_gas_cost = GAS_COLD_ACCOUNT_ACCESS
342
343
    create_gas_cost = (
344
        Uint(0)
345
        if is_account_alive(evm.env.state, to) or value == 0
346
        else GAS_NEW_ACCOUNT
347
    )
348
    transfer_gas_cost = Uint(0) if value == 0 else GAS_CALL_VALUE
349
    message_call_gas = calculate_message_call_gas(
350
        value,
351
        gas,
352
        Uint(evm.gas_left),
353
        extend_memory.cost,
354
        access_gas_cost + create_gas_cost + transfer_gas_cost,
355
    )
356
    charge_gas(evm, message_call_gas.cost + extend_memory.cost)
357
    if evm.message.is_static and value != U256(0):
358
        raise WriteInStaticContext
359
    evm.memory += b"\x00" * extend_memory.expand_by
360
    sender_balance = get_account(
361
        evm.env.state, evm.message.current_target
362
    ).balance
363
    if sender_balance < value:
364
        push(evm.stack, U256(0))
365
        evm.return_data = b""
366
        evm.gas_left += message_call_gas.stipend
367
    else:
368
        generic_call(
369
            evm,
370
            message_call_gas.stipend,
371
            value,
372
            evm.message.current_target,
373
            to,
374
            to,
375
            True,
376
            False,
377
            memory_input_start_position,
378
            memory_input_size,
379
            memory_output_start_position,
380
            memory_output_size,
381
        )
382
383
    # PROGRAM COUNTER
384
    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:
388
    """
389
    Message-call into this account with alternative account’s code.
390
391
    Parameters
392
    ----------
393
    evm :
394
        The current EVM frame.
395
    """
396
    # STACK
397
    gas = Uint(pop(evm.stack))
398
    code_address = to_address(pop(evm.stack))
399
    value = pop(evm.stack)
400
    memory_input_start_position = pop(evm.stack)
401
    memory_input_size = pop(evm.stack)
402
    memory_output_start_position = pop(evm.stack)
403
    memory_output_size = pop(evm.stack)
404
405
    # GAS
406
    to = evm.message.current_target
407
408
    extend_memory = calculate_gas_extend_memory(
409
        evm.memory,
410
        [
411
            (memory_input_start_position, memory_input_size),
412
            (memory_output_start_position, memory_output_size),
413
        ],
414
    )
415
416
    if code_address in evm.accessed_addresses:
417
        access_gas_cost = GAS_WARM_ACCESS
418
    else:
419
        evm.accessed_addresses.add(code_address)
420
        access_gas_cost = GAS_COLD_ACCOUNT_ACCESS
421
422
    transfer_gas_cost = Uint(0) if value == 0 else GAS_CALL_VALUE
423
    message_call_gas = calculate_message_call_gas(
424
        value,
425
        gas,
426
        Uint(evm.gas_left),
427
        extend_memory.cost,
428
        access_gas_cost + transfer_gas_cost,
429
    )
430
    charge_gas(evm, message_call_gas.cost + extend_memory.cost)
431
432
    # OPERATION
433
    evm.memory += b"\x00" * extend_memory.expand_by
434
    sender_balance = get_account(
435
        evm.env.state, evm.message.current_target
436
    ).balance
437
    if sender_balance < value:
438
        push(evm.stack, U256(0))
439
        evm.return_data = b""
440
        evm.gas_left += message_call_gas.stipend
441
    else:
442
        generic_call(
443
            evm,
444
            message_call_gas.stipend,
445
            value,
446
            evm.message.current_target,
447
            to,
448
            code_address,
449
            True,
450
            False,
451
            memory_input_start_position,
452
            memory_input_size,
453
            memory_output_start_position,
454
            memory_output_size,
455
        )
456
457
    # PROGRAM COUNTER
458
    evm.pc += Uint(1)

selfdestruct

Halt execution and register account for later deletion.

Parameters

evm : The current EVM frame.

def selfdestruct(evm: Evm) -> None:
462
    """
463
    Halt execution and register account for later deletion.
464
465
    Parameters
466
    ----------
467
    evm :
468
        The current EVM frame.
469
    """
470
    # STACK
471
    beneficiary = to_address(pop(evm.stack))
472
473
    # GAS
474
    gas_cost = GAS_SELF_DESTRUCT
475
    if beneficiary not in evm.accessed_addresses:
476
        evm.accessed_addresses.add(beneficiary)
477
        gas_cost += GAS_COLD_ACCOUNT_ACCESS
478
479
    if (
480
        not is_account_alive(evm.env.state, beneficiary)
481
        and get_account(evm.env.state, evm.message.current_target).balance != 0
482
    ):
483
        gas_cost += GAS_SELF_DESTRUCT_NEW_ACCOUNT
484
485
    originator = evm.message.current_target
486
487
    refunded_accounts = evm.accounts_to_delete
488
    parent_evm = evm.message.parent_evm
489
    while parent_evm is not None:
490
        refunded_accounts.update(parent_evm.accounts_to_delete)
491
        parent_evm = parent_evm.message.parent_evm
492
493
    if originator not in refunded_accounts:
494
        evm.refund_counter += REFUND_SELF_DESTRUCT
495
496
    charge_gas(evm, gas_cost)
497
    if evm.message.is_static:
498
        raise WriteInStaticContext
499
500
    beneficiary_balance = get_account(evm.env.state, beneficiary).balance
501
    originator_balance = get_account(evm.env.state, originator).balance
502
503
    # First Transfer to beneficiary
504
    set_account_balance(
505
        evm.env.state, beneficiary, beneficiary_balance + originator_balance
506
    )
507
    # Next, Zero the balance of the address being deleted (must come after
508
    # sending to beneficiary in case the contract named itself as the
509
    # beneficiary).
510
    set_account_balance(evm.env.state, originator, U256(0))
511
512
    # register account for deletion
513
    evm.accounts_to_delete.add(originator)
514
515
    # mark beneficiary as touched
516
    if account_exists_and_is_empty(evm.env.state, beneficiary):
517
        evm.touched_accounts.add(beneficiary)
518
519
    # HALT the execution
520
    evm.running = False
521
522
    # PROGRAM COUNTER
523
    pass

delegatecall

Message-call into an account.

Parameters

evm : The current EVM frame.

def delegatecall(evm: Evm) -> None:
527
    """
528
    Message-call into an account.
529
530
    Parameters
531
    ----------
532
    evm :
533
        The current EVM frame.
534
    """
535
    # STACK
536
    gas = Uint(pop(evm.stack))
537
    code_address = to_address(pop(evm.stack))
538
    memory_input_start_position = pop(evm.stack)
539
    memory_input_size = pop(evm.stack)
540
    memory_output_start_position = pop(evm.stack)
541
    memory_output_size = pop(evm.stack)
542
543
    # GAS
544
    extend_memory = calculate_gas_extend_memory(
545
        evm.memory,
546
        [
547
            (memory_input_start_position, memory_input_size),
548
            (memory_output_start_position, memory_output_size),
549
        ],
550
    )
551
552
    if code_address in evm.accessed_addresses:
553
        access_gas_cost = GAS_WARM_ACCESS
554
    else:
555
        evm.accessed_addresses.add(code_address)
556
        access_gas_cost = GAS_COLD_ACCOUNT_ACCESS
557
558
    message_call_gas = calculate_message_call_gas(
559
        U256(0), gas, Uint(evm.gas_left), extend_memory.cost, access_gas_cost
560
    )
561
    charge_gas(evm, message_call_gas.cost + extend_memory.cost)
562
563
    # OPERATION
564
    evm.memory += b"\x00" * extend_memory.expand_by
565
    generic_call(
566
        evm,
567
        message_call_gas.stipend,
568
        evm.message.value,
569
        evm.message.caller,
570
        evm.message.current_target,
571
        code_address,
572
        False,
573
        False,
574
        memory_input_start_position,
575
        memory_input_size,
576
        memory_output_start_position,
577
        memory_output_size,
578
    )
579
580
    # PROGRAM COUNTER
581
    evm.pc += Uint(1)

staticcall

Message-call into an account.

Parameters

evm : The current EVM frame.

def staticcall(evm: Evm) -> None:
585
    """
586
    Message-call into an account.
587
588
    Parameters
589
    ----------
590
    evm :
591
        The current EVM frame.
592
    """
593
    # STACK
594
    gas = Uint(pop(evm.stack))
595
    to = to_address(pop(evm.stack))
596
    memory_input_start_position = pop(evm.stack)
597
    memory_input_size = pop(evm.stack)
598
    memory_output_start_position = pop(evm.stack)
599
    memory_output_size = pop(evm.stack)
600
601
    # GAS
602
    extend_memory = calculate_gas_extend_memory(
603
        evm.memory,
604
        [
605
            (memory_input_start_position, memory_input_size),
606
            (memory_output_start_position, memory_output_size),
607
        ],
608
    )
609
610
    if to in evm.accessed_addresses:
611
        access_gas_cost = GAS_WARM_ACCESS
612
    else:
613
        evm.accessed_addresses.add(to)
614
        access_gas_cost = GAS_COLD_ACCOUNT_ACCESS
615
616
    message_call_gas = calculate_message_call_gas(
617
        U256(0),
618
        gas,
619
        Uint(evm.gas_left),
620
        extend_memory.cost,
621
        access_gas_cost,
622
    )
623
    charge_gas(evm, message_call_gas.cost + extend_memory.cost)
624
625
    # OPERATION
626
    evm.memory += b"\x00" * extend_memory.expand_by
627
    generic_call(
628
        evm,
629
        message_call_gas.stipend,
630
        U256(0),
631
        evm.message.current_target,
632
        to,
633
        to,
634
        True,
635
        True,
636
        memory_input_start_position,
637
        memory_input_size,
638
        memory_output_start_position,
639
        memory_output_size,
640
    )
641
642
    # PROGRAM COUNTER
643
    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:
647
    """
648
    Stop execution and revert state changes, without consuming all provided gas
649
    and also has the ability to return a reason
650
    Parameters
651
    ----------
652
    evm :
653
        The current EVM frame.
654
    """
655
    # STACK
656
    memory_start_index = pop(evm.stack)
657
    size = pop(evm.stack)
658
659
    # GAS
660
    extend_memory = calculate_gas_extend_memory(
661
        evm.memory, [(memory_start_index, size)]
662
    )
663
664
    charge_gas(evm, extend_memory.cost)
665
666
    # OPERATION
667
    evm.memory += b"\x00" * extend_memory.expand_by
668
    output = memory_read_bytes(evm.memory, memory_start_index, size)
669
    evm.output = bytes(output)
670
    raise Revert
671
672
    # PROGRAM COUNTER
673
    pass