Library QedkConcrete.consensustransaction

Concrete execution preserves an accepted transaction's budget

This is the transaction step of the window induction. Configuration agreement is a separate storage invariant; no balance or history effect is postulated for the VM. Emptying eligibility preserves the snapshot balance only while the account has no intervening sender/authority activity.
From Stdlib Require Import List String ZArith Lia Bool.
From stdpp Require Import decidable strings.
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.transactionchecks.
Require Import QedkConcrete.consensusdecisions.
Require Import QedkConcrete.nativeinputs.
Require Import QedkConcrete.stakingsystem.
Set Default Goal Selector "!".
#[local] Hint Extern 100 (Basics.Unreachable _) ⇒
  (apply Opaque.legacy_witness; Opaque.opaque_inhabit 64%nat) : typeclass_instances.
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 staking := (@MonadVM.Staking.staking_address).
Local Abbreviation systemSender := (@Chain.Monad.system_sender).

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))).

Theorem accepted_transaction_preserves_budget snapshot previous next signers until block tx validation post :
  (@E.Params F).(Execution.Make_Params_signature.revision) = Support.Variants.Revision.Eleven →
  keccak256OutputSize → @nativeOutputBounds args →
  wordRange (Support.UintN.to_z (Chain.Monad.PARAMS.chain_id params)) →
  headerWords (Chain.Ethereum.Block.t.header (B.t.current_block block)) →
  balanceInvariant (B.t.world_state block) → worldFormat (B.t.world_state block) →
  HistoryMatches snapshot (consensusHistory previous) (B.t.world_state block) signers until →
  (∀ a, List.In a signers → @A.is_smart_contract (readAccount snapshot a) = false) →
  (∀ a, List.In a signers → a ≠ native ∧ a ≠ staking) →
  Forall (fun auth ⇒ OCamlString.length (T.Authorization.t.address auth) = 20) (T.authorization_list tx) →
  transactionValueAndDataBounds tx →
  @E.validate_transaction F block tx = inl validation →
  processTransaction block tx = inl post →
  List.In (E.transaction_validation.sender validation) signers →
  E.transaction_validation.sender validation ≠ systemSender →
  (blockNumber block ≤ until < blockNumber block + 3)%N →
  (∀ a, List.In a signers → configured (B.t.world_state block) (blockNumber block) a =
    configured snapshot (blockNumber block) a) →
  (∀ a, List.In a signers →
    0 ≤ consensusBudgets previous a ∧
    consensusBudgets previous a ≤ balance (B.t.world_state block) a ∧
    consensusBudgets previous a ≤ Numeric.U256.to_z (configured snapshot (blockNumber block) a)) →
  (∀ a, List.In a signers → a ≠ systemSender → ∀ future, (future ≤ until)%N →
    @emptyingEligible (B.t.world_state block) a future = true →
    balance snapshot a ≤ balance (B.t.world_state block) a) →
  checkTransaction snapshot (Chain.Ethereum.Block.t.header (B.t.current_block block)) previous tx = Some next →
  let '(msg, entry) := vmInput block tx validation in
  (∀ a, List.In a signers →
    ¬ List.In a (S.t.accounts_created_in_current_transaction (snd (callFromEoa tx msg entry)))) →
  HistoryMatches snapshot (consensusHistory next) (B.t.world_state post) signers until ∧
  (∀ a, List.In a signers →
    0 ≤ consensusBudgets next a ∧
    consensusBudgets next a ≤ balance (B.t.world_state post) a ∧
    consensusBudgets next a ≤ Numeric.U256.to_z (configured snapshot (blockNumber block) a)) ∧
  (∀ a, List.In a signers → a ≠ systemSender → ∀ future, (future ≤ until)%N →
    @emptyingEligible (B.t.world_state post) a future = true →
    balance snapshot a ≤ balance (B.t.world_state post) a).
