Library QedkConcrete.consensusblock

Fee-error exclusion for the generated block executor

This is the single-block induction step, not the final delayed-window theorem. Incoming budget backing and configuration agreement during this block are still its induction obligations. The proof does not assume successful block execution or successful validation of the next candidate.
From Stdlib Require Import List String ZArith Lia Bool.
From stdpp Require Import decidable strings.
Require Import RocqOfOCaml.RocqOfOCaml.
Require Import MonadVM.Execution QedkConcrete.consensushistory
  QedkConcrete.consensushistoryproofs
  QedkConcrete.transactionnonces QedkConcrete.authorizationnonces
  QedkConcrete.transactionstage QedkConcrete.transactioncode
  QedkConcrete.transactionhistory QedkConcrete.authorizationcode
  QedkConcrete.transactionpersistentcode QedkConcrete.transactionchecked
  QedkConcrete.publictransaction QedkConcrete.protectedstorageboundary.
Require Import QedkConcrete.consensuswindow QedkConcrete.consensuswindowproofs
  QedkConcrete.consensusbudget QedkConcrete.transactionbudget
  QedkConcrete.transactionhistoryprojection QedkConcrete.supply
  QedkConcrete.assemblychecked QedkConcrete.hashwords QedkConcrete.wordrange
  QedkConcrete.hostaccountformat.
Require Import QedkConcrete.consensustransaction QedkConcrete.consensusbacking
  QedkConcrete.transactionformat.
Require Import QedkConcrete.blocktransactionentry QedkConcrete.consensusprefix
  QedkConcrete.blockpersistentcode QedkConcrete.consensusboundaries QedkConcrete.credits.
Require Import QedkConcrete.blocknumbers.
Require Import QedkConcrete.nativeinputs.
Set Default Goal Selector "!".
#[local] Hint Extern 100 (Basics.Unreachable _) ⇒
  (apply Opaque.legacy_witness; Opaque.opaque_inhabit 64%nat) : typeclass_instances.
Import ListNotations.
Local Open Scope Z_scope.
Module E := Execution.Make.
Module S := State.TransactionState.
Module B := State.BlockState.
Module W := State.WorldState.
Module A := Chain.Ethereum.Account.
Module T := Chain.Ethereum.Transaction.
Module Addr := Chain.Ethereum.Address.

Section Execution.
Context {F : E.FArgs}.

Local Abbreviation args := (@E.Instantiation_fargs F).
Local Abbreviation params := args.(Host.Instantiate.ChainParams).

Context {UI0 : Basics.Unreachable Support.UintN.Impl.t}
  {UB : Basics.Unreachable Bloom.t}
  {U8 : Basics.Unreachable Byte_string.B8.(Byte_string.Fixed.Fixed_result.t)}.

Local Abbreviation prefix := (@transactionPrefix F).
Local Abbreviation vmInput := (@transactionVmInput F).
Local Abbreviation callFromEoa := (@E.Host.call_from_eoa F).
Local Abbreviation processTransaction := (@E.process_transaction F).
Local Abbreviation dispatchTransaction := (@E.dispatch_transaction F).
Local Abbreviation readAccount := (fun world addr ⇒ State.op_pointcaretlparenrparen
  world (@W.account None addr)).

Local Abbreviation native := (@Reserve_balance.precompile_address).
Local Abbreviation staking := (@MonadVM.Staking.staking_address).
Local Abbreviation systemSender := (@Chain.Monad.system_sender).

Local Abbreviation recoveredAuthority := (@Chain.Ethereum.Transaction.Authorization.authority).

Local Abbreviation blockNumber := (fun block ⇒ Chain.Ethereum.Block.Header.t.number
  (Chain.Ethereum.Block.t.header (B.t.current_block block))).
Local Abbreviation emptying := (@Reserve_balance.is_emptying_tx).

Local Abbreviation configured := (@Reserve_balance.user_reserve_balance).
Local Abbreviation balance := (fun world a ⇒ Numeric.U256.to_z (A.t.balance (readAccount world a))).

Context {UBlock : Basics.Unreachable Chain.Ethereum.Block.t}
  {UTries : Basics.Unreachable (OCamlIarray.t Mpt.Trie.t)}
  {UChar : Basics.Unreachable OCamlChar.t} {UWorld : Basics.Unreachable W.t}
  {UTrie : Basics.Unreachable Mpt.Trie.t} {UNibbles : Basics.Unreachable Mpt.Nibbles.t}.
