Library QedkConcrete.miscblockproofs

Header and gas checks exclude block errors without assuming VM behavior.
From Stdlib Require Import List String NArith ZArith Lia Bool.
From stdpp Require Import decidable.
Require Export QedkConcrete.miscblockchecks.
Require Import QedkConcrete.blocknumbers QedkConcrete.transactionchecked
  QedkConcrete.transactionformat QedkConcrete.blocktransactionentry.
Set Default Goal Selector "!".
Import ListNotations.
Module E := Execution.Make.
Module B := State.BlockState.

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

Lemma drop_while_all_false {A : Set} (p : A → bool) (xs : list A) :
  Forall (fun x ⇒ p x = false) xs →
  Result.List.drop_while p xs = xs.
Proof.
  intro Hxs. destruct Hxs as [|x xs Hx _]; [reflexivity | cbn; rewrite Hx; reflexivity].
Qed.

Lemma take_while_all_false {A : Set} (p : A → bool) (xs : list A) :
  Forall (fun x ⇒ p x = false) xs →
  Result.List.take_while p xs = [].
Proof.
  intro Hxs. destruct Hxs as [|x xs Hx _]; [reflexivity | cbn; rewrite Hx; reflexivity].
Qed.

Lemma exists_all_false {A : Set} (p : A → bool) (xs : list A) :
  Forall (fun x ⇒ p x = false) xs →
  Result.List._exists p xs = false.
Proof.
  intro Hxs. induction Hxs as [|x xs Hx Hxs IH]; [reflexivity | cbn; rewrite Hx; exact IH].
Qed.

Lemma validate_monad_block_no_system_transactions block :
  Forall (fun tx ⇒ @E.is_system_transaction F tx = false) (Block.t.transactions block) →
  @E.validate_monad_block F block = inl tt.
Proof.
  intro HnoSys. unfold E.validate_monad_block.
  rewrite (drop_while_all_false (@E.is_system_transaction F) _ HnoSys).
  rewrite (exists_all_false (@E.is_system_transaction F) _ HnoSys).
  rewrite (take_while_all_false (@E.is_system_transaction F) _ HnoSys).
  reflexivity.
Qed.

Local Lemma result_fold_leftM_inl_ext {Acc A E1 E2 : Set}
    (f1 : Acc → A → Acc + E1) (f2 : Acc → A → Acc + E2)
    (xs : list A) :
  (∀ acc x,
    match f1 acc x, f2 acc x with
    | inl a1, inl a2 ⇒ a1 = a2
    | inl _, inr _ ⇒ False
    | _, _ ⇒ True
    end) →
  ∀ init res,
    Result.List.fold_leftM f1 init xs = inl res →
    Result.List.fold_leftM f2 init xs = inl res.
Proof.
  intro Hf. induction xs as [|x xs IH]; intros init res H1.
  - injection H1 as <-. reflexivity.
  - change (Result.op_letdollar (f1 init x)
      (fun next ⇒ Result.List.fold_leftM f1 next xs) = inl res) in H1.
    change (Result.op_letdollar (f2 init x)
      (fun next ⇒ Result.List.fold_leftM f2 next xs) = inl res).
    pose proof (Hf init x) as Hstep.
    destruct (f1 init x) as [next1|e1]; [|discriminate H1].
    destruct (f2 init x) as [next2|e2]; [|contradiction Hstep].
    subst next2. exact (IH next1 res H1).
Qed.

Lemma validate_monad_block_same_transactions b1 b2 :
  Block.t.transactions b1 = Block.t.transactions b2 →
  @E.validate_monad_block F b1 = inl tt →
  @E.validate_monad_block F b2 = inl tt.
