Library SetcodeConcrete.accountcreate

CREATE preserves account properties outside its recorded targets

From Stdlib Require Import List String ZArith.
Require Import RocqOfOCaml.RocqOfOCaml.
Require Import SetcodeConcrete.accountframe SetcodeConcrete.creationtracking
  QedkConcrete.hostcode QedkConcrete.hostframe QedkConcrete.worldaccounts
  QedkConcrete.vmhostframe.
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.
Section Creation.
Context {F : H.FArgs}.
Lemma process_create_account_frame query P msg :
  (∀ nested code, 0 ≤ Evmc.Message.t.gas nested < 2^63 →
    ∀ initial, preservesCallback (creationsRetained initial)
      (H.Vm.(Evmc.Vm.SIG.execute) nested code)) →
  (∀ nested code, 0 ≤ Evmc.Message.t.gas nested < 2^63 →
    ∀ s, accountFrame query P s →
    (Evmc.Message.t.recipient nested = query →
      List.In query (S.t.accounts_created_in_current_transaction s)) →
    accountFrame query P (snd ((@H.Vm F).(Evmc.Vm.SIG.execute) nested code s))) →
  0 ≤ Evmc.Message.t.gas msg < 2^63 →
  preservesCallback (accountFrame query P)
    (@H.process_create F msg).
Proof.
  intros Hcreation Hvm Hgas s Hinitial.
  unfold H.process_create, H.contract_creation_address, H.M.
  cbn -[Addr.of_contract_creation W.account Numeric.U64.op_ltgt
    Byte_string.Bytes.op_ltgt H.transfer_ether accountFrame].
  match goal with |- context [if ?test then _ else _] ⇒ destruct test eqn:Hcollision end.
  { exact Hinitial. }
  match goal with |- context [H.transfer_ether _ ?addr _ _] ⇒ set (target := addr) in × end.
  match goal with |- context [H.transfer_ether _ _ _
    (S.t.with_world_state ?nonceWorld (S.t.with_world_state ?storageWorld ?created))] ⇒
    set (marked := created) in *;
    set (stored := S.t.with_world_state storageWorld marked) in *;
    set (prepared := S.t.with_world_state nonceWorld stored) in ×
  end.
  assert (Hmarked : accountFrame query P marked).
  { apply account_frame_mark_creation. exact Hinitial. }
  assert (Hstored : accountFrame query P stored).
  { unfold stored. eapply account_frame_write with (s := marked) (keep := false) (addr := target);
      [exact Hmarked|intro Heq; subst query; left; apply address_set_add_members; left; reflexivity]. }
  assert (Hprepared : accountFrame query P prepared).
  { unfold prepared. eapply account_frame_write with (s := stored) (keep := false) (addr := target);
      [exact Hstored|intro Heq; subst query; left].
    change (List.In target (Addr._Set.(OCamlSet.S.add) target
      (S.t.accounts_created_in_current_transaction s))).
    apply address_set_add_members. left; reflexivity. }
  assert (HtargetPrepared : List.In target (S.t.accounts_created_in_current_transaction prepared)).
  { change (List.In target (Addr._Set.(OCamlSet.S.add) target
      (S.t.accounts_created_in_current_transaction s))).
    apply address_set_add_members. left. reflexivity. }
  pose proof (@transfer_account_frame F query P (Evmc.Message.t.sender msg)
    target (Evmc.Message.t.value msg) prepared Hprepared) as Htransfer.
  pose proof (@transfer_preserves_creation _ F (Evmc.Message.t.sender msg)
    target (Evmc.Message.t.value msg) prepared (proj1 Hprepared)) as HtransferFrame.
  destruct (@H.transfer_ether F (Evmc.Message.t.sender msg)
    target (Evmc.Message.t.value msg) prepared) as [ok middle] eqn:HtransferRun.
  cbn beta iota zeta delta [fst snd] in Htransfer, HtransferFrame.
  destruct HtransferFrame as [HmiddleWf HmiddleFrame].
  assert (HtargetMiddle : List.In target (S.t.accounts_created_in_current_transaction middle)).
  { apply (creations_retained _ _ HmiddleFrame). exact HtargetPrepared. }
  cbn -[W.account Addr.of_contract_creation target stored prepared marked accountFrame].
  destruct ok.
  2: { cbn -[W.account Addr.of_contract_creation target stored prepared marked accountFrame].
    exact Htransfer. }
  cbn -[W.account Addr.of_contract_creation target stored prepared marked accountFrame].
  match goal with |- context [(@H.Vm F).(Evmc.Vm.SIG.execute) ?nested ?code middle] ⇒
    assert (Hcode : Evmc.Message.t.recipient nested = query →
      List.In query (S.t.accounts_created_in_current_transaction middle)) by
      (intro Heq; change (target = query) in Heq; rewrite <- Heq; exact HtargetMiddle);
    pose proof (Hvm nested code Hgas middle Htransfer Hcode) as Hrecursive;
    pose proof (Hcreation nested code Hgas
      (S.t.accounts_created_in_current_transaction middle) middle (fun a H ⇒ H)) as HrecursiveFrame;
    destruct ((@H.Vm F).(Evmc.Vm.SIG.execute) nested code middle) as [result afterVm]
  end.
  cbn beta iota zeta delta [fst snd] in Hrecursive, HrecursiveFrame.
  assert (HtargetAfter : List.In target (S.t.accounts_created_in_current_transaction afterVm)).
  { apply HrecursiveFrame. exact HtargetMiddle. }
  destruct result as [status gasleft refund output address]. destruct status;
    cbn -[W.account Addr.of_contract_creation target stored prepared marked accountFrame];
    try exact Hrecursive.
  match goal with |- context [if ?adopted then _ else _] ⇒
    destruct adopted; [exact Hrecursive|] end.
  match goal with |- context [match ?failure with Some _ ⇒ _ | None ⇒ _ end] ⇒
    destruct failure as [error|] end.
  - exact Hrecursive.
  - eapply account_frame_write with (s := afterVm) (keep := false) (addr := target);
      [exact Hrecursive|intro Heq; subst query; left; exact HtargetAfter].
Qed.
End Creation.
Print Assumptions process_create_account_frame.