Library SetcodeConcrete.noncecallimpl

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.
Set Default Goal Selector "!".
Local Open Scope Z_scope.

Lemma nonce_result_rebase query gas allowance before other result after :
  S.t.world_state other = S.t.world_state before →
  checkedNonceResult query gas allowance other result after →
  checkedNonceResult query gas allowance before result after.
Proof. unfold checkedNonceResult. intros Hsame Hresult. rewrite Hsame in Hresult. exact Hresult. Qed.

Lemma nonce_result_rollback query gas allowance before result after :
  nonceState query before → 0 ≤ allowance →
  nonceResult query gas allowance before result after →
  nonceResult query gas allowance before result before.
Proof.
  intros Hbefore Hallowance [_ [Hgas Htotal]].
  split; [exact Hbefore|]. split; [exact Hgas|lia].
Qed.

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

Theorem call_impl_nonce_bound query from_tx 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 from_tx 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_impl from_tx msg state in
    checkedNonceResult query (Evmc.Message.t.gas msg)
      (if Evmc.Message.CallKind.is_create (Evmc.Message.t.kind msg) then 1 else 0)
      state result after.
Proof.
  intros Hvm Hcall Hgas state Hinitial.
  pose proof (process_create_nonce_bound query msg Hvm Hgas) as Hcreate.
  unfold H.call_impl.
  destruct (Evmc.Message.t.kind msg);
    cbn -[H.process_call H.process_create checkedNonceResult nonceState nonceAt
      Addr.of_contract_creation Reserve_balance.dipped_into_reserve].
  all: match goal with
  | |- context [Monad.Identity.Impl.op_gtgteq (?action ?initial) _] ⇒
    let allowance := lazymatch action with
      | H.process_create _ ⇒ constr:(1%Z)
      | H.process_call _ _ ⇒ constr:(0%Z)
      end in
    assert (Hcore : checkedNonceResult query (Evmc.Message.t.gas msg) allowance state
      (fst (action initial)) (snd (action initial))) by
      (eapply nonce_result_rebase with (other := initial); [reflexivity|];
       lazymatch action with
       | H.process_create _ ⇒ pose proof (Hcreate initial Hinitial) as Hresult
       | H.process_call _ _ ⇒ pose proof (Hcall initial Hinitial) as Hresult
       end;
       destruct (action initial); first [exact Hresult | apply nonce_result_checked; exact Hresult]);
    destruct (action initial) as [result intermediate] eqn:Hrun
  end.
  all: cbn beta iota zeta delta [fst snd] in Hcore.
  all: destruct result as [status gasleft refund output address].
  all: destruct from_tx as [tx|]; destruct status;
    cbn -[checkedNonceResult nonceState nonceAt Reserve_balance.dipped_into_reserve];
    unfold Monad.Make_Monad.when_;
    cbn -[checkedNonceResult nonceState nonceAt Reserve_balance.dipped_into_reserve].
  all: try exact Hcore.
  all: try solve [unfold checkedNonceResult; split; [exact Hinitial|];
    match goal with |- _ + checkedRefund ?gas ?result ≤ _ ⇒
      pose proof (checked_refund_bound gas result ltac:(lia)) end;
    cbn [S.t.world_state S.t.with_accessed_addresses]; lia].
  all: match goal with
  | |- context [Monad.Identity.Impl.op_gtgteq (Monad.Identity.Impl.op_gtgteq ?check _) _] ⇒
    assert (Hcheck : snd check = intermediate) by reflexivity;
    destruct check as [dipped checked]; cbn [snd] in Hcheck; subst checked
  end.
  all: destruct dipped; cbn -[checkedNonceResult nonceState nonceAt];
    try exact Hcore.
  all: unfold checkedNonceResult; split; [exact Hinitial|];
    match goal with |- _ + checkedRefund ?gas ?result ≤ _ ⇒
      pose proof (checked_refund_bound gas result ltac:(lia)) end;
    cbn [S.t.world_state S.t.with_accessed_addresses]; lia.
Qed.

End Calls.

Print Assumptions call_impl_nonce_bound.