Offers functional tests for duplicated payment ids
[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_request::InvoiceRequest;
25 use crate::offers::parse::Bolt12SemanticError;
26 use crate::onion_message::messenger::PeeledOnion;
27 use crate::onion_message::offers::OffersMessage;
28 use crate::onion_message::packet::ParsedOnionMessageContents;
29
30 use crate::prelude::*;
31
32 macro_rules! expect_recent_payment {
33         ($node: expr, $payment_state: path, $payment_id: expr) => {
34                 match $node.node.list_recent_payments().first() {
35                         Some(&$payment_state { payment_id: actual_payment_id, .. }) => {
36                                 assert_eq!($payment_id, actual_payment_id);
37                         },
38                         Some(_) => panic!("Unexpected recent payment state"),
39                         None => panic!("No recent payments"),
40                 }
41         }
42 }
43
44 fn connect_peers<'a, 'b, 'c>(node_a: &Node<'a, 'b, 'c>, node_b: &Node<'a, 'b, 'c>) {
45         let node_id_a = node_a.node.get_our_node_id();
46         let node_id_b = node_b.node.get_our_node_id();
47
48         let init_a = Init {
49                 features: node_a.init_features(&node_id_b),
50                 networks: None,
51                 remote_network_address: None,
52         };
53         let init_b = Init {
54                 features: node_b.init_features(&node_id_a),
55                 networks: None,
56                 remote_network_address: None,
57         };
58
59         node_a.node.peer_connected(&node_id_b, &init_b, true).unwrap();
60         node_b.node.peer_connected(&node_id_a, &init_a, false).unwrap();
61         node_a.onion_messenger.peer_connected(&node_id_b, &init_b, true).unwrap();
62         node_b.onion_messenger.peer_connected(&node_id_a, &init_a, false).unwrap();
63 }
64
65 fn disconnect_peers<'a, 'b, 'c>(node_a: &Node<'a, 'b, 'c>, peers: &[&Node<'a, 'b, 'c>]) {
66         for node_b in peers {
67                 node_a.node.peer_disconnected(&node_b.node.get_our_node_id());
68                 node_b.node.peer_disconnected(&node_a.node.get_our_node_id());
69                 node_a.onion_messenger.peer_disconnected(&node_b.node.get_our_node_id());
70                 node_b.onion_messenger.peer_disconnected(&node_a.node.get_our_node_id());
71         }
72 }
73
74 fn route_bolt12_payment<'a, 'b, 'c>(
75         node: &Node<'a, 'b, 'c>, path: &[&Node<'a, 'b, 'c>], invoice: &Bolt12Invoice
76 ) {
77         // Monitor added when handling the invoice onion message.
78         check_added_monitors(node, 1);
79
80         let mut events = node.node.get_and_clear_pending_msg_events();
81         assert_eq!(events.len(), 1);
82         let ev = remove_first_msg_event_to_node(&path[0].node.get_our_node_id(), &mut events);
83
84         // Use a fake payment_hash and bypass checking for the PaymentClaimable event since the
85         // invoice contains the payment_hash but it was encrypted inside an onion message.
86         let amount_msats = invoice.amount_msats();
87         let payment_hash = invoice.payment_hash();
88         do_pass_along_path(
89                 node, path, amount_msats, payment_hash, None, ev, false, false, None, false
90         );
91 }
92
93 fn claim_bolt12_payment<'a, 'b, 'c>(node: &Node<'a, 'b, 'c>, path: &[&Node<'a, 'b, 'c>]) {
94         let recipient = &path[path.len() - 1];
95         match get_event!(recipient, Event::PaymentClaimable) {
96                 Event::PaymentClaimable {
97                         purpose: PaymentPurpose::InvoicePayment {
98                                 payment_preimage: Some(payment_preimage), ..
99                         }, ..
100                 } => claim_payment(node, path, payment_preimage),
101                 _ => panic!(),
102         };
103 }
104
105 fn extract_invoice_request<'a, 'b, 'c>(
106         node: &Node<'a, 'b, 'c>, message: &OnionMessage
107 ) -> (InvoiceRequest, Option<BlindedPath>) {
108         match node.onion_messenger.peel_onion_message(message) {
109                 Ok(PeeledOnion::Receive(message, _, reply_path)) => match message {
110                         ParsedOnionMessageContents::Offers(offers_message) => match offers_message {
111                                 OffersMessage::InvoiceRequest(invoice_request) => (invoice_request, reply_path),
112                                 OffersMessage::Invoice(invoice) => panic!("Unexpected invoice: {:?}", invoice),
113                                 OffersMessage::InvoiceError(error) => panic!("Unexpected invoice_error: {:?}", error),
114                         },
115                         ParsedOnionMessageContents::Custom(message) => panic!("Unexpected custom message: {:?}", message),
116                 },
117                 Ok(PeeledOnion::Forward(_, _)) => panic!("Unexpected onion message forward"),
118                 Err(e) => panic!("Failed to process onion message {:?}", e),
119         }
120 }
121
122 fn extract_invoice<'a, 'b, 'c>(node: &Node<'a, 'b, 'c>, message: &OnionMessage) -> Bolt12Invoice {
123         match node.onion_messenger.peel_onion_message(message) {
124                 Ok(PeeledOnion::Receive(message, _, _)) => match message {
125                         ParsedOnionMessageContents::Offers(offers_message) => match offers_message {
126                                 OffersMessage::InvoiceRequest(invoice_request) => panic!("Unexpected invoice_request: {:?}", invoice_request),
127                                 OffersMessage::Invoice(invoice) => invoice,
128                                 OffersMessage::InvoiceError(error) => panic!("Unexpected invoice_error: {:?}", error),
129                         },
130                         ParsedOnionMessageContents::Custom(message) => panic!("Unexpected custom message: {:?}", message),
131                 },
132                 Ok(PeeledOnion::Forward(_, _)) => panic!("Unexpected onion message forward"),
133                 Err(e) => panic!("Failed to process onion message {:?}", e),
134         }
135 }
136
137 /// Checks that an offer can be paid through blinded paths and that ephemeral pubkeys are used
138 /// rather than exposing a node's pubkey.
139 #[test]
140 fn creates_and_pays_for_offer_using_two_hop_blinded_path() {
141         let mut accept_forward_cfg = test_default_channel_config();
142         accept_forward_cfg.accept_forwards_to_priv_channels = true;
143
144         let mut features = channelmanager::provided_init_features(&accept_forward_cfg);
145         features.set_onion_messages_optional();
146         features.set_route_blinding_optional();
147
148         let chanmon_cfgs = create_chanmon_cfgs(6);
149         let node_cfgs = create_node_cfgs(6, &chanmon_cfgs);
150
151         *node_cfgs[1].override_init_features.borrow_mut() = Some(features);
152
153         let node_chanmgrs = create_node_chanmgrs(
154                 6, &node_cfgs, &[None, Some(accept_forward_cfg), None, None, None, None]
155         );
156         let nodes = create_network(6, &node_cfgs, &node_chanmgrs);
157
158         create_unannounced_chan_between_nodes_with_value(&nodes, 0, 1, 10_000_000, 1_000_000_000);
159         create_unannounced_chan_between_nodes_with_value(&nodes, 2, 3, 10_000_000, 1_000_000_000);
160         create_announced_chan_between_nodes_with_value(&nodes, 1, 2, 10_000_000, 1_000_000_000);
161         create_announced_chan_between_nodes_with_value(&nodes, 1, 4, 10_000_000, 1_000_000_000);
162         create_announced_chan_between_nodes_with_value(&nodes, 1, 5, 10_000_000, 1_000_000_000);
163         create_announced_chan_between_nodes_with_value(&nodes, 2, 4, 10_000_000, 1_000_000_000);
164         create_announced_chan_between_nodes_with_value(&nodes, 2, 5, 10_000_000, 1_000_000_000);
165
166         let (alice, bob, charlie, david) = (&nodes[0], &nodes[1], &nodes[2], &nodes[3]);
167         let alice_id = alice.node.get_our_node_id();
168         let bob_id = bob.node.get_our_node_id();
169         let charlie_id = charlie.node.get_our_node_id();
170         let david_id = david.node.get_our_node_id();
171
172         disconnect_peers(alice, &[charlie, david, &nodes[4], &nodes[5]]);
173         disconnect_peers(david, &[bob, &nodes[4], &nodes[5]]);
174
175         let offer = alice.node
176                 .create_offer_builder("coffee".to_string()).unwrap()
177                 .amount_msats(10_000_000)
178                 .build().unwrap();
179         assert_ne!(offer.signing_pubkey(), alice_id);
180         assert!(!offer.paths().is_empty());
181         for path in offer.paths() {
182                 assert_eq!(path.introduction_node_id, bob_id);
183         }
184
185         let payment_id = PaymentId([1; 32]);
186         david.node.pay_for_offer(&offer, None, None, None, payment_id, Retry::Attempts(0), None)
187                 .unwrap();
188         expect_recent_payment!(david, RecentPaymentDetails::AwaitingInvoice, payment_id);
189
190         connect_peers(david, bob);
191
192         let onion_message = david.onion_messenger.next_onion_message_for_peer(bob_id).unwrap();
193         bob.onion_messenger.handle_onion_message(&david_id, &onion_message);
194
195         connect_peers(alice, charlie);
196
197         let onion_message = bob.onion_messenger.next_onion_message_for_peer(alice_id).unwrap();
198         alice.onion_messenger.handle_onion_message(&bob_id, &onion_message);
199
200         let (invoice_request, reply_path) = extract_invoice_request(alice, &onion_message);
201         assert_eq!(invoice_request.amount_msats(), None);
202         assert_ne!(invoice_request.payer_id(), david_id);
203         assert_eq!(reply_path.unwrap().introduction_node_id, charlie_id);
204
205         let onion_message = alice.onion_messenger.next_onion_message_for_peer(charlie_id).unwrap();
206         charlie.onion_messenger.handle_onion_message(&alice_id, &onion_message);
207
208         let onion_message = charlie.onion_messenger.next_onion_message_for_peer(david_id).unwrap();
209         david.onion_messenger.handle_onion_message(&charlie_id, &onion_message);
210
211         let invoice = extract_invoice(david, &onion_message);
212         assert_eq!(invoice.amount_msats(), 10_000_000);
213         assert_ne!(invoice.signing_pubkey(), alice_id);
214         assert!(!invoice.payment_paths().is_empty());
215         for (_, path) in invoice.payment_paths() {
216                 assert_eq!(path.introduction_node_id, bob_id);
217         }
218
219         route_bolt12_payment(david, &[charlie, bob, alice], &invoice);
220         expect_recent_payment!(david, RecentPaymentDetails::Pending, payment_id);
221
222         claim_bolt12_payment(david, &[charlie, bob, alice]);
223         expect_recent_payment!(david, RecentPaymentDetails::Fulfilled, payment_id);
224 }
225
226 /// Checks that a refund can be paid through blinded paths and that ephemeral pubkeys are used
227 /// rather than exposing a node's pubkey.
228 #[test]
229 fn creates_and_pays_for_refund_using_two_hop_blinded_path() {
230         let mut accept_forward_cfg = test_default_channel_config();
231         accept_forward_cfg.accept_forwards_to_priv_channels = true;
232
233         let mut features = channelmanager::provided_init_features(&accept_forward_cfg);
234         features.set_onion_messages_optional();
235         features.set_route_blinding_optional();
236
237         let chanmon_cfgs = create_chanmon_cfgs(6);
238         let node_cfgs = create_node_cfgs(6, &chanmon_cfgs);
239
240         *node_cfgs[1].override_init_features.borrow_mut() = Some(features);
241
242         let node_chanmgrs = create_node_chanmgrs(
243                 6, &node_cfgs, &[None, Some(accept_forward_cfg), None, None, None, None]
244         );
245         let nodes = create_network(6, &node_cfgs, &node_chanmgrs);
246
247         create_unannounced_chan_between_nodes_with_value(&nodes, 0, 1, 10_000_000, 1_000_000_000);
248         create_unannounced_chan_between_nodes_with_value(&nodes, 2, 3, 10_000_000, 1_000_000_000);
249         create_announced_chan_between_nodes_with_value(&nodes, 1, 2, 10_000_000, 1_000_000_000);
250         create_announced_chan_between_nodes_with_value(&nodes, 1, 4, 10_000_000, 1_000_000_000);
251         create_announced_chan_between_nodes_with_value(&nodes, 1, 5, 10_000_000, 1_000_000_000);
252         create_announced_chan_between_nodes_with_value(&nodes, 2, 4, 10_000_000, 1_000_000_000);
253         create_announced_chan_between_nodes_with_value(&nodes, 2, 5, 10_000_000, 1_000_000_000);
254
255         let (alice, bob, charlie, david) = (&nodes[0], &nodes[1], &nodes[2], &nodes[3]);
256         let alice_id = alice.node.get_our_node_id();
257         let bob_id = bob.node.get_our_node_id();
258         let charlie_id = charlie.node.get_our_node_id();
259         let david_id = david.node.get_our_node_id();
260
261         disconnect_peers(alice, &[charlie, david, &nodes[4], &nodes[5]]);
262         disconnect_peers(david, &[bob, &nodes[4], &nodes[5]]);
263
264         let absolute_expiry = Duration::from_secs(u64::MAX);
265         let payment_id = PaymentId([1; 32]);
266         let refund = david.node
267                 .create_refund_builder(
268                         "refund".to_string(), 10_000_000, absolute_expiry, payment_id, Retry::Attempts(0), None
269                 )
270                 .unwrap()
271                 .build().unwrap();
272         assert_eq!(refund.amount_msats(), 10_000_000);
273         assert_eq!(refund.absolute_expiry(), Some(absolute_expiry));
274         assert_ne!(refund.payer_id(), david_id);
275         assert!(!refund.paths().is_empty());
276         for path in refund.paths() {
277                 assert_eq!(path.introduction_node_id, charlie_id);
278         }
279         expect_recent_payment!(david, RecentPaymentDetails::AwaitingInvoice, payment_id);
280
281         alice.node.request_refund_payment(&refund).unwrap();
282
283         connect_peers(alice, charlie);
284
285         let onion_message = alice.onion_messenger.next_onion_message_for_peer(charlie_id).unwrap();
286         charlie.onion_messenger.handle_onion_message(&alice_id, &onion_message);
287
288         let onion_message = charlie.onion_messenger.next_onion_message_for_peer(david_id).unwrap();
289         david.onion_messenger.handle_onion_message(&charlie_id, &onion_message);
290
291         let invoice = extract_invoice(david, &onion_message);
292         assert_eq!(invoice.amount_msats(), 10_000_000);
293         assert_ne!(invoice.signing_pubkey(), alice_id);
294         assert!(!invoice.payment_paths().is_empty());
295         for (_, path) in invoice.payment_paths() {
296                 assert_eq!(path.introduction_node_id, bob_id);
297         }
298
299         route_bolt12_payment(david, &[charlie, bob, alice], &invoice);
300         expect_recent_payment!(david, RecentPaymentDetails::Pending, payment_id);
301
302         claim_bolt12_payment(david, &[charlie, bob, alice]);
303         expect_recent_payment!(david, RecentPaymentDetails::Fulfilled, payment_id);
304 }
305
306 /// Checks that an offer can be paid through a one-hop blinded path and that ephemeral pubkeys are
307 /// used rather than exposing a node's pubkey. However, the node's pubkey is still used as the
308 /// introduction node of the blinded path.
309 #[test]
310 fn creates_and_pays_for_offer_using_one_hop_blinded_path() {
311         let chanmon_cfgs = create_chanmon_cfgs(2);
312         let node_cfgs = create_node_cfgs(2, &chanmon_cfgs);
313         let node_chanmgrs = create_node_chanmgrs(2, &node_cfgs, &[None, None]);
314         let nodes = create_network(2, &node_cfgs, &node_chanmgrs);
315
316         create_announced_chan_between_nodes_with_value(&nodes, 0, 1, 10_000_000, 1_000_000_000);
317
318         let alice = &nodes[0];
319         let alice_id = alice.node.get_our_node_id();
320         let bob = &nodes[1];
321         let bob_id = bob.node.get_our_node_id();
322
323         let offer = alice.node
324                 .create_offer_builder("coffee".to_string()).unwrap()
325                 .amount_msats(10_000_000)
326                 .build().unwrap();
327         assert_ne!(offer.signing_pubkey(), alice_id);
328         assert!(!offer.paths().is_empty());
329         for path in offer.paths() {
330                 assert_eq!(path.introduction_node_id, alice_id);
331         }
332
333         let payment_id = PaymentId([1; 32]);
334         bob.node.pay_for_offer(&offer, None, None, None, payment_id, Retry::Attempts(0), None).unwrap();
335         expect_recent_payment!(bob, RecentPaymentDetails::AwaitingInvoice, payment_id);
336
337         let onion_message = bob.onion_messenger.next_onion_message_for_peer(alice_id).unwrap();
338         alice.onion_messenger.handle_onion_message(&bob_id, &onion_message);
339
340         let (invoice_request, reply_path) = extract_invoice_request(alice, &onion_message);
341         assert_eq!(invoice_request.amount_msats(), None);
342         assert_ne!(invoice_request.payer_id(), bob_id);
343         assert_eq!(reply_path.unwrap().introduction_node_id, bob_id);
344
345         let onion_message = alice.onion_messenger.next_onion_message_for_peer(bob_id).unwrap();
346         bob.onion_messenger.handle_onion_message(&alice_id, &onion_message);
347
348         let invoice = extract_invoice(bob, &onion_message);
349         assert_eq!(invoice.amount_msats(), 10_000_000);
350         assert_ne!(invoice.signing_pubkey(), alice_id);
351         assert!(!invoice.payment_paths().is_empty());
352         for (_, path) in invoice.payment_paths() {
353                 assert_eq!(path.introduction_node_id, alice_id);
354         }
355
356         route_bolt12_payment(bob, &[alice], &invoice);
357         expect_recent_payment!(bob, RecentPaymentDetails::Pending, payment_id);
358
359         claim_bolt12_payment(bob, &[alice]);
360         expect_recent_payment!(bob, RecentPaymentDetails::Fulfilled, payment_id);
361 }
362
363 /// Checks that a refund can be paid through a one-hop blinded path and that ephemeral pubkeys are
364 /// used rather than exposing a node's pubkey. However, the node's pubkey is still used as the
365 /// introduction node of the blinded path.
366 #[test]
367 fn creates_and_pays_for_refund_using_one_hop_blinded_path() {
368         let chanmon_cfgs = create_chanmon_cfgs(2);
369         let node_cfgs = create_node_cfgs(2, &chanmon_cfgs);
370         let node_chanmgrs = create_node_chanmgrs(2, &node_cfgs, &[None, None]);
371         let nodes = create_network(2, &node_cfgs, &node_chanmgrs);
372
373         create_announced_chan_between_nodes_with_value(&nodes, 0, 1, 10_000_000, 1_000_000_000);
374
375         let alice = &nodes[0];
376         let alice_id = alice.node.get_our_node_id();
377         let bob = &nodes[1];
378         let bob_id = bob.node.get_our_node_id();
379
380         let absolute_expiry = Duration::from_secs(u64::MAX);
381         let payment_id = PaymentId([1; 32]);
382         let refund = bob.node
383                 .create_refund_builder(
384                         "refund".to_string(), 10_000_000, absolute_expiry, payment_id, Retry::Attempts(0), None
385                 )
386                 .unwrap()
387                 .build().unwrap();
388         assert_eq!(refund.amount_msats(), 10_000_000);
389         assert_eq!(refund.absolute_expiry(), Some(absolute_expiry));
390         assert_ne!(refund.payer_id(), bob_id);
391         assert!(!refund.paths().is_empty());
392         for path in refund.paths() {
393                 assert_eq!(path.introduction_node_id, bob_id);
394         }
395         expect_recent_payment!(bob, RecentPaymentDetails::AwaitingInvoice, payment_id);
396
397         alice.node.request_refund_payment(&refund).unwrap();
398
399         let onion_message = alice.onion_messenger.next_onion_message_for_peer(bob_id).unwrap();
400         bob.onion_messenger.handle_onion_message(&alice_id, &onion_message);
401
402         let invoice = extract_invoice(bob, &onion_message);
403         assert_eq!(invoice.amount_msats(), 10_000_000);
404         assert_ne!(invoice.signing_pubkey(), alice_id);
405         assert!(!invoice.payment_paths().is_empty());
406         for (_, path) in invoice.payment_paths() {
407                 assert_eq!(path.introduction_node_id, alice_id);
408         }
409
410         route_bolt12_payment(bob, &[alice], &invoice);
411         expect_recent_payment!(bob, RecentPaymentDetails::Pending, payment_id);
412
413         claim_bolt12_payment(bob, &[alice]);
414         expect_recent_payment!(bob, RecentPaymentDetails::Fulfilled, payment_id);
415 }
416
417 /// Checks that an invoice for an offer without any blinded paths can be requested. Note that while
418 /// the requested is sent directly using the node's pubkey, the response and the payment still use
419 /// blinded paths as required by the spec.
420 #[test]
421 fn pays_for_offer_without_blinded_paths() {
422         let chanmon_cfgs = create_chanmon_cfgs(2);
423         let node_cfgs = create_node_cfgs(2, &chanmon_cfgs);
424         let node_chanmgrs = create_node_chanmgrs(2, &node_cfgs, &[None, None]);
425         let nodes = create_network(2, &node_cfgs, &node_chanmgrs);
426
427         create_announced_chan_between_nodes_with_value(&nodes, 0, 1, 10_000_000, 1_000_000_000);
428
429         let alice = &nodes[0];
430         let alice_id = alice.node.get_our_node_id();
431         let bob = &nodes[1];
432         let bob_id = bob.node.get_our_node_id();
433
434         let offer = alice.node
435                 .create_offer_builder("coffee".to_string()).unwrap()
436                 .clear_paths()
437                 .amount_msats(10_000_000)
438                 .build().unwrap();
439         assert_eq!(offer.signing_pubkey(), alice_id);
440         assert!(offer.paths().is_empty());
441
442         let payment_id = PaymentId([1; 32]);
443         bob.node.pay_for_offer(&offer, None, None, None, payment_id, Retry::Attempts(0), None).unwrap();
444         expect_recent_payment!(bob, RecentPaymentDetails::AwaitingInvoice, payment_id);
445
446         let onion_message = bob.onion_messenger.next_onion_message_for_peer(alice_id).unwrap();
447         alice.onion_messenger.handle_onion_message(&bob_id, &onion_message);
448
449         let onion_message = alice.onion_messenger.next_onion_message_for_peer(bob_id).unwrap();
450         bob.onion_messenger.handle_onion_message(&alice_id, &onion_message);
451
452         let invoice = extract_invoice(bob, &onion_message);
453         route_bolt12_payment(bob, &[alice], &invoice);
454         expect_recent_payment!(bob, RecentPaymentDetails::Pending, payment_id);
455
456         claim_bolt12_payment(bob, &[alice]);
457         expect_recent_payment!(bob, RecentPaymentDetails::Fulfilled, payment_id);
458 }
459
460 /// Checks that a refund without any blinded paths can be paid. Note that while the invoice is sent
461 /// directly using the node's pubkey, the payment still use blinded paths as required by the spec.
462 #[test]
463 fn pays_for_refund_without_blinded_paths() {
464         let chanmon_cfgs = create_chanmon_cfgs(2);
465         let node_cfgs = create_node_cfgs(2, &chanmon_cfgs);
466         let node_chanmgrs = create_node_chanmgrs(2, &node_cfgs, &[None, None]);
467         let nodes = create_network(2, &node_cfgs, &node_chanmgrs);
468
469         create_announced_chan_between_nodes_with_value(&nodes, 0, 1, 10_000_000, 1_000_000_000);
470
471         let alice = &nodes[0];
472         let alice_id = alice.node.get_our_node_id();
473         let bob = &nodes[1];
474         let bob_id = bob.node.get_our_node_id();
475
476         let absolute_expiry = Duration::from_secs(u64::MAX);
477         let payment_id = PaymentId([1; 32]);
478         let refund = bob.node
479                 .create_refund_builder(
480                         "refund".to_string(), 10_000_000, absolute_expiry, payment_id, Retry::Attempts(0), None
481                 )
482                 .unwrap()
483                 .clear_paths()
484                 .build().unwrap();
485         assert_eq!(refund.payer_id(), bob_id);
486         assert!(refund.paths().is_empty());
487         expect_recent_payment!(bob, RecentPaymentDetails::AwaitingInvoice, payment_id);
488
489         alice.node.request_refund_payment(&refund).unwrap();
490
491         let onion_message = alice.onion_messenger.next_onion_message_for_peer(bob_id).unwrap();
492         bob.onion_messenger.handle_onion_message(&alice_id, &onion_message);
493
494         let invoice = extract_invoice(bob, &onion_message);
495         route_bolt12_payment(bob, &[alice], &invoice);
496         expect_recent_payment!(bob, RecentPaymentDetails::Pending, payment_id);
497
498         claim_bolt12_payment(bob, &[alice]);
499         expect_recent_payment!(bob, RecentPaymentDetails::Fulfilled, payment_id);
500 }
501
502 /// Fails creating an offer when a blinded path cannot be created without exposing the node's id.
503 #[test]
504 fn fails_creating_offer_without_blinded_paths() {
505         let chanmon_cfgs = create_chanmon_cfgs(2);
506         let node_cfgs = create_node_cfgs(2, &chanmon_cfgs);
507         let node_chanmgrs = create_node_chanmgrs(2, &node_cfgs, &[None, None]);
508         let nodes = create_network(2, &node_cfgs, &node_chanmgrs);
509
510         create_unannounced_chan_between_nodes_with_value(&nodes, 0, 1, 10_000_000, 1_000_000_000);
511
512         match nodes[0].node.create_offer_builder("coffee".to_string()) {
513                 Ok(_) => panic!("Expected error"),
514                 Err(e) => assert_eq!(e, Bolt12SemanticError::MissingPaths),
515         }
516 }
517
518 /// Fails creating a refund when a blinded path cannot be created without exposing the node's id.
519 #[test]
520 fn fails_creating_refund_without_blinded_paths() {
521         let chanmon_cfgs = create_chanmon_cfgs(2);
522         let node_cfgs = create_node_cfgs(2, &chanmon_cfgs);
523         let node_chanmgrs = create_node_chanmgrs(2, &node_cfgs, &[None, None]);
524         let nodes = create_network(2, &node_cfgs, &node_chanmgrs);
525
526         create_unannounced_chan_between_nodes_with_value(&nodes, 0, 1, 10_000_000, 1_000_000_000);
527
528         let absolute_expiry = Duration::from_secs(u64::MAX);
529         let payment_id = PaymentId([1; 32]);
530
531         match nodes[0].node.create_refund_builder(
532                 "refund".to_string(), 10_000, absolute_expiry, payment_id, Retry::Attempts(0), None
533         ) {
534                 Ok(_) => panic!("Expected error"),
535                 Err(e) => assert_eq!(e, Bolt12SemanticError::MissingPaths),
536         }
537
538         assert!(nodes[0].node.list_recent_payments().is_empty());
539 }
540
541 /// Fails creating an invoice request when a blinded reply path cannot be created without exposing
542 /// the node's id.
543 #[test]
544 fn fails_creating_invoice_request_without_blinded_reply_path() {
545         let chanmon_cfgs = create_chanmon_cfgs(6);
546         let node_cfgs = create_node_cfgs(6, &chanmon_cfgs);
547         let node_chanmgrs = create_node_chanmgrs(6, &node_cfgs, &[None, None, None, None, None, None]);
548         let nodes = create_network(6, &node_cfgs, &node_chanmgrs);
549
550         create_unannounced_chan_between_nodes_with_value(&nodes, 0, 1, 10_000_000, 1_000_000_000);
551         create_unannounced_chan_between_nodes_with_value(&nodes, 2, 3, 10_000_000, 1_000_000_000);
552         create_announced_chan_between_nodes_with_value(&nodes, 1, 2, 10_000_000, 1_000_000_000);
553         create_announced_chan_between_nodes_with_value(&nodes, 1, 4, 10_000_000, 1_000_000_000);
554         create_announced_chan_between_nodes_with_value(&nodes, 1, 5, 10_000_000, 1_000_000_000);
555
556         let (alice, bob, charlie, david) = (&nodes[0], &nodes[1], &nodes[2], &nodes[3]);
557
558         disconnect_peers(alice, &[charlie, david, &nodes[4], &nodes[5]]);
559         disconnect_peers(david, &[bob, &nodes[4], &nodes[5]]);
560
561         let offer = alice.node
562                 .create_offer_builder("coffee".to_string()).unwrap()
563                 .amount_msats(10_000_000)
564                 .build().unwrap();
565
566         let payment_id = PaymentId([1; 32]);
567
568         match david.node.pay_for_offer(&offer, None, None, None, payment_id, Retry::Attempts(0), None) {
569                 Ok(_) => panic!("Expected error"),
570                 Err(e) => assert_eq!(e, Bolt12SemanticError::MissingPaths),
571         }
572
573         assert!(nodes[0].node.list_recent_payments().is_empty());
574 }
575
576 #[test]
577 fn fails_creating_invoice_request_with_duplicate_payment_id() {
578         let chanmon_cfgs = create_chanmon_cfgs(6);
579         let node_cfgs = create_node_cfgs(6, &chanmon_cfgs);
580         let node_chanmgrs = create_node_chanmgrs(6, &node_cfgs, &[None, None, None, None, None, None]);
581         let nodes = create_network(6, &node_cfgs, &node_chanmgrs);
582
583         create_unannounced_chan_between_nodes_with_value(&nodes, 0, 1, 10_000_000, 1_000_000_000);
584         create_unannounced_chan_between_nodes_with_value(&nodes, 2, 3, 10_000_000, 1_000_000_000);
585         create_announced_chan_between_nodes_with_value(&nodes, 1, 2, 10_000_000, 1_000_000_000);
586         create_announced_chan_between_nodes_with_value(&nodes, 1, 4, 10_000_000, 1_000_000_000);
587         create_announced_chan_between_nodes_with_value(&nodes, 1, 5, 10_000_000, 1_000_000_000);
588         create_announced_chan_between_nodes_with_value(&nodes, 2, 4, 10_000_000, 1_000_000_000);
589         create_announced_chan_between_nodes_with_value(&nodes, 2, 5, 10_000_000, 1_000_000_000);
590
591         let (alice, _bob, charlie, david) = (&nodes[0], &nodes[1], &nodes[2], &nodes[3]);
592
593         disconnect_peers(alice, &[charlie, david, &nodes[4], &nodes[5]]);
594
595         let offer = alice.node
596                 .create_offer_builder("coffee".to_string()).unwrap()
597                 .amount_msats(10_000_000)
598                 .build().unwrap();
599
600         let payment_id = PaymentId([1; 32]);
601         assert!(
602                 david.node.pay_for_offer(
603                         &offer, None, None, None, payment_id, Retry::Attempts(0), None
604                 ).is_ok()
605         );
606         expect_recent_payment!(david, RecentPaymentDetails::AwaitingInvoice, payment_id);
607
608         match david.node.pay_for_offer(&offer, None, None, None, payment_id, Retry::Attempts(0), None) {
609                 Ok(_) => panic!("Expected error"),
610                 Err(e) => assert_eq!(e, Bolt12SemanticError::DuplicatePaymentId),
611         }
612
613         expect_recent_payment!(david, RecentPaymentDetails::AwaitingInvoice, payment_id);
614 }
615
616 #[test]
617 fn fails_creating_refund_with_duplicate_payment_id() {
618         let chanmon_cfgs = create_chanmon_cfgs(2);
619         let node_cfgs = create_node_cfgs(2, &chanmon_cfgs);
620         let node_chanmgrs = create_node_chanmgrs(2, &node_cfgs, &[None, None]);
621         let nodes = create_network(2, &node_cfgs, &node_chanmgrs);
622
623         create_announced_chan_between_nodes_with_value(&nodes, 0, 1, 10_000_000, 1_000_000_000);
624
625         let absolute_expiry = Duration::from_secs(u64::MAX);
626         let payment_id = PaymentId([1; 32]);
627         assert!(
628                 nodes[0].node.create_refund_builder(
629                         "refund".to_string(), 10_000, absolute_expiry, payment_id, Retry::Attempts(0), None
630                 ).is_ok()
631         );
632         expect_recent_payment!(nodes[0], RecentPaymentDetails::AwaitingInvoice, payment_id);
633
634         match nodes[0].node.create_refund_builder(
635                 "refund".to_string(), 10_000, absolute_expiry, payment_id, Retry::Attempts(0), None
636         ) {
637                 Ok(_) => panic!("Expected error"),
638                 Err(e) => assert_eq!(e, Bolt12SemanticError::DuplicatePaymentId),
639         }
640
641         expect_recent_payment!(nodes[0], RecentPaymentDetails::AwaitingInvoice, payment_id);
642 }