Skip to content

Commit 884848b

Browse files
author
Conor Okus
committed
Update MessageHandler parameters
1 parent 45cc7af commit 884848b

File tree

4 files changed

+68
-136
lines changed

4 files changed

+68
-136
lines changed

docs/tutorials/building-a-node-with-ldk/handling-events.md

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,12 +13,14 @@ use lightning::util::events::{Event};
1313
// In the event handler passed to BackgroundProcessor::start
1414
match event {
1515
Event::PaymentSent { payment_preimage } => {
16-
// Handle successful payment
16+
// Handle successful payment
1717
}
1818
Event::PaymentFailed { payment_hash, rejected_by_dest } => {
19-
// Handle failed payment
19+
// Handle failed payment
20+
}
21+
Event::FundingGenerationReady { .. } => {
22+
// Generate the funding transaction for the channel
2023
}
21-
Event::FundingGenerationReady { .. } =>
2224
}
2325
```
2426

docs/tutorials/building-a-node-with-ldk/introduction.md

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -9,10 +9,10 @@ For an integrated example of an LDK node in Rust, see the [Sample Node](https://
99
The following tutorials will show you how to build the simplest lightning node using LDK, that fufills the following tasks:
1010

1111
1. **Connecting to Peers**
12-
2. **Open Channels**
13-
3. **Send Payments**
14-
4. **Receive Payments**
15-
5. **Close Channels**
12+
2. **Opening Channels**
13+
3. **Sending Payments**
14+
4. **Receiving Payments**
15+
5. **Closing Channels**
1616

1717
### Foundational Components
1818

docs/tutorials/building-a-node-with-ldk/sending-payments.md

Lines changed: 31 additions & 105 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ string in accordance with BOLT 11. After parsing the invoice, you'll need to
55
find a route from your node to the recipient and then make the payment using
66
`ChannelManager`.
77

8-
<CodeSwitcher :languages="{rust:'Rust', java:'Java', kotlin:'Kotlin', swift:'Swift'}">
8+
<CodeSwitcher :languages="{rust:'Rust', kotlin:'Kotlin', swift:'Swift'}">
99
<template v-slot:rust>
1010

1111
```rust
@@ -39,113 +39,40 @@ channel_manager.send_payment(&route, payment_hash, &payment_secret)
3939
```
4040

4141
</template>
42-
<template v-slot:java>
43-
44-
```java
45-
String invoice_str = // get an invoice from the payee
46-
Result_InvoiceNoneZ parsed_invoice = Invoice.from_str(invoice_str);
47-
48-
if (parsed_invoice instanceof Result_InvoiceNoneZ.Result_InvoiceNoneZ_OK) {
49-
Invoice invoice = ((Result_InvoiceNoneZ.Result_InvoiceNoneZ_OK) parsed_invoice).res;
50-
long amt_msat = 0;
51-
if (invoice.amount_pico_btc() instanceof Option_u64Z.Some) {
52-
amt_msat = ((Option_u64Z.Some)invoice.amount_pico_btc()).some / 10;
53-
}
54-
if (amt_msat == 0) {
55-
// <Handle a zero-value invoice>
56-
}
57-
58-
Route route;
59-
try (LockedNetworkGraph netgraph = router.read_locked_graph()) {
60-
NetworkGraph graph = netgraph.graph();
61-
Result_RouteLightningErrorZ route_res = UtilMethods.get_route(
62-
channel_manager.get_our_node_id(),
63-
graph, invoice.recover_payee_pub_key(), invoice.features(),
64-
channel_manager.list_usable_channels(), invoice.route_hints(),
65-
amt_msat, invoice.min_final_cltv_expiry(), logger);
66-
assert route_res instanceof Result_RouteLightningErrorZ.Result_RouteLightningErrorZ_OK;
67-
route = ((Result_RouteLightningErrorZ.Result_RouteLightningErrorZ_OK) route_res).res;
68-
}
69-
70-
Result_NonePaymentSendFailureZ payment_res = channel_manager.send_payment(
71-
route, invoice.payment_hash(), invoice.payment_secret());
72-
assert payment_res instanceof Result_NonePaymentSendFailureZ.Result_NonePaymentSendFailureZ_OK;
73-
}
74-
```
75-
76-
</template>
77-
<template v-slot:kotlin>
42+
<template v-slot:kotlin>
7843

7944
```java
8045
// Get an invoice from the recipient/payee
81-
val parsedInvoice = Invoice.from_str(recipientInvoice)
82-
if (!parsedInvoice.is_ok) {
83-
// Unable to parse invoice
84-
}
46+
val parsedInvoice = Bolt11Invoice.from_str(recipientInvoice)
47+
val invoiceVal = (parsedInvoice as Result_Bolt11InvoiceSignOrCreationErrorZ.Result_Bolt11InvoiceSignOrCreationErrorZ_OK).res
8548

