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

return_

Halts execution returning output data.

Parameters

evm : The current EVM frame.

def return_(evm: Evm) -> None:
255
    """
256
    Halts execution returning output data.
257
258
    Parameters
259
    ----------
260
    evm :
261
        The current EVM frame.
262
263
    """
264
    # STACK
265
    memory_start_position = pop(evm.stack)
266
    memory_size = pop(evm.stack)
267
268
    # GAS
269
    extend_memory = calculate_gas_extend_memory(
270
        evm.memory, [(memory_start_position, memory_size)]
271
    )
272
273
    charge_gas(evm, GAS_ZERO + extend_memory.cost)
274
275
    # OPERATION
276
    evm.memory += b"\x00" * extend_memory.expand_by
277
    evm.output = memory_read_bytes(
278
        evm.memory, memory_start_position, memory_size
279
    )
280
281
    evm.running = False
282
283
    # PROGRAM COUNTER
284
    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, ​​code: Bytes, ​​disable_precompiles: bool) -> None:
303
    """
304
    Perform the core logic of the `CALL*` family of opcodes.
305
    """
306
    from ...vm.interpreter import STACK_DEPTH_LIMIT, process_message
307
308
    evm.return_data = b""
309
310
    if evm.message.depth + Uint(1) > STACK_DEPTH_LIMIT:
311
        evm.gas_left += gas
312
        push(evm.stack, U256(0))
313
        return
314
315
    call_data = memory_read_bytes(
316
        evm.memory, memory_input_start_position, memory_input_size
317
    )
318
319
    child_message = Message(
320
        block_env=evm.message.block_env,
321
        tx_env=evm.message.tx_env,
322
        caller=caller,
323
        target=to,
324
        gas=gas,
325
        value=value,
326
        data=call_data,
327
        code=code,
328
        current_target=to,
329
        depth=evm.message.depth + Uint(1),
330
        code_address=code_address,
331
        should_transfer_value=should_transfer_value,
332
        is_static=True if is_staticcall else evm.message.is_static,
333
        accessed_addresses=evm.accessed_addresses.copy(),
334
        accessed_storage_keys=evm.accessed_storage_keys.copy(),
335
        disable_precompiles=disable_precompiles,
336
        parent_evm=evm,
337
        is_create=False,
338
    )
339
340
    child_evm = process_message(child_message)
341
342
    if child_evm.error:
343
        incorporate_child_on_error(evm, child_evm)
344
        evm.return_data = child_evm.output
345
        push(evm.stack, U256(0))
346
    else:
347
        incorporate_child_on_success(evm, child_evm)
348
        evm.return_data = child_evm.output
349
        push(evm.stack, U256(1))
350
351
    actual_output_size = min(memory_output_size, U256(len(child_evm.output)))
352
    memory_write(
353
        evm.memory,
354
        memory_output_start_position,
355
        child_evm.output[:actual_output_size],
356
    )

call

Message-call into an account.

Parameters

evm : The current EVM frame.

def call(evm: Evm) -> None:
360
    """
361
    Message-call into an account.
362
363
    Parameters
364
    ----------
365
    evm :
366
        The current EVM frame.
367
368
    """
369
    # STACK
370
    gas = Uint(pop(evm.stack))
371
    to = to_address_masked(pop(evm.stack))
372
    value = pop(evm.stack)
373
    memory_input_start_position = pop(evm.stack)
374
    memory_input_size = pop(evm.stack)
375
    memory_output_start_position = pop(evm.stack)
376
    memory_output_size = pop(evm.stack)
377
378
    if evm.message.is_static and value != U256(0):
379
        raise WriteInStaticContext
380
381
    # GAS
382
    extend_memory = calculate_gas_extend_memory(
383
        evm.memory,
384
        [
385
            (memory_input_start_position, memory_input_size),
386
            (memory_output_start_position, memory_output_size),
387
        ],
388
    )
389
376
    if to in evm.accessed_addresses:
377
        access_gas_cost = GAS_WARM_ACCESS
390
    is_cold_access = to not in evm.accessed_addresses
391
    if is_cold_access:
392
        access_gas_cost = GAS_COLD_ACCOUNT_ACCESS
393
    else:
379
        evm.accessed_addresses.add(to)
380
        access_gas_cost = GAS_COLD_ACCOUNT_ACCESS
