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