Generalize BlindedPath::introduction_node_id field
[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::InvoicePayment {
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         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.amount_msats(), 10_000_000);
488         assert_ne!(invoice.signing_pubkey(), alice_id);
489         assert!(!invoice.payment_paths().is_empty());
490         for (_, path) in invoice.payment_paths() {
491                 assert_eq!(path.introduction_node, IntroductionNode::NodeId(bob_id));
492         }
493
494         route_bolt12_payment(david, &[charlie, bob, alice], &invoice);
495         expect_recent_payment!(david, RecentPaymentDetails::Pending, payment_id);
496
497         claim_bolt12_payment(david, &[charlie, bob, alice]);
498         expect_recent_payment!(david, RecentPaymentDetails::Fulfilled, payment_id);
499 }
500
501 /// Checks that an offer can be paid through a one-hop blinded path and that ephemeral pubkeys are
502 /// used rather than exposing a node's pubkey. However, the node's pubkey is still used as the
503 /// introduction node of the blinded path.
504 #[test]
505 fn creates_and_pays_for_offer_using_one_hop_blinded_path() {
506         let chanmon_cfgs = create_chanmon_cfgs(2);
507         let node_cfgs = create_node_cfgs(2, &chanmon_cfgs);
508         let node_chanmgrs = create_node_chanmgrs(2, &node_cfgs, &[None, None]);
509         let nodes = create_network(2, &node_cfgs, &node_chanmgrs);
510
511         create_announced_chan_between_nodes_with_value(&nodes, 0, 1, 10_000_000, 1_000_000_000);
512
513         let alice = &nodes[0];
514         let alice_id = alice.node.get_our_node_id();
515         let bob = &nodes[1];
516         let bob_id = bob.node.get_our_node_id();
517
518         let offer = alice.node
519                 .create_offer_builder("coffee".to_string()).unwrap()
520                 .amount_msats(10_000_000)
521                 .build().unwrap();
522         assert_ne!(offer.signing_pubkey(), alice_id);
523         assert!(!offer.paths().is_empty());
524         for path in offer.paths() {
525                 assert_eq!(path.introduction_node, IntroductionNode::NodeId(alice_id));
526         }
527
528         let payment_id = PaymentId([1; 32]);
529         bob.node.pay_for_offer(&offer, None, None, None, payment_id, Retry::Attempts(0), None).unwrap();
530         expect_recent_payment!(bob, RecentPaymentDetails::AwaitingInvoice, payment_id);
531
532         let onion_message = bob.onion_messenger.next_onion_message_for_peer(alice_id).unwrap();
533         alice.onion_messenger.handle_onion_message(&bob_id, &onion_message);
534
535         let (invoice_request, reply_path) = extract_invoice_request(alice, &onion_message);
536         assert_eq!(invoice_request.amount_msats(), None);
537         assert_ne!(invoice_request.payer_id(), bob_id);
538         assert_eq!(reply_path.unwrap().introduction_node, IntroductionNode::NodeId(bob_id));
539
540         let onion_message = alice.onion_messenger.next_onion_message_for_peer(bob_id).unwrap();
541         bob.onion_messenger.handle_onion_message(&alice_id, &onion_message);
542
543         let invoice = extract_invoice(bob, &onion_message);
544         assert_eq!(invoice.amount_msats(), 10_000_000);
545         assert_ne!(invoice.signing_pubkey(), alice_id);
546         assert!(!invoice.payment_paths().is_empty());
547         for (_, path) in invoice.payment_paths() {
548                 assert_eq!(path.introduction_node, IntroductionNode::NodeId(alice_id));
549         }
550
551         route_bolt12_payment(bob, &[alice], &invoice);
552         expect_recent_payment!(bob, RecentPaymentDetails::Pending, payment_id);
553
554         claim_bolt12_payment(bob, &[alice]);
555         expect_recent_payment!(bob, RecentPaymentDetails::Fulfilled, payment_id);
556 }
557
558 /// Checks that a refund can be paid through a one-hop blinded path and that ephemeral pubkeys are
559 /// used rather than exposing a node's pubkey. However, the node's pubkey is still used as the
560 /// introduction node of the blinded path.
561 #[test]
562 fn creates_and_pays_for_refund_using_one_hop_blinded_path() {
563         let chanmon_cfgs = create_chanmon_cfgs(2);
564         let node_cfgs = create_node_cfgs(2, &chanmon_cfgs);
565         let node_chanmgrs = create_node_chanmgrs(2, &node_cfgs, &[None, None]);
566         let nodes = create_network(2, &node_cfgs, &node_chanmgrs);
567
568         create_announced_chan_between_nodes_with_value(&nodes, 0, 1, 10_000_000, 1_000_000_000);
569
570         let alice = &nodes[0];
571         let alice_id = alice.node.get_our_node_id();
572         let bob = &nodes[1];
573         let bob_id = bob.node.get_our_node_id();
574
575         let absolute_expiry = Duration::from_secs(u64::MAX);
576         let payment_id = PaymentId([1; 32]);
577         let refund = bob.node
578                 .create_refund_builder(
579                         "refund".to_string(), 10_000_000, absolute_expiry, payment_id, Retry::Attempts(0), None
580                 )
581                 .unwrap()
582                 .build().unwrap();
583         assert_eq!(refund.amount_msats(), 10_000_000);
584         assert_eq!(refund.absolute_expiry(), Some(absolute_expiry));
585         assert_ne!(refund.payer_id(), bob_id);
586         assert!(!refund.paths().is_empty());
587         for path in refund.paths() {
588                 assert_eq!(path.introduction_node, IntroductionNode::NodeId(bob_id));
589         }
590         expect_recent_payment!(bob, RecentPaymentDetails::AwaitingInvoice, payment_id);
591
592         alice.node.request_refund_payment(&refund).unwrap();
593
594         let onion_message = alice.onion_messenger.next_onion_message_for_peer(bob_id).unwrap();
595         bob.onion_messenger.handle_onion_message(&alice_id, &onion_message);
596
597         let invoice = extract_invoice(bob, &onion_message);
598         assert_eq!(invoice.amount_msats(), 10_000_000);
599         assert_ne!(invoice.signing_pubkey(), alice_id);
600         assert!(!invoice.payment_paths().is_empty());
601         for (_, path) in invoice.payment_paths() {
602                 assert_eq!(path.introduction_node, IntroductionNode::NodeId(alice_id));
603         }
604
605         route_bolt12_payment(bob, &[alice], &invoice);
606         expect_recent_payment!(bob, RecentPaymentDetails::Pending, payment_id);
607
608         claim_bolt12_payment(bob, &[alice]);
609         expect_recent_payment!(bob, RecentPaymentDetails::Fulfilled, payment_id);
610 }
611
612 /// Checks that an invoice for an offer without any blinded paths can be requested. Note that while
613 /// the requested is sent directly using the node's pubkey, the response and the payment still use
614 /// blinded paths as required by the spec.
615 #[test]
616 fn pays_for_offer_without_blinded_paths() {
617         let chanmon_cfgs = create_chanmon_cfgs(2);
618         let node_cfgs = create_node_cfgs(2, &chanmon_cfgs);
619         let node_chanmgrs = create_node_chanmgrs(2, &node_cfgs, &[None, None]);
620         let nodes = create_network(2, &node_cfgs, &node_chanmgrs);
621
622         create_announced_chan_between_nodes_with_value(&nodes, 0, 1, 10_000_000, 1_000_000_000);
623
624         let alice = &nodes[0];
625         let alice_id = alice.node.get_our_node_id();
626         let bob = &nodes[1];
627         let bob_id = bob.node.get_our_node_id();
628
629         let offer = alice.node
630                 .create_offer_builder("coffee".to_string()).unwrap()
631                 .clear_paths()
632                 .amount_msats(10_000_000)
633                 .build().unwrap();
634         assert_eq!(offer.signing_pubkey(), alice_id);
635         assert!(offer.paths().is_empty());
636
637         let payment_id = PaymentId([1; 32]);
638         bob.node.pay_for_offer(&offer, None, None, None, payment_id, Retry::Attempts(0), None).unwrap();
639         expect_recent_payment!(bob, RecentPaymentDetails::AwaitingInvoice, payment_id);
640
641         let onion_message = bob.onion_messenger.next_onion_message_for_peer(alice_id).unwrap();
642         alice.onion_messenger.handle_onion_message(&bob_id, &onion_message);
643
644         let onion_message = alice.onion_messenger.next_onion_message_for_peer(bob_id).unwrap();
645         bob.onion_messenger.handle_onion_message(&alice_id, &onion_message);
646
647         let invoice = extract_invoice(bob, &onion_message);
648         route_bolt12_payment(bob, &[alice], &invoice);
649         expect_recent_payment!(bob, RecentPaymentDetails::Pending, payment_id);
650
651         claim_bolt12_payment(bob, &[alice]);
652         expect_recent_payment!(bob, RecentPaymentDetails::Fulfilled, payment_id);
653 }
654
655 /// Checks that a refund without any blinded paths can be paid. Note that while the invoice is sent
656 /// directly using the node's pubkey, the payment still use blinded paths as required by the spec.
657 #[test]
658 fn pays_for_refund_without_blinded_paths() {
659         let chanmon_cfgs = create_chanmon_cfgs(2);
660         let node_cfgs = create_node_cfgs(2, &chanmon_cfgs);
661         let node_chanmgrs = create_node_chanmgrs(2, &node_cfgs, &[None, None]);
662         let nodes = create_network(2, &node_cfgs, &node_chanmgrs);
663
664         create_announced_chan_between_nodes_with_value(&nodes, 0, 1, 10_000_000, 1_000_000_000);
665
666         let alice = &nodes[0];
667         let alice_id = alice.node.get_our_node_id();
668         let bob = &nodes[1];
669         let bob_id = bob.node.get_our_node_id();
670
671         let absolute_expiry = Duration::from_secs(u64::MAX);
672         let payment_id = PaymentId([1; 32]);
673         let refund = bob.node
674                 .create_refund_builder(
675                         "refund".to_string(), 10_000_000, absolute_expiry, payment_id, Retry::Attempts(0), None
676                 )
677                 .unwrap()
678                 .clear_paths()
679                 .build().unwrap();
680         assert_eq!(refund.payer_id(), bob_id);
681         assert!(refund.paths().is_empty());
682         expect_recent_payment!(bob, RecentPaymentDetails::AwaitingInvoice, payment_id);
683
684         alice.node.request_refund_payment(&refund).unwrap();
685
686         let onion_message = alice.onion_messenger.next_onion_message_for_peer(bob_id).unwrap();
687         bob.onion_messenger.handle_onion_message(&alice_id, &onion_message);
688
689         let invoice = extract_invoice(bob, &onion_message);
690         route_bolt12_payment(bob, &[alice], &invoice);
691         expect_recent_payment!(bob, RecentPaymentDetails::Pending, payment_id);
692
693         claim_bolt12_payment(bob, &[alice]);
694         expect_recent_payment!(bob, RecentPaymentDetails::Fulfilled, payment_id);
695 }
696
697 /// Fails creating an offer when a blinded path cannot be created without exposing the node's id.
698 #[test]
699 fn fails_creating_offer_without_blinded_paths() {
700         let chanmon_cfgs = create_chanmon_cfgs(2);
701         let node_cfgs = create_node_cfgs(2, &chanmon_cfgs);
702         let node_chanmgrs = create_node_chanmgrs(2, &node_cfgs, &[None, None]);
703         let nodes = create_network(2, &node_cfgs, &node_chanmgrs);
704
705         create_unannounced_chan_between_nodes_with_value(&nodes, 0, 1, 10_000_000, 1_000_000_000);
706
707         match nodes[0].node.create_offer_builder("coffee".to_string()) {
708                 Ok(_) => panic!("Expected error"),
709                 Err(e) => assert_eq!(e, Bolt12SemanticError::MissingPaths),
710         }
711 }
712
713 /// Fails creating a refund when a blinded path cannot be created without exposing the node's id.
714 #[test]
715 fn fails_creating_refund_without_blinded_paths() {
716         let chanmon_cfgs = create_chanmon_cfgs(2);
717         let node_cfgs = create_node_cfgs(2, &chanmon_cfgs);
718         let node_chanmgrs = create_node_chanmgrs(2, &node_cfgs, &[None, None]);
719         let nodes = create_network(2, &node_cfgs, &node_chanmgrs);
720
721         create_unannounced_chan_between_nodes_with_value(&nodes, 0, 1, 10_000_000, 1_000_000_000);
722
723         let absolute_expiry = Duration::from_secs(u64::MAX);
724         let payment_id = PaymentId([1; 32]);
725
726         match nodes[0].node.create_refund_builder(
727                 "refund".to_string(), 10_000, absolute_expiry, payment_id, Retry::Attempts(0), None
728         ) {
729                 Ok(_) => panic!("Expected error"),
730                 Err(e) => assert_eq!(e, Bolt12SemanticError::MissingPaths),
731         }
732
733         assert!(nodes[0].node.list_recent_payments().is_empty());
734 }
735
736 /// Fails creating an invoice request when the offer contains an unsupported chain.
737 #[test]
738 fn fails_creating_invoice_request_for_unsupported_chain() {
739         let chanmon_cfgs = create_chanmon_cfgs(2);
740         let node_cfgs = create_node_cfgs(2, &chanmon_cfgs);
741         let node_chanmgrs = create_node_chanmgrs(2, &node_cfgs, &[None, None]);
742         let nodes = create_network(2, &node_cfgs, &node_chanmgrs);
743
744         create_announced_chan_between_nodes_with_value(&nodes, 0, 1, 10_000_000, 1_000_000_000);
745
746         let alice = &nodes[0];
747         let bob = &nodes[1];
748
749         let offer = alice.node
750                 .create_offer_builder("coffee".to_string()).unwrap()
751                 .clear_chains()
752                 .chain(Network::Signet)
753                 .build().unwrap();
754
755         let payment_id = PaymentId([1; 32]);
756         match bob.node.pay_for_offer(&offer, None, None, None, payment_id, Retry::Attempts(0), None) {
757                 Ok(_) => panic!("Expected error"),
758                 Err(e) => assert_eq!(e, Bolt12SemanticError::UnsupportedChain),
759         }
760 }
761
762 /// Fails requesting a payment when the refund contains an unsupported chain.
763 #[test]
764 fn fails_sending_invoice_with_unsupported_chain_for_refund() {
765         let chanmon_cfgs = create_chanmon_cfgs(2);
766         let node_cfgs = create_node_cfgs(2, &chanmon_cfgs);
767         let node_chanmgrs = create_node_chanmgrs(2, &node_cfgs, &[None, None]);
768         let nodes = create_network(2, &node_cfgs, &node_chanmgrs);
769
770         create_announced_chan_between_nodes_with_value(&nodes, 0, 1, 10_000_000, 1_000_000_000);
771
772         let alice = &nodes[0];
773         let bob = &nodes[1];
774
775         let absolute_expiry = Duration::from_secs(u64::MAX);
776         let payment_id = PaymentId([1; 32]);
777         let refund = bob.node
778                 .create_refund_builder(
779                         "refund".to_string(), 10_000_000, absolute_expiry, payment_id, Retry::Attempts(0), None
780                 )
781                 .unwrap()
782                 .chain(Network::Signet)
783                 .build().unwrap();
784
785         match alice.node.request_refund_payment(&refund) {
786                 Ok(_) => panic!("Expected error"),
787                 Err(e) => assert_eq!(e, Bolt12SemanticError::UnsupportedChain),
788         }
789 }
790
791 /// Fails creating an invoice request when a blinded reply path cannot be created without exposing
792 /// the node's id.
793 #[test]
794 fn fails_creating_invoice_request_without_blinded_reply_path() {
795         let chanmon_cfgs = create_chanmon_cfgs(6);
796         let node_cfgs = create_node_cfgs(6, &chanmon_cfgs);
797         let node_chanmgrs = create_node_chanmgrs(6, &node_cfgs, &[None, None, None, None, None, None]);
798         let nodes = create_network(6, &node_cfgs, &node_chanmgrs);
799
800         create_unannounced_chan_between_nodes_with_value(&nodes, 0, 1, 10_000_000, 1_000_000_000);
801         create_unannounced_chan_between_nodes_with_value(&nodes, 2, 3, 10_000_000, 1_000_000_000);
802         create_announced_chan_between_nodes_with_value(&nodes, 1, 2, 10_000_000, 1_000_000_000);
803         create_announced_chan_between_nodes_with_value(&nodes, 1, 4, 10_000_000, 1_000_000_000);
804         create_announced_chan_between_nodes_with_value(&nodes, 1, 5, 10_000_000, 1_000_000_000);
805
806         let (alice, bob, charlie, david) = (&nodes[0], &nodes[1], &nodes[2], &nodes[3]);
807
808         disconnect_peers(alice, &[charlie, david, &nodes[4], &nodes[5]]);
809         disconnect_peers(david, &[bob, &nodes[4], &nodes[5]]);
810
811         let offer = alice.node
812                 .create_offer_builder("coffee".to_string()).unwrap()
813                 .amount_msats(10_000_000)
814                 .build().unwrap();
815
816         let payment_id = PaymentId([1; 32]);
817
818         match david.node.pay_for_offer(&offer, None, None, None, payment_id, Retry::Attempts(0), None) {
819                 Ok(_) => panic!("Expected error"),
820                 Err(e) => assert_eq!(e, Bolt12SemanticError::MissingPaths),
821         }
822
823         assert!(nodes[0].node.list_recent_payments().is_empty());
824 }
825
826 #[test]
827 fn fails_creating_invoice_request_with_duplicate_payment_id() {
828         let chanmon_cfgs = create_chanmon_cfgs(6);
829         let node_cfgs = create_node_cfgs(6, &chanmon_cfgs);
830         let node_chanmgrs = create_node_chanmgrs(6, &node_cfgs, &[None, None, None, None, None, None]);
831         let nodes = create_network(6, &node_cfgs, &node_chanmgrs);
832
833         create_unannounced_chan_between_nodes_with_value(&nodes, 0, 1, 10_000_000, 1_000_000_000);
834         create_unannounced_chan_between_nodes_with_value(&nodes, 2, 3, 10_000_000, 1_000_000_000);
835         create_announced_chan_between_nodes_with_value(&nodes, 1, 2, 10_000_000, 1_000_000_000);
836         create_announced_chan_between_nodes_with_value(&nodes, 1, 4, 10_000_000, 1_000_000_000);
837         create_announced_chan_between_nodes_with_value(&nodes, 1, 5, 10_000_000, 1_000_000_000);
838         create_announced_chan_between_nodes_with_value(&nodes, 2, 4, 10_000_000, 1_000_000_000);
839         create_announced_chan_between_nodes_with_value(&nodes, 2, 5, 10_000_000, 1_000_000_000);
840
841         let (alice, _bob, charlie, david) = (&nodes[0], &nodes[1], &nodes[2], &nodes[3]);
842
843         disconnect_peers(alice, &[charlie, david, &nodes[4], &nodes[5]]);
844
845         let offer = alice.node
846                 .create_offer_builder("coffee".to_string()).unwrap()
847                 .amount_msats(10_000_000)
848                 .build().unwrap();
849
850         let payment_id = PaymentId([1; 32]);
851         assert!(
852                 david.node.pay_for_offer(
853                         &offer, None, None, None, payment_id, Retry::Attempts(0), None
854                 ).is_ok()
855         );
856         expect_recent_payment!(david, RecentPaymentDetails::AwaitingInvoice, payment_id);
857
858         match david.node.pay_for_offer(&offer, None, None, None, payment_id, Retry::Attempts(0), None) {
859                 Ok(_) => panic!("Expected error"),
860                 Err(e) => assert_eq!(e, Bolt12SemanticError::DuplicatePaymentId),
861         }
862
863         expect_recent_payment!(david, RecentPaymentDetails::AwaitingInvoice, payment_id);
864 }
865
866 #[test]
867 fn fails_creating_refund_with_duplicate_payment_id() {
868         let chanmon_cfgs = create_chanmon_cfgs(2);
869         let node_cfgs = create_node_cfgs(2, &chanmon_cfgs);
870         let node_chanmgrs = create_node_chanmgrs(2, &node_cfgs, &[None, None]);
871         let nodes = create_network(2, &node_cfgs, &node_chanmgrs);
872
873         create_announced_chan_between_nodes_with_value(&nodes, 0, 1, 10_000_000, 1_000_000_000);
874
875         let absolute_expiry = Duration::from_secs(u64::MAX);
876         let payment_id = PaymentId([1; 32]);
877         assert!(
878                 nodes[0].node.create_refund_builder(
879                         "refund".to_string(), 10_000, absolute_expiry, payment_id, Retry::Attempts(0), None
880                 ).is_ok()
881         );
882         expect_recent_payment!(nodes[0], RecentPaymentDetails::AwaitingInvoice, payment_id);
883
884         match nodes[0].node.create_refund_builder(
885                 "refund".to_string(), 10_000, absolute_expiry, payment_id, Retry::Attempts(0), None
886         ) {
887                 Ok(_) => panic!("Expected error"),
888                 Err(e) => assert_eq!(e, Bolt12SemanticError::DuplicatePaymentId),
889         }
890
891         expect_recent_payment!(nodes[0], RecentPaymentDetails::AwaitingInvoice, payment_id);
892 }
893
894 #[test]
895 fn fails_sending_invoice_without_blinded_payment_paths_for_offer() {
896         let mut accept_forward_cfg = test_default_channel_config();
897         accept_forward_cfg.accept_forwards_to_priv_channels = true;
898
899         // Clearing route_blinding prevents forming any payment paths since the node is unannounced.
900         let mut features = channelmanager::provided_init_features(&accept_forward_cfg);
901         features.set_onion_messages_optional();
902         features.clear_route_blinding();
903
904         let chanmon_cfgs = create_chanmon_cfgs(6);
905         let node_cfgs = create_node_cfgs(6, &chanmon_cfgs);
906
907         *node_cfgs[1].override_init_features.borrow_mut() = Some(features);
908
909         let node_chanmgrs = create_node_chanmgrs(
910                 6, &node_cfgs, &[None, Some(accept_forward_cfg), None, None, None, None]
911         );
912         let nodes = create_network(6, &node_cfgs, &node_chanmgrs);
913
914         create_unannounced_chan_between_nodes_with_value(&nodes, 0, 1, 10_000_000, 1_000_000_000);
915         create_unannounced_chan_between_nodes_with_value(&nodes, 2, 3, 10_000_000, 1_000_000_000);
916         create_announced_chan_between_nodes_with_value(&nodes, 1, 2, 10_000_000, 1_000_000_000);
917         create_announced_chan_between_nodes_with_value(&nodes, 1, 4, 10_000_000, 1_000_000_000);
918         create_announced_chan_between_nodes_with_value(&nodes, 1, 5, 10_000_000, 1_000_000_000);
919         create_announced_chan_between_nodes_with_value(&nodes, 2, 4, 10_000_000, 1_000_000_000);
920         create_announced_chan_between_nodes_with_value(&nodes, 2, 5, 10_000_000, 1_000_000_000);
921
922         let (alice, bob, charlie, david) = (&nodes[0], &nodes[1], &nodes[2], &nodes[3]);
923         let alice_id = alice.node.get_our_node_id();
924         let bob_id = bob.node.get_our_node_id();
925         let charlie_id = charlie.node.get_our_node_id();
926         let david_id = david.node.get_our_node_id();
927
928         disconnect_peers(alice, &[charlie, david, &nodes[4], &nodes[5]]);
929         disconnect_peers(david, &[bob, &nodes[4], &nodes[5]]);
930
931         let offer = alice.node
932                 .create_offer_builder("coffee".to_string()).unwrap()
933                 .amount_msats(10_000_000)
934                 .build().unwrap();
935
936         let payment_id = PaymentId([1; 32]);
937         david.node.pay_for_offer(&offer, None, None, None, payment_id, Retry::Attempts(0), None)
938                 .unwrap();
939
940         connect_peers(david, bob);
941
942         let onion_message = david.onion_messenger.next_onion_message_for_peer(bob_id).unwrap();
943         bob.onion_messenger.handle_onion_message(&david_id, &onion_message);
944
945         connect_peers(alice, charlie);
946
947         let onion_message = bob.onion_messenger.next_onion_message_for_peer(alice_id).unwrap();
948         alice.onion_messenger.handle_onion_message(&bob_id, &onion_message);
949
950         let onion_message = alice.onion_messenger.next_onion_message_for_peer(charlie_id).unwrap();
951         charlie.onion_messenger.handle_onion_message(&alice_id, &onion_message);
952
953         let onion_message = charlie.onion_messenger.next_onion_message_for_peer(david_id).unwrap();
954         david.onion_messenger.handle_onion_message(&charlie_id, &onion_message);
955
956         let invoice_error = extract_invoice_error(david, &onion_message);
957         assert_eq!(invoice_error, InvoiceError::from(Bolt12SemanticError::MissingPaths));
958 }
959
960 #[test]
961 fn fails_sending_invoice_without_blinded_payment_paths_for_refund() {
962         let mut accept_forward_cfg = test_default_channel_config();
963         accept_forward_cfg.accept_forwards_to_priv_channels = true;
964
965         // Clearing route_blinding prevents forming any payment paths since the node is unannounced.
966         let mut features = channelmanager::provided_init_features(&accept_forward_cfg);
967         features.set_onion_messages_optional();
968         features.clear_route_blinding();
969
970         let chanmon_cfgs = create_chanmon_cfgs(6);
971         let node_cfgs = create_node_cfgs(6, &chanmon_cfgs);
972
973         *node_cfgs[1].override_init_features.borrow_mut() = Some(features);
974
975         let node_chanmgrs = create_node_chanmgrs(
976                 6, &node_cfgs, &[None, Some(accept_forward_cfg), None, None, None, None]
977         );
978         let nodes = create_network(6, &node_cfgs, &node_chanmgrs);
979
980         create_unannounced_chan_between_nodes_with_value(&nodes, 0, 1, 10_000_000, 1_000_000_000);
981         create_unannounced_chan_between_nodes_with_value(&nodes, 2, 3, 10_000_000, 1_000_000_000);
982         create_announced_chan_between_nodes_with_value(&nodes, 1, 2, 10_000_000, 1_000_000_000);
983         create_announced_chan_between_nodes_with_value(&nodes, 1, 4, 10_000_000, 1_000_000_000);
984         create_announced_chan_between_nodes_with_value(&nodes, 1, 5, 10_000_000, 1_000_000_000);
985         create_announced_chan_between_nodes_with_value(&nodes, 2, 4, 10_000_000, 1_000_000_000);
986         create_announced_chan_between_nodes_with_value(&nodes, 2, 5, 10_000_000, 1_000_000_000);
987
988         let (alice, bob, charlie, david) = (&nodes[0], &nodes[1], &nodes[2], &nodes[3]);
989
990         disconnect_peers(alice, &[charlie, david, &nodes[4], &nodes[5]]);
991         disconnect_peers(david, &[bob, &nodes[4], &nodes[5]]);
992
993         let absolute_expiry = Duration::from_secs(u64::MAX);
994         let payment_id = PaymentId([1; 32]);
995         let refund = david.node
996                 .create_refund_builder(
997                         "refund".to_string(), 10_000_000, absolute_expiry, payment_id, Retry::Attempts(0), None
998                 )
999                 .unwrap()
1000                 .build().unwrap();
1001
1002         match alice.node.request_refund_payment(&refund) {
1003                 Ok(_) => panic!("Expected error"),
1004                 Err(e) => assert_eq!(e, Bolt12SemanticError::MissingPaths),
1005         }
1006 }
1007
1008 #[test]
1009 fn fails_paying_invoice_more_than_once() {
1010         let mut accept_forward_cfg = test_default_channel_config();
1011         accept_forward_cfg.accept_forwards_to_priv_channels = true;
1012
1013         let mut features = channelmanager::provided_init_features(&accept_forward_cfg);
1014         features.set_onion_messages_optional();
1015         features.set_route_blinding_optional();
1016
1017         let chanmon_cfgs = create_chanmon_cfgs(6);
1018         let node_cfgs = create_node_cfgs(6, &chanmon_cfgs);
1019
1020         *node_cfgs[1].override_init_features.borrow_mut() = Some(features);
1021
1022         let node_chanmgrs = create_node_chanmgrs(
1023                 6, &node_cfgs, &[None, Some(accept_forward_cfg), None, None, None, None]
1024         );
1025         let nodes = create_network(6, &node_cfgs, &node_chanmgrs);
1026
1027         create_unannounced_chan_between_nodes_with_value(&nodes, 0, 1, 10_000_000, 1_000_000_000);
1028         create_unannounced_chan_between_nodes_with_value(&nodes, 2, 3, 10_000_000, 1_000_000_000);
1029         create_announced_chan_between_nodes_with_value(&nodes, 1, 2, 10_000_000, 1_000_000_000);
1030         create_announced_chan_between_nodes_with_value(&nodes, 1, 4, 10_000_000, 1_000_000_000);
1031         create_announced_chan_between_nodes_with_value(&nodes, 1, 5, 10_000_000, 1_000_000_000);
1032         create_announced_chan_between_nodes_with_value(&nodes, 2, 4, 10_000_000, 1_000_000_000);
1033         create_announced_chan_between_nodes_with_value(&nodes, 2, 5, 10_000_000, 1_000_000_000);
1034
1035         let (alice, bob, charlie, david) = (&nodes[0], &nodes[1], &nodes[2], &nodes[3]);
1036         let alice_id = alice.node.get_our_node_id();
1037         let bob_id = bob.node.get_our_node_id();
1038         let charlie_id = charlie.node.get_our_node_id();
1039         let david_id = david.node.get_our_node_id();
1040
1041         disconnect_peers(alice, &[charlie, david, &nodes[4], &nodes[5]]);
1042         disconnect_peers(david, &[bob, &nodes[4], &nodes[5]]);
1043
1044         let absolute_expiry = Duration::from_secs(u64::MAX);
1045         let payment_id = PaymentId([1; 32]);
1046         let refund = david.node
1047                 .create_refund_builder(
1048                         "refund".to_string(), 10_000_000, absolute_expiry, payment_id, Retry::Attempts(0), None
1049                 )
1050                 .unwrap()
1051                 .build().unwrap();
1052         expect_recent_payment!(david, RecentPaymentDetails::AwaitingInvoice, payment_id);
1053
1054         // Alice sends the first invoice
1055         alice.node.request_refund_payment(&refund).unwrap();
1056
1057         connect_peers(alice, charlie);
1058
1059         let onion_message = alice.onion_messenger.next_onion_message_for_peer(charlie_id).unwrap();
1060         charlie.onion_messenger.handle_onion_message(&alice_id, &onion_message);
1061
1062         let onion_message = charlie.onion_messenger.next_onion_message_for_peer(david_id).unwrap();
1063         david.onion_messenger.handle_onion_message(&charlie_id, &onion_message);
1064
1065         // David pays the first invoice
1066         let invoice1 = extract_invoice(david, &onion_message);
1067
1068         route_bolt12_payment(david, &[charlie, bob, alice], &invoice1);
1069         expect_recent_payment!(david, RecentPaymentDetails::Pending, payment_id);
1070
1071         claim_bolt12_payment(david, &[charlie, bob, alice]);
1072         expect_recent_payment!(david, RecentPaymentDetails::Fulfilled, payment_id);
1073
1074         disconnect_peers(alice, &[charlie]);
1075
1076         // Alice sends the second invoice
1077         alice.node.request_refund_payment(&refund).unwrap();
1078
1079         connect_peers(alice, charlie);
1080         connect_peers(david, bob);
1081
1082         let onion_message = alice.onion_messenger.next_onion_message_for_peer(charlie_id).unwrap();
1083         charlie.onion_messenger.handle_onion_message(&alice_id, &onion_message);
1084
1085         let onion_message = charlie.onion_messenger.next_onion_message_for_peer(david_id).unwrap();
1086         david.onion_messenger.handle_onion_message(&charlie_id, &onion_message);
1087
1088         let invoice2 = extract_invoice(david, &onion_message);
1089         assert_eq!(invoice1.payer_metadata(), invoice2.payer_metadata());
1090
1091         // David sends an error instead of paying the second invoice
1092         let onion_message = david.onion_messenger.next_onion_message_for_peer(bob_id).unwrap();
1093         bob.onion_messenger.handle_onion_message(&david_id, &onion_message);
1094
1095         let onion_message = bob.onion_messenger.next_onion_message_for_peer(alice_id).unwrap();
1096         alice.onion_messenger.handle_onion_message(&bob_id, &onion_message);
1097
1098         let invoice_error = extract_invoice_error(alice, &onion_message);
1099         assert_eq!(invoice_error, InvoiceError::from_string("DuplicateInvoice".to_string()));
1100 }