Library QedkConcrete.blockpersistentcode

Prescribed system code persists across blocks

The approved condition is on the initial state, not every block boundary. Existing contracts survive authorization processing, contract creation and final deletion. System calls, reserve-precompile activation and withdrawal credits preserve their code too. This proof uses the real block executor.
From Stdlib Require Import List String ZArith Lia.
Require Import RocqOfOCaml.RocqOfOCaml.
Require Import QedkConcrete.transactionpersistentcode QedkConcrete.transactioncode
  QedkConcrete.executionpersistentcode QedkConcrete.persistentcode
  QedkConcrete.hostcode QedkConcrete.systemcode.
Set Default Goal Selector "!".
#[local] Hint Extern 100 (Basics.Unreachable _) ⇒
  (apply Opaque.legacy_witness; Opaque.opaque_inhabit 64%nat) : typeclass_instances.
Import ListNotations.
Local Open Scope Z_scope.
Module E := Execution.Make.
Module S := State.TransactionState.
Module B := State.BlockState.
Module W := State.WorldState.
Module A := Chain.Ethereum.Account.
Module T := Chain.Ethereum.Transaction.
Import transactionhistory transactionstage.

Section Execution.
Context {F : E.FArgs}.

Local Abbreviation args := (@E.Instantiation_fargs F).
Local Abbreviation params := args.(Host.Instantiate.ChainParams).

Context {UI0 : Basics.Unreachable Support.UintN.Impl.t}
  {UB : Basics.Unreachable Bloom.t}
  {U8 : Basics.Unreachable Byte_string.B8.(Byte_string.Fixed.Fixed_result.t)}.

Local Abbreviation prefix := (@transactionPrefix F).
Local Abbreviation vmInput := (@transactionVmInput F).
Local Abbreviation callFromEoa := (@E.Host.call_from_eoa F).
Local Abbreviation processTransaction := (@E.process_transaction F).
Local Abbreviation readAccount := (fun world addr ⇒ State.op_pointcaretlparenrparen
  world (@W.account None addr)).

Local Abbreviation hostCall := (@E.Host.call F).
Local Abbreviation systemMessage := (@E.process_system_message F).
Local Opaque Evmc.Instantiate._recursive_modules_Host_Vm.

Lemma system_message_existing_code block addr data query :
  (@E.Params F).(Execution.Make_Params_signature.revision) = Support.Variants.Revision.Eleven →
  stringmaps.WellFormed (W.t.accounts (B.t.world_state block)) →
  A.t.code (readAccount (B.t.world_state block) query) ≠ Byte_string.Bytes.empty →
  let post := snd (systemMessage block addr data) in
  stringmaps.WellFormed (W.t.accounts (B.t.world_state post)) ∧
  A.t.code (readAccount (B.t.world_state post) query) =
    A.t.code (readAccount (B.t.world_state block) query).
Proof.
  intros Hrevision Hmap Hnonempty. unfold E.process_system_message.
  match goal with |- context [if ?test then _ else _] ⇒ destruct test end.
  { split; [exact Hmap|reflexivity]. }
  cbn beta iota zeta.
  match goal with |- context [E.Host.call ?message ?entry] ⇒
    set (msg := message); set (initial := entry)
  end.
  assert (Hgas : 0 ≤ Evmc.Message.t.gas msg < 2^63).
  { change (0 ≤ 30000000 < 2^63). lia. }
  assert (Hentry : @persistentCode query
    (A.t.code (readAccount (B.t.world_state block) query)) initial).
  { constructor; [exact Hmap|reflexivity|cbn; tauto|cbn; tauto]. }
  pose proof (@execution_call_preserves_existing_code F
    query (A.t.code (readAccount (B.t.world_state block) query))
    msg Hnonempty Hrevision Hgas initial Hentry) as Hafter.
  destruct (hostCall msg initial) as [result afterVm].
  split; [exact (persistent_code_map _ _ _ Hafter)|exact (persistent_code_value _ _ _ Hafter)].
Qed.

Lemma withdrawal_code block withdrawal query :
  stringmaps.WellFormed (W.t.accounts (B.t.world_state block)) →
  let post := @E.process_withdrawal F block withdrawal in
  stringmaps.WellFormed (W.t.accounts (B.t.world_state post)) ∧
  A.t.code (readAccount (B.t.world_state post) query) =
    A.t.code (readAccount (B.t.world_state block) query).
Proof.
  intro Hmap. unfold E.process_withdrawal. split.
  - apply stringmaps.update_wellformed. exact Hmap.
  - apply balance_write_preserves_code. exact Hmap.
Qed.

Lemma withdrawals_code block withdrawals query :
  stringmaps.WellFormed (W.t.accounts (B.t.world_state block)) →
  let post := Result.List.fold_left (@E.process_withdrawal F) block withdrawals in
  stringmaps.WellFormed (W.t.accounts (B.t.world_state post)) ∧
  A.t.code (readAccount (B.t.world_state post) query) =
    A.t.code (readAccount (B.t.world_state block) query).
