ethereum.forks.bogota.vm.frame_interpreter

Execute the frames of an EIP-8141 frame transaction.

Where a regular transaction describes a single top-level call, a frame transaction describes a list of frames that process_frames executes in order, each as its own top-level call with a fresh gas meter holding the frame's gas limit. The frames share the transaction's state, a FrameJournal of the effects accrued across frames, and the approval context that the APPROVE instruction advances.

FrameJournal

Effects accrued across the frames of a frame transaction.

Each finished frame's contributions are incorporated here: the warm journal that successful frames feed, and the refunds and scheduled deletions that settle the transaction once the last frame has run. Everything gas-shaped settles from the frame receipts instead. When an atomic batch opens, a copy of the journal joins the batch's rollback point; unrolling the batch resumes from that copy.

66
@final
67
@dataclass
class FrameJournal:

warm_addresses

Addresses left warm for later frames by successful frames.

81
    warm_addresses: Set[Address]

warm_storage_keys

Storage keys left warm for later frames by successful frames.

86
    warm_storage_keys: Set[Tuple[Address, Bytes32]]

refund_counter

Refunds accrued by successful frames.

91
    refund_counter: int

accounts_to_delete

Accounts scheduled for deletion by successful frames.

96
    accounts_to_delete: Set[Address]

copy_frame_journal

Return an independent copy of the journal, safe to keep as a rollback point while the original continues to accrue.

def copy_frame_journal(​journal: FrameJournal​) -> FrameJournal:
103
    <snip>
107
    return FrameJournal(
108
        warm_addresses=set(journal.warm_addresses),
109
        warm_storage_keys=set(journal.warm_storage_keys),
110
        refund_counter=journal.refund_counter,
111
        accounts_to_delete=set(journal.accounts_to_delete),
112
    )

AtomicBatch

Rollback point captured when an atomic batch opens.

A frame carrying the atomic batch flag opens a batch that runs up to and including the next frame without the flag. When a batch frame fails, unroll_atomic_batch restores everything captured here: one snapshot per rollback domain — the transaction state, the frame context, and the frame journal.

115
@final
116
@dataclass
class AtomicBatch:

first_frame_index

Index of the frame that opened the batch.

128
    first_frame_index: Uint

state_snapshot

Copy of the transaction state taken before the batch began.

133
    state_snapshot: TransactionState

context_snapshot

Copy of the frame context taken before the batch began: the approval fields, the receipts of the pre-batch frames — restoring them undoes the refills the batch's frames applied to pre-batch state charges — and the outstanding-charge ownership map.

138
    context_snapshot: FrameContext

journal

Copy of the frame journal taken before the batch began.

146
    journal: FrameJournal

FrameOutcome

Reduced outcome of a single frame.

A finished frame's EVM is read once and immediately reduced to this record: the consensus receipt entry, plus the quantities the frame contributes to the transaction's settlement that the receipt does not carry. Everything gas-shaped is derived from the receipt: the frame's unused gas, in either dimension, is its budget less its receipt's usage.

152
@final
153
@dataclass
class FrameOutcome:

receipt

The frame's receipt entry.

166
    receipt: FrameReceipt

refund_counter

Refunds the frame accrued; zero unless the frame succeeded.

171
    refund_counter: int

accounts_to_delete

Accounts the frame scheduled for deletion; empty unless the frame succeeded.

176
    accounts_to_delete: Set[Address]

incorporate_frame_outcome

Incorporate a finished frame's settlement quantities into the journal.

The frame's warm accesses are not carried on the outcome: execute_frame commits them into the journal only when the frame succeeds.

def incorporate_frame_outcome(​journal: FrameJournal, ​​outcome: FrameOutcome​) -> None:
186
    <snip>
194
    journal.refund_counter += outcome.refund_counter
195
    journal.accounts_to_delete |= outcome.accounts_to_delete

unroll_atomic_batch

Unroll a failed atomic batch.

The transaction state and the frame context are restored to the condition immediately before the batch began — undoing, with the batch's state changes, the refills its frames applied to pre-batch receipts — and the receipts of the executed batch frames are re-appended keeping their status and execution gas, with their logs emptied and their state gas zeroed. Return the batch's journal copy for the frame loop to continue from; the gas the batch frames consumed remains charged, since their receipts keep it.

def unroll_atomic_batch(​tx_env: TransactionEnvironment, ​​batch: AtomicBatch​) -> FrameJournal:
202
    <snip>
215
    frame_context = tx_env.frame_context
216
    assert frame_context is not None
217
218
    # Static validity bans approval scopes on batch frames, so the
219
    # unroll can never move the approval context.
