Library QedkConcrete.transactionbudget

Preserving the consensus budget through actual transactions

The arithmetic check uses a snapshot balance and a carried budget. The lemmas below connect its two branches to the generated executor, including fee debit, authorization processing, VM rollback and finalization. These are induction steps, not the complete delayed-window theorem.
From Stdlib Require Import List String ZArith Lia.
Require Import RocqOfOCaml.RocqOfOCaml.
Require Import MonadVM.Execution QedkConcrete.transactionstage
  QedkConcrete.transactionchecked QedkConcrete.consensusbudget
  QedkConcrete.publictransaction QedkConcrete.supply QedkConcrete.wordrange
  QedkConcrete.hashwords QedkConcrete.hostcontextwords QedkConcrete.hostaccountformat
  QedkConcrete.assemblychecked QedkConcrete.validation QedkConcrete.transactionhistory.
Require Import QedkConcrete.nativeinputs.
Set Default Goal Selector "!".
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.

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 readAccount := (fun world addr ⇒ State.op_pointcaretlparenrparen
  world (@W.account None addr)).

Local Abbreviation configured := (@Reserve_balance.user_reserve_balance).
Local Abbreviation emptying := (@Reserve_balance.is_emptying_tx).
Local Abbreviation balance := (fun world addr ⇒ Numeric.U256.to_z (A.t.balance (readAccount world addr))).
Local Abbreviation blockNumber := (fun block ⇒ Chain.Ethereum.Block.Header.t.number
  (Chain.Ethereum.Block.t.header (B.t.current_block block))).

No map-entry assumption is needed: a previously absent account has zero balance and hence no positive protected budget to lose.
Theorem process_transaction_protected_balance block tx validation post query :
  MonadVM.Staking.staking_address ≠ query →
  (@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)) →
  headerWords (Chain.Ethereum.Block.t.header (B.t.current_block block)) →
  balanceInvariant (B.t.world_state block) → worldFormat (B.t.world_state block) →
  Forall (fun auth ⇒ OCamlString.length (T.Authorization.t.address auth) = 20)
    (T.authorization_list tx) →
  transactionValueAndDataBounds tx →
  @E.validate_transaction F block tx = inl validation →
  processTransaction block tx = inl post →
  @A.is_smart_contract (readAccount (B.t.world_state block) query) = false →
  let '(msg, entry) := vmInput block tx validation in
  (query = E.transaction_validation.sender validation → fst (emptying entry) = false) →
  ¬ List.In query (S.t.accounts_created_in_current_transaction (snd (callFromEoa tx msg entry))) →
  Z.min (balance (B.t.world_state block) query)
    (Numeric.U256.to_z (configured (B.t.world_state block) (blockNumber block) query)) -
    (if String.eqb (E.transaction_validation.sender validation) query
     then Numeric.U256.to_z (E.transaction_validation.total_fee validation) else 0) ≤
  balance (B.t.world_state post) query.
