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