Library QedkConcrete.miscblockchecks
Block-header and aggregate-gas checks
From Stdlib Require Import List String NArith ZArith Bool.
From stdpp Require Import decidable numbers strings list.
Require Import MonadVM.Execution.
Import ListNotations.
Module E := Execution.Make.
Module Block := Chain.Ethereum.Block.
Module Header := Block.Header.
Module T := Chain.Ethereum.Transaction.
Module W := State.WorldState.
Local Abbreviation asbool := bool_decide.
Section Execution.
Context {F : E.FArgs}.
From stdpp Require Import decidable numbers strings list.
Require Import MonadVM.Execution.
Import ListNotations.
Module E := Execution.Make.
Module Block := Chain.Ethereum.Block.
Module Header := Block.Header.
Module T := Chain.Ethereum.Transaction.
Module W := State.WorldState.
Local Abbreviation asbool := bool_decide.
Section Execution.
Context {F : E.FArgs}.
Return true when block passes
E.validate_monad_block:
all Monad system transactions form a leading prefix in valid
system-transaction order, without duplicates and with reward value at
most max_block_reward (25 MON).
Definition validMonadBlock (block : Block.t) : bool :=
match @E.validate_monad_block F block with
| inl _ ⇒ true
| inr _ ⇒ false
end.
match @E.validate_monad_block F block with
| inl _ ⇒ true
| inr _ ⇒ false
end.
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.
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).
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, the sum of transaction gas limits within
that same limit, and validMonadBlock (Monad system-transaction prefix
ordering and the 25 MON reward cap).
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 ∧
validMonadBlock block = true).
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 ∧
validMonadBlock block = true).
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.
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.
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.
- 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.
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.
End Execution.
match blocks with
| [] ⇒ true
| _ :: _ ⇒
match W.t.history snapshot with
| [] ⇒ false
| parent :: _ ⇒ checkBlockSequence (Block.t.header parent) blocks
end
end.
End Execution.