Library QedkConcrete.miscblockchecks

Block-header and aggregate-gas checks

These checks use only the parent header and the supplied block data. They do not execute transactions or compare claimed execution roots.
From Stdlib Require Import List String NArith ZArith Bool.
From stdpp Require Import decidable numbers strings list.
Require Import MonadVM.Execution.
Import ListNotations.
Module Block := Chain.Ethereum.Block.
Module Header := Block.Header.
Module T := Chain.Ethereum.Transaction.
Module W := State.WorldState.
Local Abbreviation asbool := bool_decide.

Return the sum of the supplied transactions' gas limits, in gas units. This executor counts the full limit even when a transaction reverts or leaves gas unused. System messages do not contribute to this sum.
Definition transactionGas (txs : list T.t) : N :=
  fold_right (fun tx total ⇒ (T.gas_limit tx + total)%N) 0%N txs.

parent is the preceding block's header; h is the candidate header. Return true exactly when number, timestamp, gas-limit adjustment, empty ommers hash, zero difficulty/nonce and extra-data length pass the native header checks. Gas usage is checked separately by blockDataChecks. Neither the parent hash nor execution-result roots are checked here.
Definition headerChecks (parent h : Header.t) : bool :=
  let limit := Header.t.gas_limit h in
  let parentLimit := Header.t.gas_limit parent in
  let adjustment := (parentLimit / 1024)%N in
  asbool (
    Header.t.number h = (Header.t.number parent + 1)%N ∧
    (5000 ≤ limit)%N ∧
    (parentLimit - adjustment < limit < parentLimit + adjustment)%N ∧
    (Header.t.timestamp parent ≤ Header.t.timestamp h)%Z ∧
    Header.t.ommers_hash h = Crypto.keccak_256 (Rlp.encode (Rlp.List [])) ∧
    Header.t.difficulty h = 0%N ∧
    Header.t.nonce h = Byte_string.B8.(Byte_string.Fixed.Fixed_result.zeros) ∧
    (OCamlString.length (Header.t.extra_data h) ≤ 32)%Z).

For the supplied block, require an empty ommers list, declared gas usage within its gas limit, and the sum of transaction gas limits within that same limit. With result verification disabled, the declared usage need not equal the sum: execution replaces it with the computed sum.
Definition blockDataChecks (block : Block.t) : bool :=
  let h := Block.t.header block in
  asbool (Block.t.ommers block = [] ∧
    (Header.t.gas_used h ≤ Header.t.gas_limit h)%N ∧
    (transactionGas (Block.t.transactions block) ≤ Header.t.gas_limit h)%N).

Check blocks in list order, starting after header parent. Each supplied header becomes the parent for the next check. Return true for an empty list; otherwise every header and block-data check must pass.
Fixpoint checkBlockSequence (parent : Header.t) (blocks : list Block.t) : bool :=
  match blocks with
  | [] ⇒ true
  | block :: rest ⇒
      headerChecks parent (Block.t.header block) && blockDataChecks block &&
        checkBlockSequence (Block.t.header block) rest
  end.

Check the block conditions not covered by reserve/transaction accounting.
  • snapshot: execution state immediately before blocks; the first entry of its history supplies the parent header.
  • blocks: ordered blocks to execute, with result verification disabled.
Return true exactly when all checkBlockSequence checks pass. A nonempty list also requires a parent in the snapshot; an empty list always passes. This checks no account balances, nonces, VM results or execution roots.
Definition miscBlockChecks (snapshot : W.t) (blocks : list Block.t) : bool :=
  match blocks with
  | [] ⇒ true
  | _ :: _ ⇒
      match W.t.history snapshot with
      | [] ⇒ false
      | parent :: _ ⇒ checkBlockSequence (Block.t.header parent) blocks
      end
  end.