diff --git a/docs/.vuepress/config.js b/docs/.vuepress/config.js index 69f590d68..c8c02bbc7 100644 --- a/docs/.vuepress/config.js +++ b/docs/.vuepress/config.js @@ -92,8 +92,19 @@ const tutorialSidebar = [ collapsable: false, children: [ '/tutorials/getting-started', - '/tutorials/build_a_node_in_java', - '/tutorials/build_a_node_in_rust' + { + title: 'Building a node with LDK', + collapsable: false, + children: [ + ['/tutorials/building-a-node-with-ldk/introduction', 'Introduction'], + ['/tutorials/building-a-node-with-ldk/setting-up-a-channel-manager', 'Setting up a Channel Manager'], + ['/tutorials/building-a-node-with-ldk/handling-events', 'Handling Events'], + ['/tutorials/building-a-node-with-ldk/setting-up-a-peer-manager', 'Setting up a Peer Manager'], + ['/tutorials/building-a-node-with-ldk/connect-to-peers', 'Connect to Peers'], + ['/tutorials/building-a-node-with-ldk/opening-a-channel', 'Opening a Channel'], + ['/tutorials/building-a-node-with-ldk/sending-payments', 'Sending Payments'] + ] + }, ], } ] diff --git a/docs/tutorials/building-a-node-with-ldk/connect-to-peers.md b/docs/tutorials/building-a-node-with-ldk/connect-to-peers.md new file mode 100644 index 000000000..402ed1113 --- /dev/null +++ b/docs/tutorials/building-a-node-with-ldk/connect-to-peers.md @@ -0,0 +1,107 @@ +# Connect to Peers + +In this section you'll learn how to join the lightning network. + +Firstly we need to have the ability to do high performance I/O operations. LDK provides default implementations for initializing all of your networking needs. If you are using Rust, you can use our simple socket handling library `lightning_net_tokio`. In Kotlin/Java you can use the `NioPeerHandler` which uses Java's NIO I/O interface. + +**What it's used for**: making peer connections, facilitating peer data to and from LDK + + + + + + + + +Connections to other peers are established with `PeerManager`. You'll need to know the pubkey and address of another node that you want as a peer. Once the connection is established and the handshake is complete, `PeerManager` will show the peer's pubkey in its list of peers. + + + + + + + +**Dependencies:** `PeerManager` + +**References:** [Rust `lightning-net-tokio` docs](https://docs.rs/lightning-net-tokio/*/lightning_net_tokio/), [Rust `PeerManager` docs](https://docs.rs/lightning/*/lightning/ln/peer_handler/struct.PeerManager.html), [Java `NioPeerHandler` bindings](https://github.com/lightningdevkit/ldk-garbagecollected/blob/main/src/main/java/org/ldk/batteries/NioPeerHandler.java), +[Java `PeerManager` bindings](https://github.com/lightningdevkit/ldk-garbagecollected/blob/main/src/main/java/org/ldk/structs/PeerManager.java), + + + + + + + diff --git a/docs/tutorials/building-a-node-with-ldk/handling-events.md b/docs/tutorials/building-a-node-with-ldk/handling-events.md new file mode 100644 index 000000000..44cd0fa25 --- /dev/null +++ b/docs/tutorials/building-a-node-with-ldk/handling-events.md @@ -0,0 +1,47 @@ +# Handling Events + +LDK requires that you handle many different events throughout your app's life cycle. You can learn more by reading about our event-driven [architecture](/overview/architecture.md). + +To start handling events in your application, run: + + + + + + + +References: [Rust `Event` docs](https://docs.rs/lightning/0.0.114/lightning/util/events/enum.Event.html), [Java `Event` bindings](https://github.com/lightningdevkit/ldk-garbagecollected/blob/main/src/main/java/org/ldk/structs/Event.java) \ No newline at end of file diff --git a/docs/tutorials/building-a-node-with-ldk/introduction.md b/docs/tutorials/building-a-node-with-ldk/introduction.md new file mode 100644 index 000000000..a355f1c53 --- /dev/null +++ b/docs/tutorials/building-a-node-with-ldk/introduction.md @@ -0,0 +1,35 @@ +# Building a Node with LDK + +## Learn how to build a basic LDK node from scratch + +::: tip Note +For an integrated example of an LDK node in Rust, see the [Sample Node](https://github.com/lightningdevkit/ldk-sample) +::: + +The following tutorials will show you how to build the simplest lightning node using LDK, that fufills the following tasks: + +1. **Connecting to Peers** +2. **Open Channels** +3. **Send Payments** +4. **Receive Payments** +5. **Close Channels** + +### Foundational Components + +Let's start by looking at the core components we'll need to make this node work for the tasks we outlined above. + +1. A [`PeerManager`](https://docs.rs/lightning/*/lightning/ln/peer_handler/struct.PeerManager.html), for establishing TCP/IP connections to other nodes on the lightning network. +2. A [`ChannelManager`](https://docs.rs/lightning/*/lightning/ln/channelmanager/struct.ChannelManager.html), to open and close channels. +3. Payments & Routing, ability to create and pay invoices. + +To make the above work we also need to setup a series of supporting modules, including: +1. A [`FeeEstimator`](https://docs.rs/lightning/*/lightning/chain/chaininterface/trait.FeeEstimator.html) +2. A [`Logger`](https://docs.rs/lightning/*/lightning/util/logger/index.html) +3. A Transaction [`Broadcaster`](https://docs.rs/lightning/*/lightning/chain/chaininterface/trait.BroadcasterInterface.html) +4. A [`NetworkGraph`](https://docs.rs/lightning/*/lightning/routing/gossip/struct.NetworkGraph.html) +5. A [`Persister`](https://docs.rs/lightning/*/lightning/util/persist/trait.Persister.html) +6. An [`EventHandler`](https://docs.rs/lightning/*/lightning/util/events/trait.EventHandler.html) +7. A Transaction [`Filter`](https://docs.rs/lightning/*/lightning/chain/trait.Filter.html) +8. A [`ChainMonitor`](https://docs.rs/lightning/*/lightning/chain/chainmonitor/index.html) +9. A [`KeysManager`](https://docs.rs/lightning/*/lightning/chain/keysinterface/struct.KeysManager.html) +10. A [`Scorer`](https://docs.rs/lightning/*/lightning/routing/scoring/index.html) diff --git a/docs/tutorials/building-a-node-with-ldk/opening-a-channel.md b/docs/tutorials/building-a-node-with-ldk/opening-a-channel.md new file mode 100644 index 000000000..ca3db0a8b --- /dev/null +++ b/docs/tutorials/building-a-node-with-ldk/opening-a-channel.md @@ -0,0 +1,223 @@ +# Opening a Channel + +Channels are the basic building blocks of the Lightning Network. With channels, you can transact not only with your immediate peers but with others on the network. Let's explore how to open a channel with LDK. + +Now that you have a peer, you can open a channel with them using `ChannelManager`. You'll need the peer's pubkey as before along with: + +- the amount in sats to use when funding the channel, +- any msats to push to your peer, +- an id which is given back in the `FundingGenerationReady` event, +- an optional `UserConfig` for overriding `ChannelManager` defaults + +Channels can be announced to the network or can remain private, which is controlled via `UserConfig::announced_channel`. + + + + + + + +# FundingGenerationReady Event Handling + +At this point, an outbound channel has been initiated with your peer and it will appear in `ChannelManager::list_channels`. However, the channel is not yet funded. Once your peer accepts the channel, you will be notified with a `FundingGenerationReady` event. It's then your responsibility to construct the funding transaction and pass it to ChannelManager, which will broadcast it once it receives your channel counterparty's signature. + +::: tip Note + +Remember that the funding transaction must only spend SegWit inputs. + +::: + + + + + + + + +**References:** [Rust `FundingGenerationReady` docs](https://docs.rs/lightning/*/lightning/util/events/enum.Event.html#variant.FundingGenerationReady), [Java `FundingGenerationReady` bindings](https://github.com/lightningdevkit/ldk-garbagecollected/blob/main/src/main/java/org/ldk/structs/Event.java#L95) +# Broadcasting the Funding Transaction + +After crafting the funding transaction you'll need to send it to the Bitcoin network where it will hopefully be mined and added to the blockchain. You'll need to watch this transaction and wait for a minimum of 6 confirmations before the channel is ready to use. + + + + + + + +::: tip Keep LDK in sync + +Remember if you are restarting and have open channels then you should [let LDK know about the latest channel state.](./setting-up-a-channel-manager/#sync-channelmonitors-and-channelmanager-to-chain-tip) + +::: + + + + + + diff --git a/docs/tutorials/building-a-node-with-ldk/sending-payments.md b/docs/tutorials/building-a-node-with-ldk/sending-payments.md new file mode 100644 index 000000000..f12d8544a --- /dev/null +++ b/docs/tutorials/building-a-node-with-ldk/sending-payments.md @@ -0,0 +1,169 @@ +# Sending Payments + +Lightning payments are used to pay invoices, which are typically encoded as a +string in accordance with BOLT 11. After parsing the invoice, you'll need to +find a route from your node to the recipient and then make the payment using +`ChannelManager`. + + + + + + + +An event is generated once a payment has completed. Successful payments result +in a `PaymentSent` event with the preimage of the payment hash. Be sure to look +out for a `PaymentFailed` event, if the payment fails for some reason, and act +accordingly. + + + + + diff --git a/docs/tutorials/building-a-node-with-ldk/setting-up-a-channel-manager.md b/docs/tutorials/building-a-node-with-ldk/setting-up-a-channel-manager.md new file mode 100644 index 000000000..db3374ebe --- /dev/null +++ b/docs/tutorials/building-a-node-with-ldk/setting-up-a-channel-manager.md @@ -0,0 +1,796 @@ +# Setting up a ChannelManager + +The ChannelManager is responsible for several tasks related to managing channel state. This includes keeping track of many channels, sending messages to appropriate channels, creating channels and more. + +## Adding a ChannelManager + +To add a `ChannelManager` to your application, run: + + + + + + + +There are a few dependencies needed to get this working. Let's walk through setting up each one so we can plug them into our `ChannelManager`. + +### Initialize the `FeeEstimator` + +**What it's used for:** estimating fees for on-chain transactions that LDK wants broadcasted. + + + + + + + +**Implementation notes:** +1. Fees must be returned in: satoshis per 1000 weight units +2. Fees must be no smaller than 253 (equivalent to 1 satoshi/vbyte, rounded up) +3. To reduce network traffic, you may want to cache fee results rather than +retrieving fresh ones every time + +**Dependencies:** *none* + +**References:** [Rust `FeeEstimator` docs](https://docs.rs/lightning/*/lightning/chain/chaininterface/trait.FeeEstimator.html), [Java `FeeEstimator` bindings](https://github.com/lightningdevkit/ldk-garbagecollected/blob/main/src/main/java/org/ldk/structs/FeeEstimator.java) + +### Initialize the `Logger` +**What it's used for:** LDK logging + + + + + + + +**Implementation notes:** you'll likely want to write the logs to a file for debugging purposes. + +**Dependencies:** *none* + +**References:** [Rust `Logger` docs](https://docs.rs/lightning/*/lightning/util/logger/trait.Logger.html), [Java `Logger` bindings](https://github.com/lightningdevkit/ldk-garbagecollected/blob/main/src/main/java/org/ldk/structs/Logger.java) + +### Initialize the `BroadcasterInterface` +**What it's used for:** broadcasting various transactions to the bitcoin network + + + + + + + +**Dependencies:** *none* + +**References:** [Rust `BroadcasterInterface` docs](https://docs.rs/lightning/*/lightning/chain/chaininterface/trait.BroadcasterInterface.html), [Java `BroadcasterInterface` bindings](https://github.com/lightningdevkit/ldk-garbagecollected/blob/main/src/main/java/org/ldk/structs/BroadcasterInterface.java) + +### Initialize `Persist` +**What it's used for:** persisting `ChannelMonitor`s, which contain crucial channel data, in a timely manner + + + + + + + + + + + +**Implementation notes:** +* `ChannelMonitor`s are objects which are capable of +responding to on-chain events for a given channel. Thus, you will have one +`ChannelMonitor` per channel. They are persisted in real-time and the `Persist` +methods will block progress on sending or receiving payments until they return. +You must ensure that `ChannelMonitor`s are durably persisted to disk before +returning or you may lose funds. +* If you implement a custom persister, it's important to read the trait docs (linked in References) to make sure you satisfy the API requirements, particularly for `update_persisted_channel` + +**Dependencies:** *none* + +**References:** [Rust `Persister` docs](https://docs.rs/lightning/*/lightning/chain/chainmonitor/trait.Persist.html), [Java `Persister` bindings](https://github.com/lightningdevkit/ldk-garbagecollected/blob/main/src/main/java/org/ldk/structs/Persist.java) + +### Optional: Initialize the Transaction `Filter` +**You must follow this step if:** you are *not* providing full blocks to LDK, +i.e. if you're using BIP 157/158 or Electrum as your chain backend + +**What it's used for:** if you are not providing full blocks, LDK uses this +object to tell you what transactions and outputs to watch for on-chain. You'll +inform LDK about these transactions/outputs in Step 14. + + + + + + + + +**Implementation notes:** see the [Blockchain Data](/blockchain_data/introduction.md) guide for more info + +**Dependencies:** *none* + +**References:** [Rust `Filter` docs](https://docs.rs/lightning/*/lightning/chain/trait.Filter.html), [Java `Filter` bindings](https://github.com/lightningdevkit/ldk-garbagecollected/blob/main/src/main/java/org/ldk/structs/Filter.java) + +### Initialize the `ChainMonitor` +**What it's used for:** tracking one or more `ChannelMonitor`s and using them to monitor the chain for lighting transactions that are relevant to our node, and broadcasting transactions if need be. + + + + + + + +**Implementation notes:** `Filter` must be non-`None` if you're using Electrum or BIP 157/158 as your chain backend + +**Dependencies:** `FeeEstimator`, `Logger`, `BroadcasterInterface`, `Persist` + +**Optional dependency:** `Filter` + +**References:** [Rust `ChainMonitor` docs](https://docs.rs/lightning/*/lightning/chain/chainmonitor/struct.ChainMonitor.html), [Java `ChainMonitor` bindings](https://github.com/lightningdevkit/ldk-garbagecollected/blob/main/src/main/java/org/ldk/structs/ChainMonitor.java) + +### Initialize the `KeysManager` +**What it's used for:** providing keys for signing Lightning transactions + + + + + + + +**Implementation notes:** +* See the [Key Management](/key_management.md) guide for more info +* Note that you must write the `key_seed` you give to the `KeysManager` on + startup to disk, and keep using it to initialize the `KeysManager` every time + you restart. This `key_seed` is used to derive your node's secret key (which + corresponds to its node pubkey) and all other secret key material. +* The current time is part of the `KeysManager`'s parameters because it is used to derive +random numbers from the seed where required, to ensure all random +generation is unique across restarts. + +**Dependencies:** random bytes + +**References:** [Rust `KeysManager` docs](https://docs.rs/lightning/*/lightning/chain/keysinterface/struct.KeysManager.html), [Java `KeysManager` bindings](https://github.com/lightningdevkit/ldk-garbagecollected/blob/main/src/main/java/org/ldk/structs/KeysManager.java) + +### Read `ChannelMonitor` state from disk + +**What it's used for:** if LDK is restarting and has at least 1 channel, its `ChannelMonitor`s will need to be (1) fed to the `ChannelManager` and (2) synced to chain. + + + + + + + +**Dependencies:** `KeysManager` + +**References:** [Rust `load_outputs_to_watch` docs](https://docs.rs/lightning/*/lightning/chain/channelmonitor/struct.ChannelMonitor.html#method.load_outputs_to_watch) + +### Initialize the `ChannelManager` +**What it's used for:** managing channel state + + + + + + + +**Implementation notes:** No methods should be called on `ChannelManager` until +*after* the `ChannelMonitor`s and `ChannelManager` are synced to the chain tip (next step). + +**Dependencies:** `KeysManager`, `FeeEstimator`, `ChainMonitor`, `BroadcasterInterface`, `Logger` +* If restarting: `ChannelMonitor`s and `ChannelManager` bytes from Step 7 and Step 18 respectively + +**References:** [Rust `ChannelManager` docs](https://docs.rs/lightning/*/lightning/ln/channelmanager/struct.ChannelManager.html), [Java `ChannelManager` bindings](https://github.com/lightningdevkit/ldk-garbagecollected/blob/main/src/main/java/org/ldk/structs/ChannelManager.java) + +### Sync `ChannelMonitor`s and `ChannelManager` to chain tip +**What it's used for:** this step is only necessary if you're restarting and have open channels. This step ensures that LDK channel state is up-to-date with the bitcoin blockchain + +**Example:** + + + + + + + +**Implementation notes:** + +There are 2 main options for synchronizing to chain on startup: + +**Full Blocks or BIP 157/158** + +If you are connecting full blocks or using BIP 157/158, then it is recommended to use +LDK's `lightning_block_sync` sample module as in the example above: the high-level steps that must be done for both `ChannelManager` and each `ChannelMonitor` are as follows: + +1. Get the last blockhash that each object saw. + * Receive the latest block hash when through [deserializtion](https://docs.rs/lightning/*/lightning/ln/channelmanager/struct.ChannelManagerReadArgs.html) of the `ChannelManager` via `read()` + * Each `ChannelMonitor`'s is in `channel_manager.channel_monitors`, as the 2nd element in each tuple +2. For each object, if its latest known blockhash has been reorged out of the chain, then disconnect blocks using `channel_manager.as_Listen().block_disconnected(..)` or `channel_monitor.block_disconnected(..)` until you reach the last common ancestor with the main chain. +3. For each object, reconnect blocks starting from the common ancestor until it gets to your best known chain tip using `channel_manager.as_Listen().block_connected(..)` and/or `channel_monitor.block_connected(..)`. +4. Call `channel_manager.chain_sync_completed(..)` to complete the initial sync process. + +**Esplora** + +Alternatively, you can use LDK's `lightning-transaction-sync` crate. This provides utilities for syncing LDK via the transaction-based `Confirm` interface. + +**Electrum/Esplora** + +Otherwise, you can use LDK's `Confirm` interface directly as in the examples above. The high-level steps are as follows: + 1. Tell LDK about relevant confirmed and unconfirmed transactions. + 2. Tell LDK what your best known block header and height is. + 3. Call `channel_manager_constructor.chain_sync_completed(..)` to complete the initial sync process. + +**References:** [Rust `Confirm` docs](https://docs.rs/lightning/*/lightning/chain/trait.Confirm.html), [Rust `Listen` docs](https://docs.rs/lightning/*/lightning/chain/trait.Listen.html), [Rust `lightning_block_sync` module docs](https://docs.rs/lightning-block-sync/*/lightning_block_sync/), [Rust `lightning_transaction_sync` module docs](https://docs.rs/lightning-transaction-sync/*/lightning_transaction_sync/) + +**Dependencies:** `ChannelManager`, `ChainMonitor`, `ChannelMonitor`s +* If providing providing full blocks or BIP 157/158: set of `ChannelMonitor`s +* If using Electrum: `ChainMonitor` + +### Optional: Initialize `P2PGossipSync or RapidGossipSync` + +**You must follow this step if:** you need LDK to provide routes for sending payments (i.e. you are *not* providing your own routes) + +**What it's used for:** generating routes to send payments over + + + + + + + + + + + +**Implementation notes:** this struct is not required if you are providing your own routes. It will be used internally in `ChannelManager` to build a `NetworkGraph`. Other networking options are: `LDKNetwork_Bitcoin`, `LDKNetwork_Regtest` and `LDKNetwork_Testnet` + +**Dependencies:** `Logger` + +**Optional dependency:** `Access`, a source of chain information. Recommended to be able to verify channels before adding them to the internal network graph. + +**References:** [Rust `P2PGossipSync` docs](https://docs.rs/lightning/*/lightning/routing/gossip/struct.P2PGossipSync.html), [`Access` docs](https://docs.rs/lightning/*/lightning/chain/trait.Access.html), [Java `P2PGossipSync` bindings](https://github.com/lightningdevkit/ldk-garbagecollected/blob/main/src/main/java/org/ldk/structs/P2PGossipSync.java), [Rust `RapidGossipSync` docs](https://docs.rs/lightning-rapid-gossip-sync/*/lightning_rapid_gossip_sync/), [Java `RapidGossipSync` bindings](https://github.com/lightningdevkit/ldk-garbagecollected/blob/main/src/main/java/org/ldk/structs/RapidGossipSync.java) + diff --git a/docs/tutorials/building-a-node-with-ldk/setting-up-a-peer-manager.md b/docs/tutorials/building-a-node-with-ldk/setting-up-a-peer-manager.md new file mode 100644 index 000000000..f1aeca96f --- /dev/null +++ b/docs/tutorials/building-a-node-with-ldk/setting-up-a-peer-manager.md @@ -0,0 +1,51 @@ +# Setting up a PeerManager + +The Peer Manager is responsible for managing a set of peer connections and all data associated with those peers. + + +## Adding a PeerManager + +To add a PeerManager to your application, run: + + + + + + + +**Implementation notes:** if you did not initialize `P2PGossipSync` in the previous step, you can initialize your own struct (which can be a dummy struct) that implements `RoutingMessageHandler` + +**Dependencies:** `ChannelManager`, `RoutingMessageHandler`, `KeysManager`, random bytes, `Logger` + +**References:** [Rust `PeerManager` docs](https://docs.rs/lightning/*/lightning/ln/peer_handler/struct.PeerManager.html), [Rust `RoutingMessageHandler` docs](https://docs.rs/lightning/*/lightning/ln/msgs/trait.RoutingMessageHandler.html), [Java `PeerManager` bindings](https://github.com/lightningdevkit/ldk-garbagecollected/blob/main/src/main/java/org/ldk/structs/PeerManager.java), [Java `RoutingMessageHandler` bindings](https://github.com/lightningdevkit/ldk-garbagecollected/blob/main/src/main/java/org/ldk/structs/RoutingMessageHandler.java) + diff --git a/docs/tutorials/getting-started.md b/docs/tutorials/getting-started.md index 3a28a8e09..8eafa5c45 100644 --- a/docs/tutorials/getting-started.md +++ b/docs/tutorials/getting-started.md @@ -9,7 +9,7 @@ MacOS, Windows and Linux are supported. ## Installation To add LDK to a project, run: - + -