Library monadpure.reservebaldelayed_qedk
QEDK's account-configurable reserve draft (2026-10-14 export)
Require Import monadpure.reservebaldelayed.
Require Import monadpure.accountstate.
From stdpp Require Import base list tactics sets.
From Stdlib Require Import Lia.
Set Default Goal Selector "!".
Open Scope N_scope.
Import ListNotations.
Definition defaultReserve : N := 10000000000000000000.
Definition unconfiguredAccount : ExtraAcState :=
{| lastTxInBlockIndex := None; lastDelUndelInBlockIndex := None;
settledReserveBal := defaultReserve; pendingReserveBal := None |}.
Require Import monadpure.accountstate.
From stdpp Require Import base list tactics sets.
From Stdlib Require Import Lia.
Set Default Goal Selector "!".
Open Scope N_scope.
Import ListNotations.
Definition defaultReserve : N := 10000000000000000000.
Definition unconfiguredAccount : ExtraAcState :=
{| lastTxInBlockIndex := None; lastDelUndelInBlockIndex := None;
settledReserveBal := defaultReserve; pendingReserveBal := None |}.
Boundaries are explicit even for empty blocks. The transaction header must
agree with its enclosing block; windowOrdered below checks this.
Inductive WindowEvent :=
| BeginBlock (block : N)
| RunTx (tx : TxWithHdr).
Definition blockEvents (block : N) (txs : list TxWithHdr) : list WindowEvent :=
BeginBlock block :: map RunTx txs.
Definition eventBlock (e : WindowEvent) : N :=
match e with BeginBlock b ⇒ b | RunTx tx ⇒ txBlockNum tx end.
Fixpoint windowOrdered (h current : N) (events : list WindowEvent) : Prop :=
match events with
| [] ⇒ True
| BeginBlock b :: rest ⇒
b = N.succ current ∧ h < b ∧ windowOrdered h b rest
| RunTx tx :: rest ⇒
txBlockNum tx = current ∧ h < current ∧ windowOrdered h current rest
end.
Definition windowTxs (events : list WindowEvent) : list TxWithHdr :=
flat_map (fun e ⇒ match e with BeginBlock _ ⇒ [] | RunTx tx ⇒ [tx] end) events.
Section Draft.
Variable K : N.
Hypothesis K_pos : 0 < K.
| BeginBlock (block : N)
| RunTx (tx : TxWithHdr).
Definition blockEvents (block : N) (txs : list TxWithHdr) : list WindowEvent :=
BeginBlock block :: map RunTx txs.
Definition eventBlock (e : WindowEvent) : N :=
match e with BeginBlock b ⇒ b | RunTx tx ⇒ txBlockNum tx end.
Fixpoint windowOrdered (h current : N) (events : list WindowEvent) : Prop :=
match events with
| [] ⇒ True
| BeginBlock b :: rest ⇒
b = N.succ current ∧ h < b ∧ windowOrdered h b rest
| RunTx tx :: rest ⇒
txBlockNum tx = current ∧ h < current ∧ windowOrdered h current rest
end.
Definition windowTxs (events : list WindowEvent) : list TxWithHdr :=
flat_map (fun e ⇒ match e with BeginBlock _ ⇒ [] | RunTx tx ⇒ [tx] end) events.
Section Draft.
Variable K : N.
Hypothesis K_pos : 0 < K.
Abstract setter result, after call-context and gas validation. None
means the precompile call fails, not that the enclosing transaction must
revert. The VM resolves that control flow and emits only surviving writes.
The imported representation normalizes matured records to settled values;
unlike the storage encoding, None does not mean "use the default".
Definition setReserveBalance (old : ExtraAcState) (block requested : N)
: option ExtraAcState :=
if bool_decide (requested < 2^256 ∧ 0 < block + K < 2^256) then
let active := promotePendingReserveBal K old block in
match pendingReserveBal active with
| Some (_, earlier) ⇒
if bool_decide (earlier = block)
then Some (applyReserveBalUpdate active block requested) else None
| None ⇒ Some (applyReserveBalUpdate active block requested)
end
else None.
Definition initialBudget (snapshot : AugmentedState) (h : N) : EffReserveBals :=
fun a ⇒ Z.of_N (N.min (balanceOfAc snapshot.1 a)
(delayedReserveBalOfAddr K snapshot.2 a h)).
Definition senderBudget (snapshot : AugmentedState) (budget : Z)
(previous : list TxWithHdr) (tx : TxWithHdr) : option Z :=
if isAllowedToEmpty K snapshot previous tx then
if bool_decide (maxTxFee tx ≤ balanceOfAc snapshot.1 (sender tx)) then
Some (Z.min
(Z.max (Z.of_N (balanceOfAc snapshot.1 (sender tx)) -
Z.of_N (maxTxFee tx) - Z.of_N (value tx) - Z.of_N (maxStorageFee tx)) 0)
(Z.of_N (delayedReserveBalOfAddr K snapshot.2 (sender tx) (txBlockNum tx))))
else None
else if bool_decide (Z.of_N (maxTxFee tx) ≤ budget)%Z
then Some (budget - Z.of_N (maxTxFee tx))%Z else None.
Fixpoint checkWindow (snapshot : AugmentedState) (budget : EffReserveBals)
(previous : list TxWithHdr) (events : list WindowEvent) : option EffReserveBals :=
match events with
| [] ⇒ Some budget
| BeginBlock b :: rest ⇒
checkWindow snapshot (advanceEffReserveBals K snapshot budget b) previous rest
| RunTx tx :: rest ⇒
match senderBudget snapshot (budget (sender tx)) previous tx with
| None ⇒ None
| Some remaining ⇒ checkWindow snapshot
(updateKey budget (sender tx) (fun _ ⇒ remaining)) (tx :: previous) rest
end
end.
Definition consensusAccepts (snapshot : AugmentedState) (h : N)
(events : list WindowEvent) : Prop :=
∃ budget, checkWindow snapshot (initialBudget snapshot h) [] events = Some budget.
Variable evmExecTxCore : StateOfAccounts → TxWithHdr →
((StateOfAccounts × TxResult) × list EvmAddr) × ReserveBalUpdate.
Variable revertTx : StateOfAccounts → TxWithHdr → StateOfAccounts × TxResult.
: option ExtraAcState :=
if bool_decide (requested < 2^256 ∧ 0 < block + K < 2^256) then
let active := promotePendingReserveBal K old block in
match pendingReserveBal active with
| Some (_, earlier) ⇒
if bool_decide (earlier = block)
then Some (applyReserveBalUpdate active block requested) else None
| None ⇒ Some (applyReserveBalUpdate active block requested)
end
else None.
Definition initialBudget (snapshot : AugmentedState) (h : N) : EffReserveBals :=
fun a ⇒ Z.of_N (N.min (balanceOfAc snapshot.1 a)
(delayedReserveBalOfAddr K snapshot.2 a h)).
Definition senderBudget (snapshot : AugmentedState) (budget : Z)
(previous : list TxWithHdr) (tx : TxWithHdr) : option Z :=
if isAllowedToEmpty K snapshot previous tx then
if bool_decide (maxTxFee tx ≤ balanceOfAc snapshot.1 (sender tx)) then
Some (Z.min
(Z.max (Z.of_N (balanceOfAc snapshot.1 (sender tx)) -
Z.of_N (maxTxFee tx) - Z.of_N (value tx) - Z.of_N (maxStorageFee tx)) 0)
(Z.of_N (delayedReserveBalOfAddr K snapshot.2 (sender tx) (txBlockNum tx))))
else None
else if bool_decide (Z.of_N (maxTxFee tx) ≤ budget)%Z
then Some (budget - Z.of_N (maxTxFee tx))%Z else None.
Fixpoint checkWindow (snapshot : AugmentedState) (budget : EffReserveBals)
(previous : list TxWithHdr) (events : list WindowEvent) : option EffReserveBals :=
match events with
| [] ⇒ Some budget
| BeginBlock b :: rest ⇒
checkWindow snapshot (advanceEffReserveBals K snapshot budget b) previous rest
| RunTx tx :: rest ⇒
match senderBudget snapshot (budget (sender tx)) previous tx with
| None ⇒ None
| Some remaining ⇒ checkWindow snapshot
(updateKey budget (sender tx) (fun _ ⇒ remaining)) (tx :: previous) rest
end
end.
Definition consensusAccepts (snapshot : AugmentedState) (h : N)
(events : list WindowEvent) : Prop :=
∃ budget, checkWindow snapshot (initialBudget snapshot h) [] events = Some budget.
Variable evmExecTxCore : StateOfAccounts → TxWithHdr →
((StateOfAccounts × TxResult) × list EvmAddr) × ReserveBalUpdate.
Variable revertTx : StateOfAccounts → TxWithHdr → StateOfAccounts × TxResult.
The explicit fee guard must not be replaced by saturating subtraction.
It is tested before accepting the core result, even if that result's changed
account list does not contain the sender.
Definition senderFeeProtected (s : AugmentedState) (tx : TxWithHdr) : bool :=
isAllowedToEmpty K s [] tx ||
bool_decide (maxTxFee tx ≤ N.min (balanceOfAc s.1 (sender tx))
(delayedReserveBalOfAddr K s.2 (sender tx) (txBlockNum tx))).
Definition execValidatedTx (s : AugmentedState) (tx : TxWithHdr)
: AugmentedState × TxResult :=
if senderFeeProtected s tx then
reservebaldelayed.execValidatedTx K evmExecTxCore revertTx s tx
else let '(post, receipt) := revertTx s.1 tx in
((post, updateExtraStateNoReserve K s.2 tx), receipt).
Fixpoint executeWindow (s : AugmentedState) (events : list WindowEvent)
: option (AugmentedState × list TxResult) :=
match events with
| [] ⇒ Some (s, [])
| BeginBlock _ :: rest ⇒ executeWindow s rest
| RunTx tx :: rest ⇒
if validateTx s.1 tx then
let '(next, receipt) := execValidatedTx s tx in
match executeWindow next rest with
| None ⇒ None
| Some (post, receipts) ⇒ Some (post, receipt :: receipts)
end
else None
end.
isAllowedToEmpty K s [] tx ||
bool_decide (maxTxFee tx ≤ N.min (balanceOfAc s.1 (sender tx))
(delayedReserveBalOfAddr K s.2 (sender tx) (txBlockNum tx))).
Definition execValidatedTx (s : AugmentedState) (tx : TxWithHdr)
: AugmentedState × TxResult :=
if senderFeeProtected s tx then
reservebaldelayed.execValidatedTx K evmExecTxCore revertTx s tx
else let '(post, receipt) := revertTx s.1 tx in
((post, updateExtraStateNoReserve K s.2 tx), receipt).
Fixpoint executeWindow (s : AugmentedState) (events : list WindowEvent)
: option (AugmentedState × list TxResult) :=
match events with
| [] ⇒ Some (s, [])
| BeginBlock _ :: rest ⇒ executeWindow s rest
| RunTx tx :: rest ⇒
if validateTx s.1 tx then
let '(next, receipt) := execValidatedTx s tx in
match executeWindow next rest with
| None ⇒ None
| Some (post, receipts) ⇒ Some (post, receipt :: receipts)
end
else None
end.
Correctness
Definition feeSolvency : Prop :=
∀ snapshot h events,
EVMAssupmtions evmExecTxCore revertTx (windowTxs events) →
windowOrdered h h events →
(∀ e, e ∈ events → eventBlock e ≤ h + K) →
(∀ a, a ∈ map sender (windowTxs events) → isSC snapshot.1 a = false) →
(∀ tx, tx ∈ windowTxs events →
txCannotCreateContractAtAddrs K evmExecTxCore revertTx tx
(map sender (windowTxs events))) →
consensusAccepts snapshot h events →
∃ post receipts, executeWindow snapshot events = Some (post, receipts).
Proofs
Lemma failed_reserve_check_discards_updates s tx :
let result := evmExecTxCore s.1 tx in
senderFeeProtected s tx = false ∨
allFinalBalSufficient K s result.1.1.1 result.1.2 tx = false →
(execValidatedTx s tx).1.2 = updateExtraStateNoReserve K s.2 tx.
Proof using All.
unfold execValidatedTx, reservebaldelayed.execValidatedTx.
destruct (evmExecTxCore s.1 tx) as [[[post receipt] changed] updates].
cbn [fst snd]. destruct (senderFeeProtected s tx) eqn:Hguard.
{ intros [Hfalse|Hcheck]; [discriminate|]. rewrite Hcheck.
destruct (revertTx s.1 tx). reflexivity. }
intros _. destruct (revertTx s.1 tx). reflexivity.
Qed.
Lemma successful_setting_effect old block requested updated :
setReserveBalance old block requested = Some updated →
updated = applyReserveBalUpdate (promotePendingReserveBal K old block) block requested.
Proof using All.
unfold setReserveBalance.
destruct (bool_decide (requested < 2^256 ∧ 0 < block + K < 2^256));
[|discriminate].
destruct (pendingReserveBal (promotePendingReserveBal K old block))
as [[pending earlier]|].
{ destruct (bool_decide (earlier = block)); congruence. }
congruence.
Qed.
Lemma setting_unchanged_until_activation old block requested updated n :
setReserveBalance old block requested = Some updated →
block ≤ n < block + K →
delayedReserveBal K updated n = delayedReserveBal K old n.
Proof using All.
intros Hset Hrange. apply successful_setting_effect in Hset. subst updated.
rewrite (delayedReserveBal_applyReserveBalUpdate_ltK K K_pos); [|lia].
apply (delayedReserveBal_promoteWithinK K K_pos); lia.
Qed.
Lemma setting_matures old block requested updated :
setReserveBalance old block requested = Some updated →
delayedReserveBal K updated (block + K) = requested.
Proof using All.
unfold setReserveBalance.
destruct (bool_decide (requested < 2^256 ∧ 0 < block + K < 2^256));
[|discriminate].
remember (promotePendingReserveBal K old block) as active.
destruct (pendingReserveBal active) as [[pending earlier]|] eqn:Hpending.
{ destruct (bool_decide (earlier = block)) eqn:Hsame; [|discriminate].
apply bool_decide_eq_true_1 in Hsame. subst earlier.
unfold applyReserveBalUpdate. rewrite Hpending bool_decide_true; [|reflexivity].
intros Hset. injection Hset as <-.
unfold delayedReserveBal. simpl. rewrite bool_decide_true; [reflexivity|lia]. }
unfold applyReserveBalUpdate. rewrite Hpending.
intros Hset. injection Hset as <-.
unfold delayedReserveBal. simpl. rewrite bool_decide_true; [reflexivity|lia].
Qed.
Lemma prior_pending_setting_rejected old block requested pending earlier :
pendingReserveBal old = Some (pending, earlier) →
earlier < block < earlier + K →
setReserveBalance old block requested = None.
Proof using All.
intros Hpending Hrange. unfold setReserveBalance.
destruct (bool_decide (requested < 2^256 ∧ 0 < block + K < 2^256));
[|reflexivity].
unfold promotePendingReserveBal. rewrite Hpending.
rewrite bool_decide_false; [|lia].
rewrite Hpending bool_decide_false; [reflexivity|lia].
Qed.
let result := evmExecTxCore s.1 tx in
senderFeeProtected s tx = false ∨
allFinalBalSufficient K s result.1.1.1 result.1.2 tx = false →
(execValidatedTx s tx).1.2 = updateExtraStateNoReserve K s.2 tx.
Proof using All.
unfold execValidatedTx, reservebaldelayed.execValidatedTx.
destruct (evmExecTxCore s.1 tx) as [[[post receipt] changed] updates].
cbn [fst snd]. destruct (senderFeeProtected s tx) eqn:Hguard.
{ intros [Hfalse|Hcheck]; [discriminate|]. rewrite Hcheck.
destruct (revertTx s.1 tx). reflexivity. }
intros _. destruct (revertTx s.1 tx). reflexivity.
Qed.
Lemma successful_setting_effect old block requested updated :
setReserveBalance old block requested = Some updated →
updated = applyReserveBalUpdate (promotePendingReserveBal K old block) block requested.
Proof using All.
unfold setReserveBalance.
destruct (bool_decide (requested < 2^256 ∧ 0 < block + K < 2^256));
[|discriminate].
destruct (pendingReserveBal (promotePendingReserveBal K old block))
as [[pending earlier]|].
{ destruct (bool_decide (earlier = block)); congruence. }
congruence.
Qed.
Lemma setting_unchanged_until_activation old block requested updated n :
setReserveBalance old block requested = Some updated →
block ≤ n < block + K →
delayedReserveBal K updated n = delayedReserveBal K old n.
Proof using All.
intros Hset Hrange. apply successful_setting_effect in Hset. subst updated.
rewrite (delayedReserveBal_applyReserveBalUpdate_ltK K K_pos); [|lia].
apply (delayedReserveBal_promoteWithinK K K_pos); lia.
Qed.
Lemma setting_matures old block requested updated :
setReserveBalance old block requested = Some updated →
delayedReserveBal K updated (block + K) = requested.
Proof using All.
unfold setReserveBalance.
destruct (bool_decide (requested < 2^256 ∧ 0 < block + K < 2^256));
[|discriminate].
remember (promotePendingReserveBal K old block) as active.
destruct (pendingReserveBal active) as [[pending earlier]|] eqn:Hpending.
{ destruct (bool_decide (earlier = block)) eqn:Hsame; [|discriminate].
apply bool_decide_eq_true_1 in Hsame. subst earlier.
unfold applyReserveBalUpdate. rewrite Hpending bool_decide_true; [|reflexivity].
intros Hset. injection Hset as <-.
unfold delayedReserveBal. simpl. rewrite bool_decide_true; [reflexivity|lia]. }
unfold applyReserveBalUpdate. rewrite Hpending.
intros Hset. injection Hset as <-.
unfold delayedReserveBal. simpl. rewrite bool_decide_true; [reflexivity|lia].
Qed.
Lemma prior_pending_setting_rejected old block requested pending earlier :
pendingReserveBal old = Some (pending, earlier) →
earlier < block < earlier + K →
setReserveBalance old block requested = None.
Proof using All.
intros Hpending Hrange. unfold setReserveBalance.
destruct (bool_decide (requested < 2^256 ∧ 0 < block + K < 2^256));
[|reflexivity].
unfold promotePendingReserveBal. rewrite Hpending.
rewrite bool_decide_false; [|lia].
rewrite Hpending bool_decide_false; [reflexivity|lia].
Qed.
Empty blocks can reduce coverage, but cannot create new coverage. In
particular a scheduled increase cannot erase fees already budgeted.
Lemma empty_blocks_never_refill snapshot blocks :
∀ budget previous finalBudget,
checkWindow snapshot budget previous (map BeginBlock blocks) = Some finalBudget →
∀ a, (finalBudget a ≤ budget a)%Z.
Proof using All.
induction blocks as [|b rest IH]; intros budget previous finalBudget Hcheck a.
{ simpl in Hcheck. injection Hcheck as <-. lia. }
cbn [map checkWindow] in Hcheck.
specialize (IH _ _ _ Hcheck a). unfold advanceEffReserveBals in IH. lia.
Qed.
Record WindowInvariant (snapshot actual : AugmentedState)
(budget : EffReserveBals) (previous : list TxWithHdr)
(signers : list EvmAddr) (current last : N) : Prop := {
budgetCovered : ∀ a, a ∈ signers →
(budget a ≤ Z.of_N (balanceOfAc actual.1 a))%Z;
reserveCovered : ∀ a, a ∈ signers →
(budget a ≤ Z.of_N (delayedReserveBalOfAddr K snapshot.2 a current))%Z;
thresholdsKnown : ∀ a n, current ≤ n ≤ last →
delayedReserveBalOfAddr K actual.2 a n =
delayedReserveBalOfAddr K snapshot.2 a n;
emptyingKnown : ∀ inter tx, current ≤ txBlockNum tx ≤ last →
isAllowedToEmpty K actual inter tx =
isAllowedToEmpty K snapshot (previous ++ inter) tx;
emptyingBalance : ∀ inter tx, sender tx ∈ signers →
current ≤ txBlockNum tx ≤ last →
isAllowedToEmpty K actual inter tx = true →
balanceOfAc snapshot.1 (sender tx) ≤ balanceOfAc actual.1 (sender tx)
}.
Lemma emptying_cons_app s previous inter tx candidate :
isAllowedToEmpty K s ((tx :: previous) ++ inter) candidate =
isAllowedToEmpty K s (previous ++ tx :: inter) candidate.
Proof using All.
unfold isAllowedToEmpty.
assert (Hsend : bool_decide (sender candidate ∈ map sender ((tx :: previous) ++ inter)) =
bool_decide (sender candidate ∈ map sender (previous ++ tx :: inter))).
{ apply bool_decide_ext. rewrite !map_app. simpl. set_solver. }
assert (Hauth : bool_decide (sender candidate ∈ flat_map addrsDelUndelByTx
(candidate :: (tx :: previous) ++ inter)) =
bool_decide (sender candidate ∈ flat_map addrsDelUndelByTx
(candidate :: previous ++ tx :: inter))).
{ apply bool_decide_ext. simpl. rewrite !flat_map_app. simpl. set_solver. }
rewrite Hsend Hauth. reflexivity.
Qed.
∀ budget previous finalBudget,
checkWindow snapshot budget previous (map BeginBlock blocks) = Some finalBudget →
∀ a, (finalBudget a ≤ budget a)%Z.
Proof using All.
induction blocks as [|b rest IH]; intros budget previous finalBudget Hcheck a.
{ simpl in Hcheck. injection Hcheck as <-. lia. }
cbn [map checkWindow] in Hcheck.
specialize (IH _ _ _ Hcheck a). unfold advanceEffReserveBals in IH. lia.
Qed.
Record WindowInvariant (snapshot actual : AugmentedState)
(budget : EffReserveBals) (previous : list TxWithHdr)
(signers : list EvmAddr) (current last : N) : Prop := {
budgetCovered : ∀ a, a ∈ signers →
(budget a ≤ Z.of_N (balanceOfAc actual.1 a))%Z;
reserveCovered : ∀ a, a ∈ signers →
(budget a ≤ Z.of_N (delayedReserveBalOfAddr K snapshot.2 a current))%Z;
thresholdsKnown : ∀ a n, current ≤ n ≤ last →
delayedReserveBalOfAddr K actual.2 a n =
delayedReserveBalOfAddr K snapshot.2 a n;
emptyingKnown : ∀ inter tx, current ≤ txBlockNum tx ≤ last →
isAllowedToEmpty K actual inter tx =
isAllowedToEmpty K snapshot (previous ++ inter) tx;
emptyingBalance : ∀ inter tx, sender tx ∈ signers →
current ≤ txBlockNum tx ≤ last →
isAllowedToEmpty K actual inter tx = true →
balanceOfAc snapshot.1 (sender tx) ≤ balanceOfAc actual.1 (sender tx)
}.
Lemma emptying_cons_app s previous inter tx candidate :
isAllowedToEmpty K s ((tx :: previous) ++ inter) candidate =
isAllowedToEmpty K s (previous ++ tx :: inter) candidate.
Proof using All.
unfold isAllowedToEmpty.
assert (Hsend : bool_decide (sender candidate ∈ map sender ((tx :: previous) ++ inter)) =
bool_decide (sender candidate ∈ map sender (previous ++ tx :: inter))).
{ apply bool_decide_ext. rewrite !map_app. simpl. set_solver. }
assert (Hauth : bool_decide (sender candidate ∈ flat_map addrsDelUndelByTx
(candidate :: (tx :: previous) ++ inter)) =
bool_decide (sender candidate ∈ flat_map addrsDelUndelByTx
(candidate :: previous ++ tx :: inter))).
{ apply bool_decide_ext. simpl. rewrite !flat_map_app. simpl. set_solver. }
rewrite Hsend Hauth. reflexivity.
Qed.
The execution guard is redundant on accepted transactions, but not on
arbitrary transactions. This is why retaining it does not weaken solvency
or require assuming that the VM already respects the reserve check.
Lemma senderBudget_fee snapshot actual budget previous signers current last tx remaining :
WindowInvariant snapshot actual budget previous signers current last →
sender tx ∈ signers → txBlockNum tx = current → current ≤ last →
senderBudget snapshot (budget (sender tx)) previous tx = Some remaining →
maxTxFee tx ≤ balanceOfAc actual.1 (sender tx) ∧
senderFeeProtected actual tx = true.
Proof using All.
intros Hinv Hin Hblock Hlast Hcheck.
destruct Hinv as [Hbalance Hreserve Hthreshold Hempty Hunchanged].
specialize (Hbalance _ Hin). specialize (Hreserve _ Hin).
pose proof (Hempty [] tx ltac:(lia)) as Hgate.
rewrite app_nil_r in Hgate.
pose proof (Hthreshold (sender tx) (txBlockNum tx) ltac:(lia)) as Hr.
unfold senderBudget in Hcheck.
rewrite <- Hgate in Hcheck.
unfold senderFeeProtected.
destruct (isAllowedToEmpty K actual [] tx) eqn:Hallowed.
{ destruct (bool_decide (maxTxFee tx ≤ balanceOfAc snapshot.1 (sender tx)))
eqn:Hfee; [|discriminate].
apply bool_decide_eq_true_1 in Hfee.
specialize (Hunchanged [] tx Hin ltac:(lia) Hallowed).
split; [lia|reflexivity]. }
destruct (bool_decide (Z.of_N (maxTxFee tx) ≤ budget (sender tx))%Z)
eqn:Hfee; [|discriminate].
apply bool_decide_eq_true_1 in Hfee.
split; [lia|]. simpl. apply bool_decide_eq_true_2.
rewrite Hr Hblock. lia.
Qed.
Section ExecutionProofs.
Context {transactions : list TxWithHdr}
{eas : EVMAssupmtions evmExecTxCore revertTx transactions}.
Lemma senderBudget_post snapshot actual budget previous signers current last tx remaining :
WindowInvariant snapshot actual budget previous signers current last →
sender tx ∈ signers → txBlockNum tx = current → current ≤ last →
senderBudget snapshot (budget (sender tx)) previous tx = Some remaining →
isSC (reservebaldelayed.execValidatedTx K evmExecTxCore revertTx actual tx).1.1
(sender tx) = false →
(remaining ≤ Z.of_N (balanceOfAc
(reservebaldelayed.execValidatedTx K evmExecTxCore revertTx actual tx).1.1
(sender tx)))%Z ∧
(remaining ≤ Z.of_N (delayedReserveBalOfAddr K snapshot.2 (sender tx) current))%Z.
Proof using All.
intros Hinv Hin Hblock Hlast Hcheck Hsc.
pose proof (senderBudget_fee _ _ _ _ _ _ _ _ _ Hinv Hin Hblock Hlast Hcheck)
as [Hfee _].
pose proof (execTxSenderBal K K_pos evmExecTxCore revertTx tx actual Hfee Hsc) as Hpost.
unfold balanceOfAcA in Hpost.
destruct Hinv as [Hbalance Hreserve Hthreshold Hempty Hunchanged].
specialize (Hbalance _ Hin). specialize (Hreserve _ Hin).
pose proof (Hempty [] tx ltac:(lia)) as Hgate.
rewrite app_nil_r in Hgate.
pose proof (Hthreshold (sender tx) (txBlockNum tx) ltac:(lia)) as Hr.
unfold senderBudget in Hcheck. rewrite <- Hgate in Hcheck.
destruct (isAllowedToEmpty K actual [] tx) eqn:Hallowed.
{ destruct (bool_decide (maxTxFee tx ≤ balanceOfAc snapshot.1 (sender tx)));
[|discriminate].
injection Hcheck as <-.
specialize (Hunchanged [] tx Hin ltac:(lia) Hallowed).
rewrite Hblock. destruct Hpost; split; lia. }
destruct (bool_decide (Z.of_N (maxTxFee tx) ≤ budget (sender tx))%Z);
[|discriminate].
injection Hcheck as <-. rewrite Hr Hblock in Hpost. split; lia.
Qed.
Lemma invariant_boundary snapshot actual budget previous signers current last b :
WindowInvariant snapshot actual budget previous signers current last →
current ≤ b ≤ last →
WindowInvariant snapshot actual (advanceEffReserveBals K snapshot budget b)
previous signers b last.
Proof using All.
intros Hinv Hrange.
destruct Hinv as [Hbalance Hreserve Hthreshold Hempty Hunchanged].
constructor.
{ intros a Hin. specialize (Hbalance a Hin).
unfold advanceEffReserveBals. lia. }
{ intros a Hin. unfold advanceEffReserveBals. lia. }
{ intros a n Hn. apply Hthreshold. lia. }
{ intros inter tx Hn. apply Hempty. lia. }
{ intros inter tx Hin Hn Hgate. eapply Hunchanged; eauto; lia. }
Qed.
WindowInvariant snapshot actual budget previous signers current last →
sender tx ∈ signers → txBlockNum tx = current → current ≤ last →
senderBudget snapshot (budget (sender tx)) previous tx = Some remaining →
maxTxFee tx ≤ balanceOfAc actual.1 (sender tx) ∧
senderFeeProtected actual tx = true.
Proof using All.
intros Hinv Hin Hblock Hlast Hcheck.
destruct Hinv as [Hbalance Hreserve Hthreshold Hempty Hunchanged].
specialize (Hbalance _ Hin). specialize (Hreserve _ Hin).
pose proof (Hempty [] tx ltac:(lia)) as Hgate.
rewrite app_nil_r in Hgate.
pose proof (Hthreshold (sender tx) (txBlockNum tx) ltac:(lia)) as Hr.
unfold senderBudget in Hcheck.
rewrite <- Hgate in Hcheck.
unfold senderFeeProtected.
destruct (isAllowedToEmpty K actual [] tx) eqn:Hallowed.
{ destruct (bool_decide (maxTxFee tx ≤ balanceOfAc snapshot.1 (sender tx)))
eqn:Hfee; [|discriminate].
apply bool_decide_eq_true_1 in Hfee.
specialize (Hunchanged [] tx Hin ltac:(lia) Hallowed).
split; [lia|reflexivity]. }
destruct (bool_decide (Z.of_N (maxTxFee tx) ≤ budget (sender tx))%Z)
eqn:Hfee; [|discriminate].
apply bool_decide_eq_true_1 in Hfee.
split; [lia|]. simpl. apply bool_decide_eq_true_2.
rewrite Hr Hblock. lia.
Qed.
Section ExecutionProofs.
Context {transactions : list TxWithHdr}
{eas : EVMAssupmtions evmExecTxCore revertTx transactions}.
Lemma senderBudget_post snapshot actual budget previous signers current last tx remaining :
WindowInvariant snapshot actual budget previous signers current last →
sender tx ∈ signers → txBlockNum tx = current → current ≤ last →
senderBudget snapshot (budget (sender tx)) previous tx = Some remaining →
isSC (reservebaldelayed.execValidatedTx K evmExecTxCore revertTx actual tx).1.1
(sender tx) = false →
(remaining ≤ Z.of_N (balanceOfAc
(reservebaldelayed.execValidatedTx K evmExecTxCore revertTx actual tx).1.1
(sender tx)))%Z ∧
(remaining ≤ Z.of_N (delayedReserveBalOfAddr K snapshot.2 (sender tx) current))%Z.
Proof using All.
intros Hinv Hin Hblock Hlast Hcheck Hsc.
pose proof (senderBudget_fee _ _ _ _ _ _ _ _ _ Hinv Hin Hblock Hlast Hcheck)
as [Hfee _].
pose proof (execTxSenderBal K K_pos evmExecTxCore revertTx tx actual Hfee Hsc) as Hpost.
unfold balanceOfAcA in Hpost.
destruct Hinv as [Hbalance Hreserve Hthreshold Hempty Hunchanged].
specialize (Hbalance _ Hin). specialize (Hreserve _ Hin).
pose proof (Hempty [] tx ltac:(lia)) as Hgate.
rewrite app_nil_r in Hgate.
pose proof (Hthreshold (sender tx) (txBlockNum tx) ltac:(lia)) as Hr.
unfold senderBudget in Hcheck. rewrite <- Hgate in Hcheck.
destruct (isAllowedToEmpty K actual [] tx) eqn:Hallowed.
{ destruct (bool_decide (maxTxFee tx ≤ balanceOfAc snapshot.1 (sender tx)));
[|discriminate].
injection Hcheck as <-.
specialize (Hunchanged [] tx Hin ltac:(lia) Hallowed).
rewrite Hblock. destruct Hpost; split; lia. }
destruct (bool_decide (Z.of_N (maxTxFee tx) ≤ budget (sender tx))%Z);
[|discriminate].
injection Hcheck as <-. rewrite Hr Hblock in Hpost. split; lia.
Qed.
Lemma invariant_boundary snapshot actual budget previous signers current last b :
WindowInvariant snapshot actual budget previous signers current last →
current ≤ b ≤ last →
WindowInvariant snapshot actual (advanceEffReserveBals K snapshot budget b)
previous signers b last.
Proof using All.
intros Hinv Hrange.
destruct Hinv as [Hbalance Hreserve Hthreshold Hempty Hunchanged].
constructor.
{ intros a Hin. specialize (Hbalance a Hin).
unfold advanceEffReserveBals. lia. }
{ intros a Hin. unfold advanceEffReserveBals. lia. }
{ intros a n Hn. apply Hthreshold. lia. }
{ intros inter tx Hn. apply Hempty. lia. }
{ intros inter tx Hin Hn Hgate. eapply Hunchanged; eauto; lia. }
Qed.
Ordinary fees subtract from a budget already protected by execution.
Emptying uses a different argument: eligibility implies that earlier
transactions could not debit this sender, so the snapshot balance is still
a lower bound. No threshold-monotonicity assumption is needed.
Lemma invariant_transaction snapshot actual budget previous signers current last tx remaining :
(∀ a, a ∈ signers → a ∈ map sender transactions) →
WindowInvariant snapshot actual budget previous signers current last →
sender tx ∈ signers → txBlockNum tx = current → current ≤ last →
last < current + K →
senderBudget snapshot (budget (sender tx)) previous tx = Some remaining →
(∀ a, a ∈ signers → isSC
(reservebaldelayed.execValidatedTx K evmExecTxCore revertTx actual tx).1.1 a = false) →
WindowInvariant snapshot (execValidatedTx actual tx).1
(updateKey budget (sender tx) (fun _ ⇒ remaining)) (tx :: previous)
signers current last.
Proof using All.
intros Hscope Hinv Hin Hblock Hlast Hdelay Hcheck Hsc.
pose proof (senderBudget_fee _ _ _ _ _ _ _ _ _ Hinv Hin Hblock Hlast Hcheck)
as [Hfee Hguard].
pose proof (senderBudget_post _ _ _ _ _ _ _ _ _ Hinv Hin Hblock Hlast Hcheck
(Hsc _ Hin)) as [Hsender HsenderReserve].
unfold execValidatedTx. rewrite Hguard.
destruct Hinv as [Hbalance Hreserve Hthreshold Hempty Hunchanged].
constructor.
{ intros a Ha. unfold updateKey.
destruct (decide (a = sender tx)) as [->|Hneq].
{ rewrite bool_decide_true; [exact Hsender|reflexivity]. }
rewrite bool_decide_false; [|congruence].
pose proof (execTxOtherBalanceLB K K_pos evmExecTxCore revertTx tx actual Hfee a Hneq)
as Hpost.
rewrite (Hsc a Ha) in Hpost. unfold balanceOfAcA in Hpost.
rewrite (Hthreshold a (txBlockNum tx) ltac:(lia)) Hblock in Hpost.
specialize (Hbalance a Ha). specialize (Hreserve a Ha). lia. }
{ intros a Ha. unfold updateKey.
destruct (decide (a = sender tx)) as [->|Hneq].
{ rewrite bool_decide_true; [exact HsenderReserve|reflexivity]. }
rewrite bool_decide_false; [apply Hreserve; exact Ha|congruence]. }
{ intros a n Hrange.
rewrite (delayedReserveBalOfAddrWithinK K K_pos evmExecTxCore revertTx
actual tx a n); [apply Hthreshold; exact Hrange|lia|lia]. }
{ intros inter candidate Hrange.
rewrite <- (execPreservesIsAllowedToEmpty K K_pos evmExecTxCore revertTx
actual tx inter candidate); [|lia].
rewrite (Hempty (tx :: inter) candidate Hrange).
symmetry. apply emptying_cons_app. }
{ intros inter candidate Ha Hrange Hgate.
assert (Hbefore : isAllowedToEmpty K actual (tx :: inter) candidate = true).
{ rewrite (execPreservesIsAllowedToEmpty K K_pos evmExecTxCore revertTx
actual tx inter candidate); [exact Hgate|lia]. }
pose proof (Hunchanged (tx :: inter) candidate Ha Hrange Hbefore) as Hsnapshot.
pose proof (emptyBalanceUb K K_pos evmExecTxCore revertTx actual tx inter candidate
(Hscope _ Ha) (Hsc _ Ha) Hbefore) as Hpost.
lia. }
Qed.
Lemma checkWindow_exec snapshot events :
∀ actual budget previous signers h current last,
(∀ a, a ∈ signers → a ∈ map sender transactions) →
WindowInvariant snapshot actual budget previous signers current last →
current ≤ last → last ≤ h + K →
windowOrdered h current events →
(∀ e, e ∈ events → eventBlock e ≤ last) →
(∀ a, a ∈ signers → isSC actual.1 a = false) →
(∀ tx, tx ∈ windowTxs events → sender tx ∈ signers ∧
txCannotCreateContractAtAddrs K evmExecTxCore revertTx tx signers) →
(∃ finalBudget, checkWindow snapshot budget previous events = Some finalBudget) →
∃ post receipts, executeWindow actual events = Some (post, receipts).
Proof using All.
induction events as [|event rest IH]; intros actual budget previous signers h current last
Hscope Hinv Hlast Hdelay Hordered Hbounds Hsc Hcrypto Hcheck.
{ ∃ actual, []. reflexivity. }
destruct event as [b|tx].
{ simpl in Hordered. destruct Hordered as [Hb [Hh Hordered]].
simpl in Hcheck |- ×.
eapply (IH actual (advanceEffReserveBals K snapshot budget b) previous signers h b last);
[exact Hscope|eapply invariant_boundary; [exact Hinv|] | | | | | | |].
{ specialize (Hbounds (BeginBlock b) ltac:(set_solver)). simpl in Hbounds. lia. }
{ specialize (Hbounds (BeginBlock b) ltac:(set_solver)). simpl in Hbounds. lia. }
{ exact Hdelay. }
{ exact Hordered. }
{ intros e He. apply Hbounds. set_solver. }
{ exact Hsc. }
{ exact Hcrypto. }
{ exact Hcheck. } }
simpl in Hordered. destruct Hordered as [Hblock [Hh Hordered]].
destruct Hcheck as [finalBudget Hcheck].
cbn [checkWindow] in Hcheck.
destruct (senderBudget snapshot (budget (sender tx)) previous tx)
as [remaining|] eqn:Hsender; [|discriminate].
destruct (Hcrypto tx ltac:(unfold windowTxs; simpl; set_solver)) as [Hin HtxCrypto].
pose proof (senderBudget_fee _ _ _ _ _ _ _ _ _ Hinv Hin Hblock Hlast Hsender)
as [Hfee Hguard].
assert (HscPost : ∀ a, a ∈ signers → isSC
(reservebaldelayed.execValidatedTx K evmExecTxCore revertTx actual tx).1.1 a = false).
{ intros a Ha. apply HtxCrypto; [exact Ha|apply Hsc; exact Ha]. }
pose proof (invariant_transaction _ _ _ _ _ _ _ _ _ Hscope Hinv Hin Hblock Hlast
ltac:(lia) Hsender HscPost) as Hnext.
assert (HscNext : ∀ a, a ∈ signers → isSC (execValidatedTx actual tx).1.1 a = false).
{ unfold execValidatedTx. rewrite Hguard. exact HscPost. }
destruct (IH (execValidatedTx actual tx).1
(updateKey budget (sender tx) (fun _ ⇒ remaining)) (tx :: previous)
signers h current last Hscope Hnext Hlast Hdelay Hordered)
as [post [receipts Hexecute]].
{ intros e He. apply Hbounds. set_solver. }
{ exact HscNext. }
{ intros t Ht. apply Hcrypto. unfold windowTxs in ×. simpl in ×. set_solver. }
{ ∃ finalBudget. exact Hcheck. }
∃ post, ((execValidatedTx actual tx).2 :: receipts).
cbn [executeWindow]. unfold validateTx. rewrite bool_decide_true; [|exact Hfee].
destruct (execValidatedTx actual tx) as [next receipt]. simpl in Hexecute |- ×.
rewrite Hexecute. reflexivity.
Qed.
End ExecutionProofs.
(∀ a, a ∈ signers → a ∈ map sender transactions) →
WindowInvariant snapshot actual budget previous signers current last →
sender tx ∈ signers → txBlockNum tx = current → current ≤ last →
last < current + K →
senderBudget snapshot (budget (sender tx)) previous tx = Some remaining →
(∀ a, a ∈ signers → isSC
(reservebaldelayed.execValidatedTx K evmExecTxCore revertTx actual tx).1.1 a = false) →
WindowInvariant snapshot (execValidatedTx actual tx).1
(updateKey budget (sender tx) (fun _ ⇒ remaining)) (tx :: previous)
signers current last.
Proof using All.
intros Hscope Hinv Hin Hblock Hlast Hdelay Hcheck Hsc.
pose proof (senderBudget_fee _ _ _ _ _ _ _ _ _ Hinv Hin Hblock Hlast Hcheck)
as [Hfee Hguard].
pose proof (senderBudget_post _ _ _ _ _ _ _ _ _ Hinv Hin Hblock Hlast Hcheck
(Hsc _ Hin)) as [Hsender HsenderReserve].
unfold execValidatedTx. rewrite Hguard.
destruct Hinv as [Hbalance Hreserve Hthreshold Hempty Hunchanged].
constructor.
{ intros a Ha. unfold updateKey.
destruct (decide (a = sender tx)) as [->|Hneq].
{ rewrite bool_decide_true; [exact Hsender|reflexivity]. }
rewrite bool_decide_false; [|congruence].
pose proof (execTxOtherBalanceLB K K_pos evmExecTxCore revertTx tx actual Hfee a Hneq)
as Hpost.
rewrite (Hsc a Ha) in Hpost. unfold balanceOfAcA in Hpost.
rewrite (Hthreshold a (txBlockNum tx) ltac:(lia)) Hblock in Hpost.
specialize (Hbalance a Ha). specialize (Hreserve a Ha). lia. }
{ intros a Ha. unfold updateKey.
destruct (decide (a = sender tx)) as [->|Hneq].
{ rewrite bool_decide_true; [exact HsenderReserve|reflexivity]. }
rewrite bool_decide_false; [apply Hreserve; exact Ha|congruence]. }
{ intros a n Hrange.
rewrite (delayedReserveBalOfAddrWithinK K K_pos evmExecTxCore revertTx
actual tx a n); [apply Hthreshold; exact Hrange|lia|lia]. }
{ intros inter candidate Hrange.
rewrite <- (execPreservesIsAllowedToEmpty K K_pos evmExecTxCore revertTx
actual tx inter candidate); [|lia].
rewrite (Hempty (tx :: inter) candidate Hrange).
symmetry. apply emptying_cons_app. }
{ intros inter candidate Ha Hrange Hgate.
assert (Hbefore : isAllowedToEmpty K actual (tx :: inter) candidate = true).
{ rewrite (execPreservesIsAllowedToEmpty K K_pos evmExecTxCore revertTx
actual tx inter candidate); [exact Hgate|lia]. }
pose proof (Hunchanged (tx :: inter) candidate Ha Hrange Hbefore) as Hsnapshot.
pose proof (emptyBalanceUb K K_pos evmExecTxCore revertTx actual tx inter candidate
(Hscope _ Ha) (Hsc _ Ha) Hbefore) as Hpost.
lia. }
Qed.
Lemma checkWindow_exec snapshot events :
∀ actual budget previous signers h current last,
(∀ a, a ∈ signers → a ∈ map sender transactions) →
WindowInvariant snapshot actual budget previous signers current last →
current ≤ last → last ≤ h + K →
windowOrdered h current events →
(∀ e, e ∈ events → eventBlock e ≤ last) →
(∀ a, a ∈ signers → isSC actual.1 a = false) →
(∀ tx, tx ∈ windowTxs events → sender tx ∈ signers ∧
txCannotCreateContractAtAddrs K evmExecTxCore revertTx tx signers) →
(∃ finalBudget, checkWindow snapshot budget previous events = Some finalBudget) →
∃ post receipts, executeWindow actual events = Some (post, receipts).
Proof using All.
induction events as [|event rest IH]; intros actual budget previous signers h current last
Hscope Hinv Hlast Hdelay Hordered Hbounds Hsc Hcrypto Hcheck.
{ ∃ actual, []. reflexivity. }
destruct event as [b|tx].
{ simpl in Hordered. destruct Hordered as [Hb [Hh Hordered]].
simpl in Hcheck |- ×.
eapply (IH actual (advanceEffReserveBals K snapshot budget b) previous signers h b last);
[exact Hscope|eapply invariant_boundary; [exact Hinv|] | | | | | | |].
{ specialize (Hbounds (BeginBlock b) ltac:(set_solver)). simpl in Hbounds. lia. }
{ specialize (Hbounds (BeginBlock b) ltac:(set_solver)). simpl in Hbounds. lia. }
{ exact Hdelay. }
{ exact Hordered. }
{ intros e He. apply Hbounds. set_solver. }
{ exact Hsc. }
{ exact Hcrypto. }
{ exact Hcheck. } }
simpl in Hordered. destruct Hordered as [Hblock [Hh Hordered]].
destruct Hcheck as [finalBudget Hcheck].
cbn [checkWindow] in Hcheck.
destruct (senderBudget snapshot (budget (sender tx)) previous tx)
as [remaining|] eqn:Hsender; [|discriminate].
destruct (Hcrypto tx ltac:(unfold windowTxs; simpl; set_solver)) as [Hin HtxCrypto].
pose proof (senderBudget_fee _ _ _ _ _ _ _ _ _ Hinv Hin Hblock Hlast Hsender)
as [Hfee Hguard].
assert (HscPost : ∀ a, a ∈ signers → isSC
(reservebaldelayed.execValidatedTx K evmExecTxCore revertTx actual tx).1.1 a = false).
{ intros a Ha. apply HtxCrypto; [exact Ha|apply Hsc; exact Ha]. }
pose proof (invariant_transaction _ _ _ _ _ _ _ _ _ Hscope Hinv Hin Hblock Hlast
ltac:(lia) Hsender HscPost) as Hnext.
assert (HscNext : ∀ a, a ∈ signers → isSC (execValidatedTx actual tx).1.1 a = false).
{ unfold execValidatedTx. rewrite Hguard. exact HscPost. }
destruct (IH (execValidatedTx actual tx).1
(updateKey budget (sender tx) (fun _ ⇒ remaining)) (tx :: previous)
signers h current last Hscope Hnext Hlast Hdelay Hordered)
as [post [receipts Hexecute]].
{ intros e He. apply Hbounds. set_solver. }
{ exact HscNext. }
{ intros t Ht. apply Hcrypto. unfold windowTxs in ×. simpl in ×. set_solver. }
{ ∃ finalBudget. exact Hcheck. }
∃ post, ((execValidatedTx actual tx).2 :: receipts).
cbn [executeWindow]. unfold validateTx. rewrite bool_decide_true; [|exact Hfee].
destruct (execValidatedTx actual tx) as [next receipt]. simpl in Hexecute |- ×.
rewrite Hexecute. reflexivity.
Qed.
End ExecutionProofs.
Consensus acceptance of the entire designated window guarantees that
every transaction reaches EVM execution or an ordinary charged revert,
rather than aborting the block for inability to pay its gas fee.
The EVM frame rule is needed only for this window's transaction signers,
including delegated senders. The non-contract-signer and no-contract-
creation premises are inherited from the imported model. No premise assumes
fee solvency, monotone thresholds, or success of configuration writes.
Theorem consensus_fee_solvency : feeSolvency.
Proof using All.
intros snapshot h events eas Hordered Hbounds Hsc Hcrypto Haccept.
eapply (checkWindow_exec snapshot events snapshot (initialBudget snapshot h) []
(map sender (windowTxs events)) h h (h + K)); try eassumption.
{ intros a Ha. exact Ha. }
{ constructor.
{ intros a Ha. unfold initialBudget. lia. }
{ intros a Ha. unfold initialBudget. lia. }
{ intros a n Hn. reflexivity. }
{ intros inter tx Hn. reflexivity. }
{ intros inter tx Ha Hn Hgate. lia. } }
{ lia. }
{ lia. }
{ intros tx Htx. split.
{ apply list_elem_of_In, in_map, list_elem_of_In. exact Htx. }
{ apply Hcrypto. exact Htx. } }
Qed.
Lemma executeWindow_receipt_count events :
∀ state post receipts,
executeWindow state events = Some (post, receipts) →
length receipts = length (windowTxs events).
Proof using All.
induction events as [|event rest IH]; intros state post receipts Hexec.
{ simpl in Hexec. injection Hexec as <- <-. reflexivity. }
destruct event as [b|tx].
{ eapply IH. exact Hexec. }
cbn [executeWindow] in Hexec.
destruct (validateTx state.1 tx); [|discriminate].
destruct (execValidatedTx state tx) as [next receipt].
destruct (executeWindow next rest) as [[final tail]|] eqn:Hrest; [|discriminate].
injection Hexec as <- <-. cbn [windowTxs flat_map app length].
f_equal. eapply IH. exact Hrest.
Qed.
End Draft.
Print Assumptions consensus_fee_solvency.
Proof using All.
intros snapshot h events eas Hordered Hbounds Hsc Hcrypto Haccept.
eapply (checkWindow_exec snapshot events snapshot (initialBudget snapshot h) []
(map sender (windowTxs events)) h h (h + K)); try eassumption.
{ intros a Ha. exact Ha. }
{ constructor.
{ intros a Ha. unfold initialBudget. lia. }
{ intros a Ha. unfold initialBudget. lia. }
{ intros a n Hn. reflexivity. }
{ intros inter tx Hn. reflexivity. }
{ intros inter tx Ha Hn Hgate. lia. } }
{ lia. }
{ lia. }
{ intros tx Htx. split.
{ apply list_elem_of_In, in_map, list_elem_of_In. exact Htx. }
{ apply Hcrypto. exact Htx. } }
Qed.
Lemma executeWindow_receipt_count events :
∀ state post receipts,
executeWindow state events = Some (post, receipts) →
length receipts = length (windowTxs events).
Proof using All.
induction events as [|event rest IH]; intros state post receipts Hexec.
{ simpl in Hexec. injection Hexec as <- <-. reflexivity. }
destruct event as [b|tx].
{ eapply IH. exact Hexec. }
cbn [executeWindow] in Hexec.
destruct (validateTx state.1 tx); [|discriminate].
destruct (execValidatedTx state tx) as [next receipt].
destruct (executeWindow next rest) as [[final tail]|] eqn:Hrest; [|discriminate].
injection Hexec as <- <-. cbn [windowTxs flat_map app length].
f_equal. eapply IH. exact Hrest.
Qed.
End Draft.
Print Assumptions consensus_fee_solvency.
Configuration regressions
These use a three-block delay. A zero reserve is a configured value, not a request to restore the default. A mature update permits a fresh request.
Example default_is_ten_mon :
delayedReserveBal 3 unconfiguredAccount 100 = 10000000000000000000.
Proof. reflexivity. Qed.
Example zero_activates_exactly_at_four :
match setReserveBalance 3 unconfiguredAccount 1 0 with
| None ⇒ False
| Some configured ⇒
delayedReserveBal 3 configured 3 = defaultReserve ∧
delayedReserveBal 3 configured 4 = 0
end.
Proof. vm_compute. auto. Qed.
Section BudgetRegressions.
Variable accounts : StateOfAccounts.
Variable alice : EvmAddr.
Hypothesis alice_balance : balanceOfAc accounts alice = 100.
Example empty_block_increase_does_not_refill :
let snapshot := (accounts, fun _ ⇒
{| lastTxInBlockIndex := None; lastDelUndelInBlockIndex := None;
settledReserveBal := 10; pendingReserveBal := Some (100, 0) |}) in
match checkWindow 3 snapshot (initialBudget 3 snapshot 0) []
[BeginBlock 1; BeginBlock 2; BeginBlock 3] with
| None ⇒ False
| Some budget ⇒ budget alice = 10%Z
end.
Proof using All.
cbn [checkWindow]. unfold advanceEffReserveBals, initialBudget.
cbn [fst snd]. rewrite alice_balance.
vm_compute. reflexivity.
Qed.
Example empty_block_decrease_clamps :
let snapshot := (accounts, fun _ ⇒
{| lastTxInBlockIndex := None; lastDelUndelInBlockIndex := None;
settledReserveBal := 10; pendingReserveBal := Some (2, 0) |}) in
match checkWindow 3 snapshot (initialBudget 3 snapshot 0) []
[BeginBlock 1; BeginBlock 2; BeginBlock 3] with
| None ⇒ False
| Some budget ⇒ budget alice = 2%Z
end.
Proof using All.
cbn [checkWindow]. unfold advanceEffReserveBals, initialBudget.
cbn [fst snd]. rewrite alice_balance.
vm_compute. reflexivity.
Qed.
End BudgetRegressions.
Example same_block_last_setting_wins :
match setReserveBalance 3 unconfiguredAccount 1 0 with
| None ⇒ False
| Some first ⇒
match setReserveBalance 3 first 1 42 with
| None ⇒ False
| Some second ⇒ delayedReserveBal 3 second 4 = 42
end
end.
Proof. vm_compute. reflexivity. Qed.
Example cannot_replace_an_earlier_blocks_pending_setting :
match setReserveBalance 3 unconfiguredAccount 1 0 with
| None ⇒ False
| Some first ⇒ setReserveBalance 3 first 2 42 = None
end.
Proof. vm_compute. reflexivity. Qed.
Example can_schedule_again_after_activation :
match setReserveBalance 3 unconfiguredAccount 1 0 with
| None ⇒ False
| Some first ⇒
match setReserveBalance 3 first 4 42 with
| None ⇒ False
| Some second ⇒ delayedReserveBal 3 second 4 = 0 ∧
delayedReserveBal 3 second 7 = 42
end
end.
Proof. vm_compute. auto. Qed.
Example overflowing_activation_rejected :
setReserveBalance 3 unconfiguredAccount (2^256 - 2) 10 = None.
Proof. vm_compute. reflexivity. Qed.
Example out_of_range_reserve_rejected :
setReserveBalance 3 unconfiguredAccount 1 (2^256) = None.
Proof. vm_compute. reflexivity. Qed.
delayedReserveBal 3 unconfiguredAccount 100 = 10000000000000000000.
Proof. reflexivity. Qed.
Example zero_activates_exactly_at_four :
match setReserveBalance 3 unconfiguredAccount 1 0 with
| None ⇒ False
| Some configured ⇒
delayedReserveBal 3 configured 3 = defaultReserve ∧
delayedReserveBal 3 configured 4 = 0
end.
Proof. vm_compute. auto. Qed.
Section BudgetRegressions.
Variable accounts : StateOfAccounts.
Variable alice : EvmAddr.
Hypothesis alice_balance : balanceOfAc accounts alice = 100.
Example empty_block_increase_does_not_refill :
let snapshot := (accounts, fun _ ⇒
{| lastTxInBlockIndex := None; lastDelUndelInBlockIndex := None;
settledReserveBal := 10; pendingReserveBal := Some (100, 0) |}) in
match checkWindow 3 snapshot (initialBudget 3 snapshot 0) []
[BeginBlock 1; BeginBlock 2; BeginBlock 3] with
| None ⇒ False
| Some budget ⇒ budget alice = 10%Z
end.
Proof using All.
cbn [checkWindow]. unfold advanceEffReserveBals, initialBudget.
cbn [fst snd]. rewrite alice_balance.
vm_compute. reflexivity.
Qed.
Example empty_block_decrease_clamps :
let snapshot := (accounts, fun _ ⇒
{| lastTxInBlockIndex := None; lastDelUndelInBlockIndex := None;
settledReserveBal := 10; pendingReserveBal := Some (2, 0) |}) in
match checkWindow 3 snapshot (initialBudget 3 snapshot 0) []
[BeginBlock 1; BeginBlock 2; BeginBlock 3] with
| None ⇒ False
| Some budget ⇒ budget alice = 2%Z
end.
Proof using All.
cbn [checkWindow]. unfold advanceEffReserveBals, initialBudget.
cbn [fst snd]. rewrite alice_balance.
vm_compute. reflexivity.
Qed.
End BudgetRegressions.
Example same_block_last_setting_wins :
match setReserveBalance 3 unconfiguredAccount 1 0 with
| None ⇒ False
| Some first ⇒
match setReserveBalance 3 first 1 42 with
| None ⇒ False
| Some second ⇒ delayedReserveBal 3 second 4 = 42
end
end.
Proof. vm_compute. reflexivity. Qed.
Example cannot_replace_an_earlier_blocks_pending_setting :
match setReserveBalance 3 unconfiguredAccount 1 0 with
| None ⇒ False
| Some first ⇒ setReserveBalance 3 first 2 42 = None
end.
Proof. vm_compute. reflexivity. Qed.
Example can_schedule_again_after_activation :
match setReserveBalance 3 unconfiguredAccount 1 0 with
| None ⇒ False
| Some first ⇒
match setReserveBalance 3 first 4 42 with
| None ⇒ False
| Some second ⇒ delayedReserveBal 3 second 4 = 0 ∧
delayedReserveBal 3 second 7 = 42
end
end.
Proof. vm_compute. auto. Qed.
Example overflowing_activation_rejected :
setReserveBalance 3 unconfiguredAccount (2^256 - 2) 10 = None.
Proof. vm_compute. reflexivity. Qed.
Example out_of_range_reserve_rejected :
setReserveBalance 3 unconfiguredAccount 1 (2^256) = None.
Proof. vm_compute. reflexivity. Qed.
A delegated sender cannot use snapshot funds to refresh a spent budget.
The final execution check must reject an excessive fee even when saturating
subtraction alone would have returned zero. All test fees are positive.
Section SenderRegressions.
Variable accounts : StateOfAccounts.
Variable tx : TxWithHdr.
Hypothesis delegated_sender : addrDelegated accounts (sender tx) = true.
Hypothesis sender_balance : balanceOfAc accounts (sender tx) = 100.
Hypothesis sender_fee : maxTxFee tx = 3.
Example ordinary_budget_is_not_refreshed :
let snapshot := (accounts, fun _ ⇒
{| lastTxInBlockIndex := None; lastDelUndelInBlockIndex := None;
settledReserveBal := 100; pendingReserveBal := None |}) in
senderBudget 3 snapshot 2 [] tx = None.
Proof using All.
unfold senderBudget, isAllowedToEmpty. cbn [fst snd].
rewrite delegated_sender sender_fee. simpl. reflexivity.
Qed.
Example fee_exceeding_protected_balance_is_rejected :
let state := (accounts, fun _ ⇒
{| lastTxInBlockIndex := None; lastDelUndelInBlockIndex := None;
settledReserveBal := 2; pendingReserveBal := None |}) in
senderFeeProtected 3 state tx = false.
Proof using All.
unfold senderFeeProtected, isAllowedToEmpty. cbn [fst snd].
rewrite delegated_sender sender_fee sender_balance. simpl. reflexivity.
Qed.
End SenderRegressions.
Variable accounts : StateOfAccounts.
Variable tx : TxWithHdr.
Hypothesis delegated_sender : addrDelegated accounts (sender tx) = true.
Hypothesis sender_balance : balanceOfAc accounts (sender tx) = 100.
Hypothesis sender_fee : maxTxFee tx = 3.
Example ordinary_budget_is_not_refreshed :
let snapshot := (accounts, fun _ ⇒
{| lastTxInBlockIndex := None; lastDelUndelInBlockIndex := None;
settledReserveBal := 100; pendingReserveBal := None |}) in
senderBudget 3 snapshot 2 [] tx = None.
Proof using All.
unfold senderBudget, isAllowedToEmpty. cbn [fst snd].
rewrite delegated_sender sender_fee. simpl. reflexivity.
Qed.
Example fee_exceeding_protected_balance_is_rejected :
let state := (accounts, fun _ ⇒
{| lastTxInBlockIndex := None; lastDelUndelInBlockIndex := None;
settledReserveBal := 2; pendingReserveBal := None |}) in
senderFeeProtected 3 state tx = false.
Proof using All.
unfold senderFeeProtected, isAllowedToEmpty. cbn [fst snd].
rewrite delegated_sender sender_fee sender_balance. simpl. reflexivity.
Qed.
End SenderRegressions.