Library SetcodeConcrete.noncecall

Nonce accounting through the generated rollback boundary

A failed child call restores the state saved by call_impl. For CREATE, call increments the creator before taking that snapshot, so that one increment survives failure. call_nonce_bound counts both that increment and the new account's initialization, without assuming rollback removes either one. The recursive VM and native-precompile obligations are kept explicit in this Host induction step.
From Stdlib Require Import ZArith Lia.
Require Import RocqOfOCaml.RocqOfOCaml MonadVM.Host.
Require Import SetcodeConcrete.checkednonce SetcodeConcrete.noncecreate SetcodeConcrete.noncebudget
  SetcodeConcrete.nonceeffects SetcodeConcrete.noncecallimpl.
Set Default Goal Selector "!".
Local Open Scope Z_scope.

Local Opaque H.call_impl H.try_increment_nonce.

Section Calls.
Context {F : H.FArgs}.

Theorem call_nonce_bound query msg :
  (∀ nested code state,
    0 ≤ Evmc.Message.t.gas nested < 2^63 → nonceState query state →
    let '(result, after) := H.Vm.(Evmc.Vm.SIG.execute) nested code state in
    nonceResult query (Evmc.Message.t.gas nested) 0 state result after) →
  (∀ state, nonceState query state →
    let '(result, after) := H.process_call None msg state in
    checkedNonceResult query (Evmc.Message.t.gas msg) 0 state result after) →
  0 ≤ Evmc.Message.t.gas msg < 2^63 →
  ∀ state, nonceState query state →
    let '(result, after) := H.call msg state in
    checkedNonceResult query (Evmc.Message.t.gas msg)
      (if Evmc.Message.CallKind.is_create (Evmc.Message.t.kind msg) then 2 else 0)
      state result after.
Proof.
  intros Hvm Hcall Hgas state Hinitial.
  pose proof (call_impl_nonce_bound query None msg Hvm Hcall Hgas) as Himpl.
  unfold H.call, H.M.
  destruct (Evmc.Message.CallKind.is_create (Evmc.Message.t.kind msg)) eqn:Hkind.
  - pose proof (create_increment_nonce_bound (F := F) (Evmc.Message.t.sender msg)
      state query (proj1 Hinitial) (proj2 Hinitial)) as Hincrement.
    cbn -[H.try_increment_nonce H.call_impl checkedNonceResult nonceState nonceAt].
    destruct (H.try_increment_nonce (Evmc.Message.t.sender msg) state)
      as [ok prepared] eqn:HincrementRun.
    cbn beta iota zeta delta [fst snd] in Hincrement.
    destruct Hincrement as [Hmap [Hnonnegative Hbound]].
    destruct ok.
    + specialize (Himpl prepared (conj Hmap Hnonnegative)).
      destruct (H.call_impl None msg prepared) as [result after] eqn:Hrun.
      destruct Himpl as [Hafter Htotal].
      cbn -[checkedNonceResult nonceState nonceAt].
      rewrite Hrun.
      unfold checkedNonceResult. split; [exact Hafter|]. lia.
    + cbn -[checkedNonceResult nonceState nonceAt]. unfold checkedNonceResult.
      split; [split; assumption|].
      cbn -[nonceAt checkedRefund].
      match goal with |- _ + checkedRefund ?gas ?result ≤ _ ⇒
        pose proof (checked_refund_bound gas result ltac:(lia)) end. lia.
  - cbn -[H.call_impl checkedNonceResult nonceState nonceAt].
    specialize (Himpl state Hinitial).
    destruct (H.call_impl None msg state). exact Himpl.
Qed.
End Calls.

Print Assumptions call_nonce_bound.