Proof using F U8 UB UI0.
  intros Hnot_staking Hrevision Hhash Hnative Hchain Hheader Hinitial Hformat Hwidth Hvalue Hvalidation Hrun Hsc.
  destruct (vmInput block tx validation) as [msg entry] eqn:Hinput.
  intros Hempty Hcreated.
  destruct (Chain.Ethereum.Address.Map.find_opt query (W.t.accounts (B.t.world_state block)))
    as [original|] eqn:Hfind.
  - assert (Horiginal : readAccount (B.t.world_state block) query = original).
    { change ((match Chain.Ethereum.Address.Map.find_opt query (W.t.accounts (B.t.world_state block)) with
        Some ac ⇒ ac | None ⇒ @A.empty end) = original). rewrite Hfind. reflexivity. }
    destruct (String.eqb (E.transaction_validation.sender validation) query) eqn:Heq.
    + apply String.eqb_eq in Heq. subst query.
      pose proof (@process_transaction_sender_reserve F UI0 UB U8
        block tx validation post original
        Hrevision Hhash Hnative Hchain Hheader Hinitial Hformat Hwidth Hvalue Hvalidation Hrun
        Hnot_staking Hfind Hsc) as Hbound.
      rewrite Hinput in Hbound. specialize (Hbound (Hempty eq_refl) Hcreated).
      cbv beta. rewrite Horiginal. rewrite reservecheck.u256_min in Hbound. lia.
    + assert (Hsender : query ≠ E.transaction_validation.sender validation)
        by (apply String.eqb_neq in Heq; congruence).
      pose proof (@process_transaction_nonsender_reserve F UI0 UB U8
        block tx validation post query original Hnot_staking
        Hrevision Hhash Hnative Hchain Hheader Hinitial Hformat Hwidth Hvalue Hvalidation Hrun
        Hfind Hsc Hsender) as Hbound.
      rewrite Hinput in Hbound. specialize (Hbound Hcreated).
      cbv beta. rewrite Horiginal. rewrite reservecheck.u256_min in Hbound. lia.
  - assert (Hzero : balance (B.t.world_state block) query = 0).
    { change (Numeric.U256.to_z (A.t.balance
        (match Chain.Ethereum.Address.Map.find_opt query (W.t.accounts (B.t.world_state block)) with
         Some ac ⇒ ac | None ⇒ @A.empty end)) = 0). rewrite Hfind. reflexivity. }
    pose proof (@process_transaction_preserves_supply F UI0 UB U8
      block tx post Hrevision Hhash Hnative Hchain
      (proj1 (proj2 Hheader)) (proj2 (proj2 Hheader)) Hinitial Hformat Hwidth Hvalue Hrun) as [Hpost _].
    pose proof (@account_balance_bounded_by_supply (B.t.world_state post) query
      (proj1 (proj2 Hpost))) as Hafter.
    pose proof (@account_balance_bounded_by_supply (B.t.world_state block)
      (E.transaction_validation.sender validation) (proj1 (proj2 Hinitial))) as Hsender.
    pose proof (proj2 (proj2 Hinitial)) as Htotal.
    pose proof (validated_debit_range block tx validation Hvalidation ltac:(cbn beta in Hsender; lia))
      as [Hfee _].
    rewrite Hzero. rewrite Hfee. cbv beta in Hafter |- ×.
    pose proof (N2Z.is_nonneg (T.gas_limit tx × E.transaction_validation.effective_gas_price validation)).
    destruct (String.eqb (E.transaction_validation.sender validation) query); lia.
Qed.

The next transaction's fee check is justified before invoking its validator. The premises here are the window induction's numerical invariant, not successful validation or an EVM balance contract.
Theorem budget_check_no_insufficient_balance block tx sender fee
    empty snapshot threshold previous value remaining :
  transactionQuote (Chain.Ethereum.Block.t.header (B.t.current_block block)) tx = Some (sender, fee) →
  checkSenderBudget empty snapshot threshold previous (Z.of_N fee) value = Some remaining →
  previous ≤ balance (B.t.world_state block) sender →
  (empty = true → snapshot ≤ balance (B.t.world_state block) sender) →
  ∀ details,
    @E.validate_transaction F block tx ≠
      @E.invalid_transaction F E.transaction_validation (B.t.current_block block) tx
        (Execution.Error.Insufficient_balance details).
Proof.
  intros Hquote Haccepted Hbudget Hempty.
  apply affordable_quote_no_insufficient_balance with (sender := sender) (fee := fee); [exact Hquote|].
  eapply accepted_sender_fee; eassumption.
Qed.

