Library QedkConcrete.consensusrebase
Moving the consensus snapshot forward
From Stdlib Require Import List String ZArith Bool Lia.
From stdpp Require Import decidable strings.
Require Import RocqOfOCaml.RocqOfOCaml.
Require Import QedkConcrete.consensuswindow QedkConcrete.consensusbudget
QedkConcrete.consensushistoryproofs QedkConcrete.consensusbacking
QedkConcrete.consensushistory QedkConcrete.publictransaction
QedkConcrete.authorizationnonces QedkConcrete.transactionhistoryprojection.
Require Import QedkConcrete.transactionchecks.
Require Import QedkConcrete.consensusdecisions.
Set Default Goal Selector "!".
Import ListNotations.
Local Open Scope Z_scope.
Module E := Execution.Make.
Module W := State.WorldState.
Module A := Chain.Ethereum.Account.
Module T := Chain.Ethereum.Transaction.
Module Addr := Chain.Ethereum.Address.
Module Block := Chain.Ethereum.Block.
Lemma sender_budget_monotone emptying balance threshold previous fee value remaining
balance' threshold' previous' :
previous ≤ previous' → threshold ≤ threshold' →
(emptying = true → balance ≤ balance') →
checkSenderBudget emptying balance threshold previous fee value = Some remaining →
∃ remaining',
checkSenderBudget emptying balance' threshold' previous' fee value = Some remaining' ∧
remaining ≤ remaining'.
Proof.
intros Hprevious Hthreshold Hbalance. unfold checkSenderBudget. rewrite !bool_decide_Z_le.
destruct emptying.
- specialize (Hbalance eq_refl).
destruct (fee <=? balance) eqn:Hfee; [|discriminate].
apply Z.leb_le in Hfee. rewrite (proj2 (Z.leb_le fee balance') ltac:(lia)).
intro Heq. injection Heq as <-. eexists. split; [reflexivity|lia].
- destruct (fee <=? previous) eqn:Hfee; [|discriminate].
apply Z.leb_le in Hfee. rewrite (proj2 (Z.leb_le fee previous') ltac:(lia)).
intro Heq. injection Heq as <-. eexists. split; [reflexivity|lia].
Qed.
Section Rebase.
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).
Local Abbreviation recoveredAuthority := (@T.Authorization.authority).
Record ConsensusRefines (snapshot newer : W.t) (old fresh : ConsensusState)
(signers : list Addr.t) (until : N) : Prop := {
rebase_code : ∀ a, In a signers →
@A.is_smart_contract (readAccount snapshot a) =
@A.is_smart_contract (readAccount newer a);
rebase_nonces : ∀ a, In a signers →
historyNonces (consensusHistory old) a = historyNonces (consensusHistory fresh) a;
rebase_emptying : ∀ a, In a signers → a ≠ Chain.Monad.system_sender →
∀ n, (n ≤ until)%N →
historyAllowsEmptying snapshot (consensusHistory old) a n =
historyAllowsEmptying newer (consensusHistory fresh) a n;
rebase_balance : ∀ a, In a signers → a ≠ Chain.Monad.system_sender →
∀ n, (n ≤ until)%N →
historyAllowsEmptying snapshot (consensusHistory old) a n = true →
balance snapshot a ≤ balance newer a;
rebase_budgets : ∀ a, In a signers → consensusBudgets old a ≤ consensusBudgets fresh a
}.
Lemma check_transaction_rebase snapshot newer old fresh signers until header tx next :
ConsensusRefines snapshot newer old fresh signers until →
(Block.Header.t.number header ≤ until)%N →
(∀ a, transactionSender tx = Some a → In a signers) →
(∀ a, In a signers → configured snapshot (Block.Header.t.number header) a =
configured newer (Block.Header.t.number header) a) →
checkTransaction snapshot header old tx = Some next →
∃ next', checkTransaction newer header fresh tx = Some next' ∧
ConsensusRefines snapshot newer next next' signers until.
Proof.
intros Href Hheight Hsender Hconfig Hcheck.
destruct Href as [Hcode Hnonce Hempty Hbalance Hbudget].
unfold checkTransaction, accountAt, reserveAt in Hcheck |- ×.
destruct (transactionSender tx) as [sender|] eqn:Hsender_rec; [|discriminate].
assert (Hsigned : In sender signers) by exact (Hsender sender eq_refl).
pose proof (authorizations_nonce_on (fun a ⇒ In a signers) (T.authorization_list tx)
(fun a ⇒ negb (@A.is_smart_contract (readAccount snapshot a)))
(fun a ⇒ if String.eqb sender a then
@Numeric.U64.op_plus (historyNonces (consensusHistory old) a) (@Numeric.U64.one)
else historyNonces (consensusHistory old) a)
(fun a ⇒ negb (@A.is_smart_contract (readAccount newer a)))
(fun a ⇒ if String.eqb sender a then
@Numeric.U64.op_plus (historyNonces (consensusHistory fresh) a) (@Numeric.U64.one)
else historyNonces (consensusHistory fresh) a) [] [] ltac:(reflexivity)) as Hprojection.
specialize (Hprojection ltac:(intros a Ha; cbv beta; rewrite (Hcode a Ha), (Hnonce a Ha); auto)).
assert (Hadvance : (∀ a, In a signers →
existsb (fun b ⇒ String.eqb b a) (fst (advanceHistory snapshot (consensusHistory old) sender tx)) =
existsb (fun b ⇒ String.eqb b a) (fst (advanceHistory newer (consensusHistory fresh) sender tx))) ∧
∀ a, In a signers →
historyNonces (snd (advanceHistory snapshot (consensusHistory old) sender tx)) a =
historyNonces (snd (advanceHistory newer (consensusHistory fresh) sender tx)) a).
{ unfold advanceHistory.
destruct (@authorizationNonces F _ _ _) as [auths nonces],
(@authorizationNonces F _ _ _) as [auths' nonces'].
exact Hprojection. }
pose proof (advance_history_emptying snapshot (consensusHistory old) sender tx) as HoldEmpty.
pose proof (advance_history_emptying newer (consensusHistory fresh) sender tx) as HnewEmpty.
destruct (advanceHistory snapshot (consensusHistory old) sender tx) as [auths history] eqn:Hold.
destruct (advanceHistory newer (consensusHistory fresh) sender tx) as [auths' history'] eqn:Hnew.
cbn [fst snd] in Hadvance, HoldEmpty, HnewEmpty.
destruct Hadvance as [Hauthorities Hnonces].
rewrite <- (Hnonce sender Hsigned).
destruct (bool_decide_reflect (sender = Chain.Monad.system_sender)) as [Hsys | HnotSys].
- destruct (systemTransactionChecks tx (historyNonces (consensusHistory old) sender));
[|discriminate Hcheck].
injection Hcheck as <-.
eexists. split; [reflexivity|]. constructor; cbn [consensusHistory consensusBudgets].
+ exact Hcode.
+ exact Hnonces.
+ intros a Ha HnotSys_a n Hn. rewrite HoldEmpty, HnewEmpty, (Hempty a Ha HnotSys_a n Hn).
cbn [existsb]. rewrite (Hauthorities a Ha). reflexivity.
+ intros a Ha HnotSys_a n Hn He. rewrite HoldEmpty in He.
apply Bool.andb_true_iff in He as [He _]. exact (Hbalance a Ha HnotSys_a n Hn He).
+ exact Hbudget.
- destruct (transactionQuote header tx) as [[qsender fee]|] eqn:Hquote; [|discriminate].
match type of Hcheck with context [transactionChecks ?h ?t ?n] ⇒
destruct (transactionChecks h t n) end; [|discriminate].
rewrite !bool_decide_not_in in Hcheck |- ×.
rewrite <- (Hauthorities sender Hsigned).
rewrite <- (Hempty sender Hsigned HnotSys _ Hheight), <- (Hconfig sender Hsigned).
match type of Hcheck with context [checkSenderBudget ?e ?b ?t ?p ?f ?v] ⇒
destruct (checkSenderBudget e b t p f v) as [remaining|] eqn:Haccepted
end; [|discriminate].
destruct (sender_budget_monotone _ _ _ _ _ _ _ (balance newer sender)
(Numeric.U256.to_z (configured snapshot (Block.Header.t.number header) sender))
(consensusBudgets fresh sender) (Hbudget sender Hsigned) (Z.le_refl _)
ltac:(intro He; apply Bool.andb_true_iff in He as [He _];
exact (Hbalance sender Hsigned HnotSys _ Hheight He)) Haccepted)
as [remaining' [Haccepted' Hremaining]].
rewrite Haccepted'. injection Hcheck as <-.
eexists. split; [reflexivity|]. constructor; cbn [consensusHistory consensusBudgets].
+ exact Hcode.
+ exact Hnonces.
+ intros a Ha HnotSys_a n Hn. rewrite HoldEmpty, HnewEmpty, (Hempty a Ha HnotSys_a n Hn).
cbn [existsb]. rewrite (Hauthorities a Ha). reflexivity.
+ intros a Ha HnotSys_a n Hn He. rewrite HoldEmpty in He.
apply Bool.andb_true_iff in He as [He _]. exact (Hbalance a Ha HnotSys_a n Hn He).
+ intros a Ha. rewrite !bool_decide_string_eq.
destruct (String.eqb sender a); [exact Hremaining|exact (Hbudget a Ha)].
Qed.
Lemma check_transactions_rebase snapshot newer signers until header txs :
(Block.Header.t.number header ≤ until)%N →
Forall (fun tx ⇒
(∀ a, transactionSender tx = Some a → In a signers)) txs →
(∀ a, In a signers → configured snapshot (Block.Header.t.number header) a =
configured newer (Block.Header.t.number header) a) →
∀ old fresh next,
ConsensusRefines snapshot newer old fresh signers until →
checkTransactions snapshot header old txs = Some next →
∃ next', checkTransactions newer header fresh txs = Some next' ∧
ConsensusRefines snapshot newer next next' signers until.
Proof.
intros Hheight Hinputs Hconfig. induction Hinputs as [|tx txs Hsender Hinputs IH];
intros old fresh next Href Hcheck.
- cbn [checkTransactions] in Hcheck |- ×. injection Hcheck as <-.
∃ fresh. auto.
- cbn [checkTransactions] in Hcheck |- ×.
destruct (checkTransaction snapshot header old tx) as [middle|] eqn:Htx; [|discriminate].
destruct (check_transaction_rebase snapshot newer old fresh signers until header tx middle
Href Hheight Hsender Hconfig Htx) as [middle' [Htx' Hmiddle]].
rewrite Htx'. exact (IH middle middle' next Hmiddle Hcheck).
Qed.
Lemma rebase_clamp snapshot newer old fresh signers until n :
ConsensusRefines snapshot newer old fresh signers until →
(∀ a, In a signers → configured snapshot n a = configured newer n a) →
ConsensusRefines snapshot newer
{| consensusHistory := consensusHistory old;
consensusBudgets := fun a ⇒ Z.min (consensusBudgets old a)
(Numeric.U256.to_z (configured snapshot n a)) |}
{| consensusHistory := consensusHistory fresh;
consensusBudgets := fun a ⇒ Z.min (consensusBudgets fresh a)
(Numeric.U256.to_z (configured newer n a)) |} signers until.
Proof.
intros [Hcode Hnonce Hempty Hbalance Hbudget] Hconfig.
constructor; cbn [consensusHistory consensusBudgets]; try assumption.
intros a Ha. rewrite (Hconfig a Ha). specialize (Hbudget a Ha). lia.
Qed.
Theorem check_blocks_rebase snapshot newer signers until blocks :
Forall (fun block ⇒
(Block.Header.t.number (Block.t.header block) ≤ until)%N ∧
Forall (fun tx ⇒
(∀ a, transactionSender tx = Some a → In a signers)) (Block.t.transactions block) ∧
(∀ a, In a signers → configured snapshot (Block.Header.t.number (Block.t.header block)) a =
configured newer (Block.Header.t.number (Block.t.header block)) a)) blocks →
∀ old fresh next,
ConsensusRefines snapshot newer old fresh signers until →
checkBlocks snapshot old blocks = Some next →
∃ next', checkBlocks newer fresh blocks = Some next' ∧
ConsensusRefines snapshot newer next next' signers until.
Proof.
intro Hinputs. induction Hinputs as [|block blocks [Hheight [Htxs Hconfig]] Hinputs IH];
intros old fresh next Href Hcheck.
- cbn [checkBlocks] in Hcheck |- ×. unfold reserveAt in Hcheck |- ×. injection Hcheck as <-. ∃ fresh. auto.
- cbn [checkBlocks] in Hcheck |- ×. unfold reserveAt in Hcheck |- ×.
match type of Hcheck with context [checkTransactions ?s ?h ?p ?ts] ⇒
destruct (checkTransactions s h p ts) as [middle|] eqn:HtxsCheck
end; [|discriminate].
destruct (check_transactions_rebase snapshot newer signers until
(Block.t.header block) (Block.t.transactions block) Hheight Htxs Hconfig
_ _ middle (rebase_clamp _ _ _ _ _ _ _ Href Hconfig) HtxsCheck)
as [middle' [HtxsCheck' Hmiddle]].
rewrite HtxsCheck'. exact (IH middle middle' next Hmiddle Hcheck).
Qed.
From stdpp Require Import decidable strings.
Require Import RocqOfOCaml.RocqOfOCaml.
Require Import QedkConcrete.consensuswindow QedkConcrete.consensusbudget
QedkConcrete.consensushistoryproofs QedkConcrete.consensusbacking
QedkConcrete.consensushistory QedkConcrete.publictransaction
QedkConcrete.authorizationnonces QedkConcrete.transactionhistoryprojection.
Require Import QedkConcrete.transactionchecks.
Require Import QedkConcrete.consensusdecisions.
Set Default Goal Selector "!".
Import ListNotations.
Local Open Scope Z_scope.
Module E := Execution.Make.
Module W := State.WorldState.
Module A := Chain.Ethereum.Account.
Module T := Chain.Ethereum.Transaction.
Module Addr := Chain.Ethereum.Address.
Module Block := Chain.Ethereum.Block.
Lemma sender_budget_monotone emptying balance threshold previous fee value remaining
balance' threshold' previous' :
previous ≤ previous' → threshold ≤ threshold' →
(emptying = true → balance ≤ balance') →
checkSenderBudget emptying balance threshold previous fee value = Some remaining →
∃ remaining',
checkSenderBudget emptying balance' threshold' previous' fee value = Some remaining' ∧
remaining ≤ remaining'.
Proof.
intros Hprevious Hthreshold Hbalance. unfold checkSenderBudget. rewrite !bool_decide_Z_le.
destruct emptying.
- specialize (Hbalance eq_refl).
destruct (fee <=? balance) eqn:Hfee; [|discriminate].
apply Z.leb_le in Hfee. rewrite (proj2 (Z.leb_le fee balance') ltac:(lia)).
intro Heq. injection Heq as <-. eexists. split; [reflexivity|lia].
- destruct (fee <=? previous) eqn:Hfee; [|discriminate].
apply Z.leb_le in Hfee. rewrite (proj2 (Z.leb_le fee previous') ltac:(lia)).
intro Heq. injection Heq as <-. eexists. split; [reflexivity|lia].
Qed.
Section Rebase.
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).
Local Abbreviation recoveredAuthority := (@T.Authorization.authority).
Record ConsensusRefines (snapshot newer : W.t) (old fresh : ConsensusState)
(signers : list Addr.t) (until : N) : Prop := {
rebase_code : ∀ a, In a signers →
@A.is_smart_contract (readAccount snapshot a) =
@A.is_smart_contract (readAccount newer a);
rebase_nonces : ∀ a, In a signers →
historyNonces (consensusHistory old) a = historyNonces (consensusHistory fresh) a;
rebase_emptying : ∀ a, In a signers → a ≠ Chain.Monad.system_sender →
∀ n, (n ≤ until)%N →
historyAllowsEmptying snapshot (consensusHistory old) a n =
historyAllowsEmptying newer (consensusHistory fresh) a n;
rebase_balance : ∀ a, In a signers → a ≠ Chain.Monad.system_sender →
∀ n, (n ≤ until)%N →
historyAllowsEmptying snapshot (consensusHistory old) a n = true →
balance snapshot a ≤ balance newer a;
rebase_budgets : ∀ a, In a signers → consensusBudgets old a ≤ consensusBudgets fresh a
}.
Lemma check_transaction_rebase snapshot newer old fresh signers until header tx next :
ConsensusRefines snapshot newer old fresh signers until →
(Block.Header.t.number header ≤ until)%N →
(∀ a, transactionSender tx = Some a → In a signers) →
(∀ a, In a signers → configured snapshot (Block.Header.t.number header) a =
configured newer (Block.Header.t.number header) a) →
checkTransaction snapshot header old tx = Some next →
∃ next', checkTransaction newer header fresh tx = Some next' ∧
ConsensusRefines snapshot newer next next' signers until.
Proof.
intros Href Hheight Hsender Hconfig Hcheck.
destruct Href as [Hcode Hnonce Hempty Hbalance Hbudget].
unfold checkTransaction, accountAt, reserveAt in Hcheck |- ×.
destruct (transactionSender tx) as [sender|] eqn:Hsender_rec; [|discriminate].
assert (Hsigned : In sender signers) by exact (Hsender sender eq_refl).
pose proof (authorizations_nonce_on (fun a ⇒ In a signers) (T.authorization_list tx)
(fun a ⇒ negb (@A.is_smart_contract (readAccount snapshot a)))
(fun a ⇒ if String.eqb sender a then
@Numeric.U64.op_plus (historyNonces (consensusHistory old) a) (@Numeric.U64.one)
else historyNonces (consensusHistory old) a)
(fun a ⇒ negb (@A.is_smart_contract (readAccount newer a)))
(fun a ⇒ if String.eqb sender a then
@Numeric.U64.op_plus (historyNonces (consensusHistory fresh) a) (@Numeric.U64.one)
else historyNonces (consensusHistory fresh) a) [] [] ltac:(reflexivity)) as Hprojection.
specialize (Hprojection ltac:(intros a Ha; cbv beta; rewrite (Hcode a Ha), (Hnonce a Ha); auto)).
assert (Hadvance : (∀ a, In a signers →
existsb (fun b ⇒ String.eqb b a) (fst (advanceHistory snapshot (consensusHistory old) sender tx)) =
existsb (fun b ⇒ String.eqb b a) (fst (advanceHistory newer (consensusHistory fresh) sender tx))) ∧
∀ a, In a signers →
historyNonces (snd (advanceHistory snapshot (consensusHistory old) sender tx)) a =
historyNonces (snd (advanceHistory newer (consensusHistory fresh) sender tx)) a).
{ unfold advanceHistory.
destruct (@authorizationNonces F _ _ _) as [auths nonces],
(@authorizationNonces F _ _ _) as [auths' nonces'].
exact Hprojection. }
pose proof (advance_history_emptying snapshot (consensusHistory old) sender tx) as HoldEmpty.
pose proof (advance_history_emptying newer (consensusHistory fresh) sender tx) as HnewEmpty.
destruct (advanceHistory snapshot (consensusHistory old) sender tx) as [auths history] eqn:Hold.
destruct (advanceHistory newer (consensusHistory fresh) sender tx) as [auths' history'] eqn:Hnew.
cbn [fst snd] in Hadvance, HoldEmpty, HnewEmpty.
destruct Hadvance as [Hauthorities Hnonces].
rewrite <- (Hnonce sender Hsigned).
destruct (bool_decide_reflect (sender = Chain.Monad.system_sender)) as [Hsys | HnotSys].
- destruct (systemTransactionChecks tx (historyNonces (consensusHistory old) sender));
[|discriminate Hcheck].
injection Hcheck as <-.
eexists. split; [reflexivity|]. constructor; cbn [consensusHistory consensusBudgets].
+ exact Hcode.
+ exact Hnonces.
+ intros a Ha HnotSys_a n Hn. rewrite HoldEmpty, HnewEmpty, (Hempty a Ha HnotSys_a n Hn).
cbn [existsb]. rewrite (Hauthorities a Ha). reflexivity.
+ intros a Ha HnotSys_a n Hn He. rewrite HoldEmpty in He.
apply Bool.andb_true_iff in He as [He _]. exact (Hbalance a Ha HnotSys_a n Hn He).
+ exact Hbudget.
- destruct (transactionQuote header tx) as [[qsender fee]|] eqn:Hquote; [|discriminate].
match type of Hcheck with context [transactionChecks ?h ?t ?n] ⇒
destruct (transactionChecks h t n) end; [|discriminate].
rewrite !bool_decide_not_in in Hcheck |- ×.
rewrite <- (Hauthorities sender Hsigned).
rewrite <- (Hempty sender Hsigned HnotSys _ Hheight), <- (Hconfig sender Hsigned).
match type of Hcheck with context [checkSenderBudget ?e ?b ?t ?p ?f ?v] ⇒
destruct (checkSenderBudget e b t p f v) as [remaining|] eqn:Haccepted
end; [|discriminate].
destruct (sender_budget_monotone _ _ _ _ _ _ _ (balance newer sender)
(Numeric.U256.to_z (configured snapshot (Block.Header.t.number header) sender))
(consensusBudgets fresh sender) (Hbudget sender Hsigned) (Z.le_refl _)
ltac:(intro He; apply Bool.andb_true_iff in He as [He _];
exact (Hbalance sender Hsigned HnotSys _ Hheight He)) Haccepted)
as [remaining' [Haccepted' Hremaining]].
rewrite Haccepted'. injection Hcheck as <-.
eexists. split; [reflexivity|]. constructor; cbn [consensusHistory consensusBudgets].
+ exact Hcode.
+ exact Hnonces.
+ intros a Ha HnotSys_a n Hn. rewrite HoldEmpty, HnewEmpty, (Hempty a Ha HnotSys_a n Hn).
cbn [existsb]. rewrite (Hauthorities a Ha). reflexivity.
+ intros a Ha HnotSys_a n Hn He. rewrite HoldEmpty in He.
apply Bool.andb_true_iff in He as [He _]. exact (Hbalance a Ha HnotSys_a n Hn He).
+ intros a Ha. rewrite !bool_decide_string_eq.
destruct (String.eqb sender a); [exact Hremaining|exact (Hbudget a Ha)].
Qed.
Lemma check_transactions_rebase snapshot newer signers until header txs :
(Block.Header.t.number header ≤ until)%N →
Forall (fun tx ⇒
(∀ a, transactionSender tx = Some a → In a signers)) txs →
(∀ a, In a signers → configured snapshot (Block.Header.t.number header) a =
configured newer (Block.Header.t.number header) a) →
∀ old fresh next,
ConsensusRefines snapshot newer old fresh signers until →
checkTransactions snapshot header old txs = Some next →
∃ next', checkTransactions newer header fresh txs = Some next' ∧
ConsensusRefines snapshot newer next next' signers until.
Proof.
intros Hheight Hinputs Hconfig. induction Hinputs as [|tx txs Hsender Hinputs IH];
intros old fresh next Href Hcheck.
- cbn [checkTransactions] in Hcheck |- ×. injection Hcheck as <-.
∃ fresh. auto.
- cbn [checkTransactions] in Hcheck |- ×.
destruct (checkTransaction snapshot header old tx) as [middle|] eqn:Htx; [|discriminate].
destruct (check_transaction_rebase snapshot newer old fresh signers until header tx middle
Href Hheight Hsender Hconfig Htx) as [middle' [Htx' Hmiddle]].
rewrite Htx'. exact (IH middle middle' next Hmiddle Hcheck).
Qed.
Lemma rebase_clamp snapshot newer old fresh signers until n :
ConsensusRefines snapshot newer old fresh signers until →
(∀ a, In a signers → configured snapshot n a = configured newer n a) →
ConsensusRefines snapshot newer
{| consensusHistory := consensusHistory old;
consensusBudgets := fun a ⇒ Z.min (consensusBudgets old a)
(Numeric.U256.to_z (configured snapshot n a)) |}
{| consensusHistory := consensusHistory fresh;
consensusBudgets := fun a ⇒ Z.min (consensusBudgets fresh a)
(Numeric.U256.to_z (configured newer n a)) |} signers until.
Proof.
intros [Hcode Hnonce Hempty Hbalance Hbudget] Hconfig.
constructor; cbn [consensusHistory consensusBudgets]; try assumption.
intros a Ha. rewrite (Hconfig a Ha). specialize (Hbudget a Ha). lia.
Qed.
Theorem check_blocks_rebase snapshot newer signers until blocks :
Forall (fun block ⇒
(Block.Header.t.number (Block.t.header block) ≤ until)%N ∧
Forall (fun tx ⇒
(∀ a, transactionSender tx = Some a → In a signers)) (Block.t.transactions block) ∧
(∀ a, In a signers → configured snapshot (Block.Header.t.number (Block.t.header block)) a =
configured newer (Block.Header.t.number (Block.t.header block)) a)) blocks →
∀ old fresh next,
ConsensusRefines snapshot newer old fresh signers until →
checkBlocks snapshot old blocks = Some next →
∃ next', checkBlocks newer fresh blocks = Some next' ∧
ConsensusRefines snapshot newer next next' signers until.
Proof.
intro Hinputs. induction Hinputs as [|block blocks [Hheight [Htxs Hconfig]] Hinputs IH];
intros old fresh next Href Hcheck.
- cbn [checkBlocks] in Hcheck |- ×. unfold reserveAt in Hcheck |- ×. injection Hcheck as <-. ∃ fresh. auto.
- cbn [checkBlocks] in Hcheck |- ×. unfold reserveAt in Hcheck |- ×.
match type of Hcheck with context [checkTransactions ?s ?h ?p ?ts] ⇒
destruct (checkTransactions s h p ts) as [middle|] eqn:HtxsCheck
end; [|discriminate].
destruct (check_transactions_rebase snapshot newer signers until
(Block.t.header block) (Block.t.transactions block) Hheight Htxs Hconfig
_ _ middle (rebase_clamp _ _ _ _ _ _ _ Href Hconfig) HtxsCheck)
as [middle' [HtxsCheck' Hmiddle]].
rewrite HtxsCheck'. exact (IH middle middle' next Hmiddle Hcheck).
Qed.
Restarting from execution is justified by the maintained balance/history
invariant, not by assuming that the suffix passes the new check. The
configuration equality is supplied separately by delayed activation.
Lemma backing_rebase snapshot old newer signers height until :
BudgetBacking snapshot old newer signers height until →
(∀ a, In a signers → @A.is_smart_contract (readAccount snapshot a) = false) →
(∀ a, In a signers → configured snapshot height a = configured newer height a) →
ConsensusRefines snapshot newer old (initialConsensusState newer height) signers until.
Proof.
intros [Hhistory Hnumbers Hidle] Hcode Hconfig. constructor.
- intros a Ha. rewrite (Hcode a Ha), (history_noncontract _ _ _ _ _ Hhistory a Ha). reflexivity.
- intros a Ha. symmetry. exact (history_nonce_agreement _ _ _ _ _ Hhistory a Ha).
- intros a Ha HnotSys n Hn. cbn [initialConsensusState consensusHistory].
rewrite initial_history_emptying. symmetry.
exact (history_emptying_agreement _ _ _ _ _ Hhistory a Ha HnotSys n Hn).
- intros a Ha HnotSys n Hn He. apply (Hidle a Ha HnotSys n Hn).
rewrite (history_emptying_agreement _ _ _ _ _ Hhistory a Ha HnotSys n Hn). exact He.
- intros a Ha. cbn [initialConsensusState consensusBudgets]. unfold accountAt, reserveAt.
specialize (Hnumbers a Ha). rewrite <- (Hconfig a Ha). lia.
Qed.
Theorem backed_suffix_rechecks snapshot old newer signers height until first rest next :
BudgetBacking snapshot old newer signers height until →
(∀ a, In a signers → @A.is_smart_contract (readAccount snapshot a) = false) →
Forall (fun block ⇒
(Block.Header.t.number (Block.t.header block) ≤ until)%N ∧
Forall (fun tx ⇒
(∀ a, transactionSender tx = Some a → In a signers)) (Block.t.transactions block) ∧
(∀ a, In a signers → configured snapshot (Block.Header.t.number (Block.t.header block)) a =
configured newer (Block.Header.t.number (Block.t.header block)) a)) (first :: rest) →
checkBlocks snapshot old (first :: rest) = Some next →
∃ next', checkBlocks newer
(initialConsensusState newer (Block.Header.t.number (Block.t.header first)))
(first :: rest) = Some next'.
Proof.
intros [Hhistory Hnumbers Hidle] Hcode Hinputs Hcheck.
inversion Hinputs as [|? ? [Hheight [Htxs Hconfig]] Hrest]; subst.
set (clamped := {| consensusHistory := consensusHistory old;
consensusBudgets := fun a ⇒ Z.min (consensusBudgets old a)
(Numeric.U256.to_z (configured snapshot (Block.Header.t.number (Block.t.header first)) a)) |}).
assert (Href : ConsensusRefines snapshot newer clamped
{| consensusHistory := initialHistory newer;
consensusBudgets := fun a ⇒ Z.min
(consensusBudgets (initialConsensusState newer (Block.Header.t.number (Block.t.header first))) a)
(Numeric.U256.to_z (configured newer (Block.Header.t.number (Block.t.header first)) a)) |}
signers until).
{ constructor; cbn [clamped consensusHistory consensusBudgets initialConsensusState];
unfold accountAt, reserveAt.
- intros a Ha. rewrite (Hcode a Ha), (history_noncontract _ _ _ _ _ Hhistory a Ha). reflexivity.
- intros a Ha. symmetry. exact (history_nonce_agreement _ _ _ _ _ Hhistory a Ha).
- intros a Ha HnotSys n Hn. rewrite initial_history_emptying. symmetry.
exact (history_emptying_agreement _ _ _ _ _ Hhistory a Ha HnotSys n Hn).
- intros a Ha HnotSys n Hn He. apply (Hidle a Ha HnotSys n Hn).
rewrite (history_emptying_agreement _ _ _ _ _ Hhistory a Ha HnotSys n Hn). exact He.
- intros a Ha. specialize (Hnumbers a Ha). rewrite (Hconfig a Ha). lia. }
cbn [checkBlocks] in Hcheck |- ×. unfold reserveAt in Hcheck |- ×.
change (match checkTransactions snapshot (Block.t.header first) clamped (Block.t.transactions first) with
| Some middle ⇒ checkBlocks snapshot middle rest | None ⇒ None end = Some next) in Hcheck.
destruct (checkTransactions snapshot (Block.t.header first) clamped (Block.t.transactions first))
as [middle|] eqn:Hfirst; [|discriminate].
destruct (check_transactions_rebase snapshot newer signers until (Block.t.header first)
(Block.t.transactions first) Hheight Htxs Hconfig _ _ _ Href Hfirst)
as [middle' [Hfirst' Hmiddle]].
cbn [initialConsensusState consensusHistory consensusBudgets] in Hfirst' |- ×.
unfold accountAt, reserveAt in Hfirst' |- ×.
rewrite Hfirst'. destruct (check_blocks_rebase snapshot newer signers until rest Hrest
_ _ _ Hmiddle Hcheck) as [next' [Hnext' _]]. eauto.
Qed.
End Rebase.
BudgetBacking snapshot old newer signers height until →
(∀ a, In a signers → @A.is_smart_contract (readAccount snapshot a) = false) →
(∀ a, In a signers → configured snapshot height a = configured newer height a) →
ConsensusRefines snapshot newer old (initialConsensusState newer height) signers until.
Proof.
intros [Hhistory Hnumbers Hidle] Hcode Hconfig. constructor.
- intros a Ha. rewrite (Hcode a Ha), (history_noncontract _ _ _ _ _ Hhistory a Ha). reflexivity.
- intros a Ha. symmetry. exact (history_nonce_agreement _ _ _ _ _ Hhistory a Ha).
- intros a Ha HnotSys n Hn. cbn [initialConsensusState consensusHistory].
rewrite initial_history_emptying. symmetry.
exact (history_emptying_agreement _ _ _ _ _ Hhistory a Ha HnotSys n Hn).
- intros a Ha HnotSys n Hn He. apply (Hidle a Ha HnotSys n Hn).
rewrite (history_emptying_agreement _ _ _ _ _ Hhistory a Ha HnotSys n Hn). exact He.
- intros a Ha. cbn [initialConsensusState consensusBudgets]. unfold accountAt, reserveAt.
specialize (Hnumbers a Ha). rewrite <- (Hconfig a Ha). lia.
Qed.
Theorem backed_suffix_rechecks snapshot old newer signers height until first rest next :
BudgetBacking snapshot old newer signers height until →
(∀ a, In a signers → @A.is_smart_contract (readAccount snapshot a) = false) →
Forall (fun block ⇒
(Block.Header.t.number (Block.t.header block) ≤ until)%N ∧
Forall (fun tx ⇒
(∀ a, transactionSender tx = Some a → In a signers)) (Block.t.transactions block) ∧
(∀ a, In a signers → configured snapshot (Block.Header.t.number (Block.t.header block)) a =
configured newer (Block.Header.t.number (Block.t.header block)) a)) (first :: rest) →
checkBlocks snapshot old (first :: rest) = Some next →
∃ next', checkBlocks newer
(initialConsensusState newer (Block.Header.t.number (Block.t.header first)))
(first :: rest) = Some next'.
Proof.
intros [Hhistory Hnumbers Hidle] Hcode Hinputs Hcheck.
inversion Hinputs as [|? ? [Hheight [Htxs Hconfig]] Hrest]; subst.
set (clamped := {| consensusHistory := consensusHistory old;
consensusBudgets := fun a ⇒ Z.min (consensusBudgets old a)
(Numeric.U256.to_z (configured snapshot (Block.Header.t.number (Block.t.header first)) a)) |}).
assert (Href : ConsensusRefines snapshot newer clamped
{| consensusHistory := initialHistory newer;
consensusBudgets := fun a ⇒ Z.min
(consensusBudgets (initialConsensusState newer (Block.Header.t.number (Block.t.header first))) a)
(Numeric.U256.to_z (configured newer (Block.Header.t.number (Block.t.header first)) a)) |}
signers until).
{ constructor; cbn [clamped consensusHistory consensusBudgets initialConsensusState];
unfold accountAt, reserveAt.
- intros a Ha. rewrite (Hcode a Ha), (history_noncontract _ _ _ _ _ Hhistory a Ha). reflexivity.
- intros a Ha. symmetry. exact (history_nonce_agreement _ _ _ _ _ Hhistory a Ha).
- intros a Ha HnotSys n Hn. rewrite initial_history_emptying. symmetry.
exact (history_emptying_agreement _ _ _ _ _ Hhistory a Ha HnotSys n Hn).
- intros a Ha HnotSys n Hn He. apply (Hidle a Ha HnotSys n Hn).
rewrite (history_emptying_agreement _ _ _ _ _ Hhistory a Ha HnotSys n Hn). exact He.
- intros a Ha. specialize (Hnumbers a Ha). rewrite (Hconfig a Ha). lia. }
cbn [checkBlocks] in Hcheck |- ×. unfold reserveAt in Hcheck |- ×.
change (match checkTransactions snapshot (Block.t.header first) clamped (Block.t.transactions first) with
| Some middle ⇒ checkBlocks snapshot middle rest | None ⇒ None end = Some next) in Hcheck.
destruct (checkTransactions snapshot (Block.t.header first) clamped (Block.t.transactions first))
as [middle|] eqn:Hfirst; [|discriminate].
destruct (check_transactions_rebase snapshot newer signers until (Block.t.header first)
(Block.t.transactions first) Hheight Htxs Hconfig _ _ _ Href Hfirst)
as [middle' [Hfirst' Hmiddle]].
cbn [initialConsensusState consensusHistory consensusBudgets] in Hfirst' |- ×.
unfold accountAt, reserveAt in Hfirst' |- ×.
rewrite Hfirst'. destruct (check_blocks_rebase snapshot newer signers until rest Hrest
_ _ _ Hmiddle Hcheck) as [next' [Hnext' _]]. eauto.
Qed.
End Rebase.