Local Abbreviation processBlock := (@E.process_block F).
Local Abbreviation blockEntry := (@blockTransactionEntry F).
Local Abbreviation height := (fun block ⇒ Chain.Ethereum.Block.Header.t.number
  (Chain.Ethereum.Block.t.header block)).

The candidate need not validate or execute. Only its predecessors have run. The bound is therefore useful even when a nonce or signature check would stop validation before the insufficient-balance check.
Local Abbreviation prefixSafe := (@prefixAvoidsSignerCreation F).
Local Abbreviation creationSafePrefix := (@prefix_creation_safe_prefix F).
Local Abbreviation prefixStakingValid := (@prefixValidStakingSyscalls F).
Local Abbreviation txCredits := (fun txs ⇒ Lists.List.fold_right
  (fun tx subtotal ⇒ @systemTxCredit F tx + subtotal) 0 txs).

Lemma validate_monad_system_transaction_no_insufficient_balance state tx sender
    reportedBlock candidate details :
  @E.validate_monad_system_transaction F state tx sender ≠
    @E.invalid_transaction F unit reportedBlock candidate
      (Execution.Error.Insufficient_balance details).
Proof.
  unfold E.validate_monad_system_transaction, E.invalid_transaction.
  change (@Result.op_letdollar) with
    (fun (A Error C : Set) (first : A + Error) (next : A → C + Error) ⇒
      match first with inl value ⇒ next value | inr error ⇒ inr error end).
  change (@Result._return) with (fun (A Error : Set) (value : A) ⇒ @inl A Error value).
  change (@Result.when_) with (fun (Error : Set) (condition : bool) (failure : unit + Error) ⇒
    if condition then failure else inl tt).
  cbv beta iota zeta.
  destruct tx as [txImpl| | |]; cbn beta iota zeta; try discriminate.
  all: repeat (first
    [ discriminate
    | match goal with
      | |- context [match ?x with Some _ ⇒ _ | None ⇒ _ end] ⇒ destruct x
      | |- context [if ?b then _ else _] ⇒ destruct b
      end ]; cbn beta iota zeta).
Qed.

Lemma process_monad_system_transaction_no_insufficient_balance state tx
    reportedBlock candidate details :
  @E.process_monad_system_transaction F state tx ≠
    @E.invalid_transaction F B.t reportedBlock candidate
      (Execution.Error.Insufficient_balance details).
Proof.
  unfold E.process_monad_system_transaction.
  change (@Result.op_letdollar) with
    (fun (A Error C : Set) (first : A + Error) (next : A → C + Error) ⇒
      match first with inl value ⇒ next value | inr error ⇒ inr error end).
  change (@Result._return) with (fun (A Error : Set) (value : A) ⇒ @inl A Error value).
  change (@Result.when_) with (fun (Error : Set) (condition : bool) (failure : unit + Error) ⇒
    if condition then failure else inl tt).
  destruct (@E.validate_monad_system_transaction F state tx systemSender) as [[]|err] eqn:Hval.
  - cbn beta iota zeta.
    match goal with |- context [E.Host.call_from_monad_system_transaction ?m ?s] ⇒
      destruct (E.Host.call_from_monad_system_transaction m s) as [res after]
    end.
    cbn beta iota zeta.
    destruct (negb _); unfold E.invalid_transaction; discriminate.
  - cbn beta iota zeta. intro Heq.
    unfold E.invalid_transaction in Heq. injection Heq as →.
    exact (validate_monad_system_transaction_no_insufficient_balance
      state tx systemSender reportedBlock candidate details Hval).
Qed.

