Library SetcodeConcrete.blockactivityagreement
Public activity through system calls and complete blocks
From Stdlib Require Import List ZArith Lia.
Require Import RocqOfOCaml.RocqOfOCaml MonadVM.Execution.
Require Import QedkConcrete.blockentry QedkConcrete.hostframe
QedkConcrete.recursiveassembly.
Require Import SetcodeConcrete.activityagreement SetcodeConcrete.executioncontext
SetcodeConcrete.contextframe SetcodeConcrete.blocknonceagreement
SetcodeConcrete.consensuschecks.
Set Default Goal Selector "!".
#[local] Hint Extern 100 (Basics.Unreachable _) ⇒
(apply Opaque.legacy_witness; Opaque.opaque_inhabit 64%nat) : typeclass_instances.
Module E := Execution.Make.
Module W := State.WorldState.
Module B := State.BlockState.
Module Block := Chain.Ethereum.Block.
Section Execution.
Context {F : E.FArgs}.
Context (cold warm access write reserve : N).
Context (Hrevision : E.Params.(Execution.Make_Params_signature.revision) =
Support.Variants.Revision.Setcode (cold, warm, access, write, reserve)).
Context (Hcold : (0 < cold)%N) (Hwarm : (0 < warm)%N).
Local Opaque Evmc.Instantiate._recursive_modules_Host_Vm.
Lemma host_call_keeps_context msg :
(0 ≤ Evmc.Message.t.gas msg < 2^63)%Z →
preservesMigrationContext (@E.Host.call F msg).
Proof.
intro Hgas. unfold E.Host.call.
cbv beta iota zeta delta [E.Instantiation Host.Instantiate.functor
Host.Instantiate.Instantiate_result.Host Host.Instantiate.Host
H.functor H.Make_result.call].
eapply call_preserves_migration_context.
- exact Hrevision.
- intros nested body Hbound.
pose proof (execution_vm_preserves_migration_context cold warm access write reserve
Hrevision Hcold Hwarm nested body Hbound) as Hvm.
cbv beta iota zeta delta [Host.Instantiate.Vm.execute Host.Instantiate.Vm] in Hvm |- ×.
exact Hvm.
- exact Hgas.
Qed.
Lemma system_message_keeps_counters state addr data :
W.t.next_emptying_transaction_block (B.t.world_state
(snd (E.process_system_message state addr data))) =
W.t.next_emptying_transaction_block (B.t.world_state state).
Proof.
unfold E.process_system_message.
match goal with |- context [if ?test then _ else _] ⇒ destruct test end;
[reflexivity|].
cbn beta iota zeta.
match goal with |- context [E.Host.call ?message ?entry] ⇒
set (msg := message); set (initial := entry) end.
assert (Hgas : (0 ≤ Evmc.Message.t.gas msg < 2^63)%Z)
by (change (0 ≤ 30000000 < 2^63)%Z; lia).
pose proof (host_call_keeps_context msg Hgas initial) as Hcontext.
destruct (E.Host.call msg initial) as [result afterVm].
cbn [snd] in Hcontext. unfold migrationContext, transactionContext in Hcontext.
injection Hcontext as Hinitial Hblock Horigin Hlimit Hprice Hhistory Hcounters Hprior Hchain.
exact Hcounters.
Qed.
Lemma block_entry_keeps_counters world block :
W.t.next_emptying_transaction_block
(B.t.world_state (blockTransactionEntry world block)) =
W.t.next_emptying_transaction_block world.
Proof.
unfold blockTransactionEntry. cbn zeta.
rewrite !system_message_keeps_counters. reflexivity.
Qed.
Lemma activity_same_counters snapshot activity before after until :
W.t.next_emptying_transaction_block after = W.t.next_emptying_transaction_block before →
ActivityAgreement snapshot activity before until → ActivityAgreement snapshot activity after until.
Proof.
intros Heq [Hmap Hrecent Habsent].
assert (Hread : ∀ a, emptyingAfter after a = emptyingAfter before a).
{ intro a. unfold emptyingAfter, State.op_pointcaretlparenrparen,
W.next_emptying_transaction_block_for. cbn. rewrite Heq. reflexivity. }
constructor; [rewrite Heq; exact Hmap| |].
- intros a Hin. rewrite Hread. exact (Hrecent a Hin).
- intros a Hnot. rewrite Hread. exact (Habsent a Hnot).
Qed.
Lemma withdrawals_keep_counters state withdrawals :
W.t.next_emptying_transaction_block (B.t.world_state
(Result.List.fold_left E.process_withdrawal state withdrawals)) =
W.t.next_emptying_transaction_block (B.t.world_state state).
Proof.
revert state. induction withdrawals as [|wd rest IH]; intro state; [reflexivity|].
change (W.t.next_emptying_transaction_block (B.t.world_state
(Result.List.fold_left E.process_withdrawal (E.process_withdrawal state wd) rest)) =
W.t.next_emptying_transaction_block (B.t.world_state state)).
rewrite IH. reflexivity.
Qed.
Theorem block_activity_agreement verify snapshot previous next world block post until :
ActivityAgreement snapshot (admissionActivity previous) world until →
(until < Block.Header.t.number (Block.t.header block) + 3)%N →
checkTransactions snapshot (Block.t.header block) previous (Block.t.transactions block) = Some next →
E.process_block verify world block = inl post →
ActivityAgreement snapshot (admissionActivity next) post until.
Proof.
intros Hbefore Htime Hcheck Hrun.
assert (Hentry : ActivityAgreement snapshot (admissionActivity previous)
(B.t.world_state (blockTransactionEntry world block)) until).
{ eapply activity_same_counters; [apply block_entry_keeps_counters|exact Hbefore]. }
unfold E.process_block in Hrun. rewrite Hrevision in Hrun.
change (@Result.op_letdollar) with
(fun (A B C : Set) (r : A + B) (f : A → C + B) ⇒
match r with inl value ⇒ f value | inr error ⇒ inr error end) in Hrun.
destruct (E.validate_block world block) as [[]|error]; [|discriminate].
cbn beta iota zeta in Hrun.
match type of Hrun with context [Result.List.fold_leftM ?step ?entry ?txs] ⇒
change entry with (blockTransactionEntry world block) in Hrun end.
destruct (Result.List.fold_leftM E.process_transaction (blockTransactionEntry world block)
(Block.t.transactions block)) as [after|error] eqn:Htxs; [|discriminate].
pose proof (transactions_activity_agreement cold warm access write reserve Hrevision Hcold Hwarm
snapshot previous next until (blockTransactionEntry world block) (Block.t.transactions block) after
Hentry ltac:(rewrite block_entry_current; exact Htime)
ltac:(rewrite block_entry_current; exact Hcheck) Htxs) as Htransactions.
pose proof (activity_same_counters snapshot (admissionActivity next) _ _ until
(withdrawals_keep_counters after (Block.t.withdrawals block)) Htransactions) as Hwithdrawals.
cbn beta iota zeta in Hrun.
match type of Hrun with context [E.validate_block ?w ?b] ⇒ destruct (E.validate_block w b) as [[]|error] end;
[|discriminate].
destruct verify; [|injection Hrun as <-;
eapply activity_same_counters with (before := B.t.world_state
(Result.List.fold_left E.process_withdrawal after (Block.t.withdrawals block)));
[reflexivity|exact Hwithdrawals]].
match type of Hrun with context [match ?check with inl _ ⇒ _ | inr _ ⇒ _ end] ⇒
destruct check as [[]|error] end;
[injection Hrun as <-; eapply activity_same_counters with (before := B.t.world_state
(Result.List.fold_left E.process_withdrawal after (Block.t.withdrawals block)));
[reflexivity|exact Hwithdrawals]|discriminate].
Qed.
Theorem blocks_activity_agreement verify snapshot previous next world blocks post until :
Forall (fun block ⇒ (until < Block.Header.t.number (Block.t.header block) + 3)%N) blocks →
ActivityAgreement snapshot (admissionActivity previous) world until →
checkBlocks snapshot previous blocks = Some next →
Result.List.fold_leftM (E.process_block verify) world blocks = inl post →
ActivityAgreement snapshot (admissionActivity next) post until.
Proof.
intro Htimes. revert previous world.
induction Htimes as [|block rest Htime Hrest IH]; intros previous world Hbefore Hcheck Hrun.
- cbn [checkBlocks] in Hcheck. injection Hcheck as <-.
injection Hrun as <-. exact Hbefore.
- cbn [checkBlocks] in Hcheck.
match type of Hcheck with context [checkTransactions ?s ?h ?p ?txs] ⇒
set (capped := p) in *;
destruct (checkTransactions s h capped txs) as [middle|] eqn:Htxs end; [|discriminate].
change (Result.op_letdollar (E.process_block verify world block)
(fun after ⇒ Result.List.fold_leftM (E.process_block verify) after rest) = inl post) in Hrun.
destruct (E.process_block verify world block) as [after|error] eqn:Hblock; [|discriminate].
apply (IH middle after).
+ exact (block_activity_agreement verify snapshot capped middle world block after until
Hbefore Htime Htxs Hblock).
+ exact Hcheck.
+ exact Hrun.
Qed.
End Execution.
Print Assumptions block_activity_agreement.
Print Assumptions blocks_activity_agreement.
Require Import RocqOfOCaml.RocqOfOCaml MonadVM.Execution.
Require Import QedkConcrete.blockentry QedkConcrete.hostframe
QedkConcrete.recursiveassembly.
Require Import SetcodeConcrete.activityagreement SetcodeConcrete.executioncontext
SetcodeConcrete.contextframe SetcodeConcrete.blocknonceagreement
SetcodeConcrete.consensuschecks.
Set Default Goal Selector "!".
#[local] Hint Extern 100 (Basics.Unreachable _) ⇒
(apply Opaque.legacy_witness; Opaque.opaque_inhabit 64%nat) : typeclass_instances.
Module E := Execution.Make.
Module W := State.WorldState.
Module B := State.BlockState.
Module Block := Chain.Ethereum.Block.
Section Execution.
Context {F : E.FArgs}.
Context (cold warm access write reserve : N).
Context (Hrevision : E.Params.(Execution.Make_Params_signature.revision) =
Support.Variants.Revision.Setcode (cold, warm, access, write, reserve)).
Context (Hcold : (0 < cold)%N) (Hwarm : (0 < warm)%N).
Local Opaque Evmc.Instantiate._recursive_modules_Host_Vm.
Lemma host_call_keeps_context msg :
(0 ≤ Evmc.Message.t.gas msg < 2^63)%Z →
preservesMigrationContext (@E.Host.call F msg).
Proof.
intro Hgas. unfold E.Host.call.
cbv beta iota zeta delta [E.Instantiation Host.Instantiate.functor
Host.Instantiate.Instantiate_result.Host Host.Instantiate.Host
H.functor H.Make_result.call].
eapply call_preserves_migration_context.
- exact Hrevision.
- intros nested body Hbound.
pose proof (execution_vm_preserves_migration_context cold warm access write reserve
Hrevision Hcold Hwarm nested body Hbound) as Hvm.
cbv beta iota zeta delta [Host.Instantiate.Vm.execute Host.Instantiate.Vm] in Hvm |- ×.
exact Hvm.
- exact Hgas.
Qed.
Lemma system_message_keeps_counters state addr data :
W.t.next_emptying_transaction_block (B.t.world_state
(snd (E.process_system_message state addr data))) =
W.t.next_emptying_transaction_block (B.t.world_state state).
Proof.
unfold E.process_system_message.
match goal with |- context [if ?test then _ else _] ⇒ destruct test end;
[reflexivity|].
cbn beta iota zeta.
match goal with |- context [E.Host.call ?message ?entry] ⇒
set (msg := message); set (initial := entry) end.
assert (Hgas : (0 ≤ Evmc.Message.t.gas msg < 2^63)%Z)
by (change (0 ≤ 30000000 < 2^63)%Z; lia).
pose proof (host_call_keeps_context msg Hgas initial) as Hcontext.
destruct (E.Host.call msg initial) as [result afterVm].
cbn [snd] in Hcontext. unfold migrationContext, transactionContext in Hcontext.
injection Hcontext as Hinitial Hblock Horigin Hlimit Hprice Hhistory Hcounters Hprior Hchain.
exact Hcounters.
Qed.
Lemma block_entry_keeps_counters world block :
W.t.next_emptying_transaction_block
(B.t.world_state (blockTransactionEntry world block)) =
W.t.next_emptying_transaction_block world.
Proof.
unfold blockTransactionEntry. cbn zeta.
rewrite !system_message_keeps_counters. reflexivity.
Qed.
Lemma activity_same_counters snapshot activity before after until :
W.t.next_emptying_transaction_block after = W.t.next_emptying_transaction_block before →
ActivityAgreement snapshot activity before until → ActivityAgreement snapshot activity after until.
Proof.
intros Heq [Hmap Hrecent Habsent].
assert (Hread : ∀ a, emptyingAfter after a = emptyingAfter before a).
{ intro a. unfold emptyingAfter, State.op_pointcaretlparenrparen,
W.next_emptying_transaction_block_for. cbn. rewrite Heq. reflexivity. }
constructor; [rewrite Heq; exact Hmap| |].
- intros a Hin. rewrite Hread. exact (Hrecent a Hin).
- intros a Hnot. rewrite Hread. exact (Habsent a Hnot).
Qed.
Lemma withdrawals_keep_counters state withdrawals :
W.t.next_emptying_transaction_block (B.t.world_state
(Result.List.fold_left E.process_withdrawal state withdrawals)) =
W.t.next_emptying_transaction_block (B.t.world_state state).
Proof.
revert state. induction withdrawals as [|wd rest IH]; intro state; [reflexivity|].
change (W.t.next_emptying_transaction_block (B.t.world_state
(Result.List.fold_left E.process_withdrawal (E.process_withdrawal state wd) rest)) =
W.t.next_emptying_transaction_block (B.t.world_state state)).
rewrite IH. reflexivity.
Qed.
Theorem block_activity_agreement verify snapshot previous next world block post until :
ActivityAgreement snapshot (admissionActivity previous) world until →
(until < Block.Header.t.number (Block.t.header block) + 3)%N →
checkTransactions snapshot (Block.t.header block) previous (Block.t.transactions block) = Some next →
E.process_block verify world block = inl post →
ActivityAgreement snapshot (admissionActivity next) post until.
Proof.
intros Hbefore Htime Hcheck Hrun.
assert (Hentry : ActivityAgreement snapshot (admissionActivity previous)
(B.t.world_state (blockTransactionEntry world block)) until).
{ eapply activity_same_counters; [apply block_entry_keeps_counters|exact Hbefore]. }
unfold E.process_block in Hrun. rewrite Hrevision in Hrun.
change (@Result.op_letdollar) with
(fun (A B C : Set) (r : A + B) (f : A → C + B) ⇒
match r with inl value ⇒ f value | inr error ⇒ inr error end) in Hrun.
destruct (E.validate_block world block) as [[]|error]; [|discriminate].
cbn beta iota zeta in Hrun.
match type of Hrun with context [Result.List.fold_leftM ?step ?entry ?txs] ⇒
change entry with (blockTransactionEntry world block) in Hrun end.
destruct (Result.List.fold_leftM E.process_transaction (blockTransactionEntry world block)
(Block.t.transactions block)) as [after|error] eqn:Htxs; [|discriminate].
pose proof (transactions_activity_agreement cold warm access write reserve Hrevision Hcold Hwarm
snapshot previous next until (blockTransactionEntry world block) (Block.t.transactions block) after
Hentry ltac:(rewrite block_entry_current; exact Htime)
ltac:(rewrite block_entry_current; exact Hcheck) Htxs) as Htransactions.
pose proof (activity_same_counters snapshot (admissionActivity next) _ _ until
(withdrawals_keep_counters after (Block.t.withdrawals block)) Htransactions) as Hwithdrawals.
cbn beta iota zeta in Hrun.
match type of Hrun with context [E.validate_block ?w ?b] ⇒ destruct (E.validate_block w b) as [[]|error] end;
[|discriminate].
destruct verify; [|injection Hrun as <-;
eapply activity_same_counters with (before := B.t.world_state
(Result.List.fold_left E.process_withdrawal after (Block.t.withdrawals block)));
[reflexivity|exact Hwithdrawals]].
match type of Hrun with context [match ?check with inl _ ⇒ _ | inr _ ⇒ _ end] ⇒
destruct check as [[]|error] end;
[injection Hrun as <-; eapply activity_same_counters with (before := B.t.world_state
(Result.List.fold_left E.process_withdrawal after (Block.t.withdrawals block)));
[reflexivity|exact Hwithdrawals]|discriminate].
Qed.
Theorem blocks_activity_agreement verify snapshot previous next world blocks post until :
Forall (fun block ⇒ (until < Block.Header.t.number (Block.t.header block) + 3)%N) blocks →
ActivityAgreement snapshot (admissionActivity previous) world until →
checkBlocks snapshot previous blocks = Some next →
Result.List.fold_leftM (E.process_block verify) world blocks = inl post →
ActivityAgreement snapshot (admissionActivity next) post until.
Proof.
intro Htimes. revert previous world.
induction Htimes as [|block rest Htime Hrest IH]; intros previous world Hbefore Hcheck Hrun.
- cbn [checkBlocks] in Hcheck. injection Hcheck as <-.
injection Hrun as <-. exact Hbefore.
- cbn [checkBlocks] in Hcheck.
match type of Hcheck with context [checkTransactions ?s ?h ?p ?txs] ⇒
set (capped := p) in *;
destruct (checkTransactions s h capped txs) as [middle|] eqn:Htxs end; [|discriminate].
change (Result.op_letdollar (E.process_block verify world block)
(fun after ⇒ Result.List.fold_leftM (E.process_block verify) after rest) = inl post) in Hrun.
destruct (E.process_block verify world block) as [after|error] eqn:Hblock; [|discriminate].
apply (IH middle after).
+ exact (block_activity_agreement verify snapshot capped middle world block after until
Hbefore Htime Htxs Hblock).
+ exact Hcheck.
+ exact Hrun.
Qed.
End Execution.
Print Assumptions block_activity_agreement.
Print Assumptions blocks_activity_agreement.