Library SetcodeConcrete.transactionentry

The fee and authorization prefix of SETCODE transactions

These definitions name expressions in Execution.process_transaction. They do not replace that executor. They expose the state immediately before its VM call, or before its migrated-sender failed-receipt path. Recovered authorities affect activity; only applied authorizations change nonces.
From Stdlib Require Import List ZArith Lia.
Require Import RocqOfOCaml.RocqOfOCaml MonadVM.Execution.
Require Import QedkConcrete.authorization QedkConcrete.transactionfees
  QedkConcrete.transactionframes QedkConcrete.hostframe QedkConcrete.supply
  QedkConcrete.worldaccounts QedkConcrete.validation.
Require Import SetcodeConcrete.contextframe.
Set Default Goal Selector "!".
Import ListNotations.
Local Open Scope Z_scope.
Module E := Execution.Make.
Module S := State.TransactionState.
Module B := State.BlockState.
Module W := State.WorldState.
Module A := Chain.Ethereum.Account.
Module T := Chain.Ethereum.Transaction.
Module U := Numeric.U256.

Section Entry.
Context {F : E.FArgs}.
Local Abbreviation readAccount := (fun world addr ⇒
  State.op_pointcaretlparenrparen world (@W.account None addr)).

block is the execution state before tx, not the admission snapshot. Return whether its recovered sender already has regular code.
validation is the native validation result for tx. Return the applied authorities and the state after upfront fee debit, sender nonce advancement, and authorization processing. No VM code has run.
Return the message and transaction state prepared by the concrete executor. MIP-19 counts all recovered authorities for emptying activity, even when an authorization did not apply.
The state before the final sender-activity update, beneficiary credit and deletions. Migrated senders do not run the VM; their entry state is retained. Otherwise this is the actual Host result, including rollback.
Definition transactionFinished block tx validation : S.t :=
  let '(msg, entry) := transactionVmInput block tx validation in
  if migratedSender block (E.transaction_validation.sender validation)
  then entry else snd (E.Host.call_from_eoa tx msg entry).

Lemma authorization_preserves_destruction s auth :
  S.t.self_destruct (snd (@E.process_authorization F s auth)) = S.t.self_destruct s.
Proof.
  unfold E.process_authorization.
  match goal with |- context [if ?b then _ else _] ⇒ destruct b end.
  { reflexivity. }
  match goal with |- context [match ?a with Some _ ⇒ _ | None ⇒ _ end] ⇒
    destruct a as [authority|] end.
  2: reflexivity.
  cbn beta iota zeta.
  match goal with |- context [if ?b then _ else _] ⇒ destruct b end.
  2: reflexivity.
  match goal with |- context [if ?b then _ else _] ⇒ destruct b end; reflexivity.
Qed.

Lemma authorizations_preserve_destruction auths applied s :
  S.t.self_destruct (snd (Result.List.fold_left
    (fun '(applied, state) auth ⇒
      match @E.process_authorization F state auth with
      | (None, next) ⇒ (applied, next)
      | (Some authority, next) ⇒ (authority :: applied, next)
      end) (applied, s) auths)) = S.t.self_destruct s.
Proof.
  revert applied s. induction auths as [|auth auths IH]; intros applied s.
  - reflexivity.
  - change (@Result.List.fold_left) with (@OCamlList.fold_left).
    cbn [OCamlList.fold_left].
    pose proof (authorization_preserves_destruction s auth) as Hdes.
    destruct (@E.process_authorization F s auth) as [[authority|] next]; cbn [snd] in Hdes.
    all: rewrite IH; exact Hdes.
Qed.

Lemma transaction_entry_balances block tx validation :
  E.validate_transaction block tx = inl validation →
  balanceInvariant (B.t.world_state block) →
  ∀ query,
    U.to_z (A.t.balance (readAccount
      (S.t.world_state (snd (transactionVmInput block tx validation))) query)) =
    if String.eqb (E.transaction_validation.sender validation) query
    then U.to_z (A.t.balance (readAccount (B.t.world_state block) query)) -
      U.to_z (E.transaction_validation.total_fee validation)
    else U.to_z (A.t.balance (readAccount (B.t.world_state block) query)).
