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