Proof.
  intros Htxs Hval.
  unfold E.validate_monad_block in Hval |- ×.
  rewrite <- Htxs.
  change (@Result.op_letdollar) with
    (fun (A B C : Set) (r : A + B) (f : A → C + B) ⇒
      match r with inl value ⇒ f value | inr error ⇒ inr error end) in Hval |- ×.
  change (@Result.when_) with
    (fun (A : Set) (condition : bool) (r : unit + A) ⇒
      if condition then r else inl tt) in Hval |- ×.
  change (@Result._return) with (fun (A B : Set) (x : A) ⇒ @inl A B x) in Hval |- ×.
  cbn beta iota zeta in Hval |- ×.
  destruct (Result.List._exists (@E.is_system_transaction F)
    (Result.List.drop_while (@E.is_system_transaction F) (Block.t.transactions b1)));
    [discriminate Hval|].
  unfold Result.op_ltdollargt, Result.map in Hval |- ×.
  cbn beta iota zeta in Hval |- ×.
  match type of Hval with
  | context [Result.List.fold_leftM ?f1 ?init ?xs] ⇒
      destruct (Result.List.fold_leftM f1 init xs) as [res|] eqn:Hfold1;
      [|cbn in Hval; discriminate Hval]
  end.
  match goal with
  | |- context [Result.List.fold_leftM ?f2 ?init ?xs] ⇒
      assert (Hfold2 : Result.List.fold_leftM f2 init xs = inl res)
  end.
  { eapply result_fold_leftM_inl_ext; [|exact Hfold1].
    intros seen tx.
    unfold Result.Option.or_fail, E.invalid_block.
    destruct (@E.System_transaction_type.of_transaction F tx) as [ty|]; [|exact I].
    cbn beta iota zeta.
    destruct (andb _ _); [exact I|].
    cbn beta iota zeta.
    destruct seen as [|last_ty more]; [reflexivity|].
    destruct (E.System_transaction_type.equal last_ty ty); [exact I|].
    destruct (negb (E.System_transaction_type.can_come_before last_ty ty)); [exact I|reflexivity]. }
  rewrite Hfold2. exact Hval.
Qed.

Lemma checked_header_validates world parent history block :
  W.t.history world = parent :: history →
  headerChecks (Block.t.header parent) (Block.t.header block) = true →
  Block.t.ommers block = [] →
  (Header.t.gas_used (Block.t.header block) ≤
    Header.t.gas_limit (Block.t.header block))%N →
  @E.validate_monad_block F block = inl tt →
  @E.validate_block F world block = inl tt.