Proof.
  intros Hvalidation Hinitial query.
  pose proof (@account_balance_bounded_by_supply (B.t.world_state block)
    (E.transaction_validation.sender validation) (proj1 (proj2 Hinitial))) as Hbalance.
  pose proof (proj2 (proj2 Hinitial)) as Hsupply.
  pose proof (validated_debit_range block tx validation Hvalidation ltac:(cbn beta in Hbalance; lia))
    as [Hfee Hdebit].
  pose proof (@transaction_entry_balances_with_nonce F
    (fun account ⇒ Setcode_rules.advance_sender_nonce
      (migratedSender block (E.transaction_validation.sender validation))
      (A.t.nonce account) (T.nonce tx))
    (S.t.with_tx_chain_id E.Params.(Execution.Make_Params_signature.chain_id)
      (S.make block (E.transaction_validation.sender validation) tx))
    (E.transaction_validation.sender validation) (E.transaction_validation.total_fee validation)
    (T.authorization_list tx) (proj1 Hinitial) Hdebit) as [_ [_ Hall]].
  pose proof (Hall query) as Hbalances.
  match type of Hbalances with context [Result.List.fold_left ?f ?start (T.authorization_list tx)] ⇒
    change (Result.List.fold_left f start (T.authorization_list tx))
      with (transactionPrefix block tx validation) in Hbalances
  end.
  unfold transactionVmInput. destruct (transactionPrefix block tx validation) as [applied prepared].
  cbn beta iota zeta delta [fst snd] in Hbalances |- ×.
  unfold State.op_pointcaretlparenrparen, W.account, W.account_opt.
  cbn -[E.bump_emptying_transaction_counters Chain.Ethereum.Address.Map.find_opt].
  rewrite bump_counters_accounts. exact Hbalances.
Qed.

Lemma transaction_entry_destruction block tx validation :
  S.t.self_destruct (snd (transactionVmInput block tx validation)) = [].
Proof.
  assert (Hdes : S.t.self_destruct (snd (transactionPrefix block tx validation)) = []).
  { unfold transactionPrefix. rewrite authorizations_preserve_destruction. reflexivity. }
  unfold transactionVmInput. destruct (transactionPrefix block tx validation) as [applied prepared].
  exact Hdes.
Qed.

Lemma authorization_preserves_migration_context s auth :
  migrationContext (snd (@E.process_authorization F s auth)) = migrationContext s.
Proof.
  unfold E.process_authorization.
  match goal with |- context [if ?b then _ else _] ⇒ destruct b end.
  { reflexivity. }
  match goal with |- context [match ?a with Some _ ⇒ _ | None ⇒ _ end] ⇒
    destruct a as [authority|] end.
  2: reflexivity.
  cbn beta iota zeta.
  match goal with |- context [if ?b then _ else _] ⇒ destruct b end.
  2: reflexivity.
  match goal with |- context [if ?b then _ else _] ⇒ destruct b end; reflexivity.
Qed.

Lemma authorizations_preserve_migration_context auths applied s :
  migrationContext (snd (Result.List.fold_left
    (fun '(applied, state) auth ⇒
      match @E.process_authorization F state auth with
      | (None, next) ⇒ (applied, next)
      | (Some authority, next) ⇒ (authority :: applied, next)
      end) (applied, s) auths)) = migrationContext s.
Proof.
  revert applied s. induction auths as [|auth auths IH]; intros applied s.
  - reflexivity.
  - change (@Result.List.fold_left) with (@OCamlList.fold_left).
    cbn [OCamlList.fold_left].
    pose proof (authorization_preserves_migration_context s auth) as Hcontext.
    destruct (@E.process_authorization F s auth) as [[authority|] next]; cbn [snd] in Hcontext.
    all: rewrite IH; exact Hcontext.
Qed.

Lemma transaction_prefix_context block tx validation :
  migrationContext (snd (transactionPrefix block tx validation)) =
  migrationContext (S.t.with_tx_chain_id E.Params.(Execution.Make_Params_signature.chain_id)
    (S.make block (E.transaction_validation.sender validation) tx)).