394
        access_gas_cost = GAS_WARM_ACCESS
395
382
    code_address = to
383
    (
384
        disable_precompiles,
385
        code_address,
386
        code,
387
        delegated_access_gas_cost,
388
    ) = access_delegation(evm, code_address)
389
    access_gas_cost += delegated_access_gas_cost
396
    transfer_gas_cost = Uint(0) if value == 0 else GAS_CALL_VALUE
397
398
    # check static gas before state access
399
    check_gas(
400
        evm,
401
        access_gas_cost + transfer_gas_cost + extend_memory.cost,
402
    )
403
404
    # STATE ACCESS
405
    tx_state = evm.message.tx_env.state
406
    if is_cold_access:
407
        evm.accessed_addresses.add(to)
408
409
    create_gas_cost = GAS_NEW_ACCOUNT
392
    if value == 0 or is_account_alive(evm.message.block_env.state, to):
410
    if value == 0 or is_account_alive(tx_state, to):
411
        create_gas_cost = Uint(0)
394
    transfer_gas_cost = Uint(0) if value == 0 else GAS_CALL_VALUE
412
413
    extra_gas = access_gas_cost + transfer_gas_cost + create_gas_cost
414
    (
415
        is_delegated,
416
        code_address,
417
        delegation_access_cost,
418
    ) = calculate_delegation_cost(evm, to)
419
420
    if is_delegated:
421
        # check enough gas for delegation access
422
        extra_gas += delegation_access_cost
423
        check_gas(evm, extra_gas + extend_memory.cost)
424
        if code_address not in evm.accessed_addresses:
425
            evm.accessed_addresses.add(code_address)
426
427
    code_hash = get_account(tx_state, code_address).code_hash
428
    code = get_code(tx_state, code_hash)
429
430
    message_call_gas = calculate_message_call_gas(
431
        value,
432
        gas,
433
        Uint(evm.gas_left),
434
        extend_memory.cost,
400
        access_gas_cost + create_gas_cost + transfer_gas_cost,
435
        extra_gas,
436
    )
437
    charge_gas(evm, message_call_gas.cost + extend_memory.cost)
403
    if evm.message.is_static and value != U256(0):
404
        raise WriteInStaticContext
438
439
    evm.memory += b"\x00" * extend_memory.expand_by
406
    sender_balance = get_account(
407
        evm.message.block_env.state, evm.message.current_target
408
    ).balance
440
    sender_balance = get_account(tx_state, evm.message.current_target).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
            code,
428
            disable_precompiles,
460
            is_delegated,
461
        )
462
463
    # PROGRAM COUNTER
464
    evm.pc += Uint(1)

callcode

Message-call into this account with alternative account’s code.Message-call into this account with alternative account's code.

Parameters

evm : The current EVM frame.

def callcode(evm: Evm) -> None:
468
    """
437
    Message-call into this account with alternative account’s code.
469
    Message-call into this account with alternative account's code.
470
471
    Parameters
472
    ----------
473
    evm :
474
        The current EVM frame.
475
476
    """
477
    # STACK
478
    gas = Uint(pop(evm.stack))
479
    code_address = to_address_masked(pop(evm.stack))
480
    value = pop(evm.stack)
481
    memory_input_start_position = pop(evm.stack)
482
    memory_input_size = pop(evm.stack)
483
    memory_output_start_position = pop(evm.stack)
484
    memory_output_size = pop(evm.stack)
485
486
    # GAS
487
    to = evm.message.current_target
488
489
    extend_memory = calculate_gas_extend_memory(
490
        evm.memory,
491
        [
492
            (memory_input_start_position, memory_input_size),
493
            (memory_output_start_position, memory_output_size),
494
        ],
495
    )
496
465
    if code_address in evm.accessed_addresses:
466
        access_gas_cost = GAS_WARM_ACCESS
497
    is_cold_access = code_address not in evm.accessed_addresses
498
    if is_cold_access:
499
        access_gas_cost = GAS_COLD_ACCOUNT_ACCESS
500
    else:
468
        evm.accessed_addresses.add(code_address)
469
        access_gas_cost = GAS_COLD_ACCOUNT_ACCESS
470
471
    (
472
        disable_precompiles,
473
        code_address,
474
        code,
475
        delegated_access_gas_cost,
476
    ) = access_delegation(evm, code_address)
