ethereum.forks.byzantium.vm.instructions.systemethereum.forks.constantinople.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:
68
    """
69
    Core logic used by the `CREATE*` family of opcodes.
70
    """
71
    # This import causes a circular import error
72
    # if it's not moved inside this method
73
    from ...vm.interpreter import STACK_DEPTH_LIMIT, process_create_message
74
75
    call_data = memory_read_bytes(
76
        evm.memory, memory_start_position, memory_size
77
    )
78
79
    create_message_gas = max_message_call_gas(Uint(evm.gas_left))
80
    evm.gas_left -= create_message_gas
81
    if evm.message.is_static:
82
        raise WriteInStaticContext
83
    evm.return_data = b""
84
85
    sender_address = evm.message.current_target
86
    sender = get_account(evm.message.block_env.state, sender_address)
87
88
    if (
89
        sender.balance < endowment
90
        or sender.nonce == Uint(2**64 - 1)
91
        or evm.message.depth + Uint(1) > STACK_DEPTH_LIMIT
92
    ):
93
        evm.gas_left += create_message_gas
94
        push(evm.stack, U256(0))
95
        return
96
97
    if account_has_code_or_nonce(
98
        evm.message.block_env.state, contract_address
99
    ) or account_has_storage(evm.message.block_env.state, contract_address):
100
        increment_nonce(
101
            evm.message.block_env.state, evm.message.current_target
102
        )
103
        push(evm.stack, U256(0))
104
        return
105
106
    call_data = memory_read_bytes(
107
        evm.memory, memory_start_position, memory_size
108
    )
109
110
    increment_nonce(evm.message.block_env.state, evm.message.current_target)
111
112
    child_message = Message(
113
        block_env=evm.message.block_env,
114
        tx_env=evm.message.tx_env,
115
        caller=evm.message.current_target,
116
        target=Bytes0(),
117
        gas=create_message_gas,
118
        value=endowment,
119
        data=b"",
120
        code=call_data,
121
        current_target=contract_address,
122
        depth=evm.message.depth + Uint(1),
123
        code_address=None,
124
        should_transfer_value=True,
125
        is_static=False,
126
        parent_evm=evm,
127
    )
128
    child_evm = process_create_message(child_message)
129
130
    if child_evm.error:
131
        incorporate_child_on_error(evm, child_evm)
132
        evm.return_data = child_evm.output
133
        push(evm.stack, U256(0))
134
    else:
135
        incorporate_child_on_success(evm, child_evm)
136
        evm.return_data = b""
137
        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:
141
    """
142
    Creates a new account with associated code.
143
144
    Parameters
145
    ----------
146
    evm :
147
        The current EVM frame.
148
149
    """
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
150
    # STACK
151
    endowment = pop(evm.stack)
152
    memory_start_position = pop(evm.stack)
153
    memory_size = pop(evm.stack)
154
155
    # GAS
156
    extend_memory = calculate_gas_extend_memory(
157
        evm.memory, [(memory_start_position, memory_size)]
158
    )
159
160
    charge_gas(evm, GAS_CREATE + extend_memory.cost)
161
81
    create_message_gas = max_message_call_gas(Uint(evm.gas_left))
82
    evm.gas_left -= create_message_gas
83
    if evm.message.is_static:
84
        raise WriteInStaticContext
162
    # OPERATION
163
    evm.memory += b"\x00" * extend_memory.expand_by
86
    evm.return_data = b""
87
88
    sender_address = evm.message.current_target
89
    sender = get_account(evm.message.block_env.state, sender_address)
90
164
    contract_address = compute_contract_address(
165
        evm.message.current_target,
166
        get_account(
167
            evm.message.block_env.state, evm.message.current_target
168
        ).nonce,
169
    )
170
98
    if (
99
        sender.balance < endowment
100
        or sender.nonce == Uint(2**64 - 1)
101
        or evm.message.depth + Uint(1) > STACK_DEPTH_LIMIT
102
    ):
