Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Consolidates building a node tutorial #156

Merged

Conversation

ConorOkus
Copy link
Contributor

@ConorOkus ConorOkus commented Aug 8, 2022

This uses the new CodeSwitcher so we can have one Building a node tutorial but have code examples for multiple languages in one place. For instance, clicking the "Java" tab changes all the code examples on the page to be Java. This PR includes Rust, Java, Kotlin. Swift + JavaScript/TypeScript to follow

It also reworks the content structure to be more in line with how we currently explain LDK.

TO-DO

  • Introduction
  • Setting up a Channel Manager
  • Setting up a Peer Manager
  • Connect to Peers
  • Opening channels
  • Send payments
  • Receive payments
  • Close channels

Includes minor changes to the navigation layout.

Preview

@netlify
Copy link

netlify bot commented Aug 8, 2022

Deploy Preview for lightningdevkit ready!

Name Link
🔨 Latest commit cd008da
🔍 Latest deploy log https://app.netlify.com/sites/lightningdevkit/deploys/65c515a876ed010008a518cd
😎 Deploy Preview https://deploy-preview-156--lightningdevkit.netlify.app
📱 Preview on mobile
Toggle QR Code...

QR Code

Use your smartphone camera to open QR code link.

To edit notification comments on pull requests, go to your Netlify site configuration.

@moneyball
Copy link
Contributor

Concept ACK

@ConorOkus ConorOkus force-pushed the building-a-node-with-ldk-intro branch 2 times, most recently from e81534c to 2a05a55 Compare September 8, 2022 16:38
@ConorOkus ConorOkus force-pushed the building-a-node-with-ldk-intro branch from 2a05a55 to c597f31 Compare October 5, 2022 14:48
@ConorOkus ConorOkus force-pushed the building-a-node-with-ldk-intro branch from c597f31 to bf123ea Compare January 9, 2023 15:54
@ConorOkus ConorOkus force-pushed the building-a-node-with-ldk-intro branch from 8cda8b0 to 95ca4ce Compare January 25, 2023 14:54

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`
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We should provide an example using BDK's new fee estimator API.

Copy link
Contributor

@tnull tnull left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Did a quick scroll through and annotated what stuck out to me. Feel free to disregard if many of the things are still WIP.

@ConorOkus ConorOkus force-pushed the building-a-node-with-ldk-intro branch from de5e256 to 4bd2cde Compare March 24, 2023 21:47
@ConorOkus ConorOkus force-pushed the building-a-node-with-ldk-intro branch from 4bd2cde to c1fef0b Compare September 14, 2023 16:53
@ConorOkus ConorOkus force-pushed the building-a-node-with-ldk-intro branch from 52c58df to f8f7404 Compare October 19, 2023 14:52
Copy link
Contributor

@tnull tnull left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Did a ~full high-level pass.

While it's nice to have a single flow, I'm not fully convinced that a unified tutorial improves readability. I think at times it can be confusing where the APIs or implementation behavior of the different languages diverge slightly.

docs/tutorials/building-a-node-with-ldk/introduction.md Outdated Show resolved Hide resolved
docs/tutorials/building-a-node-with-ldk/introduction.md Outdated Show resolved Hide resolved
docs/tutorials/building-a-node-with-ldk/introduction.md Outdated Show resolved Hide resolved
docs/tutorials/building-a-node-with-ldk/introduction.md Outdated Show resolved Hide resolved
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)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think it would be nice if each of these list items would hold a short description giving some context what the respective modules are used/required for.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It would mostly be repeating what is in the Rust docs though so I'd rather just lean on that.

Event::PaymentFailed { payment_hash, rejected_by_dest } => {
// Handle failed payment
}
Event::FundingGenerationReady { .. } =>
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think we need to at least mention PaymentClaimable,PendingHTLCsForwardable, SpendableOutputs here, as they really need to be implemented. Possibly also BumpTransaction if users use Anchors.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@jkczyz jkczyz self-requested a review October 20, 2023 14:41
@ConorOkus ConorOkus force-pushed the building-a-node-with-ldk-intro branch from a7fa74b to fe72a99 Compare January 3, 2024 19:01
@ConorOkus ConorOkus force-pushed the building-a-node-with-ldk-intro branch from 6c08290 to 5e80f4e Compare February 5, 2024 17:36
Copy link
Collaborator

@jkczyz jkczyz left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Did a ~full high-level pass.

While it's nice to have a single flow, I'm not fully convinced that a unified tutorial improves readability. I think at times it can be confusing where the APIs or implementation behavior of the different languages diverge slightly.

I have similar concerns. Perhaps separate pages should be dedicated to components where the interfaces diverge (i.e., pull each of those components out of the ChannelManager setup guide).

With that in mind, it seems there's so much needed in terms of step-by-step instructions to set up ChannelManager. But a good number of the examples are just skeleton implementations of traits or simply demonstrate how to pass parameters to preexisting implementations. Whether here or later, maybe we should take an opportunity to pare back that portion of the guide to have fewer and more consolidated examples. The previous suggestion may help as well on cutting down the size.

Comment on lines +1197 to +1208
```rust
let network_graph_path = format!("{}/network_graph", ldk_data_dir.clone());
let network_graph = Arc::new(disk::read_network(Path::new(&network_graph_path), args.network, logger.clone()));

let scorer_path = format!("{}/scorer", ldk_data_dir.clone());
let scorer = Arc::new(RwLock::new(disk::read_scorer(
Path::new(&scorer_path),
Arc::clone(&network_graph),
Arc::clone(&logger),
)));

