Library QedkConcrete.consensusprefix

Concrete transaction-prefix induction

This proof derives the budget and emptying bounds at every successfully reached transaction, rather than assuming them there. Configuration agreement is still an input to this induction step; the storage/window proof must discharge it before this can be an end-to-end theorem.
From Stdlib Require Import List String ZArith Lia Bool.
From stdpp Require Import decidable strings.
Require Import RocqOfOCaml.RocqOfOCaml.
Require Export QedkConcrete.consensusrequirements.
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 QedkConcrete.stakingsupply.
Require Import QedkConcrete.transactionchecks.
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))).

Local Abbreviation prefixAvoidsSignerCreation := (@consensusrequirements.prefixAvoidsSignerCreation F).
Local Abbreviation prefixValidStakingSyscalls := (@consensusrequirements.prefixValidStakingSyscalls F).
Local Abbreviation txCredits := (fun txs ⇒ Lists.List.fold_right
  (fun tx subtotal ⇒ @systemTxCredit F tx + subtotal) 0 txs).

Lemma system_tx_credit_nonnegative tx :
  transactionValueAndDataBounds tx → 0 ≤ @systemTxCredit F tx.
Proof.
  intro Hval. unfold systemTxCredit.
  destruct (@E.is_system_transaction F tx); [exact (proj1 (proj1 Hval)) | lia].
Qed.

Lemma tx_credits_nonnegative txs :
  Forall transactionValueAndDataBounds txs → 0 ≤ txCredits txs.
Proof.
  intro Htxs. induction Htxs as [|tx rest Htx Hrest IH]; [cbn; lia|].
  change (0 ≤ @systemTxCredit F tx + txCredits rest).
  pose proof (system_tx_credit_nonnegative tx Htx). lia.
Qed.

Lemma tx_credits_app first last :
  txCredits (first ++ last) = txCredits first + txCredits last.
Proof.
  induction first as [|tx first IH]; [reflexivity|].
  change (@systemTxCredit F tx + txCredits (first ++ last) =
    @systemTxCredit F tx + txCredits first + txCredits last).
  cbv beta in IH |- ×. rewrite IH. lia.
Qed.

Lemma system_tx_credit_allowance tx :
  @E.sender_kind_value F tx ≠ Some E.System ∨
  Numeric.U256.to_z (T.value tx) ≤ @systemTxCredit F tx.
Proof.
  unfold systemTxCredit, E.is_system_transaction.
  destruct (@E.sender_kind_value F tx) as [[s|]|];
    [left; discriminate | right; lia | left; discriminate].
Qed.

Lemma prefix_creation_safe_prefix signers block first last :
  prefixAvoidsSignerCreation signers block (first ++ last) →
  prefixAvoidsSignerCreation signers block first.
Proof.
  intros Hsafe before tx after reached Hsplit Hrun.
  apply (Hsafe before tx (after ++ last) reached).
  - rewrite Hsplit, <- app_assoc. reflexivity.
  - exact Hrun.
Qed.

Lemma prefix_creation_safe_tail signers block tx rest reached :
  prefixAvoidsSignerCreation signers block (tx :: rest) →
  dispatchTransaction block tx = inl reached →
  prefixAvoidsSignerCreation signers reached rest.
Proof.
  intros Hsafe Hstep before next after state Hsplit Hrun.
  apply (Hsafe (tx :: before) next after state).
  - cbn [app]. now rewrite <- Hsplit.
  - change (Result.op_letdollar (dispatchTransaction block tx)
      (fun start ⇒ Result.List.fold_leftM dispatchTransaction start before) = inl state).
    rewrite Hstep. exact Hrun.
Qed.

Lemma prefix_staking_valid_prefix block first last :
  prefixValidStakingSyscalls block (first ++ last) →
  prefixValidStakingSyscalls block first.
Proof.
  intros Hvalid before tx after reached Hsplit Hrun.
  apply (Hvalid before tx (after ++ last) reached).
  - rewrite Hsplit, <- app_assoc. reflexivity.
  - exact Hrun.
Qed.

Lemma prefix_staking_valid_tail block tx rest reached :
  prefixValidStakingSyscalls block (tx :: rest) →
  dispatchTransaction block tx = inl reached →
  prefixValidStakingSyscalls reached rest.
