Library QedkConcrete.uncreatednoncecreate

CREATE records its nonce-changing target before executing initcode

This is the recursive Host induction step. It retains an explicit created-address exception instead of assuming CREATE avoids signers.
The recursive invocation must preserve the same relation; this is an induction step, not a new VM contract. CREATE records its target before running initcode, and cannot install code outside that recorded set.
Local Abbreviation floor := (@uncreatedNonce).

Lemma process_create_preserves_uncreated_nonce query nonce msg :
  (∀ nested code, 0 ≤ Evmc.Message.t.gas nested < 2^63 →
    preserves ((@H.Vm F).(Evmc.Vm.SIG.execute) nested code)) →
  (∀ nested code, 0 ≤ Evmc.Message.t.gas nested < 2^63 →
    ∀ s, floor query nonce s →
    (Evmc.Message.t.recipient nested = query →
      code = Byte_string.Bytes.empty ∨ List.In query (S.t.accounts_created_in_current_transaction s)) →
    floor query nonce (snd ((@H.Vm F).(Evmc.Vm.SIG.execute) nested code s))) →
  0 ≤ Evmc.Message.t.gas msg < 2^63 →
  preservesCallback (floor query nonce)
    (@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 uncreatedNonce].
  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 : floor query nonce marked).
  { apply mark_creation_uncreated_nonce. exact Hinitial. }
  assert (Hstored : floor query nonce stored).
  { unfold stored. eapply account_write_uncreated_nonce with (s := marked) (keep := false) (addr := target);
      [exact Hmarked|right; split; reflexivity]. }
  assert (Hprepared : floor query nonce prepared).
  { unfold prepared. eapply account_write_uncreated_nonce with (s := stored) (keep := false) (addr := target);
      [exact Hstored|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_preserves_uncreated_nonce UF F query nonce (Evmc.Message.t.sender msg)
    target (Evmc.Message.t.value msg) prepared Hprepared) as Htransfer.
  pose proof (@transfer_preserves_creation UF 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 uncreatedNonce].
  destruct ok.
  2: { cbn -[W.account Addr.of_contract_creation target stored prepared marked uncreatedNonce].
    exact Htransfer. }
  cbn -[W.account Addr.of_contract_creation target stored prepared marked uncreatedNonce].
  match goal with |- context [(@H.Vm F).(Evmc.Vm.SIG.execute) ?nested ?code middle] ⇒
    assert (Hcode : Evmc.Message.t.recipient nested = query →
      code = Byte_string.Bytes.empty ∨ List.In query (S.t.accounts_created_in_current_transaction middle)) by
      (intro Heq; right; 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 middle HmiddleWf) 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.
  destruct HrecursiveFrame as [HafterWf HafterFrame].
  assert (HtargetAfter : List.In target (S.t.accounts_created_in_current_transaction afterVm)).
  { apply (creations_retained _ _ HafterFrame). exact HtargetMiddle. }
  destruct result as [status gasleft refund output address]. destruct status;
    cbn -[W.account Addr.of_contract_creation target stored prepared marked uncreatedNonce];
    try exact Hrecursive.
  match goal with |- context [match ?failure with Some _ ⇒ _ | None ⇒ _ end] ⇒
    destruct failure as [error|] end.
  - exact Hrecursive.
  - eapply account_write_uncreated_nonce with (s := afterVm) (keep := false) (addr := target);
      [exact Hrecursive|left; exact HtargetAfter].
Qed.
End Creation.