本文へ移動
← Consensus Engineering — Reth で L1 のコンセンサスを作る
実コンセンサスコードを読む

L1 を作り、運用する · チェーンのシステム — 順不同

このコースの 5 / 1218 分

レッスン5 — Reth の Consensus trait を読む

問い

Reth のソースを開く。「PoS」で検索する。ほとんど何も出てこない — なぜなら Reth は PoS も BFT もまったく実装していないから。その仕事は consensus client(Lighthouse、Prysm、自前エンジン)側にある。では「Tempo は Reth で動く」「Berachain は PoL のために bera-reth を作った」と言うとき、実際に触っているコンセンサスの接合面は何か?

原理(最小モデル)

  • 役割分担: head 選択(CL) vs ブロック検証(EL = Reth). Reth の Consensus trait は chain head を選ばない。受け取ったブロックを 検証する だけ。
  • 3 つの trait. HeaderValidator(header 単体 / 親との比較)→ Consensus(validate_body_against_header と validate_block_pre_execution。EVM 不要)→ FullConsensus(validate_block_post_execution。gas used / receipts root / logs bloom / requests hash)。
  • state root はこの trait では検証しない. 実行後に engine tree(payload_validator.rs)、staged sync では MerkleStage が比較する。
  • HeaderValidator が親 trait. Header の検証は body の検証から分離されている — staged sync は header を先にダウンロード、body なしで検証可能、ライトクライアントは header だけでよい。
  • チェーン固有の header チェックは HeaderValidator に入る. Tempo の TempoConsensus は独自 header フィールドをここで検証する。リーダー選出や BFT 署名は Tempo のコンセンサスエンジン側で、この trait の外。

具体例

reth eb4c15e(2026-04-07)時点の crates/consensus/consensus/src/lib.rs(doc コメントと provided メソッドは省略):

#[auto_impl::auto_impl(&, Arc)]
pub trait FullConsensus<N: NodePrimitives>: Consensus<N::Block> {
    fn validate_block_post_execution(
        &self,
        block: &RecoveredBlock<N::Block>,
        result: &BlockExecutionResult<N::Receipt>,
        receipt_root_bloom: Option<ReceiptRootBloom>,
    ) -> Result<(), ConsensusError>;
}

#[auto_impl::auto_impl(&, Arc)]
pub trait Consensus<B: Block>: HeaderValidator<B::Header> {
    fn validate_body_against_header(
        &self,
        body: &B::Body,
        header: &SealedHeader<B::Header>,
    ) -> Result<(), ConsensusError>;

    fn validate_block_pre_execution(&self, block: &SealedBlock<B>) -> Result<(), ConsensusError>;

    // provided: validate_block_pre_execution_with_tx_root(block, Option<B256>)
}

HeaderValidator:

#[auto_impl::auto_impl(&, Arc)]
pub trait HeaderValidator<H = Header>: Debug + Send + Sync {
    fn validate_header(&self, header: &SealedHeader<H>) -> Result<(), ConsensusError>;

    fn validate_header_against_parent(
        &self,
        header: &SealedHeader<H>,
        parent: &SealedHeader<H>,
    ) -> Result<(), ConsensusError>;

    // provided: validate_header_range(&[SealedHeader<H>])
}

関連型 Error はなく、すべて ConsensusError を返す。total difficulty 用のメソッドもない(Merge 後に削除)。シグネチャはバージョンで変わる(後の main では validate_block_post_execution に block_access_list_hash 引数、Consensus に is_transient_error が追加)ので、ビルドするコミットのファイルを読むこと。

Ethereum 実装(EthBeaconConsensus)が各フェーズで見るもの:

フェーズチェック内容必要なもの
validate_header / against_parentMerge 後の形(difficulty 0 等)、extra data、gas、base fee、fork 固有フィールド、親との番号・timestamp・gas limit・base feeheader のみ
pre_executionommers hash、withdrawals root(Shanghai〜)、blob gas(Cancun〜)、RLP サイズ上限(Osaka〜)、transactions rootheader + body
body_against_headertransactions root、withdrawals root、ommers hash(peer から取得した body が header に属するか)header + body
post_executiongas used、receipts root、logs bloom(Byzantium〜)、requests hash(Prague〜)実行結果

state root は表にない。engine tree が実行後に計算して header と比較する(payload_validator.rs の BodyStateRootDiff)。

失敗例(誤解)

「Reth は PoS を実装している」— 間違い。Reth は EL(execution layer)。PoS(投票、attestation、proposer 選出)は CL(consensus client)の仕事。Reth は受け取ったブロックを検証するだけで、誰が head かを選ばない。

「state root チェックは pre-execution で十分」— 間違い。state root は EVM を回した結果の post-state から計算するので、実行後にしか検証できない。ただしそれをするのは validate_block_post_execution ではなく、engine tree(staged sync では MerkleStage)。

