Skip to content

Commit e8fe207

Browse files
wip
1 parent 5bcf8af commit e8fe207

File tree

4 files changed

+89
-51
lines changed

4 files changed

+89
-51
lines changed

docs/build_node.md

Lines changed: 86 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -18,27 +18,35 @@ shutdown checklist.
1818
- [ ] Initialize the fee estimator
1919
* What it's used for: estimating fees for on-chain transactions that LDK wants broadcasted.
2020
* Dependencies: none
21-
* Example fee estimator that always returns `253` satoshis:
21+
22+
Example fee estimator that always returns `253` satoshis:
2223
```java
2324
// FeeEstimatorInterface is a functional interface, so we can implement it with a lambda
2425
final fee_estimator = FeeEstimator.new_impl((confirmation_target -> 253));
2526
```
26-
Rather than using static fees, you'll want to fill in the lambda with fetching up-to-date fees from a source like bitcoin core or your own API endpoint.
27+
Rather than using static fees, you'll want to fill in the lambda with fetching up-to-date fees from a source like bitcoin core or your own API endpoint.
2728
- [ ] Initialize the logger
29+
Example logger that prints to the console:
30+
```java
31+
// LoggerInterface is a functional interface, so we can implement it with a lambda
32+
final logger = Logger.new_impl((String arg) -> System.out.println(arg));
33+
```
2834
- [ ] Initialize the transaction broadcaster
29-
* What it's used for:
35+
* What it's used for: broadcasting various lightning transactions
3036
* Dependencies: none
3137
* Example transaction broadcaster skeleton:
3238
```java
39+
// Note that the `tx` argument is a []byte type.
3340
final tx_broadcaster = BroadcasterInterface.new_impl(tx -> {
3441
// <insert code to actually broadcast the given transaction here>
3542
});
3643
```
3744
- [ ] Initialize the channel data persister
38-
* What it's used for:
45+
* What it's used for: persisting crucial channel data in a timely manner
3946
* Dependencies: none
4047
* Example:
4148
```java
49+
let
4250
Persist persister = Persist.new_impl(new Persist.PersistInterface() {
4351
@Override
4452
public Result_NoneChannelMonitorUpdateErrZ persist_new_channel(OutPoint id, ChannelMonitor data) {
@@ -52,18 +60,16 @@ Persist persister = Persist.new_impl(new Persist.PersistInterface() {
5260
});
5361
```
5462
- [ ] Initialize the chain monitor
55-
* What it's used for:
63+
* What it's used for: monitoring the chain for lighting transactions that are relevant to our node, and broadcasting force close transactions if need be
5664
* Dependencies: fee estimator, logger, transaction broadcaster, channel data persister
57-
* Optional dependency: ChainSource
65+
* Optional dependency: a chain filter that allows LDK to let you know what transactions you should filter blocks for. This is useful if you pre-filter blocks or use compact filters. Otherwise, LDK will need full blocks.
5866
* Example:
5967
```java
6068
// The `null` here must be filled in with a struct if you are running a light client.
6169
final chain_monitor = ChainMonitor.constructor_new(null, tx_broadcaster, logger, fee_estimator, persister);
6270
```
63-
- [ ] Fill in the chain monitor's channel monitor state if LDK is restarting
64-
// TODO: add reading existing monitors from disk and handling replaying blocks, handling forks
6571
- [ ] Initialize the keys manager
66-
* What it's used for:
72+
* What it's used for: providing keys for signing lightning transactions
6773
* Dependencies: random bytes, the current bitcoin network
6874
* Example:
6975
```java
@@ -74,26 +80,71 @@ byte[] key_seed = new byte[32];
7480
// * TODO document why the current time is part of the parameters
7581
KeysManager keys = KeysManager.constructor_new(key_seed, LDKNetwork.LDKNetwork_Bitcoin, System.currentTimeMillis() / 1000, (int) (System.currentTimeMillis() * 1000));
7682
```
83+
- [ ] If LDK is restarting, fill in the chain monitor's existing channel monitor state
84+
* Dependencies: the keys manager
85+
* If LDK is restarting and has existing channels, then it's very important to read its current channel state off of disk during the restart process.
86+
* Equally important: when you read each channel monitor off of disk, it comes with a blockhash which was the last block the channel monitor saw. So it's very important to take this blockhash, and:
87+
1. If the blockhash is on a fork that's no longer current to the chain, then first you need to disconnect blocks until the channel monitor gets to the common ancestor with the main chain
88+
2. Else, you just need to connect recent blocks until the channel monitor is at the current chain tip.
89+
Example of reading channel monitors from disk, where each channel monitor's file is named after its funding outpoint: // TODO matt plz check this
90+
```java
91+
// Initialize the HashMap where we'll store the channel monitors we read from disk.
92+
final HashMap<String, ChannelMonitor> monitors = new HashMap<>();
93+
94+
byte[] monitor_bytes = [];
95+
// TODO fix this line to read from a file on disk
96+
Result_C2Tuple_BlockHashChannelMonitorZDecodeErrorZ monitor_read_result = UtilMethods.constructor_BlockHashChannelMonitorZ_read(monitor_bytes, keys_interface);
97+
98+
// Assert that the result of reading bytes from disk is OK.
99+
assert monitor_read_result instanceof Result_C2Tuple_BlockHashChannelMonitorZDecodeErrorZ.Result_C2Tuple_BlockHashChannelMonitorZDecodeErrorZ_OK;
100+
101+
TwoTuple<OutPoint, byte[]> funding_txo_and_monitor = ((Result_C2Tuple_BlockHashChannelMonitorZDecodeErrorZ.Result_C2Tuple_BlockHashChannelMonitorZDecodeErrorZ_OK) roundtrip_monitor)
102+
103+
// Cast the bytes as a ChannelMonitor.
104+
ChannelMonitor mon = ((Result_C2Tuple_BlockHashChannelMonitorZDecodeErrorZ.Result_C2Tuple_BlockHashChannelMonitorZDecodeErrorZ_OK) res).res.b;
105+
106+
// <insert code here to bring the channel monitor up to chain tip>
107+
108+
// Save the monitor in our hashmap.
109+
monitors.put(mon.get_funding_txo().a, mon);
110+
111+
// Give the monitor to the chain monitor.
112+
final chain_watch = chain_monitor.as_Watch();
113+
chain_watch.watch_channel(mon.get_funding_txo().a, mon);
114+
```
115+
116+
Rust example of bringing a channel monitor up to chain tip: https://github.com/rust-bitcoin/rust-lightning/pull/763/files#diff-f457bab978fc8b89ad308d5195f99d7b65a4a6ba1673c5f164104b2dda9a0db6R251 where the channel monitor is the `chain_listener` parameter. See the linked function and the `find_fork` function within it.
77117
- [ ] Initialize the router (which we call the `NetGraphMsgHandler`)
78-
// TODO add reading router data from disk if restarting
118+
// TODO add docs for reading router data from disk if restarting
79119
* What it's used for:
80120
* Dependencies: logger
81121
* Optional dependency: source of chain information, recommended for light clients to be able to verify channels
82122
* Example:
83123
```java
84-
// TODO: have the example include reading from disk OR starting a new one
85124
final router = NetGraphMsgHandler.constructor_new(new byte[32], null, logger);
86125
```
87126
- [ ] Initialize the channel manager
88127
// TODO add stuff if we're restarting/reading from disk
89-
* What it's used for:
90-
* Dependencies:
91-
* Optional dependency:
92-
* Example:
128+
* What it's used for: managing channel state
129+
* Dependencies: keys manager, fee estimator, chain monitor, transaction broadcaster, logger, channel configuration info, and the set of channel monitors we read from disk in the previous step
130+
Example of initializing a channel manager on a fresh node:
131+
```java
132+
final chain_watch = chain_monitor.as_Watch();
133+
final channel_manager = ChannelManager.constructor_new(LDKNetwork.LDKNetwork_Bitcoin, FeeEstimator.new_impl(confirmation_target -> 0), chain_watch, tx_broadcaster, logger, keys_interface, UserConfig.constructor_default(), 1);
134+
```
135+
Example of initializing a channel manager on restart: // TODO: fix
93136
```java
137+
byte[] serialized_channel_manager = [];
138+
Result_C2Tuple_BlockHashChannelManagerZDecodeErrorZ read_res =
139+
UtilMethods.constructor_BlockHashChannelManagerZ_read(serialized_channel_manager, keys_interface, fee_estimator, chain_watch, tx_broadcaster, logger, UserConfig.constructor_default(), monitors);
140+
141+
// Assert we were able to read successfully.
142+
assert read_res instanceof Result_C2Tuple_BlockHashChannelManagerZDecodeErrorZ.Result_C2Tuple_BlockHashChannelManagerZDecodeErrorZ_OK;
143+
144+
final channel_manager = ((Result_C2Tuple_BlockHashChannelManagerZDecodeErrorZ.Result_C2Tuple_BlockHashChannelManagerZDecodeErrorZ_OK) read_res).res.b;
94145
```
95146
- [ ] Initialize the peer manager using LDK's `PeerManager` struct combined with LDK's supplied `NioPeerHandler` networking module
96-
* What it's used for:
147+
* What it's used for: connecting to peers, facilitating peer data to and from LDK
97148
* Dependencies: channel manager, router, keys manager, random bytes, logger
98149
* Example:
99150
```java
@@ -102,49 +153,36 @@ byte[] random_data = new byte[32];
102153
final nio_peer_handler;
103154
final peer_manager = PeerManager.constructor_new(chan_manager.as_ChannelMessageHandler(), router.as_RoutingMessageHandler(), keys_interface.get_node_secret(), random_data, logger);
104155
try { nio_peer_handler = new NioPeerHandler(peer_manager); } catch (IOException e) { assert false; }
105-
```
106-
- [ ] Start a loop for the peer manager to accept new connections.
107-
* What it's used for:
108-
* Dependencies:
109-
* Optional dependency:
110-
* Example:
111-
```java
112-
TODO fix this example
113-
for (short i = 10_000; true; i++) {
114-
try {
115-
nio_peer_handler.bind_listener(new InetSocketAddress("127.0.0.1", i));
116-
nio_port = i;
117-
break;
118-
} catch (IOException e) { assert i < 10_500; }
119-
}
120-
```
121-
- [ ] Start a loop for processing the peer manager's events.
122-
* What it's used for:
123-
* Dependencies:
124-
* Example:
125-
```java
126-
```
127-
- [ ] Connect to peers on startup
128-
* What it's used for:
129-
* Dependencies:
130-
* Example:
131-
```java
156+
157+
// Finally, start NioPeerHandler listening for connections.
158+
final port = 9735;
159+
nio_peer_handler.bind_listener(new InetSocketAddress("127.0.0.1", port));
132160
```
133161
- [ ] Start a loop to handle the channel manager's generated events
134162
// TODO add persisting the channel manager after handling each event
135-
* What it's used for:
136-
* Dependencies:
137-
* Example:
163+
* What it's used for: the channel manager and chain monitor generate events that must be handled by you, such as telling you when a payment has been successfully received or when a funding transaction is ready for broadcast.
164+
* Dependencies: channel manager and chain monitor
165+
Rust example: https://github.com/TheBlueMatt/rust-lightning-bitcoinrpc/blob/master/src/main.rs#L122
166+
- [ ] Persist router data in a background loop // TODO
167+
- [ ] Start a loop to call the channel manager's `timer_chan_freshness_every_min()` every minute
168+
* What it's used for: the channel manager needs to be told every time a minute passes so that it can broadcast fresh channel updates if needed
169+
Example:
138170
```java
171+
while (true) {
172+
<wait 60 seconds>
173+
channel_manager.timer_chan_freshness_every_min();
174+
}
139175
```
140-
- [ ] Persist router data in a background loop
141-
- [ ] Start a loop to call `timer_chan_freshness_every_min` every minute
142176

143177
## Running LDK Checklist
144178
- [ ] Connect and disconnect blocks to LDK as they come in
145179
```java
180+
// header is a []byte type, txdata is TwoTuple<Long, byte[]>[], height is `int`
146181
channel_manager.block_connected(header, txn, height);
147182
chain_monitor.block_connected(header, txn, height);
183+
184+
channel_manager.block_disconnected(header);
185+
chain_monitor.block_disconnected(header);
148186
```
149187

150188
## Using LDK

docs/custom_blockdata.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
---
22
id: custom_blockdata
3-
title: Customizing Blockchain Data
3+
title: Blockchain Data
44
---
55

66
TODO

docs/custom_key_mgmt.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
---
22
id: custom_key_mgmt
3-
title: Customizing Key Management
3+
title: Key Management
44
---
55

66
TODO

docs/custom_persist.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
---
22
id: custom_persist
3-
title: Customizing Data Persistence
3+
title: Data Persistence
44
---
55

66
TODO

0 commit comments

Comments
 (0)