Library QedkConcrete.miscblocktests

Header and aggregate-gas regressions

These examples test miscBlockChecks, not transaction signatures or account budgets. The final example runs the concrete empty-block executor, rather than merely checking a header.
From Stdlib Require Import List NArith ZArith Lia.
From stdpp Require Import decidable.
Require Import QedkConcrete.miscblockproofs QedkConcrete.concreteexecutor
  QedkConcrete.reserveconfigfixture.
Import ListNotations.
Set Default Goal Selector "!".

Definition testHeader (height : N) : Header.t :=
  Header.t.with_number height
    (Header.t.with_gas_limit 150000%N
      (Header.t.with_ommers_hash (Crypto.keccak_256 (Rlp.encode (Rlp.List []))) Header.empty)).

Definition testBlock (height : N) (txs : list T.t) : Block.t :=
  Block.t.with_header (testHeader height) (Block.t.with_transactions txs Block.empty).

Definition testSnapshot : W.t :=
  W.t.with_history [testBlock 100 []] W.empty.

Lemma test_header_next height :
  headerChecks (testHeader height) (testHeader (height + 1)) = true.
Proof.
  unfold headerChecks. apply bool_decide_eq_true.
  repeat split; try reflexivity; try (vm_compute; intuition discriminate).
Qed.

#[local] Instance executor : Execution.Make.FArgs := executionArgs
  {| Execution.Make_Params_signature.chain_id := 1%N;
     Execution.Make_Params_signature.revision := Support.Variants.Revision.Eleven;
     Execution.Make_Params_signature.trace := false |}.

Lemma test_empty_block_data_checks height :
  blockDataChecks (testBlock height []) = true.
Proof.
  unfold blockDataChecks, validMonadBlock. apply bool_decide_eq_true.
  split; [reflexivity|].
  split; [vm_compute; discriminate|].
  split; [vm_compute; discriminate|].
  rewrite (validate_monad_block_no_system_transactions (testBlock height []) (Forall_nil _)).
  reflexivity.
Qed.

Example empty_window_needs_no_parent : miscBlockChecks W.empty [] = true.
Proof. reflexivity. Qed.

Example nonempty_window_requires_parent :
  miscBlockChecks W.empty [testBlock 101 []] = false.
Proof. reflexivity. Qed.

Example consecutive_empty_blocks_pass :
  miscBlockChecks testSnapshot [testBlock 101 []; testBlock 102 []] = true.
Proof.
  cbn [miscBlockChecks checkBlockSequence testSnapshot W.t.with_history W.empty W.t.history].
  change (Block.t.header (testBlock 100 [])) with (testHeader 100).
  change (Block.t.header (testBlock 101 [])) with (testHeader 101).
  change (Block.t.header (testBlock 102 [])) with (testHeader 102).
  assert (H1 : headerChecks (testHeader 100) (testHeader 101) = true)
    by exact (test_header_next 100).
  assert (H2 : headerChecks (testHeader 101) (testHeader 102) = true)
    by exact (test_header_next 101).
  rewrite H1, H2, !test_empty_block_data_checks.
  reflexivity.
Qed.

Example skipped_height_rejected : miscBlockChecks testSnapshot [testBlock 102 []] = false.
Proof.
  cbn [miscBlockChecks checkBlockSequence testSnapshot W.t.with_history W.empty W.t.history].
  change (Block.t.header (testBlock 100 [])) with (testHeader 100).
  change (Block.t.header (testBlock 102 [])) with (testHeader 102).
  assert (H : headerChecks (testHeader 100) (testHeader 102) = false).
  { unfold headerChecks. apply bool_decide_eq_false. intros [H _]. discriminate H. }
  rewrite H. reflexivity.
Qed.

Example single_transaction_fits :
  @publictransaction.transactionSender executor selfAuthorizedTx = Some signedAlice →
  blockDataChecks (testBlock 101 [selfAuthorizedTx]) = true.
Proof.
  intro Hsender. unfold blockDataChecks, validMonadBlock.
  apply bool_decide_eq_true.
  split; [reflexivity|].
  split; [vm_compute; discriminate|].
  split; [vm_compute; discriminate|].
  assert (Hmonad : @Execution.Make.validate_monad_block executor
      (testBlock 101 [selfAuthorizedTx]) = inl tt).
  { apply (@validate_monad_block_no_system_transactions executor).
    constructor; [|constructor].
    apply (@blocktransactionentry.sender_not_system_is_system_transaction executor).
    rewrite Hsender. intro Heq.
    assert (Hne : Some signedAlice ≠ Some Chain.Monad.system_sender).
    { apply (bool_decide_eq_false_1 (Some signedAlice = Some Chain.Monad.system_sender)).
      vm_compute. reflexivity. }
    exact (Hne Heq). }
  rewrite Hmonad. reflexivity.
