レッスン9 — ドリル: reorg-safe なインデクサを作る
問い
読むのはリハーサル、実装するのが記憶。reorg を正しく乗り越える HashMap インデクサを書く。テストネット(Hoodi)で reorg を観測しつつ、tx_count の不変条件を保つには?
原理(最小モデル)
- セットアップ.
reth-exex-examplesを clone + minimal で build。 - テストネットは Hoodi(または Sepolia). Holesky は 2025-09-30 に EOL、Fusaka 後に停止済み(EF ブログ 2025-09-01)。Hoodi は 2025 年 3 月開始で履歴が短く初期同期が速い。PoS にハッシュパワー競争はなく、reorg は遅延ブロックが原因の 1 ブロック程度のものが時々起きるだけ → 実 reorg は観測できればラッキー、確実な検証はテストハーネスで注入。
- トランザクションカウンター追加.
ChainCommittedで tx 数を sum + log。 - HashMap インデックスの reorg-safe 設計. ChainCommitted += / ChainReorged old -= then new += / ChainReverted -=。
- 3 アーム不変条件. 各 Address のカウント = (canonical チェーンで送られた tx 数) − (commit 後 revert されたセグメントで送られた tx 数)。
ChainReorgedの操作順序. old を先に undo してから new を apply(Reth が reorg を処理する時系列順と一致)。- 観察ポイント. Reorg ログ前後で高 tx アドレスの
tx_countをスポットチェック、古い/新しいチェーンセグメントの差と一致確認。
具体例
セットアップ:
git clone https://github.com/paradigmxyz/reth-exex-examples
cd reth-exex-examples/minimal
cargo build
Hoodi で実行(post-merge なので consensus client が必要。ドリルなら debug フラグ --debug.rpc-consensus-url で RPC からブロックを供給できる):
cargo run -- node --chain hoodi --debug.rpc-consensus-url <HOODI_RPC_URL>
トランザクションカウンター(ChainCommitted アーム拡張):
ExExNotification::ChainCommitted { new } => {
let total: usize = new.blocks().values()
.map(|b| b.body.transactions.len())
.sum();
info!(committed_chain = ?new.range(), tx_count = total, "Received commit");
}
目安: テストネットは mainnet より 1 ブロックあたりの tx がずっと少ない(0 のことも)、mainnet は数百 = ゼロレイテンシで本物のチェーンデータ読み。
Reorg-safe HashMap(フル実装):
use std::collections::HashMap;
use alloy_primitives::Address;
let mut tx_count: HashMap<Address, u64> = HashMap::new();
while let Some(notification) = ctx.notifications.try_next().await? {
match ¬ification {
ExExNotification::ChainCommitted { new } => {
for (_, block) in new.blocks() {
for tx in block.body.transactions() {
*tx_count.entry(tx.signer()).or_insert(0) += 1;
}
}
}
ExExNotification::ChainReorged { old, new } => {
// old を undo してから new を apply — 順序が大事
for (_, block) in old.blocks() {
for tx in block.body.transactions() {
*tx_count.entry(tx.signer()).or_insert(0) -= 1;
}
}
for (_, block) in new.blocks() {
for tx in block.body.transactions() {
*tx_count.entry(tx.signer()).or_insert(0) += 1;
}
}
}
ExExNotification::ChainReverted { old } => {
// old を undo、置換なし
for (_, block) in old.blocks() {
for tx in block.body.transactions() {
*tx_count.entry(tx.signer()).or_insert(0) -= 1;
}
}
}
};
if let Some(committed_chain) = notification.committed_chain() {
ctx.events.send(ExExEvent::FinishedHeight(committed_chain.tip().num_hash()))?;
}
}
3 アーム不変条件: 各 Address のカウント = (canonical で送られた tx 数) − (commit 後 revert で送られた tx 数)。
操作順序が重要: old を先に undo してから new を apply(Reth の reorg 時系列順と一致)。new 先 + old 後 の場合、共通プレフィックスがあると一旦二重カウント → old undo で復旧、ただし慣習は old-first。
Reorg 検証手順:
from_chain/to_chain範囲メモ- Reorg ログ前に高 tx アドレスの
tx_countスポットチェック → 記録 - Reorg ログ後に再記録
- 手動確認: 古い/新しいチェーンセグメントの差と一致するか
一致すれば 本番グレードインデクサ(goldsky、The Graph 等が出荷するコード)相当。
失敗例(誤解)
「ChainReverted で -= しなくても ChainCommitted で正しくなる」— 間違い。Revert された tx は canonical ではなくなった、-= 忘れるとカウント永久膨張。
「順序は new 先で良い」— 危険。共通プレフィックスがある場合、new 先 apply で一旦二重カウント → old undo で復旧、ただし中間状態が不正。Reth と同じ time-line 順(old-first)が慣習。
「再起動後にカウントは再構築できる」— 半分間違い。カウントがメモリにしかないと再起動で全消失。ノードの再 sync は不要で、ExEx 側は exex_init で ctx.set_notifications_with_head(ExExHead::new(...)) を呼べば Reth が自 DB からそのブロック以降を再実行して通知し直す。ただしメモリのみなら起点は genesis = 全履歴の再実行。カウントと処理済み最終ブロックを一緒に永続化しておく(reth-exex-examples の op-bridge / rollup は SQLite に保存)。
ステップで組み立てる
Step 1: セットアップ
reth-exex-examples clone + minimal build。
Step 2: Hoodi で実行
履歴が短く初期同期が速い。Holesky は停止済み。実 reorg はまれで浅い。
Step 3: tx カウンター追加
ChainCommitted で sum + log、実測値を確認(テストネットは mainnet よりずっと少ない)。
Step 4: 3 アーム HashMap 実装
ChainCommitted += / ChainReorged old -= then new += / ChainReverted -=。
Step 5: 不変条件を理解
各 Address のカウント = canonical で送られた tx 数 − revert されたセグメントで送られた tx 数。
Step 6: 操作順序を守る
old を先に undo、new を後で apply(Reth の reorg 時系列順)。
Step 7: Reorg を検証
ログ前後でスポットチェック、差と一致確認 → 一致すれば本番グレード。
答え合わせ
ChainReorgedで old/new を同通知で扱う論理的根拠: アトミック swap、reorg を 1 トランザクションとして扱う。old を先に undo + new を後で apply で 1 通知の処理を Reth の time-line 順と一致させる。new先だと共通プレフィックスで一旦二重カウント。- 3 アームの不変条件: 各 Address のカウント = (canonical で送られた tx 数) − (commit 後 revert で送られた tx 数)。revert アームで
-=を忘れると永久膨張、reorg アームで-=を忘れると古い + 新しいチェーンの和になる。 - 本番グレードの定義: テストネットで実際の reorg を検知(またはハーネスで注入) + 前後の
tx_count差が古い/新しいチェーンセグメントの差と一致 → goldsky や The Graph が出荷するコード相当。MEV ボット、リアルタイムリスクエンジン、ロールアップも同じ道具で実装可能。
合格基準
- Hoodi を使う理由(Holesky 停止済み + 同期が速い)と、PoS の reorg がまれで浅い理由(遅延ブロック、ハッシュパワー競争なし)を即答できる。
- 3 アーム HashMap の += / -= パターンを書ける。
- 不変条件(canonical − revert)を 1 文で言える。
- 操作順序(old undo first)の理由を Reth time-line で説明できる。
- Reorg 検証手順(前後スポットチェック)を辿れる。
まとめ(3行)
- Hoodi で 3 アーム HashMap インデクサ実装(ChainCommitted += / ChainReorged old -= then new += / ChainReverted -=)、不変条件 = canonical − revert。
- 操作順序は old を先に undo + new を後で apply(Reth time-line 順)、
new先だと共通プレフィックスで二重カウント可能。 - Reorg 観測 + スポットチェックで一致確認 → 本番グレード(goldsky / The Graph 相当)、MEV / リスクエンジン / ロールアップも同じ道具で実装。