Library SetcodeConcrete.codeevolution

Adoption cannot restore EOA code

During VM execution, an account not created in this transaction either keeps its original code or has regular code. Consequently a migrated account cannot become an EOA again through VM execution or rollback of a later call. Rollback of the adopting call itself restores its entry state, as usual.
From Stdlib Require Import List String ZArith Bool.
Require Import RocqOfOCaml.RocqOfOCaml.
Require Import SetcodeConcrete.executionaccountframe SetcodeConcrete.accountframe
  SetcodeConcrete.transactioncode QedkConcrete.vmhostframe.
Set Default Goal Selector "!".
#[local] Hint Extern 100 (Basics.Unreachable _) ⇒
  (apply Opaque.legacy_witness; Opaque.opaque_inhabit 64%nat) : typeclass_instances.

Lemma regular_source_is_regular code :
  Setcode_rules.regular_source code = true → regularCode code.
Proof.
  destruct code as [|head tail]; [discriminate|].
  unfold Setcode_rules.regular_source. rewrite Bool.andb_true_iff.
  intros [_ Hhead].
  change (negb (Ascii.eqb head "239"%char) = true) in Hhead.
  apply Bool.negb_true_iff in Hhead.
  unfold regularCode, Delegation.is_valid_delegation.
  change ((Byte_string.Bytes.length (String head tail) =? 23)%Z &&
    OCamlString.starts_with_chars Delegation.eoa_delegation_prefix (String head tail) = false).
  cbn [OCamlString.starts_with_chars Delegation.eoa_delegation_prefix].
  rewrite Ascii.eqb_sym, Hhead. simpl. apply Bool.andb_false_r.
Qed.

Lemma regular_code_contract account :
  regularCode (A.t.code account) ↔ A.is_smart_contract account = true.
Proof.
  unfold regularCode, A.is_smart_contract, Byte_string.Bytes.op_ltgt.
  change (orb (Byte_string.Bytes.op_eq (A.t.code account) Byte_string.Bytes.empty)
      (Delegation.is_valid_delegation (A.t.code account)) = false ↔
    andb (negb (Byte_string.Bytes.op_eq (A.t.code account) Byte_string.Bytes.empty))
      (negb (Delegation.is_valid_delegation (A.t.code account))) = true).
  rewrite Bool.orb_false_iff, Bool.andb_true_iff, !Bool.negb_true_iff. reflexivity.
Qed.

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).

Theorem transaction_call_code_evolution query tx msg initial :
  query ≠ Reserve_balance.precompile_address →
  stringmaps.WellFormed (W.t.accounts (S.t.world_state initial)) →
  (0 ≤ Evmc.Message.t.gas msg < 2^63)%Z →
  let post := snd (E.Host.call_from_eoa tx msg initial) in
  ¬ In query (S.t.accounts_created_in_current_transaction post) →
  let before := State.op_pointcaretlparenrparen (S.t.world_state initial) (W.account None query) in
  let after := State.op_pointcaretlparenrparen (S.t.world_state post) (W.account None query) in
  regularCode (A.t.code after) ∨ A.t.code after = A.t.code before.
Proof.
  intros Hdifferent Hmap Hgas post Hnot before after.
  set (P := fun code (_ : Numeric.U64.t) ⇒ regularCode code ∨ code = A.t.code before).
  pose proof (@execution_call_from_eoa_preserves_account_frame F query P Hdifferent
    (fun code nonce Hcode ⇒ or_introl (regular_source_is_regular code Hcode))
    (fun code nonce HP ⇒ HP) cold warm access write reserve Hcold Hwarm
    tx msg Hrevision Hgas initial (conj Hmap (or_intror (or_intror eq_refl)))) as Hafter.
  destruct Hafter as [_ [Hin|Hcode]]; [contradiction|exact Hcode].
Qed.

Corollary transaction_call_preserves_regular_sender query tx msg initial :
  query ≠ Reserve_balance.precompile_address →
  stringmaps.WellFormed (W.t.accounts (S.t.world_state initial)) →
  (0 ≤ Evmc.Message.t.gas msg < 2^63)%Z →
  A.is_smart_contract (State.op_pointcaretlparenrparen (S.t.world_state initial)
    (W.account None query)) = true →
  let post := snd (E.Host.call_from_eoa tx msg initial) in
  ¬ In query (S.t.accounts_created_in_current_transaction post) →
  A.is_smart_contract (State.op_pointcaretlparenrparen (S.t.world_state post)
    (W.account None query)) = true.
Proof.
  intros Hdifferent Hmap Hgas Hregular post Hnot.
  apply regular_code_contract.
  destruct (transaction_call_code_evolution query tx msg initial
    Hdifferent Hmap Hgas Hnot) as [Hcode|Hsame]; [exact Hcode|].
  apply regular_code_contract in Hregular.
  exact (eq_rect _ regularCode Hregular _ (eq_sym Hsame)).
Qed.
End Execution.
Print Assumptions transaction_call_code_evolution.