Library QedkConcrete.hostframe

Transaction context preserved by concrete host operations

The reserve check reads the initial snapshot and fee fields after the VM runs. These fields must still describe the same transaction. World-history and emptying counters are also outside the VM's write interface.
Require Import RocqOfOCaml.RocqOfOCaml.
Require Import MonadVM.Host.
Set Default Goal Selector "!".

Module S := MonadVM.State.TransactionState.
Module W := MonadVM.State.WorldState.
Module H := MonadVM.Host.Make.

Definition transactionContext (s : S.t) :=
  (S.t.initial_world_state s, S.t.current_block s, S.t.tx_origin s,
    S.t.tx_gas_limit s, S.t.tx_gas_price s,
    W.t.history (S.t.world_state s),
    W.t.next_emptying_transaction_block (S.t.world_state s)).

Definition preservesContext {A : Set} (action : S.t → A × S.t) : Prop :=
  ∀ s, transactionContext (snd (action s)) = transactionContext s.

Lemma context_bind {A B : Set} (action : S.t → A × S.t)
    (next : A → S.t → B × S.t) :
  preservesContext action →
  (∀ a, preservesContext (next a)) →
  preservesContext (fun s ⇒ let '(a, middle) := action s in next a middle).
Proof.
  intros Haction Hnext s.
  specialize (Haction s). destruct (action s) as [a middle].
  rewrite Hnext. exact Haction.
Qed.

Section HostOperations.
Context {F : H.FArgs}.
Context {U64 : Basics.Unreachable Numeric.U64.t}.
Context {U256 : Basics.Unreachable Numeric.U256.t}.
Context {Uint : Basics.Unreachable int}.
Context {U32 : Basics.Unreachable
  Byte_string.B32.(Byte_string.Fixed.Fixed_result.t)}.
Context {Uunit : Basics.Unreachable unit}.
Context {Ustring : Basics.Unreachable string}.
Context {Ubyte : Basics.Unreachable ascii}.
Context {Uint64 : Basics.Unreachable MonadVM.Uint64.t}.
Context {UN : Basics.Unreachable MonadVM.Support.UintN.t}.
Context {Ubytes : Basics.Unreachable Byte_string.Bytes.t}.

Lemma transfer_preserves_context sender recipient amount :
  preservesContext (@H.transfer_ether F sender recipient amount).
Proof.
  intro s. unfold H.transfer_ether.
  cbn -[Numeric.U256.op_gteq].
  match goal with |- context [if ?b then _ else _] ⇒ destruct b end;
    reflexivity.
Qed.

Lemma storage_preserves_context addr key value :
  preservesContext (@H.set_storage F addr key value).
Proof. intro s. reflexivity. Qed.

Lemma transient_storage_preserves_context addr key value :
  preservesContext (@H.set_transient_storage F addr key value).
Proof. intro s. reflexivity. Qed.

Lemma log_preserves_context addr data topics :
  preservesContext (@H.emit_log F addr data topics).
Proof. intro s. reflexivity. Qed.

Lemma access_account_preserves_context addr :
  preservesContext (@H.access_account F addr).
Proof.
  intro s. unfold H.access_account. cbn.
  match goal with |- context [if ?b then _ else _] ⇒ destruct b end;
    reflexivity.
Qed.

Lemma access_storage_preserves_context addr key :
  preservesContext (@H.access_storage F addr key).
Proof.
  intro s. unfold H.access_storage. cbn.
  match goal with |- context [if ?b then _ else _] ⇒ destruct b end;
    reflexivity.
Qed.

Lemma transfer_preserves_creation_tracking sender recipient amount s :
  let sf := snd (@H.transfer_ether F sender recipient amount s) in
  (S.t.accounts_created_in_current_transaction sf, S.t.self_destruct sf) =
  (S.t.accounts_created_in_current_transaction s, S.t.self_destruct s).
Proof.
  cbn zeta. unfold H.transfer_ether. cbn -[Numeric.U256.op_gteq].
  match goal with |- context [if ?b then _ else _] ⇒ destruct b end;
    reflexivity.
Qed.

Lemma selfdestruct_preserves_context addr beneficiary :
  preservesContext (@H.selfdestruct F addr beneficiary).
Proof.
  intro s. unfold H.selfdestruct.
  cbn -[H.transfer_ether].
  match goal with
  | |- context [H.transfer_ether ?from ?to ?value ?pre] ⇒
      pose proof (transfer_preserves_context from to value pre) as Htransfer;
      destruct (H.transfer_ether from to value pre) as [ok middle]
  end.
  destruct ok; cbn.
  all: match goal with |- context [if ?b then _ else _] ⇒ destruct b end.
  all: exact Htransfer.
Qed.

SELFDESTRUCT adds an exemption only for an address recorded as created in this transaction. Proving that CREATE cannot record a signer address will let the reserve theorem exclude this exemption for such signers.
Lemma selfdestruct_tracking addr beneficiary s :
  S.t.self_destruct (snd (@H.selfdestruct F addr beneficiary s)) =
  if Chain.Ethereum.Address._Set.(OCamlSet.S.mem) addr
    (S.t.accounts_created_in_current_transaction s)
  then Chain.Ethereum.Address._Set.(OCamlSet.S.add) addr (S.t.self_destruct s)
  else S.t.self_destruct s.
Proof.
  unfold H.selfdestruct. cbn -[H.transfer_ether Chain.Ethereum.Address._Set].
  match goal with
  | |- context [H.transfer_ether ?from ?to ?value ?pre] ⇒
      pose proof (transfer_preserves_creation_tracking from to value pre) as Htracking;
      destruct (H.transfer_ether from to value pre) as [ok middle]
  end.
  cbn in Htracking. injection Htracking as Hcreated Hdestroyed.
  destruct ok; cbn -[Chain.Ethereum.Address._Set].
  all: rewrite Hcreated.
  all: destruct (Chain.Ethereum.Address._Set.(OCamlSet.S.mem) addr
    (S.t.accounts_created_in_current_transaction s));
    cbn -[Chain.Ethereum.Address._Set]; rewrite Hdestroyed; reflexivity.
Qed.

Host reads preserve the complete transaction state, even when a pure decoder takes an exceptional fallback path.
Lemma account_exists_read_only addr s : snd (@H.account_exists F addr s) = s.
Proof. reflexivity. Qed.

Lemma get_storage_read_only addr key s :
  snd (@H.get_storage F addr key s) = s.
Proof. reflexivity. Qed.

Lemma get_balance_read_only addr s : snd (@H.get_balance F addr s) = s.
Proof. reflexivity. Qed.

Lemma get_code_size_read_only addr s : snd (@H.get_code_size F addr s) = s.
Proof. reflexivity. Qed.

Lemma get_code_hash_read_only addr s :
  snd (@H.get_code_hash F addr s) = s.
Proof. reflexivity. Qed.

Lemma copy_code_read_only addr offset size s :
  snd (@H.copy_code F addr offset size s) = s.
Proof.
  unfold H.copy_code. cbn -[Byte_string.Bytes.length].
  match goal with |- context [if ?b then _ else _] ⇒ destruct b end;
    reflexivity.
Qed.

Lemma get_tx_context_read_only s :
  snd (@H.get_tx_context F s) = s.
Proof. reflexivity. Qed.

Lemma get_block_hash_read_only block s :
  snd (@H.get_block_hash F block s) = s.
Proof. reflexivity. Qed.

Lemma get_transient_storage_read_only addr key s :
  snd (@H.get_transient_storage F addr key s) = s.
Proof. reflexivity. Qed.

End HostOperations.

Print Assumptions selfdestruct_tracking.