```
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is using utilities from the sample, which aren't available in LDK. Similarly above for gossip sync.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

For the sake of keeping the code snippets short maybe it's ok?


</CodeSwitcher>

References: [Rust `Event` docs](https://docs.rs/lightning/0.0.114/lightning/util/events/enum.Event.html), [Java/Kotlin `Event` bindings](https://github.com/lightningdevkit/ldk-garbagecollected/blob/main/src/main/java/org/ldk/structs/Event.java)
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We'll want to use * or latest instead of version numbers. Same elsewhere.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fixed in 39e95b4

```rust
use lightning::util::events::{Event};

// In the event handler passed to BackgroundProcessor::start
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks like BackgroundProcessor was removed from the guide. We'll want to have a section on it as it drives everything. Probably somewhere in the introduction.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fixed in 43b12ca

Comment on lines 21 to 22
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.
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

May want to switch the order of these two given the order they are covered.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fixed in 31f155f

Comment on lines 7 to 28
To add a `ChannelManager` to your application, run:

<CodeSwitcher :languages="{rust:'Rust', kotlin:'Kotlin', swift:'Swift'}">
<template v-slot:rust>

```rust
use lightning::ln::channelmanager;

let channel_manager = ChannelManager::new(
&fee_estimator,
&chain_monitor,
&broadcaster,
&router,
&logger,
&entropy_source,
&node_signer,
&signer_provider,
user_config,
chain_params,
current_timestamp
);
```
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This alone isn't very valuable and requires that prerequisites that follow. Additionally, it is repeated in greater detail later on under "Initialize the ChannelManager".

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Because the ChannelManager is so "involved" I find it useful to start with what one might end up with as it helps shape the mental model of a critical piece of the API. I feel like when you see this the next logical question is well how do I set up a FeeEstimator etc arguably it's already covered in the intro. Slight rewording d26a8e8

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/)
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

s/module/crate

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fixed in 5787ac6

**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:
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

s/lightning_block_sync sample module/lightning-block-sync crate

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fixed in 74a2f49

&fee_estimator,
&chain_monitor,
&broadcaster,
&router,
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The most recent version of LDK has ChannelManager take Router, so this is correct. But a later section with a similar examples is using an older version of LDK that does not take a Router.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fixed in 8f276a0

@ConorOkus ConorOkus force-pushed the building-a-node-with-ldk-intro branch from 0f797b1 to 63d3ead Compare February 6, 2024 22:57
@ConorOkus
Copy link
Contributor Author

Did a ~full high-level pass.
While it's nice to have a single flow, I'm not fully convinced that a unified tutorial improves readability. I think at times it can be confusing where the APIs or implementation behavior of the different languages diverge slightly.

I have similar concerns. Perhaps separate pages should be dedicated to components where the interfaces diverge (i.e., pull each of those components out of the ChannelManager setup guide).

With that in mind, it seems there's so much needed in terms of step-by-step instructions to set up ChannelManager. But a good number of the examples are just skeleton implementations of traits or simply demonstrate how to pass parameters to preexisting implementations. Whether here or later, maybe we should take an opportunity to pare back that portion of the guide to have fewer and more consolidated examples. The previous suggestion may help as well on cutting down the size.

I feel like it is quite a common pattern/layout in SDK's that offer multiple languages, some examples include.

The structure is supposed to reflect someone who never built an LDK node before and wants to build the simplest node that can perform the basic functions. More detailed/advanced examples can follow.

Copy link
Contributor

@tnull tnull left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Did another pass.

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 [`ChannelManager`](https://docs.rs/lightning/*/lightning/ln/channelmanager/struct.ChannelManager.html), to open and close channels.
2. 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.
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit: Technically it's not the PeerManager, but the network stack, i.e., lightning-net-tokio that establishes the connections. The PeerManager just keeps track of them and the associated peer state.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Updated d41fecb

docs/building-a-node-with-ldk/installation.md Outdated Show resolved Hide resolved
docs/building-a-node-with-ldk/installation.md Outdated Show resolved Hide resolved
docs/building-a-node-with-ldk/installation.md Outdated Show resolved Hide resolved

</CodeSwitcher>

**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`
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The networking options here are really bindings specific types, won't work in Rust as described. Also not sure what 'other' is referring to, and Signet seems missing.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Updated in eb7ca72

docs/building-a-node-with-ldk/setting-up-a-peer-manager.md Outdated Show resolved Hide resolved
docs/building-a-node-with-ldk/setting-up-a-peer-manager.md Outdated Show resolved Hide resolved
docs/building-a-node-with-ldk/setting-up-a-peer-manager.md Outdated Show resolved Hide resolved
let router = DefaultRouter::new(
network_graph.clone(),
logger.clone(),
keys_manager.get_secure_random_bytes(),
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think by now it takes an EntropySource (i.e., KeysManager reference) directly.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This snippet is from ldk-node and ldk-sample. I don't see an example with EntropySource

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ah, I think this was part of the bindings fixes for the last release, i.e., they have not 'officially' released on the Rust side yet. But probably makes sense to already update to the upcoming API versions here to not have the guide immediately outdated.

@ConorOkus ConorOkus merged commit 20dc0a5 into lightningdevkit:main Feb 9, 2024
5 checks passed
@ConorOkus ConorOkus deleted the building-a-node-with-ldk-intro branch February 9, 2024 16:01
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants