|
| 1 | +# CLAUDE.md |
| 2 | + |
| 3 | +This file provides guidance to Claude Code (claude.ai/code) when working with code in this repository. |
| 4 | + |
| 5 | +## Development Rules |
| 6 | + |
| 7 | +- Always ensure tests pass before committing. |
| 8 | +- Run `cargo fmt --all` after every code change |
| 9 | +- Never add new dependencies unless explicitly requested |
| 10 | +- Please always disclose the use of any AI tools in commit messages and PR descriptions |
| 11 | +- When adding new `.rs` files, please ensure to always add the licensing header as found in all other files. |
| 12 | + |
| 13 | +## Architecture Overview |
| 14 | + |
| 15 | +LDK-Node is a self-custodial Lightning Network node library built on top of **LDK** (Lightning Development Kit) for Lightning functionality and **BDK** (Bitcoin Development Kit) for on-chain wallet operations. It provides a simple, ready-to-go interface for building Lightning applications with language bindings for Swift, Kotlin, and Python via UniFFI. |
| 16 | + |
| 17 | +### Core Components |
| 18 | + |
| 19 | +| Component | Location | Responsibility | |
| 20 | +|-----------|----------|----------------| |
| 21 | +| `Node` | `src/lib.rs` | Central abstraction containing all subsystems; entry point for API | |
| 22 | +| `Builder` | `src/builder.rs` | Fluent configuration interface for constructing `Node` instances | |
| 23 | +| `Wallet` | `src/wallet/` | BDK-based on-chain wallet with SQLite persistence | |
| 24 | +| `ChainSource` | `src/chain/` | Chain data abstraction (Esplora, Electrum, Bitcoin Core) | |
| 25 | +| `EventHandler` | `src/event.rs` | Translates LDK events to user-facing `Node` events | |
| 26 | +| `PaymentStore` | `src/payment/store.rs` | Persistent payment tracking with status and metadata | |
| 27 | + |
| 28 | +### Module Organization |
| 29 | + |
| 30 | +| Module | Purpose | |
| 31 | +|--------|---------| |
| 32 | +| `payment/` | Payment processing (BOLT11, BOLT12, on-chain, spontaneous, unified) | |
| 33 | +| `wallet/` | On-chain wallet abstraction, serialization, persistence | |
| 34 | +| `chain/` | Chain data sources, wallet syncing, transaction broadcasting | |
| 35 | +| `io/` | Persistence layer (`SQLiteStore`, `VssStore`, KV-store utilities) | |
| 36 | +| `liquidity.rs` | Liquidity provider integration (LSPS1/LSPS2) | |
| 37 | +| `connection.rs` | Peer connection management and reconnection logic | |
| 38 | +| `gossip.rs` | Gossip data source management (RGS, P2P) | |
| 39 | +| `graph.rs` | Network graph querying and channel/node information | |
| 40 | +| `types.rs` | Type aliases for LDK components (`ChannelManager`, `PeerManager`, etc.) | |
| 41 | +| `ffi/` | UniFFI bindings for cross-language support | |
| 42 | + |
| 43 | +### Key Design Patterns |
| 44 | + |
| 45 | +- **Arc-based Shared Ownership**: Extensive use of `Arc` for thread-safe shared components enabling background task spawning |
| 46 | +- **Event-Driven Architecture**: Events flow from LDK → `EventHandler` → `EventQueue` → User application |
| 47 | +- **Trait-based Abstraction**: `KVStore`/`KVStoreSync` for storage, `ChainSource` for chain backends, `StorableObject` for persistence |
| 48 | +- **Builder Pattern**: Fluent configuration with sensible defaults and validation during build phase |
| 49 | +- **Background Tasks**: Multiple categories (wallet sync, gossip updates, peer reconnection, fee updates, event processing) |
| 50 | + |
| 51 | +### Lifecycle |
| 52 | + |
| 53 | +**Startup (`node.start()`)**: Acquires lock → starts chain source → updates fee rates → spawns background sync tasks → sets up gossip/listeners/peer reconnection → starts event processor → marks running |
| 54 | + |
| 55 | +**Shutdown (`node.stop()`)**: Acquires lock → signals stop → aborts cancellable tasks → waits for background tasks → disconnects peers → persists final state |
| 56 | + |
| 57 | +### Type Aliases (from `types.rs`) |
| 58 | + |
| 59 | +Key LDK type aliases used throughout the codebase: |
| 60 | +- `ChannelManager` - LDK channel management |
| 61 | +- `ChainMonitor` - LDK chain monitoring |
| 62 | +- `PeerManager` - LDK peer connections |
| 63 | +- `OnionMessenger` - LDK onion messaging |
| 64 | +- `Router` - LDK pathfinding (`DefaultRouter`) |
| 65 | +- `Scorer` - Combined probabilistic + external scoring |
| 66 | +- `Graph` - `NetworkGraph` |
| 67 | +- `Sweeper` - `OutputSweeper` |
0 commit comments