Library QedkConcrete.consensusboundaries

Budget backing across non-transaction block operations

The reserve check does not guard system calls or withdrawals. Prescribed system bytecode prevents the former from spending signer balances; the aggregate credit bound prevents the latter from wrapping U256. Neither operation gives consensus a fresh fee budget.
From Stdlib Require Import List String ZArith Lia Bool.
Require Import RocqOfOCaml.RocqOfOCaml.
Require Import MonadVM.Execution QedkConcrete.consensushistory
  QedkConcrete.consensushistoryproofs
  QedkConcrete.transactionnonces QedkConcrete.authorizationnonces
  QedkConcrete.transactionstage QedkConcrete.transactioncode
  QedkConcrete.transactionhistory QedkConcrete.authorizationcode
  QedkConcrete.transactionpersistentcode QedkConcrete.transactionchecked
  QedkConcrete.publictransaction QedkConcrete.protectedstorageboundary.
Require Import QedkConcrete.consensuswindow QedkConcrete.consensuswindowproofs
  QedkConcrete.consensusbudget QedkConcrete.transactionbudget
  QedkConcrete.transactionhistoryprojection QedkConcrete.supply
  QedkConcrete.assemblychecked QedkConcrete.hashwords QedkConcrete.wordrange
  QedkConcrete.hostaccountformat.
Require Import QedkConcrete.consensustransaction QedkConcrete.consensusbacking
  QedkConcrete.transactionformat.
Require Import QedkConcrete.blockhistoryprojection QedkConcrete.systemmessages
  QedkConcrete.systemcode QedkConcrete.blockpersistentcode QedkConcrete.credits
  QedkConcrete.reservewrites.
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 Addr := Chain.Ethereum.Address.

Section Execution.
Context {F : E.FArgs}.

Local Abbreviation args := (@E.Instantiation_fargs F).
Local Abbreviation params := args.(Host.Instantiate.ChainParams).

Context {UI0 : Basics.Unreachable Support.UintN.Impl.t}
  {UB : Basics.Unreachable Bloom.t}
  {U8 : Basics.Unreachable Byte_string.B8.(Byte_string.Fixed.Fixed_result.t)}.

Local Abbreviation prefix := (@transactionPrefix F).
Local Abbreviation vmInput := (@transactionVmInput F).
Local Abbreviation callFromEoa := (@E.Host.call_from_eoa F).
Local Abbreviation processTransaction := (@E.process_transaction F).
Local Abbreviation readAccount := (fun world addr ⇒ State.op_pointcaretlparenrparen
  world (@W.account None addr)).

Local Abbreviation native := (@Reserve_balance.precompile_address).

Local Abbreviation recoveredAuthority := (@Chain.Ethereum.Transaction.Authorization.authority).

Local Abbreviation blockNumber := (fun block ⇒ Chain.Ethereum.Block.Header.t.number
  (Chain.Ethereum.Block.t.header (B.t.current_block block))).
Local Abbreviation emptying := (@Reserve_balance.is_emptying_tx).

Local Abbreviation configured := (@Reserve_balance.user_reserve_balance).
Local Abbreviation balance := (fun world a ⇒ Numeric.U256.to_z (A.t.balance (readAccount world a))).

Local Abbreviation systemMessage := (@E.process_system_message F).
Local Abbreviation activate := (@Reserve_balance.activate_precompile).

Lemma initial_backing snapshot signers height until :
  balanceInvariant snapshot → worldFormat snapshot →
  stringmaps.WellFormed (W.t.next_emptying_transaction_block snapshot) →
  (∀ a, List.In a signers → @A.is_smart_contract (readAccount snapshot a) = false) →
  BudgetBacking snapshot (initialConsensusState snapshot height) snapshot signers height until.
Proof using F

   .
  intros Hbalance Hformat Hcounters Hnoncontract. constructor.
  - apply initial_history_matches; [exact (proj1 Hbalance)|exact Hcounters|exact Hnoncontract].
  - intros a _. apply (@initial_budget_backed
      snapshot height Hbalance Hformat a).
  - intros. apply Z.le_refl.
Qed.

Lemma activation_backing snapshot consensus world signers height until :
  (∀ a, List.In a signers → a ≠ native) →
  BudgetBacking snapshot consensus world signers height until →
  BudgetBacking snapshot consensus (activate world) signers height until.