477
    access_gas_cost += delegated_access_gas_cost
501
        access_gas_cost = GAS_WARM_ACCESS
502
503
    transfer_gas_cost = Uint(0) if value == 0 else GAS_CALL_VALUE
504
505
    # check static gas before state access
506
    check_gas(
507
        evm,
508
        access_gas_cost + extend_memory.cost + transfer_gas_cost,
509
    )
510
511
    # STATE ACCESS
512
    tx_state = evm.message.tx_env.state
513
    if is_cold_access:
514
        evm.accessed_addresses.add(code_address)
515
516
    extra_gas = access_gas_cost + transfer_gas_cost
517
    (
518
        is_delegated,
519
        code_address,
520
        delegation_access_cost,
521
    ) = calculate_delegation_cost(evm, code_address)
522
523
    if is_delegated:
524
        # check enough gas for delegation access
525
        extra_gas += delegation_access_cost
526
        check_gas(evm, extra_gas + extend_memory.cost)
527
        if code_address not in evm.accessed_addresses:
528
            evm.accessed_addresses.add(code_address)
529
530
    code_hash = get_account(tx_state, code_address).code_hash
531
    code = get_code(tx_state, code_hash)
532
533
    message_call_gas = calculate_message_call_gas(
534
        value,
535
        gas,
536
        Uint(evm.gas_left),
537
        extend_memory.cost,
485
        access_gas_cost + transfer_gas_cost,
538
        extra_gas,
539
    )
540
    charge_gas(evm, message_call_gas.cost + extend_memory.cost)
541
542
    # OPERATION
543
    evm.memory += b"\x00" * extend_memory.expand_by
491
    sender_balance = get_account(
492
        evm.message.block_env.state, evm.message.current_target
493
    ).balance
544
    sender_balance = get_account(tx_state, evm.message.current_target).balance
545
546
    if sender_balance < value:
547
        push(evm.stack, U256(0))
548
        evm.return_data = b""
549
        evm.gas_left += message_call_gas.sub_call
550
    else:
551
        generic_call(
552
            evm,
553
            message_call_gas.sub_call,
554
            value,
555
            evm.message.current_target,
556
            to,
557
            code_address,
558
            True,
559
            False,
560
            memory_input_start_position,
561
            memory_input_size,
562
            memory_output_start_position,
563
            memory_output_size,
564
            code,
513
            disable_precompiles,
565
            is_delegated,
566
        )
567
568
    # PROGRAM COUNTER
569
    evm.pc += Uint(1)

selfdestruct

Halt execution and register account for later deletion.

Parameters

evm : The current EVM frame.

def selfdestruct(evm: Evm) -> None:
573
    """
574
    Halt execution and register account for later deletion.
575
576
    Parameters
577
    ----------
578
    evm :
579
        The current EVM frame.
580
581
    """
582
    if evm.message.is_static:
583
        raise WriteInStaticContext
584
585
    # STACK
586
    beneficiary = to_address_masked(pop(evm.stack))
587
588
    # GAS
589
    gas_cost = GAS_SELF_DESTRUCT
535
    if beneficiary not in evm.accessed_addresses:
536
        evm.accessed_addresses.add(beneficiary)
537
        gas_cost += GAS_COLD_ACCOUNT_ACCESS
590
591
    is_cold_access = beneficiary not in evm.accessed_addresses
592
    if is_cold_access:
593
        gas_cost += GAS_COLD_ACCOUNT_ACCESS
594
595
    # check access gas cost before state access
596
    check_gas(evm, gas_cost)
597
598
    # STATE ACCESS
599
    tx_state = evm.message.tx_env.state
600
    if is_cold_access:
601
        evm.accessed_addresses.add(beneficiary)
602
603
    if (
540
        not is_account_alive(evm.message.block_env.state, beneficiary)
541
        and get_account(
542
            evm.message.block_env.state, evm.message.current_target
543
        ).balance
544
        != 0
604
        not is_account_alive(tx_state, beneficiary)
605
        and get_account(tx_state, evm.message.current_target).balance != 0
606
    ):
607
        gas_cost += GAS_SELF_DESTRUCT_NEW_ACCOUNT
608
609
    charge_gas(evm, gas_cost)
549
    if evm.message.is_static:
550
        raise WriteInStaticContext