103
        push(evm.stack, U256(0))
104
        evm.gas_left += create_message_gas
105
    elif account_has_code_or_nonce(
106
        evm.message.block_env.state, contract_address
107
    ) or account_has_storage(evm.message.block_env.state, contract_address):
108
        increment_nonce(
109
            evm.message.block_env.state, evm.message.current_target
110
        )
111
        push(evm.stack, U256(0))
112
    else:
113
        call_data = memory_read_bytes(
114
            evm.memory, memory_start_position, memory_size
115
        )
116
117
        increment_nonce(
118
            evm.message.block_env.state, evm.message.current_target
119
        )
120
121
        child_message = Message(
122
            block_env=evm.message.block_env,
123
            tx_env=evm.message.tx_env,
124
            caller=evm.message.current_target,
125
            target=Bytes0(),
126
            gas=create_message_gas,
127
            value=endowment,
128
            data=b"",
129
            code=call_data,
130
            current_target=contract_address,
131
            depth=evm.message.depth + Uint(1),
132
            code_address=None,
133
            should_transfer_value=True,
134
            is_static=False,
135
            parent_evm=evm,
136
        )
137
        child_evm = process_create_message(child_message)
138
139
        if child_evm.error:
140
            incorporate_child_on_error(evm, child_evm)
141
            evm.return_data = child_evm.output
142
            push(evm.stack, U256(0))
143
        else:
144
            incorporate_child_on_success(evm, child_evm)
145
            evm.return_data = b""
146
            push(
147
                evm.stack, U256.from_be_bytes(child_evm.message.current_target)
148
            )
171
    generic_create(
172
        evm, endowment, contract_address, memory_start_position, memory_size
173
    )
174
175
    # PROGRAM COUNTER
176
    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:
180
    """
181
    Creates a new account with associated code.
182
183
    It's similar to the CREATE opcode except that the address of the new
184
    account depends on the init_code instead of the nonce of sender.
185
186
    Parameters
187
    ----------
188
    evm :
189
        The current EVM frame.
190
191
    """
192
    # STACK
193
    endowment = pop(evm.stack)
194
    memory_start_position = pop(evm.stack)
195
    memory_size = pop(evm.stack)
196
    salt = pop(evm.stack).to_be_bytes32()
197
198
    # GAS
199
    extend_memory = calculate_gas_extend_memory(
200
        evm.memory, [(memory_start_position, memory_size)]
201
    )
202
    call_data_words = ceil32(Uint(memory_size)) // Uint(32)
203
    charge_gas(
204
        evm,
205
        GAS_CREATE
206
        + GAS_KECCAK256_PER_WORD * call_data_words
207
        + extend_memory.cost,
208
    )
209
210
    # OPERATION
211
    evm.memory += b"\x00" * extend_memory.expand_by
212
    contract_address = compute_create2_contract_address(
213
        evm.message.current_target,
214
        salt,
215
        memory_read_bytes(evm.memory, memory_start_position, memory_size),
216
    )
217
218
    generic_create(
219
        evm, endowment, contract_address, memory_start_position, memory_size
220
    )
221
222
    # PROGRAM COUNTER
223
    evm.pc += Uint(1)

return_

Halts execution returning output data.

Parameters

evm : The current EVM frame.

def return_(evm: Evm) -> None:
227
    """
228
    Halts execution returning output data.
229
230
    Parameters
231
    ----------
232
    evm :
233
        The current EVM frame.
234
235
    """
236
    # STACK
237
    memory_start_position = pop(evm.stack)
238
    memory_size = pop(evm.stack)
239
240
    # GAS
241
    extend_memory = calculate_gas_extend_memory(
242
        evm.memory, [(memory_start_position, memory_size)]
243
    )
244
245
    charge_gas(evm, GAS_ZERO + extend_memory.cost)
246
247
    # OPERATION
248
    evm.memory += b"\x00" * extend_memory.expand_by