Theorem accepted_block_prefix_fee snapshot signers until world block previous next before actual after reached :
  (@E.Params F).(Execution.Make_Params_signature.revision) = Support.Variants.Revision.Eleven →
  keccak256OutputSize → @nativeOutputBounds args →
  wordRange (Support.UintN.to_z (Chain.Monad.PARAMS.chain_id params)) →
  (∀ a, List.In a signers → @A.is_smart_contract (readAccount snapshot a) = false) →
  (∀ a, List.In a signers → a ≠ native ∧ a ≠ staking) →
  Forall (fun tx ⇒ transactionValueAndDataBounds tx ∧
    Forall (fun auth ⇒ OCamlString.length (T.Authorization.t.address auth) = 20)
      (T.authorization_list tx) ∧
    Forall (fun auth ⇒ recoveredAuthority auth ≠ Some native)
      (T.authorization_list tx) ∧
    (∀ a, transactionSender tx = Some a → List.In a signers)) (Chain.Ethereum.Block.t.transactions block) →
  prefixSafe signers (blockEntry world block)
    (Chain.Ethereum.Block.t.transactions block) →
  transactionSender actual ≠ Some systemSender →
  headerWords (Chain.Ethereum.Block.t.header block) →
  OCamlString.length (Chain.Ethereum.Block.Header.t.parent_beacon_block_root
    (Chain.Ethereum.Block.t.header block)) = 32 →
  balanceInvariant world →
  totalBalance world + txCredits (Chain.Ethereum.Block.t.transactions block) < 2^256 →
  worldFormat world → @prescribedSystemCode F world →
  BudgetBacking snapshot previous world signers (height block) until →
  (height block ≤ until < height block + 3)%N →
  (∀ before after state,
    Chain.Ethereum.Block.t.transactions block = before ++ after →
    Result.List.fold_leftM dispatchTransaction (blockEntry world block) before = inl state →
    ∀ a, List.In a signers → configured (B.t.world_state state) (height block) a =
      configured snapshot (height block) a) →
  checkTransactions snapshot (Chain.Ethereum.Block.t.header block) previous
    (Chain.Ethereum.Block.t.transactions block) = Some next →
  Chain.Ethereum.Block.t.transactions block = before ++ actual :: after →
  Result.List.fold_leftM dispatchTransaction (blockEntry world block) before = inl reached →
  ∃ sender fee,
    transactionQuote (Chain.Ethereum.Block.t.header (B.t.current_block reached)) actual = Some (sender, fee) ∧
    Z.of_N fee ≤ balance (B.t.world_state reached) sender ∧
    (∀ details, @E.validate_transaction F reached actual ≠
      @E.invalid_transaction F E.transaction_validation (B.t.current_block reached) actual
        (Execution.Error.Insufficient_balance details)) ∧
    ∃ validation, @E.validate_transaction F reached actual = inl validation.
Proof using F U8 UB UI0.
  intros Hrevision Hhash Houtputs Hchain Hsnapshot Hnative Hinputs Hsafe HnotSys
    Hheader Hroot Hbalance Hsupply Hformat Hsystem Hbacked Htime Hconfig Hcheck Hsplit Hprefix.
  destruct (@block_transaction_entry_backing F
    snapshot previous signers (height block) until world block
    Hrevision Hhash Hchain Hheader Hroot Hbalance Hformat Hsystem
    (fun a Ha ⇒ proj1 (Hnative a Ha)) Hbacked)
    as [HentryBacked [HentryBalance [HentryFormat [HentryTotal _]]]].
  pose proof (@block_transaction_entry_header F
    world block) as HentryHeader.
  assert (HbeforeInputs : Forall (fun tx ⇒ transactionValueAndDataBounds tx ∧
    Forall (fun auth ⇒ OCamlString.length (T.Authorization.t.address auth) = 20)
      (T.authorization_list tx) ∧
    Forall (fun auth ⇒ recoveredAuthority auth ≠ Some native)
      (T.authorization_list tx) ∧
    (∀ a, transactionSender tx = Some a → List.In a signers)) before).
  { rewrite Hsplit, Forall_app in Hinputs. exact (proj1 Hinputs). }
  assert (HtailNonneg : 0 ≤ txCredits (actual :: after)).
  { rewrite Hsplit, Forall_app in Hinputs.
    apply tx_credits_nonnegative. eapply Forall_impl; [|exact (proj2 Hinputs)].
    intros t [Hv _]; exact Hv. }
  assert (HentrySupply : totalBalance (B.t.world_state (blockEntry world block)) + txCredits before < 2^256).
  { rewrite Hsplit, tx_credits_app in Hsupply. lia. }
  assert (Hsender : ∀ a, transactionSender actual = Some a → List.In a signers).
  { rewrite Hsplit, Forall_app in Hinputs. pose proof (proj2 Hinputs) as Hrest. inversion Hrest as [|? ? Hactual _].
    exact (proj2 (proj2 (proj2 Hactual))). }
  assert (HbeforeSafe : prefixSafe signers (blockEntry world block) before).
  { apply (creationSafePrefix signers (blockEntry world block) before (actual :: after)).
    rewrite <- Hsplit. exact Hsafe. }
  exact (@accepted_prefix_next_fee F UI0 UB U8
    snapshot signers until before actual after
    (blockEntry world block) reached previous next Hrevision Hhash Houtputs Hchain Hsnapshot Hnative
    HbeforeInputs HbeforeSafe Hsender HnotSys
    ltac:(rewrite HentryHeader; exact Hheader) HentryBalance HentrySupply HentryFormat
    ltac:(cbv beta; rewrite HentryHeader; exact HentryBacked)
    ltac:(cbv beta; rewrite HentryHeader; exact Htime)
    ltac:(intros first last state Hparts Hrun a Ha; cbv beta; rewrite HentryHeader;
      apply (Hconfig first (last ++ actual :: after) state);
      [rewrite Hsplit, Hparts, app_assoc; reflexivity|exact Hrun|exact Ha])
    ltac:(rewrite HentryHeader, <- Hsplit; exact Hcheck) Hprefix).
