Library SetcodeConcrete.consensusnonce

Nonce exhaustion is excluded by initial conditions and public checks

This file connects the actual experimental consensus checks to complete block execution. The intermediate NonceAdmission property is proved from those checks, not imposed as an additional theorem hypothesis. Other transaction/block failures and fee coverage remain separate.
From Stdlib Require Import List String ZArith Bool Lia.
Require Import SetcodeConcrete.consensuschecks SetcodeConcrete.validation
  SetcodeConcrete.windownonce SetcodeConcrete.noncebudget
  SetcodeConcrete.blockcode SetcodeConcrete.noncesequence
  QedkConcrete.admissionnonces QedkConcrete.transactionchecks.
Set Default Goal Selector "!".
Import ListNotations.

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

Lemma checked_transactions_nonce_admission snapshot header previous txs next suffix :
  checkTransactions snapshot header previous txs = Some next →
  NonceAdmission (eligibleAt snapshot) (admissionNonces next) suffix →
  NonceAdmission (eligibleAt snapshot) (admissionNonces previous) (txs ++ suffix).
Proof.
  revert previous. induction txs as [|tx rest IH]; intros previous Hcheck Hsuffix.
  - cbn [checkTransactions] in Hcheck. injection Hcheck as <-. exact Hsuffix.
  - cbn [checkTransactions] in Hcheck.
    destruct (checkTransaction snapshot header previous tx) as [middle|] eqn:Htx; [|discriminate].
    pose proof (IH middle Hcheck Hsuffix) as Htail.
    unfold checkTransaction in Htx.
    destruct (T.sender _ tx) as [sender|] eqn:Hsender; [|discriminate].
    destruct (Gas.tx_effective_gas_price _ tx); [|discriminate].
    destruct (transactionChecks header tx _ _) eqn:Hchecks; [|discriminate].
    cbn zeta in Htx. match type of Htx with context [if ?test then _ else _] ⇒
      destruct test end; [|discriminate].
    injection Htx as <-. cbn [app]. apply nonce_admission_cons with (sender := sender).
    + exact Hsender.
    + exact (checked_nonce_matches _ _ _ _ Hchecks).
    + exact Htail.
Qed.

Lemma checked_blocks_nonce_admission snapshot previous blocks next suffix :
  checkBlocks snapshot previous blocks = Some next →
  NonceAdmission (eligibleAt snapshot) (admissionNonces next) suffix →
  NonceAdmission (eligibleAt snapshot) (admissionNonces previous)
    (windowTransactions blocks ++ suffix).
Proof.
  revert previous. induction blocks as [|block rest IH]; intros previous Hcheck Hsuffix.
  - cbn [checkBlocks] in Hcheck. injection Hcheck as <-. exact Hsuffix.
  - cbn [checkBlocks] in Hcheck.
    match type of Hcheck with context [checkTransactions ?s ?h ?p ?txs] ⇒
      destruct (checkTransactions s h p txs) as [middle|] eqn:Htxs end; [|discriminate].
    pose proof (IH middle Hcheck Hsuffix) as Htail.
    pose proof (checked_transactions_nonce_admission _ _ _ _ _ _ Htxs Htail) as Hstep.
    change (windowTransactions (block :: rest)) with
      (Block.t.transactions block ++ windowTransactions rest).
    rewrite <- app_assoc. exact Hstep.
Qed.

Theorem public_checks_imply_nonce_admission snapshot height blocks :
  consensusAccepts snapshot height blocks →
  NonceAdmission (eligibleAt snapshot) (nonceAt snapshot) (windowTransactions blocks).
Proof.
  intros [next Hcheck].
  pose proof (checked_blocks_nonce_admission snapshot _ blocks next [] Hcheck
    (nonce_admission_nil _ _)) as Hadmission.
  rewrite app_nil_r in Hadmission. exact Hadmission.
Qed.

senders contains every recovered transaction sender in the window. Nonces are initially below 2^63, and transaction count + authorization count + total gas limits is below 2^63. Only the initial system-contract code is prescribed. Every block and nested VM/Host call is covered.
On success, return an actual-state nonce bound and preserved system code. On failure, exclude Nonce_overflow, not every other validation error.
Theorem consensus_window_no_nonce_overflow cold warm access write reserve
    verify snapshot height blocks senders :
  E.Params.(Execution.Make_Params_signature.revision) =
    Support.Variants.Revision.Setcode (cold, warm, access, write, reserve) →
  (0 < cold)%N → (0 < warm)%N →
  (∀ a, In a senders → a ≠ Reserve_balance.precompile_address) →
  Forall (fun tx ⇒ ∀ a,
    T.sender E.Params.(Execution.Make_Params_signature.chain_id) tx = Some a → In a senders)
    (windowTransactions blocks) →
  consensusAccepts snapshot height blocks →
  prescribedSystemCode snapshot → InitialNonceBounds senders snapshot blocks →
  match Result.List.fold_leftM (E.process_block verify) snapshot blocks with
  | inl post ⇒ prescribedSystemCode post ∧
      accountNonceBound senders post (2^63-1 + windowNonceBudget blocks)
  | inr error ⇒ ¬ nonceOverflowError error
  end.
Proof.
  intros Hrevision Hcold Hwarm Hnative Hsenders Hcheck Hsystem Hinitial.
  exact (admitted_window_does_not_exhaust_nonces cold warm access write reserve
    verify blocks snapshot senders (eligibleAt snapshot) Hrevision Hcold Hwarm
    Hnative Hsenders (public_checks_imply_nonce_admission _ _ _ Hcheck) Hsystem Hinitial).
Qed.

Execute past from a window admitted as past ++ rest. On success, its actual nonces still have capacity for rest. No successful-prefix premise and no fresh admission of migrated senders are required.
Corollary consensus_prefix_retains_nonce_capacity cold warm access write reserve
    verify snapshot height past rest senders :
  E.Params.(Execution.Make_Params_signature.revision) =
    Support.Variants.Revision.Setcode (cold, warm, access, write, reserve) →
  (0 < cold)%N → (0 < warm)%N →
  (∀ a, In a senders → a ≠ Reserve_balance.precompile_address) →
  Forall (fun tx ⇒ ∀ a,
    T.sender E.Params.(Execution.Make_Params_signature.chain_id) tx = Some a → In a senders)
    (windowTransactions (past ++ rest)) →
  consensusAccepts snapshot height (past ++ rest) →
  prescribedSystemCode snapshot → InitialNonceBounds senders snapshot (past ++ rest) →
  match Result.List.fold_leftM (E.process_block verify) snapshot past with
  | inl post ⇒ prescribedSystemCode post ∧ ∀ a, In a senders →
      (0 ≤ nonceAt post a ∧ nonceAt post a + windowNonceBudget rest < 2^64-1)%Z
  | inr error ⇒ ¬ nonceOverflowError error
  end.
Proof.
  intros Hrevision Hcold Hwarm Hnative Hsenders Hcheck Hsystem Hinitial.
  exact (admitted_prefix_retains_nonce_capacity cold warm access write reserve
    verify past rest snapshot senders (eligibleAt snapshot) Hrevision Hcold Hwarm
    Hnative Hsenders (public_checks_imply_nonce_admission _ _ _ Hcheck) Hsystem Hinitial).
Qed.
End Admission.
Print Assumptions consensus_window_no_nonce_overflow.
Print Assumptions consensus_prefix_retains_nonce_capacity.