Rename InvoicePayment to Bolt11InvoicePayment
[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 bitcoin::network::constants::Network;
44 use core::time::Duration;
45 use crate::blinded_path::{BlindedPath, IntroductionNode};
46 use crate::events::{Event, MessageSendEventsProvider, PaymentPurpose};
47 use crate::ln::channelmanager::{PaymentId, RecentPaymentDetails, Retry, self};
48 use crate::ln::functional_test_utils::*;
49 use crate::ln::msgs::{ChannelMessageHandler, Init, NodeAnnouncement, OnionMessage, OnionMessageHandler, RoutingMessageHandler, SocketAddress, UnsignedGossipMessage, UnsignedNodeAnnouncement};
50 use crate::offers::invoice::Bolt12Invoice;
51 use crate::offers::invoice_error::InvoiceError;
52 use crate::offers::invoice_request::InvoiceRequest;
53 use crate::offers::parse::Bolt12SemanticError;
54 use crate::onion_message::messenger::PeeledOnion;
55 use crate::onion_message::offers::OffersMessage;
56 use crate::onion_message::packet::ParsedOnionMessageContents;
57 use crate::routing::gossip::{NodeAlias, NodeId};
58 use crate::sign::{NodeSigner, Recipient};
59
60 use crate::prelude::*;
61
62 macro_rules! expect_recent_payment {
63         ($node: expr, $payment_state: path, $payment_id: expr) => {
64                 match $node.node.list_recent_payments().first() {
65                         Some(&$payment_state { payment_id: actual_payment_id, .. }) => {
66                                 assert_eq!($payment_id, actual_payment_id);
67                         },
68                         Some(_) => panic!("Unexpected recent payment state"),
69                         None => panic!("No recent payments"),
70                 }
71         }
72 }
73
74 fn connect_peers<'a, 'b, 'c>(node_a: &Node<'a, 'b, 'c>, node_b: &Node<'a, 'b, 'c>) {
75         let node_id_a = node_a.node.get_our_node_id();
76         let node_id_b = node_b.node.get_our_node_id();
77
78         let init_a = Init {
79                 features: node_a.init_features(&node_id_b),
80                 networks: None,
81                 remote_network_address: None,
82         };
83         let init_b = Init {
84                 features: node_b.init_features(&node_id_a),
85                 networks: None,
86                 remote_network_address: None,
87         };
88
89         node_a.node.peer_connected(&node_id_b, &init_b, true).unwrap();
90         node_b.node.peer_connected(&node_id_a, &init_a, false).unwrap();
91         node_a.onion_messenger.peer_connected(&node_id_b, &init_b, true).unwrap();
92         node_b.onion_messenger.peer_connected(&node_id_a, &init_a, false).unwrap();
93 }
94
95 fn disconnect_peers<'a, 'b, 'c>(node_a: &Node<'a, 'b, 'c>, peers: &[&Node<'a, 'b, 'c>]) {
96         for node_b in peers {
97                 node_a.node.peer_disconnected(&node_b.node.get_our_node_id());
98                 node_b.node.peer_disconnected(&node_a.node.get_our_node_id());
99                 node_a.onion_messenger.peer_disconnected(&node_b.node.get_our_node_id());
100                 node_b.onion_messenger.peer_disconnected(&node_a.node.get_our_node_id());
101         }
102 }
103
104 fn announce_node_address<'a, 'b, 'c>(
105         node: &Node<'a, 'b, 'c>, peers: &[&Node<'a, 'b, 'c>], address: SocketAddress,
106 ) {
107         let features = node.onion_messenger.provided_node_features()
108                 | node.gossip_sync.provided_node_features();
109         let rgb = [0u8; 3];
110         let announcement = UnsignedNodeAnnouncement {
111                 features,
112                 timestamp: 1000,
113                 node_id: NodeId::from_pubkey(&node.keys_manager.get_node_id(Recipient::Node).unwrap()),
114                 rgb,
115                 alias: NodeAlias([0u8; 32]),
116                 addresses: vec![address],
117                 excess_address_data: Vec::new(),
118                 excess_data: Vec::new(),
119         };
120         let signature = node.keys_manager.sign_gossip_message(
121                 UnsignedGossipMessage::NodeAnnouncement(&announcement)
122         ).unwrap();
123
124         let msg = NodeAnnouncement {
125                 signature,
126                 contents: announcement
127         };
128
129         node.gossip_sync.handle_node_announcement(&msg).unwrap();
130         for peer in peers {
131                 peer.gossip_sync.handle_node_announcement(&msg).unwrap();
132         }
133 }
134
135 fn route_bolt12_payment<'a, 'b, 'c>(
136         node: &Node<'a, 'b, 'c>, path: &[&Node<'a, 'b, 'c>], invoice: &Bolt12Invoice
137 ) {
138         // Monitor added when handling the invoice onion message.
139         check_added_monitors(node, 1);
140
141         let mut events = node.node.get_and_clear_pending_msg_events();
142         assert_eq!(events.len(), 1);
143         let ev = remove_first_msg_event_to_node(&path[0].node.get_our_node_id(), &mut events);
144
145         // Use a fake payment_hash and bypass checking for the PaymentClaimable event since the
146         // invoice contains the payment_hash but it was encrypted inside an onion message.
147         let amount_msats = invoice.amount_msats();
148         let payment_hash = invoice.payment_hash();
149         let args = PassAlongPathArgs::new(node, path, amount_msats, payment_hash, ev)
150                 .without_clearing_recipient_events();
151         do_pass_along_path(args);
152 }
153
154 fn claim_bolt12_payment<'a, 'b, 'c>(node: &Node<'a, 'b, 'c>, path: &[&Node<'a, 'b, 'c>]) {
155         let recipient = &path[path.len() - 1];
156         match get_event!(recipient, Event::PaymentClaimable) {
157                 Event::PaymentClaimable {
158                         purpose: PaymentPurpose::Bolt11InvoicePayment {
159                                 payment_preimage: Some(payment_preimage), ..
160                         }, ..
161                 } => claim_payment(node, path, payment_preimage),
162                 _ => panic!(),
163         };
164 }
165
166 fn extract_invoice_request<'a, 'b, 'c>(
167         node: &Node<'a, 'b, 'c>, message: &OnionMessage
168 ) -> (InvoiceRequest, Option<BlindedPath>) {
169         match node.onion_messenger.peel_onion_message(message) {
170                 Ok(PeeledOnion::Receive(message, _, reply_path)) => match message {
171                         ParsedOnionMessageContents::Offers(offers_message) => match offers_message {
172                                 OffersMessage::InvoiceRequest(invoice_request) => (invoice_request, reply_path),
173                                 OffersMessage::Invoice(invoice) => panic!("Unexpected invoice: {:?}", invoice),
174                                 OffersMessage::InvoiceError(error) => panic!("Unexpected invoice_error: {:?}", error),
175                         },
176                         ParsedOnionMessageContents::Custom(message) => panic!("Unexpected custom message: {:?}", message),
177                 },
178                 Ok(PeeledOnion::Forward(_, _)) => panic!("Unexpected onion message forward"),
179                 Err(e) => panic!("Failed to process onion message {:?}", e),
180         }
181 }
182
183 fn extract_invoice<'a, 'b, 'c>(node: &Node<'a, 'b, 'c>, message: &OnionMessage) -> Bolt12Invoice {
184         match node.onion_messenger.peel_onion_message(message) {
185                 Ok(PeeledOnion::Receive(message, _, _)) => match message {
186                         ParsedOnionMessageContents::Offers(offers_message) => match offers_message {
187                                 OffersMessage::InvoiceRequest(invoice_request) => panic!("Unexpected invoice_request: {:?}", invoice_request),
188                                 OffersMessage::Invoice(invoice) => invoice,
189                                 OffersMessage::InvoiceError(error) => panic!("Unexpected invoice_error: {:?}", error),
190                         },
191                         ParsedOnionMessageContents::Custom(message) => panic!("Unexpected custom message: {:?}", message),
192                 },
193                 Ok(PeeledOnion::Forward(_, _)) => panic!("Unexpected onion message forward"),
194                 Err(e) => panic!("Failed to process onion message {:?}", e),
195         }
196 }
197
198 fn extract_invoice_error<'a, 'b, 'c>(
199         node: &Node<'a, 'b, 'c>, message: &OnionMessage
200 ) -> InvoiceError {
201         match node.onion_messenger.peel_onion_message(message) {
202                 Ok(PeeledOnion::Receive(message, _, _)) => match message {
203                         ParsedOnionMessageContents::Offers(offers_message) => match offers_message {
204                                 OffersMessage::InvoiceRequest(invoice_request) => panic!("Unexpected invoice_request: {:?}", invoice_request),
205                                 OffersMessage::Invoice(invoice) => panic!("Unexpected invoice: {:?}", invoice),
206                                 OffersMessage::InvoiceError(error) => error,
207                         },
208                         ParsedOnionMessageContents::Custom(message) => panic!("Unexpected custom message: {:?}", message),
209                 },
210                 Ok(PeeledOnion::Forward(_, _)) => panic!("Unexpected onion message forward"),
211                 Err(e) => panic!("Failed to process onion message {:?}", e),
212         }
213 }
214
215 /// Checks that blinded paths without Tor-only nodes are preferred when constructing an offer.
216 #[test]
217 fn prefers_non_tor_nodes_in_blinded_paths() {
218         let mut accept_forward_cfg = test_default_channel_config();
219         accept_forward_cfg.accept_forwards_to_priv_channels = true;
220
221         let mut features = channelmanager::provided_init_features(&accept_forward_cfg);
222         features.set_onion_messages_optional();
223         features.set_route_blinding_optional();
224
225         let chanmon_cfgs = create_chanmon_cfgs(6);
226         let node_cfgs = create_node_cfgs(6, &chanmon_cfgs);
227
228         *node_cfgs[1].override_init_features.borrow_mut() = Some(features);
229
230         let node_chanmgrs = create_node_chanmgrs(
231                 6, &node_cfgs, &[None, Some(accept_forward_cfg), None, None, None, None]
232         );
233         let nodes = create_network(6, &node_cfgs, &node_chanmgrs);
234
235         create_unannounced_chan_between_nodes_with_value(&nodes, 0, 1, 10_000_000, 1_000_000_000);
236         create_unannounced_chan_between_nodes_with_value(&nodes, 2, 3, 10_000_000, 1_000_000_000);
237         create_announced_chan_between_nodes_with_value(&nodes, 1, 2, 10_000_000, 1_000_000_000);
238         create_announced_chan_between_nodes_with_value(&nodes, 1, 4, 10_000_000, 1_000_000_000);
239         create_announced_chan_between_nodes_with_value(&nodes, 1, 5, 10_000_000, 1_000_000_000);
240         create_announced_chan_between_nodes_with_value(&nodes, 2, 4, 10_000_000, 1_000_000_000);
241         create_announced_chan_between_nodes_with_value(&nodes, 2, 5, 10_000_000, 1_000_000_000);
242
243         // Add an extra channel so that more than one of Bob's peers have MIN_PEER_CHANNELS.
244         create_announced_chan_between_nodes_with_value(&nodes, 4, 5, 10_000_000, 1_000_000_000);
245
246         let (alice, bob, charlie, david) = (&nodes[0], &nodes[1], &nodes[2], &nodes[3]);
247         let bob_id = bob.node.get_our_node_id();
248         let charlie_id = charlie.node.get_our_node_id();
249
250         disconnect_peers(alice, &[charlie, david, &nodes[4], &nodes[5]]);
251         disconnect_peers(david, &[bob, &nodes[4], &nodes[5]]);
252
253         let tor = SocketAddress::OnionV2([255, 254, 253, 252, 251, 250, 249, 248, 247, 246, 38, 7]);
254         announce_node_address(charlie, &[alice, bob, david, &nodes[4], &nodes[5]], tor.clone());
255
256         let offer = bob.node
257                 .create_offer_builder("coffee".to_string()).unwrap()
258                 .amount_msats(10_000_000)
259                 .build().unwrap();
260         assert_ne!(offer.signing_pubkey(), bob_id);
261         assert!(!offer.paths().is_empty());
262         for path in offer.paths() {
263                 assert_ne!(path.introduction_node, IntroductionNode::NodeId(bob_id));
264                 assert_ne!(path.introduction_node, IntroductionNode::NodeId(charlie_id));
265         }
266
267         // Use a one-hop blinded path when Bob is announced and all his peers are Tor-only.
268         announce_node_address(&nodes[4], &[alice, bob, charlie, david, &nodes[5]], tor.clone());
269         announce_node_address(&nodes[5], &[alice, bob, charlie, david, &nodes[4]], tor.clone());
270
271         let offer = bob.node
272                 .create_offer_builder("coffee".to_string()).unwrap()
273                 .amount_msats(10_000_000)
274                 .build().unwrap();
275         assert_ne!(offer.signing_pubkey(), bob_id);
276         assert!(!offer.paths().is_empty());
277         for path in offer.paths() {
278                 assert_eq!(path.introduction_node, IntroductionNode::NodeId(bob_id));
279         }
280 }
281
282 /// Checks that blinded paths prefer an introduction node that is the most connected.
283 #[test]
284 fn prefers_more_connected_nodes_in_blinded_paths() {
285         let mut accept_forward_cfg = test_default_channel_config();
286         accept_forward_cfg.accept_forwards_to_priv_channels = true;
287
288         let mut features = channelmanager::provided_init_features(&accept_forward_cfg);
289         features.set_onion_messages_optional();
290         features.set_route_blinding_optional();
291
292         let chanmon_cfgs = create_chanmon_cfgs(6);
293         let node_cfgs = create_node_cfgs(6, &chanmon_cfgs);
294
295         *node_cfgs[1].override_init_features.borrow_mut() = Some(features);
296
297         let node_chanmgrs = create_node_chanmgrs(
298                 6, &node_cfgs, &[None, Some(accept_forward_cfg), None, None, None, None]
299         );
300         let nodes = create_network(6, &node_cfgs, &node_chanmgrs);
301
302         create_unannounced_chan_between_nodes_with_value(&nodes, 0, 1, 10_000_000, 1_000_000_000);
303         create_unannounced_chan_between_nodes_with_value(&nodes, 2, 3, 10_000_000, 1_000_000_000);
304         create_announced_chan_between_nodes_with_value(&nodes, 1, 2, 10_000_000, 1_000_000_000);
305         create_announced_chan_between_nodes_with_value(&nodes, 1, 4, 10_000_000, 1_000_000_000);
306         create_announced_chan_between_nodes_with_value(&nodes, 1, 5, 10_000_000, 1_000_000_000);
307         create_announced_chan_between_nodes_with_value(&nodes, 2, 4, 10_000_000, 1_000_000_000);
308         create_announced_chan_between_nodes_with_value(&nodes, 2, 5, 10_000_000, 1_000_000_000);
309
310         // Add extra channels so that more than one of Bob's peers have MIN_PEER_CHANNELS and one has
311         // more than the others.
312         create_announced_chan_between_nodes_with_value(&nodes, 0, 4, 10_000_000, 1_000_000_000);
313         create_announced_chan_between_nodes_with_value(&nodes, 3, 4, 10_000_000, 1_000_000_000);
314
315         let (alice, bob, charlie, david) = (&nodes[0], &nodes[1], &nodes[2], &nodes[3]);
316         let bob_id = bob.node.get_our_node_id();
317
318         disconnect_peers(alice, &[charlie, david, &nodes[4], &nodes[5]]);
319         disconnect_peers(david, &[bob, &nodes[4], &nodes[5]]);
320
321         let offer = bob.node
322                 .create_offer_builder("coffee".to_string()).unwrap()
323                 .amount_msats(10_000_000)
324                 .build().unwrap();
325         assert_ne!(offer.signing_pubkey(), bob_id);
326         assert!(!offer.paths().is_empty());
327         for path in offer.paths() {
328                 assert_eq!(path.introduction_node, IntroductionNode::NodeId(nodes[4].node.get_our_node_id()));
329         }
330 }
331
332 /// Checks that an offer can be paid through blinded paths and that ephemeral pubkeys are used
333 /// rather than exposing a node's pubkey.
334 #[test]
335 fn creates_and_pays_for_offer_using_two_hop_blinded_path() {
336         let mut accept_forward_cfg = test_default_channel_config();
337         accept_forward_cfg.accept_forwards_to_priv_channels = true;
338
339         let mut features = channelmanager::provided_init_features(&accept_forward_cfg);
340         features.set_onion_messages_optional();
341         features.set_route_blinding_optional();
342
343         let chanmon_cfgs = create_chanmon_cfgs(6);
344         let node_cfgs = create_node_cfgs(6, &chanmon_cfgs);
345
346         *node_cfgs[1].override_init_features.borrow_mut() = Some(features);
347
348         let node_chanmgrs = create_node_chanmgrs(
349                 6, &node_cfgs, &[None, Some(accept_forward_cfg), None, None, None, None]
350         );
351         let nodes = create_network(6, &node_cfgs, &node_chanmgrs);
352
353         create_unannounced_chan_between_nodes_with_value(&nodes, 0, 1, 10_000_000, 1_000_000_000);
354         create_unannounced_chan_between_nodes_with_value(&nodes, 2, 3, 10_000_000, 1_000_000_000);
355         create_announced_chan_between_nodes_with_value(&nodes, 1, 2, 10_000_000, 1_000_000_000);
356         create_announced_chan_between_nodes_with_value(&nodes, 1, 4, 10_000_000, 1_000_000_000);
357         create_announced_chan_between_nodes_with_value(&nodes, 1, 5, 10_000_000, 1_000_000_000);
358         create_announced_chan_between_nodes_with_value(&nodes, 2, 4, 10_000_000, 1_000_000_000);
359         create_announced_chan_between_nodes_with_value(&nodes, 2, 5, 10_000_000, 1_000_000_000);
360
361         let (alice, bob, charlie, david) = (&nodes[0], &nodes[1], &nodes[2], &nodes[3]);
362         let alice_id = alice.node.get_our_node_id();
363         let bob_id = bob.node.get_our_node_id();
364         let charlie_id = charlie.node.get_our_node_id();
365         let david_id = david.node.get_our_node_id();
366
367         disconnect_peers(alice, &[charlie, david, &nodes[4], &nodes[5]]);
368         disconnect_peers(david, &[bob, &nodes[4], &nodes[5]]);
369
370         let offer = alice.node
371                 .create_offer_builder("coffee".to_string()).unwrap()
372                 .amount_msats(10_000_000)
373                 .build().unwrap();
374         assert_ne!(offer.signing_pubkey(), alice_id);
375         assert!(!offer.paths().is_empty());
376         for path in offer.paths() {
377                 assert_eq!(path.introduction_node, IntroductionNode::NodeId(bob_id));
378         }
379
380         let payment_id = PaymentId([1; 32]);
381         david.node.pay_for_offer(&offer, None, None, None, payment_id, Retry::Attempts(0), None)
382                 .unwrap();
383         expect_recent_payment!(david, RecentPaymentDetails::AwaitingInvoice, payment_id);
384
385         connect_peers(david, bob);
386
387         let onion_message = david.onion_messenger.next_onion_message_for_peer(bob_id).unwrap();
388         bob.onion_messenger.handle_onion_message(&david_id, &onion_message);
389
390         connect_peers(alice, charlie);
391
392         let onion_message = bob.onion_messenger.next_onion_message_for_peer(alice_id).unwrap();
393         alice.onion_messenger.handle_onion_message(&bob_id, &onion_message);
394
395         let (invoice_request, reply_path) = extract_invoice_request(alice, &onion_message);
396         assert_eq!(invoice_request.amount_msats(), None);
397         assert_ne!(invoice_request.payer_id(), david_id);
398         assert_eq!(reply_path.unwrap().introduction_node, IntroductionNode::NodeId(charlie_id));
399
400         let onion_message = alice.onion_messenger.next_onion_message_for_peer(charlie_id).unwrap();
401         charlie.onion_messenger.handle_onion_message(&alice_id, &onion_message);
402
403         let onion_message = charlie.onion_messenger.next_onion_message_for_peer(david_id).unwrap();
404         david.onion_messenger.handle_onion_message(&charlie_id, &onion_message);
405
406         let invoice = extract_invoice(david, &onion_message);
407         assert_eq!(invoice.amount_msats(), 10_000_000);
408         assert_ne!(invoice.signing_pubkey(), alice_id);
409         assert!(!invoice.payment_paths().is_empty());
410         for (_, path) in invoice.payment_paths() {
411                 assert_eq!(path.introduction_node, IntroductionNode::NodeId(bob_id));
412         }
413
414         route_bolt12_payment(david, &[charlie, bob, alice], &invoice);
415         expect_recent_payment!(david, RecentPaymentDetails::Pending, payment_id);
416
417         claim_bolt12_payment(david, &[charlie, bob, alice]);
418         expect_recent_payment!(david, RecentPaymentDetails::Fulfilled, payment_id);
419 }
420
421 /// Checks that a refund can be paid through blinded paths and that ephemeral pubkeys are used
422 /// rather than exposing a node's pubkey.
423 #[test]
424 fn creates_and_pays_for_refund_using_two_hop_blinded_path() {
425         let mut accept_forward_cfg = test_default_channel_config();
426         accept_forward_cfg.accept_forwards_to_priv_channels = true;
427
428         let mut features = channelmanager::provided_init_features(&accept_forward_cfg);
429         features.set_onion_messages_optional();
430         features.set_route_blinding_optional();
431
432         let chanmon_cfgs = create_chanmon_cfgs(6);
433         let node_cfgs = create_node_cfgs(6, &chanmon_cfgs);
434
435         *node_cfgs[1].override_init_features.borrow_mut() = Some(features);
436
437         let node_chanmgrs = create_node_chanmgrs(
438                 6, &node_cfgs, &[None, Some(accept_forward_cfg), None, None, None, None]
439         );
440         let nodes = create_network(6, &node_cfgs, &node_chanmgrs);
441
442         create_unannounced_chan_between_nodes_with_value(&nodes, 0, 1, 10_000_000, 1_000_000_000);
443         create_unannounced_chan_between_nodes_with_value(&nodes, 2, 3, 10_000_000, 1_000_000_000);
444         create_announced_chan_between_nodes_with_value(&nodes, 1, 2, 10_000_000, 1_000_000_000);
445         create_announced_chan_between_nodes_with_value(&nodes, 1, 4, 10_000_000, 1_000_000_000);
446         create_announced_chan_between_nodes_with_value(&nodes, 1, 5, 10_000_000, 1_000_000_000);
447         create_announced_chan_between_nodes_with_value(&nodes, 2, 4, 10_000_000, 1_000_000_000);
448         create_announced_chan_between_nodes_with_value(&nodes, 2, 5, 10_000_000, 1_000_000_000);
449
450         let (alice, bob, charlie, david) = (&nodes[0], &nodes[1], &nodes[2], &nodes[3]);
451         let alice_id = alice.node.get_our_node_id();
452         let bob_id = bob.node.get_our_node_id();
453         let charlie_id = charlie.node.get_our_node_id();
454         let david_id = david.node.get_our_node_id();
455
456         disconnect_peers(alice, &[charlie, david, &nodes[4], &nodes[5]]);
457         disconnect_peers(david, &[bob, &nodes[4], &nodes[5]]);
458
459         let absolute_expiry = Duration::from_secs(u64::MAX);
460         let payment_id = PaymentId([1; 32]);
461         let refund = david.node
462                 .create_refund_builder(
463                         "refund".to_string(), 10_000_000, absolute_expiry, payment_id, Retry::Attempts(0), None
464                 )
465                 .unwrap()
466                 .build().unwrap();
467         assert_eq!(refund.amount_msats(), 10_000_000);
468         assert_eq!(refund.absolute_expiry(), Some(absolute_expiry));
469         assert_ne!(refund.payer_id(), david_id);
470         assert!(!refund.paths().is_empty());
471         for path in refund.paths() {
472                 assert_eq!(path.introduction_node, IntroductionNode::NodeId(charlie_id));
473         }
474         expect_recent_payment!(david, RecentPaymentDetails::AwaitingInvoice, payment_id);
475
476         let expected_invoice = alice.node.request_refund_payment(&refund).unwrap();
477
478         connect_peers(alice, charlie);
479
480         let onion_message = alice.onion_messenger.next_onion_message_for_peer(charlie_id).unwrap();
481         charlie.onion_messenger.handle_onion_message(&alice_id, &onion_message);
482
483         let onion_message = charlie.onion_messenger.next_onion_message_for_peer(david_id).unwrap();
484         david.onion_messenger.handle_onion_message(&charlie_id, &onion_message);
485
486         let invoice = extract_invoice(david, &onion_message);
487         assert_eq!(invoice, expected_invoice);
488
489         assert_eq!(invoice.amount_msats(), 10_000_000);
490         assert_ne!(invoice.signing_pubkey(), alice_id);
491         assert!(!invoice.payment_paths().is_empty());
492         for (_, path) in invoice.payment_paths() {
493                 assert_eq!(path.introduction_node, IntroductionNode::NodeId(bob_id));
494         }
495
496         route_bolt12_payment(david, &[charlie, bob, alice], &invoice);
497         expect_recent_payment!(david, RecentPaymentDetails::Pending, payment_id);
498
499         claim_bolt12_payment(david, &[charlie, bob, alice]);
500         expect_recent_payment!(david, RecentPaymentDetails::Fulfilled, payment_id);
501 }
502
503 /// Checks that an offer can be paid through a one-hop blinded path and that ephemeral pubkeys are
504 /// used rather than exposing a node's pubkey. However, the node's pubkey is still used as the
505 /// introduction node of the blinded path.
506 #[test]
507 fn creates_and_pays_for_offer_using_one_hop_blinded_path() {
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 offer = alice.node
521                 .create_offer_builder("coffee".to_string()).unwrap()
522                 .amount_msats(10_000_000)
523                 .build().unwrap();
524         assert_ne!(offer.signing_pubkey(), alice_id);
525         assert!(!offer.paths().is_empty());
526         for path in offer.paths() {
527                 assert_eq!(path.introduction_node, IntroductionNode::NodeId(alice_id));
528         }
529
530         let payment_id = PaymentId([1; 32]);
531         bob.node.pay_for_offer(&offer, None, None, None, payment_id, Retry::Attempts(0), None).unwrap();
532         expect_recent_payment!(bob, RecentPaymentDetails::AwaitingInvoice, payment_id);
533
534         let onion_message = bob.onion_messenger.next_onion_message_for_peer(alice_id).unwrap();
535         alice.onion_messenger.handle_onion_message(&bob_id, &onion_message);
536
537         let (invoice_request, reply_path) = extract_invoice_request(alice, &onion_message);
538         assert_eq!(invoice_request.amount_msats(), None);
539         assert_ne!(invoice_request.payer_id(), bob_id);
540         assert_eq!(reply_path.unwrap().introduction_node, IntroductionNode::NodeId(bob_id));
541
542         let onion_message = alice.onion_messenger.next_onion_message_for_peer(bob_id).unwrap();
543         bob.onion_messenger.handle_onion_message(&alice_id, &onion_message);
544
545         let invoice = extract_invoice(bob, &onion_message);
546         assert_eq!(invoice.amount_msats(), 10_000_000);
547         assert_ne!(invoice.signing_pubkey(), alice_id);
548         assert!(!invoice.payment_paths().is_empty());
549         for (_, path) in invoice.payment_paths() {
550                 assert_eq!(path.introduction_node, IntroductionNode::NodeId(alice_id));
551         }
552
553         route_bolt12_payment(bob, &[alice], &invoice);
554         expect_recent_payment!(bob, RecentPaymentDetails::Pending, payment_id);
555
556         claim_bolt12_payment(bob, &[alice]);
557         expect_recent_payment!(bob, RecentPaymentDetails::Fulfilled, payment_id);
558 }
559
560 /// Checks that a refund can be paid through a one-hop blinded path and that ephemeral pubkeys are
561 /// used rather than exposing a node's pubkey. However, the node's pubkey is still used as the
562 /// introduction node of the blinded path.
563 #[test]
564 fn creates_and_pays_for_refund_using_one_hop_blinded_path() {
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_announced_chan_between_nodes_with_value(&nodes, 0, 1, 10_000_000, 1_000_000_000);
571
572         let alice = &nodes[0];
573         let alice_id = alice.node.get_our_node_id();
574         let bob = &nodes[1];
575         let bob_id = bob.node.get_our_node_id();
576
577         let absolute_expiry = Duration::from_secs(u64::MAX);
578         let payment_id = PaymentId([1; 32]);
579         let refund = bob.node
580                 .create_refund_builder(
581                         "refund".to_string(), 10_000_000, absolute_expiry, payment_id, Retry::Attempts(0), None
582                 )
583                 .unwrap()
584                 .build().unwrap();
585         assert_eq!(refund.amount_msats(), 10_000_000);
586         assert_eq!(refund.absolute_expiry(), Some(absolute_expiry));
587         assert_ne!(refund.payer_id(), bob_id);
588         assert!(!refund.paths().is_empty());
589         for path in refund.paths() {
590                 assert_eq!(path.introduction_node, IntroductionNode::NodeId(bob_id));
591         }
592         expect_recent_payment!(bob, RecentPaymentDetails::AwaitingInvoice, payment_id);
593
594         let expected_invoice = alice.node.request_refund_payment(&refund).unwrap();
595
596         let onion_message = alice.onion_messenger.next_onion_message_for_peer(bob_id).unwrap();
597         bob.onion_messenger.handle_onion_message(&alice_id, &onion_message);
598
599         let invoice = extract_invoice(bob, &onion_message);
600         assert_eq!(invoice, expected_invoice);
601
602         assert_eq!(invoice.amount_msats(), 10_000_000);
603         assert_ne!(invoice.signing_pubkey(), alice_id);
604         assert!(!invoice.payment_paths().is_empty());
605         for (_, path) in invoice.payment_paths() {
606                 assert_eq!(path.introduction_node, IntroductionNode::NodeId(alice_id));
607         }
608
609         route_bolt12_payment(bob, &[alice], &invoice);
610         expect_recent_payment!(bob, RecentPaymentDetails::Pending, payment_id);
611
612         claim_bolt12_payment(bob, &[alice]);
613         expect_recent_payment!(bob, RecentPaymentDetails::Fulfilled, payment_id);
614 }
615
616 /// Checks that an invoice for an offer without any blinded paths can be requested. Note that while
617 /// the requested is sent directly using the node's pubkey, the response and the payment still use
618 /// blinded paths as required by the spec.
619 #[test]
620 fn pays_for_offer_without_blinded_paths() {
621         let chanmon_cfgs = create_chanmon_cfgs(2);
622         let node_cfgs = create_node_cfgs(2, &chanmon_cfgs);
623         let node_chanmgrs = create_node_chanmgrs(2, &node_cfgs, &[None, None]);
624         let nodes = create_network(2, &node_cfgs, &node_chanmgrs);
625
626         create_announced_chan_between_nodes_with_value(&nodes, 0, 1, 10_000_000, 1_000_000_000);
627
628         let alice = &nodes[0];
629         let alice_id = alice.node.get_our_node_id();
630         let bob = &nodes[1];
631         let bob_id = bob.node.get_our_node_id();
632
633         let offer = alice.node
634                 .create_offer_builder("coffee".to_string()).unwrap()
635                 .clear_paths()
636                 .amount_msats(10_000_000)
637                 .build().unwrap();
638         assert_eq!(offer.signing_pubkey(), alice_id);
639         assert!(offer.paths().is_empty());
640
641         let payment_id = PaymentId([1; 32]);
642         bob.node.pay_for_offer(&offer, None, None, None, payment_id, Retry::Attempts(0), None).unwrap();
643         expect_recent_payment!(bob, RecentPaymentDetails::AwaitingInvoice, payment_id);
644
645         let onion_message = bob.onion_messenger.next_onion_message_for_peer(alice_id).unwrap();
646         alice.onion_messenger.handle_onion_message(&bob_id, &onion_message);
647
648         let onion_message = alice.onion_messenger.next_onion_message_for_peer(bob_id).unwrap();
649         bob.onion_messenger.handle_onion_message(&alice_id, &onion_message);
650
651         let invoice = extract_invoice(bob, &onion_message);
652         route_bolt12_payment(bob, &[alice], &invoice);
653         expect_recent_payment!(bob, RecentPaymentDetails::Pending, payment_id);
654
655         claim_bolt12_payment(bob, &[alice]);
656         expect_recent_payment!(bob, RecentPaymentDetails::Fulfilled, payment_id);
657 }
658
659 /// Checks that a refund without any blinded paths can be paid. Note that while the invoice is sent
660 /// directly using the node's pubkey, the payment still use blinded paths as required by the spec.
661 #[test]
662 fn pays_for_refund_without_blinded_paths() {
663         let chanmon_cfgs = create_chanmon_cfgs(2);
664         let node_cfgs = create_node_cfgs(2, &chanmon_cfgs);
665         let node_chanmgrs = create_node_chanmgrs(2, &node_cfgs, &[None, None]);
666         let nodes = create_network(2, &node_cfgs, &node_chanmgrs);
667
668         create_announced_chan_between_nodes_with_value(&nodes, 0, 1, 10_000_000, 1_000_000_000);
669
670         let alice = &nodes[0];
671         let alice_id = alice.node.get_our_node_id();
672         let bob = &nodes[1];
673         let bob_id = bob.node.get_our_node_id();
674
675         let absolute_expiry = Duration::from_secs(u64::MAX);
676         let payment_id = PaymentId([1; 32]);
677         let refund = bob.node
678                 .create_refund_builder(
679                         "refund".to_string(), 10_000_000, absolute_expiry, payment_id, Retry::Attempts(0), None
680                 )
681                 .unwrap()
682                 .clear_paths()
683                 .build().unwrap();
684         assert_eq!(refund.payer_id(), bob_id);
685         assert!(refund.paths().is_empty());
686         expect_recent_payment!(bob, RecentPaymentDetails::AwaitingInvoice, payment_id);
687
688         let expected_invoice = alice.node.request_refund_payment(&refund).unwrap();
689
690         let onion_message = alice.onion_messenger.next_onion_message_for_peer(bob_id).unwrap();
691         bob.onion_messenger.handle_onion_message(&alice_id, &onion_message);
692
693         let invoice = extract_invoice(bob, &onion_message);
694         assert_eq!(invoice, expected_invoice);
695
696         route_bolt12_payment(bob, &[alice], &invoice);
697         expect_recent_payment!(bob, RecentPaymentDetails::Pending, payment_id);
698
699         claim_bolt12_payment(bob, &[alice]);
700         expect_recent_payment!(bob, RecentPaymentDetails::Fulfilled, payment_id);
701 }
702
703 /// Fails creating an offer when a blinded path cannot be created without exposing the node's id.
704 #[test]
705 fn fails_creating_offer_without_blinded_paths() {
706         let chanmon_cfgs = create_chanmon_cfgs(2);
707         let node_cfgs = create_node_cfgs(2, &chanmon_cfgs);
708         let node_chanmgrs = create_node_chanmgrs(2, &node_cfgs, &[None, None]);
709         let nodes = create_network(2, &node_cfgs, &node_chanmgrs);
710
711         create_unannounced_chan_between_nodes_with_value(&nodes, 0, 1, 10_000_000, 1_000_000_000);
712
713         match nodes[0].node.create_offer_builder("coffee".to_string()) {
714                 Ok(_) => panic!("Expected error"),
715                 Err(e) => assert_eq!(e, Bolt12SemanticError::MissingPaths),
716         }
717 }
718
719 /// Fails creating a refund when a blinded path cannot be created without exposing the node's id.
720 #[test]
721 fn fails_creating_refund_without_blinded_paths() {
722         let chanmon_cfgs = create_chanmon_cfgs(2);
723         let node_cfgs = create_node_cfgs(2, &chanmon_cfgs);
724         let node_chanmgrs = create_node_chanmgrs(2, &node_cfgs, &[None, None]);
725         let nodes = create_network(2, &node_cfgs, &node_chanmgrs);
726
727         create_unannounced_chan_between_nodes_with_value(&nodes, 0, 1, 10_000_000, 1_000_000_000);
728
729         let absolute_expiry = Duration::from_secs(u64::MAX);
730         let payment_id = PaymentId([1; 32]);
731
732         match nodes[0].node.create_refund_builder(
733                 "refund".to_string(), 10_000, absolute_expiry, payment_id, Retry::Attempts(0), None
734         ) {
735                 Ok(_) => panic!("Expected error"),
736                 Err(e) => assert_eq!(e, Bolt12SemanticError::MissingPaths),
737         }
738
739         assert!(nodes[0].node.list_recent_payments().is_empty());
740 }
741
742 /// Fails creating an invoice request when the offer contains an unsupported chain.
743 #[test]
744 fn fails_creating_invoice_request_for_unsupported_chain() {
745         let chanmon_cfgs = create_chanmon_cfgs(2);
746         let node_cfgs = create_node_cfgs(2, &chanmon_cfgs);
747         let node_chanmgrs = create_node_chanmgrs(2, &node_cfgs, &[None, None]);
748         let nodes = create_network(2, &node_cfgs, &node_chanmgrs);
749
750         create_announced_chan_between_nodes_with_value(&nodes, 0, 1, 10_000_000, 1_000_000_000);
751
752         let alice = &nodes[0];
753         let bob = &nodes[1];
754
755         let offer = alice.node
756                 .create_offer_builder("coffee".to_string()).unwrap()
757                 .clear_chains()
758                 .chain(Network::Signet)
759                 .build().unwrap();
760
761         let payment_id = PaymentId([1; 32]);
762         match bob.node.pay_for_offer(&offer, None, None, None, payment_id, Retry::Attempts(0), None) {
763                 Ok(_) => panic!("Expected error"),
764                 Err(e) => assert_eq!(e, Bolt12SemanticError::UnsupportedChain),
765         }
766 }
767
768 /// Fails requesting a payment when the refund contains an unsupported chain.
769 #[test]
770 fn fails_sending_invoice_with_unsupported_chain_for_refund() {
771         let chanmon_cfgs = create_chanmon_cfgs(2);
772         let node_cfgs = create_node_cfgs(2, &chanmon_cfgs);
773         let node_chanmgrs = create_node_chanmgrs(2, &node_cfgs, &[None, None]);
774         let nodes = create_network(2, &node_cfgs, &node_chanmgrs);
775
776         create_announced_chan_between_nodes_with_value(&nodes, 0, 1, 10_000_000, 1_000_000_000);
777
778         let alice = &nodes[0];
779         let bob = &nodes[1];
780
781         let absolute_expiry = Duration::from_secs(u64::MAX);
782         let payment_id = PaymentId([1; 32]);
783         let refund = bob.node
784                 .create_refund_builder(
785                         "refund".to_string(), 10_000_000, absolute_expiry, payment_id, Retry::Attempts(0), None
786                 )
787                 .unwrap()
788                 .chain(Network::Signet)
789                 .build().unwrap();
790
791         match alice.node.request_refund_payment(&refund) {
792                 Ok(_) => panic!("Expected error"),
793                 Err(e) => assert_eq!(e, Bolt12SemanticError::UnsupportedChain),
794         }
795 }
796
797 /// Fails creating an invoice request when a blinded reply path cannot be created without exposing
798 /// the node's id.
799 #[test]
800 fn fails_creating_invoice_request_without_blinded_reply_path() {
801         let chanmon_cfgs = create_chanmon_cfgs(6);
802         let node_cfgs = create_node_cfgs(6, &chanmon_cfgs);
803         let node_chanmgrs = create_node_chanmgrs(6, &node_cfgs, &[None, None, None, None, None, None]);
804         let nodes = create_network(6, &node_cfgs, &node_chanmgrs);
805
806         create_unannounced_chan_between_nodes_with_value(&nodes, 0, 1, 10_000_000, 1_000_000_000);
807         create_unannounced_chan_between_nodes_with_value(&nodes, 2, 3, 10_000_000, 1_000_000_000);
808         create_announced_chan_between_nodes_with_value(&nodes, 1, 2, 10_000_000, 1_000_000_000);
809         create_announced_chan_between_nodes_with_value(&nodes, 1, 4, 10_000_000, 1_000_000_000);
810         create_announced_chan_between_nodes_with_value(&nodes, 1, 5, 10_000_000, 1_000_000_000);
811
812         let (alice, bob, charlie, david) = (&nodes[0], &nodes[1], &nodes[2], &nodes[3]);
813
814         disconnect_peers(alice, &[charlie, david, &nodes[4], &nodes[5]]);
815         disconnect_peers(david, &[bob, &nodes[4], &nodes[5]]);
816
817         let offer = alice.node
818                 .create_offer_builder("coffee".to_string()).unwrap()
819                 .amount_msats(10_000_000)
820                 .build().unwrap();
821
822         let payment_id = PaymentId([1; 32]);
823
824         match david.node.pay_for_offer(&offer, None, None, None, payment_id, Retry::Attempts(0), None) {
825                 Ok(_) => panic!("Expected error"),
826                 Err(e) => assert_eq!(e, Bolt12SemanticError::MissingPaths),
827         }
828
829         assert!(nodes[0].node.list_recent_payments().is_empty());
830 }
831
832 #[test]
833 fn fails_creating_invoice_request_with_duplicate_payment_id() {
834         let chanmon_cfgs = create_chanmon_cfgs(6);
835         let node_cfgs = create_node_cfgs(6, &chanmon_cfgs);
836         let node_chanmgrs = create_node_chanmgrs(6, &node_cfgs, &[None, None, None, None, None, None]);
837         let nodes = create_network(6, &node_cfgs, &node_chanmgrs);
838
839         create_unannounced_chan_between_nodes_with_value(&nodes, 0, 1, 10_000_000, 1_000_000_000);
840         create_unannounced_chan_between_nodes_with_value(&nodes, 2, 3, 10_000_000, 1_000_000_000);
841         create_announced_chan_between_nodes_with_value(&nodes, 1, 2, 10_000_000, 1_000_000_000);
842         create_announced_chan_between_nodes_with_value(&nodes, 1, 4, 10_000_000, 1_000_000_000);
843         create_announced_chan_between_nodes_with_value(&nodes, 1, 5, 10_000_000, 1_000_000_000);
844         create_announced_chan_between_nodes_with_value(&nodes, 2, 4, 10_000_000, 1_000_000_000);
845         create_announced_chan_between_nodes_with_value(&nodes, 2, 5, 10_000_000, 1_000_000_000);
846
847         let (alice, _bob, charlie, david) = (&nodes[0], &nodes[1], &nodes[2], &nodes[3]);
848
849         disconnect_peers(alice, &[charlie, david, &nodes[4], &nodes[5]]);
850
851         let offer = alice.node
852                 .create_offer_builder("coffee".to_string()).unwrap()
853                 .amount_msats(10_000_000)
854                 .build().unwrap();
855
856         let payment_id = PaymentId([1; 32]);
857         assert!(
858                 david.node.pay_for_offer(
859                         &offer, None, None, None, payment_id, Retry::Attempts(0), None
860                 ).is_ok()
861         );
862         expect_recent_payment!(david, RecentPaymentDetails::AwaitingInvoice, payment_id);
863
864         match david.node.pay_for_offer(&offer, None, None, None, payment_id, Retry::Attempts(0), None) {
865                 Ok(_) => panic!("Expected error"),
866                 Err(e) => assert_eq!(e, Bolt12SemanticError::DuplicatePaymentId),
867         }
868
869         expect_recent_payment!(david, RecentPaymentDetails::AwaitingInvoice, payment_id);
870 }
871
872 #[test]
873 fn fails_creating_refund_with_duplicate_payment_id() {
874         let chanmon_cfgs = create_chanmon_cfgs(2);
875         let node_cfgs = create_node_cfgs(2, &chanmon_cfgs);
876         let node_chanmgrs = create_node_chanmgrs(2, &node_cfgs, &[None, None]);
877         let nodes = create_network(2, &node_cfgs, &node_chanmgrs);
878
879         create_announced_chan_between_nodes_with_value(&nodes, 0, 1, 10_000_000, 1_000_000_000);
880
881         let absolute_expiry = Duration::from_secs(u64::MAX);
882         let payment_id = PaymentId([1; 32]);
883         assert!(
884                 nodes[0].node.create_refund_builder(
885                         "refund".to_string(), 10_000, absolute_expiry, payment_id, Retry::Attempts(0), None
886                 ).is_ok()
887         );
888         expect_recent_payment!(nodes[0], RecentPaymentDetails::AwaitingInvoice, payment_id);
889
890         match nodes[0].node.create_refund_builder(
891                 "refund".to_string(), 10_000, absolute_expiry, payment_id, Retry::Attempts(0), None
892         ) {
893                 Ok(_) => panic!("Expected error"),
894                 Err(e) => assert_eq!(e, Bolt12SemanticError::DuplicatePaymentId),
895         }
896
897         expect_recent_payment!(nodes[0], RecentPaymentDetails::AwaitingInvoice, payment_id);
898 }
899
900 #[test]
901 fn fails_sending_invoice_without_blinded_payment_paths_for_offer() {
902         let mut accept_forward_cfg = test_default_channel_config();
903         accept_forward_cfg.accept_forwards_to_priv_channels = true;
904
905         // Clearing route_blinding prevents forming any payment paths since the node is unannounced.
906         let mut features = channelmanager::provided_init_features(&accept_forward_cfg);
907         features.set_onion_messages_optional();
908         features.clear_route_blinding();
909
910         let chanmon_cfgs = create_chanmon_cfgs(6);
911         let node_cfgs = create_node_cfgs(6, &chanmon_cfgs);
912
913         *node_cfgs[1].override_init_features.borrow_mut() = Some(features);
914
915         let node_chanmgrs = create_node_chanmgrs(
916                 6, &node_cfgs, &[None, Some(accept_forward_cfg), None, None, None, None]
917         );
918         let nodes = create_network(6, &node_cfgs, &node_chanmgrs);
919
920         create_unannounced_chan_between_nodes_with_value(&nodes, 0, 1, 10_000_000, 1_000_000_000);
921         create_unannounced_chan_between_nodes_with_value(&nodes, 2, 3, 10_000_000, 1_000_000_000);
922         create_announced_chan_between_nodes_with_value(&nodes, 1, 2, 10_000_000, 1_000_000_000);
923         create_announced_chan_between_nodes_with_value(&nodes, 1, 4, 10_000_000, 1_000_000_000);
924         create_announced_chan_between_nodes_with_value(&nodes, 1, 5, 10_000_000, 1_000_000_000);
925         create_announced_chan_between_nodes_with_value(&nodes, 2, 4, 10_000_000, 1_000_000_000);
926         create_announced_chan_between_nodes_with_value(&nodes, 2, 5, 10_000_000, 1_000_000_000);
927
928         let (alice, bob, charlie, david) = (&nodes[0], &nodes[1], &nodes[2], &nodes[3]);
929         let alice_id = alice.node.get_our_node_id();
930         let bob_id = bob.node.get_our_node_id();
931         let charlie_id = charlie.node.get_our_node_id();
932         let david_id = david.node.get_our_node_id();
933
934         disconnect_peers(alice, &[charlie, david, &nodes[4], &nodes[5]]);
935         disconnect_peers(david, &[bob, &nodes[4], &nodes[5]]);
936
937         let offer = alice.node
938                 .create_offer_builder("coffee".to_string()).unwrap()
939                 .amount_msats(10_000_000)
940                 .build().unwrap();
941
942         let payment_id = PaymentId([1; 32]);
943         david.node.pay_for_offer(&offer, None, None, None, payment_id, Retry::Attempts(0), None)
944                 .unwrap();
945
946         connect_peers(david, bob);
947
948         let onion_message = david.onion_messenger.next_onion_message_for_peer(bob_id).unwrap();
949         bob.onion_messenger.handle_onion_message(&david_id, &onion_message);
950
951         connect_peers(alice, charlie);
952
953         let onion_message = bob.onion_messenger.next_onion_message_for_peer(alice_id).unwrap();
954         alice.onion_messenger.handle_onion_message(&bob_id, &onion_message);
955
956         let onion_message = alice.onion_messenger.next_onion_message_for_peer(charlie_id).unwrap();
957         charlie.onion_messenger.handle_onion_message(&alice_id, &onion_message);
958
959         let onion_message = charlie.onion_messenger.next_onion_message_for_peer(david_id).unwrap();
960         david.onion_messenger.handle_onion_message(&charlie_id, &onion_message);
961
962         let invoice_error = extract_invoice_error(david, &onion_message);
963         assert_eq!(invoice_error, InvoiceError::from(Bolt12SemanticError::MissingPaths));
964 }
965
966 #[test]
967 fn fails_sending_invoice_without_blinded_payment_paths_for_refund() {
968         let mut accept_forward_cfg = test_default_channel_config();
969         accept_forward_cfg.accept_forwards_to_priv_channels = true;
970
971         // Clearing route_blinding prevents forming any payment paths since the node is unannounced.
972         let mut features = channelmanager::provided_init_features(&accept_forward_cfg);
973         features.set_onion_messages_optional();
974         features.clear_route_blinding();
975
976         let chanmon_cfgs = create_chanmon_cfgs(6);
977         let node_cfgs = create_node_cfgs(6, &chanmon_cfgs);
978
979         *node_cfgs[1].override_init_features.borrow_mut() = Some(features);
980
981         let node_chanmgrs = create_node_chanmgrs(
982                 6, &node_cfgs, &[None, Some(accept_forward_cfg), None, None, None, None]
983         );
984         let nodes = create_network(6, &node_cfgs, &node_chanmgrs);
985
986         create_unannounced_chan_between_nodes_with_value(&nodes, 0, 1, 10_000_000, 1_000_000_000);
987         create_unannounced_chan_between_nodes_with_value(&nodes, 2, 3, 10_000_000, 1_000_000_000);
988         create_announced_chan_between_nodes_with_value(&nodes, 1, 2, 10_000_000, 1_000_000_000);
989         create_announced_chan_between_nodes_with_value(&nodes, 1, 4, 10_000_000, 1_000_000_000);
990         create_announced_chan_between_nodes_with_value(&nodes, 1, 5, 10_000_000, 1_000_000_000);
991         create_announced_chan_between_nodes_with_value(&nodes, 2, 4, 10_000_000, 1_000_000_000);
992         create_announced_chan_between_nodes_with_value(&nodes, 2, 5, 10_000_000, 1_000_000_000);
993
994         let (alice, bob, charlie, david) = (&nodes[0], &nodes[1], &nodes[2], &nodes[3]);
995
996         disconnect_peers(alice, &[charlie, david, &nodes[4], &nodes[5]]);
997         disconnect_peers(david, &[bob, &nodes[4], &nodes[5]]);
998
999         let absolute_expiry = Duration::from_secs(u64::MAX);
1000         let payment_id = PaymentId([1; 32]);
1001         let refund = david.node
1002                 .create_refund_builder(
1003                         "refund".to_string(), 10_000_000, absolute_expiry, payment_id, Retry::Attempts(0), None
1004                 )
1005                 .unwrap()
1006                 .build().unwrap();
1007
1008         match alice.node.request_refund_payment(&refund) {
1009                 Ok(_) => panic!("Expected error"),
1010                 Err(e) => assert_eq!(e, Bolt12SemanticError::MissingPaths),
1011         }
1012 }
1013
1014 #[test]
1015 fn fails_paying_invoice_more_than_once() {
1016         let mut accept_forward_cfg = test_default_channel_config();
1017         accept_forward_cfg.accept_forwards_to_priv_channels = true;
1018
1019         let mut features = channelmanager::provided_init_features(&accept_forward_cfg);
1020         features.set_onion_messages_optional();
1021         features.set_route_blinding_optional();
1022
1023         let chanmon_cfgs = create_chanmon_cfgs(6);
1024         let node_cfgs = create_node_cfgs(6, &chanmon_cfgs);
1025
1026         *node_cfgs[1].override_init_features.borrow_mut() = Some(features);
1027
1028         let node_chanmgrs = create_node_chanmgrs(
1029                 6, &node_cfgs, &[None, Some(accept_forward_cfg), None, None, None, None]
1030         );
1031         let nodes = create_network(6, &node_cfgs, &node_chanmgrs);
1032
1033         create_unannounced_chan_between_nodes_with_value(&nodes, 0, 1, 10_000_000, 1_000_000_000);
1034         create_unannounced_chan_between_nodes_with_value(&nodes, 2, 3, 10_000_000, 1_000_000_000);
1035         create_announced_chan_between_nodes_with_value(&nodes, 1, 2, 10_000_000, 1_000_000_000);
1036         create_announced_chan_between_nodes_with_value(&nodes, 1, 4, 10_000_000, 1_000_000_000);
1037         create_announced_chan_between_nodes_with_value(&nodes, 1, 5, 10_000_000, 1_000_000_000);
1038         create_announced_chan_between_nodes_with_value(&nodes, 2, 4, 10_000_000, 1_000_000_000);
1039         create_announced_chan_between_nodes_with_value(&nodes, 2, 5, 10_000_000, 1_000_000_000);
1040
1041         let (alice, bob, charlie, david) = (&nodes[0], &nodes[1], &nodes[2], &nodes[3]);
1042         let alice_id = alice.node.get_our_node_id();
1043         let bob_id = bob.node.get_our_node_id();
1044         let charlie_id = charlie.node.get_our_node_id();
1045         let david_id = david.node.get_our_node_id();
1046
1047         disconnect_peers(alice, &[charlie, david, &nodes[4], &nodes[5]]);
1048         disconnect_peers(david, &[bob, &nodes[4], &nodes[5]]);
1049
1050         let absolute_expiry = Duration::from_secs(u64::MAX);
1051         let payment_id = PaymentId([1; 32]);
1052         let refund = david.node
1053                 .create_refund_builder(
1054                         "refund".to_string(), 10_000_000, absolute_expiry, payment_id, Retry::Attempts(0), None
1055                 )
1056                 .unwrap()
1057                 .build().unwrap();
1058         expect_recent_payment!(david, RecentPaymentDetails::AwaitingInvoice, payment_id);
1059
1060         // Alice sends the first invoice
1061         alice.node.request_refund_payment(&refund).unwrap();
1062
1063         connect_peers(alice, charlie);
1064
1065         let onion_message = alice.onion_messenger.next_onion_message_for_peer(charlie_id).unwrap();
1066         charlie.onion_messenger.handle_onion_message(&alice_id, &onion_message);
1067
1068         let onion_message = charlie.onion_messenger.next_onion_message_for_peer(david_id).unwrap();
1069         david.onion_messenger.handle_onion_message(&charlie_id, &onion_message);
1070
1071         // David pays the first invoice
1072         let invoice1 = extract_invoice(david, &onion_message);
1073
1074         route_bolt12_payment(david, &[charlie, bob, alice], &invoice1);
1075         expect_recent_payment!(david, RecentPaymentDetails::Pending, payment_id);
1076
1077         claim_bolt12_payment(david, &[charlie, bob, alice]);
1078         expect_recent_payment!(david, RecentPaymentDetails::Fulfilled, payment_id);
1079
1080         disconnect_peers(alice, &[charlie]);
1081
1082         // Alice sends the second invoice
1083         alice.node.request_refund_payment(&refund).unwrap();
1084
1085         connect_peers(alice, charlie);
1086         connect_peers(david, bob);
1087
1088         let onion_message = alice.onion_messenger.next_onion_message_for_peer(charlie_id).unwrap();
1089         charlie.onion_messenger.handle_onion_message(&alice_id, &onion_message);
1090
1091         let onion_message = charlie.onion_messenger.next_onion_message_for_peer(david_id).unwrap();
1092         david.onion_messenger.handle_onion_message(&charlie_id, &onion_message);
1093
1094         let invoice2 = extract_invoice(david, &onion_message);
1095         assert_eq!(invoice1.payer_metadata(), invoice2.payer_metadata());
1096
1097         // David sends an error instead of paying the second invoice
1098         let onion_message = david.onion_messenger.next_onion_message_for_peer(bob_id).unwrap();
1099         bob.onion_messenger.handle_onion_message(&david_id, &onion_message);
1100
1101         let onion_message = bob.onion_messenger.next_onion_message_for_peer(alice_id).unwrap();
1102         alice.onion_messenger.handle_onion_message(&bob_id, &onion_message);
1103
1104         let invoice_error = extract_invoice_error(alice, &onion_message);
1105         assert_eq!(invoice_error, InvoiceError::from_string("DuplicateInvoice".to_string()));
1106 }