|
| 1 | +# CLAUDE.md |
| 2 | + |
| 3 | +This file provides guidance to Claude Code (claude.ai/code) when working with code in this repository. |
| 4 | + |
| 5 | +## Project Architecture |
| 6 | + |
| 7 | +This is a Rust workspace for the Scroll rollup node, built on the Reth framework. The architecture follows an event-driven, actor-based pattern with clear separation of concerns and extensive use of async channels for communication. |
| 8 | + |
| 9 | +### Core Components & Data Flow |
| 10 | + |
| 11 | +The system follows this critical data flow pattern: |
| 12 | + |
| 13 | +1. **L1 Watcher** (`crates/watcher/`) monitors Ethereum L1 for events |
| 14 | + - Watches for batch commits, finalizations, and L1 messages |
| 15 | + - Handles chain reorganizations and maintains unfinalized block buffer |
| 16 | + - Emits `L1Notification` events to other components |
| 17 | + |
| 18 | +2. **Chain Orchestrator** (`crates/chain-orchestrator/`) orchestrates L2 chain state |
| 19 | + - Receives L1 notifications and manages chain progression |
| 20 | + - Maintains in-memory chain buffer |
| 21 | + - Coordinates between L1 events and L2 block production |
| 22 | + |
| 23 | +3. **Derivation Pipeline** (`crates/derivation-pipeline/`) transforms L1 data to L2 payloads |
| 24 | + - Decodes batch data using scroll-codec |
| 25 | + - Streams `ScrollPayloadAttributesWithBatchInfo` to engine |
| 26 | + |
| 27 | +4. **Engine Driver** (`crates/engine/`) manages block execution |
| 28 | + - Interfaces with Reth execution engine via Engine API |
| 29 | + - Handles forkchoice updates and payload building |
| 30 | + - Manages consolidation of L1-derived blocks |
| 31 | + |
| 32 | +5. **Sequencer** (`crates/sequencer/`) creates new blocks |
| 33 | + - Builds payload attributes from L1 messages and txpool |
| 34 | + - Supports different L1 message inclusion strategies |
| 35 | + - Configurable block time and gas limits |
| 36 | + |
| 37 | +6. **Network Layer** (`crates/scroll-wire/`) handles P2P communication |
| 38 | + - Custom wire protocol for Scroll-specific block propagation |
| 39 | + - Manages peer discovery and block gossiping |
| 40 | + - Built on top of Reth networking infrastructure |
| 41 | + |
| 42 | +### Supporting Components |
| 43 | + |
| 44 | +- **Manager** (`crates/manager/`) - Top-level orchestration and command handling |
| 45 | +- **Signer** (`crates/signer/`) - Block signing with AWS KMS or private key support |
| 46 | +- **Database** (`crates/database/`) - SeaORM-based data persistence layer |
| 47 | +- **Providers** (`crates/providers/`) - Abstractions for L1, beacon, and block data |
| 48 | +- **Primitives** (`crates/primitives/`) - Shared types and data structures |
| 49 | +- **Codec** (`crates/codec/`) - Encoding/decoding for rollup data formats |
| 50 | + |
| 51 | +## Development Commands |
| 52 | + |
| 53 | +### Build and Run |
| 54 | +```bash |
| 55 | +# Build main binary |
| 56 | +cargo build --bin rollup-node |
| 57 | + |
| 58 | +# Run sequencer with test signer (bypasses signing requirements) |
| 59 | +cargo run --bin rollup-node -- node --chain dev -d --sequencer.enabled --test --http --http.api admin,debug,eth,net,trace,txpool,web3,rpc,reth,ots,flashbots,miner,mev |
| 60 | + |
| 61 | +# Production build |
| 62 | +cargo build --release --bin rollup-node |
| 63 | + |
| 64 | +# Check node help and available options |
| 65 | +cargo run --bin rollup-node -- node --help |
| 66 | +``` |
| 67 | + |
| 68 | +### Testing Strategy |
| 69 | +```bash |
| 70 | +# Run all tests (excluding docker tests) |
| 71 | +make test |
| 72 | + |
| 73 | +# Test specific crate |
| 74 | +cargo test -p <crate-name> |
| 75 | + |
| 76 | +# Run E2E tests only |
| 77 | +cargo test -p rollup-node --test e2e |
| 78 | + |
| 79 | +# Run specific E2E test |
| 80 | +cargo test -p rollup-node --test e2e test_custom_genesis_block_production_and_propagation |
| 81 | + |
| 82 | +# Run docker-based integration tests (excluded by default) |
| 83 | +cargo test --package tests --test block_propagation_multi_clients |
| 84 | + |
| 85 | +# Run with debug logs |
| 86 | +RUST_LOG=debug cargo test <test-name> |
| 87 | +``` |
| 88 | + |
| 89 | +### Linting and Code Quality |
| 90 | +```bash |
| 91 | +# Complete lint suite |
| 92 | +make lint |
| 93 | + |
| 94 | +# Individual components |
| 95 | +cargo +nightly fmt # Format code |
| 96 | +cargo +nightly clippy --workspace # Clippy lints |
| 97 | +dprint fmt # Format TOML files |
| 98 | +codespell --skip "*.json" # Spell check |
| 99 | +zepter run check # Feature uniformity check |
| 100 | +cargo +nightly udeps # Unused dependencies check |
| 101 | +``` |
| 102 | + |
| 103 | +### Pre-commit Workflow |
| 104 | +```bash |
| 105 | +# Complete pre-commit checks |
| 106 | +make pr # Runs lint + test + docs |
| 107 | +``` |
| 108 | + |
| 109 | +## Critical Implementation Details |
| 110 | + |
| 111 | +### Database Configuration |
| 112 | +- **Default**: SQLite with path `{datadir}/scroll.db?mode=rwc` |
| 113 | +- **Schema**: SeaORM migrations in `crates/database/migration/` |
| 114 | +- **Key Tables**: batch_commit, block_data, l1_message, l2_block, metadata |
| 115 | + |
| 116 | +### Sequencer Configuration |
| 117 | +- **Signer Required**: Unless `--test` flag bypasses requirement |
| 118 | + |
| 119 | +## Testing Patterns & Utilities |
| 120 | + |
| 121 | +### E2E Test Setup |
| 122 | +```rust |
| 123 | +use rollup_node::test_utils::{ |
| 124 | + setup_engine, |
| 125 | + default_test_scroll_rollup_node_config, |
| 126 | + default_sequencer_test_scroll_rollup_node_config |
| 127 | +}; |
| 128 | + |
| 129 | +// Standard E2E test pattern |
| 130 | +let (nodes, _tasks, _wallet) = setup_engine( |
| 131 | + node_config, |
| 132 | + node_count, |
| 133 | + chain_spec, |
| 134 | + enable_discovery, |
| 135 | + enable_sequencer |
| 136 | +).await?; |
| 137 | +``` |
| 138 | + |
| 139 | +### Test Configuration Patterns |
| 140 | +```rust |
| 141 | +// Test with memory database |
| 142 | +DatabaseArgs { path: Some(PathBuf::from("sqlite::memory:")) } |
| 143 | + |
| 144 | +// Test with mock blob source |
| 145 | +BeaconProviderArgs { blob_source: BlobSource::Mock, ..Default::default() } |
| 146 | + |
| 147 | +// Bypass signer in tests |
| 148 | +test: true // in ScrollRollupNodeConfig |
| 149 | +``` |
| 150 | + |
| 151 | +### Mock Providers |
| 152 | +- **MockL1Provider**: For testing L1 interactions |
| 153 | +- **MockL2Provider**: For testing L2 block data |
| 154 | +- **DatabaseL1MessageProvider**: For L1 message testing |
| 155 | + |
| 156 | +## Critical Gotchas & Non-Obvious Behavior |
| 157 | + |
| 158 | +### 1. L1 Message Processing |
| 159 | +- **Sequential Processing**: L1 messages must be processed in queue order |
| 160 | +- **Cursor Management**: Provider maintains cursor state for message iteration |
| 161 | +- **Gas Limit Enforcement**: Messages rejected if they exceed block gas limit |
| 162 | + |
| 163 | +### 2. Consensus Modes |
| 164 | +- **Noop**: No consensus validation (test mode) |
| 165 | +- **SystemContract**: Validates against authorized signer from L1 contract |
| 166 | + - **PoA**: with `consensus.authorized-signer` the signer is not read from L1 and stays constant |
| 167 | + |
| 168 | +## Debugging & Troubleshooting |
| 169 | + |
| 170 | +### Important Log Targets |
| 171 | +```bash |
| 172 | +RUST_LOG=scroll::watcher=debug # L1 watcher events |
| 173 | +RUST_LOG=rollup_node::sequencer=debug # Sequencer operations |
| 174 | +RUST_LOG=scroll::engine=debug # Engine driver events |
| 175 | +RUST_LOG=scroll::derivation=debug # Derivation pipeline |
| 176 | +RUST_LOG=scroll::network=debug # P2P networking |
| 177 | +``` |
| 178 | + |
| 179 | +### Channel Communication Debugging |
| 180 | +- Components communicate via `tokio::sync::mpsc` channels |
| 181 | +- Use `tracing::trace!` to debug message flow |
| 182 | +- Channel capacity limits defined per component |
| 183 | + |
| 184 | +## Code Navigation Guide |
| 185 | + |
| 186 | +### Key Entry Points |
| 187 | +- **Main Binary**: `crates/node/src/main.rs` |
| 188 | +- **Node Implementation**: `crates/node/src/node.rs` |
| 189 | +- **Configuration**: `crates/node/src/args.rs` |
| 190 | +- **Add-ons**: `crates/node/src/add_ons/` |
| 191 | + |
| 192 | +### Finding Specific Logic |
| 193 | +- **Batch Processing**: `crates/derivation-pipeline/src/lib.rs:252` (`derive` function) |
| 194 | +- **L1 Message Handling**: `crates/watcher/src/lib.rs:378` (`handle_l1_messages`) |
| 195 | +- **Block Building**: `crates/sequencer/src/lib.rs:213` (`build_payload_attributes`) |
| 196 | +- **Engine API**: `crates/engine/src/api.rs` |
| 197 | +- **Network Protocol**: `crates/scroll-wire/src/` |
| 198 | + |
| 199 | +## Dependencies & Framework Integration |
| 200 | + |
| 201 | +### Core Dependencies |
| 202 | +- **Reth Framework**: scroll-tech fork with Scroll-specific modifications |
| 203 | +- **Alloy**: Ethereum primitives and RPC types |
| 204 | +- **SeaORM**: Database operations with automated migrations |
| 205 | +- **Tokio**: Async runtime with extensive use of channels and streams |
| 206 | +- **scroll-alloy**: Custom consensus and RPC types for Scroll |
| 207 | + |
| 208 | +### Contributing Guidelines |
| 209 | +- Follow existing error handling patterns using `thiserror` and `eyre` |
| 210 | +- Use `tracing` for structured logging with appropriate targets |
| 211 | +- Implement comprehensive tests with appropriate mock objects |
| 212 | +- Document public APIs with examples where possible |
0 commit comments