Proof.
  intros Hhistory Hheader Hommers Hused Hmonad.
  unfold headerChecks in Hheader. apply bool_decide_eq_true in Hheader.
  destruct Hheader as [Hnumber [Hminimum [[Hlower Hupper]
    [Htime [Hhash [Hdifficulty [Hnonce Hextra]]]]]]].
  unfold E.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).
  rewrite Hhistory. cbn zeta.
  change (@Result.List.hd) with
    (fun (A : Set) (fallback : A) (xs : list A) ⇒
      match xs with [] ⇒ fallback | x :: _ ⇒ x end).
  cbn beta iota zeta.
  assert (Hn : Support.UintN.op_ltgt (Header.t.number (Block.t.header block))
    (Support.UintN.op_plus (Header.t.number (Block.t.header parent)) Support.UintN.one) = false).
  { change (negb (N.eqb (Header.t.number (Block.t.header block))
      (Header.t.number (Block.t.header parent) + 1)) = false). apply negb_false_iff, N.eqb_eq. exact Hnumber. }
  rewrite Hn. cbn beta iota.
  assert (Hsub : N.leb (Header.t.gas_limit (Block.t.header parent) / 1024)
      (Header.t.gas_limit (Block.t.header parent)) = true).
  { apply N.leb_le, N.Div0.div_le_upper_bound; lia. }
  assert (Hlimit :
    (Gas.op_lt (Header.t.gas_limit (Block.t.header block)) (Gas.op_tildedollar 5000%Z) ||
     (Gas.op_gteq (Header.t.gas_limit (Block.t.header block))
       (Gas.op_plus (Header.t.gas_limit (Block.t.header parent))
         (Gas.op_div (Header.t.gas_limit (Block.t.header parent)) (Gas.op_tildedollar 1024%Z))) ||
      Gas.op_lteq (Header.t.gas_limit (Block.t.header block))
       (Gas.op_minus (Header.t.gas_limit (Block.t.header parent))
         (Gas.op_div (Header.t.gas_limit (Block.t.header parent)) (Gas.op_tildedollar 1024%Z))))) = false).
  { change (Gas.op_div (Header.t.gas_limit (Block.t.header parent)) (Gas.op_tildedollar 1024%Z))
      with (Header.t.gas_limit (Block.t.header parent) / 1024)%N.
    unfold Gas.op_minus, Support.UintN.op_minus. rewrite Hsub.
    change ((N.ltb (Header.t.gas_limit (Block.t.header block)) 5000 ||
      (N.leb (Header.t.gas_limit (Block.t.header parent) + Header.t.gas_limit (Block.t.header parent) / 1024)
        (Header.t.gas_limit (Block.t.header block)) ||
       N.leb (Header.t.gas_limit (Block.t.header block))
        (Header.t.gas_limit (Block.t.header parent) - Header.t.gas_limit (Block.t.header parent) / 1024))) = false).
    apply orb_false_iff. split; [apply N.ltb_ge; exact Hminimum|].
    apply orb_false_iff. split; apply N.leb_gt; assumption. }
  rewrite Hlimit. cbn beta iota.
  assert (Hu : Gas.op_gt (Header.t.gas_used (Block.t.header block))
    (Header.t.gas_limit (Block.t.header block)) = false).
  { apply N.ltb_ge. exact Hused. }
  rewrite Hu. cbn beta iota.
  assert (Ht : Numeric.U256.op_lt (Header.t.timestamp (Block.t.header block))
    (Header.t.timestamp (Block.t.header parent)) = false).
  { rewrite reservecheck.u256_lt. apply Z.ltb_ge. exact Htime. }
  rewrite Ht, Hommers, Hhash.
  change (Byte_string.B32.(Byte_string.Fixed.Fixed_result.op_ltgt)) with
    (fun x y ⇒ OCamlInt.not_equal (OCamlString.compare x y) 0%Z).
  cbn beta. rewrite OCamlString.compare_matches_structural.
  unfold OCamlString.compare_structural. rewrite String.eqb_refl. cbn beta iota.
  rewrite Hdifficulty. cbn beta iota.
  change (Byte_string.B8.(Byte_string.Fixed.Fixed_result.op_ltgt)) with
    (fun x y ⇒ OCamlInt.not_equal (OCamlString.compare x y) 0%Z).
  rewrite Hnonce. cbn beta. rewrite OCamlString.compare_matches_structural.
  unfold OCamlString.compare_structural. rewrite String.eqb_refl. cbn beta iota.
  assert (He : Z.gtb (OCamlString.length (Header.t.extra_data (Block.t.header block))) 32 = false)
    by (rewrite Z.gtb_ltb; apply Z.ltb_ge; exact Hextra).
  change (Byte_string.Bytes.length) with OCamlString.length.
  rewrite He. cbn beta iota zeta.
  rewrite Hmonad. reflexivity.
Qed.

Lemma delete_accounts_gas block addresses :
  B.t.gas_used (OCamlSeq.fold_left
    (fun s addr ⇒ State.op_pointcaretlparenrparenltminus s
      (@B.account_opt None addr) None) block addresses) = B.t.gas_used block.
Proof.
  revert block. induction addresses as [|addr addresses IH]; intro block.
  - reflexivity.
  - cbn [OCamlSeq.fold_left]. rewrite IH. reflexivity.
Qed.
Lemma transaction_gas_exact block tx post :
  @E.process_transaction F block tx = inl post →
  B.t.gas_used post = (B.t.gas_used block + T.gas_limit tx)%N.