Qed.

Theorem accepted_block_next_dispatch snapshot signers until world block previous next before actual after reached :
  (@E.Params F).(Execution.Make_Params_signature.revision) = Support.Variants.Revision.Eleven →
  keccak256OutputSize → @nativeOutputBounds args →
  wordRange (Support.UintN.to_z (Chain.Monad.PARAMS.chain_id params)) →
  (∀ a, List.In a signers → @A.is_smart_contract (readAccount snapshot a) = false) →
  (∀ a, List.In a signers → a ≠ native ∧ a ≠ staking) →
  Forall (fun tx ⇒ transactionValueAndDataBounds tx ∧
    Forall (fun auth ⇒ OCamlString.length (T.Authorization.t.address auth) = 20)
      (T.authorization_list tx) ∧
    Forall (fun auth ⇒ recoveredAuthority auth ≠ Some native)
      (T.authorization_list tx) ∧
    (∀ a, transactionSender tx = Some a → List.In a signers)) (Chain.Ethereum.Block.t.transactions block) →
  prefixSafe signers (blockEntry world block)
    (Chain.Ethereum.Block.t.transactions block) →
  prefixStakingValid (blockEntry world block)
    (Chain.Ethereum.Block.t.transactions block) →
  headerWords (Chain.Ethereum.Block.t.header block) →
  OCamlString.length (Chain.Ethereum.Block.Header.t.parent_beacon_block_root
    (Chain.Ethereum.Block.t.header block)) = 32 →
  balanceInvariant world →
  totalBalance world + txCredits (Chain.Ethereum.Block.t.transactions block) < 2^256 →
  worldFormat world → @prescribedSystemCode F world →
  BudgetBacking snapshot previous world signers (height block) until →
  (height block ≤ until < height block + 3)%N →
  (∀ before after state,
    Chain.Ethereum.Block.t.transactions block = before ++ after →
    Result.List.fold_leftM dispatchTransaction (blockEntry world block) before = inl state →
    ∀ a, List.In a signers → configured (B.t.world_state state) (height block) a =
      configured snapshot (height block) a) →
  checkTransactions snapshot (Chain.Ethereum.Block.t.header block) previous
    (Chain.Ethereum.Block.t.transactions block) = Some next →
  Chain.Ethereum.Block.t.transactions block = before ++ actual :: after →
  Result.List.fold_leftM dispatchTransaction (blockEntry world block) before = inl reached →
  ∃ nextState, dispatchTransaction reached actual = inl nextState.