220
    assert frame_context.payer == batch.context_snapshot.payer
221
    assert (
222
        frame_context.sender_approved == batch.context_snapshot.sender_approved
223
    )
224
225
    executed_batch_receipts = frame_context.frame_receipts[
226
        int(batch.first_frame_index) :
227
    ]
228
229
    restore_tx_state(tx_env.state, batch.state_snapshot)
230
    restore_frame_context(tx_env, batch.context_snapshot)
231
232
    for receipt in executed_batch_receipts:
233
        frame_context.frame_receipts.append(
234
            replace(
235
                receipt,
236
                gas_used=replace(receipt.gas_used, state=Uint(0)),
237
                logs=(),
238
            )
239
        )
240
241
    return batch.journal

create_evm_from_frame

Build a frame's top-level EVM.

The access sets arrive from execute_frame, which seeded them from the journal shared across frames and charged the resolved target's warm or cold access into them at frame entry. Charged here are the frame's remaining entry costs: the access for resolving an EIP-7702 delegation, from the frame's own execution gas budget, and the state gas for a value transfer reviving a dead account — after the caller's balance check and before the frame's code executes. A charge exceeding either budget raises instead of building the EVM, failing the frame.

def create_evm_from_frame(​block_env: BlockEnvironment, ​​tx_env: TransactionEnvironment, ​​frame: Frame, ​​resolved_target: Address, ​​gas_meter: GasMeter, ​​accessed_addresses: Set[Address], ​​accessed_storage_keys: Set[Tuple[Address, Bytes32]]​) -> Evm:
253
    <snip>
266
    frame_context = tx_env.frame_context
267
    assert frame_context is not None
268
269
    if frame.value > U256(0) and not is_account_alive(
270
        tx_env.state, resolved_target
271
    ):
272
        charge_frame_state_gas(frame_context, StateGasCosts.NEW_ACCOUNT)
273
274
    code_address, disable_precompiles = resolve_delegated_code_address(
275
        tx_env.state, gas_meter, accessed_addresses, resolved_target
276
    )
277
278
    code = get_code(
279
        tx_env.state,
280
        get_account(tx_env.state, code_address).code_hash,
281
    )
282
283
    ## Build the frame
284
    return Evm(
285
        # Context
286
        block_env=block_env,
287
        tx_env=tx_env,
288
        parent_evm=None,
289
        depth=Uint(0),
290
        # Call Parameters
291
        caller=tx_env.origin,
292
        current_target=resolved_target,
293
        value=frame.value,
294
        call_data=frame.data,
295
        should_transfer_value=True,
296
        is_static=frame.mode == FrameMode.VERIFY,
297
        disable_precompiles=disable_precompiles,
298
        # Code
299
        code_address=code_address,
300
        code=code,
301
        valid_jump_destinations=get_valid_jump_destinations(code),
302
        # Machine State
303
        gas_meter=gas_meter,
304
        pc=Uint(0),
305
        stack=[],
306
        memory=bytearray(),
307
        return_data=b"",
308
        # Accrued Effects
309
        logs=(),
310
        accounts_to_delete=set(),
311
        accessed_addresses=accessed_addresses,
312
        accessed_storage_keys=accessed_storage_keys,
313
        # Outcome
314
        running=True,
315
        output=b"",
316
        error=None,
317
    )

execute_default_verify_code

Execute the protocol default code of a VERIFY frame whose resolved target has no code, returning the frame's status.

The default code draws no execution gas of its own: the frame's only execution charge is the resolved target's access, taken by execute_frame at frame entry. It can consume state gas through APPROVE, when incrementing the nonce creates the sender account; a pool that cannot cover that charge raises, halting the frame exceptionally.

The default code approves the scope allowed by the frame's flags, provided the transaction carries an authorizing secp256k1 signature entry over the canonical signature hash whose resolved signer is the frame's resolved target: the entry at index 0 for frames allowed to approve execution, or at index 1 for payment-only frames. Anything else reverts the frame — which, for a VERIFY frame, invalidates the transaction.

def execute_default_verify_code(​tx_env: TransactionEnvironment, ​​frame: Frame​) -> FrameStatus:
324
    <snip>
343
    frame_context = tx_env.frame_context
344
    assert frame_context is not None
345
    tx = frame_context.tx
346
    resolved_target = resolve_frame_target(tx, frame)
347
348
    allowed_scope = frame.flags & APPROVE_SCOPE_MASK
349
    # The frame is not allowed to approve anything.
350
    if not allowed_scope:
351
        return FrameStatus.FAILURE