Proof.
  intros Hnative Hbacked.
  pose proof (backing_history _ _ _ _ _ _ Hbacked) as Hhistory.
  apply (backing_nondebit _ _ _ _ _ _ _ Hbacked).
  - apply (activate_history); assumption.
  - intros a _. rewrite activate_preserves_balances;
      [apply Z.le_refl|exact (history_accounts_wf _ _ _ _ _ Hhistory)].
Qed.

The system address need not be separately excluded from the signer list: its prescribed code makes it a contract, whereas the history invariant already says tracked signer accounts are not contracts.
Theorem system_message_backing snapshot consensus signers height until block addr data :
  (@E.Params F).(Execution.Make_Params_signature.revision) = Support.Variants.Revision.Eleven →
  keccak256OutputSize → wordRange (Support.UintN.to_z (Chain.Monad.PARAMS.chain_id params)) →
  headerWords (Chain.Ethereum.Block.t.header (B.t.current_block block)) →
  OCamlString.length data < 2^62 →
  balanceInvariant (B.t.world_state block) → worldFormat (B.t.world_state block) →
  (A.t.code (readAccount (B.t.world_state block) addr) = beaconRootsCode ∨
   A.t.code (readAccount (B.t.world_state block) addr) = historyStorageCode) →
  (∀ a, List.In a signers → a ≠ native) →
  BudgetBacking snapshot consensus (B.t.world_state block) signers height until →
  BudgetBacking snapshot consensus
    (B.t.world_state (snd (systemMessage block addr data))) signers height until.
Proof.
  intros Hrevision Hhash Hchain Hheader Hdata Hbalance Hformat Hcode Hnative Hbacked.
  pose proof (backing_history _ _ _ _ _ _ Hbacked) as Hhistory.
  assert (Hfree : callFreeBytes (A.t.code (readAccount (B.t.world_state block) addr)) = true).
  { destruct Hcode as [Hcode|Hcode]; rewrite Hcode;
      [apply beaconRootsCode_call_free|apply historyStorageCode_call_free]. }
  pose proof (@system_message_history F
    snapshot (consensusHistory consensus) signers until block addr data
    Hrevision Hhash Hchain Hheader Hdata Hbalance Hformat Hfree Hnative Hhistory) as HafterHistory.
  destruct (@system_message_prescribed_code F
    block addr data Hrevision Hhash Hchain Hheader Hdata
    Hbalance Hformat Hcode) as [_ [_ [_ Hnondebit]]].
  apply (backing_nondebit _ _ _ _ _ _ _ Hbacked HafterHistory).
  intros a Ha. apply Hnondebit. intro Heq. subst a.
  pose proof (@prescribed_code_is_contract (readAccount (B.t.world_state block) addr) Hcode) as Hcontract.
  pose proof (eq_trans (eq_sym (history_noncontract _ _ _ _ _ Hhistory addr Ha)) Hcontract)
    as Hfalse. discriminate.
Qed.

Theorem withdrawals_backing snapshot consensus signers height until block withdrawals :
  BudgetBacking snapshot consensus (B.t.world_state block) signers height until →
  balanceInvariant (B.t.world_state block) →
  Forall (fun wd ⇒ 0 ≤ Numeric.U64.to_z (Chain.Ethereum.Withdrawal.t.amount wd) < 2^64) withdrawals →
  totalBalance (B.t.world_state block) +
    Lists.List.fold_right (fun wd total ⇒ withdrawalWei wd + total) 0 withdrawals < 2^256 →
  BudgetBacking snapshot consensus
    (B.t.world_state (Result.List.fold_left (@E.process_withdrawal F) block withdrawals)) signers height until.
Proof.
  intros Hbacked Hbalance Hwidth Hsupply.
  pose proof (backing_history _ _ _ _ _ _ Hbacked) as Hhistory.
  pose proof (@withdrawals_history F snapshot (consensusHistory consensus) signers until block withdrawals Hhistory)
    as HafterHistory.
  destruct (@withdrawals_preserve_supply_bound F block withdrawals Hbalance Hwidth Hsupply)
    as [_ [_ Hnondebit]].
  apply (backing_nondebit _ _ _ _ _ _ _ Hbacked HafterHistory).
  intros a _. exact (Hnondebit a).
Qed.
End Execution.