Proof using F U8 UB UI0.
  intros Hrevision Hhash HnativeOutput Hchain Hheader Hbalance Hformat Hhistory Hsnapshot Hnative
    Hwidth Hvalue Hvalidation Hrun Hsender HnotSys Htime Hconfig Hbudgets Hidle Hcheck.
  unfold checkTransaction in Hcheck.
  pose proof (@validated_sender_recovered F block tx validation Hvalidation) as Hsender_rec.
  change (T.sender _ tx) with (@transactionSender F tx) in Hsender_rec.
  rewrite Hsender_rec, (bool_decide_eq_false_2 _ HnotSys) in Hcheck.
  rewrite (validated_transaction_quote block tx validation Hvalidation) in Hcheck.
  set (sender := E.transaction_validation.sender validation) in ×.
  match type of Hcheck with context [transactionChecks ?h ?t ?n] ⇒
    destruct (transactionChecks h t n) end; [|discriminate].
  destruct (advanceHistory snapshot (consensusHistory previous) sender tx)
    as [authorities history] eqn:Hadvance.
  rewrite bool_decide_not_in in Hcheck.
  match type of Hcheck with context [checkSenderBudget ?e ?b ?t ?p ?f ?v] ⇒
    destruct (checkSenderBudget e b t p f v) as [remaining|] eqn:Haccepted
  end; [|discriminate].
  injection Hcheck as <-.
  pose proof (@history_current_emptying F snapshot (consensusHistory previous)
    signers until block tx validation Hhistory Hsnapshot Hwidth Hsender HnotSys (proj1 Htime)) as Hempty.
  cbn zeta in Hempty. fold sender in Hempty. rewrite Hadvance in Hempty. cbn beta iota zeta delta [fst snd] in Hempty.
  assert (HidleSender : historyAllowsEmptying snapshot (consensusHistory previous) sender (blockNumber block) &&
    negb (existsb (fun a ⇒ String.eqb a sender) authorities) = true →
    balance snapshot sender ≤ balance (B.t.world_state block) sender).
  { intro Hallowed. apply Bool.andb_true_iff in Hallowed as [Hallowed _].
    apply (Hidle sender Hsender HnotSys (blockNumber block) (proj1 Htime)).
    rewrite (history_emptying_agreement _ _ _ _ _ Hhistory sender Hsender HnotSys _ (proj1 Htime)).
    exact Hallowed. }
  assert (HactualBudgets : ∀ a, List.In a signers →
    0 ≤ consensusBudgets previous a ∧
    consensusBudgets previous a ≤ balance (B.t.world_state block) a ∧
    consensusBudgets previous a ≤ Numeric.U256.to_z (configured (B.t.world_state block) (blockNumber block) a)).
  { intros a Ha. rewrite Hconfig by exact Ha. apply Hbudgets, Ha. }
  change (checkSenderBudget
    (historyAllowsEmptying snapshot (consensusHistory previous) sender (blockNumber block) &&
      negb (existsb (fun a ⇒ String.eqb a sender) authorities))
    (balance snapshot sender) (Numeric.U256.to_z (configured snapshot (blockNumber block) sender))
    (consensusBudgets previous sender)
    (Z.of_N (T.gas_limit tx × E.transaction_validation.effective_gas_price validation))
    (Numeric.U256.to_z (T.value tx)) = Some remaining) in Haccepted.
  pose proof (eq_ind_r (fun level ⇒ checkSenderBudget
    (historyAllowsEmptying snapshot (consensusHistory previous) sender (blockNumber block) &&
      negb (existsb (fun a ⇒ String.eqb a sender) authorities))
    (balance snapshot sender) (Numeric.U256.to_z level)
    (consensusBudgets previous sender)
    (Z.of_N (T.gas_limit tx × E.transaction_validation.effective_gas_price validation))
    (Numeric.U256.to_z (T.value tx)) = Some remaining)
    Haccepted (Hconfig sender Hsender)) as HactualAccepted.
  pose proof (@process_transaction_budget_map F UI0 UB U8
    block tx validation post signers
    (historyAllowsEmptying snapshot (consensusHistory previous) sender (blockNumber block) &&
      negb (existsb (fun a ⇒ String.eqb a sender) authorities)) (balance snapshot sender)
    (consensusBudgets previous) remaining Hrevision Hhash HnativeOutput Hchain Hheader Hbalance Hformat
    Hwidth Hvalue Hvalidation Hrun Hsender
    (fun a Ha ⇒ not_eq_sym (proj2 (Hnative a Ha)))
    (history_noncontract _ _ _ _ _ Hhistory)
    HactualBudgets HactualAccepted HidleSender) as HbudgetStep.
  pose proof (@process_transaction_history_projection F
    snapshot (consensusHistory previous) signers until
    block tx validation post Hrevision Hhistory Hsnapshot
    (fun a Ha ⇒ proj1 (Hnative a Ha)) Hwidth Hvalidation Hrun
    (proj2 Htime)) as HhistoryStep.
  fold sender in HhistoryStep. rewrite Hadvance in HhistoryStep.
  destruct (vmInput block tx validation) as [msg entry] eqn:Hinput.
  cbn beta iota zeta delta [fst snd] in Hempty, HbudgetStep, HhistoryStep |- ×.
  intro Hcreated. split; [exact (HhistoryStep Hcreated)|]. split.
  - intros a Ha.
    specialize (HbudgetStep (eq_sym Hempty) Hcreated a Ha).
    cbn [consensusBudgets]. rewrite !bool_decide_string_eq.
    rewrite Hconfig in HbudgetStep by exact Ha. exact HbudgetStep.
  - intros a Ha HnotSys_a future Hfuture Heligible.
    pose proof (@process_transaction_future_emptying F
      block tx validation post a future Hrevision
      (history_accounts_wf _ _ _ _ _ Hhistory) (history_counters_wf _ _ _ _ _ Hhistory)
      Hvalidation Hrun ltac:(lia)) as HeligibleStep.
    rewrite Hinput in HeligibleStep. specialize (HeligibleStep (Hcreated a Ha)).
    rewrite Heligible in HeligibleStep. symmetry in HeligibleStep.
    apply Bool.andb_true_iff in HeligibleStep as [Hbefore _].
    specialize (Hidle a Ha HnotSys_a future Hfuture Hbefore).
    unfold emptyingEligible in Heligible. apply Bool.andb_true_iff in Heligible as [Hdel Hcounter].
    apply Bool.negb_true_iff in Hdel. apply N.leb_le in Hcounter.
    pose proof (@process_transaction_future_emptying_nondebit F UI0 UB U8
      block tx validation post a future
      (not_eq_sym (proj2 (Hnative a Ha)))
      Hrevision Hhash
      HnativeOutput Hchain Hheader Hbalance Hformat (history_counters_wf _ _ _ _ _ Hhistory)
      Hwidth Hvalue Hvalidation Hrun
      (history_noncontract _ _ _ _ _ Hhistory a Ha)
      ltac:(lia) Hcounter Hdel) as Hnondebit.
    rewrite Hinput in Hnondebit. specialize (Hnondebit (Hcreated a Ha)).
    eapply Z.le_trans; [exact Hidle|exact Hnondebit].
