Library QedkConcrete.consensusbudget

QEDK consensus budget arithmetic

snapshotBalance is the balance at the designated executed snapshot, not the current execution balance. An emptying transaction may use it because its activity check rules out intervening debits. Ordinary transactions spend the carried budget; a higher configured threshold does not replenish it. All quantities here are mathematical integers, so neither subtraction nor a fee quotation can wrap.
From Stdlib Require Import ZArith Lia.
Require Export QedkConcrete.consensuscorrectness.
Require Import QedkConcrete.consensusdecisions.
Set Default Goal Selector "!".
Local Open Scope Z_scope.

The only balance information needed before validation is the maintained budget bound, or the unchanged snapshot balance in the emptying case. No fact about successful validation is used here.
Lemma accepted_sender_fee emptying snapshot threshold previous fee value remaining balance :
  checkSenderBudget emptying snapshot threshold previous fee value = Some remaining →
  previous ≤ balance →
  (emptying = true → snapshot ≤ balance) →
  fee ≤ balance.
Proof.
  unfold checkSenderBudget. rewrite !bool_decide_Z_le. destruct emptying.
  - destruct (Z.leb fee snapshot) eqn:Hfee; [|discriminate].
    apply Z.leb_le in Hfee. intros _ _ Hsnapshot. specialize (Hsnapshot eq_refl). lia.
  - destruct (Z.leb fee previous) eqn:Hfee; [|discriminate].
    apply Z.leb_le in Hfee. intros _ Hbalance _. lia.
Qed.

Lemma accepted_sender_budget_nonnegative emptying snapshot threshold previous fee value remaining :
  0 ≤ threshold →
  checkSenderBudget emptying snapshot threshold previous fee value = Some remaining →
  0 ≤ remaining.
Proof.
  intro Hthreshold. unfold checkSenderBudget. rewrite !bool_decide_Z_le. destruct emptying.
  - destruct (Z.leb fee snapshot); [|discriminate]. intro Heq. injection Heq as <-. lia.
  - destruct (Z.leb fee previous) eqn:Hfee; [|discriminate].
    apply Z.leb_le in Hfee. intro Heq. injection Heq as <-. lia.
Qed.

Lemma accepted_sender_budget_threshold emptying snapshot threshold previous fee value remaining :
  0 ≤ fee → previous ≤ threshold →
  checkSenderBudget emptying snapshot threshold previous fee value = Some remaining →
  remaining ≤ threshold.
Proof.
  intros Hfee Hprevious. unfold checkSenderBudget. rewrite !bool_decide_Z_le. destruct emptying.
  - destruct (Z.leb fee snapshot); [|discriminate]. intro Heq. injection Heq as <-. lia.
  - destruct (Z.leb fee previous); [|discriminate]. intro Heq. injection Heq as <-. lia.
Qed.

These are the two lower bounds derived from concrete transaction execution: only the sender's fee can reduce its protected reserve; an emptying sender can additionally spend the explicit value.
Lemma accepted_sender_budget_preserved emptying snapshot threshold previous fee value remaining before after :
  checkSenderBudget emptying snapshot threshold previous fee value = Some remaining →
  previous ≤ before → previous ≤ threshold →
  (emptying = true → snapshot ≤ before) →
  (emptying = false → Z.min before threshold - fee ≤ after) →
  (emptying = true → Z.max (before - fee - value) 0 ≤ after) →
  remaining ≤ after.
Proof.
  unfold checkSenderBudget. rewrite !bool_decide_Z_le. destruct emptying.
  - destruct (Z.leb fee snapshot); [|discriminate].
    intro Heq. injection Heq as <-. intros _ _ Hsnapshot _ Hafter.
    specialize (Hsnapshot eq_refl). specialize (Hafter eq_refl). lia.
  - destruct (Z.leb fee previous); [|discriminate].
    intro Heq. injection Heq as <-. intros Hbalance Hthreshold _ Hafter _.
    specialize (Hafter eq_refl). lia.
Qed.