Library QedkConcrete.consensuswindowproofs

Fee-check and history components of the window induction

From Stdlib Require Import List String ZArith Bool Lia.
From stdpp Require Import decidable strings.
Require Import RocqOfOCaml.RocqOfOCaml.
Require Import MonadVM.Execution QedkConcrete.consensuswindow
  QedkConcrete.consensushistory QedkConcrete.consensusbudget
  QedkConcrete.publictransaction QedkConcrete.transactionhistoryprojection
  QedkConcrete.transactioncode QedkConcrete.transactionchecksproofs
  QedkConcrete.stakingsystem.
Require Import QedkConcrete.transactionchecks.
Set Default Goal Selector "!".
Import ListNotations.
Local Open Scope Z_scope.
Module E := Execution.Make.
Module B := State.BlockState.
Module W := State.WorldState.
Module A := Chain.Ethereum.Account.
Module T := Chain.Ethereum.Transaction.
Module Addr := Chain.Ethereum.Address.
Module Block := Chain.Ethereum.Block.

Section Consensus.
Context {F : E.FArgs}.
Local Abbreviation readAccount := (fun world addr ⇒
  State.op_pointcaretlparenrparen world (@W.account None addr)).
Local Abbreviation balance := (fun world a ⇒ Numeric.U256.to_z (A.t.balance (readAccount world a))).

Lemma check_transaction_history snapshot header previous tx next :
  checkTransaction snapshot header previous tx = Some next →
  ∃ sender, transactionSender tx = Some sender ∧
    consensusHistory next = snd (advanceHistory snapshot (consensusHistory previous) sender tx).
Proof.
  unfold checkTransaction.
  destruct (transactionSender tx) as [sender|] eqn:Hsender; [|discriminate].
  destruct (bool_decide (sender = Chain.Monad.system_sender)).
  - destruct (systemTransactionChecks tx _); [|discriminate].
    destruct (advanceHistory snapshot (consensusHistory previous) sender tx)
      as [authorities history] eqn:Hadvance.
    intro Hnext. injection Hnext as <-.
    ∃ sender. split; [reflexivity|rewrite Hadvance; reflexivity].
  - destruct (transactionQuote header tx) as [[_ fee]|]; [|discriminate].
    match goal 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.
    match goal with |- context [checkSenderBudget ?e ?b ?t ?p ?f ?v] ⇒
      destruct (checkSenderBudget e b t p f v) as [remaining|]
    end; [|discriminate].
    intro Hnext. injection Hnext as <-.
    ∃ sender. split; [reflexivity|rewrite Hadvance; reflexivity].
Qed.

Lemma check_transactions_history snapshot header previous txs next :
  checkTransactions snapshot header previous txs = Some next →
  scanHistory snapshot (consensusHistory previous) txs = Some (consensusHistory next).
Proof.
  revert previous. induction txs as [|tx rest IH]; intros previous Hcheck.
  - cbn [checkTransactions] in Hcheck. injection Hcheck as <-. reflexivity.
  - cbn [checkTransactions] in Hcheck.
    destruct (checkTransaction snapshot header previous tx) as [middle|] eqn:Htx; [|discriminate].
    destruct (check_transaction_history _ _ _ _ _ Htx) as [sender [Hsender Hhistory]].
    cbn [scanHistory]. rewrite Hsender, <- Hhistory. apply IH. exact Hcheck.
Qed.

Lemma check_transactions_app snapshot header previous prefix suffix :
  checkTransactions snapshot header previous (prefix ++ suffix) =
    match checkTransactions snapshot header previous prefix with
    | Some middle ⇒ checkTransactions snapshot header middle suffix
    | None ⇒ None
    end.
Proof.
  revert previous. induction prefix as [|tx prefix IH]; intro previous; [reflexivity|].
  cbn [app checkTransactions]. destruct (checkTransaction snapshot header previous tx) as [next|];
    [apply IH|reflexivity].
Qed.

Lemma check_blocks_app snapshot previous prefix suffix :
  checkBlocks snapshot previous (prefix ++ suffix) =
    match checkBlocks snapshot previous prefix with
    | Some middle ⇒ checkBlocks snapshot middle suffix
    | None ⇒ None
    end.
Proof.
  revert previous. induction prefix as [|block prefix IH]; intro previous; [reflexivity|].
  cbn [app checkBlocks].
  match goal with |- context [checkTransactions ?s ?h ?p ?txs] ⇒
    destruct (checkTransactions s h p txs) as [next|]
  end; [apply IH|reflexivity].
Qed.

A setting becoming effective during otherwise empty blocks must not restore fees already consumed in the unknown window. This holds for arbitrary increases and decreases, not just monotone configurations.
Lemma empty_blocks_do_not_refill snapshot blocks previous next :
  Forall (fun block ⇒ Block.t.transactions block = []) blocks →
  checkBlocks snapshot previous blocks = Some next →
  ∀ a, consensusBudgets next a ≤ consensusBudgets previous a.
