Library QedkConcrete.reserveprecompileeffects

State writes performed by the reserve precompile

Pure reads and rejected setters preserve the state; a successful setter changes only the world-state field. No VM invariant is assumed here.
From Stdlib Require Import ZArith.
Require Import RocqOfOCaml.RocqOfOCaml.
Require Import MonadVM.Precompiles.Reserve_balance_introspection.
Module R := Reserve_balance_introspection.Make.
Module S := State.TransactionState.
Section Effects.
Context {F : R.FArgs}.
Local Abbreviation precompile := (@R.precompile F).
Local Abbreviation introspection := (@R.introspection F).
Local Abbreviation setter := (@Reserve_balance.set_reserve_balance).
Local Abbreviation blockNumber := (fun s ⇒
  Chain.Ethereum.Block.Header.t.number (Chain.Ethereum.Block.t.header (S.t.current_block s))).
Lemma introspection_read_only msg s : snd (introspection msg s) = s.
Proof.
  unfold R.introspection.
  repeat match goal with |- context [if ?test then _ else _] ⇒ destruct test end.
  all: reflexivity.
Qed.

Lemma precompile_world_effect msg s :
  snd (precompile msg s) = s ∨
  ∃ value world,
    setter (S.t.world_state s) (blockNumber s) (Evmc.Message.t.sender msg) value = Some world ∧
    snd (precompile msg s) = S.t.with_world_state world s.
Proof.
  unfold R.precompile.
  cbn beta zeta.
  repeat match goal with
    |- context [match ?methods with None ⇒ _ | Some _ ⇒ _ end] ⇒
      destruct methods as [[gas length]|]
  end.
  all: try (left; apply introspection_read_only).
  all: repeat match goal with |- context [if ?test then _ else _] ⇒ destruct test end.
  all: try (left; reflexivity).
  cbn -[Reserve_balance.set_reserve_balance Numeric.U256.of_bytes_be_exn
    Byte_string.Bytes.sub Numeric.U256.to_repr_bytes Numeric.U256.of_uint_exn
    Gas.to_uint64 Gas.op_minus Gas.of_uint64 Gas.of_int Evmc.Result.failure].
  match goal with |- context [match ?writeResult with Some _ ⇒ _ | None ⇒ _ end] ⇒
    destruct writeResult as [world|] eqn:Hwrite end.
  - right. eexists _, world. split; [exact Hwrite|reflexivity].
  - left. reflexivity.
Qed.

Lemma precompile_preserves_creation_set msg s :
  S.t.accounts_created_in_current_transaction (snd (precompile msg s)) =
    S.t.accounts_created_in_current_transaction s.
Proof.
  destruct (precompile_world_effect msg s) as [Hsame|[value [world [Hwrite Hafter]]]];
    [rewrite Hsame|rewrite Hafter]; reflexivity.
Qed.

End Effects.