Proof using F U8 UB UI0.
  intros Hrevision Hhash Houtputs Hchain Hsnapshot Hnative Hinputs Hsafe Hstaking
    Hheader Hroot Hbalance Hsupply Hformat Hsystem Hbacked Htime Hconfig Hcheck Hsplit Hprefix.
  destruct (@block_transaction_entry_backing F
    snapshot previous signers (height block) until world block
    Hrevision Hhash Hchain Hheader Hroot Hbalance Hformat Hsystem
    (fun a Ha ⇒ proj1 (Hnative a Ha)) Hbacked)
    as [HentryBacked [HentryBalance [HentryFormat [HentryTotal _]]]].
  pose proof (@block_transaction_entry_header F
    world block) as HentryHeader.
  assert (HbeforeInputs : Forall (fun tx ⇒ transactionValueAndDataBounds tx ∧
    Forall (fun auth ⇒ OCamlString.length (T.Authorization.t.address auth) = 20)
      (T.authorization_list tx) ∧
    Forall (fun auth ⇒ recoveredAuthority auth ≠ Some native)
      (T.authorization_list tx) ∧
    (∀ a, transactionSender tx = Some a → List.In a signers)) before).
  { rewrite Hsplit, Forall_app in Hinputs. exact (proj1 Hinputs). }
  assert (HtailNonneg : 0 ≤ txCredits (actual :: after)).
  { rewrite Hsplit, Forall_app in Hinputs.
    apply tx_credits_nonnegative. eapply Forall_impl; [|exact (proj2 Hinputs)].
    intros t [Hv _]; exact Hv. }
  assert (HentrySupply : totalBalance (B.t.world_state (blockEntry world block)) + txCredits before < 2^256).
  { rewrite Hsplit, tx_credits_app in Hsupply. lia. }
  assert (Hsender : ∀ a, transactionSender actual = Some a → List.In a signers).
  { rewrite Hsplit, Forall_app in Hinputs. pose proof (proj2 Hinputs) as Hrest. inversion Hrest as [|? ? Hactual _].
    exact (proj2 (proj2 (proj2 Hactual))). }
  assert (HbeforeSafe : prefixSafe signers (blockEntry world block) before).
  { apply (creationSafePrefix signers (blockEntry world block) before (actual :: after)).
    rewrite <- Hsplit. exact Hsafe. }
  exact (@accepted_prefix_next_dispatch F UI0 UB U8
    snapshot signers until before actual after
    (blockEntry world block) reached previous next Hrevision Hhash Houtputs Hchain Hsnapshot Hnative
    HbeforeInputs HbeforeSafe ltac:(rewrite <- Hsplit; exact Hstaking) Hsender
    ltac:(rewrite HentryHeader; exact Hheader) HentryBalance HentrySupply HentryFormat
    ltac:(cbv beta; rewrite HentryHeader; exact HentryBacked)
    ltac:(cbv beta; rewrite HentryHeader; exact Htime)
    ltac:(intros first last state Hparts Hrun a Ha; cbv beta; rewrite HentryHeader;
      apply (Hconfig first (last ++ actual :: after) state);
      [rewrite Hsplit, Hparts, app_assoc; reflexivity|exact Hrun|exact Ha])
    ltac:(rewrite HentryHeader, <- Hsplit; exact Hcheck) Hprefix).
Qed.

Theorem accepted_block_no_insufficient_balance snapshot signers until verify world block previous next :
  (@E.Params F).(Execution.Make_Params_signature.revision) = Support.Variants.Revision.Eleven →
  keccak256OutputSize → @nativeOutputBounds args →
  wordRange (Support.UintN.to_z (Chain.Monad.PARAMS.chain_id params)) →
  (∀ a, List.In a signers → @A.is_smart_contract (readAccount snapshot a) = false) →
  (∀ a, List.In a signers → a ≠ native ∧ a ≠ staking) →
  Forall (fun tx ⇒ transactionValueAndDataBounds tx ∧
    Forall (fun auth ⇒ OCamlString.length (T.Authorization.t.address auth) = 20)
      (T.authorization_list tx) ∧
    Forall (fun auth ⇒ recoveredAuthority auth ≠ Some native)
      (T.authorization_list tx) ∧
    (∀ a, transactionSender tx = Some a → List.In a signers)) (Chain.Ethereum.Block.t.transactions block) →
  (@E.validate_block F world block = inl tt →
   prefixSafe signers (blockEntry world block)
    (Chain.Ethereum.Block.t.transactions block)) →
  headerWords (Chain.Ethereum.Block.t.header block) →
  OCamlString.length (Chain.Ethereum.Block.Header.t.parent_beacon_block_root
    (Chain.Ethereum.Block.t.header block)) = 32 →
  balanceInvariant world →
  totalBalance world + txCredits (Chain.Ethereum.Block.t.transactions block) < 2^256 →
  worldFormat world → @prescribedSystemCode F world →
  BudgetBacking snapshot previous world signers (height block) until →
  (height block ≤ until < height block + 3)%N →
  (∀ before after state,
    Chain.Ethereum.Block.t.transactions block = before ++ after →
    Result.List.fold_leftM dispatchTransaction (blockEntry world block) before = inl state →
    ∀ a, List.In a signers → configured (B.t.world_state state) (height block) a =
      configured snapshot (height block) a) →
  checkTransactions snapshot (Chain.Ethereum.Block.t.header block) previous
    (Chain.Ethereum.Block.t.transactions block) = Some next →
  ∀ reportedBlock candidate details,
    processBlock verify world block ≠
      @E.invalid_transaction F W.t reportedBlock candidate (Execution.Error.Insufficient_balance details).
