$npx -y skills add lightninglabs/lightning-agent-tools --skill lnd-navigateNavigation guide for the lnd (Lightning Network Daemon) Go codebase — where to find things, key patterns, and how subsystems connect.
| 1 | The user needs help navigating or understanding the lnd codebase at `https://github.com/lightningnetwork/lnd`. Use the reference below to locate code quickly and explain how subsystems relate. |
| 2 | |
| 3 | --- |
| 4 | |
| 5 | # lnd Codebase Navigation Guide |
| 6 | |
| 7 | ## Entry Points |
| 8 | |
| 9 | | File | Role | |
| 10 | |---|---| |
| 11 | | `main.go` | Binary entry point. Parses config, calls `lnd.Main()`. | |
| 12 | | `lnd.go` | `Main()` function. Initialises chain backend, wallet, then starts `server`. | |
| 13 | | `server.go` | `server` struct. Owns all subsystem handles. `NewServer()` wires everything together; `Start()` / `Stop()` lifecycle. | |
| 14 | | `rpcserver.go` | `rpcServer` — implements the `LightningServer` gRPC interface. All external API calls enter here. | |
| 15 | | `config.go` | Top-level `Config` struct. Parsed from flags + `lnd.conf`. | |
| 16 | | `subrpcserver_config.go` | Aggregates configs for all gRPC subservers. | |
| 17 | | `accessman.go` | Per-peer slot/rate-limit manager; controls who may open channels or send messages. | |
| 18 | |
| 19 | Start reading at `server.go:NewServer()` to understand how the daemon assembles itself. |
| 20 | |
| 21 | --- |
| 22 | |
| 23 | ## Key Subsystems |
| 24 | |
| 25 | ### Payment Routing — `routing/` |
| 26 | - **`routing/router.go`** — `ChannelRouter`: the central router. Pathfinding, mission control, payment lifecycle state machine (`SendPayment`, `SendToRoute`). |
| 27 | - **`routing/pathfind.go`** — Dijkstra-based pathfinder with probability weighting. |
| 28 | - **`routing/missioncontrol.go`** / `missioncontrol_state.go` — tracks per-pair failure history to bias future path choices. |
| 29 | - **`routing/payment_lifecycle.go`** — drives a single payment from initiation through HTLC attempts to settlement or failure. |
| 30 | - **`routing/payment_session.go`** — wraps the pathfinder for one payment attempt; handles fee limits, MPP splitting. |
| 31 | - **`routing/blindedpath/`** — BOLT-12 blinded route construction (`BuildBlindedPaymentPaths`). |
| 32 | - **`routing/route/`** — `Route`, `Hop`, `Vertex` types; `ToSphinxPath` for onion packet construction. |
| 33 | - **`routing/chainview/`** — `FilteredChainView` interface; backends for btcd, bitcoind, neutrino. Feeds channel-close events to the router. |
| 34 | |
| 35 | ### HTLC Switch — `htlcswitch/` |
| 36 | - **`htlcswitch/switch.go`** — `Switch`: forwards, settles, and fails HTLC packets between links. Maintains the circuit map. |
| 37 | - **`htlcswitch/link.go`** — `channelLink`: per-channel HTLC processor. Commitment update loop, bandwidth tracking, retransmission on reconnect. |
| 38 | - **`htlcswitch/interceptable_switch.go`** — `InterceptableSwitch`: wraps Switch to support HTLC interception hooks (used by routerrpc). |
| 39 | - **`htlcswitch/hop/`** — `Payload`, `Iterator`: TLV hop payload parsing and iteration for onion packets. |
| 40 | - **`htlcswitch/hodl/`** — Flags that let tests hold HTLCs at various pipeline stages. |
| 41 | |
| 42 | ### Channel Wallet — `lnwallet/` |
| 43 | - **`lnwallet/wallet.go`** — `LightningWallet`: on-chain wallet and channel factory. Coin selection, UTXO management, channel reservation lifecycle. |
| 44 | - **`lnwallet/channel.go`** — `LightningChannel`: low-level channel state machine. Commitment transactions, HTLC updates, revocation, signing. |
| 45 | - **`lnwallet/interface.go`** — `WalletController`, `BlockChainIO`, `Signer`, `MessageSigner`, `Keychain` interfaces. |
| 46 | - **`lnwallet/reservation.go`** — `ChannelReservation`: in-flight channel open negotiation state. |
| 47 | - **`lnwallet/btcwallet/`** — Production `WalletController` backed by btcwallet. |
| 48 | - **`lnwallet/rpcwallet/`** — `RPCKeyRing` remote-signer pattern: all signing delegated to a remote gRPC endpoint; local node is watch-only. |
| 49 | - **`lnwallet/chanfunding/`** — `Assembler`/`Intent` interfaces for funding flows: `WalletAssembler` (normal), `CannedAssembler` (pre-signed), `PsbtAssembler` (PSBT external signing). |
| 50 | - **`lnwallet/chancloser/`** — `ChanCloser` cooperative-close state machine: `closeIdle` → `closingNegotiated` → `closeComplete`. |
| 51 | |
| 52 | ### Channel Funding — `funding/` |
| 53 | - **`funding/manager.go`** — `Manager`: drives the channel-open state machine from `OpenChannel` RPC through funding tx broadcast and channel ready. |
| 54 | |
| 55 | ### Channel Database — `channeldb/` |
| 56 | - **`channeldb/db.go`** — Top-level BoltDB/SQL store. Open/close channels, forwarding log, peers. |
| 57 | - **`channeldb/channel.go`** — `OpenChannel`: serialization of all channel state to disk. |
| 58 | - **`channeldb/invoices.go`** — Invoice storage (legacy; SQL path in `invoices/sql_store.go`). |
| 59 | - **`channeldb/migrations/`** — 35 sequential schema migrations (`migration_01_to_11` … `migration35`). |
| 60 | |
| 61 | ### Routing Graph Database — `graph/db/` |
| 62 | - **`graph/db/`** — `ChannelGraph` + `Store` interface. KV store (BoltDB) and SQL implementations. |
| 63 | - **`graph/db/models/`** — `LightningNode`, `ChannelEdgeInfo`, `ChannelEdgePolicy`, `CachedEdgePolicy`. |
| 64 | - The in-memory `GraphCache` lives in `graph/db/graph_cache.go`; it provides O(1) neighbor lookups during pathfinding. |
| 65 | |
| 66 | ### Invoice Registry — `invoices/` |
| 67 | - **`invoices/invoiceregistry.go`** — `InvoiceRegistry`: ce |