Library QedkConcrete.reserveconfigacceptance

Complete consensus check of a signed self-authorizing transaction

selfAuthorizedTx has sender nonce 1, authorization nonce 2, gas limit 100000, gas price 200000000000000 wei, and no value transfer. Its gas-fee bound is exactly 20 MON. The authorization clears Alice's delegation. The setting transaction has already consumed nonce 0. This transaction consumes nonce 1 before the authorization consumes 2.
The two recovery equations below are the only external facts used by these examples. Signature recovery and Keccak are abstract libraries in Rocq. The native companion test signs this exact fixture with public test key 1 and checks both equations using real libraries; it also validates and executes the transaction successfully. No consensus, validation, nonce, budget or emptying result is assumed.

From Stdlib Require Import List String NArith ZArith Bool Lia.
From stdpp Require Import decidable numbers strings list.
Require Import QedkConcrete.reserveconfigtests QedkConcrete.reserveconfigfixture
  QedkConcrete.consensuscorrectness QedkConcrete.consensushistory
  QedkConcrete.authorizationnonces QedkConcrete.transactionchecks
  QedkConcrete.publictransaction QedkConcrete.consensusdecisions.
Set Default Goal Selector "!".
Import ListNotations.
Local Open Scope N_scope.
#[local] Existing Instance reserveconfigtests.executor.

Candidate block at b containing only the signed fixture. The header base fee equals its maximum fee, so its quoted gas-fee bound is 20 MON.
Definition signedBlock (b : N) : Block.t :=
  Block.t.with_transactions [selfAuthorizedTx]
    (Block.t.with_header
      (Header.t.with_base_fee_per_gas 200000000000000
        (Header.t.with_number b Header.empty)) Block.empty).

Lemma signed_transaction_public_checks b :
  transactionChecks (Block.t.header (signedBlock b)) selfAuthorizedTx 1%Z = true.
Proof. vm_compute. reflexivity. Qed.

Lemma alice_initial_nonce : historyNonces (initialHistory afterSetting) alice = 1%Z.
Proof.
  change (A.t.nonce (accountAt afterSetting alice) = 1%Z).
  rewrite setting_preserves_alice_account. vm_compute. reflexivity.
Qed.

Lemma alice_not_contract : isContractAt afterSetting alice = false.
Proof.
  unfold isContractAt. rewrite setting_preserves_alice_account. vm_compute. reflexivity.
Qed.

Section VerifiedRecovery.
Hypothesis sender_recovery : transactionSender selfAuthorizedTx = Some alice.
Hypothesis authorization_recovery : T.Authorization.authority selfAuthorization = Some alice.

Lemma signed_transaction_quote b :
  transactionQuote (Block.t.header (signedBlock b)) selfAuthorizedTx =
    Some (alice, 20 × 10^18).
Proof using sender_recovery.
  unfold transactionQuote. rewrite sender_recovery. vm_compute. reflexivity.
Qed.

Lemma signed_authorization_applies :
  fst (advanceHistory afterSetting (initialHistory afterSetting) alice selfAuthorizedTx) = [alice].
Proof using authorization_recovery.
  unfold advanceHistory, authorizationNonces.
  cbn [T.authorization_list selfAuthorizedTx OCamlList.fold_left List.fold_left authorizationNonceStep].
  unfold authorizationNonceStep.
  rewrite authorization_recovery.
  unfold initialHistory.
  change (A.t.nonce (State.op_pointcaretlparenrparen afterSetting (@W.account None alice)))
    with (A.t.nonce (accountAt afterSetting alice)).
  change (A.is_smart_contract (State.op_pointcaretlparenrparen afterSetting (@W.account None alice)))
    with (A.is_smart_contract (accountAt afterSetting alice)).
  rewrite setting_preserves_alice_account. vm_compute. reflexivity.
Qed.

Lemma signed_history_nonce_three :
  historyNonces
    (snd (advanceHistory afterSetting (initialHistory afterSetting) alice selfAuthorizedTx))
    alice = 3%Z.
Proof using authorization_recovery.
  unfold advanceHistory, authorizationNonces.
  cbn [T.authorization_list selfAuthorizedTx OCamlList.fold_left List.fold_left].
  unfold authorizationNonceStep. rewrite authorization_recovery.
  unfold initialHistory.
  change (A.t.nonce (State.op_pointcaretlparenrparen afterSetting (@W.account None alice)))
    with (A.t.nonce (accountAt afterSetting alice)).
  change (A.is_smart_contract (State.op_pointcaretlparenrparen afterSetting (@W.account None alice)))
    with (A.is_smart_contract (accountAt afterSetting alice)).
  rewrite setting_preserves_alice_account. vm_compute. reflexivity.
Qed.

Reduction of the complete checker, not a replacement checker: sender recovery, public checks, nonce equality and self-authorization processing all run. The applied self-authorization makes emptying ineligible.
Lemma complete_transaction_step b budgets :
  checkTransaction afterSetting (Block.t.header (signedBlock b))
    {| consensusHistory := initialHistory afterSetting; consensusBudgets := budgets |}
    selfAuthorizedTx =
  if bool_decide (20 × 10^18 ≤ budgets alice)%Z then
    Some {| consensusHistory :=
      snd (advanceHistory afterSetting (initialHistory afterSetting) alice selfAuthorizedTx);
      consensusBudgets := fun a ⇒ if bool_decide (alice = a)
        then (budgets alice - 20 × 10^18)%Z else budgets a |}
  else None.