Proof.
  intros Hvalid Hstep before next after state Hsplit Hrun.
  apply (Hvalid (tx :: before) next after state).
  - cbn [app]. now rewrite <- Hsplit.
  - change (Result.op_letdollar (dispatchTransaction block tx)
      (fun start ⇒ Result.List.fold_leftM dispatchTransaction start before) = inl state).
    rewrite Hstep. exact Hrun.
Qed.

Lemma process_transaction_succeeds_from_validation state tx validation :
  @E.validate_transaction F state tx = inl validation →
  ∃ post, processTransaction state tx = inl post.
Proof.
  intro Hvalid.
  destruct (processTransaction state tx) as [post|err] eqn:Hrun.
  - ∃ post; reflexivity.
  - unfold E.process_transaction in Hrun. rewrite Hvalid in Hrun.
    change (@Result.op_letdollar) with
      (fun (A B C : Set) (r : A + B) (f : A → C + B) ⇒
        match r with inl v ⇒ f v | inr e ⇒ inr e end) in Hrun.
    destruct validation as [s fee p i]. cbn beta iota zeta in Hrun.
    match type of Hrun with context [Result.List.fold_left ?step ?init ?auths] ⇒
      destruct (Result.List.fold_left step init auths) as [authorities authorized]
    end.
    cbn beta iota zeta in Hrun.
    match type of Hrun with context [E.Host.call_from_eoa ?t ?m ?e] ⇒
      destruct (E.Host.call_from_eoa t m e) as [receipt after]
    end.
    discriminate Hrun.
Qed.

Theorem accepted_prefix_backed snapshot signers until txs block :
  (@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)) txs →
  prefixAvoidsSignerCreation signers block txs →
  ∀ post previous next,
    headerWords (Chain.Ethereum.Block.t.header (B.t.current_block block)) →
    balanceInvariant (B.t.world_state block) →
    totalBalance (B.t.world_state block) + txCredits txs < 2^256 →
    worldFormat (B.t.world_state block) →
    BudgetBacking snapshot previous (B.t.world_state block) signers (blockNumber block) until →
    (blockNumber block ≤ until < blockNumber block + 3)%N →
    (∀ prefix suffix reached,
      txs = prefix ++ suffix →
      Result.List.fold_leftM dispatchTransaction block prefix = inl reached →
      ∀ a, List.In a signers →
      configured (B.t.world_state reached) (blockNumber block) a =
        configured snapshot (blockNumber block) a) →
    checkTransactions snapshot (Chain.Ethereum.Block.t.header (B.t.current_block block)) previous txs = Some next →
    Result.List.fold_leftM dispatchTransaction block txs = inl post →
    BudgetBacking snapshot next (B.t.world_state post) signers (blockNumber block) until ∧
    balanceInvariant (B.t.world_state post) ∧ worldFormat (B.t.world_state post) ∧
    totalBalance (B.t.world_state post) ≤ totalBalance (B.t.world_state block) + txCredits txs ∧
    B.t.current_block post = B.t.current_block block.