Proof.
  intro Hrun. unfold E.process_transaction in Hrun.
  destruct (@E.validate_transaction F block tx) as [[sender fee price intrinsic]|error];
    [|discriminate Hrun].
  change (@MonadVM.Result.op_letdollar) with
    (fun (A B C : Set) (r : A + B) (f : A → C + B) ⇒
      match r with inl value ⇒ f value | inr error ⇒ inr error end) in Hrun.
  cbn beta iota zeta delta [E.transaction_validation.sender E.transaction_validation.total_fee
    E.transaction_validation.intrinsic_gas E.transaction_validation.effective_gas_price] in Hrun.
  match type of Hrun with context [MonadVM.Result.List.fold_left ?f ?start (T.authorization_list tx)] ⇒
    destruct (MonadVM.Result.List.fold_left f start (T.authorization_list tx)) as [authorities prepared]
  end.
  cbn beta iota zeta in Hrun.
  match type of Hrun with context [E.Host.call_from_eoa tx ?msg ?initial] ⇒
    destruct (E.Host.call_from_eoa tx msg initial) as [result afterVm]
  end.
  injection Hrun as <-.
  cbn beta iota zeta delta [B.t.with_transactions_processed B.t.with_gas_used B.t.gas_used].
  change (@MonadVM.Result.Seq.fold_left) with (@OCamlSeq.fold_left).
  rewrite delete_accounts_gas. reflexivity.
Qed.

Lemma transactions_gas_exact block txs post :
  Result.List.fold_leftM (@E.process_transaction F) block txs = inl post →
  B.t.gas_used post = (B.t.gas_used block + transactionGas txs)%N.
Proof.
  revert block. induction txs as [|tx txs IH]; intros block Hrun.
  - injection Hrun as <-. unfold transactionGas. cbn [Stdlib.Lists.List.fold_right]. lia.
  - change (Result.op_letdollar (@E.process_transaction F block tx)
      (fun next ⇒ Result.List.fold_leftM (@E.process_transaction F) next txs) = inl post) in Hrun.
    destruct (@E.process_transaction F block tx) as [next|error] eqn:Hstep;
      [|discriminate Hrun].
    rewrite (IH next Hrun), (transaction_gas_exact block tx next Hstep).
    unfold transactionGas. cbn [Stdlib.Lists.List.fold_right]. lia.
Qed.

Lemma system_transaction_gas block tx post :
  @E.process_monad_system_transaction F block tx = inl post →
  B.t.gas_used post = B.t.gas_used block.
Proof.
  intro Hrun. unfold E.process_monad_system_transaction in Hrun.
  destruct (@E.validate_monad_system_transaction F block tx Chain.Monad.system_sender)
    as [[]|error]; [|discriminate Hrun].
  change (@MonadVM.Result.op_letdollar) with
    (fun (A B C : Set) (r : A + B) (f : A → C + B) ⇒
      match r with inl value ⇒ f value | inr error ⇒ inr error end) in Hrun.
  cbn beta iota zeta in Hrun.
  match type of Hrun with context [E.Host.call_from_monad_system_transaction ?msg ?initial] ⇒
    destruct (E.Host.call_from_monad_system_transaction msg initial) as [result afterVm]
  end.
  unfold MonadVM.Result.when_ in Hrun.
  destruct (negb (Evmc.Result.StatusCode.is_success (Evmc.Result.t.status_code result)));
    [discriminate Hrun|].
  injection Hrun as <-. reflexivity.
Qed.

Lemma dispatch_transaction_gas_le block tx post :
  @E.dispatch_transaction F block tx = inl post →
  (B.t.gas_used post ≤ B.t.gas_used block + T.gas_limit tx)%N.
Proof.
  intro Hrun.
  destruct (@dispatch_transaction_cases F block tx post Hrun)
    as [[_ [_ Hproc]] | [_ Hsys]].
  - rewrite (transaction_gas_exact block tx post Hproc). lia.
  - rewrite (system_transaction_gas block tx post Hsys). lia.
