Library QedkConcrete.transactionfees

Beneficiary fees redistribute the upfront charge

The beneficiary receives only the priority-price part of the fee already removed from the sender. VM execution may burn balance, but must not mint it. Consequently the credit fits under the initial supply bound even when the beneficiary is the sender; it needs no external-credit allowance.
From Stdlib Require Import NArith ZArith Lia.
Require Import RocqOfOCaml.RocqOfOCaml.
Require Import MonadVM.Execution QedkConcrete.validation QedkConcrete.supply
  QedkConcrete.credits QedkConcrete.worldaccounts QedkConcrete.authorization.
Set Default Goal Selector "!".
Local Open Scope Z_scope.

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

Lemma validated_priority_price block tx validation :
  @E.validate_transaction F block tx = inl validation →
  (Chain.Ethereum.Block.Header.t.base_fee_per_gas (Chain.Ethereum.Block.t.header (B.t.current_block block)) ≤
    E.transaction_validation.effective_gas_price validation)%N.
Proof.
  intro Hvalidated. unfold E.validate_transaction in Hvalidated. cbn zeta in Hvalidated.
  do 13 (apply validation.bind_success in Hvalidated;
    let step := fresh "step" in let Hstep := fresh "Hstep" in
    destruct Hvalidated as [step [Hstep Hvalidated]];
    cbn beta iota zeta in Hvalidated).
  injection Hvalidated as Heq. subst validation.
  cbn beta iota zeta delta [E.transaction_validation.effective_gas_price].
  match goal with
  | H : Result.when_ (Gas.op_lt ?price ?base) _ = inl _ |- _ ⇒
    destruct (Gas.op_lt price base) eqn:HtooLow; [cbn in H; discriminate H|]
  end.
  apply N.ltb_ge in HtooLow. exact HtooLow.
Qed.

Lemma validated_beneficiary_fee block tx validation :
  @E.validate_transaction F block tx = inl validation →
  balanceInvariant (B.t.world_state block) →
  let reward := @U.of_uint_exn (@Gas.op_star (T.gas_limit tx)
      (@Gas.op_minus (E.transaction_validation.effective_gas_price validation)
        (Chain.Ethereum.Block.Header.t.base_fee_per_gas (Chain.Ethereum.Block.t.header (B.t.current_block block))))) in
  0 ≤ U.to_z reward ≤ U.to_z (E.transaction_validation.total_fee validation).
Proof.
  intros Hvalidated Hinitial. cbn zeta.
  pose proof (validated_priority_price block tx validation Hvalidated) as Hprice.
  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 Htotal.
  pose proof (validated_debit_range block tx validation Hvalidated ltac:(cbn beta in Hbalance; lia))
    as [Hfee Hdebit].
  pose proof (N.mul_le_mono_l
    (E.transaction_validation.effective_gas_price validation -
      Chain.Ethereum.Block.Header.t.base_fee_per_gas (Chain.Ethereum.Block.t.header (B.t.current_block block)))
    (E.transaction_validation.effective_gas_price validation) (T.gas_limit tx)
    (N.le_sub_l _ _)) as Hreward.
  apply N2Z.inj_le in Hreward.
  assert (Hguard : N.leb (Chain.Ethereum.Block.Header.t.base_fee_per_gas (Chain.Ethereum.Block.t.header (B.t.current_block block)))
    (E.transaction_validation.effective_gas_price validation) = true) by
    (apply N.leb_le; exact Hprice).
  unfold Gas.op_minus, Support.UintN.op_minus. rewrite Hguard.
  change (0 ≤ U.to_z (@U.of_uint_exn (T.gas_limit tx × (E.transaction_validation.effective_gas_price validation -
      Chain.Ethereum.Block.Header.t.base_fee_per_gas (Chain.Ethereum.Block.t.header (B.t.current_block block))))%N) ≤
    U.to_z (E.transaction_validation.total_fee validation)).
  rewrite reservecheck.u256_of_uint by (cbn beta in Hbalance; lia).
  rewrite Hfee. split; [apply N2Z.is_nonneg|exact Hreward].
Qed.