Proof using F U8 UB UI0.
  intros Hrevision Hhash Houtputs Hchain Hsnapshot Hnative Hinputs Hsafe.
  revert block Hinputs Hsafe.
  induction txs as [|tx rest IH]; intros block Hinputs Hsafe post previous next
    Hheader Hbalance Hsupply Hformat Hbacked Htime Hconfig Haccepted Hrun.
  - cbn [checkTransactions] in Haccepted. injection Haccepted as <-.
    cbn [Result.List.fold_leftM] in Hrun. injection Hrun as <-.
    cbn [Lists.List.fold_right].
    split; [exact Hbacked|]. split; [exact Hbalance|]. split; [exact Hformat|].
    split; [lia|reflexivity].
  - inversion Hinputs as [|? ? [Hvalue [Hwidth [Hauthorities Hsenders]]] Hrest]; subst.
    cbn [checkTransactions] in Haccepted.
    destruct (checkTransaction snapshot _ previous tx) as [middle|] eqn:Hcheck; [|discriminate].
    change (Result.op_letdollar (dispatchTransaction block tx)
      (fun reached ⇒ Result.List.fold_leftM dispatchTransaction reached rest) = inl post) in Hrun.
    destruct (dispatchTransaction block tx) as [reached|error] eqn:Hstep; [|discriminate Hrun].
    assert (HrestNonneg : 0 ≤ txCredits rest).
    { apply tx_credits_nonnegative. eapply Forall_impl; [|exact Hrest].
      intros t [Hv _]; exact Hv. }
    assert (HtxNonneg : 0 ≤ @systemTxCredit F tx) by exact (system_tx_credit_nonnegative tx Hvalue).
    assert (Hsup : supply_with_credits (B.t.world_state block) (@systemTxCredit F tx :: [])).
    { split; [exact Hbalance|]. split; [constructor; [exact HtxNonneg | constructor]|].
      change (totalBalance (B.t.world_state block) + (@systemTxCredit F tx + txCredits rest) < 2^256) in Hsupply.
      change (totalBalance (B.t.world_state block) + (@systemTxCredit F tx + 0) < 2^256). lia. }
    destruct (@dispatch_transaction_supply_with_credits F UI0 UB U8
      block tx (@systemTxCredit F tx) [] reached Hrevision Hhash Houtputs Hchain
      (proj1 (proj2 Hheader)) (proj2 (proj2 Hheader)) Hsup Hformat Hwidth Hvalue
      (system_tx_credit_allowance tx) Hstep) as [HsupStep HtotalStep].
    apply supply_with_credits_nil in HsupStep.
    assert (HsupplyStep : totalBalance (B.t.world_state reached) + txCredits rest < 2^256).
    { change (totalBalance (B.t.world_state block) + (@systemTxCredit F tx + txCredits rest) < 2^256) in Hsupply.
      lia. }
    pose proof (@dispatch_transaction_preserves_format F
      block tx Hrevision Hwidth Hformat) as HformatStep.
    rewrite Hstep in HformatStep.
    pose proof (@dispatch_transaction_current_block F
      block tx reached Hstep) as Hblock.
    destruct (check_transaction_history _ _ _ _ _ Hcheck) as [sender [Hsender_rec _]].
    destruct Hbacked as [Hhistory Hbudgets Hidle].
    assert (HbackedParts :
      HistoryMatches snapshot (consensusHistory middle) (B.t.world_state reached) signers until ∧
      (∀ a, List.In a signers →
        0 ≤ consensusBudgets middle a ∧
        consensusBudgets middle a ≤ balance (B.t.world_state reached) a ∧
        consensusBudgets middle a ≤ Numeric.U256.to_z (configured snapshot (blockNumber block) a)) ∧
      (∀ a, List.In a signers → a ≠ systemSender → ∀ future, (future ≤ until)%N →
        @emptyingEligible (B.t.world_state reached) a future = true →
        balance snapshot a ≤ balance (B.t.world_state reached) a)).
    { destruct (bool_decide_reflect (sender = systemSender)) as [Hsys | HnotSys].
      - subst sender.
        destruct (check_system_transaction_dispatch _ _ _ _ _ Hsender_rec Hcheck)
          as [_ [_ HdispEq]].
        pose proof Hstep as HsysStep. rewrite HdispEq in HsysStep.
        exact (@accepted_system_transaction_preserves_budget F
          snapshot previous middle signers until block tx reached
          Hhistory (fun a Ha ⇒ proj2 (Hnative a Ha)) Hsender_rec HsysStep
          Hbudgets Hidle Hcheck).
      - assert (HprocEq : dispatchTransaction block tx = processTransaction block tx).
        { pose proof Hcheck as Hchk.
          unfold checkTransaction in Hchk.
          rewrite Hsender_rec, (bool_decide_eq_false_2 _ HnotSys) in Hchk.
          destruct (transactionQuote _ tx) as [[qs qfee]|]; [|discriminate Hchk].
          match type of Hchk with context [transactionChecks ?h ?t ?n] ⇒
            destruct (transactionChecks h t n) eqn:Hchecks
          end; [|discriminate Hchk].
          exact (proj2 (proj2 (@user_sender_dispatch_eq F _ tx sender _ Hsender_rec HnotSys Hchecks)) block). }
        pose proof Hstep as HprocStep. rewrite HprocEq in HprocStep.
        assert (Hvalid : ∃ validation, @E.validate_transaction F block tx = inl validation).
        { unfold E.process_transaction in HprocStep.
          destruct (@E.validate_transaction F block tx) as [validation|error];
            [∃ validation; reflexivity|discriminate HprocStep]. }
        destruct Hvalid as [validation Hvalid].
        pose proof (@validated_sender_recovered F block tx validation Hvalid) as HvalSender.
        change (T.sender _ tx) with (@transactionSender F tx) in HvalSender.
        assert (HeqSender : E.transaction_validation.sender validation = sender)
          by congruence.
        assert (HsenderIn : List.In (E.transaction_validation.sender validation) signers)
          by (rewrite HeqSender; exact (Hsenders sender Hsender_rec)).
        assert (HvalNotSys : E.transaction_validation.sender validation ≠ systemSender)
          by (rewrite HeqSender; exact HnotSys).
        pose proof (@accepted_transaction_preserves_budget F UI0 UB U8
          snapshot previous middle signers until
          block tx validation reached Hrevision Hhash Houtputs Hchain Hheader Hbalance Hformat
          Hhistory Hsnapshot Hnative Hwidth Hvalue Hvalid HprocStep HsenderIn HvalNotSys Htime
          (Hconfig [] (tx :: rest) block eq_refl eq_refl) Hbudgets Hidle Hcheck) as HbackedStep.
        destruct (vmInput block tx validation) as [msg entry] eqn:Hinput.
        assert (Hcreated : ∀ a, List.In a signers →
          ¬ List.In a (S.t.accounts_created_in_current_transaction (snd (callFromEoa tx msg entry)))).
        { intros a Ha. pose proof (Hsafe [] tx rest block eq_refl eq_refl validation a (or_introl Ha) Hvalid
            (history_noncontract _ _ _ _ _ Hhistory a Ha)) as Hcreation.
          rewrite Hinput in Hcreation. exact Hcreation. }
        exact (HbackedStep Hcreated). }
    destruct HbackedParts as [HhistoryStep [HbudgetsStep HidleStep]].
    assert (HtimeStep : (blockNumber reached ≤ until < blockNumber reached + 3)%N).
    { cbv beta. rewrite Hblock. exact Htime. }
    assert (HheaderStep : headerWords (Chain.Ethereum.Block.t.header (B.t.current_block reached))).
    { rewrite Hblock. exact Hheader. }
    assert (HbackingStep : BudgetBacking snapshot middle (B.t.world_state reached) signers
        (blockNumber reached) until).
    { cbv beta. rewrite Hblock. constructor; assumption. }
    assert (HconfigStep : ∀ prefix suffix final,
      rest = prefix ++ suffix → Result.List.fold_leftM dispatchTransaction reached prefix = inl final →
      ∀ a, List.In a signers → configured (B.t.world_state final) (blockNumber reached) a =
        configured snapshot (blockNumber reached) a).
    { intros prefix suffix final Hsplit Hprefix a Ha. cbv beta. rewrite Hblock.
      apply (Hconfig (tx :: prefix) suffix final).
      - cbn [app]. now rewrite <- Hsplit.
      - change (Result.op_letdollar (dispatchTransaction block tx)
          (fun first ⇒ Result.List.fold_leftM dispatchTransaction first prefix) = inl final).
        rewrite Hstep. exact Hprefix.
      - exact Ha. }
    assert (HcheckRest : checkTransactions snapshot
      (Chain.Ethereum.Block.t.header (B.t.current_block reached)) middle rest = Some next).
    { rewrite Hblock. exact Haccepted. }
    destruct (IH reached Hrest (prefix_creation_safe_tail signers block tx rest reached Hsafe Hstep)
      post middle next HheaderStep HsupStep HsupplyStep HformatStep HbackingStep HtimeStep
      HconfigStep HcheckRest Hrun) as [Hpost [HpostBalance [HpostFormat [HpostTotal HpostBlock]]]].
    cbv beta in Hpost. rewrite Hblock in Hpost.
    cbn [Lists.List.fold_right].
    split; [exact Hpost|]. split; [exact HpostBalance|]. split; [exact HpostFormat|].
    split; [lia | exact (eq_trans HpostBlock Hblock)].