Qed.

Lemma dispatched_transactions_gas_le block txs post :
  Result.List.fold_leftM (@E.dispatch_transaction F) block txs = inl post →
  (B.t.gas_used post ≤ B.t.gas_used block + transactionGas txs)%N.
Proof.
  revert block. induction txs as [|tx txs IH]; intros block Hrun.
  - injection Hrun as <-. unfold transactionGas. cbn [Stdlib.Lists.List.fold_right]. lia.
  - change (Result.op_letdollar (@E.dispatch_transaction F block tx)
      (fun next ⇒ Result.List.fold_leftM (@E.dispatch_transaction F) next txs) = inl post) in Hrun.
    destruct (@E.dispatch_transaction F block tx) as [next|error] eqn:Hstep;
      [|discriminate Hrun].
    pose proof (IH next Hrun) as Hrest.
    pose proof (dispatch_transaction_gas_le block tx next Hstep) as Hfirst.
    unfold transactionGas in Hrest |- ×. cbn [Stdlib.Lists.List.fold_right]. lia.
Qed.

Lemma system_message_gas block addr data :
  B.t.gas_used (snd (@E.process_system_message F block addr data)) = B.t.gas_used block.
Proof.
  unfold E.process_system_message.
  match goal with |- context [if ?test then _ else _] ⇒ destruct test end; [reflexivity|].
  cbn beta iota zeta.
  match goal with |- context [E.Host.call ?msg ?initial] ⇒
    destruct (E.Host.call msg initial) as [result afterVm]
  end. reflexivity.
Qed.

Lemma transaction_entry_gas world block :
  B.t.gas_used (@blockTransactionEntry F world block) = 0%N.
Proof.
  unfold blockTransactionEntry. rewrite !system_message_gas. reflexivity.
Qed.

Lemma withdrawals_gas block withdrawals :
  B.t.gas_used (Result.List.fold_left (@E.process_withdrawal F) block withdrawals) =
    B.t.gas_used block.
Proof.
  revert block. induction withdrawals as [|withdrawal withdrawals IH]; intro block.
  - reflexivity.
  - change (B.t.gas_used (Result.List.fold_left (@E.process_withdrawal F)
      (@E.process_withdrawal F block withdrawal) withdrawals) = B.t.gas_used block).
    rewrite IH. reflexivity.
Qed.

Lemma finalized_header_checks parent block :
  headerChecks parent (Block.t.header (B.finalize_current_block block)) =
    headerChecks parent (Block.t.header (B.t.current_block block)).
Proof. reflexivity. Qed.

Lemma finalized_parent_checks block rest :
  checkBlockSequence (Block.t.header (B.finalize_current_block block)) rest =
    checkBlockSequence (Block.t.header (B.t.current_block block)) rest.
Proof. destruct rest; reflexivity. Qed.

Lemma valid_monad_block_iff block :
  validMonadBlock block = true ↔ @E.validate_monad_block F block = inl tt.
Proof.
  unfold validMonadBlock.
  destruct (@E.validate_monad_block F block) as [[]|];
    split; intro H; try reflexivity; discriminate.
Qed.

Successful transaction processing accumulates at most the public gas limits. The block checks therefore also validate the finalized header.
Lemma checked_finalized_block world parent history block after :
  W.t.history world = parent :: history →
  headerChecks (Block.t.header parent) (Block.t.header block) = true →
  blockDataChecks block = true →
  Result.List.fold_leftM (@E.dispatch_transaction F) (@blockTransactionEntry F world block)
    (Block.t.transactions block) = inl after →
  @E.validate_block F world
    (B.finalize_current_block (Result.List.fold_left (@E.process_withdrawal F)
      after (Block.t.withdrawals block))) = inl tt.