This is the concrete beneficiary-credit operation after the VM. The supply deficit left by the upfront charge covers the credit. The VM conservation premise here is a composition obligation, not a VM axiom.
Lemma beneficiary_credit_reuses_fee block tx validation afterVm :
  @E.validate_transaction F block tx = inl validation →
  balanceInvariant (B.t.world_state block) →
  balanceInvariant afterVm →
  totalBalance afterVm ≤ totalBalance (B.t.world_state block) -
    U.to_z (E.transaction_validation.total_fee validation) →
  let reward := @U.of_uint_exn (@Gas.op_star (T.gas_limit tx)
      (@Gas.op_minus (E.transaction_validation.effective_gas_price validation)
        (Chain.Ethereum.Block.Header.t.base_fee_per_gas (Chain.Ethereum.Block.t.header (B.t.current_block block))))) in
  let credited := State.op_pointcaretdollarlparenrparenltminus afterVm
    (Lens.Infix.op_pipeminusminus (@W.account None
      (Chain.Ethereum.Block.Header.t.beneficiary (Chain.Ethereum.Block.t.header (B.t.current_block block)))) A.balance)
    (fun balance ⇒ @U.op_plus balance reward) in
  balanceInvariant credited ∧
  totalBalance credited ≤ totalBalance (B.t.world_state block) ∧
  ∀ query, U.to_z (A.t.balance (readAccount afterVm query)) ≤
    U.to_z (A.t.balance (readAccount credited query)).
Proof.
  intros Hvalidated Hinitial Hafter Hdebit reward credited.
  pose proof (validated_beneficiary_fee block tx validation Hvalidated Hinitial) as Hreward.
  fold reward in Hreward. cbn zeta in Hreward.
  assert (Hbudget : totalBalance afterVm + U.to_z reward < 2^256).
  { pose proof (proj2 (proj2 Hinitial)). lia. }
  unfold credited. split.
  - apply credit_preserves_invariant; [exact (proj1 Hafter)|exact (proj1 (proj2 Hafter))|lia|exact Hbudget].
  - split.
    + rewrite credit_total; [lia|exact (proj1 (proj2 Hafter))|lia|exact Hbudget].
    + intro query. apply credit_nondecreasing_from_supply;
        [exact (proj1 Hafter)|exact (proj1 (proj2 Hafter))|lia|exact Hbudget].
Qed.

Lemma authorization_preserves_balance_invariant state auth :
  balanceInvariant (S.t.world_state state) →
  balanceInvariant (S.t.world_state (snd (@E.process_authorization F state auth))).
Proof.
  intro Hinitial. unfold E.process_authorization.
  match goal with |- context [if ?b then _ else _] ⇒ destruct b end.
  { exact Hinitial. }
  match goal with |- context [match ?a with Some _ ⇒ _ | None ⇒ _ end] ⇒
    destruct a as [authority|] end.
  2: exact Hinitial.
  cbn beta iota zeta delta [S.t.with_accessed_addresses S.t.world_state].
  match goal with |- context [if ?b then _ else _] ⇒ destruct b end.
  2: exact Hinitial.
  match goal with |- context [State.op_pointcaretlparenrparenltminus _
    (@S.account None authority) ?value] ⇒ set (updated := value) end.
  assert (Hbalance : U.to_z (A.t.balance updated) =
    U.to_z (A.t.balance (readAccount (S.t.world_state state) authority))) by reflexivity.
  cbn beta in Hbalance.
  match goal with |- context [if ?b then _ else _] ⇒ destruct b end.
  all: change (balanceInvariant (State.op_pointcaretlparenrparenltminus
    (S.t.world_state state) (@W.account (Some false) authority) updated)).
  all: exact (proj1 (@account_write_preserves_supply false
    (S.t.world_state state) authority updated Hinitial Hbalance)).
Qed.

Lemma authorizations_preserve_balance_invariant auths authorities state :
  balanceInvariant (S.t.world_state state) →
  let result := Result.List.fold_left
    (fun '(authorities, current) auth ⇒
      match @E.process_authorization F current auth with
      | (None, next) ⇒ (authorities, next)
      | (Some authority, next) ⇒ (authority :: authorities, next)
      end) (authorities, state) auths in
  balanceInvariant (S.t.world_state (snd result)).