Qed.

Successful processing is required only of the preceding transactions. Each reached user EOA candidate is proved to validate, using the simulated nonce and public checks as well as its reserve-backed fee. The configuration premise here remains the separate delayed-storage obligation.
Theorem accepted_prefix_next_fee snapshot signers until prefix candidate suffix block reached 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)) prefix →
  prefixAvoidsSignerCreation signers block prefix →
  (∀ a, transactionSender candidate = Some a → List.In a signers) →
  transactionSender candidate ≠ Some systemSender →
  headerWords (Chain.Ethereum.Block.t.header (B.t.current_block block)) →
  balanceInvariant (B.t.world_state block) →
  totalBalance (B.t.world_state block) + txCredits prefix < 2^256 →
  worldFormat (B.t.world_state block) →
  BudgetBacking snapshot previous (B.t.world_state block) signers (blockNumber block) until →
  (blockNumber block ≤ until < blockNumber block + 3)%N →
  (∀ before after state,
    prefix = before ++ after → Result.List.fold_leftM dispatchTransaction block before = inl state →
    ∀ a, List.In a signers → configured (B.t.world_state state) (blockNumber block) a =
      configured snapshot (blockNumber block) a) →
  checkTransactions snapshot (Chain.Ethereum.Block.t.header (B.t.current_block block)) previous
    (prefix ++ candidate :: suffix) = Some next →
  Result.List.fold_leftM dispatchTransaction block prefix = inl reached →
  ∃ sender fee,
    transactionQuote (Chain.Ethereum.Block.t.header (B.t.current_block reached)) candidate = Some (sender, fee) ∧
    Z.of_N fee ≤ balance (B.t.world_state reached) sender ∧
    (∀ details, @E.validate_transaction F reached candidate ≠
      @E.invalid_transaction F E.transaction_validation (B.t.current_block reached) candidate
        (Execution.Error.Insufficient_balance details)) ∧
    ∃ validation, @E.validate_transaction F reached candidate = inl validation.