After execution, the accepted sender budget is still backed by the concrete balance. Emptying and protected-sender branches use different proved VM bounds, rather than treating an emptying account as protected.
Theorem process_transaction_sender_budget block tx validation post
    empty snapshot previous remaining :
  MonadVM.Staking.staking_address ≠ E.transaction_validation.sender validation →
  (@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)) →
  headerWords (Chain.Ethereum.Block.t.header (B.t.current_block block)) →
  balanceInvariant (B.t.world_state block) → worldFormat (B.t.world_state block) →
  Forall (fun auth ⇒ OCamlString.length (T.Authorization.t.address auth) = 20)
    (T.authorization_list tx) →
  transactionValueAndDataBounds tx →
  @E.validate_transaction F block tx = inl validation →
  processTransaction block tx = inl post →
  let sender := E.transaction_validation.sender validation in
  let threshold := Numeric.U256.to_z (configured (B.t.world_state block) (blockNumber block) sender) in
  checkSenderBudget empty snapshot threshold previous
    (Z.of_N (T.gas_limit tx × E.transaction_validation.effective_gas_price validation))
    (Numeric.U256.to_z (T.value tx)) = Some remaining →
  previous ≤ balance (B.t.world_state block) sender → previous ≤ threshold →
  (empty = true → snapshot ≤ balance (B.t.world_state block) sender) →
  let '(msg, entry) := vmInput block tx validation in
  empty = fst (emptying entry) →
  ¬ List.In sender (S.t.accounts_created_in_current_transaction (snd (callFromEoa tx msg entry))) →
  0 ≤ remaining ∧ remaining ≤ threshold ∧ remaining ≤ balance (B.t.world_state post) sender.
Proof using F U8 UB UI0.
  intros Hnot_staking Hrevision Hhash Hnative Hchain Hheader Hinitial Hformat Hwidth Hvalue Hvalidation Hrun
    sender threshold Haccepted Hbudget Hthreshold Hsnapshot.
  assert (Hlevel : 0 ≤ threshold).
  { exact (proj1 (@configured_word_range

       (B.t.world_state block) (blockNumber block) sender Hformat)). }
  pose proof (@account_balance_bounded_by_supply (B.t.world_state block) sender
    (proj1 (proj2 Hinitial))) as HsenderBalance.
  pose proof (proj2 (proj2 Hinitial)) as Htotal.
  pose proof (validated_debit_range block tx validation Hvalidation ltac:(cbn beta in HsenderBalance; unfold sender in HsenderBalance; lia))
    as [Hfee _].
  pose proof (validated_sender_noncontract block tx validation Hvalidation) as Hsc.
  pose proof (process_transaction_protected_balance block tx validation post sender Hnot_staking
    Hrevision Hhash Hnative Hchain Hheader Hinitial Hformat Hwidth Hvalue Hvalidation Hrun Hsc) as Hprotected.
  pose proof (@process_transaction_emptying_sender F UI0 UB U8
    block tx validation post
    Hrevision Hhash Hnative Hchain Hheader Hinitial Hformat Hwidth Hvalue Hvalidation Hrun
    Hnot_staking) as Hemptying.
  destruct (vmInput block tx validation) as [msg entry] eqn:Hinput.
  cbn beta iota zeta delta [fst snd] in Hprotected, Hemptying |- ×.
  intros Hempty Hcreated.
  split; [eapply accepted_sender_budget_nonnegative; eassumption|].
  split.
  - eapply accepted_sender_budget_threshold; [apply N2Z.is_nonneg|exact Hthreshold|exact Haccepted].
  - eapply accepted_sender_budget_preserved; [exact Haccepted|exact Hbudget|exact Hthreshold|exact Hsnapshot| |].
    + intro Hfalse. specialize (Hprotected (fun _ ⇒ eq_trans (eq_sym Hempty) Hfalse) Hcreated).
      fold sender in Hprotected. rewrite String.eqb_refl, Hfee in Hprotected.
      exact Hprotected.
    + intro Htrue. specialize (Hemptying (eq_trans (eq_sym Hempty) Htrue) Hcreated).
      rewrite Hfee in Hemptying. exact Hemptying.
Qed.

