Blinded payments to unannounced introduction node
[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::Network;
44 use bitcoin::secp256k1::PublicKey;
45 use core::time::Duration;
46 use crate::blinded_path::{BlindedPath, IntroductionNode};
47 use crate::blinded_path::payment::{Bolt12OfferContext, Bolt12RefundContext, PaymentContext};
48 use crate::events::{Event, MessageSendEventsProvider, PaymentPurpose};
49 use crate::ln::channelmanager::{Bolt12PaymentError, MAX_SHORT_LIVED_RELATIVE_EXPIRY, PaymentId, RecentPaymentDetails, Retry, self};
50 use crate::ln::functional_test_utils::*;
51 use crate::ln::msgs::{ChannelMessageHandler, Init, NodeAnnouncement, OnionMessage, OnionMessageHandler, RoutingMessageHandler, SocketAddress, UnsignedGossipMessage, UnsignedNodeAnnouncement};
52 use crate::ln::outbound_payment::IDEMPOTENCY_TIMEOUT_TICKS;
53 use crate::offers::invoice::Bolt12Invoice;
54 use crate::offers::invoice_error::InvoiceError;
55 use crate::offers::invoice_request::{InvoiceRequest, InvoiceRequestFields};
56 use crate::offers::parse::Bolt12SemanticError;
57 use crate::onion_message::messenger::PeeledOnion;
58 use crate::onion_message::offers::OffersMessage;
59 use crate::onion_message::packet::ParsedOnionMessageContents;
60 use crate::routing::gossip::{NodeAlias, NodeId};
61 use crate::sign::{NodeSigner, Recipient};
62
63 use crate::prelude::*;
64
65 macro_rules! expect_recent_payment {
66         ($node: expr, $payment_state: path, $payment_id: expr) => {
67                 match $node.node.list_recent_payments().first() {
68                         Some(&$payment_state { payment_id: actual_payment_id, .. }) => {
69                                 assert_eq!($payment_id, actual_payment_id);
70                         },
71                         Some(_) => panic!("Unexpected recent payment state"),
72                         None => panic!("No recent payments"),
73                 }
74         }
75 }
76
77 fn connect_peers<'a, 'b, 'c>(node_a: &Node<'a, 'b, 'c>, node_b: &Node<'a, 'b, 'c>) {
78         let node_id_a = node_a.node.get_our_node_id();
79         let node_id_b = node_b.node.get_our_node_id();
80
81         let init_a = Init {
82                 features: node_a.init_features(&node_id_b),
83                 networks: None,
84                 remote_network_address: None,
85         };
86         let init_b = Init {
87                 features: node_b.init_features(&node_id_a),
88                 networks: None,
89                 remote_network_address: None,
90         };
91
92         node_a.node.peer_connected(&node_id_b, &init_b, true).unwrap();
93         node_b.node.peer_connected(&node_id_a, &init_a, false).unwrap();
94         node_a.onion_messenger.peer_connected(&node_id_b, &init_b, true).unwrap();
95         node_b.onion_messenger.peer_connected(&node_id_a, &init_a, false).unwrap();
96 }
97
98 fn disconnect_peers<'a, 'b, 'c>(node_a: &Node<'a, 'b, 'c>, peers: &[&Node<'a, 'b, 'c>]) {
99         for node_b in peers {
100                 node_a.node.peer_disconnected(&node_b.node.get_our_node_id());
101                 node_b.node.peer_disconnected(&node_a.node.get_our_node_id());
102                 node_a.onion_messenger.peer_disconnected(&node_b.node.get_our_node_id());
103                 node_b.onion_messenger.peer_disconnected(&node_a.node.get_our_node_id());
104         }
105 }
106
107 fn announce_node_address<'a, 'b, 'c>(
108         node: &Node<'a, 'b, 'c>, peers: &[&Node<'a, 'b, 'c>], address: SocketAddress,
109 ) {
110         let features = node.onion_messenger.provided_node_features()
111                 | node.gossip_sync.provided_node_features();
112         let rgb = [0u8; 3];
113         let announcement = UnsignedNodeAnnouncement {
114                 features,
115                 timestamp: 1000,
116                 node_id: NodeId::from_pubkey(&node.keys_manager.get_node_id(Recipient::Node).unwrap()),
117                 rgb,
118                 alias: NodeAlias([0u8; 32]),
119                 addresses: vec![address],
120                 excess_address_data: Vec::new(),
121                 excess_data: Vec::new(),
122         };
123         let signature = node.keys_manager.sign_gossip_message(
124                 UnsignedGossipMessage::NodeAnnouncement(&announcement)
125         ).unwrap();
126
127         let msg = NodeAnnouncement {
128                 signature,
129                 contents: announcement
130         };
131
132         node.gossip_sync.handle_node_announcement(&msg).unwrap();
133         for peer in peers {
134                 peer.gossip_sync.handle_node_announcement(&msg).unwrap();
135         }
136 }
137
138 fn resolve_introduction_node<'a, 'b, 'c>(node: &Node<'a, 'b, 'c>, path: &BlindedPath) -> PublicKey {
139         path.public_introduction_node_id(&node.network_graph.read_only())
140                 .and_then(|node_id| node_id.as_pubkey().ok())
141                 .unwrap()
142 }
143
144 fn route_bolt12_payment<'a, 'b, 'c>(
145         node: &Node<'a, 'b, 'c>, path: &[&Node<'a, 'b, 'c>], invoice: &Bolt12Invoice
146 ) {
147         // Monitor added when handling the invoice onion message.
148         check_added_monitors(node, 1);
149
150         let mut events = node.node.get_and_clear_pending_msg_events();
151         assert_eq!(events.len(), 1);
152         let ev = remove_first_msg_event_to_node(&path[0].node.get_our_node_id(), &mut events);
153
154         // Use a fake payment_hash and bypass checking for the PaymentClaimable event since the
155         // invoice contains the payment_hash but it was encrypted inside an onion message.
156         let amount_msats = invoice.amount_msats();
157         let payment_hash = invoice.payment_hash();
158         let args = PassAlongPathArgs::new(node, path, amount_msats, payment_hash, ev)
159                 .without_clearing_recipient_events();
160         do_pass_along_path(args);
161 }
162
163 fn claim_bolt12_payment<'a, 'b, 'c>(
164         node: &Node<'a, 'b, 'c>, path: &[&Node<'a, 'b, 'c>], expected_payment_context: PaymentContext
165 ) {
166         let recipient = &path[path.len() - 1];
167         let payment_purpose = match get_event!(recipient, Event::PaymentClaimable) {
168                 Event::PaymentClaimable { purpose, .. } => purpose,
169                 _ => panic!("No Event::PaymentClaimable"),
170         };
171         let payment_preimage = match payment_purpose.preimage() {
172                 Some(preimage) => preimage,
173                 None => panic!("No preimage in Event::PaymentClaimable"),
174         };
175         match payment_purpose {
176                 PaymentPurpose::Bolt12OfferPayment { payment_context, .. } => {
177                         assert_eq!(PaymentContext::Bolt12Offer(payment_context), expected_payment_context);
178                 },
179                 PaymentPurpose::Bolt12RefundPayment { payment_context, .. } => {
180                         assert_eq!(PaymentContext::Bolt12Refund(payment_context), expected_payment_context);
181                 },
182                 _ => panic!("Unexpected payment purpose: {:?}", payment_purpose),
183         }
184         claim_payment(node, path, payment_preimage);
185 }
186
187 fn extract_invoice_request<'a, 'b, 'c>(
188         node: &Node<'a, 'b, 'c>, message: &OnionMessage
189 ) -> (InvoiceRequest, BlindedPath) {
190         match node.onion_messenger.peel_onion_message(message) {
191                 Ok(PeeledOnion::Receive(message, _, reply_path)) => match message {
192                         ParsedOnionMessageContents::Offers(offers_message) => match offers_message {
193                                 OffersMessage::InvoiceRequest(invoice_request) => (invoice_request, reply_path.unwrap()),
194                                 OffersMessage::Invoice(invoice) => panic!("Unexpected invoice: {:?}", invoice),
195                                 OffersMessage::InvoiceError(error) => panic!("Unexpected invoice_error: {:?}", error),
196                         },
197                         ParsedOnionMessageContents::Custom(message) => panic!("Unexpected custom message: {:?}", message),
198                 },
199                 Ok(PeeledOnion::Forward(_, _)) => panic!("Unexpected onion message forward"),
200                 Err(e) => panic!("Failed to process onion message {:?}", e),
201         }
202 }
203
204 fn extract_invoice<'a, 'b, 'c>(node: &Node<'a, 'b, 'c>, message: &OnionMessage) -> Bolt12Invoice {
205         match node.onion_messenger.peel_onion_message(message) {
206                 Ok(PeeledOnion::Receive(message, _, _)) => match message {
207                         ParsedOnionMessageContents::Offers(offers_message) => match offers_message {
208                                 OffersMessage::InvoiceRequest(invoice_request) => panic!("Unexpected invoice_request: {:?}", invoice_request),
209                                 OffersMessage::Invoice(invoice) => invoice,
210                                 OffersMessage::InvoiceError(error) => panic!("Unexpected invoice_error: {:?}", error),
211                         },
212                         ParsedOnionMessageContents::Custom(message) => panic!("Unexpected custom message: {:?}", message),
213                 },
214                 Ok(PeeledOnion::Forward(_, _)) => panic!("Unexpected onion message forward"),
215                 Err(e) => panic!("Failed to process onion message {:?}", e),
216         }
217 }
218
219 fn extract_invoice_error<'a, 'b, 'c>(
220         node: &Node<'a, 'b, 'c>, message: &OnionMessage
221 ) -> InvoiceError {
222         match node.onion_messenger.peel_onion_message(message) {
223                 Ok(PeeledOnion::Receive(message, _, _)) => match message {
224                         ParsedOnionMessageContents::Offers(offers_message) => match offers_message {
225                                 OffersMessage::InvoiceRequest(invoice_request) => panic!("Unexpected invoice_request: {:?}", invoice_request),
226                                 OffersMessage::Invoice(invoice) => panic!("Unexpected invoice: {:?}", invoice),
227                                 OffersMessage::InvoiceError(error) => error,
228                         },
229                         ParsedOnionMessageContents::Custom(message) => panic!("Unexpected custom message: {:?}", message),
230                 },
231                 Ok(PeeledOnion::Forward(_, _)) => panic!("Unexpected onion message forward"),
232                 Err(e) => panic!("Failed to process onion message {:?}", e),
233         }
234 }
235
236 /// Checks that blinded paths without Tor-only nodes are preferred when constructing an offer.
237 #[test]
238 fn prefers_non_tor_nodes_in_blinded_paths() {
239         let mut accept_forward_cfg = test_default_channel_config();
240         accept_forward_cfg.accept_forwards_to_priv_channels = true;
241
242         let mut features = channelmanager::provided_init_features(&accept_forward_cfg);
243         features.set_onion_messages_optional();
244         features.set_route_blinding_optional();
245
246         let chanmon_cfgs = create_chanmon_cfgs(6);
247         let node_cfgs = create_node_cfgs(6, &chanmon_cfgs);
248
249         *node_cfgs[1].override_init_features.borrow_mut() = Some(features);
250
251         let node_chanmgrs = create_node_chanmgrs(
252                 6, &node_cfgs, &[None, Some(accept_forward_cfg), None, None, None, None]
253         );
254         let nodes = create_network(6, &node_cfgs, &node_chanmgrs);
255
256         create_unannounced_chan_between_nodes_with_value(&nodes, 0, 1, 10_000_000, 1_000_000_000);
257         create_unannounced_chan_between_nodes_with_value(&nodes, 2, 3, 10_000_000, 1_000_000_000);
258         create_announced_chan_between_nodes_with_value(&nodes, 1, 2, 10_000_000, 1_000_000_000);
259         create_announced_chan_between_nodes_with_value(&nodes, 1, 4, 10_000_000, 1_000_000_000);
260         create_announced_chan_between_nodes_with_value(&nodes, 1, 5, 10_000_000, 1_000_000_000);
261         create_announced_chan_between_nodes_with_value(&nodes, 2, 4, 10_000_000, 1_000_000_000);
262         create_announced_chan_between_nodes_with_value(&nodes, 2, 5, 10_000_000, 1_000_000_000);
263
264         // Add an extra channel so that more than one of Bob's peers have MIN_PEER_CHANNELS.
265         create_announced_chan_between_nodes_with_value(&nodes, 4, 5, 10_000_000, 1_000_000_000);
266
267         let (alice, bob, charlie, david) = (&nodes[0], &nodes[1], &nodes[2], &nodes[3]);
268         let bob_id = bob.node.get_our_node_id();
269         let charlie_id = charlie.node.get_our_node_id();
270
271         disconnect_peers(alice, &[charlie, david, &nodes[4], &nodes[5]]);
272         disconnect_peers(david, &[bob, &nodes[4], &nodes[5]]);
273
274         let tor = SocketAddress::OnionV2([255, 254, 253, 252, 251, 250, 249, 248, 247, 246, 38, 7]);
275         announce_node_address(charlie, &[alice, bob, david, &nodes[4], &nodes[5]], tor.clone());
276
277         let offer = bob.node
278                 .create_offer_builder(None).unwrap()
279                 .amount_msats(10_000_000)
280                 .build().unwrap();
281         assert_ne!(offer.signing_pubkey(), Some(bob_id));
282         assert!(!offer.paths().is_empty());
283         for path in offer.paths() {
284                 let introduction_node_id = resolve_introduction_node(david, &path);
285                 assert_ne!(introduction_node_id, bob_id);
286                 assert_ne!(introduction_node_id, charlie_id);
287         }
288
289         // Use a one-hop blinded path when Bob is announced and all his peers are Tor-only.
290         announce_node_address(&nodes[4], &[alice, bob, charlie, david, &nodes[5]], tor.clone());
291         announce_node_address(&nodes[5], &[alice, bob, charlie, david, &nodes[4]], tor.clone());
292
293         let offer = bob.node
294                 .create_offer_builder(None).unwrap()
295                 .amount_msats(10_000_000)
296                 .build().unwrap();
297         assert_ne!(offer.signing_pubkey(), Some(bob_id));
298         assert!(!offer.paths().is_empty());
299         for path in offer.paths() {
300                 let introduction_node_id = resolve_introduction_node(david, &path);
301                 assert_eq!(introduction_node_id, bob_id);
302         }
303 }
304
305 /// Checks that blinded paths prefer an introduction node that is the most connected.
306 #[test]
307 fn prefers_more_connected_nodes_in_blinded_paths() {
308         let mut accept_forward_cfg = test_default_channel_config();
309         accept_forward_cfg.accept_forwards_to_priv_channels = true;
310
311         let mut features = channelmanager::provided_init_features(&accept_forward_cfg);
312         features.set_onion_messages_optional();
313         features.set_route_blinding_optional();
314
315         let chanmon_cfgs = create_chanmon_cfgs(6);
316         let node_cfgs = create_node_cfgs(6, &chanmon_cfgs);
317
318         *node_cfgs[1].override_init_features.borrow_mut() = Some(features);
319
320         let node_chanmgrs = create_node_chanmgrs(
321                 6, &node_cfgs, &[None, Some(accept_forward_cfg), None, None, None, None]
322         );
323         let nodes = create_network(6, &node_cfgs, &node_chanmgrs);
324
325         create_unannounced_chan_between_nodes_with_value(&nodes, 0, 1, 10_000_000, 1_000_000_000);
326         create_unannounced_chan_between_nodes_with_value(&nodes, 2, 3, 10_000_000, 1_000_000_000);
327         create_announced_chan_between_nodes_with_value(&nodes, 1, 2, 10_000_000, 1_000_000_000);
328         create_announced_chan_between_nodes_with_value(&nodes, 1, 4, 10_000_000, 1_000_000_000);
329         create_announced_chan_between_nodes_with_value(&nodes, 1, 5, 10_000_000, 1_000_000_000);
330         create_announced_chan_between_nodes_with_value(&nodes, 2, 4, 10_000_000, 1_000_000_000);
331         create_announced_chan_between_nodes_with_value(&nodes, 2, 5, 10_000_000, 1_000_000_000);
332
333         // Add extra channels so that more than one of Bob's peers have MIN_PEER_CHANNELS and one has
334         // more than the others.
335         create_announced_chan_between_nodes_with_value(&nodes, 0, 4, 10_000_000, 1_000_000_000);
336         create_announced_chan_between_nodes_with_value(&nodes, 3, 4, 10_000_000, 1_000_000_000);
337
338         let (alice, bob, charlie, david) = (&nodes[0], &nodes[1], &nodes[2], &nodes[3]);
339         let bob_id = bob.node.get_our_node_id();
340
341         disconnect_peers(alice, &[charlie, david, &nodes[4], &nodes[5]]);
342         disconnect_peers(david, &[bob, &nodes[4], &nodes[5]]);
343
344         let offer = bob.node
345                 .create_offer_builder(None).unwrap()
346                 .amount_msats(10_000_000)
347                 .build().unwrap();
348         assert_ne!(offer.signing_pubkey(), Some(bob_id));
349         assert!(!offer.paths().is_empty());
350         for path in offer.paths() {
351                 let introduction_node_id = resolve_introduction_node(david, &path);
352                 assert_eq!(introduction_node_id, nodes[4].node.get_our_node_id());
353         }
354 }
355
356 /// Checks that blinded paths are compact for short-lived offers.
357 #[test]
358 fn creates_short_lived_offer() {
359         let chanmon_cfgs = create_chanmon_cfgs(2);
360         let node_cfgs = create_node_cfgs(2, &chanmon_cfgs);
361         let node_chanmgrs = create_node_chanmgrs(2, &node_cfgs, &[None, None]);
362         let nodes = create_network(2, &node_cfgs, &node_chanmgrs);
363
364         create_announced_chan_between_nodes_with_value(&nodes, 0, 1, 10_000_000, 1_000_000_000);
365
366         let alice = &nodes[0];
367         let alice_id = alice.node.get_our_node_id();
368         let bob = &nodes[1];
369
370         let absolute_expiry = alice.node.duration_since_epoch() + MAX_SHORT_LIVED_RELATIVE_EXPIRY;
371         let offer = alice.node
372                 .create_offer_builder(Some(absolute_expiry)).unwrap()
373                 .build().unwrap();
374         assert_eq!(offer.absolute_expiry(), Some(absolute_expiry));
375         assert!(!offer.paths().is_empty());
376         for path in offer.paths() {
377                 let introduction_node_id = resolve_introduction_node(bob, &path);
378                 assert_eq!(introduction_node_id, alice_id);
379                 assert!(matches!(path.introduction_node, IntroductionNode::DirectedShortChannelId(..)));
380         }
381 }
382
383 /// Checks that blinded paths are not compact for long-lived offers.
384 #[test]
385 fn creates_long_lived_offer() {
386         let chanmon_cfgs = create_chanmon_cfgs(2);
387         let node_cfgs = create_node_cfgs(2, &chanmon_cfgs);
388         let node_chanmgrs = create_node_chanmgrs(2, &node_cfgs, &[None, None]);
389         let nodes = create_network(2, &node_cfgs, &node_chanmgrs);
390
391         create_announced_chan_between_nodes_with_value(&nodes, 0, 1, 10_000_000, 1_000_000_000);
392
393         let alice = &nodes[0];
394         let alice_id = alice.node.get_our_node_id();
395
396         let absolute_expiry = alice.node.duration_since_epoch() + MAX_SHORT_LIVED_RELATIVE_EXPIRY
397                 + Duration::from_secs(1);
398         let offer = alice.node
399                 .create_offer_builder(Some(absolute_expiry))
400                 .unwrap()
401                 .build().unwrap();
402         assert_eq!(offer.absolute_expiry(), Some(absolute_expiry));
403         assert!(!offer.paths().is_empty());
404         for path in offer.paths() {
405                 assert_eq!(path.introduction_node, IntroductionNode::NodeId(alice_id));
406         }
407
408         let offer = alice.node
409                 .create_offer_builder(None).unwrap()
410                 .build().unwrap();
411         assert_eq!(offer.absolute_expiry(), None);
412         assert!(!offer.paths().is_empty());
413         for path in offer.paths() {
414                 assert_eq!(path.introduction_node, IntroductionNode::NodeId(alice_id));
415         }
416 }
417
418 /// Checks that blinded paths are compact for short-lived refunds.
419 #[test]
420 fn creates_short_lived_refund() {
421         let chanmon_cfgs = create_chanmon_cfgs(2);
422         let node_cfgs = create_node_cfgs(2, &chanmon_cfgs);
423         let node_chanmgrs = create_node_chanmgrs(2, &node_cfgs, &[None, None]);
424         let nodes = create_network(2, &node_cfgs, &node_chanmgrs);
425
426         create_announced_chan_between_nodes_with_value(&nodes, 0, 1, 10_000_000, 1_000_000_000);
427
428         let alice = &nodes[0];
429         let bob = &nodes[1];
430         let bob_id = bob.node.get_our_node_id();
431
432         let absolute_expiry = bob.node.duration_since_epoch() + MAX_SHORT_LIVED_RELATIVE_EXPIRY;
433         let payment_id = PaymentId([1; 32]);
434         let refund = bob.node
435                 .create_refund_builder(10_000_000, absolute_expiry, payment_id, Retry::Attempts(0), None)
436                 .unwrap()
437                 .build().unwrap();
438         assert_eq!(refund.absolute_expiry(), Some(absolute_expiry));
439         assert!(!refund.paths().is_empty());
440         for path in refund.paths() {
441                 let introduction_node_id = resolve_introduction_node(alice, &path);
442                 assert_eq!(introduction_node_id, bob_id);
443                 assert!(matches!(path.introduction_node, IntroductionNode::DirectedShortChannelId(..)));
444         }
445 }
446
447 /// Checks that blinded paths are not compact for long-lived refunds.
448 #[test]
449 fn creates_long_lived_refund() {
450         let chanmon_cfgs = create_chanmon_cfgs(2);
451         let node_cfgs = create_node_cfgs(2, &chanmon_cfgs);
452         let node_chanmgrs = create_node_chanmgrs(2, &node_cfgs, &[None, None]);
453         let nodes = create_network(2, &node_cfgs, &node_chanmgrs);
454
455         create_announced_chan_between_nodes_with_value(&nodes, 0, 1, 10_000_000, 1_000_000_000);
456
457         let bob = &nodes[1];
458         let bob_id = bob.node.get_our_node_id();
459
460         let absolute_expiry = bob.node.duration_since_epoch() + MAX_SHORT_LIVED_RELATIVE_EXPIRY
461                 + Duration::from_secs(1);
462         let payment_id = PaymentId([1; 32]);
463         let refund = bob.node
464                 .create_refund_builder(10_000_000, absolute_expiry, payment_id, Retry::Attempts(0), None)
465                 .unwrap()
466                 .build().unwrap();
467         assert_eq!(refund.absolute_expiry(), Some(absolute_expiry));
468         assert!(!refund.paths().is_empty());
469         for path in refund.paths() {
470                 assert_eq!(path.introduction_node, IntroductionNode::NodeId(bob_id));
471         }
472 }
473
474 /// Checks that an offer can be paid through blinded paths and that ephemeral pubkeys are used
475 /// rather than exposing a node's pubkey.
476 #[test]
477 fn creates_and_pays_for_offer_using_two_hop_blinded_path() {
478         let mut accept_forward_cfg = test_default_channel_config();
479         accept_forward_cfg.accept_forwards_to_priv_channels = true;
480
481         let mut features = channelmanager::provided_init_features(&accept_forward_cfg);
482         features.set_onion_messages_optional();
483         features.set_route_blinding_optional();
484
485         let chanmon_cfgs = create_chanmon_cfgs(6);
486         let node_cfgs = create_node_cfgs(6, &chanmon_cfgs);
487
488         *node_cfgs[1].override_init_features.borrow_mut() = Some(features);
489
490         let node_chanmgrs = create_node_chanmgrs(
491                 6, &node_cfgs, &[None, Some(accept_forward_cfg), None, None, None, None]
492         );
493         let nodes = create_network(6, &node_cfgs, &node_chanmgrs);
494
495         create_unannounced_chan_between_nodes_with_value(&nodes, 0, 1, 10_000_000, 1_000_000_000);
496         create_unannounced_chan_between_nodes_with_value(&nodes, 2, 3, 10_000_000, 1_000_000_000);
497         create_announced_chan_between_nodes_with_value(&nodes, 1, 2, 10_000_000, 1_000_000_000);
498         create_announced_chan_between_nodes_with_value(&nodes, 1, 4, 10_000_000, 1_000_000_000);
499         create_announced_chan_between_nodes_with_value(&nodes, 1, 5, 10_000_000, 1_000_000_000);
500         create_announced_chan_between_nodes_with_value(&nodes, 2, 4, 10_000_000, 1_000_000_000);
501         create_announced_chan_between_nodes_with_value(&nodes, 2, 5, 10_000_000, 1_000_000_000);
502
503         let (alice, bob, charlie, david) = (&nodes[0], &nodes[1], &nodes[2], &nodes[3]);
504         let alice_id = alice.node.get_our_node_id();
505         let bob_id = bob.node.get_our_node_id();
506         let charlie_id = charlie.node.get_our_node_id();
507         let david_id = david.node.get_our_node_id();
508
509         disconnect_peers(alice, &[charlie, david, &nodes[4], &nodes[5]]);
510         disconnect_peers(david, &[bob, &nodes[4], &nodes[5]]);
511
512         let offer = alice.node
513                 .create_offer_builder(None)
514                 .unwrap()
515                 .amount_msats(10_000_000)
516                 .build().unwrap();
517         assert_ne!(offer.signing_pubkey(), Some(alice_id));
518         assert!(!offer.paths().is_empty());
519         for path in offer.paths() {
520                 assert_eq!(path.introduction_node, IntroductionNode::NodeId(bob_id));
521         }
522
523         let payment_id = PaymentId([1; 32]);
524         david.node.pay_for_offer(&offer, None, None, None, payment_id, Retry::Attempts(0), None)
525                 .unwrap();
526         expect_recent_payment!(david, RecentPaymentDetails::AwaitingInvoice, payment_id);
527
528         connect_peers(david, bob);
529
530         let onion_message = david.onion_messenger.next_onion_message_for_peer(bob_id).unwrap();
531         bob.onion_messenger.handle_onion_message(&david_id, &onion_message);
532
533         connect_peers(alice, charlie);
534
535         let onion_message = bob.onion_messenger.next_onion_message_for_peer(alice_id).unwrap();
536         alice.onion_messenger.handle_onion_message(&bob_id, &onion_message);
537
538         let (invoice_request, reply_path) = extract_invoice_request(alice, &onion_message);
539         let payment_context = PaymentContext::Bolt12Offer(Bolt12OfferContext {
540                 offer_id: offer.id(),
541                 invoice_request: InvoiceRequestFields {
542                         payer_id: invoice_request.payer_id(),
543                         quantity: None,
544                         payer_note_truncated: None,
545                 },
546         });
547         assert_eq!(invoice_request.amount_msats(), None);
548         assert_ne!(invoice_request.payer_id(), david_id);
549         assert_eq!(reply_path.introduction_node, IntroductionNode::NodeId(charlie_id));
550
551         let onion_message = alice.onion_messenger.next_onion_message_for_peer(charlie_id).unwrap();
552         charlie.onion_messenger.handle_onion_message(&alice_id, &onion_message);
553
554         let onion_message = charlie.onion_messenger.next_onion_message_for_peer(david_id).unwrap();
555         david.onion_messenger.handle_onion_message(&charlie_id, &onion_message);
556
557         let invoice = extract_invoice(david, &onion_message);
558         assert_eq!(invoice.amount_msats(), 10_000_000);
559         assert_ne!(invoice.signing_pubkey(), alice_id);
560         assert!(!invoice.payment_paths().is_empty());
561         for (_, path) in invoice.payment_paths() {
562                 assert_eq!(path.introduction_node, IntroductionNode::NodeId(bob_id));
563         }
564
565         route_bolt12_payment(david, &[charlie, bob, alice], &invoice);
566         expect_recent_payment!(david, RecentPaymentDetails::Pending, payment_id);
567
568         claim_bolt12_payment(david, &[charlie, bob, alice], payment_context);
569         expect_recent_payment!(david, RecentPaymentDetails::Fulfilled, payment_id);
570 }
571
572 /// Checks that a refund can be paid through blinded paths and that ephemeral pubkeys are used
573 /// rather than exposing a node's pubkey.
574 #[test]
575 fn creates_and_pays_for_refund_using_two_hop_blinded_path() {
576         let mut accept_forward_cfg = test_default_channel_config();
577         accept_forward_cfg.accept_forwards_to_priv_channels = true;
578
579         let mut features = channelmanager::provided_init_features(&accept_forward_cfg);
580         features.set_onion_messages_optional();
581         features.set_route_blinding_optional();
582
583         let chanmon_cfgs = create_chanmon_cfgs(6);
584         let node_cfgs = create_node_cfgs(6, &chanmon_cfgs);
585
586         *node_cfgs[1].override_init_features.borrow_mut() = Some(features);
587
588         let node_chanmgrs = create_node_chanmgrs(
589                 6, &node_cfgs, &[None, Some(accept_forward_cfg), None, None, None, None]
590         );
591         let nodes = create_network(6, &node_cfgs, &node_chanmgrs);
592
593         create_unannounced_chan_between_nodes_with_value(&nodes, 0, 1, 10_000_000, 1_000_000_000);
594         create_unannounced_chan_between_nodes_with_value(&nodes, 2, 3, 10_000_000, 1_000_000_000);
595         create_announced_chan_between_nodes_with_value(&nodes, 1, 2, 10_000_000, 1_000_000_000);
596         create_announced_chan_between_nodes_with_value(&nodes, 1, 4, 10_000_000, 1_000_000_000);
597         create_announced_chan_between_nodes_with_value(&nodes, 1, 5, 10_000_000, 1_000_000_000);
598         create_announced_chan_between_nodes_with_value(&nodes, 2, 4, 10_000_000, 1_000_000_000);
599         create_announced_chan_between_nodes_with_value(&nodes, 2, 5, 10_000_000, 1_000_000_000);
600
601         let (alice, bob, charlie, david) = (&nodes[0], &nodes[1], &nodes[2], &nodes[3]);
602         let alice_id = alice.node.get_our_node_id();
603         let bob_id = bob.node.get_our_node_id();
604         let charlie_id = charlie.node.get_our_node_id();
605         let david_id = david.node.get_our_node_id();
606
607         disconnect_peers(alice, &[charlie, david, &nodes[4], &nodes[5]]);
608         disconnect_peers(david, &[bob, &nodes[4], &nodes[5]]);
609
610         let absolute_expiry = Duration::from_secs(u64::MAX);
611         let payment_id = PaymentId([1; 32]);
612         let refund = david.node
613                 .create_refund_builder(10_000_000, absolute_expiry, payment_id, Retry::Attempts(0), None)
614                 .unwrap()
615                 .build().unwrap();
616         assert_eq!(refund.amount_msats(), 10_000_000);
617         assert_eq!(refund.absolute_expiry(), Some(absolute_expiry));
618         assert_ne!(refund.payer_id(), david_id);
619         assert!(!refund.paths().is_empty());
620         for path in refund.paths() {
621                 assert_eq!(path.introduction_node, IntroductionNode::NodeId(charlie_id));
622         }
623         expect_recent_payment!(david, RecentPaymentDetails::AwaitingInvoice, payment_id);
624
625         let payment_context = PaymentContext::Bolt12Refund(Bolt12RefundContext {});
626         let expected_invoice = alice.node.request_refund_payment(&refund).unwrap();
627
628         connect_peers(alice, charlie);
629
630         let onion_message = alice.onion_messenger.next_onion_message_for_peer(charlie_id).unwrap();
631         charlie.onion_messenger.handle_onion_message(&alice_id, &onion_message);
632
633         let onion_message = charlie.onion_messenger.next_onion_message_for_peer(david_id).unwrap();
634         david.onion_messenger.handle_onion_message(&charlie_id, &onion_message);
635
636         let invoice = extract_invoice(david, &onion_message);
637         assert_eq!(invoice, expected_invoice);
638
639         assert_eq!(invoice.amount_msats(), 10_000_000);
640         assert_ne!(invoice.signing_pubkey(), alice_id);
641         assert!(!invoice.payment_paths().is_empty());
642         for (_, path) in invoice.payment_paths() {
643                 assert_eq!(path.introduction_node, IntroductionNode::NodeId(bob_id));
644         }
645
646         route_bolt12_payment(david, &[charlie, bob, alice], &invoice);
647         expect_recent_payment!(david, RecentPaymentDetails::Pending, payment_id);
648
649         claim_bolt12_payment(david, &[charlie, bob, alice], payment_context);
650         expect_recent_payment!(david, RecentPaymentDetails::Fulfilled, payment_id);
651 }
652
653 /// Checks that an offer can be paid through a one-hop blinded path and that ephemeral pubkeys are
654 /// used rather than exposing a node's pubkey. However, the node's pubkey is still used as the
655 /// introduction node of the blinded path.
656 #[test]
657 fn creates_and_pays_for_offer_using_one_hop_blinded_path() {
658         let chanmon_cfgs = create_chanmon_cfgs(2);
659         let node_cfgs = create_node_cfgs(2, &chanmon_cfgs);
660         let node_chanmgrs = create_node_chanmgrs(2, &node_cfgs, &[None, None]);
661         let nodes = create_network(2, &node_cfgs, &node_chanmgrs);
662
663         create_announced_chan_between_nodes_with_value(&nodes, 0, 1, 10_000_000, 1_000_000_000);
664
665         let alice = &nodes[0];
666         let alice_id = alice.node.get_our_node_id();
667         let bob = &nodes[1];
668         let bob_id = bob.node.get_our_node_id();
669
670         let offer = alice.node
671                 .create_offer_builder(None).unwrap()
672                 .amount_msats(10_000_000)
673                 .build().unwrap();
674         assert_ne!(offer.signing_pubkey(), Some(alice_id));
675         assert!(!offer.paths().is_empty());
676         for path in offer.paths() {
677                 assert_eq!(path.introduction_node, IntroductionNode::NodeId(alice_id));
678         }
679
680         let payment_id = PaymentId([1; 32]);
681         bob.node.pay_for_offer(&offer, None, None, None, payment_id, Retry::Attempts(0), None).unwrap();
682         expect_recent_payment!(bob, RecentPaymentDetails::AwaitingInvoice, payment_id);
683
684         let onion_message = bob.onion_messenger.next_onion_message_for_peer(alice_id).unwrap();
685         alice.onion_messenger.handle_onion_message(&bob_id, &onion_message);
686
687         let (invoice_request, reply_path) = extract_invoice_request(alice, &onion_message);
688         let payment_context = PaymentContext::Bolt12Offer(Bolt12OfferContext {
689                 offer_id: offer.id(),
690                 invoice_request: InvoiceRequestFields {
691                         payer_id: invoice_request.payer_id(),
692                         quantity: None,
693                         payer_note_truncated: None,
694                 },
695         });
696         assert_eq!(invoice_request.amount_msats(), None);
697         assert_ne!(invoice_request.payer_id(), bob_id);
698         assert_eq!(reply_path.introduction_node, IntroductionNode::NodeId(bob_id));
699
700         let onion_message = alice.onion_messenger.next_onion_message_for_peer(bob_id).unwrap();
701         bob.onion_messenger.handle_onion_message(&alice_id, &onion_message);
702
703         let invoice = extract_invoice(bob, &onion_message);
704         assert_eq!(invoice.amount_msats(), 10_000_000);
705         assert_ne!(invoice.signing_pubkey(), alice_id);
706         assert!(!invoice.payment_paths().is_empty());
707         for (_, path) in invoice.payment_paths() {
708                 assert_eq!(path.introduction_node, IntroductionNode::NodeId(alice_id));
709         }
710
711         route_bolt12_payment(bob, &[alice], &invoice);
712         expect_recent_payment!(bob, RecentPaymentDetails::Pending, payment_id);
713
714         claim_bolt12_payment(bob, &[alice], payment_context);
715         expect_recent_payment!(bob, RecentPaymentDetails::Fulfilled, payment_id);
716 }
717
718 /// Checks that a refund can be paid through a one-hop blinded path and that ephemeral pubkeys are
719 /// used rather than exposing a node's pubkey. However, the node's pubkey is still used as the
720 /// introduction node of the blinded path.
721 #[test]
722 fn creates_and_pays_for_refund_using_one_hop_blinded_path() {
723         let chanmon_cfgs = create_chanmon_cfgs(2);
724         let node_cfgs = create_node_cfgs(2, &chanmon_cfgs);
725         let node_chanmgrs = create_node_chanmgrs(2, &node_cfgs, &[None, None]);
726         let nodes = create_network(2, &node_cfgs, &node_chanmgrs);
727
728         create_announced_chan_between_nodes_with_value(&nodes, 0, 1, 10_000_000, 1_000_000_000);
729
730         let alice = &nodes[0];
731         let alice_id = alice.node.get_our_node_id();
732         let bob = &nodes[1];
733         let bob_id = bob.node.get_our_node_id();
734
735         let absolute_expiry = Duration::from_secs(u64::MAX);
736         let payment_id = PaymentId([1; 32]);
737         let refund = bob.node
738                 .create_refund_builder(10_000_000, absolute_expiry, payment_id, Retry::Attempts(0), None)
739                 .unwrap()
740                 .build().unwrap();
741         assert_eq!(refund.amount_msats(), 10_000_000);
742         assert_eq!(refund.absolute_expiry(), Some(absolute_expiry));
743         assert_ne!(refund.payer_id(), bob_id);
744         assert!(!refund.paths().is_empty());
745         for path in refund.paths() {
746                 assert_eq!(path.introduction_node, IntroductionNode::NodeId(bob_id));
747         }
748         expect_recent_payment!(bob, RecentPaymentDetails::AwaitingInvoice, payment_id);
749
750         let payment_context = PaymentContext::Bolt12Refund(Bolt12RefundContext {});
751         let expected_invoice = alice.node.request_refund_payment(&refund).unwrap();
752
753         let onion_message = alice.onion_messenger.next_onion_message_for_peer(bob_id).unwrap();
754         bob.onion_messenger.handle_onion_message(&alice_id, &onion_message);
755
756         let invoice = extract_invoice(bob, &onion_message);
757         assert_eq!(invoice, expected_invoice);
758
759         assert_eq!(invoice.amount_msats(), 10_000_000);
760         assert_ne!(invoice.signing_pubkey(), alice_id);
761         assert!(!invoice.payment_paths().is_empty());
762         for (_, path) in invoice.payment_paths() {
763                 assert_eq!(path.introduction_node, IntroductionNode::NodeId(alice_id));
764         }
765
766         route_bolt12_payment(bob, &[alice], &invoice);
767         expect_recent_payment!(bob, RecentPaymentDetails::Pending, payment_id);
768
769         claim_bolt12_payment(bob, &[alice], payment_context);
770         expect_recent_payment!(bob, RecentPaymentDetails::Fulfilled, payment_id);
771 }
772
773 /// Checks that an invoice for an offer without any blinded paths can be requested. Note that while
774 /// the requested is sent directly using the node's pubkey, the response and the payment still use
775 /// blinded paths as required by the spec.
776 #[test]
777 fn pays_for_offer_without_blinded_paths() {
778         let chanmon_cfgs = create_chanmon_cfgs(2);
779         let node_cfgs = create_node_cfgs(2, &chanmon_cfgs);
780         let node_chanmgrs = create_node_chanmgrs(2, &node_cfgs, &[None, None]);
781         let nodes = create_network(2, &node_cfgs, &node_chanmgrs);
782
783         create_announced_chan_between_nodes_with_value(&nodes, 0, 1, 10_000_000, 1_000_000_000);
784
785         let alice = &nodes[0];
786         let alice_id = alice.node.get_our_node_id();
787         let bob = &nodes[1];
788         let bob_id = bob.node.get_our_node_id();
789
790         let offer = alice.node
791                 .create_offer_builder(None).unwrap()
792                 .clear_paths()
793                 .amount_msats(10_000_000)
794                 .build().unwrap();
795         assert_eq!(offer.signing_pubkey(), Some(alice_id));
796         assert!(offer.paths().is_empty());
797
798         let payment_id = PaymentId([1; 32]);
799         bob.node.pay_for_offer(&offer, None, None, None, payment_id, Retry::Attempts(0), None).unwrap();
800         expect_recent_payment!(bob, RecentPaymentDetails::AwaitingInvoice, payment_id);
801
802         let onion_message = bob.onion_messenger.next_onion_message_for_peer(alice_id).unwrap();
803         alice.onion_messenger.handle_onion_message(&bob_id, &onion_message);
804
805         let (invoice_request, _) = extract_invoice_request(alice, &onion_message);
806         let payment_context = PaymentContext::Bolt12Offer(Bolt12OfferContext {
807                 offer_id: offer.id(),
808                 invoice_request: InvoiceRequestFields {
809                         payer_id: invoice_request.payer_id(),
810                         quantity: None,
811                         payer_note_truncated: None,
812                 },
813         });
814
815         let onion_message = alice.onion_messenger.next_onion_message_for_peer(bob_id).unwrap();
816         bob.onion_messenger.handle_onion_message(&alice_id, &onion_message);
817
818         let invoice = extract_invoice(bob, &onion_message);
819         route_bolt12_payment(bob, &[alice], &invoice);
820         expect_recent_payment!(bob, RecentPaymentDetails::Pending, payment_id);
821
822         claim_bolt12_payment(bob, &[alice], payment_context);
823         expect_recent_payment!(bob, RecentPaymentDetails::Fulfilled, payment_id);
824 }
825
826 /// Checks that a refund without any blinded paths can be paid. Note that while the invoice is sent
827 /// directly using the node's pubkey, the payment still use blinded paths as required by the spec.
828 #[test]
829 fn pays_for_refund_without_blinded_paths() {
830         let chanmon_cfgs = create_chanmon_cfgs(2);
831         let node_cfgs = create_node_cfgs(2, &chanmon_cfgs);
832         let node_chanmgrs = create_node_chanmgrs(2, &node_cfgs, &[None, None]);
833         let nodes = create_network(2, &node_cfgs, &node_chanmgrs);
834
835         create_announced_chan_between_nodes_with_value(&nodes, 0, 1, 10_000_000, 1_000_000_000);
836
837         let alice = &nodes[0];
838         let alice_id = alice.node.get_our_node_id();
839         let bob = &nodes[1];
840         let bob_id = bob.node.get_our_node_id();
841
842         let absolute_expiry = Duration::from_secs(u64::MAX);
843         let payment_id = PaymentId([1; 32]);
844         let refund = bob.node
845                 .create_refund_builder(10_000_000, absolute_expiry, payment_id, Retry::Attempts(0), None)
846                 .unwrap()
847                 .clear_paths()
848                 .build().unwrap();
849         assert_eq!(refund.payer_id(), bob_id);
850         assert!(refund.paths().is_empty());
851         expect_recent_payment!(bob, RecentPaymentDetails::AwaitingInvoice, payment_id);
852
853         let payment_context = PaymentContext::Bolt12Refund(Bolt12RefundContext {});
854         let expected_invoice = alice.node.request_refund_payment(&refund).unwrap();
855
856         let onion_message = alice.onion_messenger.next_onion_message_for_peer(bob_id).unwrap();
857         bob.onion_messenger.handle_onion_message(&alice_id, &onion_message);
858
859         let invoice = extract_invoice(bob, &onion_message);
860         assert_eq!(invoice, expected_invoice);
861
862         route_bolt12_payment(bob, &[alice], &invoice);
863         expect_recent_payment!(bob, RecentPaymentDetails::Pending, payment_id);
864
865         claim_bolt12_payment(bob, &[alice], payment_context);
866         expect_recent_payment!(bob, RecentPaymentDetails::Fulfilled, payment_id);
867 }
868
869 /// Checks that a deferred invoice can be paid asynchronously from an Event::InvoiceReceived.
870 #[test]
871 fn pays_bolt12_invoice_asynchronously() {
872         let mut manually_pay_cfg = test_default_channel_config();
873         manually_pay_cfg.manually_handle_bolt12_invoices = true;
874
875         let chanmon_cfgs = create_chanmon_cfgs(2);
876         let node_cfgs = create_node_cfgs(2, &chanmon_cfgs);
877         let node_chanmgrs = create_node_chanmgrs(2, &node_cfgs, &[None, Some(manually_pay_cfg)]);
878         let nodes = create_network(2, &node_cfgs, &node_chanmgrs);
879
880         create_announced_chan_between_nodes_with_value(&nodes, 0, 1, 10_000_000, 1_000_000_000);
881
882         let alice = &nodes[0];
883         let alice_id = alice.node.get_our_node_id();
884         let bob = &nodes[1];
885         let bob_id = bob.node.get_our_node_id();
886
887         let offer = alice.node
888                 .create_offer_builder(None).unwrap()
889                 .amount_msats(10_000_000)
890                 .build().unwrap();
891
892         let payment_id = PaymentId([1; 32]);
893         bob.node.pay_for_offer(&offer, None, None, None, payment_id, Retry::Attempts(0), None).unwrap();
894         expect_recent_payment!(bob, RecentPaymentDetails::AwaitingInvoice, payment_id);
895
896         let onion_message = bob.onion_messenger.next_onion_message_for_peer(alice_id).unwrap();
897         alice.onion_messenger.handle_onion_message(&bob_id, &onion_message);
898
899         let (invoice_request, _) = extract_invoice_request(alice, &onion_message);
900         let payment_context = PaymentContext::Bolt12Offer(Bolt12OfferContext {
901                 offer_id: offer.id(),
902                 invoice_request: InvoiceRequestFields {
903                         payer_id: invoice_request.payer_id(),
904                         quantity: None,
905                         payer_note_truncated: None,
906                 },
907         });
908
909         let onion_message = alice.onion_messenger.next_onion_message_for_peer(bob_id).unwrap();
910         bob.onion_messenger.handle_onion_message(&alice_id, &onion_message);
911
912         let invoice = match get_event!(bob, Event::InvoiceReceived) {
913                 Event::InvoiceReceived { payment_id: actual_payment_id, invoice, .. } => {
914                         assert_eq!(actual_payment_id, payment_id);
915                         invoice
916                 },
917                 _ => panic!("No Event::InvoiceReceived"),
918         };
919         assert_eq!(invoice.amount_msats(), 10_000_000);
920         assert_ne!(invoice.signing_pubkey(), alice_id);
921         assert!(!invoice.payment_paths().is_empty());
922         for (_, path) in invoice.payment_paths() {
923                 assert_eq!(path.introduction_node, IntroductionNode::NodeId(alice_id));
924         }
925
926         assert!(bob.node.send_payment_for_bolt12_invoice(&invoice).is_ok());
927         assert_eq!(
928                 bob.node.send_payment_for_bolt12_invoice(&invoice),
929                 Err(Bolt12PaymentError::DuplicateInvoice),
930         );
931
932         route_bolt12_payment(bob, &[alice], &invoice);
933         expect_recent_payment!(bob, RecentPaymentDetails::Pending, payment_id);
934
935         claim_bolt12_payment(bob, &[alice], payment_context);
936         expect_recent_payment!(bob, RecentPaymentDetails::Fulfilled, payment_id);
937
938         assert_eq!(
939                 bob.node.send_payment_for_bolt12_invoice(&invoice),
940                 Err(Bolt12PaymentError::DuplicateInvoice),
941         );
942
943         for _ in 0..=IDEMPOTENCY_TIMEOUT_TICKS {
944                 bob.node.timer_tick_occurred();
945         }
946
947         assert_eq!(
948                 bob.node.send_payment_for_bolt12_invoice(&invoice),
949                 Err(Bolt12PaymentError::UnexpectedInvoice),
950         );
951 }
952
953 /// Checks that an offer can be created using an unannounced node as a blinded path's introduction
954 /// node. This is only preferred if there are no other options which may indicated either the offer
955 /// is intended for the unannounced node or that the node is actually announced (e.g., an LSP) but
956 /// the recipient doesn't have a network graph.
957 #[test]
958 fn creates_offer_with_blinded_path_using_unannounced_introduction_node() {
959         let chanmon_cfgs = create_chanmon_cfgs(2);
960         let node_cfgs = create_node_cfgs(2, &chanmon_cfgs);
961         let node_chanmgrs = create_node_chanmgrs(2, &node_cfgs, &[None, None]);
962         let nodes = create_network(2, &node_cfgs, &node_chanmgrs);
963
964         create_unannounced_chan_between_nodes_with_value(&nodes, 0, 1, 10_000_000, 1_000_000_000);
965
966         let alice = &nodes[0];
967         let alice_id = alice.node.get_our_node_id();
968         let bob = &nodes[1];
969         let bob_id = bob.node.get_our_node_id();
970
971         let offer = alice.node
972                 .create_offer_builder(None).unwrap()
973                 .amount_msats(10_000_000)
974                 .build().unwrap();
975         assert_ne!(offer.signing_pubkey(), Some(alice_id));
976         assert!(!offer.paths().is_empty());
977         for path in offer.paths() {
978                 assert_eq!(path.introduction_node, IntroductionNode::NodeId(bob_id));
979         }
980
981         let payment_id = PaymentId([1; 32]);
982         bob.node.pay_for_offer(&offer, None, None, None, payment_id, Retry::Attempts(0), None).unwrap();
983         expect_recent_payment!(bob, RecentPaymentDetails::AwaitingInvoice, payment_id);
984
985         let onion_message = bob.onion_messenger.next_onion_message_for_peer(alice_id).unwrap();
986         alice.onion_messenger.handle_onion_message(&bob_id, &onion_message);
987
988         let (invoice_request, reply_path) = extract_invoice_request(alice, &onion_message);
989         assert_ne!(invoice_request.payer_id(), bob_id);
990         assert_eq!(reply_path.introduction_node, IntroductionNode::NodeId(alice_id));
991
992         let onion_message = alice.onion_messenger.next_onion_message_for_peer(bob_id).unwrap();
993
994         let invoice = extract_invoice(bob, &onion_message);
995         assert_ne!(invoice.signing_pubkey(), alice_id);
996         assert!(!invoice.payment_paths().is_empty());
997         for (_, path) in invoice.payment_paths() {
998                 assert_eq!(path.introduction_node, IntroductionNode::NodeId(bob_id));
999         }
1000 }
1001
1002 /// Checks that a refund can be created using an unannounced node as a blinded path's introduction
1003 /// node. This is only preferred if there are no other options which may indicated either the refund
1004 /// is intended for the unannounced node or that the node is actually announced (e.g., an LSP) but
1005 /// the sender doesn't have a network graph.
1006 #[test]
1007 fn creates_refund_with_blinded_path_using_unannounced_introduction_node() {
1008         let chanmon_cfgs = create_chanmon_cfgs(2);
1009         let node_cfgs = create_node_cfgs(2, &chanmon_cfgs);
1010         let node_chanmgrs = create_node_chanmgrs(2, &node_cfgs, &[None, None]);
1011         let nodes = create_network(2, &node_cfgs, &node_chanmgrs);
1012
1013         create_unannounced_chan_between_nodes_with_value(&nodes, 0, 1, 10_000_000, 1_000_000_000);
1014
1015         let alice = &nodes[0];
1016         let alice_id = alice.node.get_our_node_id();
1017         let bob = &nodes[1];
1018         let bob_id = bob.node.get_our_node_id();
1019
1020         let absolute_expiry = Duration::from_secs(u64::MAX);
1021         let payment_id = PaymentId([1; 32]);
1022         let refund = bob.node
1023                 .create_refund_builder(10_000_000, absolute_expiry, payment_id, Retry::Attempts(0), None)
1024                 .unwrap()
1025                 .build().unwrap();
1026         assert_ne!(refund.payer_id(), bob_id);
1027         assert!(!refund.paths().is_empty());
1028         for path in refund.paths() {
1029                 assert_eq!(path.introduction_node, IntroductionNode::NodeId(alice_id));
1030         }
1031         expect_recent_payment!(bob, RecentPaymentDetails::AwaitingInvoice, payment_id);
1032
1033         let expected_invoice = alice.node.request_refund_payment(&refund).unwrap();
1034
1035         let onion_message = alice.onion_messenger.next_onion_message_for_peer(bob_id).unwrap();
1036
1037         let invoice = extract_invoice(bob, &onion_message);
1038         assert_eq!(invoice, expected_invoice);
1039         assert_ne!(invoice.signing_pubkey(), alice_id);
1040         assert!(!invoice.payment_paths().is_empty());
1041         for (_, path) in invoice.payment_paths() {
1042                 assert_eq!(path.introduction_node, IntroductionNode::NodeId(bob_id));
1043         }
1044 }
1045
1046 /// Fails creating or paying an offer when a blinded path cannot be created because no peers are
1047 /// connected.
1048 #[test]
1049 fn fails_creating_or_paying_for_offer_without_connected_peers() {
1050         let chanmon_cfgs = create_chanmon_cfgs(6);
1051         let node_cfgs = create_node_cfgs(6, &chanmon_cfgs);
1052         let node_chanmgrs = create_node_chanmgrs(6, &node_cfgs, &[None, None, None, None, None, None]);
1053         let nodes = create_network(6, &node_cfgs, &node_chanmgrs);
1054
1055         create_unannounced_chan_between_nodes_with_value(&nodes, 0, 1, 10_000_000, 1_000_000_000);
1056         create_unannounced_chan_between_nodes_with_value(&nodes, 2, 3, 10_000_000, 1_000_000_000);
1057         create_announced_chan_between_nodes_with_value(&nodes, 1, 2, 10_000_000, 1_000_000_000);
1058         create_announced_chan_between_nodes_with_value(&nodes, 1, 4, 10_000_000, 1_000_000_000);
1059         create_announced_chan_between_nodes_with_value(&nodes, 1, 5, 10_000_000, 1_000_000_000);
1060         create_announced_chan_between_nodes_with_value(&nodes, 2, 4, 10_000_000, 1_000_000_000);
1061         create_announced_chan_between_nodes_with_value(&nodes, 2, 5, 10_000_000, 1_000_000_000);
1062
1063         let (alice, bob, charlie, david) = (&nodes[0], &nodes[1], &nodes[2], &nodes[3]);
1064
1065         disconnect_peers(alice, &[bob, charlie, david, &nodes[4], &nodes[5]]);
1066         disconnect_peers(david, &[bob, charlie, &nodes[4], &nodes[5]]);
1067
1068         let absolute_expiry = alice.node.duration_since_epoch() + MAX_SHORT_LIVED_RELATIVE_EXPIRY;
1069         match alice.node.create_offer_builder(Some(absolute_expiry)) {
1070                 Ok(_) => panic!("Expected error"),
1071                 Err(e) => assert_eq!(e, Bolt12SemanticError::MissingPaths),
1072         }
1073
1074         let mut args = ReconnectArgs::new(alice, bob);
1075         args.send_channel_ready = (true, true);
1076         reconnect_nodes(args);
1077
1078         let offer = alice.node
1079                 .create_offer_builder(Some(absolute_expiry)).unwrap()
1080                 .amount_msats(10_000_000)
1081                 .build().unwrap();
1082
1083         let payment_id = PaymentId([1; 32]);
1084
1085         match david.node.pay_for_offer(&offer, None, None, None, payment_id, Retry::Attempts(0), None) {
1086                 Ok(_) => panic!("Expected error"),
1087                 Err(e) => assert_eq!(e, Bolt12SemanticError::MissingPaths),
1088         }
1089
1090         assert!(nodes[0].node.list_recent_payments().is_empty());
1091
1092         let mut args = ReconnectArgs::new(charlie, david);
1093         args.send_channel_ready = (true, true);
1094         reconnect_nodes(args);
1095
1096         assert!(
1097                 david.node.pay_for_offer(
1098                         &offer, None, None, None, payment_id, Retry::Attempts(0), None
1099                 ).is_ok()
1100         );
1101
1102         expect_recent_payment!(david, RecentPaymentDetails::AwaitingInvoice, payment_id);
1103 }
1104
1105 /// Fails creating or sending an invoice for a refund when a blinded path cannot be created because
1106 /// no peers are connected.
1107 #[test]
1108 fn fails_creating_refund_or_sending_invoice_without_connected_peers() {
1109         let mut accept_forward_cfg = test_default_channel_config();
1110         accept_forward_cfg.accept_forwards_to_priv_channels = true;
1111
1112         let mut features = channelmanager::provided_init_features(&accept_forward_cfg);
1113         features.set_onion_messages_optional();
1114         features.set_route_blinding_optional();
1115
1116         let chanmon_cfgs = create_chanmon_cfgs(6);
1117         let node_cfgs = create_node_cfgs(6, &chanmon_cfgs);
1118
1119         *node_cfgs[1].override_init_features.borrow_mut() = Some(features);
1120
1121         let node_chanmgrs = create_node_chanmgrs(
1122                 6, &node_cfgs, &[None, Some(accept_forward_cfg), None, None, None, None]
1123         );
1124         let nodes = create_network(6, &node_cfgs, &node_chanmgrs);
1125
1126         create_unannounced_chan_between_nodes_with_value(&nodes, 0, 1, 10_000_000, 1_000_000_000);
1127         create_unannounced_chan_between_nodes_with_value(&nodes, 2, 3, 10_000_000, 1_000_000_000);
1128         create_announced_chan_between_nodes_with_value(&nodes, 1, 2, 10_000_000, 1_000_000_000);
1129         create_announced_chan_between_nodes_with_value(&nodes, 1, 4, 10_000_000, 1_000_000_000);
1130         create_announced_chan_between_nodes_with_value(&nodes, 1, 5, 10_000_000, 1_000_000_000);
1131         create_announced_chan_between_nodes_with_value(&nodes, 2, 4, 10_000_000, 1_000_000_000);
1132         create_announced_chan_between_nodes_with_value(&nodes, 2, 5, 10_000_000, 1_000_000_000);
1133
1134         let (alice, bob, charlie, david) = (&nodes[0], &nodes[1], &nodes[2], &nodes[3]);
1135
1136         disconnect_peers(alice, &[bob, charlie, david, &nodes[4], &nodes[5]]);
1137         disconnect_peers(david, &[bob, charlie, &nodes[4], &nodes[5]]);
1138
1139         let absolute_expiry = david.node.duration_since_epoch() + MAX_SHORT_LIVED_RELATIVE_EXPIRY;
1140         let payment_id = PaymentId([1; 32]);
1141         match david.node.create_refund_builder(
1142                 10_000_000, absolute_expiry, payment_id, Retry::Attempts(0), None
1143         ) {
1144                 Ok(_) => panic!("Expected error"),
1145                 Err(e) => assert_eq!(e, Bolt12SemanticError::MissingPaths),
1146         }
1147
1148         let mut args = ReconnectArgs::new(charlie, david);
1149         args.send_channel_ready = (true, true);
1150         reconnect_nodes(args);
1151
1152         let refund = david.node
1153                 .create_refund_builder(10_000_000, absolute_expiry, payment_id, Retry::Attempts(0), None)
1154                 .unwrap()
1155                 .build().unwrap();
1156
1157         match alice.node.request_refund_payment(&refund) {
1158                 Ok(_) => panic!("Expected error"),
1159                 Err(e) => assert_eq!(e, Bolt12SemanticError::MissingPaths),
1160         }
1161
1162         let mut args = ReconnectArgs::new(alice, bob);
1163         args.send_channel_ready = (true, true);
1164         reconnect_nodes(args);
1165
1166         assert!(alice.node.request_refund_payment(&refund).is_ok());
1167 }
1168
1169 /// Fails creating an invoice request when the offer contains an unsupported chain.
1170 #[test]
1171 fn fails_creating_invoice_request_for_unsupported_chain() {
1172         let chanmon_cfgs = create_chanmon_cfgs(2);
1173         let node_cfgs = create_node_cfgs(2, &chanmon_cfgs);
1174         let node_chanmgrs = create_node_chanmgrs(2, &node_cfgs, &[None, None]);
1175         let nodes = create_network(2, &node_cfgs, &node_chanmgrs);
1176
1177         create_announced_chan_between_nodes_with_value(&nodes, 0, 1, 10_000_000, 1_000_000_000);
1178
1179         let alice = &nodes[0];
1180         let bob = &nodes[1];
1181
1182         let offer = alice.node
1183                 .create_offer_builder(None).unwrap()
1184                 .clear_chains()
1185                 .chain(Network::Signet)
1186                 .build().unwrap();
1187
1188         let payment_id = PaymentId([1; 32]);
1189         match bob.node.pay_for_offer(&offer, None, None, None, payment_id, Retry::Attempts(0), None) {
1190                 Ok(_) => panic!("Expected error"),
1191                 Err(e) => assert_eq!(e, Bolt12SemanticError::UnsupportedChain),
1192         }
1193 }
1194
1195 /// Fails requesting a payment when the refund contains an unsupported chain.
1196 #[test]
1197 fn fails_sending_invoice_with_unsupported_chain_for_refund() {
1198         let chanmon_cfgs = create_chanmon_cfgs(2);
1199         let node_cfgs = create_node_cfgs(2, &chanmon_cfgs);
1200         let node_chanmgrs = create_node_chanmgrs(2, &node_cfgs, &[None, None]);
1201         let nodes = create_network(2, &node_cfgs, &node_chanmgrs);
1202
1203         create_announced_chan_between_nodes_with_value(&nodes, 0, 1, 10_000_000, 1_000_000_000);
1204
1205         let alice = &nodes[0];
1206         let bob = &nodes[1];
1207
1208         let absolute_expiry = Duration::from_secs(u64::MAX);
1209         let payment_id = PaymentId([1; 32]);
1210         let refund = bob.node
1211                 .create_refund_builder(10_000_000, absolute_expiry, payment_id, Retry::Attempts(0), None)
1212                 .unwrap()
1213                 .chain(Network::Signet)
1214                 .build().unwrap();
1215
1216         match alice.node.request_refund_payment(&refund) {
1217                 Ok(_) => panic!("Expected error"),
1218                 Err(e) => assert_eq!(e, Bolt12SemanticError::UnsupportedChain),
1219         }
1220 }
1221
1222 /// Fails creating an invoice request when a blinded reply path cannot be created.
1223 #[test]
1224 fn fails_creating_invoice_request_without_blinded_reply_path() {
1225         let chanmon_cfgs = create_chanmon_cfgs(6);
1226         let node_cfgs = create_node_cfgs(6, &chanmon_cfgs);
1227         let node_chanmgrs = create_node_chanmgrs(6, &node_cfgs, &[None, None, None, None, None, None]);
1228         let nodes = create_network(6, &node_cfgs, &node_chanmgrs);
1229
1230         create_unannounced_chan_between_nodes_with_value(&nodes, 0, 1, 10_000_000, 1_000_000_000);
1231         create_unannounced_chan_between_nodes_with_value(&nodes, 2, 3, 10_000_000, 1_000_000_000);
1232         create_announced_chan_between_nodes_with_value(&nodes, 1, 2, 10_000_000, 1_000_000_000);
1233         create_announced_chan_between_nodes_with_value(&nodes, 1, 4, 10_000_000, 1_000_000_000);
1234         create_announced_chan_between_nodes_with_value(&nodes, 1, 5, 10_000_000, 1_000_000_000);
1235
1236         let (alice, bob, charlie, david) = (&nodes[0], &nodes[1], &nodes[2], &nodes[3]);
1237
1238         disconnect_peers(alice, &[charlie, david, &nodes[4], &nodes[5]]);
1239         disconnect_peers(david, &[bob, charlie, &nodes[4], &nodes[5]]);
1240
1241         let offer = alice.node
1242                 .create_offer_builder(None).unwrap()
1243                 .amount_msats(10_000_000)
1244                 .build().unwrap();
1245
1246         let payment_id = PaymentId([1; 32]);
1247
1248         match david.node.pay_for_offer(&offer, None, None, None, payment_id, Retry::Attempts(0), None) {
1249                 Ok(_) => panic!("Expected error"),
1250                 Err(e) => assert_eq!(e, Bolt12SemanticError::MissingPaths),
1251         }
1252
1253         assert!(nodes[0].node.list_recent_payments().is_empty());
1254 }
1255
1256 #[test]
1257 fn fails_creating_invoice_request_with_duplicate_payment_id() {
1258         let chanmon_cfgs = create_chanmon_cfgs(6);
1259         let node_cfgs = create_node_cfgs(6, &chanmon_cfgs);
1260         let node_chanmgrs = create_node_chanmgrs(6, &node_cfgs, &[None, None, None, None, None, None]);
1261         let nodes = create_network(6, &node_cfgs, &node_chanmgrs);
1262
1263         create_unannounced_chan_between_nodes_with_value(&nodes, 0, 1, 10_000_000, 1_000_000_000);
1264         create_unannounced_chan_between_nodes_with_value(&nodes, 2, 3, 10_000_000, 1_000_000_000);
1265         create_announced_chan_between_nodes_with_value(&nodes, 1, 2, 10_000_000, 1_000_000_000);
1266         create_announced_chan_between_nodes_with_value(&nodes, 1, 4, 10_000_000, 1_000_000_000);
1267         create_announced_chan_between_nodes_with_value(&nodes, 1, 5, 10_000_000, 1_000_000_000);
1268         create_announced_chan_between_nodes_with_value(&nodes, 2, 4, 10_000_000, 1_000_000_000);
1269         create_announced_chan_between_nodes_with_value(&nodes, 2, 5, 10_000_000, 1_000_000_000);
1270
1271         let (alice, _bob, charlie, david) = (&nodes[0], &nodes[1], &nodes[2], &nodes[3]);
1272
1273         disconnect_peers(alice, &[charlie, david, &nodes[4], &nodes[5]]);
1274
1275         let offer = alice.node
1276                 .create_offer_builder(None).unwrap()
1277                 .amount_msats(10_000_000)
1278                 .build().unwrap();
1279
1280         let payment_id = PaymentId([1; 32]);
1281         assert!(
1282                 david.node.pay_for_offer(
1283                         &offer, None, None, None, payment_id, Retry::Attempts(0), None
1284                 ).is_ok()
1285         );
1286         expect_recent_payment!(david, RecentPaymentDetails::AwaitingInvoice, payment_id);
1287
1288         match david.node.pay_for_offer(&offer, None, None, None, payment_id, Retry::Attempts(0), None) {
1289                 Ok(_) => panic!("Expected error"),
1290                 Err(e) => assert_eq!(e, Bolt12SemanticError::DuplicatePaymentId),
1291         }
1292
1293         expect_recent_payment!(david, RecentPaymentDetails::AwaitingInvoice, payment_id);
1294 }
1295
1296 #[test]
1297 fn fails_creating_refund_with_duplicate_payment_id() {
1298         let chanmon_cfgs = create_chanmon_cfgs(2);
1299         let node_cfgs = create_node_cfgs(2, &chanmon_cfgs);
1300         let node_chanmgrs = create_node_chanmgrs(2, &node_cfgs, &[None, None]);
1301         let nodes = create_network(2, &node_cfgs, &node_chanmgrs);
1302
1303         create_announced_chan_between_nodes_with_value(&nodes, 0, 1, 10_000_000, 1_000_000_000);
1304
1305         let absolute_expiry = Duration::from_secs(u64::MAX);
1306         let payment_id = PaymentId([1; 32]);
1307         assert!(
1308                 nodes[0].node.create_refund_builder(
1309                         10_000, absolute_expiry, payment_id, Retry::Attempts(0), None
1310                 ).is_ok()
1311         );
1312         expect_recent_payment!(nodes[0], RecentPaymentDetails::AwaitingInvoice, payment_id);
1313
1314         match nodes[0].node.create_refund_builder(
1315                 10_000, absolute_expiry, payment_id, Retry::Attempts(0), None
1316         ) {
1317                 Ok(_) => panic!("Expected error"),
1318                 Err(e) => assert_eq!(e, Bolt12SemanticError::DuplicatePaymentId),
1319         }
1320
1321         expect_recent_payment!(nodes[0], RecentPaymentDetails::AwaitingInvoice, payment_id);
1322 }
1323
1324 #[test]
1325 fn fails_sending_invoice_without_blinded_payment_paths_for_offer() {
1326         let mut accept_forward_cfg = test_default_channel_config();
1327         accept_forward_cfg.accept_forwards_to_priv_channels = true;
1328
1329         // Clearing route_blinding prevents forming any payment paths since the node is unannounced.
1330         let mut features = channelmanager::provided_init_features(&accept_forward_cfg);
1331         features.set_onion_messages_optional();
1332         features.clear_route_blinding();
1333
1334         let chanmon_cfgs = create_chanmon_cfgs(6);
1335         let node_cfgs = create_node_cfgs(6, &chanmon_cfgs);
1336
1337         *node_cfgs[1].override_init_features.borrow_mut() = Some(features);
1338
1339         let node_chanmgrs = create_node_chanmgrs(
1340                 6, &node_cfgs, &[None, Some(accept_forward_cfg), None, None, None, None]
1341         );
1342         let nodes = create_network(6, &node_cfgs, &node_chanmgrs);
1343
1344         create_unannounced_chan_between_nodes_with_value(&nodes, 0, 1, 10_000_000, 1_000_000_000);
1345         create_unannounced_chan_between_nodes_with_value(&nodes, 2, 3, 10_000_000, 1_000_000_000);
1346         create_announced_chan_between_nodes_with_value(&nodes, 1, 2, 10_000_000, 1_000_000_000);
1347         create_announced_chan_between_nodes_with_value(&nodes, 1, 4, 10_000_000, 1_000_000_000);
1348         create_announced_chan_between_nodes_with_value(&nodes, 1, 5, 10_000_000, 1_000_000_000);
1349         create_announced_chan_between_nodes_with_value(&nodes, 2, 4, 10_000_000, 1_000_000_000);
1350         create_announced_chan_between_nodes_with_value(&nodes, 2, 5, 10_000_000, 1_000_000_000);
1351
1352         let (alice, bob, charlie, david) = (&nodes[0], &nodes[1], &nodes[2], &nodes[3]);
1353         let alice_id = alice.node.get_our_node_id();
1354         let bob_id = bob.node.get_our_node_id();
1355         let charlie_id = charlie.node.get_our_node_id();
1356         let david_id = david.node.get_our_node_id();
1357
1358         disconnect_peers(alice, &[charlie, david, &nodes[4], &nodes[5]]);
1359         disconnect_peers(david, &[bob, &nodes[4], &nodes[5]]);
1360
1361         let offer = alice.node
1362                 .create_offer_builder(None).unwrap()
1363                 .amount_msats(10_000_000)
1364                 .build().unwrap();
1365
1366         let payment_id = PaymentId([1; 32]);
1367         david.node.pay_for_offer(&offer, None, None, None, payment_id, Retry::Attempts(0), None)
1368                 .unwrap();
1369
1370         connect_peers(david, bob);
1371
1372         let onion_message = david.onion_messenger.next_onion_message_for_peer(bob_id).unwrap();
1373         bob.onion_messenger.handle_onion_message(&david_id, &onion_message);
1374
1375         connect_peers(alice, charlie);
1376
1377         let onion_message = bob.onion_messenger.next_onion_message_for_peer(alice_id).unwrap();
1378         alice.onion_messenger.handle_onion_message(&bob_id, &onion_message);
1379
1380         let onion_message = alice.onion_messenger.next_onion_message_for_peer(charlie_id).unwrap();
1381         charlie.onion_messenger.handle_onion_message(&alice_id, &onion_message);
1382
1383         let onion_message = charlie.onion_messenger.next_onion_message_for_peer(david_id).unwrap();
1384         david.onion_messenger.handle_onion_message(&charlie_id, &onion_message);
1385
1386         let invoice_error = extract_invoice_error(david, &onion_message);
1387         assert_eq!(invoice_error, InvoiceError::from(Bolt12SemanticError::MissingPaths));
1388 }
1389
1390 #[test]
1391 fn fails_sending_invoice_without_blinded_payment_paths_for_refund() {
1392         let mut accept_forward_cfg = test_default_channel_config();
1393         accept_forward_cfg.accept_forwards_to_priv_channels = true;
1394
1395         // Clearing route_blinding prevents forming any payment paths since the node is unannounced.
1396         let mut features = channelmanager::provided_init_features(&accept_forward_cfg);
1397         features.set_onion_messages_optional();
1398         features.clear_route_blinding();
1399
1400         let chanmon_cfgs = create_chanmon_cfgs(6);
1401         let node_cfgs = create_node_cfgs(6, &chanmon_cfgs);
1402
1403         *node_cfgs[1].override_init_features.borrow_mut() = Some(features);
1404
1405         let node_chanmgrs = create_node_chanmgrs(
1406                 6, &node_cfgs, &[None, Some(accept_forward_cfg), None, None, None, None]
1407         );
1408         let nodes = create_network(6, &node_cfgs, &node_chanmgrs);
1409
1410         create_unannounced_chan_between_nodes_with_value(&nodes, 0, 1, 10_000_000, 1_000_000_000);
1411         create_unannounced_chan_between_nodes_with_value(&nodes, 2, 3, 10_000_000, 1_000_000_000);
1412         create_announced_chan_between_nodes_with_value(&nodes, 1, 2, 10_000_000, 1_000_000_000);
1413         create_announced_chan_between_nodes_with_value(&nodes, 1, 4, 10_000_000, 1_000_000_000);
1414         create_announced_chan_between_nodes_with_value(&nodes, 1, 5, 10_000_000, 1_000_000_000);
1415         create_announced_chan_between_nodes_with_value(&nodes, 2, 4, 10_000_000, 1_000_000_000);
1416         create_announced_chan_between_nodes_with_value(&nodes, 2, 5, 10_000_000, 1_000_000_000);
1417
1418         let (alice, bob, charlie, david) = (&nodes[0], &nodes[1], &nodes[2], &nodes[3]);
1419
1420         disconnect_peers(alice, &[charlie, david, &nodes[4], &nodes[5]]);
1421         disconnect_peers(david, &[bob, &nodes[4], &nodes[5]]);
1422
1423         let absolute_expiry = Duration::from_secs(u64::MAX);
1424         let payment_id = PaymentId([1; 32]);
1425         let refund = david.node
1426                 .create_refund_builder(10_000_000, absolute_expiry, payment_id, Retry::Attempts(0), None)
1427                 .unwrap()
1428                 .build().unwrap();
1429
1430         match alice.node.request_refund_payment(&refund) {
1431                 Ok(_) => panic!("Expected error"),
1432                 Err(e) => assert_eq!(e, Bolt12SemanticError::MissingPaths),
1433         }
1434 }
1435
1436 #[test]
1437 fn fails_paying_invoice_more_than_once() {
1438         let mut accept_forward_cfg = test_default_channel_config();
1439         accept_forward_cfg.accept_forwards_to_priv_channels = true;
1440
1441         let mut features = channelmanager::provided_init_features(&accept_forward_cfg);
1442         features.set_onion_messages_optional();
1443         features.set_route_blinding_optional();
1444
1445         let chanmon_cfgs = create_chanmon_cfgs(6);
1446         let node_cfgs = create_node_cfgs(6, &chanmon_cfgs);
1447
1448         *node_cfgs[1].override_init_features.borrow_mut() = Some(features);
1449
1450         let node_chanmgrs = create_node_chanmgrs(
1451                 6, &node_cfgs, &[None, Some(accept_forward_cfg), None, None, None, None]
1452         );
1453         let nodes = create_network(6, &node_cfgs, &node_chanmgrs);
1454
1455         create_unannounced_chan_between_nodes_with_value(&nodes, 0, 1, 10_000_000, 1_000_000_000);
1456         create_unannounced_chan_between_nodes_with_value(&nodes, 2, 3, 10_000_000, 1_000_000_000);
1457         create_announced_chan_between_nodes_with_value(&nodes, 1, 2, 10_000_000, 1_000_000_000);
1458         create_announced_chan_between_nodes_with_value(&nodes, 1, 4, 10_000_000, 1_000_000_000);
1459         create_announced_chan_between_nodes_with_value(&nodes, 1, 5, 10_000_000, 1_000_000_000);
1460         create_announced_chan_between_nodes_with_value(&nodes, 2, 4, 10_000_000, 1_000_000_000);
1461         create_announced_chan_between_nodes_with_value(&nodes, 2, 5, 10_000_000, 1_000_000_000);
1462
1463         let (alice, bob, charlie, david) = (&nodes[0], &nodes[1], &nodes[2], &nodes[3]);
1464         let alice_id = alice.node.get_our_node_id();
1465         let bob_id = bob.node.get_our_node_id();
1466         let charlie_id = charlie.node.get_our_node_id();
1467         let david_id = david.node.get_our_node_id();
1468
1469         disconnect_peers(alice, &[charlie, david, &nodes[4], &nodes[5]]);
1470         disconnect_peers(david, &[bob, &nodes[4], &nodes[5]]);
1471
1472         let absolute_expiry = Duration::from_secs(u64::MAX);
1473         let payment_id = PaymentId([1; 32]);
1474         let refund = david.node
1475                 .create_refund_builder(10_000_000, absolute_expiry, payment_id, Retry::Attempts(0), None)
1476                 .unwrap()
1477                 .build().unwrap();
1478         expect_recent_payment!(david, RecentPaymentDetails::AwaitingInvoice, payment_id);
1479
1480         // Alice sends the first invoice
1481         alice.node.request_refund_payment(&refund).unwrap();
1482
1483         connect_peers(alice, charlie);
1484
1485         let onion_message = alice.onion_messenger.next_onion_message_for_peer(charlie_id).unwrap();
1486         charlie.onion_messenger.handle_onion_message(&alice_id, &onion_message);
1487
1488         let onion_message = charlie.onion_messenger.next_onion_message_for_peer(david_id).unwrap();
1489         david.onion_messenger.handle_onion_message(&charlie_id, &onion_message);
1490
1491         // David pays the first invoice
1492         let payment_context = PaymentContext::Bolt12Refund(Bolt12RefundContext {});
1493         let invoice1 = extract_invoice(david, &onion_message);
1494
1495         route_bolt12_payment(david, &[charlie, bob, alice], &invoice1);
1496         expect_recent_payment!(david, RecentPaymentDetails::Pending, payment_id);
1497
1498         claim_bolt12_payment(david, &[charlie, bob, alice], payment_context);
1499         expect_recent_payment!(david, RecentPaymentDetails::Fulfilled, payment_id);
1500
1501         disconnect_peers(alice, &[charlie]);
1502
1503         // Alice sends the second invoice
1504         alice.node.request_refund_payment(&refund).unwrap();
1505
1506         connect_peers(alice, charlie);
1507         connect_peers(david, bob);
1508
1509         let onion_message = alice.onion_messenger.next_onion_message_for_peer(charlie_id).unwrap();
1510         charlie.onion_messenger.handle_onion_message(&alice_id, &onion_message);
1511
1512         let onion_message = charlie.onion_messenger.next_onion_message_for_peer(david_id).unwrap();
1513         david.onion_messenger.handle_onion_message(&charlie_id, &onion_message);
1514
1515         let invoice2 = extract_invoice(david, &onion_message);
1516         assert_eq!(invoice1.payer_metadata(), invoice2.payer_metadata());
1517
1518         // David sends an error instead of paying the second invoice
1519         let onion_message = david.onion_messenger.next_onion_message_for_peer(bob_id).unwrap();
1520         bob.onion_messenger.handle_onion_message(&david_id, &onion_message);
1521
1522         let onion_message = bob.onion_messenger.next_onion_message_for_peer(alice_id).unwrap();
1523         alice.onion_messenger.handle_onion_message(&bob_id, &onion_message);
1524
1525         let invoice_error = extract_invoice_error(alice, &onion_message);
1526         assert_eq!(invoice_error, InvoiceError::from_string("DuplicateInvoice".to_string()));
1527 }