Proof using F U8 UB UI0.
  intros Hrevision Hhash Houtputs Hchain Hsnapshot Hnative Hinputs Hsafe Hsender HnotSysCand
    Hheader Hbalance Hsupply Hformat Hbacked Htime Hconfig Hcheck Hrun.
  rewrite check_transactions_app in Hcheck.
  destruct (checkTransactions snapshot _ previous prefix) as [middle|] eqn:Hprefix; [|discriminate].
  cbn [checkTransactions] in Hcheck.
  destruct (checkTransaction snapshot _ middle candidate) as [after|] eqn:Hcandidate; [|discriminate].
  destruct (accepted_prefix_backed snapshot signers until prefix block Hrevision Hhash Houtputs Hchain
    Hsnapshot Hnative Hinputs Hsafe reached previous middle Hheader Hbalance Hsupply Hformat
    Hbacked Htime Hconfig Hprefix Hrun) as [[Hhistory Hnumbers Hidle] [_ [_ [_ Hblock]]]].
  destruct (@check_transaction_fee_backed F snapshot middle after signers until reached candidate
    Hhistory Hsender HnotSysCand ltac:(rewrite Hblock; exact (proj1 Htime))
    (fun a Ha ⇒ proj1 (proj2 (Hnumbers a Ha)))
    ltac:(intros a Ha HnotSys_a Heligible; apply (Hidle a Ha HnotSys_a (blockNumber reached));
      [cbv beta; rewrite Hblock; exact (proj1 Htime)|exact Heligible])
    ltac:(rewrite Hblock; exact Hcandidate)) as [sender [fee [Hquote Hfee]]].
  assert (HsenderNotSys : sender ≠ systemSender).
  { unfold transactionQuote in Hquote.
    destruct (transactionSender candidate) as [s|] eqn:Hrec; [|discriminate Hquote].
    destruct (Gas.tx_effective_gas_price _ candidate); [|discriminate Hquote].
    injection Hquote as <- _. intro Heq; subst s. exact (HnotSysCand eq_refl). }
  ∃ sender, fee. split; [exact Hquote|]. split; [exact Hfee|].
  split.
  - exact (affordable_quote_no_insufficient_balance reached candidate sender fee Hquote Hfee).
  - exact (proj2 (@check_transaction_validates F snapshot middle after signers until reached candidate
      sender fee Hhistory Hsnapshot Hsender HsenderNotSys Hquote Hfee
      ltac:(rewrite Hblock; exact Hcandidate))).
Qed.