Proof using F U8 UB UI0.
  intros Hrevision Hhash Houtputs Hchain Hsnapshot Hnative Hinputs Hsafe
    Hheader Hroot Hbalance Hsupply Hformat Hsystem Hbacked Htime Hconfig Hcheck
    reportedBlock candidate details Herror.
  specialize (Hsafe (@process_block_transaction_error_validated F
    verify world block _ Herror)).
  pose proof (@process_block_dispatch_error_from_entry F
    verify world block _ Hrevision Herror) as HfoldError.
  destruct (fold_leftM_first_error _ _ _ _ HfoldError)
    as [before [actual [after [reached [Hsplit [Hprefix Hfailed]]]]]].
  pose proof Hcheck as Hchk.
  rewrite Hsplit, check_transactions_app in Hchk.
  destruct (checkTransactions snapshot _ previous before) as [middle|]; [|discriminate Hchk].
  cbn [checkTransactions] in Hchk.
  destruct (checkTransaction snapshot _ middle actual) as [afterChk|] eqn:HactualCheck;
    [|discriminate Hchk].
  destruct (check_transaction_history _ _ _ _ _ HactualCheck) as [sender [Hsender_rec _]].
  destruct (bool_decide_reflect (sender = systemSender)) as [Hsys | HnotSys].
  - subst sender.
    destruct (check_system_transaction_dispatch _ _ _ _ _ Hsender_rec HactualCheck)
      as [_ [_ HdispEq]].
    rewrite HdispEq in Hfailed.
    exact (process_monad_system_transaction_no_insufficient_balance
      reached actual reportedBlock candidate details Hfailed).
  - rewrite (dispatch_transaction_eq_process_transaction reached actual sender Hsender_rec HnotSys)
      in Hfailed.
    pose proof (@process_transaction_error_from_validation F
      reached actual _ Hfailed) as Hvalidation.
    destruct (@transaction_validation_error_metadata F reached actual _ Hvalidation) as [Hblock Htx].
    cbn in Hblock, Htx. subst reportedBlock candidate.
    assert (HnotSysActual : transactionSender actual ≠ Some systemSender)
      by (rewrite Hsender_rec; congruence).
    destruct (accepted_block_prefix_fee snapshot signers until world block previous next
      before actual after reached Hrevision Hhash Houtputs Hchain Hsnapshot Hnative
      Hinputs Hsafe HnotSysActual Hheader Hroot Hbalance Hsupply Hformat Hsystem Hbacked Htime
      Hconfig Hcheck Hsplit Hprefix) as [qsender [fee [_ [_ [Haffordable _]]]]].
    exact (Haffordable details Hvalidation).
Qed.

Theorem accepted_block_no_transaction_error snapshot signers until verify world block previous next :
  (@E.Params F).(Execution.Make_Params_signature.revision) = Support.Variants.Revision.Eleven →
  keccak256OutputSize → @nativeOutputBounds args →
  wordRange (Support.UintN.to_z (Chain.Monad.PARAMS.chain_id params)) →
  (∀ a, List.In a signers → @A.is_smart_contract (readAccount snapshot a) = false) →
  (∀ a, List.In a signers → a ≠ native ∧ a ≠ staking) →
  Forall (fun tx ⇒ transactionValueAndDataBounds tx ∧
    Forall (fun auth ⇒ OCamlString.length (T.Authorization.t.address auth) = 20)
      (T.authorization_list tx) ∧
    Forall (fun auth ⇒ recoveredAuthority auth ≠ Some native)
      (T.authorization_list tx) ∧
    (∀ a, transactionSender tx = Some a → List.In a signers)) (Chain.Ethereum.Block.t.transactions block) →
  (@E.validate_block F world block = inl tt →
   prefixSafe signers (blockEntry world block)
    (Chain.Ethereum.Block.t.transactions block)) →
  (@E.validate_block F world block = inl tt →
   prefixStakingValid (blockEntry world block)
    (Chain.Ethereum.Block.t.transactions block)) →
  headerWords (Chain.Ethereum.Block.t.header block) →
  OCamlString.length (Chain.Ethereum.Block.Header.t.parent_beacon_block_root
    (Chain.Ethereum.Block.t.header block)) = 32 →
  balanceInvariant world →
  totalBalance world + txCredits (Chain.Ethereum.Block.t.transactions block) < 2^256 →
  worldFormat world → @prescribedSystemCode F world →
  BudgetBacking snapshot previous world signers (height block) until →
  (height block ≤ until < height block + 3)%N →
  (∀ before after state,
    Chain.Ethereum.Block.t.transactions block = before ++ after →
    Result.List.fold_leftM dispatchTransaction (blockEntry world block) before = inl state →
    ∀ a, List.In a signers → configured (B.t.world_state state) (height block) a =
      configured snapshot (height block) a) →
  checkTransactions snapshot (Chain.Ethereum.Block.t.header block) previous
    (Chain.Ethereum.Block.t.transactions block) = Some next →
  ∀ details,
    processBlock verify world block ≠ inr (Execution.Error.Invalid_transaction details).