Proof.
  intros Hhistory Hheader Hdata Hrun.
  unfold blockDataChecks in Hdata. apply bool_decide_eq_true in Hdata.
  destruct Hdata as [Hommers [Hused [Hgas Hmonad]]].
  apply valid_monad_block_iff in Hmonad.
  pose proof (dispatched_transactions_gas_le _ _ _ Hrun) as Htotal.
  rewrite transaction_entry_gas, N.add_0_l in Htotal.
  pose proof (dispatched_transactions_current_block _ _ _ Hrun) as Hcurrent.
  rewrite block_transaction_entry_header in Hcurrent.
  eapply checked_header_validates; [exact Hhistory| | | |].
  - rewrite finalized_header_checks, withdrawals_current_block, Hcurrent. exact Hheader.
  - change (Block.t.ommers (B.t.current_block
      (Result.List.fold_left (@E.process_withdrawal F) after (Block.t.withdrawals block))) = []).
    rewrite withdrawals_current_block, Hcurrent. exact Hommers.
  - change (B.t.gas_used (Result.List.fold_left (@E.process_withdrawal F) after
      (Block.t.withdrawals block)) ≤ Header.t.gas_limit (Block.t.header
        (B.t.current_block (Result.List.fold_left (@E.process_withdrawal F) after
          (Block.t.withdrawals block)))))%N.
    rewrite withdrawals_gas, withdrawals_current_block, Hcurrent.
    exact (N.le_trans _ _ _ Htotal Hgas).
  - eapply validate_monad_block_same_transactions; [|exact Hmonad].
    change (Block.t.transactions block = Block.t.transactions (B.t.current_block
      (Result.List.fold_left (@E.process_withdrawal F) after (Block.t.withdrawals block)))).
    rewrite withdrawals_current_block, Hcurrent. reflexivity.
Qed.

Lemma process_transaction_no_block_error block tx details :
  @E.process_transaction F block tx ≠
    inr (Execution.Error.Invalid_block details).
Proof.
  intro Hstep.
  pose proof (@process_transaction_error_from_validation F block tx _ Hstep) as Hvalid.
  unfold E.validate_transaction in Hvalid.
  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) in Hvalid.
  unfold E.validate_authorizations, E.invalid_transaction in Hvalid.
  change (@Result.when_) with
    (fun (Error : Set) (condition : bool) (failure : unit + Error) ⇒
      if condition then failure else inl tt) in Hvalid.
  change (@Result._return) with (fun (A Error : Set) (value : A) ⇒ @inl A Error value) in Hvalid.
  destruct tx; cbn beta iota zeta in Hvalid.
  all: repeat (cbn beta iota zeta in Hvalid;
    match type of Hvalid with
    | context [if ?test then _ else _] ⇒ destruct test eqn:?
    | context [match ?test with Some _ ⇒ _ | None ⇒ _ end] ⇒ destruct test eqn:?
    | context [match ?test with [] ⇒ _ | _ :: _ ⇒ _ end] ⇒ destruct test eqn:?
    end); try discriminate.
Qed.

Lemma transactions_no_block_error block txs details :
  Result.List.fold_leftM (@E.process_transaction F) block txs ≠
    inr (Execution.Error.Invalid_block details).
Proof.
  revert block. induction txs as [|tx txs IH]; intros block Hrun; [discriminate|].
  change (Result.op_letdollar (@E.process_transaction F block tx)
    (fun next ⇒ Result.List.fold_leftM (@E.process_transaction F) next txs) =
      inr (Execution.Error.Invalid_block details)) in Hrun.
  destruct (@E.process_transaction F block tx) as [next|error] eqn:Hstep.
  - exact (IH next Hrun).
  - injection Hrun as Heq. subst error.
    exact (process_transaction_no_block_error block tx details Hstep).
Qed.

Lemma validate_monad_system_transaction_no_block_error block tx sender details :
  @E.validate_monad_system_transaction F block tx sender ≠
    inr (Execution.Error.Invalid_block details).