352
353
    if FrameFlag.APPROVE_EXECUTION in allowed_scope:
354
        signature_index = 0
355
    else:
356
        signature_index = 1
357
358
    # There is no signature entry at the authorizing index.
359
    if len(tx.signatures) <= signature_index:
360
        return FrameStatus.FAILURE
361
    signature = tx.signatures[signature_index]
362
363
    # Only a protocol-validated secp256k1 signature authorizes.
364
    if signature.scheme != FrameSignatureScheme.SECP256K1:
365
        return FrameStatus.FAILURE
366
    # The signature must cover the canonical signature hash.
367
    if len(signature.message) != 0:
368
        return FrameStatus.FAILURE
369
    # The signature must come from the frame's resolved target.
370
    if frame_context.resolved_signers[signature_index] != resolved_target:
371
        return FrameStatus.FAILURE
372
373
    if not attempt_approval(tx_env, allowed_scope):
374
        return FrameStatus.FAILURE
375
376
    return FrameStatus.SUCCESS

execute_frame

Run a single frame as a top-level call and reduce its outcome.

Every frame is charged its resolved target's warm or cold access at frame entry, from its own execution gas budget, before anything else: resolving the target's code is how the protocol dispatches the frame. A VERIFY frame whose resolved target has no code then runs the protocol default code instead of an EVM, unless that target is a precompile, which dispatches in every mode. As with an ordinary CALL, a caller that cannot cover the transferred value reverts the frame, consuming the gas charged so far.

The frame's receipt reports its usage of both gas dimensions at frame exit: the execution gas its meter consumed — the whole budget when the frame halted exceptionally — and its state budget less the remaining pool. A failing frame's state gas, the frame-entry charge included, rolls back to the checkpoint taken here at frame entry, so its receipt reports zero state gas.

On success the frame's accesses are committed back to the journal's warm sets — the resolved target included, which is what keeps a payer warm for later frames — and a failed frame's accesses are discarded, so nothing it touched stays warm.

def execute_frame(​block_env: BlockEnvironment, ​​tx_env: TransactionEnvironment, ​​frame: Frame, ​​journal: FrameJournal​) -> FrameOutcome:
385
    <snip>
410
    frame_context = tx_env.frame_context
411
    assert frame_context is not None
412
    tx = frame_context.tx
413
    tx_state = tx_env.state
414
    resolved_target = resolve_frame_target(tx, frame)
415
416
    # Checkpoint at frame entry, before any charge: a failing frame's
417
    # state gas restores to here, and any edits it made to earlier
418
    # receipts are undone with it.
419
    entry_snapshot = copy_frame_context(tx_env)
420
    state_budget = Uint(frame.gas_limits.state)
421
    execution_budget = Uint(frame.gas_limits.execution)
422
423
    gas_meter = GasMeter(
424
        gas_left=ExecutionGas(execution_budget),
425
        reservoir=None,
426
    )
427
428
    ## Warm up the frame's access sets
429
    accessed_addresses: Set[Address] = set(journal.warm_addresses)
430
    accessed_addresses.add(block_env.coinbase)
431
    accessed_addresses.update(PRE_COMPILED_CONTRACTS.keys())
432
    accessed_storage_keys = set(journal.warm_storage_keys)
433
434
    ## Charge the resolved target's access
435
    try:
436
        if resolved_target in accessed_addresses:
437
            charge_gas_from_meter(gas_meter, GasCosts.WARM_ACCESS)
438
        else:
439
            charge_gas_from_meter(gas_meter, GasCosts.COLD_ACCOUNT_ACCESS)
440
            accessed_addresses.add(resolved_target)
441
    except ExceptionalHalt:
442
        # The target's access exceeded the frame's execution budget:
443
        # the frame halts exceptionally, consuming the budget whole.
444
        # Nothing else has been charged, so there is nothing to
445
        # restore.
446
        return FrameOutcome(
447
            receipt=FrameReceipt(
448
                status=FrameStatus.FAILURE,
449
                gas_used=GasUsed(execution=execution_budget, state=Uint(0)),
450
                logs=(),
451
            ),
452
            refund_counter=0,
453
            accounts_to_delete=set(),
454
        )
455
456
    target_account = get_account(tx_state, resolved_target)
457
    if (
458
        frame.mode == FrameMode.VERIFY
459
        and resolved_target not in PRE_COMPILED_CONTRACTS
460
        and target_account.code_hash == EMPTY_CODE_HASH
461
    ):
462
        try:
463
            status = execute_default_verify_code(tx_env, frame)
464
        except ExceptionalHalt:
465
            # `APPROVE` could not cover the sender-creation state