Proof using sender_recovery authorization_recovery.
  unfold checkTransaction. rewrite sender_recovery.
  assert (HnotSys : bool_decide (alice = Chain.Monad.system_sender) = false)
    by (vm_compute; reflexivity).
  rewrite HnotSys, signed_transaction_quote.
  cbn [consensusHistory consensusBudgets].
  rewrite alice_initial_nonce, signed_transaction_public_checks.
  pose proof signed_authorization_applies as Hauthorities.
  destruct (advanceHistory afterSetting (initialHistory afterSetting) alice selfAuthorizedTx)
    as [authorities history].
  cbn [fst] in Hauthorities. subst authorities.
  rewrite bool_decide_not_in.
  change (existsb (fun b ⇒ String.eqb b alice) [alice]) with true.
  rewrite Bool.andb_false_r. unfold checkSenderBudget. cbn beta iota.
  change (Z.of_N (20 × 10^18)) with (20 × 10^18)%Z.
  destruct (bool_decide (20 × 10^18 ≤ budgets alice)%Z); reflexivity.
Qed.

Entire consensus window: two empty blocks, then the signed transaction. Acceptance and the resulting 80 MON budget and nonce 3 are all proved. Neither checkTransaction nor checkSenderBudget is bypassed.
Example signed_transaction_accepted_at_106 :
  ∃ next,
    checkBlocks afterSetting (initialConsensusState afterSetting 103)
      [emptyBlock 104; emptyBlock 105; signedBlock 106] = Some next ∧
    consensusBudgets next alice = (80 × 10^18)%Z ∧
    historyNonces (consensusHistory next) alice = 3%Z.
Proof using sender_recovery authorization_recovery.
  cbn [checkBlocks checkTransactions emptyBlock initialConsensusState
    consensusBudgets consensusHistory Block.t.with_header Block.t.with_transactions
    Header.t.with_number Header.empty Block.empty Block.t.header
    Block.t.transactions Header.t.number].
  change (Block.t.transactions (signedBlock 106)) with [selfAuthorizedTx].
  cbn [checkTransactions].
  rewrite complete_transaction_step.
  rewrite setting_preserves_alice_balance.
  change (Header.t.number (Block.t.header (signedBlock 106))) with 106%N.
  rewrite !increase_remains_effective by lia.
  match goal with
  | |- context [bool_decide ?condition] ⇒
      let Hfee := fresh "Hfee" in
      assert (Hfee : bool_decide condition = true) by (vm_compute; reflexivity);
      rewrite Hfee
  end.
  cbn [checkTransactions checkBlocks].
  match goal with |- ∃ next, Some ?computed = Some next ∧ _ ⇒ ∃ computed end.
  split; [reflexivity|].
  split; [cbn [consensusBudgets]; vm_compute; reflexivity|exact signed_history_nonce_three].
Qed.

Negative control: the identical signed transaction is rejected when the designated snapshot is 102. Its reserve has activated by block 105, but consensus initialized its budget before activation.
Example signed_transaction_rejected_at_105 :
  checkBlocks afterSetting (initialConsensusState afterSetting 102)
    [emptyBlock 103; emptyBlock 104; signedBlock 105] = None.
Proof using sender_recovery authorization_recovery.
  cbn [checkBlocks checkTransactions emptyBlock initialConsensusState
    consensusBudgets consensusHistory Block.t.with_header Block.t.with_transactions
    Header.t.with_number Header.empty Block.empty Block.t.header
    Block.t.transactions Header.t.number].
  change (Block.t.transactions (signedBlock 105)) with [selfAuthorizedTx].
  cbn [checkTransactions].
  rewrite complete_transaction_step.
  rewrite setting_preserves_alice_balance.
  change (Header.t.number (Block.t.header (signedBlock 105))) with 105%N.
  rewrite increase_not_effective_at_102.
  rewrite !increase_remains_effective by lia.
  match goal with
  | |- context [bool_decide ?condition] ⇒
      let Hfee := fresh "Hfee" in
      assert (Hfee : bool_decide condition = false) by (vm_compute; reflexivity);
      rewrite Hfee
  end.
  reflexivity.
Qed.

The public theorem derives snapshot height 103 and final height 106 from these blocks. Rechecking the last block uses its height, 106.
Example derived_window_heights :
  let blocks := [emptyBlock 104; emptyBlock 105; signedBlock 106] in
  N.pred (firstBlockHeight blocks) = 103 ∧
  lastBlockHeight blocks = 106 ∧
  firstBlockHeight [signedBlock 106] = 106.
Proof. repeat split; reflexivity. Qed.

Example derived_empty_window_heights :
  firstBlockHeight [] = 0 ∧ lastBlockHeight [] = 0.
Proof. split; reflexivity. Qed.

The complete signed-transaction acceptance test still passes when the budget-initialization height is derived rather than supplied.
Example signed_transaction_accepted_with_derived_height :
  let blocks := [emptyBlock 104; emptyBlock 105; signedBlock 106] in
  consensusAccepts afterSetting (N.pred (firstBlockHeight blocks)) blocks.
Proof using sender_recovery authorization_recovery.
  destruct signed_transaction_accepted_at_106 as [next [Hcheck _]].
  ∃ next. exact Hcheck.
Qed.

Example signed_transaction_rejected_with_derived_height :
  let blocks := [emptyBlock 103; emptyBlock 104; signedBlock 105] in
  ¬ consensusAccepts afterSetting (N.pred (firstBlockHeight blocks)) blocks.
Proof using sender_recovery authorization_recovery.
  cbv zeta. intros [next Hcheck].
  change (checkBlocks afterSetting (initialConsensusState afterSetting 102)
    [emptyBlock 103; emptyBlock 104; signedBlock 105] = Some next) in Hcheck.
  rewrite signed_transaction_rejected_at_105 in Hcheck. discriminate.
Qed.
End VerifiedRecovery.