Proof.
  intro Hvalid.
  unfold E.validate_monad_system_transaction, E.invalid_transaction in Hvalid.
  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) in Hvalid.
  change (@Result.when_) with
    (fun (Error : Set) (condition : bool) (failure : unit + Error) ⇒
      if condition then failure else inl tt) in Hvalid.
  change (@Result._return) with (fun (A Error : Set) (value : A) ⇒ @inl A Error value) in Hvalid.
  destruct tx; cbn beta iota zeta in Hvalid.
  all: repeat (cbn beta iota zeta in Hvalid;
    match type of Hvalid with
    | context [if ?test then _ else _] ⇒ destruct test eqn:?
    | context [match ?test with Some _ ⇒ _ | None ⇒ _ end] ⇒ destruct test eqn:?
    end); try discriminate.
Qed.

Lemma process_monad_system_transaction_no_block_error block tx details :
  @E.process_monad_system_transaction F block tx ≠
    inr (Execution.Error.Invalid_block details).
Proof.
  intro Hstep. unfold E.process_monad_system_transaction in Hstep.
  destruct (@E.validate_monad_system_transaction F block tx Chain.Monad.system_sender)
    as [[]|error] eqn:Hval.
  - 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) in Hstep.
    cbn beta iota zeta in Hstep.
    match type of Hstep with context [E.Host.call_from_monad_system_transaction ?msg ?initial] ⇒
      destruct (E.Host.call_from_monad_system_transaction msg initial) as [result afterVm]
    end.
    unfold Result.when_, E.invalid_transaction in Hstep.
    destruct (negb (Evmc.Result.StatusCode.is_success (Evmc.Result.t.status_code result)));
      discriminate Hstep.
  - injection Hstep as Heq. subst error.
    exact (validate_monad_system_transaction_no_block_error block tx _ details Hval).
Qed.

Lemma dispatch_transaction_no_block_error block tx details :
  @E.dispatch_transaction F block tx ≠
    inr (Execution.Error.Invalid_block details).
Proof.
  unfold E.dispatch_transaction.
  destruct (T.chain_id tx) as [cid|].
  - destruct (Support.UintN.op_ltgt cid _); [discriminate|].
    destruct (@E.sender_kind_value F tx) as [[sender|]|].
    + exact (process_transaction_no_block_error block tx details).
    + exact (process_monad_system_transaction_no_block_error block tx details).
    + discriminate.
  - destruct (@E.sender_kind_value F tx) as [[sender|]|].
    + exact (process_transaction_no_block_error block tx details).
    + exact (process_monad_system_transaction_no_block_error block tx details).
    + discriminate.
Qed.

Lemma dispatched_transactions_no_block_error block txs details :
  Result.List.fold_leftM (@E.dispatch_transaction F) block txs ≠
    inr (Execution.Error.Invalid_block details).
Proof.
  revert block. induction txs as [|tx txs IH]; intros block Hrun; [discriminate|].
  change (Result.op_letdollar (@E.dispatch_transaction F block tx)
    (fun next ⇒ Result.List.fold_leftM (@E.dispatch_transaction F) next txs) =
      inr (Execution.Error.Invalid_block details)) in Hrun.
  destruct (@E.dispatch_transaction F block tx) as [next|error] eqn:Hstep.
  - exact (IH next Hrun).
  - injection Hrun as Heq. subst error.
    exact (dispatch_transaction_no_block_error block tx details Hstep).
Qed.

A checked block cannot produce a block error. If it succeeds, its finalized header supplies the same number, timestamp and gas limit to the remaining checks as its supplied header did.
Lemma misc_checks_block_step world block rest :
  (@E.Params F).(Execution.Make_Params_signature.revision) = Support.Variants.Revision.Eleven →
  miscBlockChecks world (block :: rest) = true →
  match @E.process_block F false world block with
  | inl post ⇒ miscBlockChecks post rest = true
  | inr (Execution.Error.Invalid_block _) ⇒ False
  | inr (Execution.Error.Invalid_transaction _) ⇒ True
  end.