Proof using F U8 UB UI0.
  intros Hrevision Hhash Houtputs Hchain Hsnapshot Hnative Hinputs Hsafe Hstaking
    Hheader Hroot Hbalance Hsupply Hformat Hsystem Hbacked Htime Hconfig Hcheck details Herror.
  pose proof (@process_block_transaction_error_validated F verify world block _ Herror) as HvalBlk.
  specialize (Hsafe HvalBlk). specialize (Hstaking HvalBlk).
  pose proof (@process_block_dispatch_error_from_entry F
    verify world block _ Hrevision Herror) as HfoldError.
  destruct (fold_leftM_first_error _ _ _ _ HfoldError)
    as [before [actual [after [reached [Hsplit [Hprefix Hfailed]]]]]].
  destruct (accepted_block_next_dispatch snapshot signers until world block previous next
    before actual after reached Hrevision Hhash Houtputs Hchain Hsnapshot Hnative
    Hinputs Hsafe Hstaking Hheader Hroot Hbalance Hsupply Hformat Hsystem Hbacked Htime
    Hconfig Hcheck Hsplit Hprefix) as [nextState Hsucc].
  rewrite Hsucc in Hfailed. discriminate Hfailed.
Qed.

System reward transactions and withdrawals add bounded credits; finalization changes history, not accounts. Thus successful block execution carries the same consensus budgets to the next block without refilling them.
Theorem accepted_block_preserves_backing snapshot signers until verify world block previous next post :
  (@E.Params F).(Execution.Make_Params_signature.revision) = Support.Variants.Revision.Eleven →
  keccak256OutputSize → @nativeOutputBounds args →
  wordRange (Support.UintN.to_z (Chain.Monad.PARAMS.chain_id params)) →
  (∀ a, List.In a signers → @A.is_smart_contract (readAccount snapshot a) = false) →
  (∀ a, List.In a signers → a ≠ native ∧ a ≠ staking) →
  Forall (fun tx ⇒ transactionValueAndDataBounds tx ∧
    Forall (fun auth ⇒ OCamlString.length (T.Authorization.t.address auth) = 20)
      (T.authorization_list tx) ∧
    Forall (fun auth ⇒ recoveredAuthority auth ≠ Some native)
      (T.authorization_list tx) ∧
    (∀ a, transactionSender tx = Some a → List.In a signers)) (Chain.Ethereum.Block.t.transactions block) →
  prefixSafe signers (blockEntry world block)
    (Chain.Ethereum.Block.t.transactions block) →
  headerWords (Chain.Ethereum.Block.t.header block) →
  OCamlString.length (Chain.Ethereum.Block.Header.t.parent_beacon_block_root
    (Chain.Ethereum.Block.t.header block)) = 32 →
  balanceInvariant world → worldFormat world → @prescribedSystemCode F world →
  BudgetBacking snapshot previous world signers (height block) until →
  (height block ≤ until < height block + 3)%N →
  (∀ before after state,
    Chain.Ethereum.Block.t.transactions block = before ++ after →
    Result.List.fold_leftM dispatchTransaction (blockEntry world block) before = inl state →
    ∀ a, List.In a signers → configured (B.t.world_state state) (height block) a =
      configured snapshot (height block) a) →
  checkTransactions snapshot (Chain.Ethereum.Block.t.header block) previous
    (Chain.Ethereum.Block.t.transactions block) = Some next →
  Forall (fun wd ⇒ 0 ≤ Numeric.U64.to_z (Chain.Ethereum.Withdrawal.t.amount wd) < 2^64)
    (Chain.Ethereum.Block.t.withdrawals block) →
  let credits :=
    txCredits (Chain.Ethereum.Block.t.transactions block) +
    Lists.List.fold_right (fun wd total ⇒ withdrawalWei wd + total) 0
      (Chain.Ethereum.Block.t.withdrawals block) in
  totalBalance world + credits < 2^256 →
  processBlock verify world block = inl post →
  BudgetBacking snapshot next post signers (height block) until ∧
  balanceInvariant post ∧ worldFormat post ∧ totalBalance post ≤ totalBalance world + credits.
