Library SetcodeConcrete.blockconfig

Reserve configuration through complete SETCODE blocks

Activation, both system messages, every transaction and withdrawals preserve native storage except for successful delayed setter calls. This proof includes empty blocks; storage is not assumed unchanged by system execution.
From Stdlib Require Import List String ZArith Lia.
Require Import RocqOfOCaml.RocqOfOCaml MonadVM.Execution.
Require Import QedkConcrete.protectedstorageboundarycore QedkConcrete.protectedstorage
  QedkConcrete.protectedstoragenative QedkConcrete.reservestorage
  QedkConcrete.worldaccounts QedkConcrete.blockentry QedkConcrete.hostframe.
Require Import SetcodeConcrete.transactionconfig SetcodeConcrete.executionconfig
  SetcodeConcrete.transactionentry SetcodeConcrete.blocknonceagreement
  SetcodeConcrete.supplysequence SetcodeConcrete.codeframe.
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 Block := Chain.Ethereum.Block.
Local Abbreviation native := Reserve_balance.precompile_address.
Local Abbreviation readAccount := (fun world a ⇒ State.op_pointcaretlparenrparen world (W.account None a)).
Local Abbreviation number := (fun state ⇒ Block.Header.t.number (Block.t.header (B.t.current_block state))).
Definition worldConfig block initial := worldProtectedStorage native
  (fun storage ⇒ ∃ updates, reserveStorageWrites block initial updates storage).
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).

Lemma system_message_config state addr data initial :
  worldConfig (number state) initial (B.t.world_state state) →
  worldConfig (number state) initial (B.t.world_state (snd (E.process_system_message state addr data))).
Proof using F Hcold Hrevision Hwarm access cold reserve warm write.
  intro Hbefore. unfold E.process_system_message.
  match goal with |- context [if ?test then _ else _] ⇒ destruct test end; [exact Hbefore|].
  cbn beta iota zeta.
  match goal with |- context [E.Host.call ?message ?entry] ⇒
    set (msg := message); set (initialState := entry) end.
  assert (Hentry : configStorage (number state) initial initialState).
  { destruct Hbefore as [Hmap [Hnonce [Hcode Hwrites]]]. constructor;
      try exact Hmap; try exact Hnonce; try exact Hcode.
    - split; [reflexivity|exact Hwrites].
    - intro Hin; inversion Hin.
    - intro Hin; inversion Hin. }
  assert (Hgas : (0 ≤ Evmc.Message.t.gas msg < 2^63)%Z)
    by (change (0 ≤ 30000000 < 2^63)%Z; lia).
  assert (Hsender : Evmc.Message.t.sender msg ≠ native) by (vm_compute; discriminate).
  pose proof (system_call_only_delayed_settings cold warm access write reserve Hrevision Hcold Hwarm
    (number state) initial msg Hgas Hsender eq_refl (fun H ⇒ H) initialState Hentry) as Hcall.
  destruct (E.Host.call msg initialState) as [result afterVm].
  cbn [snd] in Hcall.
  destruct (protected_storage_world _ _ _ Hcall) as [Hmap [Hnonce [Hcode [_ Hwrites]]]].
  repeat split; assumption.
Qed.

Lemma block_entry_config world block :
  stringmaps.WellFormed (W.t.accounts world) →
  A.t.code (readAccount world native) = Byte_string.Bytes.empty →
  worldConfig (Block.Header.t.number (Block.t.header block))
    (A.t.storage (readAccount world native))
    (B.t.world_state (blockTransactionEntry world block)).
Proof using F Hcold Hrevision Hwarm access cold reserve warm write.
  intros Hmap Hcode.
  set (initial := B.make (Reserve_balance.activate_precompile world) block).
  assert (Hinitial : worldConfig (number initial) (A.t.storage (readAccount world native))
    (B.t.world_state initial)).
  { pose proof (active_account world Hmap) as [Hstorage Hnonce].
    split; [apply account_write_wellformed; exact Hmap|].
    split; [exact Hnonce|]. split.
    - exact (eq_trans (activation_preserves_code world native Hmap) Hcode).
    - ∃ nil. change (reserveStorageWrites (number initial)
        (A.t.storage (readAccount world native)) nil
        (A.t.storage (readAccount (Reserve_balance.activate_precompile world) native))).
      change (A.t.storage (readAccount (Reserve_balance.activate_precompile world) native) =
        A.t.storage (readAccount world native)) in Hstorage.
      refine (eq_ind_r (fun storage ⇒ reserveStorageWrites (number initial)
        (A.t.storage (readAccount world native)) nil storage) _ Hstorage).
      constructor. }
  unfold blockTransactionEntry. cbn zeta. fold initial.
  match goal with |- context [E.process_system_message initial ?addr ?data] ⇒
    pose proof (system_message_config initial addr data _ Hinitial) as Hbeacon end.
  match goal with |- context [E.process_system_message ?beacon E.history_storage_address ?data] ⇒
    assert (Hready : worldConfig (number beacon) (A.t.storage (readAccount world native))
      (B.t.world_state beacon)) by
      (cbv beta; rewrite system_message_current_block; exact Hbeacon);
    pose proof (system_message_config beacon E.history_storage_address data _ Hready) as Hhistory end.
  cbv beta in Hhistory. rewrite !system_message_current_block in Hhistory. exact Hhistory.