Proof.
  intro Hempty. revert previous next.
  induction Hempty as [|block blocks Htxs Hblocks IH]; intros previous next Hcheck a.
  - cbn [checkBlocks] in Hcheck. injection Hcheck as <-. lia.
  - cbn [checkBlocks] in Hcheck. rewrite Htxs in Hcheck.
    cbn [checkTransactions] in Hcheck.
    specialize (IH _ _ Hcheck a). cbn [consensusBudgets] in IH. lia.
Qed.

This is the pre-validation step of the induction for user EOA transactions, not its final theorem. The maintained lower bounds suffice even though the candidate has not executed and successful validation has not been assumed.
Theorem check_transaction_fee_backed snapshot previous next signers until state tx :
  HistoryMatches snapshot (consensusHistory previous) (B.t.world_state state) signers until →
  (∀ sender, transactionSender tx = Some sender → List.In sender signers) →
  transactionSender tx ≠ Some Chain.Monad.system_sender →
  (Block.Header.t.number (Block.t.header (B.t.current_block state)) ≤ until)%N →
  (∀ a, List.In a signers → consensusBudgets previous a ≤ balance (B.t.world_state state) a) →
  (∀ a, List.In a signers → a ≠ Chain.Monad.system_sender →
    @emptyingEligible (B.t.world_state state) a
      (Block.Header.t.number (Block.t.header (B.t.current_block state))) = true →
    balance snapshot a ≤ balance (B.t.world_state state) a) →
  checkTransaction snapshot (Block.t.header (B.t.current_block state)) previous tx = Some next →
  ∃ sender fee,
    transactionQuote (Block.t.header (B.t.current_block state)) tx = Some (sender, fee) ∧
    Z.of_N fee ≤ balance (B.t.world_state state) sender.
Proof.
  intros Hhistory Hsigners HnotSysTx Htime Hbudget Hempty Hcheck.
  unfold checkTransaction in Hcheck.
  destruct (transactionSender tx) as [sender|] eqn:Hsender; [|discriminate].
  assert (HnotSys : sender ≠ Chain.Monad.system_sender).
  { intro Heq; subst sender; exact (HnotSysTx eq_refl). }
  rewrite (bool_decide_eq_false_2 _ HnotSys) in Hcheck.
  destruct (transactionQuote (Block.t.header (B.t.current_block state)) tx)
    as [[qsender fee]|] eqn:Hquote; [|discriminate].
  assert (Hq : qsender = sender).
  { unfold transactionQuote in Hquote. rewrite Hsender in Hquote.
    destruct (Gas.tx_effective_gas_price _ tx); [|discriminate].
    injection Hquote as <- _. reflexivity. }
  subst qsender.
  match type of Hcheck with context [transactionChecks ?h ?t ?n] ⇒
    destruct (transactionChecks h t n) end; [|discriminate].
  specialize (Hsigners sender eq_refl).
  destruct (advanceHistory snapshot (consensusHistory previous) sender tx) as [authorities history].
  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].
  ∃ sender, fee. split; [reflexivity|].
  eapply accepted_sender_fee; [exact Haccepted|exact (Hbudget sender Hsigners)|].
  intro Heligible. apply Bool.andb_true_iff in Heligible as [Heligible _].
  apply Hempty; [exact Hsigners|exact HnotSys|].
  rewrite (history_emptying_agreement _ _ _ _ _ Hhistory sender Hsigners HnotSys _ Htime).
  exact Heligible.
Qed.

Theorem check_transaction_no_insufficient_balance snapshot previous next signers until state tx :
  HistoryMatches snapshot (consensusHistory previous) (B.t.world_state state) signers until →
  (∀ sender, transactionSender tx = Some sender → List.In sender signers) →
  transactionSender tx ≠ Some Chain.Monad.system_sender →
  (Block.Header.t.number (Block.t.header (B.t.current_block state)) ≤ until)%N →
  (∀ a, List.In a signers → consensusBudgets previous a ≤ balance (B.t.world_state state) a) →
  (∀ a, List.In a signers → a ≠ Chain.Monad.system_sender →
    @emptyingEligible (B.t.world_state state) a
      (Block.Header.t.number (Block.t.header (B.t.current_block state))) = true →
    balance snapshot a ≤ balance (B.t.world_state state) a) →
  checkTransaction snapshot (Block.t.header (B.t.current_block state)) previous tx = Some next →
  ∀ details, @E.validate_transaction F state tx ≠
    @E.invalid_transaction F E.transaction_validation (B.t.current_block state) tx
      (Execution.Error.Insufficient_balance details).
Proof.
  intros Hhistory Hsigners HnotSysTx Htime Hbudget Hempty Hcheck.
  destruct (check_transaction_fee_backed snapshot previous next signers until state tx
    Hhistory Hsigners HnotSysTx Htime Hbudget Hempty Hcheck) as [sender [fee [Hquote Hbalance]]].
  exact (affordable_quote_no_insufficient_balance state tx sender fee Hquote Hbalance).
Qed.

