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 Run the child-frame lifecycle for the CREATE* family of opcodes.

The opcode has already priced the operation itself; this function runs the lifecycle: preflight checks that abort without spawning, the destination access with its account-creation charge and collision check, the child's gas grant, the child frame itself, and the resolution of its outcome back into the creating frame.

def generic_create(evm: Evm, ​​endowment: U256, ​​contract_address: Address, ​​memory_start_position: U256, ​​memory_size: U256) -> None:
73
    <snip>
66
    # This import causes a circular import error
67
    # if it's not moved inside this method
68
    from ...vm.interpreter import (
69
        MAX_INIT_CODE_SIZE,
70
        STACK_DEPTH_LIMIT,
71
        process_create_message,
72
    )
82
    # These imports cause a circular import error
83
    # if they're not moved inside this method
84
    from ...vm.interpreter import STACK_DEPTH_LIMIT, process_create
85
    from ...vm.runtime import get_valid_jump_destinations
86
74
    call_data = memory_read_bytes(
87
    tx_state = evm.tx_env.state
88
89
    init_code = memory_read_bytes(
90
        evm.memory, memory_start_position, memory_size
91
    )
77
    if len(call_data) > MAX_INIT_CODE_SIZE:
78
        raise OutOfGasError
92
80
    create_message_gas = max_message_call_gas(Uint(evm.gas_left))
81
    evm.gas_left -= create_message_gas
82
    if evm.message.is_static:
83
        raise WriteInStaticContext
93
    evm.return_data = b""
94
86
    sender_address = evm.message.current_target
87
    sender = get_account(evm.message.tx_env.state, sender_address)
95
    # PREFLIGHT
96
    # Abort without spawning the child: nothing has been charged or
97
    # withheld for it yet.
98
    sender_address = evm.current_target
99
    sender = get_account(tx_state, sender_address)
100
101
    if (
102
        sender.balance < endowment
103
        or sender.nonce == Uint(2**64 - 1)
92
        or evm.message.depth + Uint(1) > STACK_DEPTH_LIMIT
104
        or evm.depth + Uint(1) > STACK_DEPTH_LIMIT
105
    ):
94
        evm.gas_left += create_message_gas
106
        push(evm.stack, U256(0))
107
        return
108
109
    # DESTINATION ACCESS
110
    # The account-creation charge is decided by existence alone,
111
    # independently of the collision outcome below.
112
    evm.accessed_addresses.add(contract_address)
113
114
    new_account_charged = not is_account_alive(tx_state, contract_address)
115
    if new_account_charged:
116
        charge_state_gas(evm, StateGasCosts.NEW_ACCOUNT)
117
118
    # CHILD GRANT
119
    # Withhold all but one 64th of the execution gas.
120
    create_message_gas = withhold_create_gas(evm.gas_meter)
121
122
    # On a collision the child's execution-gas grant is consumed and no
123
    # account is created. A collision target has code or a nonce, so
124
    # the account-creation charge above was never taken.
125
    if not account_deployable(tx_state, contract_address):
126
        increment_nonce(tx_state, sender_address)
127
        push(evm.stack, U256(0))
128
        return
129
98
    evm.accessed_addresses.add(contract_address)
130
    # The whole state gas reservoir rides along (no 63/64 rule for
131
    # state gas) and is restored when the child returns.
132
    create_message_state_gas_reservoir = drain_state_gas_reservoir(
133
        evm.gas_meter
134
    )
135
100
    if not account_deployable(evm.message.tx_env.state, contract_address):
101
        increment_nonce(evm.message.tx_env.state, evm.message.current_target)
102
        push(evm.stack, U256(0))
103
        return
136
    increment_nonce(tx_state, sender_address)
137
105
    increment_nonce(evm.message.tx_env.state, evm.message.current_target)
106
107
    child_message = Message(
108
        block_env=evm.message.block_env,
109
        tx_env=evm.message.tx_env,
110
        caller=evm.message.current_target,
111
        target=Bytes0(),
112
        gas=create_message_gas,
138
    # DISPATCH
139
140
    child_evm = Evm(
141
        # Context
142
        block_env=evm.block_env,
143
        tx_env=evm.tx_env,
144
        parent_evm=evm,
145
        depth=evm.depth + Uint(1),
146
        # Call Parameters
147
        caller=evm.current_target,
148
        current_target=contract_address,
149
        value=endowment,
114
        data=b"",
115
        code=call_data,
116
        current_target=contract_address,
117
        depth=evm.message.depth + Uint(1),
118
        code_address=None,
150
        call_data=b"",
151
        should_transfer_value=True,
152
        is_static=False,
153
        disable_precompiles=False,
154
        # Code
155
        code_address=None,
156
        code=init_code,
157
        valid_jump_destinations=get_valid_jump_destinations(init_code),
158
        # Machine State
159
        gas_meter=GasMeter(
160
            gas_left=create_message_gas,
161
            state_gas_left=create_message_state_gas_reservoir,
162
            state_gas_baseline=create_message_state_gas_reservoir,
163
        ),
164
        pc=Uint(0),
165
        stack=[],
166
        memory=bytearray(),
167
        return_data=b"",
168
        # Accrued Effects
169
        logs=(),
170
        accounts_to_delete=set(),
171
        accessed_addresses=evm.accessed_addresses.copy(),
122
        accessed_storage_keys=evm.accessed_storage_keys.copy(),
123
        disable_precompiles=False,
124
        parent_evm=evm,
172
        accessed_storage_keys=evm.accessed_storage_keys.copy(),
173
        # Outcome
174
        running=True,
175
        output=b"",
176
        error=None,
177
    )
126
    child_evm = process_create_message(child_message)
178
    child_evm = process_create(child_evm)
179
180
    # OUTCOME
181
    # The child settled its own gas; absorb it and resolve the
182
    # account-creation charge by the state's fate: it refills when a
183
    # charged creation failed.
184
    incorporate_child(evm, child_evm)
185
    if child_evm.error:
129
        incorporate_child_on_error(evm, child_evm)
186
        if new_account_charged:
187
            credit_state_gas_refund(evm.gas_meter, StateGasCosts.NEW_ACCOUNT)
188
        evm.return_data = child_evm.output
189
        push(evm.stack, U256(0))
190
    else:
133
        incorporate_child_on_success(evm, child_evm)
134
        evm.return_data = b""
135
        push(evm.stack, U256.from_be_bytes(child_evm.message.current_target))
191
        evm.return_data = b""
192
        push(evm.stack, U256.from_be_bytes(child_evm.current_target))

create

Creates a new account with associated code.

Parameters

evm : The current EVM frame.

def create(evm: Evm) -> None:
196
    <snip>
205
    # This import causes a circular import error
206
    # if it's not moved inside this method
207
    from ...vm.interpreter import MAX_INIT_CODE_SIZE
208
209
    if evm.is_static:
210
        raise WriteInStaticContext
211
212
    # STACK
213
    endowment = pop(evm.stack)
214
    memory_start_position = pop(evm.stack)
215
    memory_size = pop(evm.stack)
216
217
    # GAS
218
    extend_memory = calculate_gas_extend_memory(
219
        evm.memory, [(memory_start_position, memory_size)]
220
    )
221
    init_code_gas = init_code_cost(Uint(memory_size))
158
222
    charge_gas(
223
        evm,
161
        GasCosts.OPCODE_CREATE_BASE + extend_memory.cost + init_code_gas,
224
        GasCosts.CREATE_ACCESS + extend_memory.cost + init_code_gas,
225
    )
226
227
    if memory_size > U256(MAX_INIT_CODE_SIZE):
228
        raise OutOfGasError
229
230
    # OPERATION
231
    evm.memory += b"\x00" * extend_memory.expand_by
232
    contract_address = compute_contract_address(
167
        evm.message.current_target,
168
        get_account(
169
            evm.message.tx_env.state, evm.message.current_target
170
        ).nonce,
233
        evm.current_target,
234
        get_account(evm.tx_env.state, evm.current_target).nonce,
235
    )
236
237
    generic_create(
238
        evm,
239
        endowment,
240
        contract_address,
241
        memory_start_position,
242
        memory_size,
243
    )
244
245
    # PROGRAM COUNTER
246
    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:
250
    <snip>
262
    # This import causes a circular import error
263
    # if it's not moved inside this method
264
    from ...vm.interpreter import MAX_INIT_CODE_SIZE
265
266
    if evm.is_static:
267
        raise WriteInStaticContext
268
269
    # STACK
270
    endowment = pop(evm.stack)
271
    memory_start_position = pop(evm.stack)
272
    memory_size = pop(evm.stack)
273
    salt = pop(evm.stack).to_be_bytes32()
274
275
    # GAS
276
    extend_memory = calculate_gas_extend_memory(
277
        evm.memory, [(memory_start_position, memory_size)]
278
    )
279
    call_data_words = ceil32(Uint(memory_size)) // Uint(32)
280
    init_code_gas = init_code_cost(Uint(memory_size))
281
    charge_gas(
282
        evm,
212
        GasCosts.OPCODE_CREATE_BASE
213
        + GasCosts.OPCODE_KECCAK256_PER_WORD * call_data_words
214
        + extend_memory.cost
215
        + init_code_gas,
283
        ExecutionGas(
284
            GasCosts.CREATE_ACCESS
285
            + GasCosts.OPCODE_KECCAK256_PER_WORD * call_data_words
286
            + extend_memory.cost
287
            + init_code_gas
288
        ),
289
    )
290
291
    if memory_size > U256(MAX_INIT_CODE_SIZE):
292
        raise OutOfGasError
293
294
    # OPERATION
295
    evm.memory += b"\x00" * extend_memory.expand_by
296
    contract_address = compute_create2_contract_address(
221
        evm.message.current_target,
297
        evm.current_target,
298
        salt,
299
        memory_read_bytes(evm.memory, memory_start_position, memory_size),
300
    )
301
302
    generic_create(
303
        evm,
304
        endowment,
305
        contract_address,
306
        memory_start_position,
307
        memory_size,
308
    )
309
310
    # PROGRAM COUNTER
311
    evm.pc += Uint(1)

return_

Halts execution returning output data.

Parameters

evm : The current EVM frame.

def return_(evm: Evm) -> None:
315
    <snip>
324
    # STACK
325
    memory_start_position = pop(evm.stack)
326
    memory_size = pop(evm.stack)
327
328
    # GAS
329
    extend_memory = calculate_gas_extend_memory(
330
        evm.memory, [(memory_start_position, memory_size)]
331
    )
332
333
    charge_gas(evm, GasCosts.ZERO + extend_memory.cost)
334
335
    # OPERATION
336
    evm.memory += b"\x00" * extend_memory.expand_by
337
    evm.output = memory_read_bytes(
338
        evm.memory, memory_start_position, memory_size
339
    )
340
341
    evm.running = False
342
343
    # PROGRAM COUNTER
344
    pass

GenericCall

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

347
@final
348
@dataclass
class GenericCall:

gas

278
    gas: Uint
354
    gas: ExecutionGas

state_gas_reservoir

355
    state_gas_reservoir: StateGas

value

356
    value: U256

caller

357
    caller: Address

to

358
    to: Address

code_address

359
    code_address: Address

should_transfer_value

360
    should_transfer_value: bool

is_staticcall

361
    is_staticcall: bool

memory_input_start_position

362
    memory_input_start_position: U256

memory_input_size

363
    memory_input_size: U256

memory_output_start_position

364
    memory_output_start_position: U256

memory_output_size

365
    memory_output_size: U256

code

366
    code: Bytes

disable_precompiles

367
    disable_precompiles: bool

new_account_charged

368
    new_account_charged: bool = False

insufficient_balance

True when the calling account cannot cover value; the call then aborts in preflight without spawning the child frame.

369
    insufficient_balance: bool = False

generic_call

Perform the core logic of the Run the child-frame lifecycle for the CALL* family of opcodes.

The opcode has already priced the call and withheld the child's grant; this function only runs the lifecycle: preflight checks that abort without spawning, the child frame itself, and the resolution of its outcome back into the calling frame.

def generic_call(evm: Evm, ​​params: GenericCall) -> None:
377
    <snip>
297
    from ...vm.interpreter import STACK_DEPTH_LIMIT, process_message
385
    from ...vm.interpreter import STACK_DEPTH_LIMIT, process_call
386
    from ...vm.runtime import get_valid_jump_destinations
387
388
    evm.return_data = b""
389
301
    if evm.message.depth + Uint(1) > STACK_DEPTH_LIMIT:
302
        evm.gas_left += params.gas
390
    # PREFLIGHT
391
    # Abort without spawning the child: both grants return untouched
392
    # and any account-creation charge refills.
393
    if evm.depth + Uint(1) > STACK_DEPTH_LIMIT or params.insufficient_balance:
394
        restore_child_gas(
395
            evm.gas_meter, params.gas, params.state_gas_reservoir
396
        )
397
        if params.new_account_charged:
398
            credit_state_gas_refund(evm.gas_meter, StateGasCosts.NEW_ACCOUNT)
399
        push(evm.stack, U256(0))
400
        return
401
306
    call_data = memory_read_bytes(
402
    # DISPATCH
403
    call_data = memory_read_bytes(
404
        evm.memory,
405
        params.memory_input_start_position,
406
        params.memory_input_size,
407
    )
408
312
    child_message = Message(
313
        block_env=evm.message.block_env,
314
        tx_env=evm.message.tx_env,
409
    child_evm = Evm(
410
        # Context
411
        block_env=evm.block_env,
412
        tx_env=evm.tx_env,
413
        parent_evm=evm,
414
        depth=evm.depth + Uint(1),
415
        # Call Parameters
416
        caller=params.caller,
316
        target=params.to,
317
        gas=params.gas,
417
        current_target=params.to,
418
        value=params.value,
319
        data=call_data,
419
        call_data=call_data,
420
        should_transfer_value=params.should_transfer_value,
421
        is_static=params.is_staticcall or evm.is_static,
422
        disable_precompiles=params.disable_precompiles,
423
        # Code
424
        code_address=params.code_address,
425
        code=params.code,
321
        current_target=params.to,
322
        depth=evm.message.depth + Uint(1),
323
        code_address=params.code_address,
324
        should_transfer_value=params.should_transfer_value,
325
        is_static=params.is_staticcall or evm.message.is_static,
426
        valid_jump_destinations=get_valid_jump_destinations(params.code),
427
        # Machine State
428
        gas_meter=GasMeter(
429
            gas_left=params.gas,
430
            state_gas_left=params.state_gas_reservoir,
431
            state_gas_baseline=params.state_gas_reservoir,
432
        ),
433
        pc=Uint(0),
434
        stack=[],
435
        memory=bytearray(),
436
        return_data=b"",
437
        # Accrued Effects
438
        logs=(),
439
        accounts_to_delete=set(),
440
        accessed_addresses=evm.accessed_addresses.copy(),
327
        accessed_storage_keys=evm.accessed_storage_keys.copy(),
328
        disable_precompiles=params.disable_precompiles,
329
        parent_evm=evm,
441
        accessed_storage_keys=evm.accessed_storage_keys.copy(),
442
        # Outcome
443
        running=True,
444
        output=b"",
445
        error=None,
446
    )
331
    child_evm = process_message(child_message)
447
448
    child_evm = process_call(child_evm)
449
450
    # OUTCOME
451
    # The child settled its own gas; absorb it and resolve the
452
    # account-creation charge by the state's fate.
453
    incorporate_child(evm, child_evm)
454
    evm.return_data = child_evm.output
455
    if child_evm.error:
334
        incorporate_child_on_error(evm, child_evm)
335
        evm.return_data = child_evm.output
456
        if params.new_account_charged:
457
            credit_state_gas_refund(evm.gas_meter, StateGasCosts.NEW_ACCOUNT)
458
        push(evm.stack, U256(0))
459
    else:
338
        incorporate_child_on_success(evm, child_evm)
339
        evm.return_data = child_evm.output
340
        push(evm.stack, U256(1))
460
        push(evm.stack, CALL_SUCCESS)
461
462
    actual_output_size = min(
463
        params.memory_output_size, U256(len(child_evm.output))
464
    )
465
    memory_write(
466
        evm.memory,
467
        params.memory_output_start_position,
468
        child_evm.output[:actual_output_size],
469
    )

call

Message-call into an account.

Parameters

evm : The current EVM frame.

def call(evm: Evm) -> None:
473
    <snip>
482
    # STACK
363
    gas = Uint(pop(evm.stack))
483
    gas = ExecutionGas(Uint(pop(evm.stack)))
484
    to = to_address_masked(pop(evm.stack))
485
    value = pop(evm.stack)
486
    memory_input_start_position = pop(evm.stack)
487
    memory_input_size = pop(evm.stack)
488
    memory_output_start_position = pop(evm.stack)
489
    memory_output_size = pop(evm.stack)
490
371
    # GAS
491
    if evm.is_static and value != U256(0):
492
        raise WriteInStaticContext
493
494
    # GAS (STATE-INDEPENDENT)
495
    # Price what is computable without touching state, and check it is
496
    # affordable before any state access is performed.
497
    extend_memory = calculate_gas_extend_memory(
498
        evm.memory,
499
        [
500
            (memory_input_start_position, memory_input_size),
501
            (memory_output_start_position, memory_output_size),
502
        ],
503
    )
504
380
    if to in evm.accessed_addresses:
381
        access_gas_cost = GasCosts.WARM_ACCESS
505
    is_cold_access = to not in evm.accessed_addresses
506
    if is_cold_access:
507
        access_gas_cost = GasCosts.COLD_ACCOUNT_ACCESS
508
    else:
383
        evm.accessed_addresses.add(to)
384
        access_gas_cost = GasCosts.COLD_ACCOUNT_ACCESS
509
        access_gas_cost = GasCosts.WARM_ACCESS
510
386
    code_address = to
511
    transfer_gas_cost = GasCosts.ZERO if value == 0 else GasCosts.CALL_VALUE
512
513
    check_gas(
514
        evm,
515
        access_gas_cost + transfer_gas_cost + extend_memory.cost,
516
    )
517
518
    # STATE ACCESS (STATE-DEPENDENT GAS)
519
    # Perform the accesses and complete the state-dependent pricing --
520
    # a delegation adds its access cost -- then charge the execution
521
    # gas.
522
    tx_state = evm.tx_env.state
523
    if is_cold_access:
524
        evm.accessed_addresses.add(to)
525
526
    extra_gas = access_gas_cost + transfer_gas_cost
527
    (
388
        disable_precompiles,
528
        is_delegated,
529
        code_address,
390
        code,
391
        delegated_access_gas_cost,
392
    ) = access_delegation(evm, code_address)
393
    access_gas_cost += delegated_access_gas_cost
530
        delegation_access_cost,
531
    ) = calculate_delegation_cost(evm, to)
532
395
    create_gas_cost = GasCosts.NEW_ACCOUNT
396
    if value == 0 or is_account_alive(evm.message.tx_env.state, to):
397
        create_gas_cost = Uint(0)
398
    transfer_gas_cost = Uint(0) if value == 0 else GasCosts.CALL_VALUE
399
    message_call_gas = calculate_message_call_gas(
533
    if is_delegated:
534
        # check enough gas for delegation access
535
        extra_gas += delegation_access_cost
536
        check_gas(evm, extra_gas + extend_memory.cost)
537
        if code_address not in evm.accessed_addresses:
538
            evm.accessed_addresses.add(code_address)
539
540
    code_hash = get_account(tx_state, code_address).code_hash
541
    code = get_code(tx_state, code_hash)
542
543
    charge_gas(evm, extra_gas + extend_memory.cost)
544
545
    # STATE GAS
546
    # A value transfer that will create the recipient is charged by
547
    # the frame whose opcode causes it; refilled in `generic_call`
548
    # whenever the creation fails or never happens.
549
    has_value = value != 0
550
    new_account_charged = has_value and not is_account_alive(tx_state, to)
551
    if new_account_charged:
552
        charge_state_gas(evm, StateGasCosts.NEW_ACCOUNT)
553
554
    # CHILD GRANT
555
    # Computed after every charge above, so any state-gas spill has
556
    # already thinned `gas_left`. The whole reservoir rides along (no
557
    # 63/64 rule for state gas).
558
    message_call_gas = calculate_message_call_gas(
559
        value,
560
        gas,
402
        Uint(evm.gas_left),
403
        memory_cost=extend_memory.cost,
404
        extra_gas=access_gas_cost + create_gas_cost + transfer_gas_cost,
561
        evm.gas_meter.gas_left,
562
        memory_cost=GasCosts.ZERO,
563
        extra_gas=GasCosts.ZERO,
564
    )
406
    charge_gas(evm, message_call_gas.cost + extend_memory.cost)
407
    if evm.message.is_static and value != U256(0):
408
        raise WriteInStaticContext
409
    evm.memory += b"\x00" * extend_memory.expand_by
410
    sender_balance = get_account(
411
        evm.message.tx_env.state, evm.message.current_target
412
    ).balance
413
    if sender_balance < value:
414
        push(evm.stack, U256(0))
415
        evm.return_data = b""
416
        evm.gas_left += message_call_gas.sub_call
417
    else:
418
        generic_call(
419
            evm,
420
            GenericCall(
421
                gas=message_call_gas.sub_call,
422
                value=value,
423
                caller=evm.message.current_target,
424
                to=to,
425
                code_address=code_address,
426
                should_transfer_value=True,
427
                is_staticcall=False,
428
                memory_input_start_position=memory_input_start_position,
429
                memory_input_size=memory_input_size,
430
                memory_output_start_position=memory_output_start_position,
431
                memory_output_size=memory_output_size,
432
                code=code,
433
                disable_precompiles=disable_precompiles,
434
            ),
435
        )
565
    charge_gas(evm, message_call_gas.cost)
566
    call_state_gas_reservoir = drain_state_gas_reservoir(evm.gas_meter)
567
568
    # OPERATION
569
    evm.memory += b"\x00" * extend_memory.expand_by
570
571
    sender_balance = get_account(tx_state, evm.current_target).balance
572
573
    generic_call(
574
        evm,
575
        GenericCall(
576
            gas=message_call_gas.sub_call,
577
            state_gas_reservoir=call_state_gas_reservoir,
578
            value=value,
579
            caller=evm.current_target,
580
            to=to,
581
            code_address=code_address,
582
            should_transfer_value=True,
583
            is_staticcall=False,
584
            memory_input_start_position=memory_input_start_position,
585
            memory_input_size=memory_input_size,
586
            memory_output_start_position=memory_output_start_position,
587
            memory_output_size=memory_output_size,
588
            code=code,
589
            disable_precompiles=is_delegated,
590
            new_account_charged=new_account_charged,
591
            insufficient_balance=sender_balance < value,
592
        ),
593
    )
594
595
    # PROGRAM COUNTER
596
    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:
600
    <snip>
609
    # STACK
452
    gas = Uint(pop(evm.stack))
610
    gas = ExecutionGas(Uint(pop(evm.stack)))
611
    code_address = to_address_masked(pop(evm.stack))
612
    value = pop(evm.stack)
613
    memory_input_start_position = pop(evm.stack)
614
    memory_input_size = pop(evm.stack)
615
    memory_output_start_position = pop(evm.stack)
616
    memory_output_size = pop(evm.stack)
617
460
    # GAS
461
    to = evm.message.current_target
618
    # GAS (STATE-INDEPENDENT)
619
    # Price what is computable without touching state, and check it is
620
    # affordable before any state access is performed.
621
    to = evm.current_target
622
623
    extend_memory = calculate_gas_extend_memory(
624
        evm.memory,
625
        [
626
            (memory_input_start_position, memory_input_size),
627
            (memory_output_start_position, memory_output_size),
628
        ],
629
    )
630
471
    if code_address in evm.accessed_addresses:
472
        access_gas_cost = GasCosts.WARM_ACCESS
631
    is_cold_access = code_address not in evm.accessed_addresses
632
    if is_cold_access:
633
        access_gas_cost = GasCosts.COLD_ACCOUNT_ACCESS
634
    else:
474
        evm.accessed_addresses.add(code_address)
475
        access_gas_cost = GasCosts.COLD_ACCOUNT_ACCESS
635
        access_gas_cost = GasCosts.WARM_ACCESS
636
637
    transfer_gas_cost = GasCosts.ZERO if value == 0 else GasCosts.CALL_VALUE
638
639
    check_gas(
640
        evm,
641
        access_gas_cost + extend_memory.cost + transfer_gas_cost,
642
    )
643
644
    # STATE ACCESS (STATE-DEPENDENT GAS)
645
    # Perform the accesses and complete the state-dependent pricing --
646
    # a delegation adds its access cost; the execution gas is charged
647
    # with the child grant.
648
    tx_state = evm.tx_env.state
649
    if is_cold_access:
650
        evm.accessed_addresses.add(code_address)
651
652
    extra_gas = access_gas_cost + transfer_gas_cost
653
    (
478
        disable_precompiles,
654
        is_delegated,
655
        code_address,
480
        code,
481
        delegated_access_gas_cost,
482
    ) = access_delegation(evm, code_address)
483
    access_gas_cost += delegated_access_gas_cost
656
        delegation_access_cost,
657
    ) = calculate_delegation_cost(evm, code_address)
658
485
    transfer_gas_cost = Uint(0) if value == 0 else GasCosts.CALL_VALUE
486
    message_call_gas = calculate_message_call_gas(
659
    if is_delegated:
660
        # check enough gas for delegation access
661
        extra_gas += delegation_access_cost
662
        check_gas(evm, extra_gas + extend_memory.cost)
663
        if code_address not in evm.accessed_addresses:
664
            evm.accessed_addresses.add(code_address)
665
666
    code_hash = get_account(tx_state, code_address).code_hash
667
    code = get_code(tx_state, code_hash)
668
669
    # CHILD GRANT
670
    # Charge the call's cost and withhold the child's execution gas
671
    # share in one step. The whole reservoir rides along (no 63/64
672
    # rule for state gas).
673
    message_call_gas = calculate_message_call_gas(
674
        value,
675
        gas,
489
        Uint(evm.gas_left),
676
        evm.gas_meter.gas_left,
677
        extend_memory.cost,
491
        access_gas_cost + transfer_gas_cost,
678
        extra_gas,
679
    )
680
    charge_gas(evm, message_call_gas.cost + extend_memory.cost)
681
    call_state_gas_reservoir = drain_state_gas_reservoir(evm.gas_meter)
682
683
    # OPERATION
684
    evm.memory += b"\x00" * extend_memory.expand_by
497
    sender_balance = get_account(
498
        evm.message.tx_env.state, evm.message.current_target
499
    ).balance
500
    if sender_balance < value:
501
        push(evm.stack, U256(0))
502
        evm.return_data = b""
503
        evm.gas_left += message_call_gas.sub_call
504
    else:
505
        generic_call(
506
            evm,
507
            GenericCall(
508
                gas=message_call_gas.sub_call,
509
                value=value,
510
                caller=evm.message.current_target,
511
                to=to,
512
                code_address=code_address,
513
                should_transfer_value=True,
514
                is_staticcall=False,
515
                memory_input_start_position=memory_input_start_position,
516
                memory_input_size=memory_input_size,
517
                memory_output_start_position=memory_output_start_position,
518
                memory_output_size=memory_output_size,
519
                code=code,
520
                disable_precompiles=disable_precompiles,
521
            ),
522
        )
685
686
    sender_balance = get_account(tx_state, evm.current_target).balance
687
688
    generic_call(
689
        evm,
690
        GenericCall(
691
            gas=message_call_gas.sub_call,
692
            state_gas_reservoir=call_state_gas_reservoir,
693
            value=value,
694
            caller=evm.current_target,
695
            to=to,
696
            code_address=code_address,
697
            should_transfer_value=True,
698
            is_staticcall=False,
699
            memory_input_start_position=memory_input_start_position,
700
            memory_input_size=memory_input_size,
701
            memory_output_start_position=memory_output_start_position,
702
            memory_output_size=memory_output_size,
703
            code=code,
704
            disable_precompiles=is_delegated,
705
            insufficient_balance=sender_balance < value,
706
        ),
707
    )
708
709
    # PROGRAM COUNTER
710
    evm.pc += Uint(1)

selfdestruct

Halt execution and register account for later deletion.

Parameters

evm : The current EVM frame.

def selfdestruct(evm: Evm) -> None:
714
    <snip>
723
    if evm.is_static:
724
        raise WriteInStaticContext
725
726
    # STACK
727
    beneficiary = to_address_masked(pop(evm.stack))
728
541
    # GAS
729
    # GAS (STATE-INDEPENDENT)
730
    # Price what is computable without touching state, and check it is
731
    # affordable before any state access is performed.
732
    gas_cost = GasCosts.OPCODE_SELFDESTRUCT_BASE
543
    if beneficiary not in evm.accessed_addresses:
544
        evm.accessed_addresses.add(beneficiary)
545
        gas_cost += GasCosts.COLD_ACCOUNT_ACCESS
733
734
    is_cold_access = beneficiary not in evm.accessed_addresses
735
    if is_cold_access:
736
        gas_cost += GasCosts.COLD_ACCOUNT_ACCESS
737
738
    check_gas(evm, gas_cost)
739
740
    # STATE ACCESS (STATE-DEPENDENT GAS)
741
    # Perform the access; the pricing completes with the state gas
742
    # below.
743
    tx_state = evm.tx_env.state
744
    if is_cold_access:
745
        evm.accessed_addresses.add(beneficiary)
746
747
    # STATE GAS
748
    # A sweep that will create the beneficiary pays the account write
749
    # and the creation, charged by the frame whose opcode causes it;
750
    # it refills only through the frame's own rollback.
751
    state_gas = StateGas(Uint(0))
752
    account_write_gas = GasCosts.ZERO
753
    if (
548
        not is_account_alive(evm.message.tx_env.state, beneficiary)
549
        and get_account(
550
            evm.message.tx_env.state, evm.message.current_target
551
        ).balance
552
        != 0
754
        not is_account_alive(tx_state, beneficiary)
755
        and get_account(tx_state, evm.current_target).balance != 0
756
    ):
554
        gas_cost += GasCosts.OPCODE_SELFDESTRUCT_NEW_ACCOUNT
757
        state_gas = StateGasCosts.NEW_ACCOUNT
758
        account_write_gas = GasCosts.ACCOUNT_WRITE
759
556
    charge_gas(evm, gas_cost)
557
    if evm.message.is_static:
558
        raise WriteInStaticContext
760
    # Charge execution gas before state gas so that an execution-gas
761
    # OOG does not consume state gas that would inflate the parent's
762
    # reservoir on frame failure.
763
    charge_gas(evm, gas_cost + account_write_gas)
764
    charge_state_gas(evm, state_gas)
765
560
    originator = evm.message.current_target
561
    originator_balance = get_account(
562
        evm.message.tx_env.state, originator
563
    ).balance
766
    # OPERATION
767
    originator = evm.current_target
768
    originator_balance = get_account(tx_state, originator).balance
769
565
    move_ether(
566
        evm.message.tx_env.state,
567
        originator,
568
        beneficiary,
569
        originator_balance,
570
    )
770
    # Transfer balance
771
    move_ether(tx_state, originator, beneficiary, originator_balance)
772
572
    # register account for deletion only if it was created
573
    # in the same transaction
574
    if originator in evm.message.tx_env.state.created_accounts:
575
        # If beneficiary is the same as originator, then
576
        # the ether is burnt.
577
        set_account_balance(evm.message.tx_env.state, originator, U256(0))
578
        evm.accounts_to_delete.add(originator)
773
    # Emit transfer log
774
    if beneficiary != originator:
775
        emit_transfer_log(evm, originator, beneficiary, originator_balance)
776
777
    # Register account for deletion iff created in same transaction
778
    if originator in tx_state.created_accounts:
779
        evm.accounts_to_delete.add(originator)
780
781
    # HALT the execution
782
    evm.running = False
783
784
    # PROGRAM COUNTER
785
    pass

delegatecall

Message-call into an account.

Parameters

evm : The current EVM frame.

def delegatecall(evm: Evm) -> None:
789
    <snip>
798
    # STACK
598
    gas = Uint(pop(evm.stack))
799
    gas = ExecutionGas(Uint(pop(evm.stack)))
800
    code_address = to_address_masked(pop(evm.stack))
801
    memory_input_start_position = pop(evm.stack)
802
    memory_input_size = pop(evm.stack)
803
    memory_output_start_position = pop(evm.stack)
804
    memory_output_size = pop(evm.stack)
805
605
    # GAS
806
    # GAS (STATE-INDEPENDENT)
807
    # Price what is computable without touching state, and check it is
808
    # affordable before any state access is performed.
809
    extend_memory = calculate_gas_extend_memory(
810
        evm.memory,
811
        [
812
            (memory_input_start_position, memory_input_size),
813
            (memory_output_start_position, memory_output_size),
814
        ],
815
    )
816
614
    if code_address in evm.accessed_addresses:
615
        access_gas_cost = GasCosts.WARM_ACCESS
817
    is_cold_access = code_address not in evm.accessed_addresses
818
    if is_cold_access:
819
        access_gas_cost = GasCosts.COLD_ACCOUNT_ACCESS
820
    else:
617
        evm.accessed_addresses.add(code_address)
618
        access_gas_cost = GasCosts.COLD_ACCOUNT_ACCESS
821
        access_gas_cost = GasCosts.WARM_ACCESS
822
823
    check_gas(evm, access_gas_cost + extend_memory.cost)
824
825
    # STATE ACCESS (STATE-DEPENDENT GAS)
826
    # Perform the accesses and complete the state-dependent pricing --
827
    # a delegation adds its access cost; the execution gas is charged
828
    # with the child grant.
829
    if is_cold_access:
830
        evm.accessed_addresses.add(code_address)
831
832
    extra_gas = access_gas_cost
833
    (
621
        disable_precompiles,
834
        is_delegated,
835
        code_address,
623
        code,
624
        delegated_access_gas_cost,
625
    ) = access_delegation(evm, code_address)
626
    access_gas_cost += delegated_access_gas_cost
836
        delegation_access_cost,
837
    ) = calculate_delegation_cost(evm, code_address)
838
628
    message_call_gas = calculate_message_call_gas(
629
        U256(0), gas, Uint(evm.gas_left), extend_memory.cost, access_gas_cost
839
    if is_delegated:
840
        # check enough gas for delegation access
841
        extra_gas += delegation_access_cost
842
        check_gas(evm, extra_gas + extend_memory.cost)
843
        if code_address not in evm.accessed_addresses:
844
            evm.accessed_addresses.add(code_address)
845
846
    tx_state = evm.tx_env.state
847
    code_hash = get_account(tx_state, code_address).code_hash
848
    code = get_code(tx_state, code_hash)
849
850
    # CHILD GRANT
851
    # Charge the call's cost and withhold the child's execution gas
852
    # share in one step. The whole reservoir rides along (no 63/64
853
    # rule for state gas).
854
    message_call_gas = calculate_message_call_gas(
855
        U256(0),
856
        gas,
857
        evm.gas_meter.gas_left,
858
        extend_memory.cost,
859
        extra_gas,
860
    )
861
    charge_gas(evm, message_call_gas.cost + extend_memory.cost)
862
    call_state_gas_reservoir = drain_state_gas_reservoir(evm.gas_meter)
863
864
    # OPERATION
865
    evm.memory += b"\x00" * extend_memory.expand_by
866
867
    generic_call(
868
        evm,
869
        GenericCall(
870
            gas=message_call_gas.sub_call,
639
            value=evm.message.value,
640
            caller=evm.message.caller,
641
            to=evm.message.current_target,
871
            state_gas_reservoir=call_state_gas_reservoir,
872
            value=evm.value,
873
            caller=evm.caller,
874
            to=evm.current_target,
875
            code_address=code_address,
876
            should_transfer_value=False,
877
            is_staticcall=False,
878
            memory_input_start_position=memory_input_start_position,
879
            memory_input_size=memory_input_size,
880
            memory_output_start_position=memory_output_start_position,
881
            memory_output_size=memory_output_size,
882
            code=code,
650
            disable_precompiles=disable_precompiles,
883
            disable_precompiles=is_delegated,
884
        ),
885
    )
886
887
    # PROGRAM COUNTER
888
    evm.pc += Uint(1)

staticcall

Message-call into an account.

Parameters

evm : The current EVM frame.

def staticcall(evm: Evm) -> None:
892
    <snip>
901
    # STACK
669
    gas = Uint(pop(evm.stack))
902
    gas = ExecutionGas(Uint(pop(evm.stack)))
903
    to = to_address_masked(pop(evm.stack))
904
    memory_input_start_position = pop(evm.stack)
905
    memory_input_size = pop(evm.stack)
906
    memory_output_start_position = pop(evm.stack)
907
    memory_output_size = pop(evm.stack)
908
676
    # GAS
909
    # GAS (STATE-INDEPENDENT)
910
    # Price what is computable without touching state, and check it is
911
    # affordable before any state access is performed.
912
    extend_memory = calculate_gas_extend_memory(
913
        evm.memory,
914
        [
915
            (memory_input_start_position, memory_input_size),
916
            (memory_output_start_position, memory_output_size),
917
        ],
918
    )
919
685
    if to in evm.accessed_addresses:
686
        access_gas_cost = GasCosts.WARM_ACCESS
920
    is_cold_access = to not in evm.accessed_addresses
921
    if is_cold_access:
922
        access_gas_cost = GasCosts.COLD_ACCOUNT_ACCESS
923
    else:
688
        evm.accessed_addresses.add(to)
689
        access_gas_cost = GasCosts.COLD_ACCOUNT_ACCESS
924
        access_gas_cost = GasCosts.WARM_ACCESS
925
691
    code_address = to
926
    check_gas(evm, access_gas_cost + extend_memory.cost)
927
928
    # STATE ACCESS (STATE-DEPENDENT GAS)
929
    # Perform the accesses and complete the state-dependent pricing --
930
    # a delegation adds its access cost; the execution gas is charged
931
    # with the child grant.
932
    if is_cold_access:
933
        evm.accessed_addresses.add(to)
934
935
    extra_gas = access_gas_cost
936
    (
693
        disable_precompiles,
937
        is_delegated,
938
        code_address,
695
        code,
696
        delegated_access_gas_cost,
697
    ) = access_delegation(evm, code_address)
698
    access_gas_cost += delegated_access_gas_cost
939
        delegation_access_cost,
940
    ) = calculate_delegation_cost(evm, to)
941
700
    message_call_gas = calculate_message_call_gas(
942
    if is_delegated:
943
        # check enough gas for delegation access
944
        extra_gas += delegation_access_cost
945
        check_gas(evm, extra_gas + extend_memory.cost)
946
        if code_address not in evm.accessed_addresses:
947
            evm.accessed_addresses.add(code_address)
948
949
    tx_state = evm.tx_env.state
950
    code_hash = get_account(tx_state, code_address).code_hash
951
    code = get_code(tx_state, code_hash)
952
953
    # CHILD GRANT
954
    # Charge the call's cost and withhold the child's execution gas
955
    # share in one step. The whole reservoir rides along (no 63/64
956
    # rule for state gas).
957
    message_call_gas = calculate_message_call_gas(
958
        U256(0),
959
        gas,
703
        Uint(evm.gas_left),
960
        evm.gas_meter.gas_left,
961
        extend_memory.cost,
705
        access_gas_cost,
962
        extra_gas,
963
    )
964
    charge_gas(evm, message_call_gas.cost + extend_memory.cost)
965
    call_state_gas_reservoir = drain_state_gas_reservoir(evm.gas_meter)
966
967
    # OPERATION
968
    evm.memory += b"\x00" * extend_memory.expand_by
969
970
    generic_call(
971
        evm,
972
        GenericCall(
973
            gas=message_call_gas.sub_call,
974
            state_gas_reservoir=call_state_gas_reservoir,
975
            value=U256(0),
716
            caller=evm.message.current_target,
976
            caller=evm.current_target,
977
            to=to,
978
            code_address=code_address,
979
            should_transfer_value=True,
980
            is_staticcall=True,
981
            memory_input_start_position=memory_input_start_position,
982
            memory_input_size=memory_input_size,
983
            memory_output_start_position=memory_output_start_position,
984
            memory_output_size=memory_output_size,
985
            code=code,
726
            disable_precompiles=disable_precompiles,
986
            disable_precompiles=is_delegated,
987
        ),
988
    )
989
990
    # PROGRAM COUNTER
991
    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:
995
    <snip>
1005
    # STACK
1006
    memory_start_index = pop(evm.stack)
1007
    size = pop(evm.stack)
1008
1009
    # GAS
1010
    extend_memory = calculate_gas_extend_memory(
1011
        evm.memory, [(memory_start_index, size)]
1012
    )
1013
1014
    charge_gas(evm, extend_memory.cost)
1015
1016
    # OPERATION
1017
    evm.memory += b"\x00" * extend_memory.expand_by
1018
    output = memory_read_bytes(evm.memory, memory_start_index, size)
1019
    evm.output = Bytes(output)
1020
    raise Revert
1021
1022
    # PROGRAM COUNTER
1023
    # no-op