610
611
    originator = evm.message.current_target
553
    originator_balance = get_account(
554
        evm.message.block_env.state, originator
555
    ).balance
612
    originator_balance = get_account(tx_state, originator).balance
613
557
    move_ether(
558
        evm.message.block_env.state,
559
        originator,
560
        beneficiary,
561
        originator_balance,
562
    )
614
    # Transfer balance
615
    move_ether(tx_state, originator, beneficiary, originator_balance)
616
617
    # register account for deletion only if it was created
618
    # in the same transaction
566
    if originator in evm.message.block_env.state.created_accounts:
619
    if originator in tx_state.created_accounts:
620
        # If beneficiary is the same as originator, then
621
        # the ether is burnt.
569
        set_account_balance(evm.message.block_env.state, originator, U256(0))
622
        set_account_balance(tx_state, originator, U256(0))
623
        evm.accounts_to_delete.add(originator)
624
625
    # HALT the execution
626
    evm.running = False
627
628
    # PROGRAM COUNTER
629
    pass

delegatecall

Message-call into an account.

Parameters

evm : The current EVM frame.

def delegatecall(evm: Evm) -> None:
633
    """
634
    Message-call into an account.
635
636
    Parameters
637
    ----------
638
    evm :
639
        The current EVM frame.
640
641
    """
642
    # STACK
643
    gas = Uint(pop(evm.stack))
644
    code_address = to_address_masked(pop(evm.stack))
645
    memory_input_start_position = pop(evm.stack)
646
    memory_input_size = pop(evm.stack)
647
    memory_output_start_position = pop(evm.stack)
648
    memory_output_size = pop(evm.stack)
649
650
    # GAS
651
    extend_memory = calculate_gas_extend_memory(
652
        evm.memory,
653
        [
654
            (memory_input_start_position, memory_input_size),
655
            (memory_output_start_position, memory_output_size),
656
        ],
657
    )
658
606
    if code_address in evm.accessed_addresses:
607
        access_gas_cost = GAS_WARM_ACCESS
659
    is_cold_access = code_address not in evm.accessed_addresses
660
    if is_cold_access:
661
        access_gas_cost = GAS_COLD_ACCOUNT_ACCESS
662
    else:
609
        evm.accessed_addresses.add(code_address)
610
        access_gas_cost = GAS_COLD_ACCOUNT_ACCESS
663
        access_gas_cost = GAS_WARM_ACCESS
664
665
    # check static gas before state access
666
    check_gas(evm, access_gas_cost + extend_memory.cost)
667
668
    # STATE ACCESS
669
    tx_state = evm.message.tx_env.state
670
    if is_cold_access:
671
        evm.accessed_addresses.add(code_address)
672
673
    extra_gas = access_gas_cost
674
    (
613
        disable_precompiles,
675
        is_delegated,
676
        code_address,
615
        code,
616
        delegated_access_gas_cost,
617
    ) = access_delegation(evm, code_address)
618
    access_gas_cost += delegated_access_gas_cost
677
        delegation_access_cost,
678
    ) = calculate_delegation_cost(evm, code_address)
679
680
    if is_delegated:
681
        # check enough gas for delegation access
682
        extra_gas += delegation_access_cost
683
        check_gas(evm, extra_gas + extend_memory.cost)
684
        if code_address not in evm.accessed_addresses:
685
            evm.accessed_addresses.add(code_address)
686
687
    code_hash = get_account(tx_state, code_address).code_hash
688
    code = get_code(tx_state, code_hash)
689
690
    message_call_gas = calculate_message_call_gas(
621
        U256(0), gas, Uint(evm.gas_left), extend_memory.cost, access_gas_cost
691
        U256(0),
692
        gas,
693
        Uint(evm.gas_left),
694
        extend_memory.cost,
695
        extra_gas,
696
    )
697
    charge_gas(evm, message_call_gas.cost + extend_memory.cost)
698
699
    # OPERATION
700
    evm.memory += b"\x00" * extend_memory.expand_by
701
    generic_call(
702
        evm,
703
        message_call_gas.sub_call,
704
        evm.message.value,
705
        evm.message.caller,
706
        evm.message.current_target,
707
        code_address,
708
        False,
709
        False,
710
        memory_input_start_position,
711
        memory_input_size,
712
        memory_output_start_position,
713
        memory_output_size,
714
        code,
641
        disable_precompiles,
715
        is_delegated,
716
    )