Proof.
  unfold transactionPrefix. rewrite authorizations_preserve_migration_context. reflexivity.
Qed.

Lemma transaction_entry_metadata block tx validation :
  let entry := snd (transactionVmInput block tx validation) in
  S.t.initial_world_state entry = B.t.world_state block ∧
  S.t.current_block entry = B.t.current_block block ∧
  S.t.tx_origin entry = E.transaction_validation.sender validation.
Proof.
  pose proof (transaction_prefix_context block tx validation) as Hcontext.
  unfold transactionVmInput. destruct (transactionPrefix block tx validation) as [applied prepared].
  unfold migrationContext, transactionContext in Hcontext.
  injection Hcontext as Hsnapshot Hblock Hsender Hgas Hprice Hhistory Hcounter Hprior Hchain.
  split; [exact Hsnapshot|]. split; [exact Hblock|exact Hsender].
Qed.

Lemma bump_counters_history s addresses :
  W.t.history (S.t.world_state (@E.bump_emptying_transaction_counters F addresses s)) =
  W.t.history (S.t.world_state s).
Proof.
  unfold E.bump_emptying_transaction_counters.
  cbn beta iota zeta delta [S.t.with_world_state S.t.world_state].
  generalize (S.t.world_state s). induction addresses as [|addr addresses IH]; intro world.
  - reflexivity.
  - change (@Result.List.fold_left) with (@OCamlList.fold_left).
    cbn [OCamlList.fold_left]. rewrite IH. reflexivity.
Qed.

Activity is read from earlier executed transactions, not the current authorization list. Authorization activity affects emptying separately.
Lemma transaction_entry_recent_sender block tx validation query :
  Reserve_balance.sent_in_window (snd (transactionVmInput block tx validation)) query =
  Reserve_balance.sent_in_window
    (S.t.with_tx_chain_id E.Params.(Execution.Make_Params_signature.chain_id)
      (S.make block (E.transaction_validation.sender validation) tx)) query.
Proof.
  pose proof (transaction_prefix_context block tx validation) as Hcontext.
  unfold transactionVmInput. destruct (transactionPrefix block tx validation) as [applied prepared].
  unfold migrationContext, transactionContext in Hcontext.
  injection Hcontext as Hsnapshot Hblock Hsender Hgas Hprice Hhistory Hcounter Hprior Hchain.
  unfold Reserve_balance.sent_in_window.
  change (let sent := fun tx ⇒ match T.sender (S.t.tx_chain_id prepared) tx with
    | Some sender ⇒ Chain.Ethereum.Address.op_eq sender query | None ⇒ false end in
    OCamlList._exists sent (S.t.prior_transactions prepared) ||
    OCamlList._exists (fun b ⇒
      (Support.UintN.op_lt (Chain.Ethereum.Block.Header.t.number (Chain.Ethereum.Block.t.header b))
        (Chain.Ethereum.Block.Header.t.number (Chain.Ethereum.Block.t.header (S.t.current_block prepared))) &&
       Support.UintN.op_lt (Chain.Ethereum.Block.Header.t.number (Chain.Ethereum.Block.t.header (S.t.current_block prepared)))
        (Support.UintN.op_plus (Chain.Ethereum.Block.Header.t.number (Chain.Ethereum.Block.t.header b))
          Reserve_balance.execution_consensus_delay)) &&
      OCamlList._exists sent (Chain.Ethereum.Block.t.transactions b))
      (W.t.history (S.t.world_state (E.bump_emptying_transaction_counters
        (if Setcode_rules.enabled E.Params.(Execution.Make_Params_signature.revision)
         then Result.List.filter_map T.Authorization.authority (T.authorization_list tx) else applied) prepared))) =
    Reserve_balance.sent_in_window
      (S.t.with_tx_chain_id E.Params.(Execution.Make_Params_signature.chain_id)
        (S.make block (E.transaction_validation.sender validation) tx)) query)%bool.
  rewrite bump_counters_history, Hblock, Hhistory, Hprior, Hchain. reflexivity.
Qed.
End Entry.