Qed.

Monad system transactions preserve all user gas budgets and non-staking balances while advancing systemSender's nonce in consensusHistory.
Theorem accepted_system_transaction_preserves_budget snapshot previous next signers until block tx post :
  HistoryMatches snapshot (consensusHistory previous) (B.t.world_state block) signers until →
  (∀ a, List.In a signers → a ≠ staking) →
  transactionSender tx = Some systemSender →
  @E.process_monad_system_transaction F block tx = inl post →
  (∀ a, List.In a signers →
    0 ≤ consensusBudgets previous a ∧
    consensusBudgets previous a ≤ balance (B.t.world_state block) a ∧
    consensusBudgets previous a ≤ Numeric.U256.to_z (configured snapshot (blockNumber block) a)) →
  (∀ a, List.In a signers → a ≠ systemSender → ∀ future, (future ≤ until)%N →
    @emptyingEligible (B.t.world_state block) a future = true →
    balance snapshot a ≤ balance (B.t.world_state block) a) →
  checkTransaction snapshot (Chain.Ethereum.Block.t.header (B.t.current_block block)) previous tx = Some next →
  HistoryMatches snapshot (consensusHistory next) (B.t.world_state post) signers until ∧
  (∀ a, List.In a signers →
    0 ≤ consensusBudgets next a ∧
    consensusBudgets next a ≤ balance (B.t.world_state post) a ∧
    consensusBudgets next a ≤ Numeric.U256.to_z (configured snapshot (blockNumber block) a)) ∧
  (∀ a, List.In a signers → a ≠ systemSender → ∀ future, (future ≤ until)%N →
    @emptyingEligible (B.t.world_state post) a future = true →
    balance snapshot a ≤ balance (B.t.world_state post) a).
