c8381f2a18022e010f615f41b82ce32fe5a38d8e
[rust-lightning] / lightning / src / ln / offers_tests.rs
1 // This file is Copyright its original authors, visible in version control
2 // history.
3 //
4 // This file is licensed under the Apache License, Version 2.0 <LICENSE-APACHE
5 // or http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
6 // <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your option.
7 // You may not use this file except in accordance with one or both of these
8 // licenses.
9
10 //! Functional tests for the BOLT 12 Offers payment flow.
11 //!
12 //! [`ChannelManager`] provides utilities to create [`Offer`]s and [`Refund`]s along with utilities
13 //! to initiate and request payment for them, respectively. It also manages the payment flow via
14 //! implementing [`OffersMessageHandler`]. This module tests that functionality, including the
15 //! resulting [`Event`] generation.
16
17 use core::time::Duration;
18 use crate::blinded_path::BlindedPath;
19 use crate::events::{Event, MessageSendEventsProvider, PaymentPurpose};
20 use crate::ln::channelmanager::{PaymentId, RecentPaymentDetails, Retry, self};
21 use crate::ln::functional_test_utils::*;
22 use crate::ln::msgs::{ChannelMessageHandler, Init, OnionMessage, OnionMessageHandler};
23 use crate::offers::invoice::Bolt12Invoice;
24 use crate::offers::invoice_error::InvoiceError;
25 use crate::offers::invoice_request::InvoiceRequest;
26 use crate::offers::parse::Bolt12SemanticError;
27 use crate::onion_message::messenger::PeeledOnion;
28 use crate::onion_message::offers::OffersMessage;
29 use crate::onion_message::packet::ParsedOnionMessageContents;
30
31 use crate::prelude::*;
32
33 macro_rules! expect_recent_payment {
34         ($node: expr, $payment_state: path, $payment_id: expr) => {
35                 match $node.node.list_recent_payments().first() {
36                         Some(&$payment_state { payment_id: actual_payment_id, .. }) => {
37                                 assert_eq!($payment_id, actual_payment_id);
38                         },
39                         Some(_) => panic!("Unexpected recent payment state"),
40                         None => panic!("No recent payments"),
41                 }
42         }
43 }
44
45 fn connect_peers<'a, 'b, 'c>(node_a: &Node<'a, 'b, 'c>, node_b: &Node<'a, 'b, 'c>) {
46         let node_id_a = node_a.node.get_our_node_id();
47         let node_id_b = node_b.node.get_our_node_id();
48
49         let init_a = Init {
50                 features: node_a.init_features(&node_id_b),
51                 networks: None,
52                 remote_network_address: None,
53         };
54         let init_b = Init {
55                 features: node_b.init_features(&node_id_a),
56                 networks: None,
57                 remote_network_address: None,
58         };
59
60         node_a.node.peer_connected(&node_id_b, &init_b, true).unwrap();
61         node_b.node.peer_connected(&node_id_a, &init_a, false).unwrap();
62         node_a.onion_messenger.peer_connected(&node_id_b, &init_b, true).unwrap();
63         node_b.onion_messenger.peer_connected(&node_id_a, &init_a, false).unwrap();
64 }
65
66 fn disconnect_peers<'a, 'b, 'c>(node_a: &Node<'a, 'b, 'c>, peers: &[&Node<'a, 'b, 'c>]) {
67         for node_b in peers {
68                 node_a.node.peer_disconnected(&node_b.node.get_our_node_id());
69                 node_b.node.peer_disconnected(&node_a.node.get_our_node_id());
70                 node_a.onion_messenger.peer_disconnected(&node_b.node.get_our_node_id());
71                 node_b.onion_messenger.peer_disconnected(&node_a.node.get_our_node_id());
72         }
73 }
74
75 fn route_bolt12_payment<'a, 'b, 'c>(
76         node: &Node<'a, 'b, 'c>, path: &[&Node<'a, 'b, 'c>], invoice: &Bolt12Invoice
77 ) {
78         // Monitor added when handling the invoice onion message.
79         check_added_monitors(node, 1);
80
81         let mut events = node.node.get_and_clear_pending_msg_events();
82         assert_eq!(events.len(), 1);
83         let ev = remove_first_msg_event_to_node(&path[0].node.get_our_node_id(), &mut events);
84
85         // Use a fake payment_hash and bypass checking for the PaymentClaimable event since the
86         // invoice contains the payment_hash but it was encrypted inside an onion message.
87         let amount_msats = invoice.amount_msats();
88         let payment_hash = invoice.payment_hash();
89         do_pass_along_path(
90                 node, path, amount_msats, payment_hash, None, ev, false, false, None, false
91         );
92 }
93
94 fn claim_bolt12_payment<'a, 'b, 'c>(node: &Node<'a, 'b, 'c>, path: &[&Node<'a, 'b, 'c>]) {
95         let recipient = &path[path.len() - 1];
96         match get_event!(recipient, Event::PaymentClaimable) {
97                 Event::PaymentClaimable {
98                         purpose: PaymentPurpose::InvoicePayment {
99                                 payment_preimage: Some(payment_preimage), ..
100                         }, ..
101                 } => claim_payment(node, path, payment_preimage),
102                 _ => panic!(),
103         };
104 }
105
106 fn extract_invoice_request<'a, 'b, 'c>(
107         node: &Node<'a, 'b, 'c>, message: &OnionMessage
108 ) -> (InvoiceRequest, Option<BlindedPath>) {
109         match node.onion_messenger.peel_onion_message(message) {
110                 Ok(PeeledOnion::Receive(message, _, reply_path)) => match message {
111                         ParsedOnionMessageContents::Offers(offers_message) => match offers_message {
112                                 OffersMessage::InvoiceRequest(invoice_request) => (invoice_request, reply_path),
113                                 OffersMessage::Invoice(invoice) => panic!("Unexpected invoice: {:?}", invoice),
114                                 OffersMessage::InvoiceError(error) => panic!("Unexpected invoice_error: {:?}", error),
115                         },
116                         ParsedOnionMessageContents::Custom(message) => panic!("Unexpected custom message: {:?}", message),
117                 },
118                 Ok(PeeledOnion::Forward(_, _)) => panic!("Unexpected onion message forward"),
119                 Err(e) => panic!("Failed to process onion message {:?}", e),
120         }
121 }
122
123 fn extract_invoice<'a, 'b, 'c>(node: &Node<'a, 'b, 'c>, message: &OnionMessage) -> Bolt12Invoice {
124         match node.onion_messenger.peel_onion_message(message) {
125                 Ok(PeeledOnion::Receive(message, _, _)) => match message {
126                         ParsedOnionMessageContents::Offers(offers_message) => match offers_message {
127                                 OffersMessage::InvoiceRequest(invoice_request) => panic!("Unexpected invoice_request: {:?}", invoice_request),
128                                 OffersMessage::Invoice(invoice) => invoice,
129                                 OffersMessage::InvoiceError(error) => panic!("Unexpected invoice_error: {:?}", error),
130                         },
131                         ParsedOnionMessageContents::Custom(message) => panic!("Unexpected custom message: {:?}", message),
132                 },
133                 Ok(PeeledOnion::Forward(_, _)) => panic!("Unexpected onion message forward"),
134                 Err(e) => panic!("Failed to process onion message {:?}", e),
135         }
136 }
137
138 fn extract_invoice_error<'a, 'b, 'c>(
139         node: &Node<'a, 'b, 'c>, message: &OnionMessage
140 ) -> InvoiceError {
141         match node.onion_messenger.peel_onion_message(message) {
142                 Ok(PeeledOnion::Receive(message, _, _)) => match message {
143                         ParsedOnionMessageContents::Offers(offers_message) => match offers_message {
144                                 OffersMessage::InvoiceRequest(invoice_request) => panic!("Unexpected invoice_request: {:?}", invoice_request),
145                                 OffersMessage::Invoice(invoice) => panic!("Unexpected invoice: {:?}", invoice),
146                                 OffersMessage::InvoiceError(error) => error,
147                         },
148                         ParsedOnionMessageContents::Custom(message) => panic!("Unexpected custom message: {:?}", message),
149                 },
150                 Ok(PeeledOnion::Forward(_, _)) => panic!("Unexpected onion message forward"),
151                 Err(e) => panic!("Failed to process onion message {:?}", e),
152         }
153 }
154
155 /// Checks that an offer can be paid through blinded paths and that ephemeral pubkeys are used
156 /// rather than exposing a node's pubkey.
157 #[test]
158 fn creates_and_pays_for_offer_using_two_hop_blinded_path() {
159         let mut accept_forward_cfg = test_default_channel_config();
160         accept_forward_cfg.accept_forwards_to_priv_channels = true;
161
162         let mut features = channelmanager::provided_init_features(&accept_forward_cfg);
163         features.set_onion_messages_optional();
164         features.set_route_blinding_optional();
165
166         let chanmon_cfgs = create_chanmon_cfgs(6);
167         let node_cfgs = create_node_cfgs(6, &chanmon_cfgs);
168
169         *node_cfgs[1].override_init_features.borrow_mut() = Some(features);
170
171         let node_chanmgrs = create_node_chanmgrs(
172                 6, &node_cfgs, &[None, Some(accept_forward_cfg), None, None, None, None]
173         );
174         let nodes = create_network(6, &node_cfgs, &node_chanmgrs);
175
176         create_unannounced_chan_between_nodes_with_value(&nodes, 0, 1, 10_000_000, 1_000_000_000);
177         create_unannounced_chan_between_nodes_with_value(&nodes, 2, 3, 10_000_000, 1_000_000_000);
178         create_announced_chan_between_nodes_with_value(&nodes, 1, 2, 10_000_000, 1_000_000_000);
179         create_announced_chan_between_nodes_with_value(&nodes, 1, 4, 10_000_000, 1_000_000_000);
180         create_announced_chan_between_nodes_with_value(&nodes, 1, 5, 10_000_000, 1_000_000_000);
181         create_announced_chan_between_nodes_with_value(&nodes, 2, 4, 10_000_000, 1_000_000_000);
182         create_announced_chan_between_nodes_with_value(&nodes, 2, 5, 10_000_000, 1_000_000_000);
183
184         let (alice, bob, charlie, david) = (&nodes[0], &nodes[1], &nodes[2], &nodes[3]);
185         let alice_id = alice.node.get_our_node_id();
186         let bob_id = bob.node.get_our_node_id();
187         let charlie_id = charlie.node.get_our_node_id();
188         let david_id = david.node.get_our_node_id();
189
190         disconnect_peers(alice, &[charlie, david, &nodes[4], &nodes[5]]);
191         disconnect_peers(david, &[bob, &nodes[4], &nodes[5]]);
192
193         let offer = alice.node
194                 .create_offer_builder("coffee".to_string()).unwrap()
195                 .amount_msats(10_000_000)
196                 .build().unwrap();
197         assert_ne!(offer.signing_pubkey(), alice_id);
198         assert!(!offer.paths().is_empty());
199         for path in offer.paths() {
200                 assert_eq!(path.introduction_node_id, bob_id);
201         }
202
203         let payment_id = PaymentId([1; 32]);
204         david.node.pay_for_offer(&offer, None, None, None, payment_id, Retry::Attempts(0), None)
205                 .unwrap();
206         expect_recent_payment!(david, RecentPaymentDetails::AwaitingInvoice, payment_id);
207
208         connect_peers(david, bob);
209
210         let onion_message = david.onion_messenger.next_onion_message_for_peer(bob_id).unwrap();
211         bob.onion_messenger.handle_onion_message(&david_id, &onion_message);
212
213         connect_peers(alice, charlie);
214
215         let onion_message = bob.onion_messenger.next_onion_message_for_peer(alice_id).unwrap();
216         alice.onion_messenger.handle_onion_message(&bob_id, &onion_message);
217
218         let (invoice_request, reply_path) = extract_invoice_request(alice, &onion_message);
219         assert_eq!(invoice_request.amount_msats(), None);
220         assert_ne!(invoice_request.payer_id(), david_id);
221         assert_eq!(reply_path.unwrap().introduction_node_id, charlie_id);
222
223         let onion_message = alice.onion_messenger.next_onion_message_for_peer(charlie_id).unwrap();
224         charlie.onion_messenger.handle_onion_message(&alice_id, &onion_message);
225
226         let onion_message = charlie.onion_messenger.next_onion_message_for_peer(david_id).unwrap();
227         david.onion_messenger.handle_onion_message(&charlie_id, &onion_message);
228
229         let invoice = extract_invoice(david, &onion_message);
230         assert_eq!(invoice.amount_msats(), 10_000_000);
231         assert_ne!(invoice.signing_pubkey(), alice_id);
232         assert!(!invoice.payment_paths().is_empty());
233         for (_, path) in invoice.payment_paths() {
234                 assert_eq!(path.introduction_node_id, bob_id);
235         }
236
237         route_bolt12_payment(david, &[charlie, bob, alice], &invoice);
238         expect_recent_payment!(david, RecentPaymentDetails::Pending, payment_id);
239
240         claim_bolt12_payment(david, &[charlie, bob, alice]);
241         expect_recent_payment!(david, RecentPaymentDetails::Fulfilled, payment_id);
242 }
243
244 /// Checks that a refund can be paid through blinded paths and that ephemeral pubkeys are used
245 /// rather than exposing a node's pubkey.
246 #[test]
247 fn creates_and_pays_for_refund_using_two_hop_blinded_path() {
248         let mut accept_forward_cfg = test_default_channel_config();
249         accept_forward_cfg.accept_forwards_to_priv_channels = true;
250
251         let mut features = channelmanager::provided_init_features(&accept_forward_cfg);
252         features.set_onion_messages_optional();
253         features.set_route_blinding_optional();
254
255         let chanmon_cfgs = create_chanmon_cfgs(6);
256         let node_cfgs = create_node_cfgs(6, &chanmon_cfgs);
257
258         *node_cfgs[1].override_init_features.borrow_mut() = Some(features);
259
260         let node_chanmgrs = create_node_chanmgrs(
261                 6, &node_cfgs, &[None, Some(accept_forward_cfg), None, None, None, None]
262         );
263         let nodes = create_network(6, &node_cfgs, &node_chanmgrs);
264
265         create_unannounced_chan_between_nodes_with_value(&nodes, 0, 1, 10_000_000, 1_000_000_000);
266         create_unannounced_chan_between_nodes_with_value(&nodes, 2, 3, 10_000_000, 1_000_000_000);
267         create_announced_chan_between_nodes_with_value(&nodes, 1, 2, 10_000_000, 1_000_000_000);
268         create_announced_chan_between_nodes_with_value(&nodes, 1, 4, 10_000_000, 1_000_000_000);
269         create_announced_chan_between_nodes_with_value(&nodes, 1, 5, 10_000_000, 1_000_000_000);
270         create_announced_chan_between_nodes_with_value(&nodes, 2, 4, 10_000_000, 1_000_000_000);
271         create_announced_chan_between_nodes_with_value(&nodes, 2, 5, 10_000_000, 1_000_000_000);
272
273         let (alice, bob, charlie, david) = (&nodes[0], &nodes[1], &nodes[2], &nodes[3]);
274         let alice_id = alice.node.get_our_node_id();
275         let bob_id = bob.node.get_our_node_id();
276         let charlie_id = charlie.node.get_our_node_id();
277         let david_id = david.node.get_our_node_id();
278
279         disconnect_peers(alice, &[charlie, david, &nodes[4], &nodes[5]]);
280         disconnect_peers(david, &[bob, &nodes[4], &nodes[5]]);
281
282         let absolute_expiry = Duration::from_secs(u64::MAX);
283         let payment_id = PaymentId([1; 32]);
284         let refund = david.node
285                 .create_refund_builder(
286                         "refund".to_string(), 10_000_000, absolute_expiry, payment_id, Retry::Attempts(0), None
287                 )
288                 .unwrap()
289                 .build().unwrap();
290         assert_eq!(refund.amount_msats(), 10_000_000);
291         assert_eq!(refund.absolute_expiry(), Some(absolute_expiry));
292         assert_ne!(refund.payer_id(), david_id);
293         assert!(!refund.paths().is_empty());
294         for path in refund.paths() {
295                 assert_eq!(path.introduction_node_id, charlie_id);
296         }
297         expect_recent_payment!(david, RecentPaymentDetails::AwaitingInvoice, payment_id);
298
299         alice.node.request_refund_payment(&refund).unwrap();
300
301         connect_peers(alice, charlie);
302
303         let onion_message = alice.onion_messenger.next_onion_message_for_peer(charlie_id).unwrap();
304         charlie.onion_messenger.handle_onion_message(&alice_id, &onion_message);
305
306         let onion_message = charlie.onion_messenger.next_onion_message_for_peer(david_id).unwrap();
307         david.onion_messenger.handle_onion_message(&charlie_id, &onion_message);
308
309         let invoice = extract_invoice(david, &onion_message);
310         assert_eq!(invoice.amount_msats(), 10_000_000);
311         assert_ne!(invoice.signing_pubkey(), alice_id);
312         assert!(!invoice.payment_paths().is_empty());
313         for (_, path) in invoice.payment_paths() {
314                 assert_eq!(path.introduction_node_id, bob_id);
315         }
316
317         route_bolt12_payment(david, &[charlie, bob, alice], &invoice);
318         expect_recent_payment!(david, RecentPaymentDetails::Pending, payment_id);
319
320         claim_bolt12_payment(david, &[charlie, bob, alice]);
321         expect_recent_payment!(david, RecentPaymentDetails::Fulfilled, payment_id);
322 }
323
324 /// Checks that an offer can be paid through a one-hop blinded path and that ephemeral pubkeys are
325 /// used rather than exposing a node's pubkey. However, the node's pubkey is still used as the
326 /// introduction node of the blinded path.
327 #[test]
328 fn creates_and_pays_for_offer_using_one_hop_blinded_path() {
329         let chanmon_cfgs = create_chanmon_cfgs(2);
330         let node_cfgs = create_node_cfgs(2, &chanmon_cfgs);
331         let node_chanmgrs = create_node_chanmgrs(2, &node_cfgs, &[None, None]);
332         let nodes = create_network(2, &node_cfgs, &node_chanmgrs);
333
334         create_announced_chan_between_nodes_with_value(&nodes, 0, 1, 10_000_000, 1_000_000_000);
335
336         let alice = &nodes[0];
337         let alice_id = alice.node.get_our_node_id();
338         let bob = &nodes[1];
339         let bob_id = bob.node.get_our_node_id();
340
341         let offer = alice.node
342                 .create_offer_builder("coffee".to_string()).unwrap()
343                 .amount_msats(10_000_000)
344                 .build().unwrap();
345         assert_ne!(offer.signing_pubkey(), alice_id);
346         assert!(!offer.paths().is_empty());
347         for path in offer.paths() {
348                 assert_eq!(path.introduction_node_id, alice_id);
349         }
350
351         let payment_id = PaymentId([1; 32]);
352         bob.node.pay_for_offer(&offer, None, None, None, payment_id, Retry::Attempts(0), None).unwrap();
353         expect_recent_payment!(bob, RecentPaymentDetails::AwaitingInvoice, payment_id);
354
355         let onion_message = bob.onion_messenger.next_onion_message_for_peer(alice_id).unwrap();
356         alice.onion_messenger.handle_onion_message(&bob_id, &onion_message);
357
358         let (invoice_request, reply_path) = extract_invoice_request(alice, &onion_message);
359         assert_eq!(invoice_request.amount_msats(), None);
360         assert_ne!(invoice_request.payer_id(), bob_id);
361         assert_eq!(reply_path.unwrap().introduction_node_id, bob_id);
362
363         let onion_message = alice.onion_messenger.next_onion_message_for_peer(bob_id).unwrap();
364         bob.onion_messenger.handle_onion_message(&alice_id, &onion_message);
365
366         let invoice = extract_invoice(bob, &onion_message);
367         assert_eq!(invoice.amount_msats(), 10_000_000);
368         assert_ne!(invoice.signing_pubkey(), alice_id);
369         assert!(!invoice.payment_paths().is_empty());
370         for (_, path) in invoice.payment_paths() {
371                 assert_eq!(path.introduction_node_id, alice_id);
372         }
373
374         route_bolt12_payment(bob, &[alice], &invoice);
375         expect_recent_payment!(bob, RecentPaymentDetails::Pending, payment_id);
376
377         claim_bolt12_payment(bob, &[alice]);
378         expect_recent_payment!(bob, RecentPaymentDetails::Fulfilled, payment_id);
379 }
380
381 /// Checks that a refund can be paid through a one-hop blinded path and that ephemeral pubkeys are
382 /// used rather than exposing a node's pubkey. However, the node's pubkey is still used as the
383 /// introduction node of the blinded path.
384 #[test]
385 fn creates_and_pays_for_refund_using_one_hop_blinded_path() {
386         let chanmon_cfgs = create_chanmon_cfgs(2);
387         let node_cfgs = create_node_cfgs(2, &chanmon_cfgs);
388         let node_chanmgrs = create_node_chanmgrs(2, &node_cfgs, &[None, None]);
389         let nodes = create_network(2, &node_cfgs, &node_chanmgrs);
390
391         create_announced_chan_between_nodes_with_value(&nodes, 0, 1, 10_000_000, 1_000_000_000);
392
393         let alice = &nodes[0];
394         let alice_id = alice.node.get_our_node_id();
395         let bob = &nodes[1];
396         let bob_id = bob.node.get_our_node_id();
397
398         let absolute_expiry = Duration::from_secs(u64::MAX);
399         let payment_id = PaymentId([1; 32]);
400         let refund = bob.node
401                 .create_refund_builder(
402                         "refund".to_string(), 10_000_000, absolute_expiry, payment_id, Retry::Attempts(0), None
403                 )
404                 .unwrap()
405                 .build().unwrap();
406         assert_eq!(refund.amount_msats(), 10_000_000);
407         assert_eq!(refund.absolute_expiry(), Some(absolute_expiry));
408         assert_ne!(refund.payer_id(), bob_id);
409         assert!(!refund.paths().is_empty());
410         for path in refund.paths() {
411                 assert_eq!(path.introduction_node_id, bob_id);
412         }
413         expect_recent_payment!(bob, RecentPaymentDetails::AwaitingInvoice, payment_id);
414
415         alice.node.request_refund_payment(&refund).unwrap();
416
417         let onion_message = alice.onion_messenger.next_onion_message_for_peer(bob_id).unwrap();
418         bob.onion_messenger.handle_onion_message(&alice_id, &onion_message);
419
420         let invoice = extract_invoice(bob, &onion_message);
421         assert_eq!(invoice.amount_msats(), 10_000_000);
422         assert_ne!(invoice.signing_pubkey(), alice_id);
423         assert!(!invoice.payment_paths().is_empty());
424         for (_, path) in invoice.payment_paths() {
425                 assert_eq!(path.introduction_node_id, alice_id);
426         }
427
428         route_bolt12_payment(bob, &[alice], &invoice);
429         expect_recent_payment!(bob, RecentPaymentDetails::Pending, payment_id);
430
431         claim_bolt12_payment(bob, &[alice]);
432         expect_recent_payment!(bob, RecentPaymentDetails::Fulfilled, payment_id);
433 }
434
435 /// Checks that an invoice for an offer without any blinded paths can be requested. Note that while
436 /// the requested is sent directly using the node's pubkey, the response and the payment still use
437 /// blinded paths as required by the spec.
438 #[test]
439 fn pays_for_offer_without_blinded_paths() {
440         let chanmon_cfgs = create_chanmon_cfgs(2);
441         let node_cfgs = create_node_cfgs(2, &chanmon_cfgs);
442         let node_chanmgrs = create_node_chanmgrs(2, &node_cfgs, &[None, None]);
443         let nodes = create_network(2, &node_cfgs, &node_chanmgrs);
444
445         create_announced_chan_between_nodes_with_value(&nodes, 0, 1, 10_000_000, 1_000_000_000);
446
447         let alice = &nodes[0];
448         let alice_id = alice.node.get_our_node_id();
449         let bob = &nodes[1];
450         let bob_id = bob.node.get_our_node_id();
451
452         let offer = alice.node
453                 .create_offer_builder("coffee".to_string()).unwrap()
454                 .clear_paths()
455                 .amount_msats(10_000_000)
456                 .build().unwrap();
457         assert_eq!(offer.signing_pubkey(), alice_id);
458         assert!(offer.paths().is_empty());
459
460         let payment_id = PaymentId([1; 32]);
461         bob.node.pay_for_offer(&offer, None, None, None, payment_id, Retry::Attempts(0), None).unwrap();
462         expect_recent_payment!(bob, RecentPaymentDetails::AwaitingInvoice, payment_id);
463
464         let onion_message = bob.onion_messenger.next_onion_message_for_peer(alice_id).unwrap();
465         alice.onion_messenger.handle_onion_message(&bob_id, &onion_message);
466
467         let onion_message = alice.onion_messenger.next_onion_message_for_peer(bob_id).unwrap();
468         bob.onion_messenger.handle_onion_message(&alice_id, &onion_message);
469
470         let invoice = extract_invoice(bob, &onion_message);
471         route_bolt12_payment(bob, &[alice], &invoice);
472         expect_recent_payment!(bob, RecentPaymentDetails::Pending, payment_id);
473
474         claim_bolt12_payment(bob, &[alice]);
475         expect_recent_payment!(bob, RecentPaymentDetails::Fulfilled, payment_id);
476 }
477
478 /// Checks that a refund without any blinded paths can be paid. Note that while the invoice is sent
479 /// directly using the node's pubkey, the payment still use blinded paths as required by the spec.
480 #[test]
481 fn pays_for_refund_without_blinded_paths() {
482         let chanmon_cfgs = create_chanmon_cfgs(2);
483         let node_cfgs = create_node_cfgs(2, &chanmon_cfgs);
484         let node_chanmgrs = create_node_chanmgrs(2, &node_cfgs, &[None, None]);
485         let nodes = create_network(2, &node_cfgs, &node_chanmgrs);
486
487         create_announced_chan_between_nodes_with_value(&nodes, 0, 1, 10_000_000, 1_000_000_000);
488
489         let alice = &nodes[0];
490         let alice_id = alice.node.get_our_node_id();
491         let bob = &nodes[1];
492         let bob_id = bob.node.get_our_node_id();
493
494         let absolute_expiry = Duration::from_secs(u64::MAX);
495         let payment_id = PaymentId([1; 32]);
496         let refund = bob.node
497                 .create_refund_builder(
498                         "refund".to_string(), 10_000_000, absolute_expiry, payment_id, Retry::Attempts(0), None
499                 )
500                 .unwrap()
501                 .clear_paths()
502                 .build().unwrap();
503         assert_eq!(refund.payer_id(), bob_id);
504         assert!(refund.paths().is_empty());
505         expect_recent_payment!(bob, RecentPaymentDetails::AwaitingInvoice, payment_id);
506
507         alice.node.request_refund_payment(&refund).unwrap();
508
509         let onion_message = alice.onion_messenger.next_onion_message_for_peer(bob_id).unwrap();
510         bob.onion_messenger.handle_onion_message(&alice_id, &onion_message);
511
512         let invoice = extract_invoice(bob, &onion_message);
513         route_bolt12_payment(bob, &[alice], &invoice);
514         expect_recent_payment!(bob, RecentPaymentDetails::Pending, payment_id);
515
516         claim_bolt12_payment(bob, &[alice]);
517         expect_recent_payment!(bob, RecentPaymentDetails::Fulfilled, payment_id);
518 }
519
520 /// Fails creating an offer when a blinded path cannot be created without exposing the node's id.
521 #[test]
522 fn fails_creating_offer_without_blinded_paths() {
523         let chanmon_cfgs = create_chanmon_cfgs(2);
524         let node_cfgs = create_node_cfgs(2, &chanmon_cfgs);
525         let node_chanmgrs = create_node_chanmgrs(2, &node_cfgs, &[None, None]);
526         let nodes = create_network(2, &node_cfgs, &node_chanmgrs);
527
528         create_unannounced_chan_between_nodes_with_value(&nodes, 0, 1, 10_000_000, 1_000_000_000);
529
530         match nodes[0].node.create_offer_builder("coffee".to_string()) {
531                 Ok(_) => panic!("Expected error"),
532                 Err(e) => assert_eq!(e, Bolt12SemanticError::MissingPaths),
533         }
534 }
535
536 /// Fails creating a refund when a blinded path cannot be created without exposing the node's id.
537 #[test]
538 fn fails_creating_refund_without_blinded_paths() {
539         let chanmon_cfgs = create_chanmon_cfgs(2);
540         let node_cfgs = create_node_cfgs(2, &chanmon_cfgs);
541         let node_chanmgrs = create_node_chanmgrs(2, &node_cfgs, &[None, None]);
542         let nodes = create_network(2, &node_cfgs, &node_chanmgrs);
543
544         create_unannounced_chan_between_nodes_with_value(&nodes, 0, 1, 10_000_000, 1_000_000_000);
545
546         let absolute_expiry = Duration::from_secs(u64::MAX);
547         let payment_id = PaymentId([1; 32]);
548
549         match nodes[0].node.create_refund_builder(
550                 "refund".to_string(), 10_000, absolute_expiry, payment_id, Retry::Attempts(0), None
551         ) {
552                 Ok(_) => panic!("Expected error"),
553                 Err(e) => assert_eq!(e, Bolt12SemanticError::MissingPaths),
554         }
555
556         assert!(nodes[0].node.list_recent_payments().is_empty());
557 }
558
559 /// Fails creating an invoice request when a blinded reply path cannot be created without exposing
560 /// the node's id.
561 #[test]
562 fn fails_creating_invoice_request_without_blinded_reply_path() {
563         let chanmon_cfgs = create_chanmon_cfgs(6);
564         let node_cfgs = create_node_cfgs(6, &chanmon_cfgs);
565         let node_chanmgrs = create_node_chanmgrs(6, &node_cfgs, &[None, None, None, None, None, None]);
566         let nodes = create_network(6, &node_cfgs, &node_chanmgrs);
567
568         create_unannounced_chan_between_nodes_with_value(&nodes, 0, 1, 10_000_000, 1_000_000_000);
569         create_unannounced_chan_between_nodes_with_value(&nodes, 2, 3, 10_000_000, 1_000_000_000);
570         create_announced_chan_between_nodes_with_value(&nodes, 1, 2, 10_000_000, 1_000_000_000);
571         create_announced_chan_between_nodes_with_value(&nodes, 1, 4, 10_000_000, 1_000_000_000);
572         create_announced_chan_between_nodes_with_value(&nodes, 1, 5, 10_000_000, 1_000_000_000);
573
574         let (alice, bob, charlie, david) = (&nodes[0], &nodes[1], &nodes[2], &nodes[3]);
575
576         disconnect_peers(alice, &[charlie, david, &nodes[4], &nodes[5]]);
577         disconnect_peers(david, &[bob, &nodes[4], &nodes[5]]);
578
579         let offer = alice.node
580                 .create_offer_builder("coffee".to_string()).unwrap()
581                 .amount_msats(10_000_000)
582                 .build().unwrap();
583
584         let payment_id = PaymentId([1; 32]);
585
586         match david.node.pay_for_offer(&offer, None, None, None, payment_id, Retry::Attempts(0), None) {
587                 Ok(_) => panic!("Expected error"),
588                 Err(e) => assert_eq!(e, Bolt12SemanticError::MissingPaths),
589         }
590
591         assert!(nodes[0].node.list_recent_payments().is_empty());
592 }
593
594 #[test]
595 fn fails_creating_invoice_request_with_duplicate_payment_id() {
596         let chanmon_cfgs = create_chanmon_cfgs(6);
597         let node_cfgs = create_node_cfgs(6, &chanmon_cfgs);
598         let node_chanmgrs = create_node_chanmgrs(6, &node_cfgs, &[None, None, None, None, None, None]);
599         let nodes = create_network(6, &node_cfgs, &node_chanmgrs);
600
601         create_unannounced_chan_between_nodes_with_value(&nodes, 0, 1, 10_000_000, 1_000_000_000);
602         create_unannounced_chan_between_nodes_with_value(&nodes, 2, 3, 10_000_000, 1_000_000_000);
603         create_announced_chan_between_nodes_with_value(&nodes, 1, 2, 10_000_000, 1_000_000_000);
604         create_announced_chan_between_nodes_with_value(&nodes, 1, 4, 10_000_000, 1_000_000_000);
605         create_announced_chan_between_nodes_with_value(&nodes, 1, 5, 10_000_000, 1_000_000_000);
606         create_announced_chan_between_nodes_with_value(&nodes, 2, 4, 10_000_000, 1_000_000_000);
607         create_announced_chan_between_nodes_with_value(&nodes, 2, 5, 10_000_000, 1_000_000_000);
608
609         let (alice, _bob, charlie, david) = (&nodes[0], &nodes[1], &nodes[2], &nodes[3]);
610
611         disconnect_peers(alice, &[charlie, david, &nodes[4], &nodes[5]]);
612
613         let offer = alice.node
614                 .create_offer_builder("coffee".to_string()).unwrap()
615                 .amount_msats(10_000_000)
616                 .build().unwrap();
617
618         let payment_id = PaymentId([1; 32]);
619         assert!(
620                 david.node.pay_for_offer(
621                         &offer, None, None, None, payment_id, Retry::Attempts(0), None
622                 ).is_ok()
623         );
624         expect_recent_payment!(david, RecentPaymentDetails::AwaitingInvoice, payment_id);
625
626         match david.node.pay_for_offer(&offer, None, None, None, payment_id, Retry::Attempts(0), None) {
627                 Ok(_) => panic!("Expected error"),
628                 Err(e) => assert_eq!(e, Bolt12SemanticError::DuplicatePaymentId),
629         }
630
631         expect_recent_payment!(david, RecentPaymentDetails::AwaitingInvoice, payment_id);
632 }
633
634 #[test]
635 fn fails_creating_refund_with_duplicate_payment_id() {
636         let chanmon_cfgs = create_chanmon_cfgs(2);
637         let node_cfgs = create_node_cfgs(2, &chanmon_cfgs);
638         let node_chanmgrs = create_node_chanmgrs(2, &node_cfgs, &[None, None]);
639         let nodes = create_network(2, &node_cfgs, &node_chanmgrs);
640
641         create_announced_chan_between_nodes_with_value(&nodes, 0, 1, 10_000_000, 1_000_000_000);
642
643         let absolute_expiry = Duration::from_secs(u64::MAX);
644         let payment_id = PaymentId([1; 32]);
645         assert!(
646                 nodes[0].node.create_refund_builder(
647                         "refund".to_string(), 10_000, absolute_expiry, payment_id, Retry::Attempts(0), None
648                 ).is_ok()
649         );
650         expect_recent_payment!(nodes[0], RecentPaymentDetails::AwaitingInvoice, payment_id);
651
652         match nodes[0].node.create_refund_builder(
653                 "refund".to_string(), 10_000, absolute_expiry, payment_id, Retry::Attempts(0), None
654         ) {
655                 Ok(_) => panic!("Expected error"),
656                 Err(e) => assert_eq!(e, Bolt12SemanticError::DuplicatePaymentId),
657         }
658
659         expect_recent_payment!(nodes[0], RecentPaymentDetails::AwaitingInvoice, payment_id);
660 }
661
662 #[test]
663 fn fails_sending_invoice_without_blinded_payment_paths_for_offer() {
664         let mut accept_forward_cfg = test_default_channel_config();
665         accept_forward_cfg.accept_forwards_to_priv_channels = true;
666
667         // Clearing route_blinding prevents forming any payment paths since the node is unannounced.
668         let mut features = channelmanager::provided_init_features(&accept_forward_cfg);
669         features.set_onion_messages_optional();
670         features.clear_route_blinding();
671
672         let chanmon_cfgs = create_chanmon_cfgs(6);
673         let node_cfgs = create_node_cfgs(6, &chanmon_cfgs);
674
675         *node_cfgs[1].override_init_features.borrow_mut() = Some(features);
676
677         let node_chanmgrs = create_node_chanmgrs(
678                 6, &node_cfgs, &[None, Some(accept_forward_cfg), None, None, None, None]
679         );
680         let nodes = create_network(6, &node_cfgs, &node_chanmgrs);
681
682         create_unannounced_chan_between_nodes_with_value(&nodes, 0, 1, 10_000_000, 1_000_000_000);
683         create_unannounced_chan_between_nodes_with_value(&nodes, 2, 3, 10_000_000, 1_000_000_000);
684         create_announced_chan_between_nodes_with_value(&nodes, 1, 2, 10_000_000, 1_000_000_000);
685         create_announced_chan_between_nodes_with_value(&nodes, 1, 4, 10_000_000, 1_000_000_000);
686         create_announced_chan_between_nodes_with_value(&nodes, 1, 5, 10_000_000, 1_000_000_000);
687         create_announced_chan_between_nodes_with_value(&nodes, 2, 4, 10_000_000, 1_000_000_000);
688         create_announced_chan_between_nodes_with_value(&nodes, 2, 5, 10_000_000, 1_000_000_000);
689
690         let (alice, bob, charlie, david) = (&nodes[0], &nodes[1], &nodes[2], &nodes[3]);
691         let alice_id = alice.node.get_our_node_id();
692         let bob_id = bob.node.get_our_node_id();
693         let charlie_id = charlie.node.get_our_node_id();
694         let david_id = david.node.get_our_node_id();
695
696         disconnect_peers(alice, &[charlie, david, &nodes[4], &nodes[5]]);
697         disconnect_peers(david, &[bob, &nodes[4], &nodes[5]]);
698
699         let offer = alice.node
700                 .create_offer_builder("coffee".to_string()).unwrap()
701                 .amount_msats(10_000_000)
702                 .build().unwrap();
703
704         let payment_id = PaymentId([1; 32]);
705         david.node.pay_for_offer(&offer, None, None, None, payment_id, Retry::Attempts(0), None)
706                 .unwrap();
707
708         connect_peers(david, bob);
709
710         let onion_message = david.onion_messenger.next_onion_message_for_peer(bob_id).unwrap();
711         bob.onion_messenger.handle_onion_message(&david_id, &onion_message);
712
713         connect_peers(alice, charlie);
714
715         let onion_message = bob.onion_messenger.next_onion_message_for_peer(alice_id).unwrap();
716         alice.onion_messenger.handle_onion_message(&bob_id, &onion_message);
717
718         let onion_message = alice.onion_messenger.next_onion_message_for_peer(charlie_id).unwrap();
719         charlie.onion_messenger.handle_onion_message(&alice_id, &onion_message);
720
721         let onion_message = charlie.onion_messenger.next_onion_message_for_peer(david_id).unwrap();
722         david.onion_messenger.handle_onion_message(&charlie_id, &onion_message);
723
724         let invoice_error = extract_invoice_error(david, &onion_message);
725         assert_eq!(invoice_error, InvoiceError::from(Bolt12SemanticError::MissingPaths));
726 }
727
728 #[test]
729 fn fails_sending_invoice_without_blinded_payment_paths_for_refund() {
730         let mut accept_forward_cfg = test_default_channel_config();
731         accept_forward_cfg.accept_forwards_to_priv_channels = true;
732
733         // Clearing route_blinding prevents forming any payment paths since the node is unannounced.
734         let mut features = channelmanager::provided_init_features(&accept_forward_cfg);
735         features.set_onion_messages_optional();
736         features.clear_route_blinding();
737
738         let chanmon_cfgs = create_chanmon_cfgs(6);
739         let node_cfgs = create_node_cfgs(6, &chanmon_cfgs);
740
741         *node_cfgs[1].override_init_features.borrow_mut() = Some(features);
742
743         let node_chanmgrs = create_node_chanmgrs(
744                 6, &node_cfgs, &[None, Some(accept_forward_cfg), None, None, None, None]
745         );
746         let nodes = create_network(6, &node_cfgs, &node_chanmgrs);
747
748         create_unannounced_chan_between_nodes_with_value(&nodes, 0, 1, 10_000_000, 1_000_000_000);
749         create_unannounced_chan_between_nodes_with_value(&nodes, 2, 3, 10_000_000, 1_000_000_000);
750         create_announced_chan_between_nodes_with_value(&nodes, 1, 2, 10_000_000, 1_000_000_000);
751         create_announced_chan_between_nodes_with_value(&nodes, 1, 4, 10_000_000, 1_000_000_000);
752         create_announced_chan_between_nodes_with_value(&nodes, 1, 5, 10_000_000, 1_000_000_000);
753         create_announced_chan_between_nodes_with_value(&nodes, 2, 4, 10_000_000, 1_000_000_000);
754         create_announced_chan_between_nodes_with_value(&nodes, 2, 5, 10_000_000, 1_000_000_000);
755
756         let (alice, bob, charlie, david) = (&nodes[0], &nodes[1], &nodes[2], &nodes[3]);
757
758         disconnect_peers(alice, &[charlie, david, &nodes[4], &nodes[5]]);
759         disconnect_peers(david, &[bob, &nodes[4], &nodes[5]]);
760
761         let absolute_expiry = Duration::from_secs(u64::MAX);
762         let payment_id = PaymentId([1; 32]);
763         let refund = david.node
764                 .create_refund_builder(
765                         "refund".to_string(), 10_000_000, absolute_expiry, payment_id, Retry::Attempts(0), None
766                 )
767                 .unwrap()
768                 .build().unwrap();
769
770         match alice.node.request_refund_payment(&refund) {
771                 Ok(_) => panic!("Expected error"),
772                 Err(e) => assert_eq!(e, Bolt12SemanticError::MissingPaths),
773         }
774 }
775
776 #[test]
777 fn fails_paying_invoice_more_than_once() {
778         let mut accept_forward_cfg = test_default_channel_config();
779         accept_forward_cfg.accept_forwards_to_priv_channels = true;
780
781         let mut features = channelmanager::provided_init_features(&accept_forward_cfg);
782         features.set_onion_messages_optional();
783         features.set_route_blinding_optional();
784
785         let chanmon_cfgs = create_chanmon_cfgs(6);
786         let node_cfgs = create_node_cfgs(6, &chanmon_cfgs);
787
788         *node_cfgs[1].override_init_features.borrow_mut() = Some(features);
789
790         let node_chanmgrs = create_node_chanmgrs(
791                 6, &node_cfgs, &[None, Some(accept_forward_cfg), None, None, None, None]
792         );
793         let nodes = create_network(6, &node_cfgs, &node_chanmgrs);
794
795         create_unannounced_chan_between_nodes_with_value(&nodes, 0, 1, 10_000_000, 1_000_000_000);
796         create_unannounced_chan_between_nodes_with_value(&nodes, 2, 3, 10_000_000, 1_000_000_000);
797         create_announced_chan_between_nodes_with_value(&nodes, 1, 2, 10_000_000, 1_000_000_000);
798         create_announced_chan_between_nodes_with_value(&nodes, 1, 4, 10_000_000, 1_000_000_000);
799         create_announced_chan_between_nodes_with_value(&nodes, 1, 5, 10_000_000, 1_000_000_000);
800         create_announced_chan_between_nodes_with_value(&nodes, 2, 4, 10_000_000, 1_000_000_000);
801         create_announced_chan_between_nodes_with_value(&nodes, 2, 5, 10_000_000, 1_000_000_000);
802
803         let (alice, bob, charlie, david) = (&nodes[0], &nodes[1], &nodes[2], &nodes[3]);
804         let alice_id = alice.node.get_our_node_id();
805         let bob_id = bob.node.get_our_node_id();
806         let charlie_id = charlie.node.get_our_node_id();
807         let david_id = david.node.get_our_node_id();
808
809         disconnect_peers(alice, &[charlie, david, &nodes[4], &nodes[5]]);
810         disconnect_peers(david, &[bob, &nodes[4], &nodes[5]]);
811
812         let absolute_expiry = Duration::from_secs(u64::MAX);
813         let payment_id = PaymentId([1; 32]);
814         let refund = david.node
815                 .create_refund_builder(
816                         "refund".to_string(), 10_000_000, absolute_expiry, payment_id, Retry::Attempts(0), None
817                 )
818                 .unwrap()
819                 .build().unwrap();
820         expect_recent_payment!(david, RecentPaymentDetails::AwaitingInvoice, payment_id);
821
822         // Alice sends the first invoice
823         alice.node.request_refund_payment(&refund).unwrap();
824
825         connect_peers(alice, charlie);
826
827         let onion_message = alice.onion_messenger.next_onion_message_for_peer(charlie_id).unwrap();
828         charlie.onion_messenger.handle_onion_message(&alice_id, &onion_message);
829
830         let onion_message = charlie.onion_messenger.next_onion_message_for_peer(david_id).unwrap();
831         david.onion_messenger.handle_onion_message(&charlie_id, &onion_message);
832
833         // David pays the first invoice
834         let invoice1 = extract_invoice(david, &onion_message);
835
836         route_bolt12_payment(david, &[charlie, bob, alice], &invoice1);
837         expect_recent_payment!(david, RecentPaymentDetails::Pending, payment_id);
838
839         claim_bolt12_payment(david, &[charlie, bob, alice]);
840         expect_recent_payment!(david, RecentPaymentDetails::Fulfilled, payment_id);
841
842         disconnect_peers(alice, &[charlie]);
843
844         // Alice sends the second invoice
845         alice.node.request_refund_payment(&refund).unwrap();
846
847         connect_peers(alice, charlie);
848         connect_peers(david, bob);
849
850         let onion_message = alice.onion_messenger.next_onion_message_for_peer(charlie_id).unwrap();
851         charlie.onion_messenger.handle_onion_message(&alice_id, &onion_message);
852
853         let onion_message = charlie.onion_messenger.next_onion_message_for_peer(david_id).unwrap();
854         david.onion_messenger.handle_onion_message(&charlie_id, &onion_message);
855
856         let invoice2 = extract_invoice(david, &onion_message);
857         assert_eq!(invoice1.payer_metadata(), invoice2.payer_metadata());
858
859         // David sends an error instead of paying the second invoice
860         let onion_message = david.onion_messenger.next_onion_message_for_peer(bob_id).unwrap();
861         bob.onion_messenger.handle_onion_message(&david_id, &onion_message);
862
863         let onion_message = bob.onion_messenger.next_onion_message_for_peer(alice_id).unwrap();
864         alice.onion_messenger.handle_onion_message(&bob_id, &onion_message);
865
866         let invoice_error = extract_invoice_error(alice, &onion_message);
867         assert_eq!(invoice_error, InvoiceError::from_string("DuplicateInvoice".to_string()));
868 }