466
            # charge: the frame halts exceptionally with no approval
467
            # effects, consuming its execution budget.
468
            restore_frame_context(tx_env, entry_snapshot)
469
            return FrameOutcome(
470
                receipt=FrameReceipt(
471
                    status=FrameStatus.FAILURE,
472
                    gas_used=GasUsed(
473
                        execution=execution_budget,
474
                        state=Uint(0),
475
                    ),
476
                    logs=(),
477
                ),
478
                refund_counter=0,
479
                accounts_to_delete=set(),
480
            )
481
        if status == FrameStatus.SUCCESS:
482
            journal.warm_addresses.update(accessed_addresses)
483
        return FrameOutcome(
484
            receipt=FrameReceipt(
485
                status=status,
486
                gas_used=GasUsed(
487
                    execution=execution_budget - Uint(gas_meter.gas_left),
488
                    state=state_budget - Uint(frame_context.state_gas_left),
489
                ),
490
                logs=(),
491
            ),
492
            refund_counter=0,
493
            accounts_to_delete=set(),
494
        )
495
496
    if frame.value != U256(0):
497
        caller_balance = get_account(tx_state, tx_env.origin).balance
498
        if caller_balance < frame.value:
499
            return FrameOutcome(
500
                receipt=FrameReceipt(
501
                    status=FrameStatus.FAILURE,
502
                    gas_used=GasUsed(
503
                        execution=execution_budget - Uint(gas_meter.gas_left),
504
                        state=Uint(0),
505
                    ),
506
                    logs=(),
507
                ),
508
                refund_counter=0,
509
                accounts_to_delete=set(),
510
            )
511
512
    try:
513
        evm = create_evm_from_frame(
514
            block_env,
515
            tx_env,
516
            frame,
517
            resolved_target,
518
            gas_meter,
519
            accessed_addresses,
520
            accessed_storage_keys,
521
        )
522
    except ExceptionalHalt:
523
        # The frame's entry charges exceeded its own budgets: the
524
        # frame halts exceptionally, consuming its execution budget.
525
        restore_frame_context(tx_env, entry_snapshot)
526
        return FrameOutcome(
527
            receipt=FrameReceipt(
528
                status=FrameStatus.FAILURE,
529
                gas_used=GasUsed(
530
                    execution=execution_budget,
531
                    state=Uint(0),
532
                ),
533
                logs=(),
534
            ),
535
            refund_counter=0,
536
            accounts_to_delete=set(),
537
        )
538
539
    process_call(evm)
540
541
    if evm.error is not None:
542
        # The frame failed: `process_call` rolled its state gas back
543
        # to the call, and this restore extends the rollback over the
544
        # frame-entry charge.
545
        restore_frame_context(tx_env, entry_snapshot)
546
547
    gas_used = GasUsed(
548
        execution=execution_budget - Uint(gas_meter.gas_left),
549
        state=state_budget - Uint(frame_context.state_gas_left),
550
    )
551
    if evm.error is None:
552
        journal.warm_addresses.update(evm.accessed_addresses)
553
        journal.warm_storage_keys.update(evm.accessed_storage_keys)
554
        receipt = FrameReceipt(
555
            status=FrameStatus.SUCCESS, gas_used=gas_used, logs=evm.logs
556
        )
557
        accounts_to_delete = set(evm.accounts_to_delete)
558
    else:
559
        receipt = FrameReceipt(
560
            status=FrameStatus.FAILURE, gas_used=gas_used, logs=()
561
        )
562
        accounts_to_delete = set()
563
564
    return FrameOutcome(
565
        receipt=receipt,
566
        refund_counter=gas_meter.refund_counter,
567
        accounts_to_delete=accounts_to_delete,
568
    )

process_frames

Execute the frames of a frame transaction in order.

Each frame runs against fresh gas pools holding its declared budgets: a gas meter for the execution dimension and the frame context's state gas pool for the state dimension. Unused gas is not available to later frames. Between frames the transient storage is discarded and the environment's origin is rebound to the caller of the frame about to run.

A failing frame of an atomic batch unrolls the batch, and the remaining batch frames are skipped.

Both gas dimensions settle from the final receipts: each frame's unused gas is its budget less its receipt's usage, so gas a rollback or a later frame's refill removed from a receipt counts as unused without further accounting.

Unlike process_top_level, this flow can invalidate the whole transaction: a VERIFY frame reverting, a SENDER frame before execution approval, or no frame having approved payment by the end raises FrameTransactionExecutionError.

def process_frames(​block_env: BlockEnvironment, ​​tx_env: TransactionEnvironment​) -> TransactionOutput:
575
    <snip>
