|
| 1 | +# AGENTS.md - CKB Light Client Development Guide |
| 2 | + |
| 3 | +This guide provides essential information for AI coding agents working in the ckb-light-client repository. |
| 4 | + |
| 5 | +## Project Overview |
| 6 | + |
| 7 | +CKB light client reference implementation based on RFC 44 and RFC 45. This is a Rust workspace project with both native (Rust) and WebAssembly (Wasm) targets. |
| 8 | + |
| 9 | +**Workspace Structure:** |
| 10 | +- `light-client-lib/` - Core library (native + wasm, `ckb-light-client-lib`) |
| 11 | +- `light-client-bin/` - CLI binary (native only, `ckb-light-client`) |
| 12 | +- `wasm/light-client-wasm/` - Wasm bindings |
| 13 | +- `wasm/light-client-db-worker/` - Database worker for browsers |
| 14 | +- `wasm/light-client-db-common/` - Common database code |
| 15 | +- `wasm/light-client-js/` - JavaScript/TypeScript wrapper |
| 16 | + |
| 17 | +**Rust Toolchain:** 1.92.0 (specified in `rust-toolchain`) |
| 18 | + |
| 19 | +## Build Commands |
| 20 | + |
| 21 | +### Native Rust |
| 22 | +```bash |
| 23 | +# Build all native targets |
| 24 | +make build |
| 25 | +cargo build |
| 26 | + |
| 27 | +# Build release |
| 28 | +cargo build --release |
| 29 | + |
| 30 | +# Build specific package |
| 31 | +cargo build -p ckb-light-client |
| 32 | +cargo build -p ckb-light-client-lib |
| 33 | +``` |
| 34 | + |
| 35 | +### WebAssembly |
| 36 | +```bash |
| 37 | +# Install dependencies and build all wasm packages |
| 38 | +make build-wasm |
| 39 | + |
| 40 | +# Equivalent to: |
| 41 | +npm install |
| 42 | +npm run build -ws |
| 43 | +``` |
| 44 | + |
| 45 | +## Testing |
| 46 | + |
| 47 | +### Run All Tests |
| 48 | +```bash |
| 49 | +# Run native tests using cargo-nextest (required) |
| 50 | +make test |
| 51 | + |
| 52 | +# Runs: |
| 53 | +cargo nextest run --hide-progress-bar --success-output immediate \ |
| 54 | + --failure-output immediate -p ckb-light-client-lib -p ckb-light-client |
| 55 | +``` |
| 56 | + |
| 57 | +### Run Tests on macOS |
| 58 | +```bash |
| 59 | +# Use portable feature on macOS |
| 60 | +make test-portable |
| 61 | + |
| 62 | +# Runs with feature flag: |
| 63 | +cargo nextest run --features portable ... |
| 64 | +``` |
| 65 | + |
| 66 | +### Run Single Test |
| 67 | +```bash |
| 68 | +# Run a specific test by name |
| 69 | +cargo nextest run -p <package-name> <test-name> |
| 70 | + |
| 71 | +# Examples: |
| 72 | +cargo nextest run -p ckb-light-client-lib test_verify_blocks_proof |
| 73 | +cargo nextest run -p ckb-light-client synchronizer::tests |
| 74 | +``` |
| 75 | + |
| 76 | +### Run Wasm Tests |
| 77 | +```bash |
| 78 | +# Run wasm-pack tests for browser environment |
| 79 | +make test-wasm |
| 80 | + |
| 81 | +# Runs: |
| 82 | +wasm-pack test --node ./wasm/light-client-db-common/ |
| 83 | +``` |
| 84 | + |
| 85 | +### JavaScript/TypeScript Tests |
| 86 | +```bash |
| 87 | +# Run tests in wasm workspace packages |
| 88 | +npm test -ws |
| 89 | + |
| 90 | +# Or for specific package: |
| 91 | +cd wasm/light-client-js && npm test |
| 92 | +``` |
| 93 | + |
| 94 | +## Linting & Formatting |
| 95 | + |
| 96 | +### Format Check |
| 97 | +```bash |
| 98 | +# Check formatting (CI mode) |
| 99 | +make fmt |
| 100 | + |
| 101 | +# Runs: |
| 102 | +cargo fmt --all --check |
| 103 | + |
| 104 | +# Auto-fix formatting |
| 105 | +cargo fmt --all |
| 106 | +``` |
| 107 | + |
| 108 | +### Clippy Linting |
| 109 | +```bash |
| 110 | +# Run clippy with warnings as errors |
| 111 | +make clippy |
| 112 | + |
| 113 | +# This runs two separate checks: |
| 114 | +# 1. Wasm targets |
| 115 | +cargo clippy --target wasm32-unknown-unknown \ |
| 116 | + -p light-client-wasm -p ckb-light-client-lib \ |
| 117 | + -p light-client-db-common -p light-client-db-worker \ |
| 118 | + --locked -- --deny warnings |
| 119 | + |
| 120 | +# 2. Native targets |
| 121 | +cargo clippy -p ckb-light-client --locked -- --deny warnings |
| 122 | +``` |
| 123 | + |
| 124 | +## Code Style Guidelines |
| 125 | + |
| 126 | +### Imports |
| 127 | +- Group imports by: std library, external crates, internal crates |
| 128 | +- Use explicit imports rather than glob imports |
| 129 | +- Example from codebase: |
| 130 | + ```rust |
| 131 | + use std::{fmt, result}; |
| 132 | + |
| 133 | + use thiserror::Error; |
| 134 | + |
| 135 | + use crate::storage; |
| 136 | + ``` |
| 137 | + |
| 138 | +### Error Handling |
| 139 | +- Use `thiserror` for error types |
| 140 | +- Define custom error enums with `#[derive(Error, Debug)]` |
| 141 | +- Use descriptive error messages with `#[error("...")]` |
| 142 | +- Create custom `Result<T>` type aliases: |
| 143 | + ```rust |
| 144 | + pub type Result<T> = result::Result<T, Error>; |
| 145 | + ``` |
| 146 | +- Provide helper methods for error construction: |
| 147 | + ```rust |
| 148 | + impl Error { |
| 149 | + pub fn config<T: fmt::Display>(inner: T) -> Self { |
| 150 | + Self::Config(inner.to_string()) |
| 151 | + } |
| 152 | + } |
| 153 | + ``` |
| 154 | + |
| 155 | +### Types and Conversions |
| 156 | +- Implement `From` trait for type conversions between internal and external types |
| 157 | +- Use `#[serde(rename_all = "snake_case")]` for JSON serialization |
| 158 | +- Use `#[cfg(target_arch = "wasm32")]` for platform-specific code |
| 159 | +- Example: |
| 160 | + ```rust |
| 161 | + impl From<ScriptType> for storage::ScriptType { |
| 162 | + fn from(st: ScriptType) -> Self { |
| 163 | + match st { |
| 164 | + ScriptType::Lock => Self::Lock, |
| 165 | + ScriptType::Type => Self::Type, |
| 166 | + } |
| 167 | + } |
| 168 | + } |
| 169 | + ``` |
| 170 | + |
| 171 | +### Naming Conventions |
| 172 | +- Use `snake_case` for functions, variables, and modules |
| 173 | +- Use `PascalCase` for types, structs, enums, and traits |
| 174 | +- Use `SCREAMING_SNAKE_CASE` for constants |
| 175 | +- Prefer descriptive names over abbreviations |
| 176 | + |
| 177 | +### Modules and Organization |
| 178 | +- Keep module declarations in `lib.rs` or `main.rs` |
| 179 | +- Use `mod.rs` files for module organization |
| 180 | +- Separate concerns: protocols, storage, utils, service, error |
| 181 | + |
| 182 | +### Testing |
| 183 | +- Place unit tests in the same file or dedicated `tests` submodule |
| 184 | +- Use `#[cfg(test)]` attribute for test-only code |
| 185 | +- Use `#[macro_use]` for test macros when needed |
| 186 | +- Integration tests go in workspace package's `tests/` directory |
| 187 | + |
| 188 | +### Documentation |
| 189 | +- Add doc comments for public APIs |
| 190 | +- Use `///` for documentation comments |
| 191 | +- Include examples in doc comments when helpful |
| 192 | + |
| 193 | +### Conditional Compilation |
| 194 | +- Use `#[cfg(not(target_arch = "wasm32"))]` for native-only code (e.g., RocksDB) |
| 195 | +- Use `#[cfg(target_arch = "wasm32")]` for wasm-only code (e.g., IndexedDB) |
| 196 | +- Keep platform-specific code isolated in separate modules when possible |
| 197 | + |
| 198 | +### Warnings |
| 199 | +- CI enforces `-D warnings` (all warnings treated as errors) |
| 200 | +- Use `#![allow(clippy::mutable_key_type)]` sparingly and only when justified |
| 201 | +- Address clippy warnings rather than silencing them |
| 202 | + |
| 203 | +## Development Workflow |
| 204 | + |
| 205 | +1. Make code changes |
| 206 | +2. Run `cargo fmt --all` to format code |
| 207 | +3. Run `make clippy` to check for lints |
| 208 | +4. Run `make test` (or `make test-portable` on macOS) for native tests |
| 209 | +5. Run `make test-wasm` if touching wasm code |
| 210 | +6. For wasm changes, run `npm test -ws` for JS/TS tests |
| 211 | + |
| 212 | +## CI Configuration |
| 213 | + |
| 214 | +The project uses GitHub Actions (`.github/workflows/ci.yaml`): |
| 215 | +- Runs on: Ubuntu, macOS, Windows |
| 216 | +- Steps: Format check, Clippy, Native tests, Wasm tests, Code coverage |
| 217 | +- All warnings treated as errors with `RUSTFLAGS: -D warnings` |
0 commit comments