Library QedkConcrete.contractpreservation

Component proofs for the generated contract harness used by staking. The ABI wrapper and dispatch do not change transaction state themselves. State changes are exactly those made by the selected handler. These lemmas do not assume that the staking handlers preserve any particular invariant; those handler proofs remain obligations of the staking port.
From Stdlib Require Import Bool ZArith.
Require Import RocqOfOCaml.RocqOfOCaml MonadVM.Contract.
Set Default Goal Selector "!".
Module C := MonadVM.Contract.Make.
Module TS := MonadVM.State.TransactionState.
Module Msg := MonadVM.Evmc.Message.
Module Ty := MonadVM.Contract._Type.

Section Harness.
Context {Error : Set} {F : @C.FArgs Error}.

Lemma revert_state error state :
  snd (C.Function.revert error state) = state.
Proof. reflexivity. Qed.

Lemma insufficient_gas_state (I O : Set) (fn : C.Function.impl I O) msg state :
  MonadVM.Gas.op_lt (MonadVM.Gas.of_uint64 (Msg.t.gas msg))
    (C.Function.impl.gas_cost fn) = true →
  snd (C.Function.send_impl fn msg state) = state.
Proof. intro Hgas. unfold C.Function.send_impl. rewrite Hgas. reflexivity. Qed.

R may express preservation of storage shape, balances, code, nonces, or an aggregate-supply bound. Only the selected handler must establish R; rejecting before invoking it leaves the entire transaction state intact.
Lemma send_impl_relation (I O : Set) (fn : C.Function.impl I O) msg state
    (R : TS.t → TS.t → Prop) :
  R state state →
  (match C.Function.impl.impl fn with
   | C.Function.Raw handler ⇒
       R state (snd (handler (Msg.t.sender msg) (Msg.t.value msg)
         (Msg.t.input_data msg) state))
   | C.Function.Abi handler ⇒ ∀ input,
       R state (snd (handler (Msg.t.sender msg) (Msg.t.value msg) input state))
   end) →
  R state (snd (C.Function.send_impl fn msg state)).
Proof.
  intros Hsame Hhandler. unfold C.Function.send_impl.
  destruct (MonadVM.Gas.op_lt _ _); [exact Hsame|].
  destruct (andb _ _); [exact Hsame|].
  destruct (C.Function.impl.impl fn) as [handler|handler] eqn:Hfn.
  - destruct (Ty.dec_bytes _ _ _) as [input|err] eqn:Hdecode.
    + specialize (Hhandler input).
      cbn.
      destruct (handler (Msg.t.sender msg) (Msg.t.value msg) input state)
        as [[output|err] post] eqn:Hrun;
        exact Hhandler.
    + exact Hsame.
  - cbn.
    destruct (handler (Msg.t.sender msg) (Msg.t.value msg) (Msg.t.input_data msg) state)
      as [[output|err] post] eqn:Hrun;
      exact Hhandler.
Qed.

Dispatch changes only the message's input bytes; it invokes one selected function (or the fallback). It adds no transaction-state transition.
Lemma dispatch_relation (contract : C.t) msg state
    (R : TS.t → TS.t → Prop) :
  (∀ input,
    R state (snd (C.Function.send_message
      (C.Dispatch_table.t.fallback contract) input state))) →
  (∀ selector fn,
    (Abi.Selector.(Byte_string.Fixed.Fixed_result.Map)
      .(Byte_string.Fixed.Map.Map_signature.find_opt))
      selector (C.Dispatch_table.t.functions contract) = Some fn →
    ∀ input, R state (snd (C.Function.send_message fn input state))) →
  R state (snd (C.dispatch contract msg state)).
Proof.
  destruct contract as [functions fallback]. intros Hfallback Hselected.
  unfold C.dispatch. destruct (Z.ltb _ _); [apply Hfallback|].
  destruct ((Abi.Selector.(Byte_string.Fixed.Fixed_result.Map)
      .(Byte_string.Fixed.Map.Map_signature.find_opt)) _ functions)
    as [fn|] eqn:Hlookup.
  - eapply Hselected. exact Hlookup.
  - apply Hfallback.
Qed.

Print Assumptions revert_state.
Print Assumptions insufficient_gas_state.
Print Assumptions send_impl_relation.
Print Assumptions dispatch_relation.
End Harness.