Proof using F U8 UB UI0.
  intros Hrevision Hhash Houtputs Hchain Hsnapshot Hnative Hinputs Hsafe
    Hheader Hroot Hbalance Hformat Hsystem Hbacked Htime Hconfig Hcheck Hwidth credits Hsupply Hrun.
  destruct (@process_block_dispatch_success_from_entry F
    verify world block post Hrevision Hrun) as [after [finalHistory [Htransactions Hpost]]].
  destruct (@block_transaction_entry_backing F
    snapshot previous signers (height block) until world block
    Hrevision Hhash Hchain Hheader Hroot Hbalance Hformat Hsystem
    (fun a Ha ⇒ proj1 (Hnative a Ha)) Hbacked)
    as [HentryBacked [HentryBalance [HentryFormat [HentryTotal _]]]].
  pose proof (@block_transaction_entry_header F
    world block) as HentryHeader.
  set (wdCredits := Lists.List.fold_right (fun wd total ⇒ withdrawalWei wd + total) 0
    (Chain.Ethereum.Block.t.withdrawals block)).
  pose proof (withdrawal_sum_nonnegative _ Hwidth) as HwdNonneg.
  fold wdCredits in HwdNonneg.
  assert (HentrySupply :
    totalBalance (B.t.world_state (blockEntry world block)) +
    txCredits (Chain.Ethereum.Block.t.transactions block) < 2^256).
  { unfold credits in Hsupply. fold wdCredits in Hsupply. lia. }
  destruct (@accepted_prefix_backed F UI0 UB U8
    snapshot signers until
    (Chain.Ethereum.Block.t.transactions block) (blockEntry world block) Hrevision Hhash Houtputs Hchain Hsnapshot Hnative
    Hinputs Hsafe after previous next
    ltac:(rewrite HentryHeader; exact Hheader) HentryBalance HentrySupply HentryFormat
    ltac:(cbv beta; rewrite HentryHeader; exact HentryBacked)
    ltac:(cbv beta; rewrite HentryHeader; exact Htime)
    ltac:(cbv beta; rewrite HentryHeader; exact Hconfig)
    ltac:(rewrite HentryHeader; exact Hcheck) Htransactions)
    as [HafterBacked [HafterBalance [HafterFormat [HafterTotal HafterHeader]]]].
  cbv beta in HafterBacked. rewrite HentryHeader in HafterBacked.
  assert (HwithdrawBudget : totalBalance (B.t.world_state after) + wdCredits < 2^256).
  { unfold credits in Hsupply. fold wdCredits in Hsupply. lia. }
  pose proof (@withdrawals_backing F snapshot next signers (height block) until after
    (Chain.Ethereum.Block.t.withdrawals block) HafterBacked HafterBalance Hwidth HwithdrawBudget)
    as HwithdrawBacked.
  destruct (@withdrawals_preserve_supply_bound F after (Chain.Ethereum.Block.t.withdrawals block)
    HafterBalance Hwidth HwithdrawBudget) as [HwithdrawBalance [HwithdrawTotal _]].
  pose proof (@withdrawals_preserve_format F after (Chain.Ethereum.Block.t.withdrawals block) HafterFormat)
    as HwithdrawFormat.
  subst post. split.
  - destruct HwithdrawBacked as [Hhistory Hnumbers Hidle]. constructor; [|exact Hnumbers|exact Hidle].
    destruct Hhistory. constructor; assumption.
  - split; [exact HwithdrawBalance|]. split; [exact HwithdrawFormat|].
    change (totalBalance (B.t.world_state (OCamlList.fold_left (@E.process_withdrawal F) after
      (Chain.Ethereum.Block.t.withdrawals block))) ≤ totalBalance world + credits).
    rewrite HwithdrawTotal. unfold credits. fold wdCredits. lia.
Qed.
End Execution.