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