The history invariant supplies the actual pre-transaction nonce and code eligibility. Only the fee bound comes from the reserve-balance argument.
Theorem check_transaction_validates snapshot previous next signers until state tx sender fee :
  HistoryMatches snapshot (consensusHistory previous) (B.t.world_state state) signers until →
  (∀ a, List.In a signers →
    @A.is_smart_contract (readAccount snapshot a) = false) →
  (∀ a, transactionSender tx = Some a → List.In a signers) →
  sender ≠ Chain.Monad.system_sender →
  transactionQuote (Block.t.header (B.t.current_block state)) tx = Some (sender, fee) →
  Z.of_N fee ≤ balance (B.t.world_state state) sender →
  checkTransaction snapshot (Block.t.header (B.t.current_block state)) previous tx = Some next →
  (@E.sender_kind_value F tx = Some (E.EOA sender) ∧
   @E.is_system_transaction F tx = false ∧
   ∀ blk, @E.dispatch_transaction F blk tx = @E.process_transaction F blk tx) ∧
  ∃ validation, @E.validate_transaction F state tx = inl validation.
Proof.
  intros Hhistory Hsnapshot Hsenders HnotSys Hquote Hfee Hcheck.
  assert (Hsender_rec : transactionSender tx = Some sender).
  { unfold transactionQuote in Hquote.
    destruct (transactionSender tx); [|discriminate].
    destruct (Gas.tx_effective_gas_price _ tx); [|discriminate].
    injection Hquote as <- _. reflexivity. }
  assert (Hsender : List.In sender signers) by exact (Hsenders sender Hsender_rec).
  unfold checkTransaction in Hcheck.
  rewrite Hsender_rec, (bool_decide_eq_false_2 _ HnotSys), Hquote in Hcheck.
  match type of Hcheck with context [transactionChecks ?h ?t ?n] ⇒
    destruct (transactionChecks h t n) eqn:Hchecks end; [|discriminate].
  split.
  - exact (@user_sender_dispatch_eq F
      (Block.t.header (B.t.current_block state)) tx sender
      (historyNonces (consensusHistory previous) sender)
      Hsender_rec HnotSys Hchecks).
  - eapply transaction_checks_validate; [exact Hquote| | |exact Hfee].
    + rewrite (history_nonce_agreement _ _ _ _ _ Hhistory sender Hsender).
      exact Hchecks.
    + exact (history_noncontract _ _ _ _ _ Hhistory sender Hsender).
Qed.

Consensus-accepted Monad system transactions always dispatch through process_monad_system_transaction.
Lemma check_system_transaction_dispatch snapshot previous next header tx :
  transactionSender tx = Some Chain.Monad.system_sender →
  checkTransaction snapshot header previous tx = Some next →
  @E.sender_kind_value F tx = Some E.System ∧
  @E.is_system_transaction F tx = true ∧
  ∀ blk, @E.dispatch_transaction F blk tx = @E.process_monad_system_transaction F blk tx.
Proof.
  intros Hsender Hcheck.
  unfold checkTransaction in Hcheck.
  rewrite Hsender, (bool_decide_eq_true_2 _ eq_refl) in Hcheck.
  destruct (systemTransactionChecks tx
    (historyNonces (consensusHistory previous) Chain.Monad.system_sender)) eqn:Hchecks;
    [|discriminate Hcheck].
  exact (@system_sender_dispatch_eq F tx
    (historyNonces (consensusHistory previous) Chain.Monad.system_sender)
    Hsender Hchecks).
Qed.

Consensus-accepted Monad system transactions validate and execute via process_monad_system_transaction whenever validStakingSyscall holds.
Theorem check_system_transaction_executes snapshot previous next signers until state tx :
  HistoryMatches snapshot (consensusHistory previous) (B.t.world_state state) signers until →
  List.In Chain.Monad.system_sender signers →
  transactionSender tx = Some Chain.Monad.system_sender →
  validStakingSyscall (B.t.world_state state) tx →
  checkTransaction snapshot (Block.t.header (B.t.current_block state)) previous tx = Some next →
  (@E.sender_kind_value F tx = Some E.System ∧
   @E.is_system_transaction F tx = true ∧
   ∀ blk, @E.dispatch_transaction F blk tx = @E.process_monad_system_transaction F blk tx) ∧
  ∃ post, @E.process_monad_system_transaction F state tx = inl post.
Proof.
  intros Hhistory HsenderIn Hsender HvalidSys Hcheck.
  unfold checkTransaction in Hcheck.
  rewrite Hsender, (bool_decide_eq_true_2 _ eq_refl) in Hcheck.
  destruct (systemTransactionChecks tx
    (historyNonces (consensusHistory previous) Chain.Monad.system_sender)) eqn:Hchecks;
    [|discriminate Hcheck].
  split.
  - exact (@system_sender_dispatch_eq F tx
      (historyNonces (consensusHistory previous) Chain.Monad.system_sender)
      Hsender Hchecks).
  - apply (@system_transaction_validates_and_executes F state tx).
    + exact (history_accounts_wf _ _ _ _ _ Hhistory).
    + rewrite (history_nonce_agreement _ _ _ _ _ Hhistory _ HsenderIn).
      exact Hchecks.
    + exact (history_noncontract _ _ _ _ _ Hhistory _ HsenderIn).
    + exact HvalidSys.
Qed.
End Consensus.