86-
val invoice = (parsedInvoice as Result_InvoiceParseOrSemanticErrorZ.Result_InvoiceParseOrSemanticErrorZ_OK).res
49+
val res = UtilMethods.pay_invoice(invoice, Retry.attempts(6), channelManager)
8750

88-
var amountSats: Long = 0
89-
if (invoice.amount_milli_satoshis() is Option_u64Z.Some) {
90-
amountSats = (invoice.amount_milli_satoshis() as Option_u64Z.Some).some * 1000
91-
}
92-
93-
if (amountSats == 0L) {
94-
// Handle a zero-value invoice
95-
}
96-
97-
val res = channelManagerConstructor.payer.pay_invoice(invoice)
98-
99-
100-
101-
if (parsed_invoice instanceof Result_InvoiceNoneZ.Result_InvoiceNoneZ_OK) {
102-
Invoice invoice = ((Result_InvoiceNoneZ.Result_InvoiceNoneZ_OK) parsed_invoice).res;
103-
long amt_msat = 0;
104-
if (invoice.amount_pico_btc() instanceof Option_u64Z.Some) {
105-
amt_msat = ((Option_u64Z.Some)invoice.amount_pico_btc()).some / 10;
106-
}
107-
if (amt_msat == 0) {
108-
// <Handle a zero-value invoice>
109-
}
110-
111-
Route route;
112-
try (LockedNetworkGraph netgraph = router.read_locked_graph()) {
113-
NetworkGraph graph = netgraph.graph();
114-
Result_RouteLightningErrorZ route_res = UtilMethods.get_route(
115-
channel_manager.get_our_node_id(),
116-
graph, invoice.recover_payee_pub_key(), invoice.features(),
117-
channel_manager.list_usable_channels(), invoice.route_hints(),
118-
amt_msat, invoice.min_final_cltv_expiry(), logger);
119-
assert route_res instanceof Result_RouteLightningErrorZ.Result_RouteLightningErrorZ_OK;
120-
route = ((Result_RouteLightningErrorZ.Result_RouteLightningErrorZ_OK) route_res).res;
121-
}
122-
123-
Result_NonePaymentSendFailureZ payment_res = channel_manager.send_payment(
124-
route, invoice.payment_hash(), invoice.payment_secret());
125-
assert payment_res instanceof Result_NonePaymentSendFailureZ.Result_NonePaymentSendFailureZ_OK;
51+
if (res.is_ok) {
52+
// Payment success
12653
}
12754
```
12855

12956
</template>
13057

13158
<template v-slot:swift>
13259

133-
```Swift
134-
let invoiceStr = // get an invoice from the payee
135-
let parsedInvoice = Bolt11Invoice.fromStr(s: invoiceStr)
136-
137-
if let invoiceVal = parsedInvoice.getValue() {
138-
let invoicePaymentResult = Bindings.payInvoice(
139-
invoice: invoiceVal,
140-
retryStrategy: Bindings.Retry.initWithTimeout(a: 15),
141-
channelmanager: channelManager
142-
)
143-
144-
if invoicePaymentResult.isOk() {
145-
// Payment Sent
146-
}
60+
```Swift
61+
let invoiceStr = // get an invoice from the payee
62+
let parsedInvoice = Bolt11Invoice.fromStr(s: invoiceStr)
63+
64+
if let invoiceVal = parsedInvoice.getValue() {
65+
let invoicePaymentResult = Bindings.payInvoice(
66+
invoice: invoiceVal,
67+
retryStrategy: Bindings.Retry.initWithTimeout(a: 15),
68+
channelmanager: channelManager
69+
)
70+
71+
if invoicePaymentResult.isOk() {
72+
// Payment Sent
14773
}
148-
```
74+
}
75+
```
14976

15077
</template>
15178

@@ -156,7 +83,7 @@ in a `PaymentSent` event with the preimage of the payment hash. Be sure to look
15683
out for a `PaymentFailed` event, if the payment fails for some reason, and act
15784
accordingly.
15885

159-
<CodeSwitcher :languages="{rust:'Rust', java:'Java', swift:'Swift'}">
86+
<CodeSwitcher :languages="{rust:'Rust', kotlin:'Kotlin', swift:'Swift'}">
16087
<template v-slot:rust>
16188

16289
```rust
@@ -173,17 +100,16 @@ match event {
173100
```
174101

175102
</template>
176-
<template v-slot:java>
103+
<template v-slot:kotlin>
177104

178105
```java
179-
// In the `handle_event` method of ChannelManagerPersister implementation
180-
else if (e instanceof Event.PaymentSent) {
181-
// Handle successful payment
182-
Event.PaymentSent event = ((Event.PaymentSent) e);
106+
// In the `handleEvent` method of ChannelManagerPersister implementation
107+
if(event is Event.PaymentSent) {
108+
// Handle successful payment
183109
}
184-
else if (e instanceof Event.PaymentFailed) {
185-
// Handle failed payment
186-
Event.PaymentFailed event = ((Event.PaymentFailed) e);
110+
111+
if(event is Event.PaymentFailed) {
112+
// Handle failed payment
187113
}
188114
```
189115

@@ -202,4 +128,4 @@ if let paymentSentEvent = event.getValueAsPaymentSent() {
202128

203129
</template>
204130

205-
</CodeSwitcher>
131+
</CodeSwitcher>
Lines changed: 28 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -1,35 +1,39 @@
11
# Setting up a PeerManager
22

3-
The Peer Manager is responsible for managing a set of peer connections and all data associated with those peers.
3+
The Peer Manager is responsible for managing a set of peer connections and data associated with those peers.
44

5-
6-
## Adding a PeerManager
5+
## Adding a `PeerManager`
76

87
To add a PeerManager to your application, run:
98

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

13-
```rust
14-
use lightning::ln::peer_handler::{PeerManager};
15-
16-
let mut ephemeral_bytes = [0; 32];
17-
rand::thread_rng().fill_bytes(&mut ephemeral_bytes);
18-
19-
let lightning_msg_handler = MessageHandler {
20-
chan_handler: &channel_manager,
21-
route_handler: &gossip_sync,
22-
};
23-
24-
let ignoring_custom_msg_handler = IgnoringMessageHandler {};
25-
let peer_manager = PeerManager::new(
26-
lightning_msg_handler,
27-
keys_manager.get_node_secret(),
28-
&ephemeral_bytes,
29-
&logger,
30-
&ignoring_custom_msg_handler,
31-
);
32-
```
12+
```rust
13+
use lightning::ln::peer_handler::{PeerManager};
14+
15+
let mut ephemeral_bytes = [0; 32];
16+
rand::thread_rng().fill_bytes(&mut ephemeral_bytes);
17+
18+
let lightning_msg_handler = MessageHandler {
19+
chan_handler: channel_manager,
20+
route_handler: gossip_sync,
21+
onion_message_handler: onion_messenger,
22+
custom_message_handler: IgnoringMessageHandler {}
23+
};
24+
25+
let peer_manager = PeerManager::new(
26+
lightning_msg_handler,
27+
cur_time.as_secs().try_into().map_err(|e| {
28+
log_error!(logger, "Failed to get current time: {}", e);
29+
BuildError::InvalidSystemTime
30+
})?,
31+
&ephemeral_bytes,
32+
&logger,
33+
&keys_manager
34+
);
35+
```
36+
3337
</template>
3438

3539
<template v-slot:kotlin>
@@ -58,4 +62,4 @@ To add a PeerManager to your application, run:
5862

5963
**Dependencies:** `ChannelManager`, `RoutingMessageHandler`, `KeysManager`, random bytes, `Logger`
6064

61-
**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)
65+
**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)

0 commit comments

Comments
 (0)