Proof.
  revert block. induction withdrawals as [|wd withdrawals IH]; intros block Hmap.
  - split; [exact Hmap|reflexivity].
  - change (let post := Result.List.fold_left (@E.process_withdrawal F)
      (@E.process_withdrawal F block wd) withdrawals in
      stringmaps.WellFormed (W.t.accounts (B.t.world_state post)) ∧
      A.t.code (readAccount (B.t.world_state post) query) =
        A.t.code (readAccount (B.t.world_state block) query)).
    destruct (withdrawal_code block wd query Hmap) as [HnextMap Hcode].
    destruct (IH (@E.process_withdrawal F block wd) HnextMap) as [HpostMap HpostCode].
    split; [exact HpostMap|etransitivity; eassumption].
Qed.

Context {UTries : Basics.Unreachable (OCamlIarray.t Mpt.Trie.t)}
  {UChar : Basics.Unreachable OCamlChar.t} {UWorld : Basics.Unreachable W.t}
  {UTrie : Basics.Unreachable Mpt.Trie.t} {UNibbles : Basics.Unreachable Mpt.Nibbles.t}
  {UBlock : Basics.Unreachable Chain.Ethereum.Block.t}.
Local Abbreviation processBlock := (@E.process_block F).

No assumption about later system code is necessary: any existing non-delegated contract retains its code through the complete block. This includes the two prescribed system contracts.
Theorem process_block_contract_code verify world block query :
  (@E.Params F).(Execution.Make_Params_signature.revision) = Support.Variants.Revision.Eleven →
  stringmaps.WellFormed (W.t.accounts world) →
  @A.is_smart_contract (readAccount world query) = true →
  match processBlock verify world block with
  | inl post ⇒ stringmaps.WellFormed (W.t.accounts post) ∧
      A.t.code (readAccount post query) = A.t.code (readAccount world query)
  | inr _ ⇒ True
  end.
Proof.
  intros Hrevision Hmap Hsc.
  assert (Hnonempty : A.t.code (readAccount world query) ≠ Byte_string.Bytes.empty).
  { unfold A.is_smart_contract in Hsc. apply Bool.andb_true_iff in Hsc as [Hpresent _].
    intro Heq. cbv beta in Heq, Hpresent.
    match type of Heq with ?code = _ ⇒
      change (Byte_string.Bytes.op_ltgt code Byte_string.Bytes.empty = true) in Hpresent
    end.
    rewrite Heq in Hpresent. discriminate. }
  unfold E.process_block. rewrite Hrevision.
  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).
  destruct (@E.validate_block F world block) as [[]|error]; [|exact I].
  cbn beta iota zeta.
  match goal with |- context [B.make ?w block] ⇒ set (initial := B.make w block) end.
  assert (HinitialMap : stringmaps.WellFormed (W.t.accounts (B.t.world_state initial))).
  { apply worldaccounts.account_write_wellformed. exact Hmap. }
  assert (HinitialCode : A.t.code (readAccount (B.t.world_state initial) query) =
    A.t.code (readAccount world query)).
  { eapply account_write_same_code; [exact Hmap|reflexivity]. }
  match goal with |- context [E.process_system_message initial ?addr ?data] ⇒
    pose proof (system_message_existing_code initial addr data query Hrevision HinitialMap
      ltac:(intro Hempty; apply Hnonempty; exact (eq_trans (eq_sym HinitialCode) Hempty))) as Hbeacon;
    destruct (E.process_system_message initial addr data) as [beaconResult afterBeacon]
  end.
  cbn beta iota zeta delta [snd] in Hbeacon |- ×.
  destruct Hbeacon as [HbeaconMap HbeaconCode].
  assert (HbeaconWorldCode : A.t.code (readAccount (B.t.world_state afterBeacon) query) =
    A.t.code (readAccount world query)) by (etransitivity; eassumption).
  match goal with |- context [E.process_system_message afterBeacon ?addr ?data] ⇒
    pose proof (system_message_existing_code afterBeacon addr data query Hrevision HbeaconMap
      ltac:(intro Hempty; apply Hnonempty; exact (eq_trans (eq_sym HbeaconWorldCode) Hempty))) as Hhistory;
    destruct (E.process_system_message afterBeacon addr data) as [historyResult afterHistory]
  end.
  cbn beta iota zeta delta [snd] in Hhistory |- ×.
  destruct Hhistory as [HhistoryMap HhistoryCode].
  assert (HhistoryWorldCode : A.t.code (readAccount (B.t.world_state afterHistory) query) =
    A.t.code (readAccount world query)) by (etransitivity; eassumption).
  assert (HhistorySc : @A.is_smart_contract (readAccount (B.t.world_state afterHistory) query) = true).
  { etransitivity; [apply (@executionreserve.contract_test_same_code); exact HhistoryWorldCode|exact Hsc]. }
  match goal with |- context [Result.List.fold_leftM ?step afterHistory ?txs] ⇒
    destruct (Result.List.fold_leftM step afterHistory txs) as [afterTransactions|error] eqn:Htxs
  end.
  2: exact I.
  destruct (@dispatched_transactions_contract_code F
    query (Chain.Ethereum.Block.t.transactions block)
    Hrevision afterHistory afterTransactions HhistoryMap HhistorySc Htxs) as [HtxMap HtxCode].
  destruct (withdrawals_code afterTransactions (Chain.Ethereum.Block.t.withdrawals block)
    query HtxMap) as [HwithdrawMap HwithdrawCode].
  assert (HfinalCode : A.t.code (readAccount (B.t.world_state (Result.List.fold_left
      (@E.process_withdrawal F) afterTransactions (Chain.Ethereum.Block.t.withdrawals block))) query) =
    A.t.code (readAccount world query)).
  { etransitivity; [exact HwithdrawCode|]. etransitivity; [exact HtxCode|exact HhistoryWorldCode]. }
  cbn beta iota zeta.
  match goal with |- context [E.validate_block ?w ?b] ⇒
    destruct (E.validate_block w b) as [[]|error]
  end.
  2: exact I.
  match goal with |- context [if verify then ?check else _] ⇒
    destruct verify; [destruct check as [[]|error]; [|exact I]|]
  end.
  all: split; [exact HwithdrawMap|exact HfinalCode].