Proof.
  intros Hhistory HnotStaking Hsender Hrun Hbudgets Hidle Hcheck.
  unfold checkTransaction in Hcheck.
  rewrite Hsender, (bool_decide_eq_true_2 _ eq_refl) in Hcheck.
  destruct (systemTransactionChecks tx _) as [|]; [|discriminate Hcheck].
  destruct (advanceHistory snapshot (consensusHistory previous) systemSender tx)
    as [authorities history] eqn:Hadvance.
  injection Hcheck as <-.
  pose proof (@process_monad_system_transaction_history_projection F
    snapshot (consensusHistory previous) signers until block tx post Hhistory Hrun) as HhistoryStep.
  rewrite Hadvance in HhistoryStep.
  cbn [snd consensusHistory consensusBudgets] in HhistoryStep |- ×.
  destruct (@process_monad_system_transaction_frame F block tx post
    (history_accounts_wf _ _ _ _ _ Hhistory) Hrun)
    as [_ [_ [Hcounters [_ [Hcode [_ [_ [Hbal _]]]]]]]].
  split; [exact HhistoryStep|]. split.
  - intros a Ha.
    rewrite (Hbal a (not_eq_sym (HnotStaking a Ha))).
    exact (Hbudgets a Ha).
  - intros a Ha HnotSys future Hfuture Heligible.
    rewrite (Hbal a (not_eq_sym (HnotStaking a Ha))).
    apply (Hidle a Ha HnotSys future Hfuture).
    unfold emptyingEligible in Heligible |- ×. cbv beta in Heligible |- ×.
    rewrite <- (Hcode a), <- (read_counter_same_map _ _ a Hcounters).
    exact Heligible.
Qed.
Initialize numerical backing from the executed snapshot itself, not from a supplied solvency invariant. The full window proof also initializes nonce/activity correspondence with initial_history_matches.
Lemma initial_budget_backed snapshot height :
  balanceInvariant snapshot → worldFormat snapshot →
  ∀ a,
    0 ≤ consensusBudgets (initialConsensusState snapshot height) a ∧
    consensusBudgets (initialConsensusState snapshot height) a ≤ balance snapshot a ∧
    consensusBudgets (initialConsensusState snapshot height) a ≤
      Numeric.U256.to_z (configured snapshot height a).
Proof.
  intros Hbalance Hformat a.
  pose proof (@account_balance_bounded_by_supply snapshot a (proj1 (proj2 Hbalance))) as Hbal.
  pose proof (@configured_word_range
    snapshot height a Hformat) as Hlevel.
  change (0 ≤ Z.min (balance snapshot a) (Numeric.U256.to_z (configured snapshot height a)) ∧
    Z.min (balance snapshot a) (Numeric.U256.to_z (configured snapshot height a)) ≤ balance snapshot a ∧
    Z.min (balance snapshot a) (Numeric.U256.to_z (configured snapshot height a)) ≤
      Numeric.U256.to_z (configured snapshot height a)).
  unfold wordRange in Hlevel. cbv beta in Hbal. lia.
Qed.
End Execution.