🛑 予測。 Reth はコンセンサスレイヤから来るブロックを検証する必要がある。EVM が tx を実行する前に、どんなチェックが走るべきか? 4 つ列挙。(答え: ① 整合性 = transactions root / withdrawals root / ommers hash が body と一致(receipt root は実行後にしか分からない)、② header 単体 = Merge 後の形、gas used ≤ gas limit、fork 固有フィールドの有無、③ 親との関係 = 番号が +1、timestamp が親より大きい、gas limit と base fee の変化が規則どおり、④ チェーン固有 = header に載っているならチェーン独自のフィールドや proposer 署名。)

ステップで組み立てる

Step 1: 3 フェーズを実行順で覚える

論理的な順序は validate_header + validate_block_pre_execution + validate_header_against_parent → 実行 → validate_block_post_execution → state root 比較(crates/engine/tree/src/tree/payload_validator.rs 冒頭の doc コメント)。「pre-execution」は「実行結果が不要」という意味で、「必ず先に走る」ではない: eb4c15e の engine は payload のデコードと並行して実行を始め、header / pre / 親 / post のチェックは実行後に validate_post_execution の中でまとめて走る。staged sync では文字通りの順序(header・body のダウンローダが ExecutionStage より先に検証)。validate_body_against_header は peer から取得した body を header と組み合わせるとき(crates/net/p2p/src/full_block.rs など)に呼ばれる。

Step 2: デフォルト実装 EthBeaconConsensus を読む

crates/ethereum/consensus を開く。チェック内容:

  • Merge 後の header の形(difficulty 0、nonce 0、空の ommers hash)
  • Fork 固有フィールドの有無(withdrawals root、blob gas、requests hash)
  • Gas limit の変化が parent/1024 未満(Yellow Paper 以来の境界、GAS_LIMIT_BOUND_DIVISOR)
  • BaseFee が EIP-1559 公式に従うこと
  • Timestamp が親より大きいこと
  • 実行後: gas used、receipts root、logs bloom、requests hash(state root は engine 側)

PoS の検証はしない — それは CL の仕事。

Step 3: BFT 用のカスタム override 箇所

Override 箇所理由
HeaderValidatorチェーン固有 header フィールド。header に載っているなら proposer 署名、validator set 包含、view/round 番号
validate_block_pre_executionチェーン固有の body ルール(Tempo は system tx がブロック末尾にあり有効かを検証)
validate_block_post_executionチェーン固有の receipt / request 形式に対する実行結果チェック
Slashing 関連フィールドheader や body に slashing 証拠が含まれる場合の暗号検証

trait を書き直すわけではない — 3 つの trait を実装し、ConsensusBuilder で包んで ComponentsBuilder::consensus(...) に渡す(.consensus が受け取るのは consensus の値ではなく builder)。例: .with_components(EthereumNode::components().consensus(MyConsensusBuilder))。EN 版にこの形で reth eb4c15e に対してコンパイルが通るコード全体がある。

Step 4: リポで実物を読む

git clone https://github.com/paradigmxyz/reth
cd reth
git checkout eb4c15e   # このレッスンが引用しているコミット
  1. crates/consensus/consensus/src/lib.rs を開いて HeaderValidator / Consensus / FullConsensus を読む
  2. crates/ethereum/consensus/src/lib.rs を開いて EthBeaconConsensus の 3 つの impl を見つける
  3. 数える: provided メソッドのうち override しているのは?(validate_block_pre_execution_with_tx_root)デフォルトのままなのは?(validate_header_range)
  4. validate_block_post_execution の呼び出し箇所を探す(grep -rn 'validate_block_post_execution(' crates)。eb4c15e では consensus crate 以外に 4 箇所: ExecutionStage、engine tree の payload_validator.rs、ブロック検証 RPC(crates/rpc/rpc/src/validation.rs)、re-execute コマンド

答え合わせ

  • head 選択 vs ブロック検証の境界: head 選択 = consensus client(Lighthouse / Prysm / 自前)、ブロック検証 = Reth の Consensus 実装。
  • state root が実行後に検証される理由: state root は EVM 実行の結果。pre には実行結果がない。比較するのは trait ではなく engine tree / MerkleStage。
  • カスタム L1 で実装を変える箇所: HeaderValidator(チェーン固有 header フィールド、header に載っているなら proposer 署名)、validate_block_pre_execution(チェーン固有 body ルール)、validate_block_post_execution(チェーン固有の実行結果チェック)。

合格基準

  • 3 検証フェーズを実行順で言える。
  • 「Reth は head を選ばない」「CL が head を選ぶ」を即答できる。
  • HeaderValidator がなぜ Consensus trait の親なのか言える。
  • リポを clone して Consensus と EthBeaconConsensus の関係を辿れる。

まとめ(3行)

  • Reth の Consensus trait は ブロック検証 のみを担う — head 選択は consensus client の仕事。
  • 3 trait(HeaderValidator / Consensus / FullConsensus)。post-execution は gas used / receipts root / logs bloom / requests hash、state root は engine / MerkleStage が比較。
  • カスタム L1 は trait を書き直さず 異なる実装を提供 し、ConsensusBuilder 経由で ComponentsBuilder::consensus(...) に渡す。