Qed.

Each transaction has gas limit 100000, below the block limit 150000; the pair's total 200000 exceeds it. No signature facts are needed.
Example aggregate_gas_rejected :
  blockDataChecks (testBlock 101 [selfAuthorizedTx; selfAuthorizedTx]) = false.
Proof.
  unfold blockDataChecks. apply bool_decide_eq_false.
  intros [_ [_ [Hgas _]]]. vm_compute in Hgas. exact (Hgas eq_refl).
Qed.

Example declared_gas_rejected :
  blockDataChecks (Block.t.with_header (Header.t.with_gas_used 150001%N (testHeader 101))
    (testBlock 101 [])) = false.
Proof.
  unfold blockDataChecks. apply bool_decide_eq_false.
  intros [_ [Hused _]]. vm_compute in Hused. exact (Hused eq_refl).
Qed.

The counterexample excluded by the new premise: after block 100, execution rejects block 102 before processing any transaction.
Example skipped_height_execution_rejected :
  ∃ details, @Execution.Make.process_block executor false testSnapshot (testBlock 102 []) =
    inr (Execution.Error.Invalid_block details).
Proof.
  assert (Hval : ∃ details,
    @Execution.Make.validate_block executor testSnapshot (testBlock 102 []) =
      inr (Execution.Error.Invalid_block details)).
  { unfold Execution.Make.validate_block.
    change (@Result.op_letdollar) with
      (fun (A Error C : Set) (r : A + Error) (f : A → C + Error) ⇒
        match r with inl value ⇒ f value | inr error ⇒ inr error end).
    change (@Result.when_) with
      (fun (Error : Set) (condition : bool) (failure : unit + Error) ⇒
        if condition then failure else inl tt).
    change (@Result.List.hd) with
      (fun (A : Set) (fallback : A) (xs : list A) ⇒
        match xs with [] ⇒ fallback | x :: _ ⇒ x end).
    destruct (W.t.history testSnapshot) as [|parent rest] eqn:Hhist; [discriminate Hhist|].
    injection Hhist as <- _.
    assert (Hn : Support.UintN.op_ltgt (Header.t.number (Block.t.header (testBlock 102 [])))
      (Support.UintN.op_plus (Header.t.number (Block.t.header (testBlock 100 []))) Support.UintN.one) = true)
      by reflexivity.
    cbn beta zeta. rewrite Hn. eexists. reflexivity. }
  destruct Hval as [details Hval]. ∃ details.
  unfold Execution.Make.process_block.
  change ((@Execution.Make.Params executor).(Execution.Make_Params_signature.revision))
    with Support.Variants.Revision.Eleven.
  change (@Result.op_letdollar) with
    (fun (A Error C : Set) (r : A + Error) (f : A → C + Error) ⇒
      match r with inl value ⇒ f value | inr error ⇒ inr error end).
  cbn beta iota zeta. rewrite Hval. reflexivity.
Qed.

The concrete executor succeeds on a checked empty block. This assumes neither transaction validation nor block-execution success.
Example checked_empty_block_executes :
  ∃ post, @Execution.Make.process_block executor false testSnapshot (testBlock 101 []) = inl post.
Proof.
  assert (Hcheck : miscBlockChecks testSnapshot [testBlock 101 []] = true).
  { cbn [miscBlockChecks checkBlockSequence testSnapshot W.t.with_history W.empty W.t.history].
    change (Block.t.header (testBlock 100 [])) with (testHeader 100).
    change (Block.t.header (testBlock 101 [])) with (testHeader 101).
    assert (H : headerChecks (testHeader 100) (testHeader 101) = true)
      by exact (test_header_next 100).
    rewrite H, test_empty_block_data_checks. reflexivity. }
  pose proof (@misc_checks_block_step executor testSnapshot (testBlock 101 []) [] eq_refl Hcheck)
    as Hsafe.
  destruct (@Execution.Make.process_block executor false testSnapshot (testBlock 101 []))
    as [post|[blockError|txError]] eqn:Hrun.
  - ∃ post. reflexivity.
  - contradiction.
  - pose proof (@blocktransactionentry.process_block_dispatch_error_from_entry executor false
      testSnapshot (testBlock 101 []) txError eq_refl Hrun) as Htx.
    discriminate Htx.
Qed.