Every reached transaction (user EOA or Monad staking system transaction) dispatches without error when prefixValidStakingSyscalls holds.
Theorem accepted_prefix_next_dispatch snapshot signers until prefix candidate suffix block reached 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)) prefix →
  prefixAvoidsSignerCreation signers block prefix →
  prefixValidStakingSyscalls block (prefix ++ candidate :: suffix) →
  (∀ a, transactionSender candidate = Some a → List.In a signers) →
  headerWords (Chain.Ethereum.Block.t.header (B.t.current_block block)) →
  balanceInvariant (B.t.world_state block) →
  totalBalance (B.t.world_state block) + txCredits prefix < 2^256 →
  worldFormat (B.t.world_state block) →
  BudgetBacking snapshot previous (B.t.world_state block) signers (blockNumber block) until →
  (blockNumber block ≤ until < blockNumber block + 3)%N →
  (∀ before after state,
    prefix = before ++ after → Result.List.fold_leftM dispatchTransaction block before = inl state →
    ∀ a, List.In a signers → configured (B.t.world_state state) (blockNumber block) a =
      configured snapshot (blockNumber block) a) →
  checkTransactions snapshot (Chain.Ethereum.Block.t.header (B.t.current_block block)) previous
    (prefix ++ candidate :: suffix) = Some next →
  Result.List.fold_leftM dispatchTransaction block prefix = inl reached →
  ∃ nextState, dispatchTransaction reached candidate = inl nextState.
Proof using F U8 UB UI0.
  intros Hrevision Hhash Houtputs Hchain Hsnapshot Hnative Hinputs Hsafe Hstaking Hsender
    Hheader Hbalance Hsupply Hformat Hbacked Htime Hconfig Hcheck Hrun.
  pose proof Hcheck as HcheckAll.
  rewrite check_transactions_app in Hcheck.
  destruct (checkTransactions snapshot _ previous prefix) as [middle|] eqn:Hprefix; [|discriminate].
  cbn [checkTransactions] in Hcheck.
  destruct (checkTransaction snapshot _ middle candidate) as [after|] eqn:Hcandidate; [|discriminate].
  destruct (check_transaction_history _ _ _ _ _ Hcandidate) as [sender [Hsender_rec _]].
  destruct (bool_decide_reflect (sender = systemSender)) as [Hsys | HnotSys].
  - subst sender.
    destruct (accepted_prefix_backed snapshot signers until prefix block Hrevision Hhash Houtputs Hchain
      Hsnapshot Hnative Hinputs Hsafe reached previous middle Hheader Hbalance Hsupply Hformat
      Hbacked Htime Hconfig Hprefix Hrun) as [[Hhistory _ _] [_ [_ [_ Hblock]]]].
    destruct (check_system_transaction_dispatch _ _ _ _ _ Hsender_rec Hcandidate)
      as [_ [HisSys HdispEq]].
    pose proof (Hstaking prefix candidate suffix reached eq_refl Hrun HisSys) as HvalidSys.
    destruct (@check_system_transaction_executes F snapshot middle after signers until reached candidate
      Hhistory (Hsender systemSender Hsender_rec) Hsender_rec HvalidSys
      ltac:(rewrite Hblock; exact Hcandidate)) as [_ [post Hpost]].
    ∃ post. rewrite HdispEq. exact Hpost.
  - assert (HnotSysCand : transactionSender candidate ≠ Some systemSender)
      by (rewrite Hsender_rec; congruence).
    destruct (accepted_prefix_next_fee snapshot signers until prefix candidate suffix block reached
      previous next Hrevision Hhash Houtputs Hchain Hsnapshot Hnative Hinputs Hsafe Hsender HnotSysCand
      Hheader Hbalance Hsupply Hformat Hbacked Htime Hconfig HcheckAll Hrun)
      as [qsender [fee [Hquote [Hfee [_ [validation Hvalid]]]]]].
    destruct (accepted_prefix_backed snapshot signers until prefix block Hrevision Hhash Houtputs Hchain
      Hsnapshot Hnative Hinputs Hsafe reached previous middle Hheader Hbalance Hsupply Hformat
      Hbacked Htime Hconfig Hprefix Hrun) as [[Hhistory _ _] [_ [_ [_ Hblock]]]].
    assert (HqNotSys : qsender ≠ systemSender).
    { unfold transactionQuote in Hquote. rewrite Hsender_rec in Hquote.
      destruct (Gas.tx_effective_gas_price _ candidate); [|discriminate Hquote].
      injection Hquote as <- _. exact HnotSys. }
    destruct (proj1 (@check_transaction_validates F snapshot middle after signers until reached candidate
      qsender fee Hhistory Hsnapshot Hsender HqNotSys Hquote Hfee
      ltac:(rewrite Hblock; exact Hcandidate))) as [_ [_ HdispEq]].
    destruct (process_transaction_succeeds_from_validation reached candidate validation Hvalid)
      as [post Hpost].
    ∃ post. rewrite HdispEq. exact Hpost.
Qed.

End Execution.