598
    frame_context = tx_env.frame_context
599
    assert frame_context is not None
600
    assert tx_env.top_level_context is None
601
602
    tx = frame_context.tx
603
    tx_state = tx_env.state
604
605
    journal = FrameJournal(
606
        warm_addresses={tx.sender},
607
        warm_storage_keys=set(),
608
        refund_counter=0,
609
        accounts_to_delete=set(),
610
    )
611
612
    open_batch: Optional[AtomicBatch] = None
613
    skip_batch = False
614
615
    for index, frame in enumerate(tx.frames):
616
        frame_context.current_frame_index = Uint(index)
617
        has_batch_flag = FrameFlag.ATOMIC_BATCH in frame.flags
618
619
        if has_batch_flag and open_batch is None:
620
            context_snapshot = copy_frame_context(tx_env)
621
            assert context_snapshot is not None
622
            open_batch = AtomicBatch(
623
                first_frame_index=Uint(index),
624
                state_snapshot=copy_tx_state(tx_state),
625
                context_snapshot=context_snapshot,
626
                journal=copy_frame_journal(journal),
627
            )
628
629
        if skip_batch:
630
            # A frame of a failed atomic batch never executes; its
631
            # zero-usage receipt makes its allotted gas — in both
632
            # dimensions — count as unused.
633
            frame_context.frame_receipts.append(
634
                FrameReceipt(
635
                    status=FrameStatus.SKIPPED,
636
                    gas_used=GasUsed(execution=Uint(0), state=Uint(0)),
637
                    logs=(),
638
                )
639
            )
640
            if not has_batch_flag:
641
                open_batch = None
642
                skip_batch = False
643
            continue
644
645
        if (
646
            frame.mode == FrameMode.SENDER
647
            and not frame_context.sender_approved
648
        ):
649
            raise FrameTransactionExecutionError(
650
                "SENDER frame before execution approval"
651
            )
652
653
        # Transient storage is discarded between frames.
654
        tx_state.transient_storage.clear()
655
656
        # The ORIGIN opcode returns the frame's caller at every call
657
        # depth.
658
        if frame.mode == FrameMode.SENDER:
659
            tx_env.origin = tx.sender
660
        else:
661
            tx_env.origin = FRAME_ENTRY_POINT
662
663
        # Seed the frame's state gas pool from its declared budget.
664
        frame_context.state_gas_left = StateGas(Uint(frame.gas_limits.state))
665
666
        outcome = execute_frame(block_env, tx_env, frame, journal)
667
        receipt = outcome.receipt
668
669
        if (
670
            frame.mode == FrameMode.VERIFY
671
            and receipt.status == FrameStatus.FAILURE
672
        ):
673
            raise FrameTransactionExecutionError("VERIFY frame failed")
674
675
        incorporate_frame_outcome(journal, outcome)
676
        frame_context.frame_receipts.append(receipt)
677
678
        terminates_batch = open_batch is not None and not has_batch_flag
679
        if receipt.status == FrameStatus.FAILURE and open_batch is not None:
680
            journal = unroll_atomic_batch(tx_env, open_batch)
681
            if terminates_batch:
682
                open_batch = None
683
            else:
684
                skip_batch = True
685
        elif terminates_batch:
686
            open_batch = None
687
688
    if frame_context.payer is None:
689
        raise FrameTransactionExecutionError("no frame approved gas payment")
690
691
    logs: Tuple[Log, ...] = ()
692
    for receipt in frame_context.frame_receipts:
693
        logs += receipt.logs
694
695
    # Settle both dimensions from the final receipts; every frame has
696
    # exactly one.
697
    unused_execution_gas = Uint(0)
698
    unused_state_gas = Uint(0)
699
    state_gas_used = Uint(0)
700
    for frame, receipt in zip(
701
        tx.frames, frame_context.frame_receipts, strict=True
702
    ):
703
        unused_execution_gas += (
704
            Uint(frame.gas_limits.execution) - receipt.gas_used.execution
705
        )
706
        unused_state_gas += (
707
            Uint(frame.gas_limits.state) - receipt.gas_used.state
708
        )
709
        state_gas_used += receipt.gas_used.state
710
711
    return TransactionOutput(
712
        gas_left=ExecutionGas(unused_execution_gas),
713
        refund_counter=U256(journal.refund_counter),
714
        logs=logs,
715
        accounts_to_delete=journal.accounts_to_delete,
716
        error=None,
717
        return_data=Bytes(b""),
718
        state_gas_left=StateGas(unused_state_gas),
719
        state_gas_used=int(state_gas_used),
720
    )