|
| 1 | +# CLAUDE.md - Block Package |
| 2 | + |
| 3 | +This file provides guidance to Claude Code when working with the block package of ev-node. |
| 4 | + |
| 5 | +## Package Overview |
| 6 | + |
| 7 | +The block package is the core of ev-node's block management system. It handles block creation, validation, synchronization, and submission to the Data Availability (DA) layer. This package implements the block lifecycle from transaction aggregation through to finalization. |
| 8 | + |
| 9 | +## Core Components |
| 10 | + |
| 11 | +### Manager (`manager.go`) |
| 12 | +- **Purpose**: Central orchestrator for all block operations |
| 13 | +- **Key Responsibilities**: |
| 14 | + - Transaction aggregation into blocks |
| 15 | + - Block production and validation |
| 16 | + - State synchronization |
| 17 | + - DA layer interaction |
| 18 | + - P2P block/header gossiping |
| 19 | + |
| 20 | +### Aggregation (`aggregation.go`, `lazy_aggregation_test.go`) |
| 21 | +- **Purpose**: Collects transactions from mempool and creates blocks |
| 22 | +- **Modes**: |
| 23 | + - **Normal Mode**: Produces blocks at regular intervals (BlockTime) |
| 24 | + - **Lazy Mode**: Only produces blocks when transactions are present or after LazyBlockTime |
| 25 | +- **Key Functions**: |
| 26 | + - `AggregationLoop`: Main loop for block production |
| 27 | + - `lazyAggregationLoop`: Optimized for low-traffic scenarios |
| 28 | + - `normalAggregationLoop`: Regular block production |
| 29 | + |
| 30 | +### Synchronization (`sync.go`, `sync_test.go`) |
| 31 | +- **Purpose**: Keeps the node synchronized with the network |
| 32 | +- **Key Functions**: |
| 33 | + - `SyncLoop`: Main synchronization loop |
| 34 | + - Processes headers from P2P network |
| 35 | + - Retrieves block data from DA layer |
| 36 | + - Handles header and data caching |
| 37 | + |
| 38 | +### Data Availability (`da_includer.go`, `submitter.go`, `retriever.go`) |
| 39 | +- **DA Includer**: Manages DA blob inclusion proofs and validation |
| 40 | +- **Submitter**: Handles block submission to the DA layer with retry logic |
| 41 | +- **Retriever**: Fetches blocks from the DA layer |
| 42 | +- **Key Features**: |
| 43 | + - Multiple DA layer support |
| 44 | + - Configurable retry attempts |
| 45 | + - Batch submission optimization |
| 46 | + |
| 47 | +### Storage (`store.go`, `store_test.go`) |
| 48 | +- **Purpose**: Persistent storage for blocks and state |
| 49 | +- **Key Features**: |
| 50 | + - Block height tracking |
| 51 | + - Block and commit storage |
| 52 | + - State root management |
| 53 | + - Migration support for namespace changes |
| 54 | + |
| 55 | +### Pending Blocks (`pending_base.go`, `pending_headers.go`, `pending_data.go`) |
| 56 | +- **Purpose**: Manages blocks awaiting DA inclusion or validation |
| 57 | +- **Components**: |
| 58 | + - **PendingBase**: Base structure for pending blocks |
| 59 | + - **PendingHeaders**: Header queue management |
| 60 | + - **PendingData**: Block data queue management |
| 61 | +- **Key Features**: |
| 62 | + - Ordered processing by height |
| 63 | + - Validation before acceptance |
| 64 | + - Memory-efficient caching |
| 65 | + |
| 66 | +### Metrics (`metrics.go`, `metrics_helpers.go`) |
| 67 | +- **Purpose**: Performance monitoring and observability |
| 68 | +- **Key Metrics**: |
| 69 | + - Block production times |
| 70 | + - Sync status and progress |
| 71 | + - DA layer submission metrics |
| 72 | + - Transaction throughput |
| 73 | + |
| 74 | +## Key Workflows |
| 75 | + |
| 76 | +### Block Production Flow |
| 77 | +1. Transactions collected from mempool |
| 78 | +2. Block created with proper header and data |
| 79 | +3. Block executed through executor |
| 80 | +4. Block submitted to DA layer |
| 81 | +5. Block gossiped to P2P network |
| 82 | + |
| 83 | +### Synchronization Flow |
| 84 | +1. Headers received from P2P network |
| 85 | +2. Headers validated and cached |
| 86 | +3. Block data retrieved from DA layer |
| 87 | +4. Blocks applied to state |
| 88 | +5. Sync progress updated |
| 89 | + |
| 90 | +### DA Submission Flow |
| 91 | +1. Block prepared for submission |
| 92 | +2. Blob created with block data |
| 93 | +3. Submission attempted with retries |
| 94 | +4. Inclusion proof obtained |
| 95 | +5. Block marked as finalized |
| 96 | + |
| 97 | +## Configuration |
| 98 | + |
| 99 | +### Time Parameters |
| 100 | +- `BlockTime`: Target time between blocks (default: 1s) |
| 101 | +- `DABlockTime`: DA layer block time (default: 6s) |
| 102 | +- `LazyBlockTime`: Max time between blocks in lazy mode (default: 60s) |
| 103 | + |
| 104 | +### Limits |
| 105 | +- `maxSubmitAttempts`: Max DA submission retries (30) |
| 106 | +- `defaultMempoolTTL`: Blocks until tx dropped (25) |
| 107 | + |
| 108 | +## Testing Strategy |
| 109 | + |
| 110 | +### Unit Tests |
| 111 | +- Test individual components in isolation |
| 112 | +- Mock external dependencies (DA, executor, sequencer) |
| 113 | +- Focus on edge cases and error conditions |
| 114 | + |
| 115 | +### Integration Tests |
| 116 | +- Test component interactions |
| 117 | +- Verify block flow from creation to storage |
| 118 | +- Test synchronization scenarios |
| 119 | + |
| 120 | +### Performance Tests (`da_speed_test.go`) |
| 121 | +- Measure DA submission performance |
| 122 | +- Test batch processing efficiency |
| 123 | +- Validate metrics accuracy |
| 124 | + |
| 125 | +## Common Development Tasks |
| 126 | + |
| 127 | +### Adding a New DA Feature |
| 128 | +1. Update DA interfaces in `core/da` |
| 129 | +2. Modify `da_includer.go` for inclusion logic |
| 130 | +3. Update `submitter.go` for submission flow |
| 131 | +4. Add retrieval logic in `retriever.go` |
| 132 | +5. Update tests and metrics |
| 133 | + |
| 134 | +### Modifying Block Production |
| 135 | +1. Update aggregation logic in `aggregation.go` |
| 136 | +2. Adjust timing in Manager configuration |
| 137 | +3. Update metrics collection |
| 138 | +4. Test both normal and lazy modes |
| 139 | + |
| 140 | +### Implementing New Sync Strategy |
| 141 | +1. Modify `SyncLoop` in `sync.go` |
| 142 | +2. Update pending block handling |
| 143 | +3. Adjust cache strategies |
| 144 | +4. Test various network conditions |
| 145 | + |
| 146 | +## Error Handling Patterns |
| 147 | + |
| 148 | +- Use structured errors with context |
| 149 | +- Retry transient failures (network, DA) |
| 150 | +- Log errors with appropriate levels |
| 151 | +- Maintain sync state consistency |
| 152 | +- Handle node restart gracefully |
| 153 | + |
| 154 | +## Performance Considerations |
| 155 | + |
| 156 | +- Batch DA operations when possible |
| 157 | +- Use caching to reduce redundant work |
| 158 | +- Optimize header validation path |
| 159 | +- Monitor goroutine lifecycle |
| 160 | +- Profile memory usage in caches |
| 161 | + |
| 162 | +## Security Considerations |
| 163 | + |
| 164 | +- Validate all headers before processing |
| 165 | +- Verify DA inclusion proofs |
| 166 | +- Check block signatures |
| 167 | +- Prevent DOS through rate limiting |
| 168 | +- Validate state transitions |
| 169 | + |
| 170 | +## Debugging Tips |
| 171 | + |
| 172 | +- Enable debug logging for detailed flow |
| 173 | +- Monitor metrics for performance issues |
| 174 | +- Check pending queues for blockages |
| 175 | +- Verify DA layer connectivity |
| 176 | +- Inspect cache hit rates |
| 177 | + |
| 178 | +## Code Patterns to Follow |
| 179 | + |
| 180 | +- Use context for cancellation |
| 181 | +- Implement graceful shutdown |
| 182 | +- Log with structured fields |
| 183 | +- Return errors with context |
| 184 | +- Use metrics for observability |
| 185 | +- Test error conditions thoroughly |
0 commit comments