Library SetcodeConcrete.transactionaccountfinalize
Code and nonce through complete transaction finalization
From Stdlib Require Import List String ZArith.
Require Import RocqOfOCaml.RocqOfOCaml MonadVM.Execution.
Require Import QedkConcrete.accountcode QedkConcrete.accountnonces
QedkConcrete.transactionfinalize QedkConcrete.worldaccounts
QedkConcrete.transactionframes.
Require Import SetcodeConcrete.transactionentry SetcodeConcrete.contextframe.
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 S := State.TransactionState.
Module A := Chain.Ethereum.Account.
Module T := Chain.Ethereum.Transaction.
Module Addr := Chain.Ethereum.Address.
Definition worldAccount query (P : string → Numeric.U64.t → Prop) world :=
stringmaps.WellFormed (W.t.accounts world) ∧
P (A.t.code (State.op_pointcaretlparenrparen world (W.account None query)))
(A.t.nonce (State.op_pointcaretlparenrparen world (W.account None query))).
Lemma credit_world_account query P world who amount :
worldAccount query P world →
worldAccount query P (State.op_pointcaretlparenrparenltminus world (W.account (Some false) who)
(A.t.with_balance amount (State.op_pointcaretlparenrparen world (W.account None who)))).
Proof.
intros [Hmap HP]. split; [apply account_write_wellformed; exact Hmap|].
rewrite code_after_account_write, nonce_after_account_write by exact Hmap.
destruct (String.eqb who query) eqn:Heq; [apply String.eqb_eq in Heq; subst who|]; exact HP.
Qed.
Lemma delete_world_account query P block addresses :
worldAccount query P (B.t.world_state block) → ¬ In query addresses →
worldAccount query P (B.t.world_state (OCamlSeq.fold_left
(fun s addr ⇒ State.op_pointcaretlparenrparenltminus s (B.account_opt None addr) None)
block (OCamlSeq.of_list addresses))).
Proof.
intros [Hmap HP] Hnot.
destruct (delete_accounts_preserves_other block addresses query Hmap Hnot) as [Hwf Hread].
split; [exact Hwf|].
exact (eq_rect _ (fun a ⇒ P (A.t.code a) (A.t.nonce a)) HP _ (eq_sym Hread)).
Qed.
Section Transaction.
Context {F : E.FArgs}.
Lemma bump_world_account query P s addresses :
worldAccount query P (S.t.world_state s) →
worldAccount query P (S.t.world_state (E.bump_emptying_transaction_counters addresses s)).
Proof.
unfold worldAccount, State.op_pointcaretlparenrparen, W.account, W.account_opt.
cbn -[E.bump_emptying_transaction_counters Addr.Map.find_opt].
rewrite bump_counters_accounts. exact (fun H ⇒ H).
Qed.
Theorem process_transaction_retains_finished_account block tx validation post query P :
E.validate_transaction block tx = inl validation →
E.process_transaction block tx = inl post →
worldAccount query P (S.t.world_state (transactionFinished block tx validation)) →
¬ In query (S.t.self_destruct (transactionFinished block tx validation)) →
worldAccount query P (B.t.world_state post).
Proof.
intros Hvalidation Hrun HP Hnot.
unfold E.process_transaction in Hrun. rewrite Hvalidation in Hrun.
destruct validation as [sender fee price intrinsic].
change (@Result.op_letdollar) with
(fun (A Error C : Set) (first : A + Error) (next : A → C + Error) ⇒
match first with inl value ⇒ next value | inr error ⇒ inr error end) in Hrun.
cbn beta iota zeta in Hrun, HP, Hnot.
match type of Hrun with context [Setcode_rules.advance_sender_nonce ?flag _ _] ⇒
set (migrated := flag) in × end.
match type of Hrun with context [Result.List.fold_left ?step ?initial ?auths] ⇒
set (authorized := Result.List.fold_left step initial auths) in × end.
assert (Hprefix : transactionPrefix block tx
{| E.transaction_validation.sender := sender; E.transaction_validation.total_fee := fee;
E.transaction_validation.effective_gas_price := price;
E.transaction_validation.intrinsic_gas := intrinsic |} = authorized) by reflexivity.
unfold transactionFinished, transactionVmInput in HP, Hnot.
rewrite Hprefix in HP, Hnot.
unfold migratedSender in HP, Hnot.
destruct authorized as [applied prepared].
cbn beta iota zeta delta [fst snd] in Hrun, HP, Hnot.
match type of Hrun with context [S.initialize_access_sets tx ?entry ?precompiles] ⇒
set (beforeVm := S.initialize_access_sets tx entry precompiles) in × end.
match type of Hrun with context [E.Host.call_from_eoa tx ?message beforeVm] ⇒
set (msg := message) in × end.
change (worldAccount query P (S.t.world_state
(if migrated then beforeVm else snd (E.Host.call_from_eoa tx msg beforeVm)))) in HP.
change (¬ In query (S.t.self_destruct
(if migrated then beforeVm else snd (E.Host.call_from_eoa tx msg beforeVm)))) in Hnot.
destruct (E.Host.call_from_eoa tx msg beforeVm) as [result afterVm].
destruct migrated; cbn beta iota zeta delta [fst snd] in Hrun, HP, Hnot.
all: try (destruct (Evmc.Message.CallKind.is_create (Evmc.Message.t.kind msg));
cbn beta iota zeta in Hrun).
all: match type of Hrun with Result._return ?computed = inl ?target ⇒
assert (Hpost : computed = target) by
(exact (f_equal (fun r ⇒ match r with inl value ⇒ value | inr _ ⇒ target end) Hrun));
rewrite <- Hpost; clear Hpost Hrun end.
all: cbn beta iota zeta delta [B.t.with_transactions_processed B.t.with_transaction_frames
B.t.with_gas_used B.t.world_state].
all: match goal with |- context [E.bump_emptying_transaction_counters (?who :: nil) ?entry] ⇒
set (final := E.bump_emptying_transaction_counters (who :: nil) entry) in × end.
all: assert (Hfinal : worldAccount query P (S.t.world_state final)) by
(unfold final; apply bump_world_account; exact HP).
all: apply delete_world_account; [apply credit_world_account; exact Hfinal|exact Hnot].
Qed.
End Transaction.
Require Import RocqOfOCaml.RocqOfOCaml MonadVM.Execution.
Require Import QedkConcrete.accountcode QedkConcrete.accountnonces
QedkConcrete.transactionfinalize QedkConcrete.worldaccounts
QedkConcrete.transactionframes.
Require Import SetcodeConcrete.transactionentry SetcodeConcrete.contextframe.
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 S := State.TransactionState.
Module A := Chain.Ethereum.Account.
Module T := Chain.Ethereum.Transaction.
Module Addr := Chain.Ethereum.Address.
Definition worldAccount query (P : string → Numeric.U64.t → Prop) world :=
stringmaps.WellFormed (W.t.accounts world) ∧
P (A.t.code (State.op_pointcaretlparenrparen world (W.account None query)))
(A.t.nonce (State.op_pointcaretlparenrparen world (W.account None query))).
Lemma credit_world_account query P world who amount :
worldAccount query P world →
worldAccount query P (State.op_pointcaretlparenrparenltminus world (W.account (Some false) who)
(A.t.with_balance amount (State.op_pointcaretlparenrparen world (W.account None who)))).
Proof.
intros [Hmap HP]. split; [apply account_write_wellformed; exact Hmap|].
rewrite code_after_account_write, nonce_after_account_write by exact Hmap.
destruct (String.eqb who query) eqn:Heq; [apply String.eqb_eq in Heq; subst who|]; exact HP.
Qed.
Lemma delete_world_account query P block addresses :
worldAccount query P (B.t.world_state block) → ¬ In query addresses →
worldAccount query P (B.t.world_state (OCamlSeq.fold_left
(fun s addr ⇒ State.op_pointcaretlparenrparenltminus s (B.account_opt None addr) None)
block (OCamlSeq.of_list addresses))).
Proof.
intros [Hmap HP] Hnot.
destruct (delete_accounts_preserves_other block addresses query Hmap Hnot) as [Hwf Hread].
split; [exact Hwf|].
exact (eq_rect _ (fun a ⇒ P (A.t.code a) (A.t.nonce a)) HP _ (eq_sym Hread)).
Qed.
Section Transaction.
Context {F : E.FArgs}.
Lemma bump_world_account query P s addresses :
worldAccount query P (S.t.world_state s) →
worldAccount query P (S.t.world_state (E.bump_emptying_transaction_counters addresses s)).
Proof.
unfold worldAccount, State.op_pointcaretlparenrparen, W.account, W.account_opt.
cbn -[E.bump_emptying_transaction_counters Addr.Map.find_opt].
rewrite bump_counters_accounts. exact (fun H ⇒ H).
Qed.
Theorem process_transaction_retains_finished_account block tx validation post query P :
E.validate_transaction block tx = inl validation →
E.process_transaction block tx = inl post →
worldAccount query P (S.t.world_state (transactionFinished block tx validation)) →
¬ In query (S.t.self_destruct (transactionFinished block tx validation)) →
worldAccount query P (B.t.world_state post).
Proof.
intros Hvalidation Hrun HP Hnot.
unfold E.process_transaction in Hrun. rewrite Hvalidation in Hrun.
destruct validation as [sender fee price intrinsic].
change (@Result.op_letdollar) with
(fun (A Error C : Set) (first : A + Error) (next : A → C + Error) ⇒
match first with inl value ⇒ next value | inr error ⇒ inr error end) in Hrun.
cbn beta iota zeta in Hrun, HP, Hnot.
match type of Hrun with context [Setcode_rules.advance_sender_nonce ?flag _ _] ⇒
set (migrated := flag) in × end.
match type of Hrun with context [Result.List.fold_left ?step ?initial ?auths] ⇒
set (authorized := Result.List.fold_left step initial auths) in × end.
assert (Hprefix : transactionPrefix block tx
{| E.transaction_validation.sender := sender; E.transaction_validation.total_fee := fee;
E.transaction_validation.effective_gas_price := price;
E.transaction_validation.intrinsic_gas := intrinsic |} = authorized) by reflexivity.
unfold transactionFinished, transactionVmInput in HP, Hnot.
rewrite Hprefix in HP, Hnot.
unfold migratedSender in HP, Hnot.
destruct authorized as [applied prepared].
cbn beta iota zeta delta [fst snd] in Hrun, HP, Hnot.
match type of Hrun with context [S.initialize_access_sets tx ?entry ?precompiles] ⇒
set (beforeVm := S.initialize_access_sets tx entry precompiles) in × end.
match type of Hrun with context [E.Host.call_from_eoa tx ?message beforeVm] ⇒
set (msg := message) in × end.
change (worldAccount query P (S.t.world_state
(if migrated then beforeVm else snd (E.Host.call_from_eoa tx msg beforeVm)))) in HP.
change (¬ In query (S.t.self_destruct
(if migrated then beforeVm else snd (E.Host.call_from_eoa tx msg beforeVm)))) in Hnot.
destruct (E.Host.call_from_eoa tx msg beforeVm) as [result afterVm].
destruct migrated; cbn beta iota zeta delta [fst snd] in Hrun, HP, Hnot.
all: try (destruct (Evmc.Message.CallKind.is_create (Evmc.Message.t.kind msg));
cbn beta iota zeta in Hrun).
all: match type of Hrun with Result._return ?computed = inl ?target ⇒
assert (Hpost : computed = target) by
(exact (f_equal (fun r ⇒ match r with inl value ⇒ value | inr _ ⇒ target end) Hrun));
rewrite <- Hpost; clear Hpost Hrun end.
all: cbn beta iota zeta delta [B.t.with_transactions_processed B.t.with_transaction_frames
B.t.with_gas_used B.t.world_state].
all: match goal with |- context [E.bump_emptying_transaction_counters (?who :: nil) ?entry] ⇒
set (final := E.bump_emptying_transaction_counters (who :: nil) entry) in × end.
all: assert (Hfinal : worldAccount query P (S.t.world_state final)) by
(unfold final; apply bump_world_account; exact HP).
all: apply delete_world_account; [apply credit_world_account; exact Hfinal|exact Hnot].
Qed.
End Transaction.