249
    evm.output = memory_read_bytes(
250
        evm.memory, memory_start_position, memory_size
251
    )
252
253
    evm.running = False
254
255
    # PROGRAM COUNTER
256
    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:
273
    """
274
    Perform the core logic of the `CALL*` family of opcodes.
275
    """
276
    from ...vm.interpreter import STACK_DEPTH_LIMIT, process_message
277
278
    evm.return_data = b""
279
280
    if evm.message.depth + Uint(1) > STACK_DEPTH_LIMIT:
281
        evm.gas_left += gas
282
        push(evm.stack, U256(0))
283
        return
284
285
    call_data = memory_read_bytes(
286
        evm.memory, memory_input_start_position, memory_input_size
287
    )
288
    account = get_account(evm.message.block_env.state, code_address)
289
    code = get_code(evm.message.block_env.state, account.code_hash)
290
    child_message = Message(
291
        block_env=evm.message.block_env,
292
        tx_env=evm.message.tx_env,
293
        caller=caller,
294
        target=to,
295
        gas=gas,
296
        value=value,
297
        data=call_data,
298
        code=code,
299
        current_target=to,
300
        depth=evm.message.depth + Uint(1),
301
        code_address=code_address,
302
        should_transfer_value=should_transfer_value,
303
        is_static=True if is_staticcall else evm.message.is_static,
304
        parent_evm=evm,
305
    )
306
    child_evm = process_message(child_message)
307
308
    if child_evm.error:
309
        incorporate_child_on_error(evm, child_evm)
310
        evm.return_data = child_evm.output
311
        push(evm.stack, U256(0))
312
    else:
313
        incorporate_child_on_success(evm, child_evm)
314
        evm.return_data = child_evm.output
315
        push(evm.stack, U256(1))
316
317
    actual_output_size = min(memory_output_size, U256(len(child_evm.output)))
318
    memory_write(
319
        evm.memory,
320
        memory_output_start_position,
321
        child_evm.output[:actual_output_size],
322
    )

call

Message-call into an account.

Parameters

evm : The current EVM frame.

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

selfdestruct

Halt execution and register account for later deletion.

Parameters

evm : The current EVM frame.

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

delegatecall

Message-call into an account.

Parameters

evm : The current EVM frame.

def delegatecall(evm: Evm) -> None:
538
    """
539
    Message-call into an account.
540
541
    Parameters
542
    ----------
543
    evm :
544
        The current EVM frame.
545
546
    """
547
    # STACK
548
    gas = Uint(pop(evm.stack))
549
    code_address = to_address_masked(pop(evm.stack))
550
    memory_input_start_position = pop(evm.stack)
551
    memory_input_size = pop(evm.stack)
552
    memory_output_start_position = pop(evm.stack)
553
    memory_output_size = pop(evm.stack)
554
555
    # GAS
556
    extend_memory = calculate_gas_extend_memory(
557
        evm.memory,
558
        [
559
            (memory_input_start_position, memory_input_size),
560
            (memory_output_start_position, memory_output_size),
561
        ],
562
    )
563
    message_call_gas = calculate_message_call_gas(
564
        U256(0), gas, Uint(evm.gas_left), extend_memory.cost, GAS_CALL
565
    )
566
    charge_gas(evm, message_call_gas.cost + extend_memory.cost)
567
568
    # OPERATION
569
    evm.memory += b"\x00" * extend_memory.expand_by
570
    generic_call(
571
        evm,
572
        message_call_gas.sub_call,
573
        evm.message.value,
574
        evm.message.caller,
575
        evm.message.current_target,
576
        code_address,
577
        False,
578
        False,
579
        memory_input_start_position,
580
        memory_input_size,
581
        memory_output_start_position,
582
        memory_output_size,
583
    )
584
585
    # PROGRAM COUNTER
586
    evm.pc += Uint(1)

staticcall

Message-call into an account.

Parameters

evm : The current EVM frame.

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