Library QedkConcrete.hostrollback

Rollback in the generated host

A failed CALL restores its input transaction state, including every reserve precompile slot. This is a property of the actual generated host wrapper, independent of what the VM did inside the call. Fees and authorizations are applied before that input state and are not rolled back here.
From Stdlib Require Import ZArith.
Require Import RocqOfOCaml.RocqOfOCaml.
Require Import MonadVM.Host.
Require Import QedkConcrete.reservecheck.
Set Default Goal Selector "!".

Module H := MonadVM.Host.Make.
Module S := MonadVM.State.TransactionState.
Module E := MonadVM.Evmc.

Section Rollback.
The actual OCaml functor configuration; no VM contract is assumed.
Context `{_fargs : H.FArgs}.

Local Abbreviation call := (@H.call_impl _fargs).

Lemma failed_call_restores_state from_tx msg s :
  E.Message.t.kind msg = E.Message.CallKind.Call →
  E.Result.t.status_code (fst (call from_tx msg s)) ≠
    E.Result.StatusCode.Success →
  snd (call from_tx msg s) = s.
Proof.
  intro Hkind. unfold H.call_impl. rewrite Hkind.
  cbn -[H.process_call Reserve_balance.dipped_into_reserve].
  match goal with
  | |- context [Monad.Identity.Impl.op_gtgteq ?run _] ⇒
      destruct run as [result intermediate] eqn:Hcore
  end.
  destruct result as [status gasleft refund output address].
  destruct from_tx as [tx|]; destruct status;
    cbn -[Reserve_balance.dipped_into_reserve];
    unfold Monad.Make_Monad.when_; cbn -[Reserve_balance.dipped_into_reserve];
    try (intros; reflexivity); try congruence.
  match goal with
  | |- context [Monad.Identity.Impl.op_gtgteq
        (Monad.Identity.Impl.op_gtgteq ?check _) _] ⇒
      destruct check as [dipped checked]
  end.
  destruct dipped; cbn; [intros; reflexivity|congruence].
Qed.

CREATE/CREATE2 retain access-list warming on failure, but still restore the entire world state. This includes reserve settings made in initcode.
Lemma failed_message_restores_world from_tx msg s :
  E.Result.t.status_code (fst (call from_tx msg s)) ≠
    E.Result.StatusCode.Success →
  S.t.world_state (snd (call from_tx msg s)) = S.t.world_state s.
Proof.
  unfold H.call_impl.
  destruct (E.Message.t.kind msg);
    cbn -[H.process_call H.process_create
      MonadVM.Chain.Ethereum.Address.of_contract_creation
      Reserve_balance.dipped_into_reserve].
  all:
  match goal with
  | |- context [Monad.Identity.Impl.op_gtgteq ?run _] ⇒
      destruct run as [result intermediate] eqn:Hcore
  end.
  all: destruct result as [status gasleft refund output address].
  all: destruct from_tx as [tx|]; destruct status;
    cbn -[Reserve_balance.dipped_into_reserve];
    unfold Monad.Make_Monad.when_; cbn -[Reserve_balance.dipped_into_reserve];
    try (intros; reflexivity); try congruence.
  all: match goal with
  | |- context [Monad.Identity.Impl.op_gtgteq
        (Monad.Identity.Impl.op_gtgteq ?check _) _] ⇒
      destruct check as [dipped checked]
  end.
  all: destruct dipped; cbn; [intros; reflexivity|congruence].
Qed.

Local Abbreviation dip := (@Reserve_balance.dipped_into_reserve).

Success of a top-level message certifies that the concrete reserve predicate passed. Nothing is assumed about the VM's balance effects.
Lemma successful_transaction_passes_reserve tx msg s :
  E.Result.t.status_code (fst (call (Some tx) msg s)) =
    E.Result.StatusCode.Success →
  fst (dip (H.ChainParams.(MonadVM.Chain.Monad.PARAMS.revision))
    (snd (call (Some tx) msg s))) = false.
Proof.
  unfold H.call_impl.
  destruct (E.Message.t.kind msg);
    cbn -[H.process_call H.process_create
      MonadVM.Chain.Ethereum.Address.of_contract_creation
      Reserve_balance.dipped_into_reserve].
  all: match goal with
  | |- context [Monad.Identity.Impl.op_gtgteq ?run _] ⇒
      destruct run as [result intermediate] eqn:Hcore
  end.
  all: destruct result as [status gasleft refund output address]; destruct status;
    cbn -[Reserve_balance.dipped_into_reserve];
    unfold Monad.Make_Monad.when_; cbn -[Reserve_balance.dipped_into_reserve];
    try discriminate.
  all: match goal with
  | |- context [Monad.Identity.Impl.op_gtgteq
        (Monad.Identity.Impl.op_gtgteq ?check _) _] ⇒
      assert (Hread : snd check = intermediate) by reflexivity;
      destruct check as [dipped checked] eqn:Hcheck
  end.
  all: cbn in Hread; subst checked.
  all: destruct dipped; cbn -[Reserve_balance.dipped_into_reserve];
    [discriminate | intro Hsuccess; rewrite Hcheck; reflexivity].
Qed.

The generated Host either rolls the world back or enforces the reserve predicate. This statement covers VM failure and reserve-triggered failure without assuming anything about the VM's balance effects.
Theorem transaction_world_rollback_or_reserve tx msg s :
  let sf := snd (call (Some tx) msg s) in
  S.t.world_state sf = S.t.world_state s ∨
  fst (dip (H.ChainParams.(MonadVM.Chain.Monad.PARAMS.revision)) sf) = false.
Proof.
  cbn zeta.
  destruct (E.Result.t.status_code (fst (call (Some tx) msg s))) eqn:Hstatus.
  all: try (left; apply failed_message_restores_world; rewrite Hstatus; discriminate).
  right. apply successful_transaction_passes_reserve. exact Hstatus.
Qed.

Local Abbreviation protected := (@reservecheck.protected).
Local Abbreviation accountAt := (@reservecheck.accountAt).
Local Abbreviation nonsender_bound := (@reservecheck.nonsender_reserve_bound).
Local Abbreviation sender_bound := (@reservecheck.sender_reserve_bound).

These are consequences of the Host call, not assumed VM postconditions. They use the transaction metadata present at the check. Preservation of that metadata by the concrete VM is a separate refinement obligation.
Corollary successful_nonsender_reserve_bound tx msg s addr original :
  let sf := snd (call (Some tx) msg s) in
  H.ChainParams.(MonadVM.Chain.Monad.PARAMS.revision) = Support.Variants.Revision.Eleven →
  E.Result.t.status_code (fst (call (Some tx) msg s)) = E.Result.StatusCode.Success →
  Chain.Ethereum.Address.Map.find_opt addr
    (State.WorldState.t.accounts (S.t.initial_world_state sf)) = Some original →
  Chain.Ethereum.Address._Set.(RocqOfOCaml.OCamlSet.S.mem) addr (S.t.self_destruct sf) = false →
  @Chain.Ethereum.Account.is_smart_contract (accountAt sf addr) = false →
  Chain.Ethereum.Address.op_eq addr (S.t.tx_origin sf) = false →
  (Numeric.U256.to_z (protected sf addr original) ≤
    Numeric.U256.to_z (Chain.Ethereum.Account.t.balance (accountAt sf addr)))%Z.
Proof.
  cbn zeta. intros Hrevision Hsuccess Hfind Hdes Hsc Hsender.
  pose proof (successful_transaction_passes_reserve tx msg s Hsuccess) as Hcheck.
  rewrite Hrevision in Hcheck.
  apply (nonsender_bound _ addr original Hcheck).
  - apply reservecheck.address_find_member. exact Hfind.
  - exact Hdes.
  - exact Hsc.
  - exact Hsender.
Qed.

Corollary successful_sender_reserve_bound tx msg s original :
  let sf := snd (call (Some tx) msg s) in
  let addr := S.t.tx_origin sf in
  let reserve := protected sf addr original in
  let fee := (S.t.tx_gas_limit sf × S.t.tx_gas_price sf)%N in
  H.ChainParams.(MonadVM.Chain.Monad.PARAMS.revision) = Support.Variants.Revision.Eleven →
  E.Result.t.status_code (fst (call (Some tx) msg s)) = E.Result.StatusCode.Success →
  Chain.Ethereum.Address.Map.find_opt addr
    (State.WorldState.t.accounts (S.t.initial_world_state sf)) = Some original →
  Chain.Ethereum.Address._Set.(RocqOfOCaml.OCamlSet.S.mem) addr (S.t.self_destruct sf) = false →
  @Chain.Ethereum.Account.is_smart_contract (accountAt sf addr) = false →
  fst (@Reserve_balance.is_emptying_tx sf) = false →
  (0 ≤ Numeric.U256.to_z reserve < 2^256)%Z →
  (Z.of_N fee ≤ Numeric.U256.to_z reserve ∧
    Numeric.U256.to_z reserve - Z.of_N fee ≤
      Numeric.U256.to_z (Chain.Ethereum.Account.t.balance (accountAt sf addr)))%Z.
Proof.
  cbn zeta. intros Hrevision Hsuccess Hfind Hdes Hsc Hempty Hrange.
  pose proof (successful_transaction_passes_reserve tx msg s Hsuccess) as Hcheck.
  rewrite Hrevision in Hcheck.
  apply (sender_bound _ original Hcheck).
  - apply reservecheck.address_find_member. exact Hfind.
  - exact Hdes.
  - exact Hsc.
  - exact Hempty.
  - exact Hrange.
Qed.

End Rollback.

Print Assumptions failed_message_restores_world.