Only the sender's budget is debited. A delegated nonsender may lose balance during this transaction, but not below its protected reserve; that is enough to preserve every other tracked sender's budget.
Theorem process_transaction_budget_map block tx validation post
    (signers : list Chain.Ethereum.Address.t) empty snapshot (budgets : Chain.Ethereum.Address.t → Z) remaining :
  (@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)) →
  headerWords (Chain.Ethereum.Block.t.header (B.t.current_block block)) →
  balanceInvariant (B.t.world_state block) → worldFormat (B.t.world_state block) →
  Forall (fun auth ⇒ OCamlString.length (T.Authorization.t.address auth) = 20)
    (T.authorization_list tx) →
  transactionValueAndDataBounds tx →
  @E.validate_transaction F block tx = inl validation →
  processTransaction block tx = inl post →
  let sender := E.transaction_validation.sender validation in
  let thresholds := fun a ⇒ Numeric.U256.to_z
    (configured (B.t.world_state block) (blockNumber block) a) in
  List.In sender signers →
  (∀ a, List.In a signers → MonadVM.Staking.staking_address ≠ a) →
  (∀ a, List.In a signers →
    @A.is_smart_contract (readAccount (B.t.world_state block) a) = false) →
  (∀ a, List.In a signers →
    0 ≤ budgets a ∧ budgets a ≤ balance (B.t.world_state block) a ∧ budgets a ≤ thresholds a) →
  checkSenderBudget empty snapshot (thresholds sender) (budgets sender)
    (Z.of_N (T.gas_limit tx × E.transaction_validation.effective_gas_price validation))
    (Numeric.U256.to_z (T.value tx)) = Some remaining →
  (empty = true → snapshot ≤ balance (B.t.world_state block) sender) →
  let '(msg, entry) := vmInput block tx validation in
  empty = fst (emptying entry) →
  (∀ a, List.In a signers →
    ¬ List.In a (S.t.accounts_created_in_current_transaction (snd (callFromEoa tx msg entry)))) →
  ∀ a, List.In a signers →
    let next := if String.eqb sender a then remaining else budgets a in
    0 ≤ next ∧ next ≤ balance (B.t.world_state post) a ∧ next ≤ thresholds a.
Proof using F U8 UB UI0.
  intros Hrevision Hhash Hnative Hchain Hheader Hinitial Hformat Hwidth Hvalue Hvalidation Hrun
    sender thresholds Hsender Hnot_staking Hnoncontract Hbudgets Haccepted Hsnapshot.
  destruct (Hbudgets sender Hsender) as [_ [Hbudget Hthreshold]].
  pose proof (process_transaction_sender_budget block tx validation post empty snapshot (budgets sender) remaining
    (Hnot_staking sender Hsender) Hrevision Hhash Hnative Hchain Hheader Hinitial Hformat Hwidth Hvalue Hvalidation Hrun
    Haccepted Hbudget Hthreshold Hsnapshot) as HsenderBudget.
  destruct (vmInput block tx validation) as [msg entry] eqn:Hinput.
  cbn beta iota zeta delta [fst snd] in HsenderBudget |- ×.
  intros Hempty Hcreated a Hin.
  cbn zeta. destruct (String.eqb sender a) eqn:Heq.
  - apply String.eqb_eq in Heq. subst a.
    specialize (HsenderBudget Hempty (Hcreated sender Hsender)). tauto.
  - destruct (Hbudgets a Hin) as [Hnonnegative [Hbalance Hlevel]].
    pose proof (process_transaction_protected_balance block tx validation post a
      (Hnot_staking a Hin) Hrevision Hhash Hnative Hchain Hheader Hinitial Hformat Hwidth Hvalue Hvalidation Hrun
      (Hnoncontract a Hin)) as Hbound.
    rewrite Hinput in Hbound.
    assert (Hdifferent : a ≠ E.transaction_validation.sender validation)
      by (apply String.eqb_neq in Heq; unfold sender in Heq; congruence).
    specialize (Hbound ltac:(intro H; contradiction) (Hcreated a Hin)).
    fold sender in Hbound. rewrite Heq in Hbound.
    fold (thresholds a) in Hbound. repeat split; try assumption; lia.
Qed.

End Execution.