Library SetcodeConcrete.blockcode
From Stdlib Require Import List String ZArith NArith Lia.
Require Import RocqOfOCaml.RocqOfOCaml MonadVM.Execution.
Require Import QedkConcrete.systemcode QedkConcrete.persistentcode QedkConcrete.accountcode
QedkConcrete.worldaccounts QedkConcrete.stringmaps.
Require Import SetcodeConcrete.transactioncode SetcodeConcrete.executionpersistent
SetcodeConcrete.codeframe.
Set Default Goal Selector "!".
#[local] Hint Extern 100 (Basics.Unreachable _) ⇒
(apply Opaque.legacy_witness; Opaque.opaque_inhabit 64%nat) : typeclass_instances.
Local Open Scope Z_scope.
Module E := Execution.Make.
Module B := State.BlockState.
Module S := State.TransactionState.
Module W := State.WorldState.
Module A := Chain.Ethereum.Account.
Module Block := Chain.Ethereum.Block.
Definition prescribedSystemCode {F : E.FArgs} world :=
worldCode E.beacon_roots_address beaconRootsCode world ∧
worldCode E.history_storage_address historyStorageCode world.
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).
Context (query : Chain.Ethereum.Address.t) (code : string).
Context (Hsystem : Setcode_rules.system_account query = true).
Context (Hnonempty : code ≠ Byte_string.Bytes.empty).
Context (Hregular : regularCode code).
Lemma system_message_preserves_code block addr data :
worldCode query code (B.t.world_state block) →
worldCode query code (B.t.world_state (snd (E.process_system_message block addr data))).
Proof.
intros [Hmap Hcode]. unfold E.process_system_message.
match goal with |- context [if ?test then _ else _] ⇒ destruct test end;
[split; assumption|].
cbn beta iota zeta.
match goal with |- context [E.Host.call ?msg ?entry] ⇒
assert (Hentry : persistentCode query code entry) by
(constructor; [exact Hmap|exact Hcode|cbn; tauto|cbn; tauto]);
pose proof (execution_call_preserves_existing_code query code Hsystem
cold warm access write reserve Hcold Hwarm msg Hnonempty Hrevision
ltac:(change (0 ≤ 30000000 < 2^63); lia) entry Hentry) as Hafter;
destruct (E.Host.call msg entry) as [result after]
end.
split; [exact (persistent_code_map _ _ _ Hafter)|exact (persistent_code_value _ _ _ Hafter)].
Qed.
Lemma transactions_preserve_system_code txs state post :
worldCode query code (B.t.world_state state) →
Result.List.fold_leftM E.process_transaction state txs = inl post →
worldCode query code (B.t.world_state post).
Proof.
revert state. induction txs as [|tx rest IH]; intros state Hbefore Hrun.
- injection Hrun as <-. exact Hbefore.
- change (Result.op_letdollar (E.process_transaction state tx)
(fun middle ⇒ Result.List.fold_leftM E.process_transaction middle rest) = inl post) in Hrun.
destruct (E.process_transaction state tx) as [middle|error] eqn:Htx; [|discriminate].
assert (Hvalid : ∃ validation, E.validate_transaction state tx = inl validation).
{ unfold E.process_transaction in Htx. destruct (E.validate_transaction state tx);
[eexists; reflexivity|discriminate]. }
destruct Hvalid as [validation Hvalidation].
apply (IH middle).
+ eapply transaction_preserves_system_code;
[exact Hrevision|exact Hcold|exact Hwarm|exact Hsystem|exact Hregular|
exact Hnonempty|exact Hvalidation|exact Htx|exact Hbefore].
+ exact Hrun.
Qed.
Lemma withdrawals_preserve_code withdrawals block :
worldCode query code (B.t.world_state block) →
worldCode query code (B.t.world_state (Result.List.fold_left E.process_withdrawal block withdrawals)).
Proof.
revert block. induction withdrawals as [|wd rest IH]; intros block Hbefore; [exact Hbefore|].
change (worldCode query code (B.t.world_state (Result.List.fold_left E.process_withdrawal
(E.process_withdrawal block wd) rest))).
apply IH. unfold E.process_withdrawal. apply world_code_write with
(world := B.t.world_state block) (keep := false) (addr := Chain.Ethereum.Withdrawal.t.recipient wd);
[exact Hbefore|]. intro Heq; rewrite Heq. exact (proj2 Hbefore).
Qed.
Theorem block_preserves_system_account verify world block post :
worldCode query code world → E.process_block verify world block = inl post →
worldCode query code post.
Proof.
intros [Hmap Hcode] Hrun.
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 [B.make ?w block] ⇒ set (initial := B.make w block) in × end.
assert (Hinitial : worldCode query code (B.t.world_state initial)).
{ split; [apply account_write_wellformed; exact Hmap|].
exact (eq_trans (activation_preserves_code world query Hmap) Hcode). }
match type of Hrun with context [E.process_system_message initial ?addr ?data] ⇒
pose proof (system_message_preserves_code initial addr data Hinitial) as Hbeacon;
destruct (E.process_system_message initial addr data) as [beaconResult afterBeacon]
end.
cbn beta iota zeta delta [snd] in Hbeacon, Hrun.
match type of Hrun with context [E.process_system_message afterBeacon ?addr ?data] ⇒
pose proof (system_message_preserves_code afterBeacon addr data Hbeacon) as Hhistory;
destruct (E.process_system_message afterBeacon addr data) as [historyResult afterHistory]
end.
cbn beta iota zeta delta [snd] in Hhistory, Hrun.
destruct (Result.List.fold_leftM E.process_transaction afterHistory (Block.t.transactions block))
as [afterTransactions|error] eqn:Htxs; [|discriminate].
pose proof (transactions_preserve_system_code _ _ _ Hhistory Htxs) as Htransactions.
pose proof (withdrawals_preserve_code (Block.t.withdrawals block) afterTransactions 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 <-; exact Hwithdrawals].
match type of Hrun with context [match ?check with inl _ ⇒ _ | inr _ ⇒ _ end] ⇒
destruct check as [[]|error] end;
[injection Hrun as <-; exact Hwithdrawals|discriminate].
Qed.
End Execution.
Theorem block_preserves_system_code {F : E.FArgs} cold warm access write reserve verify world block post :
E.Params.(Execution.Make_Params_signature.revision) =
Support.Variants.Revision.Setcode (cold, warm, access, write, reserve) →
(0 < cold)%N → (0 < warm)%N → prescribedSystemCode world →
E.process_block verify world block = inl post → prescribedSystemCode post.
Proof.
intros Hrevision Hcold Hwarm [Hbeacon Hhistory] Hrun. split.
- eapply block_preserves_system_account;
[exact Hrevision|exact Hcold|exact Hwarm| | | |exact Hbeacon|exact Hrun].
+ vm_compute. reflexivity.
+ vm_compute. discriminate.
+ vm_compute. reflexivity.
- eapply block_preserves_system_account;
[exact Hrevision|exact Hcold|exact Hwarm| | | |exact Hhistory|exact Hrun].
+ vm_compute. reflexivity.
+ vm_compute. discriminate.
+ vm_compute. reflexivity.
Qed.
Print Assumptions block_preserves_system_code.
Require Import RocqOfOCaml.RocqOfOCaml MonadVM.Execution.
Require Import QedkConcrete.systemcode QedkConcrete.persistentcode QedkConcrete.accountcode
QedkConcrete.worldaccounts QedkConcrete.stringmaps.
Require Import SetcodeConcrete.transactioncode SetcodeConcrete.executionpersistent
SetcodeConcrete.codeframe.
Set Default Goal Selector "!".
#[local] Hint Extern 100 (Basics.Unreachable _) ⇒
(apply Opaque.legacy_witness; Opaque.opaque_inhabit 64%nat) : typeclass_instances.
Local Open Scope Z_scope.
Module E := Execution.Make.
Module B := State.BlockState.
Module S := State.TransactionState.
Module W := State.WorldState.
Module A := Chain.Ethereum.Account.
Module Block := Chain.Ethereum.Block.
Definition prescribedSystemCode {F : E.FArgs} world :=
worldCode E.beacon_roots_address beaconRootsCode world ∧
worldCode E.history_storage_address historyStorageCode world.
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).
Context (query : Chain.Ethereum.Address.t) (code : string).
Context (Hsystem : Setcode_rules.system_account query = true).
Context (Hnonempty : code ≠ Byte_string.Bytes.empty).
Context (Hregular : regularCode code).
Lemma system_message_preserves_code block addr data :
worldCode query code (B.t.world_state block) →
worldCode query code (B.t.world_state (snd (E.process_system_message block addr data))).
Proof.
intros [Hmap Hcode]. unfold E.process_system_message.
match goal with |- context [if ?test then _ else _] ⇒ destruct test end;
[split; assumption|].
cbn beta iota zeta.
match goal with |- context [E.Host.call ?msg ?entry] ⇒
assert (Hentry : persistentCode query code entry) by
(constructor; [exact Hmap|exact Hcode|cbn; tauto|cbn; tauto]);
pose proof (execution_call_preserves_existing_code query code Hsystem
cold warm access write reserve Hcold Hwarm msg Hnonempty Hrevision
ltac:(change (0 ≤ 30000000 < 2^63); lia) entry Hentry) as Hafter;
destruct (E.Host.call msg entry) as [result after]
end.
split; [exact (persistent_code_map _ _ _ Hafter)|exact (persistent_code_value _ _ _ Hafter)].
Qed.
Lemma transactions_preserve_system_code txs state post :
worldCode query code (B.t.world_state state) →
Result.List.fold_leftM E.process_transaction state txs = inl post →
worldCode query code (B.t.world_state post).
Proof.
revert state. induction txs as [|tx rest IH]; intros state Hbefore Hrun.
- injection Hrun as <-. exact Hbefore.
- change (Result.op_letdollar (E.process_transaction state tx)
(fun middle ⇒ Result.List.fold_leftM E.process_transaction middle rest) = inl post) in Hrun.
destruct (E.process_transaction state tx) as [middle|error] eqn:Htx; [|discriminate].
assert (Hvalid : ∃ validation, E.validate_transaction state tx = inl validation).
{ unfold E.process_transaction in Htx. destruct (E.validate_transaction state tx);
[eexists; reflexivity|discriminate]. }
destruct Hvalid as [validation Hvalidation].
apply (IH middle).
+ eapply transaction_preserves_system_code;
[exact Hrevision|exact Hcold|exact Hwarm|exact Hsystem|exact Hregular|
exact Hnonempty|exact Hvalidation|exact Htx|exact Hbefore].
+ exact Hrun.
Qed.
Lemma withdrawals_preserve_code withdrawals block :
worldCode query code (B.t.world_state block) →
worldCode query code (B.t.world_state (Result.List.fold_left E.process_withdrawal block withdrawals)).
Proof.
revert block. induction withdrawals as [|wd rest IH]; intros block Hbefore; [exact Hbefore|].
change (worldCode query code (B.t.world_state (Result.List.fold_left E.process_withdrawal
(E.process_withdrawal block wd) rest))).
apply IH. unfold E.process_withdrawal. apply world_code_write with
(world := B.t.world_state block) (keep := false) (addr := Chain.Ethereum.Withdrawal.t.recipient wd);
[exact Hbefore|]. intro Heq; rewrite Heq. exact (proj2 Hbefore).
Qed.
Theorem block_preserves_system_account verify world block post :
worldCode query code world → E.process_block verify world block = inl post →
worldCode query code post.
Proof.
intros [Hmap Hcode] Hrun.
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 [B.make ?w block] ⇒ set (initial := B.make w block) in × end.
assert (Hinitial : worldCode query code (B.t.world_state initial)).
{ split; [apply account_write_wellformed; exact Hmap|].
exact (eq_trans (activation_preserves_code world query Hmap) Hcode). }
match type of Hrun with context [E.process_system_message initial ?addr ?data] ⇒
pose proof (system_message_preserves_code initial addr data Hinitial) as Hbeacon;
destruct (E.process_system_message initial addr data) as [beaconResult afterBeacon]
end.
cbn beta iota zeta delta [snd] in Hbeacon, Hrun.
match type of Hrun with context [E.process_system_message afterBeacon ?addr ?data] ⇒
pose proof (system_message_preserves_code afterBeacon addr data Hbeacon) as Hhistory;
destruct (E.process_system_message afterBeacon addr data) as [historyResult afterHistory]
end.
cbn beta iota zeta delta [snd] in Hhistory, Hrun.
destruct (Result.List.fold_leftM E.process_transaction afterHistory (Block.t.transactions block))
as [afterTransactions|error] eqn:Htxs; [|discriminate].
pose proof (transactions_preserve_system_code _ _ _ Hhistory Htxs) as Htransactions.
pose proof (withdrawals_preserve_code (Block.t.withdrawals block) afterTransactions 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 <-; exact Hwithdrawals].
match type of Hrun with context [match ?check with inl _ ⇒ _ | inr _ ⇒ _ end] ⇒
destruct check as [[]|error] end;
[injection Hrun as <-; exact Hwithdrawals|discriminate].
Qed.
End Execution.
Theorem block_preserves_system_code {F : E.FArgs} cold warm access write reserve verify world block post :
E.Params.(Execution.Make_Params_signature.revision) =
Support.Variants.Revision.Setcode (cold, warm, access, write, reserve) →
(0 < cold)%N → (0 < warm)%N → prescribedSystemCode world →
E.process_block verify world block = inl post → prescribedSystemCode post.
Proof.
intros Hrevision Hcold Hwarm [Hbeacon Hhistory] Hrun. split.
- eapply block_preserves_system_account;
[exact Hrevision|exact Hcold|exact Hwarm| | | |exact Hbeacon|exact Hrun].
+ vm_compute. reflexivity.
+ vm_compute. discriminate.
+ vm_compute. reflexivity.
- eapply block_preserves_system_account;
[exact Hrevision|exact Hcold|exact Hwarm| | | |exact Hhistory|exact Hrun].
+ vm_compute. reflexivity.
+ vm_compute. discriminate.
+ vm_compute. reflexivity.
Qed.
Print Assumptions block_preserves_system_code.