Qed.

Lemma transactions_config state txs post initial :
  Forall (fun tx ⇒ signaturesAvoidAddress tx native) txs →
  worldConfig (number state) initial (B.t.world_state state) →
  Result.List.fold_leftM E.process_transaction state txs = inl post →
  worldConfig (number state) initial (B.t.world_state post).
Proof using F Hcold Hrevision Hwarm access cold reserve warm write.
  revert state. induction txs as [|tx rest IH]; intros state Hsignatures Hbefore Hrun.
  - injection Hrun as <-. exact Hbefore.
  - inversion Hsignatures as [|? ? Hsig Hrest]; subst.
    change (Result.op_letdollar (E.process_transaction state tx)
      (fun after ⇒ Result.List.fold_leftM E.process_transaction after rest) = inl post) in Hrun.
    destruct (E.process_transaction state tx) as [after|error] eqn:Hstep; [|discriminate].
    assert (Hv : ∃ v, E.validate_transaction state tx = inl v).
    { unfold E.process_transaction in Hstep. destruct (E.validate_transaction state tx);
        [eexists; reflexivity|discriminate]. }
    destruct Hv as [v Hv].
    pose proof (process_transaction_only_delayed_settings cold warm access write reserve
      Hrevision Hcold Hwarm state tx v after initial Hsig Hv Hstep Hbefore) as Hafter.
    pose proof (process_transaction_current_block state tx after Hstep) as Hcurrent.
    assert (Hready : worldConfig (number after) initial (B.t.world_state after))
      by (cbv beta; rewrite Hcurrent; exact Hafter).
    specialize (IH after Hrest Hready Hrun). cbv beta in IH.
    rewrite Hcurrent in IH. exact IH.
Qed.

Lemma withdrawals_config state withdrawals block initial :
  worldConfig block initial (B.t.world_state state) →
  worldConfig block initial (B.t.world_state
    (Result.List.fold_left E.process_withdrawal state withdrawals)).
Proof.
  revert state. induction withdrawals as [|wd rest IH]; intros state Hbefore; [exact Hbefore|].
  change (worldConfig block initial (B.t.world_state
    (Result.List.fold_left E.process_withdrawal (E.process_withdrawal state wd) rest))).
  apply IH. unfold E.process_withdrawal. apply world_protected_storage_balance. exact Hbefore.
Qed.

Theorem block_config verify world block post :
  stringmaps.WellFormed (W.t.accounts world) →
  A.t.code (readAccount world native) = Byte_string.Bytes.empty →
  Forall (fun tx ⇒ signaturesAvoidAddress tx native) (Block.t.transactions block) →
  E.process_block verify world block = inl post →
  worldConfig (Block.Header.t.number (Block.t.header block))
    (A.t.storage (readAccount world native)) post.
Proof using F Hcold Hrevision Hwarm access cold reserve warm write.
  intros Hmap Hcode Hsignatures Hrun.
  pose proof (block_entry_config world block Hmap Hcode) as Hentry.
  unfold E.process_block in Hrun. rewrite Hrevision in Hrun.
  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) in Hrun.
  destruct (E.validate_block world block) as [[]|error]; [|discriminate].
  cbn beta iota zeta in Hrun.
  match type of Hrun with context [Result.List.fold_leftM ?step ?entry ?txs] ⇒
    change entry with (blockTransactionEntry world block) in Hrun end.
  destruct (Result.List.fold_leftM E.process_transaction (blockTransactionEntry world block)
    (Block.t.transactions block)) as [after|error] eqn:Htxs; [|discriminate].
  pose proof (transactions_config (blockTransactionEntry world block) (Block.t.transactions block)
    after _ Hsignatures ltac:(cbv beta; rewrite block_entry_current; exact Hentry) Htxs) as Htransactions.
  cbv beta in Htransactions. rewrite block_entry_current in Htransactions.
  pose proof (withdrawals_config after (Block.t.withdrawals block) _ _ Htransactions) as Hwithdrawals.
  cbn beta iota zeta in Hrun.
  match type of Hrun with context [E.validate_block ?w ?b] ⇒ destruct (E.validate_block w b) as [[]|error] end;
    [|discriminate].
  destruct verify; [|injection Hrun as <-; exact Hwithdrawals].
  match type of Hrun with context [match ?check with inl _ ⇒ _ | inr _ ⇒ _ end] ⇒
    destruct check as [[]|error] end;
    [injection Hrun as <-; exact Hwithdrawals|discriminate].
Qed.
End Execution.
Print Assumptions block_config.