Proof.
  intros Hrevision Hchecks.
  unfold miscBlockChecks in Hchecks.
  destruct (W.t.history world) as [|parent history] eqn:Hhistory; [discriminate|].
  change (headerChecks (Block.t.header parent) (Block.t.header block) &&
    blockDataChecks block && checkBlockSequence (Block.t.header block) rest = true) in Hchecks.
  apply andb_true_iff in Hchecks as [Hfirst Hrest].
  apply andb_true_iff in Hfirst as [Hheader Hdata].
  assert (Hvalid : @E.validate_block F world block = inl tt).
  { pose proof Hdata as Hfields. unfold blockDataChecks in Hfields.
    apply bool_decide_eq_true in Hfields.
    destruct Hfields as [Hommers [Hused [_ Hmonad]]].
    apply valid_monad_block_iff in Hmonad.
    eapply checked_header_validates; eassumption. }
  unfold E.process_block. rewrite Hrevision, Hvalid.
  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.
  match goal with |- context [Result.List.fold_leftM ?step ?entry ?txs] ⇒
    change entry with (@blockTransactionEntry F world block)
  end.
  destruct (Result.List.fold_leftM (@E.dispatch_transaction F) (@blockTransactionEntry F world block)
    (Block.t.transactions block)) as [after|error] eqn:Htxs.
  - cbn beta iota zeta.
    match goal with |- context [E.validate_block ?entry ?finalized] ⇒
      change (E.validate_block entry finalized) with (E.validate_block world finalized)
    end.
    rewrite (checked_finalized_block world parent history block after Hhistory Hheader Hdata Htxs).
    cbn beta iota zeta.
    pose proof (dispatched_transactions_current_block _ _ _ Htxs) as Hcurrent.
    rewrite block_transaction_entry_header in Hcurrent.
    destruct rest as [|first rest]; [reflexivity|].
    change (checkBlockSequence (Block.t.header (B.finalize_current_block
      (Result.List.fold_left (@E.process_withdrawal F) after (Block.t.withdrawals block))))
      (first :: rest) = true).
    rewrite finalized_parent_checks, withdrawals_current_block, Hcurrent. exact Hrest.
  - destruct error as [details|details]; [|exact I].
    exact (dispatched_transactions_no_block_error _ _ details Htxs).
Qed.

Check the whole list once. Execution of any prefix either encounters a transaction error, or succeeds and preserves the checks on its suffix. No successful-prefix or transaction-validation premise is assumed here.
Theorem misc_checks_prefix_execution world past rest :
  (@E.Params F).(Execution.Make_Params_signature.revision) = Support.Variants.Revision.Eleven →
  miscBlockChecks world (past ++ rest) = true →
  match Result.List.fold_leftM (@E.process_block F false) world past with
  | inl post ⇒ miscBlockChecks post rest = true
  | inr (Execution.Error.Invalid_block _) ⇒ False
  | inr (Execution.Error.Invalid_transaction _) ⇒ True
  end.
Proof.
  revert world. induction past as [|block past IH]; intros world Hrevision Hchecks.
  - exact Hchecks.
  - pose proof (misc_checks_block_step world block (past ++ rest) Hrevision Hchecks) as Hstep.
    change (match Result.op_letdollar (@E.process_block F false world block)
      (fun next ⇒ Result.List.fold_leftM (@E.process_block F false) next past) with
      | inl post ⇒ miscBlockChecks post rest = true
      | inr (Execution.Error.Invalid_block _) ⇒ False
      | inr (Execution.Error.Invalid_transaction _) ⇒ True
      end).
    destruct (@E.process_block F false world block) as [next|[details|details]] eqn:Hrun.
    + exact (IH next Hrevision Hstep).
    + exact Hstep.
    + exact I.
Qed.

End Execution.