Proof.
  revert authorities state. induction auths as [|auth auths IH]; intros authorities state Hinitial.
  { exact Hinitial. }
  change (@Result.List.fold_left) with (@OCamlList.fold_left). cbn [OCamlList.fold_left].
  pose proof (authorization_preserves_balance_invariant state auth Hinitial) as Hnext.
  destruct (@E.process_authorization F state auth) as [[authority|] next]; cbn [snd] in Hnext;
    apply IH; exact Hnext.
Qed.

Upfront charging and all authorization entries establish the VM's supply budget. An authorization may create an empty account or change code, but cannot alter that budget. Validation supplies the debit-range premise.
Lemma transaction_entry_supply_with_nonce (nextNonce : A.t → Numeric.U64.t) state sender fee auths :
  balanceInvariant (S.t.world_state state) → 0 ≤ U.to_z fee →
  0 ≤ U.to_z (A.t.balance (readAccount (S.t.world_state state) sender)) - U.to_z fee < 2^256 →
  let charged := State.op_pointcaretdollarlparenrparenltminus state
    (@S.account None sender)
    (fun account ⇒ A.t.with_balance (@U.op_minus (A.t.balance account) fee)
      (A.t.with_nonce (nextNonce account) account)) in
  let result := Result.List.fold_left
    (fun '(authorities, current) auth ⇒
      match @E.process_authorization F current auth with
      | (None, next) ⇒ (authorities, next)
      | (Some authority, next) ⇒ (authority :: authorities, next)
      end) (nil, charged) auths in
  balanceInvariant (S.t.world_state (snd result)) ∧
  totalBalance (S.t.world_state (snd result)) = totalBalance (S.t.world_state state) - U.to_z fee.
Proof.
  intros Hinitial Hfee Hrange charged result.
  assert (Htotal : totalBalance (S.t.world_state charged) =
    totalBalance (S.t.world_state state) - U.to_z fee).
  { apply upfront_fee_total_with_nonce. exact Hrange. }
  assert (Hcharged : balanceInvariant (S.t.world_state charged)).
  { split.
    - apply account_write_wellformed. exact (proj1 Hinitial).
    - split.
      + apply account_write_nonnegative; [exact (proj1 (proj2 Hinitial))|].
        change (0 ≤ U.to_z (@U.op_minus (A.t.balance (readAccount (S.t.world_state state) sender)) fee)).
        rewrite reservecheck.u256_sub by exact Hrange. exact (proj1 Hrange).
      + rewrite Htotal. pose proof (proj2 (proj2 Hinitial)). lia. }
  split.
  - apply authorizations_preserve_balance_invariant. exact Hcharged.
  - change (totalBalance (S.t.world_state (snd result)) =
      totalBalance (S.t.world_state state) - U.to_z fee).
    unfold result. rewrite authorizations_preserve_supply. exact Htotal.
Qed.

Lemma transaction_entry_supply state sender fee auths :
  balanceInvariant (S.t.world_state state) → 0 ≤ U.to_z fee →
  0 ≤ U.to_z (A.t.balance (readAccount (S.t.world_state state) sender)) - U.to_z fee < 2^256 →
  let charged := State.op_pointcaretdollarlparenrparenltminus state
    (@S.account None sender)
    (fun account ⇒ A.t.with_balance (@U.op_minus (A.t.balance account) fee)
      (A.t.with_nonce (@Numeric.U64.op_plus (A.t.nonce account)
        (@Numeric.U64.one)) account)) in
  let result := Result.List.fold_left
    (fun '(authorities, current) auth ⇒
      match @E.process_authorization F current auth with
      | (None, next) ⇒ (authorities, next)
      | (Some authority, next) ⇒ (authority :: authorities, next)
      end) (nil, charged) auths in
  balanceInvariant (S.t.world_state (snd result)) ∧
  totalBalance (S.t.world_state (snd result)) = totalBalance (S.t.world_state state) - U.to_z fee.
Proof. apply (transaction_entry_supply_with_nonce (fun account ⇒
  Numeric.U64.op_plus (A.t.nonce account) Numeric.U64.one)). Qed.
End Fees.

Print Assumptions validated_beneficiary_fee.
Print Assumptions beneficiary_credit_reuses_fee.

Print Assumptions transaction_entry_supply.