Library QedkConcrete.consensusbacking

Invariant connecting public budgets to execution

Carried budgets cover later fees. An account still eligible to empty needs a different bound: its snapshot balance has not decreased. The history projection tells us exactly when that second bound is needed.
From Stdlib Require Import List ZArith Lia.
Require Import RocqOfOCaml.RocqOfOCaml.
Require Import QedkConcrete.consensustransaction QedkConcrete.consensuswindow
  QedkConcrete.transactionhistoryprojection QedkConcrete.transactioncode.
Set Default Goal Selector "!".
Local Open Scope Z_scope.
Module E := Execution.Make.
Module W := State.WorldState.
Module A := Chain.Ethereum.Account.
Module Addr := Chain.Ethereum.Address.

Section Backing.
Context {F : E.FArgs}.
Local Abbreviation readAccount := (fun world addr ⇒
  State.op_pointcaretlparenrparen world (@W.account None addr)).
Local Abbreviation balance := (fun world a ⇒ Numeric.U256.to_z (A.t.balance (readAccount world a))).
Local Abbreviation configured := (@Reserve_balance.user_reserve_balance).

Record BudgetBacking (snapshot : W.t) (consensus : ConsensusState)
    (world : W.t) (signers : list Addr.t) (height until : N) : Prop := {
  backing_history : HistoryMatches snapshot (consensusHistory consensus) world signers until;
  backing_numbers : ∀ a, List.In a signers →
    0 ≤ consensusBudgets consensus a ∧
    consensusBudgets consensus a ≤ balance world a ∧
    consensusBudgets consensus a ≤ Numeric.U256.to_z (configured snapshot height a);
  backing_idle : ∀ a, List.In a signers → a ≠ Chain.Monad.system_sender →
    ∀ future, (future ≤ until)%N →
    @emptyingEligible world a future = true → balance snapshot a ≤ balance world a
}.

Credits do not refill consensus budgets. Exact history agreement also prevents a state change from silently enabling the snapshot-balance exemption for an account whose balance has already been spent.
Lemma backing_nondebit snapshot consensus before after signers height until :
  BudgetBacking snapshot consensus before signers height until →
  HistoryMatches snapshot (consensusHistory consensus) after signers until →
  (∀ a, List.In a signers → balance before a ≤ balance after a) →
  BudgetBacking snapshot consensus after signers height until.
Proof.
  intros [Hhistory Hnumbers Hidle] Hafter Hbalance. constructor; [exact Hafter| |].
  - intros a Ha. specialize (Hnumbers a Ha). specialize (Hbalance a Ha). lia.
  - intros a Ha HnotSys future Hfuture Heligible.
    assert (Hbefore : @emptyingEligible before a future = true).
    { rewrite (history_emptying_agreement _ _ _ _ _ Hhistory a Ha HnotSys future Hfuture),
        <- (history_emptying_agreement _ _ _ _ _ Hafter a Ha HnotSys future Hfuture).
      exact Heligible. }
    eapply Z.le_trans; [exact (Hidle a Ha HnotSys future Hfuture Hbefore)|exact (Hbalance a Ha)].
Qed.

A newly effective lowering tightens every budget before the next block. A raise cannot restore already spent fees. Neither changes nonce/activity history or the balance needed for the emptying case.
Lemma backing_next_block snapshot consensus world signers height nextHeight until :
  BudgetBacking snapshot consensus world signers height until →
  (∀ a, List.In a signers → 0 ≤ Numeric.U256.to_z (configured snapshot nextHeight a)) →
  BudgetBacking snapshot
    {| consensusHistory := consensusHistory consensus;
       consensusBudgets := fun a ⇒ Z.min (consensusBudgets consensus a)
         (Numeric.U256.to_z (configured snapshot nextHeight a)) |}
    world signers nextHeight until.
Proof.
  intros [Hhistory Hnumbers Hidle] Hlevel. constructor; [exact Hhistory| |exact Hidle].
  intros a Ha. specialize (Hnumbers a Ha). specialize (Hlevel a Ha).
  cbn [consensusBudgets]. lia.
Qed.

End Backing.