717
718
    # PROGRAM COUNTER
719
    evm.pc += Uint(1)

staticcall

Message-call into an account.

Parameters

evm : The current EVM frame.

def staticcall(evm: Evm) -> None:
723
    """
724
    Message-call into an account.
725
726
    Parameters
727
    ----------
728
    evm :
729
        The current EVM frame.
730
731
    """
732
    # STACK
733
    gas = Uint(pop(evm.stack))
734
    to = to_address_masked(pop(evm.stack))
735
    memory_input_start_position = pop(evm.stack)
736
    memory_input_size = pop(evm.stack)
737
    memory_output_start_position = pop(evm.stack)
738
    memory_output_size = pop(evm.stack)
739
740
    # GAS
741
    extend_memory = calculate_gas_extend_memory(
742
        evm.memory,
743
        [
744
            (memory_input_start_position, memory_input_size),
745
            (memory_output_start_position, memory_output_size),
746
        ],
747
    )
748
675
    if to in evm.accessed_addresses:
676
        access_gas_cost = GAS_WARM_ACCESS
749
    is_cold_access = to not in evm.accessed_addresses
750
    if is_cold_access:
751
        access_gas_cost = GAS_COLD_ACCOUNT_ACCESS
752
    else:
678
        evm.accessed_addresses.add(to)
679
        access_gas_cost = GAS_COLD_ACCOUNT_ACCESS
753
        access_gas_cost = GAS_WARM_ACCESS
754
681
    code_address = to
755
    # check static gas before state access
756
    check_gas(evm, access_gas_cost + extend_memory.cost)
757
758
    # STATE ACCESS
759
    tx_state = evm.message.tx_env.state
760
    if is_cold_access:
761
        evm.accessed_addresses.add(to)
762
763
    extra_gas = access_gas_cost
764
    (
683
        disable_precompiles,
765
        is_delegated,
766
        code_address,
685
        code,
686
        delegated_access_gas_cost,
687
    ) = access_delegation(evm, code_address)
688
    access_gas_cost += delegated_access_gas_cost
767
        delegation_access_cost,
768
    ) = calculate_delegation_cost(evm, to)
769
770
    if is_delegated:
771
        # check enough gas for delegation access
772
        extra_gas += delegation_access_cost
773
        check_gas(evm, extra_gas + extend_memory.cost)
774
        if code_address not in evm.accessed_addresses:
775
            evm.accessed_addresses.add(code_address)
776
777
    code_hash = get_account(tx_state, code_address).code_hash
778
    code = get_code(tx_state, code_hash)
779
780
    message_call_gas = calculate_message_call_gas(
781
        U256(0),
782
        gas,
783
        Uint(evm.gas_left),
784
        extend_memory.cost,
695
        access_gas_cost,
785
        extra_gas,
786
    )
787
    charge_gas(evm, message_call_gas.cost + extend_memory.cost)
788
789
    # OPERATION
790
    evm.memory += b"\x00" * extend_memory.expand_by
791
    generic_call(
792
        evm,
793
        message_call_gas.sub_call,
794
        U256(0),
795
        evm.message.current_target,
796
        to,
797
        code_address,
798
        True,
799
        True,
800
        memory_input_start_position,
801
        memory_input_size,
802
        memory_output_start_position,
803
        memory_output_size,
804
        code,
715
        disable_precompiles,
805
        is_delegated,
806
    )
807
808
    # PROGRAM COUNTER
809
    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:
813
    """
814
    Stop execution and revert state changes, without consuming all provided gas
815
    and also has the ability to return a reason.
816
817
    Parameters
818
    ----------
819
    evm :
820
        The current EVM frame.
821
822
    """
823
    # STACK
824
    memory_start_index = pop(evm.stack)
825
    size = pop(evm.stack)
826
827
    # GAS
828
    extend_memory = calculate_gas_extend_memory(
829
        evm.memory, [(memory_start_index, size)]
830
    )
831
832
    charge_gas(evm, extend_memory.cost)
833
834
    # OPERATION
835
    evm.memory += b"\x00" * extend_memory.expand_by
836
    output = memory_read_bytes(evm.memory, memory_start_index, size)
837
    evm.output = Bytes(output)
838
    raise Revert