Qed.

The initial-code premise approved for the fee-solvency theorem. The preservation theorem below carries it forward; it is not a per-block assumption about VM behavior.
Definition prescribedSystemCode (world : W.t) : Prop :=
  A.t.code (readAccount world (@E.beacon_roots_address F)) = beaconRootsCode ∧
  A.t.code (readAccount world (@E.history_storage_address F)) = historyStorageCode.

Lemma prescribed_code_is_contract account :
  A.t.code account = beaconRootsCode ∨ A.t.code account = historyStorageCode →
  @A.is_smart_contract account = true.
Proof.
  intro Hcode.
  change (andb (Byte_string.Bytes.op_ltgt (A.t.code account) Byte_string.Bytes.empty)
    (negb (@Delegation.is_valid_delegation (A.t.code account))) = true).
  destruct Hcode as [Hcode|Hcode]; rewrite Hcode; vm_compute; reflexivity.
Qed.

Corollary process_block_preserves_system_code verify world block :
  (@E.Params F).(Execution.Make_Params_signature.revision) = Support.Variants.Revision.Eleven →
  stringmaps.WellFormed (W.t.accounts world) → prescribedSystemCode world →
  match processBlock verify world block with
  | inl post ⇒ stringmaps.WellFormed (W.t.accounts post) ∧ prescribedSystemCode post
  | inr _ ⇒ True
  end.
Proof.
  intros Hrevision Hmap [Hbeacon Hhistory].
  pose proof (process_block_contract_code verify world block (@E.beacon_roots_address F)
    Hrevision Hmap (prescribed_code_is_contract _ (or_introl Hbeacon))) as HbeaconPost.
  pose proof (process_block_contract_code verify world block (@E.history_storage_address F)
    Hrevision Hmap (prescribed_code_is_contract _ (or_intror Hhistory))) as HhistoryPost.
  destruct (processBlock verify world block) as [post|error]; [|exact I].
  destruct HbeaconPost as [HpostMap HbeaconCode]. destruct HhistoryPost as [_ HhistoryCode].
  split; [exact HpostMap|]. split; etransitivity; eassumption.
Qed.

Theorem blocks_preserve_system_code verify blocks :
  (@E.Params F).(Execution.Make_Params_signature.revision) = Support.Variants.Revision.Eleven →
  ∀ world, stringmaps.WellFormed (W.t.accounts world) → prescribedSystemCode world →
  match Result.List.fold_leftM (processBlock verify) world blocks with
  | inl post ⇒ stringmaps.WellFormed (W.t.accounts post) ∧ prescribedSystemCode post
  | inr _ ⇒ True
  end.
Proof.
  intro Hrevision. induction blocks as [|block blocks IH]; intros world Hmap Hcode.
  - split; assumption.
  - change (match Result.op_letdollar (processBlock verify world block)
      (fun next ⇒ Result.List.fold_leftM (processBlock verify) next blocks) with
      | inl post ⇒ stringmaps.WellFormed (W.t.accounts post) ∧ prescribedSystemCode post
      | inr _ ⇒ True end).
    pose proof (process_block_preserves_system_code verify world block Hrevision Hmap Hcode) as Hstep.
    destruct (processBlock verify world block) as [next|error]; [|exact I].
    apply IH; tauto.
Qed.
End Execution.
Print Assumptions process_block_contract_code.
Print Assumptions blocks_preserve_system_code.