Library SetcodeConcrete.transactionconfig

Configuration changes through complete SETCODE transactions

The fee/authorization prefix and beneficiary/deletion suffix preserve native storage. The actual VM contributes only successful delayed settings. Signatures exclude the native precompile address, not arbitrary authorities; no execution-state storage property is assumed.
From Stdlib Require Import List String ZArith.
Require Import RocqOfOCaml.RocqOfOCaml MonadVM.Execution.
Require Import QedkConcrete.protectedstorageboundarycore QedkConcrete.protectedstorage
  QedkConcrete.protectedstoragenative QedkConcrete.transactionframes
  QedkConcrete.reserveencoding QedkConcrete.validation.
Require Import SetcodeConcrete.transactionentry SetcodeConcrete.transactionconfigfinalize
  SetcodeConcrete.executionconfig SetcodeConcrete.executionconfigstorage.
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 T := Chain.Ethereum.Transaction.
Module A := Chain.Ethereum.Account.
Module Block := Chain.Ethereum.Block.
Local Abbreviation number := (fun state ⇒ Block.Header.t.number (Block.t.header (B.t.current_block state))).
Local Abbreviation readAccount := (fun world a ⇒ State.op_pointcaretlparenrparen world (W.account None a)).
Section Execution.
Context {F : E.FArgs}.

Lemma transaction_entry_config state tx validation query P :
  signaturesAvoidAddress tx query → E.validate_transaction state tx = inl validation →
  worldProtectedStorage query (P (number state)) (B.t.world_state state) →
  protectedStorage query P (snd (transactionVmInput state tx validation)).
Proof.
  intros [Hsender Hauths] Hv Hbefore.
  assert (Hdifferent : E.transaction_validation.sender validation ≠ query).
  { intro Heq. apply Hsender. rewrite (validated_sender_recovered state tx validation Hv), Heq. reflexivity. }
  unfold transactionVmInput, transactionPrefix.
  set (initial := S.t.with_tx_chain_id E.Params.(Execution.Make_Params_signature.chain_id)
    (S.make state (E.transaction_validation.sender validation) tx)).
  assert (Hinitial : protectedStorage query P initial).
  { destruct Hbefore as [Hmap [Hnonce [Hcode HP]]]. constructor;
      try exact Hmap; try exact Hnonce; try exact Hcode; try exact HP; intro Hin; inversion Hin. }
  match goal with |- context [Result.List.fold_left ?step (nil, ?start) ?auths] ⇒
    set (charged := start) in × end.
  assert (Hcharged : protectedStorage query P charged).
  { unfold charged. eapply protected_storage_write with (s := initial) (keep := false)
      (addr := E.transaction_validation.sender validation);
      [exact Hinitial|intro Heq; contradiction]. }
  pose proof (authorizations_preserve_protected_storage query P (T.authorization_list tx) nil charged Hcharged) as Hprefix.
  pose proof (authorizations_recovery_excludes query (T.authorization_list tx) nil charged Hauths
    ltac:(intros [])) as Hnoauth.
  match goal with |- context [Result.List.fold_left ?step (nil, charged) ?auths] ⇒
    destruct (Result.List.fold_left step (nil, charged) auths) as [applied authorized] eqn:Hfold end.
  cbn beta iota zeta delta [fst snd] in Hprefix, Hnoauth |- ×.
  specialize (Hprefix Hnoauth).
  match type of Hfold with ?result = _ ⇒
    change (protectedStorage query P (snd result)) in Hprefix;
    rewrite Hfold in Hprefix end.
  match goal with |- protectedStorage _ _ (S.initialize_access_sets _ ?entry _) ⇒
    eapply protected_storage_same_fields with (before := entry) end;
    [reflexivity|reflexivity|reflexivity|reflexivity|].
  apply bump_counters_protected_storage. exact Hprefix.
Qed.

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 process_transaction_only_delayed_settings state tx validation post initial :
  signaturesAvoidAddress tx Reserve_balance.precompile_address →
  E.validate_transaction state tx = inl validation →
  E.process_transaction state tx = inl post →
  worldProtectedStorage Reserve_balance.precompile_address
    (fun storage ⇒ ∃ updates, reserveStorageWrites (number state) initial updates storage)
    (B.t.world_state state) →
  worldProtectedStorage Reserve_balance.precompile_address
    (fun storage ⇒ ∃ updates, reserveStorageWrites (number state) initial updates storage)
    (B.t.world_state post).
Proof using F Hcold Hrevision Hwarm access cold reserve warm write.
  intros Hsignatures Hv Hrun Hbefore.
  assert (Hinput : configStorage (number state) initial (snd (transactionVmInput state tx validation))).
  { apply (transaction_entry_config state tx validation Reserve_balance.precompile_address
      (reachableReserveStorage (number state) initial) Hsignatures Hv).
    destruct Hbefore as [Hmap [Hnonce [Hcode HP]]].
    repeat split; assumption. }
  assert (Hgas : (0 ≤ Evmc.Message.t.gas (fst (transactionVmInput state tx validation)) < 2^63)%Z).
  { unfold transactionVmInput. destruct (transactionPrefix state tx validation) as [applied prepared].
    exact (validated_message_gas state tx validation _ Hv). }
  assert (Htarget : let msg := fst (transactionVmInput state tx validation) in
    Evmc.Message.t.kind msg = Evmc.Message.CallKind.Create ∨
    Evmc.Message.t.kind msg = Evmc.Message.CallKind.Create2 ∨
    (Evmc.Message.t.recipient msg = Reserve_balance.precompile_address →
      Evmc.Message.t.code_address msg = Reserve_balance.precompile_address)).
  { unfold transactionVmInput. destruct (transactionPrefix state tx validation) as [applied prepared].
    unfold E.prepare_message. destruct (T.call_or_create_value tx) as [[target]|[code]];
      cbn beta iota zeta; [right; right; exact (fun H ⇒ H)|left; reflexivity]. }
  assert (Hfinished : configStorage (number state) initial (transactionFinished state tx validation)).
  { unfold transactionFinished. destruct (transactionVmInput state tx validation) as [msg entry].
    cbn beta iota zeta delta [fst snd] in Hinput, Hgas, Htarget.
    destruct (migratedSender state (E.transaction_validation.sender validation)); [exact Hinput|].
    exact (transaction_call_only_delayed_settings cold warm access write reserve Hrevision Hcold Hwarm
      (number state) initial tx msg Hgas Htarget entry Hinput). }
  apply (process_transaction_retains_finished_config state tx validation post
    Reserve_balance.precompile_address _ Hv Hrun).
  - destruct Hfinished as [Hmap Hnonce Hcode [_ HP] Hcreated Hdestroyed].
    repeat split; assumption.
  - exact (protected_storage_undestroyed _ _ _ Hfinished).
Qed.
End Execution.
Print Assumptions process_transaction_only_delayed_settings.