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