1 // This file is Copyright its original authors, visible in version control
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
10 //! Functional tests for the BOLT 12 Offers payment flow.
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.
17 //! Two-node success tests use an announced channel:
21 //! While two-node failure tests use an unannounced channel:
25 //! Six-node tests use unannounced channels for the sender and recipient and announced channels for
26 //! the rest of the network.
32 //! Alice ... Bob -------- Charlie ... David
38 //! Unnamed nodes are needed to ensure unannounced nodes can create two-hop blinded paths.
40 //! Nodes without channels are disconnected and connected as needed to ensure that deterministic
41 //! blinded paths are used.
43 use bitcoin::network::constants::Network;
44 use core::time::Duration;
45 use crate::blinded_path::BlindedPath;
46 use crate::events::{Event, MessageSendEventsProvider, PaymentPurpose};
47 use crate::ln::channelmanager::{PaymentId, RecentPaymentDetails, Retry, self};
48 use crate::ln::functional_test_utils::*;
49 use crate::ln::msgs::{ChannelMessageHandler, Init, NodeAnnouncement, OnionMessage, OnionMessageHandler, RoutingMessageHandler, SocketAddress, UnsignedGossipMessage, UnsignedNodeAnnouncement};
50 use crate::offers::invoice::Bolt12Invoice;
51 use crate::offers::invoice_error::InvoiceError;
52 use crate::offers::invoice_request::InvoiceRequest;
53 use crate::offers::parse::Bolt12SemanticError;
54 use crate::onion_message::messenger::PeeledOnion;
55 use crate::onion_message::offers::OffersMessage;
56 use crate::onion_message::packet::ParsedOnionMessageContents;
57 use crate::routing::gossip::{NodeAlias, NodeId};
58 use crate::sign::{NodeSigner, Recipient};
60 use crate::prelude::*;
62 macro_rules! expect_recent_payment {
63 ($node: expr, $payment_state: path, $payment_id: expr) => {
64 match $node.node.list_recent_payments().first() {
65 Some(&$payment_state { payment_id: actual_payment_id, .. }) => {
66 assert_eq!($payment_id, actual_payment_id);
68 Some(_) => panic!("Unexpected recent payment state"),
69 None => panic!("No recent payments"),
74 fn connect_peers<'a, 'b, 'c>(node_a: &Node<'a, 'b, 'c>, node_b: &Node<'a, 'b, 'c>) {
75 let node_id_a = node_a.node.get_our_node_id();
76 let node_id_b = node_b.node.get_our_node_id();
79 features: node_a.init_features(&node_id_b),
81 remote_network_address: None,
84 features: node_b.init_features(&node_id_a),
86 remote_network_address: None,
89 node_a.node.peer_connected(&node_id_b, &init_b, true).unwrap();
90 node_b.node.peer_connected(&node_id_a, &init_a, false).unwrap();
91 node_a.onion_messenger.peer_connected(&node_id_b, &init_b, true).unwrap();
92 node_b.onion_messenger.peer_connected(&node_id_a, &init_a, false).unwrap();
95 fn disconnect_peers<'a, 'b, 'c>(node_a: &Node<'a, 'b, 'c>, peers: &[&Node<'a, 'b, 'c>]) {
97 node_a.node.peer_disconnected(&node_b.node.get_our_node_id());
98 node_b.node.peer_disconnected(&node_a.node.get_our_node_id());
99 node_a.onion_messenger.peer_disconnected(&node_b.node.get_our_node_id());
100 node_b.onion_messenger.peer_disconnected(&node_a.node.get_our_node_id());
104 fn announce_node_address<'a, 'b, 'c>(
105 node: &Node<'a, 'b, 'c>, peers: &[&Node<'a, 'b, 'c>], address: SocketAddress,
107 let features = node.onion_messenger.provided_node_features()
108 | node.gossip_sync.provided_node_features();
110 let announcement = UnsignedNodeAnnouncement {
113 node_id: NodeId::from_pubkey(&node.keys_manager.get_node_id(Recipient::Node).unwrap()),
115 alias: NodeAlias([0u8; 32]),
116 addresses: vec![address],
117 excess_address_data: Vec::new(),
118 excess_data: Vec::new(),
120 let signature = node.keys_manager.sign_gossip_message(
121 UnsignedGossipMessage::NodeAnnouncement(&announcement)
124 let msg = NodeAnnouncement {
126 contents: announcement
129 node.gossip_sync.handle_node_announcement(&msg).unwrap();
131 peer.gossip_sync.handle_node_announcement(&msg).unwrap();
135 fn route_bolt12_payment<'a, 'b, 'c>(
136 node: &Node<'a, 'b, 'c>, path: &[&Node<'a, 'b, 'c>], invoice: &Bolt12Invoice
138 // Monitor added when handling the invoice onion message.
139 check_added_monitors(node, 1);
141 let mut events = node.node.get_and_clear_pending_msg_events();
142 assert_eq!(events.len(), 1);
143 let ev = remove_first_msg_event_to_node(&path[0].node.get_our_node_id(), &mut events);
145 // Use a fake payment_hash and bypass checking for the PaymentClaimable event since the
146 // invoice contains the payment_hash but it was encrypted inside an onion message.
147 let amount_msats = invoice.amount_msats();
148 let payment_hash = invoice.payment_hash();
149 let args = PassAlongPathArgs::new(node, path, amount_msats, payment_hash, ev)
150 .without_clearing_recipient_events();
151 do_pass_along_path(args);
154 fn claim_bolt12_payment<'a, 'b, 'c>(node: &Node<'a, 'b, 'c>, path: &[&Node<'a, 'b, 'c>]) {
155 let recipient = &path[path.len() - 1];
156 match get_event!(recipient, Event::PaymentClaimable) {
157 Event::PaymentClaimable {
158 purpose: PaymentPurpose::InvoicePayment {
159 payment_preimage: Some(payment_preimage), ..
161 } => claim_payment(node, path, payment_preimage),
166 fn extract_invoice_request<'a, 'b, 'c>(
167 node: &Node<'a, 'b, 'c>, message: &OnionMessage
168 ) -> (InvoiceRequest, Option<BlindedPath>) {
169 match node.onion_messenger.peel_onion_message(message) {
170 Ok(PeeledOnion::Receive(message, _, reply_path)) => match message {
171 ParsedOnionMessageContents::Offers(offers_message) => match offers_message {
172 OffersMessage::InvoiceRequest(invoice_request) => (invoice_request, reply_path),
173 OffersMessage::Invoice(invoice) => panic!("Unexpected invoice: {:?}", invoice),
174 OffersMessage::InvoiceError(error) => panic!("Unexpected invoice_error: {:?}", error),
176 ParsedOnionMessageContents::Custom(message) => panic!("Unexpected custom message: {:?}", message),
178 Ok(PeeledOnion::Forward(_, _)) => panic!("Unexpected onion message forward"),
179 Err(e) => panic!("Failed to process onion message {:?}", e),
183 fn extract_invoice<'a, 'b, 'c>(node: &Node<'a, 'b, 'c>, message: &OnionMessage) -> Bolt12Invoice {
184 match node.onion_messenger.peel_onion_message(message) {
185 Ok(PeeledOnion::Receive(message, _, _)) => match message {
186 ParsedOnionMessageContents::Offers(offers_message) => match offers_message {
187 OffersMessage::InvoiceRequest(invoice_request) => panic!("Unexpected invoice_request: {:?}", invoice_request),
188 OffersMessage::Invoice(invoice) => invoice,
189 OffersMessage::InvoiceError(error) => panic!("Unexpected invoice_error: {:?}", error),
191 ParsedOnionMessageContents::Custom(message) => panic!("Unexpected custom message: {:?}", message),
193 Ok(PeeledOnion::Forward(_, _)) => panic!("Unexpected onion message forward"),
194 Err(e) => panic!("Failed to process onion message {:?}", e),
198 fn extract_invoice_error<'a, 'b, 'c>(
199 node: &Node<'a, 'b, 'c>, message: &OnionMessage
201 match node.onion_messenger.peel_onion_message(message) {
202 Ok(PeeledOnion::Receive(message, _, _)) => match message {
203 ParsedOnionMessageContents::Offers(offers_message) => match offers_message {
204 OffersMessage::InvoiceRequest(invoice_request) => panic!("Unexpected invoice_request: {:?}", invoice_request),
205 OffersMessage::Invoice(invoice) => panic!("Unexpected invoice: {:?}", invoice),
206 OffersMessage::InvoiceError(error) => error,
208 ParsedOnionMessageContents::Custom(message) => panic!("Unexpected custom message: {:?}", message),
210 Ok(PeeledOnion::Forward(_, _)) => panic!("Unexpected onion message forward"),
211 Err(e) => panic!("Failed to process onion message {:?}", e),
215 /// Checks that blinded paths without Tor-only nodes are preferred when constructing an offer.
217 fn prefers_non_tor_nodes_in_blinded_paths() {
218 let mut accept_forward_cfg = test_default_channel_config();
219 accept_forward_cfg.accept_forwards_to_priv_channels = true;
221 let mut features = channelmanager::provided_init_features(&accept_forward_cfg);
222 features.set_onion_messages_optional();
223 features.set_route_blinding_optional();
225 let chanmon_cfgs = create_chanmon_cfgs(6);
226 let node_cfgs = create_node_cfgs(6, &chanmon_cfgs);
228 *node_cfgs[1].override_init_features.borrow_mut() = Some(features);
230 let node_chanmgrs = create_node_chanmgrs(
231 6, &node_cfgs, &[None, Some(accept_forward_cfg), None, None, None, None]
233 let nodes = create_network(6, &node_cfgs, &node_chanmgrs);
235 create_unannounced_chan_between_nodes_with_value(&nodes, 0, 1, 10_000_000, 1_000_000_000);
236 create_unannounced_chan_between_nodes_with_value(&nodes, 2, 3, 10_000_000, 1_000_000_000);
237 create_announced_chan_between_nodes_with_value(&nodes, 1, 2, 10_000_000, 1_000_000_000);
238 create_announced_chan_between_nodes_with_value(&nodes, 1, 4, 10_000_000, 1_000_000_000);
239 create_announced_chan_between_nodes_with_value(&nodes, 1, 5, 10_000_000, 1_000_000_000);
240 create_announced_chan_between_nodes_with_value(&nodes, 2, 4, 10_000_000, 1_000_000_000);
241 create_announced_chan_between_nodes_with_value(&nodes, 2, 5, 10_000_000, 1_000_000_000);
243 // Add an extra channel so that more than one of Bob's peers have MIN_PEER_CHANNELS.
244 create_announced_chan_between_nodes_with_value(&nodes, 4, 5, 10_000_000, 1_000_000_000);
246 let (alice, bob, charlie, david) = (&nodes[0], &nodes[1], &nodes[2], &nodes[3]);
247 let bob_id = bob.node.get_our_node_id();
248 let charlie_id = charlie.node.get_our_node_id();
250 disconnect_peers(alice, &[charlie, david, &nodes[4], &nodes[5]]);
251 disconnect_peers(david, &[bob, &nodes[4], &nodes[5]]);
253 let tor = SocketAddress::OnionV2([255, 254, 253, 252, 251, 250, 249, 248, 247, 246, 38, 7]);
254 announce_node_address(charlie, &[alice, bob, david, &nodes[4], &nodes[5]], tor.clone());
257 .create_offer_builder("coffee".to_string()).unwrap()
258 .amount_msats(10_000_000)
260 assert_ne!(offer.signing_pubkey(), bob_id);
261 assert!(!offer.paths().is_empty());
262 for path in offer.paths() {
263 assert_ne!(path.introduction_node_id, bob_id);
264 assert_ne!(path.introduction_node_id, charlie_id);
267 // Use a one-hop blinded path when Bob is announced and all his peers are Tor-only.
268 announce_node_address(&nodes[4], &[alice, bob, charlie, david, &nodes[5]], tor.clone());
269 announce_node_address(&nodes[5], &[alice, bob, charlie, david, &nodes[4]], tor.clone());
272 .create_offer_builder("coffee".to_string()).unwrap()
273 .amount_msats(10_000_000)
275 assert_ne!(offer.signing_pubkey(), bob_id);
276 assert!(!offer.paths().is_empty());
277 for path in offer.paths() {
278 assert_eq!(path.introduction_node_id, bob_id);
282 /// Checks that blinded paths prefer an introduction node that is the most connected.
284 fn prefers_more_connected_nodes_in_blinded_paths() {
285 let mut accept_forward_cfg = test_default_channel_config();
286 accept_forward_cfg.accept_forwards_to_priv_channels = true;
288 let mut features = channelmanager::provided_init_features(&accept_forward_cfg);
289 features.set_onion_messages_optional();
290 features.set_route_blinding_optional();
292 let chanmon_cfgs = create_chanmon_cfgs(6);
293 let node_cfgs = create_node_cfgs(6, &chanmon_cfgs);
295 *node_cfgs[1].override_init_features.borrow_mut() = Some(features);
297 let node_chanmgrs = create_node_chanmgrs(
298 6, &node_cfgs, &[None, Some(accept_forward_cfg), None, None, None, None]
300 let nodes = create_network(6, &node_cfgs, &node_chanmgrs);
302 create_unannounced_chan_between_nodes_with_value(&nodes, 0, 1, 10_000_000, 1_000_000_000);
303 create_unannounced_chan_between_nodes_with_value(&nodes, 2, 3, 10_000_000, 1_000_000_000);
304 create_announced_chan_between_nodes_with_value(&nodes, 1, 2, 10_000_000, 1_000_000_000);
305 create_announced_chan_between_nodes_with_value(&nodes, 1, 4, 10_000_000, 1_000_000_000);
306 create_announced_chan_between_nodes_with_value(&nodes, 1, 5, 10_000_000, 1_000_000_000);
307 create_announced_chan_between_nodes_with_value(&nodes, 2, 4, 10_000_000, 1_000_000_000);
308 create_announced_chan_between_nodes_with_value(&nodes, 2, 5, 10_000_000, 1_000_000_000);
310 // Add extra channels so that more than one of Bob's peers have MIN_PEER_CHANNELS and one has
311 // more than the others.
312 create_announced_chan_between_nodes_with_value(&nodes, 0, 4, 10_000_000, 1_000_000_000);
313 create_announced_chan_between_nodes_with_value(&nodes, 3, 4, 10_000_000, 1_000_000_000);
315 let (alice, bob, charlie, david) = (&nodes[0], &nodes[1], &nodes[2], &nodes[3]);
316 let bob_id = bob.node.get_our_node_id();
318 disconnect_peers(alice, &[charlie, david, &nodes[4], &nodes[5]]);
319 disconnect_peers(david, &[bob, &nodes[4], &nodes[5]]);
322 .create_offer_builder("coffee".to_string()).unwrap()
323 .amount_msats(10_000_000)
325 assert_ne!(offer.signing_pubkey(), bob_id);
326 assert!(!offer.paths().is_empty());
327 for path in offer.paths() {
328 assert_eq!(path.introduction_node_id, nodes[4].node.get_our_node_id());
332 /// Checks that an offer can be paid through blinded paths and that ephemeral pubkeys are used
333 /// rather than exposing a node's pubkey.
335 fn creates_and_pays_for_offer_using_two_hop_blinded_path() {
336 let mut accept_forward_cfg = test_default_channel_config();
337 accept_forward_cfg.accept_forwards_to_priv_channels = true;
339 let mut features = channelmanager::provided_init_features(&accept_forward_cfg);
340 features.set_onion_messages_optional();
341 features.set_route_blinding_optional();
343 let chanmon_cfgs = create_chanmon_cfgs(6);
344 let node_cfgs = create_node_cfgs(6, &chanmon_cfgs);
346 *node_cfgs[1].override_init_features.borrow_mut() = Some(features);
348 let node_chanmgrs = create_node_chanmgrs(
349 6, &node_cfgs, &[None, Some(accept_forward_cfg), None, None, None, None]
351 let nodes = create_network(6, &node_cfgs, &node_chanmgrs);
353 create_unannounced_chan_between_nodes_with_value(&nodes, 0, 1, 10_000_000, 1_000_000_000);
354 create_unannounced_chan_between_nodes_with_value(&nodes, 2, 3, 10_000_000, 1_000_000_000);
355 create_announced_chan_between_nodes_with_value(&nodes, 1, 2, 10_000_000, 1_000_000_000);
356 create_announced_chan_between_nodes_with_value(&nodes, 1, 4, 10_000_000, 1_000_000_000);
357 create_announced_chan_between_nodes_with_value(&nodes, 1, 5, 10_000_000, 1_000_000_000);
358 create_announced_chan_between_nodes_with_value(&nodes, 2, 4, 10_000_000, 1_000_000_000);
359 create_announced_chan_between_nodes_with_value(&nodes, 2, 5, 10_000_000, 1_000_000_000);
361 let (alice, bob, charlie, david) = (&nodes[0], &nodes[1], &nodes[2], &nodes[3]);
362 let alice_id = alice.node.get_our_node_id();
363 let bob_id = bob.node.get_our_node_id();
364 let charlie_id = charlie.node.get_our_node_id();
365 let david_id = david.node.get_our_node_id();
367 disconnect_peers(alice, &[charlie, david, &nodes[4], &nodes[5]]);
368 disconnect_peers(david, &[bob, &nodes[4], &nodes[5]]);
370 let offer = alice.node
371 .create_offer_builder("coffee".to_string()).unwrap()
372 .amount_msats(10_000_000)
374 assert_ne!(offer.signing_pubkey(), alice_id);
375 assert!(!offer.paths().is_empty());
376 for path in offer.paths() {
377 assert_eq!(path.introduction_node_id, bob_id);
380 let payment_id = PaymentId([1; 32]);
381 david.node.pay_for_offer(&offer, None, None, None, payment_id, Retry::Attempts(0), None)
383 expect_recent_payment!(david, RecentPaymentDetails::AwaitingInvoice, payment_id);
385 connect_peers(david, bob);
387 let onion_message = david.onion_messenger.next_onion_message_for_peer(bob_id).unwrap();
388 bob.onion_messenger.handle_onion_message(&david_id, &onion_message);
390 connect_peers(alice, charlie);
392 let onion_message = bob.onion_messenger.next_onion_message_for_peer(alice_id).unwrap();
393 alice.onion_messenger.handle_onion_message(&bob_id, &onion_message);
395 let (invoice_request, reply_path) = extract_invoice_request(alice, &onion_message);
396 assert_eq!(invoice_request.amount_msats(), None);
397 assert_ne!(invoice_request.payer_id(), david_id);
398 assert_eq!(reply_path.unwrap().introduction_node_id, charlie_id);
400 let onion_message = alice.onion_messenger.next_onion_message_for_peer(charlie_id).unwrap();
401 charlie.onion_messenger.handle_onion_message(&alice_id, &onion_message);
403 let onion_message = charlie.onion_messenger.next_onion_message_for_peer(david_id).unwrap();
404 david.onion_messenger.handle_onion_message(&charlie_id, &onion_message);
406 let invoice = extract_invoice(david, &onion_message);
407 assert_eq!(invoice.amount_msats(), 10_000_000);
408 assert_ne!(invoice.signing_pubkey(), alice_id);
409 assert!(!invoice.payment_paths().is_empty());
410 for (_, path) in invoice.payment_paths() {
411 assert_eq!(path.introduction_node_id, bob_id);
414 route_bolt12_payment(david, &[charlie, bob, alice], &invoice);
415 expect_recent_payment!(david, RecentPaymentDetails::Pending, payment_id);
417 claim_bolt12_payment(david, &[charlie, bob, alice]);
418 expect_recent_payment!(david, RecentPaymentDetails::Fulfilled, payment_id);
421 /// Checks that a refund can be paid through blinded paths and that ephemeral pubkeys are used
422 /// rather than exposing a node's pubkey.
424 fn creates_and_pays_for_refund_using_two_hop_blinded_path() {
425 let mut accept_forward_cfg = test_default_channel_config();
426 accept_forward_cfg.accept_forwards_to_priv_channels = true;
428 let mut features = channelmanager::provided_init_features(&accept_forward_cfg);
429 features.set_onion_messages_optional();
430 features.set_route_blinding_optional();
432 let chanmon_cfgs = create_chanmon_cfgs(6);
433 let node_cfgs = create_node_cfgs(6, &chanmon_cfgs);
435 *node_cfgs[1].override_init_features.borrow_mut() = Some(features);
437 let node_chanmgrs = create_node_chanmgrs(
438 6, &node_cfgs, &[None, Some(accept_forward_cfg), None, None, None, None]
440 let nodes = create_network(6, &node_cfgs, &node_chanmgrs);
442 create_unannounced_chan_between_nodes_with_value(&nodes, 0, 1, 10_000_000, 1_000_000_000);
443 create_unannounced_chan_between_nodes_with_value(&nodes, 2, 3, 10_000_000, 1_000_000_000);
444 create_announced_chan_between_nodes_with_value(&nodes, 1, 2, 10_000_000, 1_000_000_000);
445 create_announced_chan_between_nodes_with_value(&nodes, 1, 4, 10_000_000, 1_000_000_000);
446 create_announced_chan_between_nodes_with_value(&nodes, 1, 5, 10_000_000, 1_000_000_000);
447 create_announced_chan_between_nodes_with_value(&nodes, 2, 4, 10_000_000, 1_000_000_000);
448 create_announced_chan_between_nodes_with_value(&nodes, 2, 5, 10_000_000, 1_000_000_000);
450 let (alice, bob, charlie, david) = (&nodes[0], &nodes[1], &nodes[2], &nodes[3]);
451 let alice_id = alice.node.get_our_node_id();
452 let bob_id = bob.node.get_our_node_id();
453 let charlie_id = charlie.node.get_our_node_id();
454 let david_id = david.node.get_our_node_id();
456 disconnect_peers(alice, &[charlie, david, &nodes[4], &nodes[5]]);
457 disconnect_peers(david, &[bob, &nodes[4], &nodes[5]]);
459 let absolute_expiry = Duration::from_secs(u64::MAX);
460 let payment_id = PaymentId([1; 32]);
461 let refund = david.node
462 .create_refund_builder(
463 "refund".to_string(), 10_000_000, absolute_expiry, payment_id, Retry::Attempts(0), None
467 assert_eq!(refund.amount_msats(), 10_000_000);
468 assert_eq!(refund.absolute_expiry(), Some(absolute_expiry));
469 assert_ne!(refund.payer_id(), david_id);
470 assert!(!refund.paths().is_empty());
471 for path in refund.paths() {
472 assert_eq!(path.introduction_node_id, charlie_id);
474 expect_recent_payment!(david, RecentPaymentDetails::AwaitingInvoice, payment_id);
476 alice.node.request_refund_payment(&refund).unwrap();
478 connect_peers(alice, charlie);
480 let onion_message = alice.onion_messenger.next_onion_message_for_peer(charlie_id).unwrap();
481 charlie.onion_messenger.handle_onion_message(&alice_id, &onion_message);
483 let onion_message = charlie.onion_messenger.next_onion_message_for_peer(david_id).unwrap();
484 david.onion_messenger.handle_onion_message(&charlie_id, &onion_message);
486 let invoice = extract_invoice(david, &onion_message);
487 assert_eq!(invoice.amount_msats(), 10_000_000);
488 assert_ne!(invoice.signing_pubkey(), alice_id);
489 assert!(!invoice.payment_paths().is_empty());
490 for (_, path) in invoice.payment_paths() {
491 assert_eq!(path.introduction_node_id, bob_id);
494 route_bolt12_payment(david, &[charlie, bob, alice], &invoice);
495 expect_recent_payment!(david, RecentPaymentDetails::Pending, payment_id);
497 claim_bolt12_payment(david, &[charlie, bob, alice]);
498 expect_recent_payment!(david, RecentPaymentDetails::Fulfilled, payment_id);
501 /// Checks that an offer can be paid through a one-hop blinded path and that ephemeral pubkeys are
502 /// used rather than exposing a node's pubkey. However, the node's pubkey is still used as the
503 /// introduction node of the blinded path.
505 fn creates_and_pays_for_offer_using_one_hop_blinded_path() {
506 let chanmon_cfgs = create_chanmon_cfgs(2);
507 let node_cfgs = create_node_cfgs(2, &chanmon_cfgs);
508 let node_chanmgrs = create_node_chanmgrs(2, &node_cfgs, &[None, None]);
509 let nodes = create_network(2, &node_cfgs, &node_chanmgrs);
511 create_announced_chan_between_nodes_with_value(&nodes, 0, 1, 10_000_000, 1_000_000_000);
513 let alice = &nodes[0];
514 let alice_id = alice.node.get_our_node_id();
516 let bob_id = bob.node.get_our_node_id();
518 let offer = alice.node
519 .create_offer_builder("coffee".to_string()).unwrap()
520 .amount_msats(10_000_000)
522 assert_ne!(offer.signing_pubkey(), alice_id);
523 assert!(!offer.paths().is_empty());
524 for path in offer.paths() {
525 assert_eq!(path.introduction_node_id, alice_id);
528 let payment_id = PaymentId([1; 32]);
529 bob.node.pay_for_offer(&offer, None, None, None, payment_id, Retry::Attempts(0), None).unwrap();
530 expect_recent_payment!(bob, RecentPaymentDetails::AwaitingInvoice, payment_id);
532 let onion_message = bob.onion_messenger.next_onion_message_for_peer(alice_id).unwrap();
533 alice.onion_messenger.handle_onion_message(&bob_id, &onion_message);
535 let (invoice_request, reply_path) = extract_invoice_request(alice, &onion_message);
536 assert_eq!(invoice_request.amount_msats(), None);
537 assert_ne!(invoice_request.payer_id(), bob_id);
538 assert_eq!(reply_path.unwrap().introduction_node_id, bob_id);
540 let onion_message = alice.onion_messenger.next_onion_message_for_peer(bob_id).unwrap();
541 bob.onion_messenger.handle_onion_message(&alice_id, &onion_message);
543 let invoice = extract_invoice(bob, &onion_message);
544 assert_eq!(invoice.amount_msats(), 10_000_000);
545 assert_ne!(invoice.signing_pubkey(), alice_id);
546 assert!(!invoice.payment_paths().is_empty());
547 for (_, path) in invoice.payment_paths() {
548 assert_eq!(path.introduction_node_id, alice_id);
551 route_bolt12_payment(bob, &[alice], &invoice);
552 expect_recent_payment!(bob, RecentPaymentDetails::Pending, payment_id);
554 claim_bolt12_payment(bob, &[alice]);
555 expect_recent_payment!(bob, RecentPaymentDetails::Fulfilled, payment_id);
558 /// Checks that a refund can be paid through a one-hop blinded path and that ephemeral pubkeys are
559 /// used rather than exposing a node's pubkey. However, the node's pubkey is still used as the
560 /// introduction node of the blinded path.
562 fn creates_and_pays_for_refund_using_one_hop_blinded_path() {
563 let chanmon_cfgs = create_chanmon_cfgs(2);
564 let node_cfgs = create_node_cfgs(2, &chanmon_cfgs);
565 let node_chanmgrs = create_node_chanmgrs(2, &node_cfgs, &[None, None]);
566 let nodes = create_network(2, &node_cfgs, &node_chanmgrs);
568 create_announced_chan_between_nodes_with_value(&nodes, 0, 1, 10_000_000, 1_000_000_000);
570 let alice = &nodes[0];
571 let alice_id = alice.node.get_our_node_id();
573 let bob_id = bob.node.get_our_node_id();
575 let absolute_expiry = Duration::from_secs(u64::MAX);
576 let payment_id = PaymentId([1; 32]);
577 let refund = bob.node
578 .create_refund_builder(
579 "refund".to_string(), 10_000_000, absolute_expiry, payment_id, Retry::Attempts(0), None
583 assert_eq!(refund.amount_msats(), 10_000_000);
584 assert_eq!(refund.absolute_expiry(), Some(absolute_expiry));
585 assert_ne!(refund.payer_id(), bob_id);
586 assert!(!refund.paths().is_empty());
587 for path in refund.paths() {
588 assert_eq!(path.introduction_node_id, bob_id);
590 expect_recent_payment!(bob, RecentPaymentDetails::AwaitingInvoice, payment_id);
592 alice.node.request_refund_payment(&refund).unwrap();
594 let onion_message = alice.onion_messenger.next_onion_message_for_peer(bob_id).unwrap();
595 bob.onion_messenger.handle_onion_message(&alice_id, &onion_message);
597 let invoice = extract_invoice(bob, &onion_message);
598 assert_eq!(invoice.amount_msats(), 10_000_000);
599 assert_ne!(invoice.signing_pubkey(), alice_id);
600 assert!(!invoice.payment_paths().is_empty());
601 for (_, path) in invoice.payment_paths() {
602 assert_eq!(path.introduction_node_id, alice_id);
605 route_bolt12_payment(bob, &[alice], &invoice);
606 expect_recent_payment!(bob, RecentPaymentDetails::Pending, payment_id);
608 claim_bolt12_payment(bob, &[alice]);
609 expect_recent_payment!(bob, RecentPaymentDetails::Fulfilled, payment_id);
612 /// Checks that an invoice for an offer without any blinded paths can be requested. Note that while
613 /// the requested is sent directly using the node's pubkey, the response and the payment still use
614 /// blinded paths as required by the spec.
616 fn pays_for_offer_without_blinded_paths() {
617 let chanmon_cfgs = create_chanmon_cfgs(2);
618 let node_cfgs = create_node_cfgs(2, &chanmon_cfgs);
619 let node_chanmgrs = create_node_chanmgrs(2, &node_cfgs, &[None, None]);
620 let nodes = create_network(2, &node_cfgs, &node_chanmgrs);
622 create_announced_chan_between_nodes_with_value(&nodes, 0, 1, 10_000_000, 1_000_000_000);
624 let alice = &nodes[0];
625 let alice_id = alice.node.get_our_node_id();
627 let bob_id = bob.node.get_our_node_id();
629 let offer = alice.node
630 .create_offer_builder("coffee".to_string()).unwrap()
632 .amount_msats(10_000_000)
634 assert_eq!(offer.signing_pubkey(), alice_id);
635 assert!(offer.paths().is_empty());
637 let payment_id = PaymentId([1; 32]);
638 bob.node.pay_for_offer(&offer, None, None, None, payment_id, Retry::Attempts(0), None).unwrap();
639 expect_recent_payment!(bob, RecentPaymentDetails::AwaitingInvoice, payment_id);
641 let onion_message = bob.onion_messenger.next_onion_message_for_peer(alice_id).unwrap();
642 alice.onion_messenger.handle_onion_message(&bob_id, &onion_message);
644 let onion_message = alice.onion_messenger.next_onion_message_for_peer(bob_id).unwrap();
645 bob.onion_messenger.handle_onion_message(&alice_id, &onion_message);
647 let invoice = extract_invoice(bob, &onion_message);
648 route_bolt12_payment(bob, &[alice], &invoice);
649 expect_recent_payment!(bob, RecentPaymentDetails::Pending, payment_id);
651 claim_bolt12_payment(bob, &[alice]);
652 expect_recent_payment!(bob, RecentPaymentDetails::Fulfilled, payment_id);
655 /// Checks that a refund without any blinded paths can be paid. Note that while the invoice is sent
656 /// directly using the node's pubkey, the payment still use blinded paths as required by the spec.
658 fn pays_for_refund_without_blinded_paths() {
659 let chanmon_cfgs = create_chanmon_cfgs(2);
660 let node_cfgs = create_node_cfgs(2, &chanmon_cfgs);
661 let node_chanmgrs = create_node_chanmgrs(2, &node_cfgs, &[None, None]);
662 let nodes = create_network(2, &node_cfgs, &node_chanmgrs);
664 create_announced_chan_between_nodes_with_value(&nodes, 0, 1, 10_000_000, 1_000_000_000);
666 let alice = &nodes[0];
667 let alice_id = alice.node.get_our_node_id();
669 let bob_id = bob.node.get_our_node_id();
671 let absolute_expiry = Duration::from_secs(u64::MAX);
672 let payment_id = PaymentId([1; 32]);
673 let refund = bob.node
674 .create_refund_builder(
675 "refund".to_string(), 10_000_000, absolute_expiry, payment_id, Retry::Attempts(0), None
680 assert_eq!(refund.payer_id(), bob_id);
681 assert!(refund.paths().is_empty());
682 expect_recent_payment!(bob, RecentPaymentDetails::AwaitingInvoice, payment_id);
684 alice.node.request_refund_payment(&refund).unwrap();
686 let onion_message = alice.onion_messenger.next_onion_message_for_peer(bob_id).unwrap();
687 bob.onion_messenger.handle_onion_message(&alice_id, &onion_message);
689 let invoice = extract_invoice(bob, &onion_message);
690 route_bolt12_payment(bob, &[alice], &invoice);
691 expect_recent_payment!(bob, RecentPaymentDetails::Pending, payment_id);
693 claim_bolt12_payment(bob, &[alice]);
694 expect_recent_payment!(bob, RecentPaymentDetails::Fulfilled, payment_id);
697 /// Fails creating an offer when a blinded path cannot be created without exposing the node's id.
699 fn fails_creating_offer_without_blinded_paths() {
700 let chanmon_cfgs = create_chanmon_cfgs(2);
701 let node_cfgs = create_node_cfgs(2, &chanmon_cfgs);
702 let node_chanmgrs = create_node_chanmgrs(2, &node_cfgs, &[None, None]);
703 let nodes = create_network(2, &node_cfgs, &node_chanmgrs);
705 create_unannounced_chan_between_nodes_with_value(&nodes, 0, 1, 10_000_000, 1_000_000_000);
707 match nodes[0].node.create_offer_builder("coffee".to_string()) {
708 Ok(_) => panic!("Expected error"),
709 Err(e) => assert_eq!(e, Bolt12SemanticError::MissingPaths),
713 /// Fails creating a refund when a blinded path cannot be created without exposing the node's id.
715 fn fails_creating_refund_without_blinded_paths() {
716 let chanmon_cfgs = create_chanmon_cfgs(2);
717 let node_cfgs = create_node_cfgs(2, &chanmon_cfgs);
718 let node_chanmgrs = create_node_chanmgrs(2, &node_cfgs, &[None, None]);
719 let nodes = create_network(2, &node_cfgs, &node_chanmgrs);
721 create_unannounced_chan_between_nodes_with_value(&nodes, 0, 1, 10_000_000, 1_000_000_000);
723 let absolute_expiry = Duration::from_secs(u64::MAX);
724 let payment_id = PaymentId([1; 32]);
726 match nodes[0].node.create_refund_builder(
727 "refund".to_string(), 10_000, absolute_expiry, payment_id, Retry::Attempts(0), None
729 Ok(_) => panic!("Expected error"),
730 Err(e) => assert_eq!(e, Bolt12SemanticError::MissingPaths),
733 assert!(nodes[0].node.list_recent_payments().is_empty());
736 /// Fails creating an invoice request when the offer contains an unsupported chain.
738 fn fails_creating_invoice_request_for_unsupported_chain() {
739 let chanmon_cfgs = create_chanmon_cfgs(2);
740 let node_cfgs = create_node_cfgs(2, &chanmon_cfgs);
741 let node_chanmgrs = create_node_chanmgrs(2, &node_cfgs, &[None, None]);
742 let nodes = create_network(2, &node_cfgs, &node_chanmgrs);
744 create_announced_chan_between_nodes_with_value(&nodes, 0, 1, 10_000_000, 1_000_000_000);
746 let alice = &nodes[0];
749 let offer = alice.node
750 .create_offer_builder("coffee".to_string()).unwrap()
752 .chain(Network::Signet)
755 let payment_id = PaymentId([1; 32]);
756 match bob.node.pay_for_offer(&offer, None, None, None, payment_id, Retry::Attempts(0), None) {
757 Ok(_) => panic!("Expected error"),
758 Err(e) => assert_eq!(e, Bolt12SemanticError::UnsupportedChain),
762 /// Fails requesting a payment when the refund contains an unsupported chain.
764 fn fails_sending_invoice_with_unsupported_chain_for_refund() {
765 let chanmon_cfgs = create_chanmon_cfgs(2);
766 let node_cfgs = create_node_cfgs(2, &chanmon_cfgs);
767 let node_chanmgrs = create_node_chanmgrs(2, &node_cfgs, &[None, None]);
768 let nodes = create_network(2, &node_cfgs, &node_chanmgrs);
770 create_announced_chan_between_nodes_with_value(&nodes, 0, 1, 10_000_000, 1_000_000_000);
772 let alice = &nodes[0];
775 let absolute_expiry = Duration::from_secs(u64::MAX);
776 let payment_id = PaymentId([1; 32]);
777 let refund = bob.node
778 .create_refund_builder(
779 "refund".to_string(), 10_000_000, absolute_expiry, payment_id, Retry::Attempts(0), None
782 .chain(Network::Signet)
785 match alice.node.request_refund_payment(&refund) {
786 Ok(_) => panic!("Expected error"),
787 Err(e) => assert_eq!(e, Bolt12SemanticError::UnsupportedChain),
791 /// Fails creating an invoice request when a blinded reply path cannot be created without exposing
794 fn fails_creating_invoice_request_without_blinded_reply_path() {
795 let chanmon_cfgs = create_chanmon_cfgs(6);
796 let node_cfgs = create_node_cfgs(6, &chanmon_cfgs);
797 let node_chanmgrs = create_node_chanmgrs(6, &node_cfgs, &[None, None, None, None, None, None]);
798 let nodes = create_network(6, &node_cfgs, &node_chanmgrs);
800 create_unannounced_chan_between_nodes_with_value(&nodes, 0, 1, 10_000_000, 1_000_000_000);
801 create_unannounced_chan_between_nodes_with_value(&nodes, 2, 3, 10_000_000, 1_000_000_000);
802 create_announced_chan_between_nodes_with_value(&nodes, 1, 2, 10_000_000, 1_000_000_000);
803 create_announced_chan_between_nodes_with_value(&nodes, 1, 4, 10_000_000, 1_000_000_000);
804 create_announced_chan_between_nodes_with_value(&nodes, 1, 5, 10_000_000, 1_000_000_000);
806 let (alice, bob, charlie, david) = (&nodes[0], &nodes[1], &nodes[2], &nodes[3]);
808 disconnect_peers(alice, &[charlie, david, &nodes[4], &nodes[5]]);
809 disconnect_peers(david, &[bob, &nodes[4], &nodes[5]]);
811 let offer = alice.node
812 .create_offer_builder("coffee".to_string()).unwrap()
813 .amount_msats(10_000_000)
816 let payment_id = PaymentId([1; 32]);
818 match david.node.pay_for_offer(&offer, None, None, None, payment_id, Retry::Attempts(0), None) {
819 Ok(_) => panic!("Expected error"),
820 Err(e) => assert_eq!(e, Bolt12SemanticError::MissingPaths),
823 assert!(nodes[0].node.list_recent_payments().is_empty());
827 fn fails_creating_invoice_request_with_duplicate_payment_id() {
828 let chanmon_cfgs = create_chanmon_cfgs(6);
829 let node_cfgs = create_node_cfgs(6, &chanmon_cfgs);
830 let node_chanmgrs = create_node_chanmgrs(6, &node_cfgs, &[None, None, None, None, None, None]);
831 let nodes = create_network(6, &node_cfgs, &node_chanmgrs);
833 create_unannounced_chan_between_nodes_with_value(&nodes, 0, 1, 10_000_000, 1_000_000_000);
834 create_unannounced_chan_between_nodes_with_value(&nodes, 2, 3, 10_000_000, 1_000_000_000);
835 create_announced_chan_between_nodes_with_value(&nodes, 1, 2, 10_000_000, 1_000_000_000);
836 create_announced_chan_between_nodes_with_value(&nodes, 1, 4, 10_000_000, 1_000_000_000);
837 create_announced_chan_between_nodes_with_value(&nodes, 1, 5, 10_000_000, 1_000_000_000);
838 create_announced_chan_between_nodes_with_value(&nodes, 2, 4, 10_000_000, 1_000_000_000);
839 create_announced_chan_between_nodes_with_value(&nodes, 2, 5, 10_000_000, 1_000_000_000);
841 let (alice, _bob, charlie, david) = (&nodes[0], &nodes[1], &nodes[2], &nodes[3]);
843 disconnect_peers(alice, &[charlie, david, &nodes[4], &nodes[5]]);
845 let offer = alice.node
846 .create_offer_builder("coffee".to_string()).unwrap()
847 .amount_msats(10_000_000)
850 let payment_id = PaymentId([1; 32]);
852 david.node.pay_for_offer(
853 &offer, None, None, None, payment_id, Retry::Attempts(0), None
856 expect_recent_payment!(david, RecentPaymentDetails::AwaitingInvoice, payment_id);
858 match david.node.pay_for_offer(&offer, None, None, None, payment_id, Retry::Attempts(0), None) {
859 Ok(_) => panic!("Expected error"),
860 Err(e) => assert_eq!(e, Bolt12SemanticError::DuplicatePaymentId),
863 expect_recent_payment!(david, RecentPaymentDetails::AwaitingInvoice, payment_id);
867 fn fails_creating_refund_with_duplicate_payment_id() {
868 let chanmon_cfgs = create_chanmon_cfgs(2);
869 let node_cfgs = create_node_cfgs(2, &chanmon_cfgs);
870 let node_chanmgrs = create_node_chanmgrs(2, &node_cfgs, &[None, None]);
871 let nodes = create_network(2, &node_cfgs, &node_chanmgrs);
873 create_announced_chan_between_nodes_with_value(&nodes, 0, 1, 10_000_000, 1_000_000_000);
875 let absolute_expiry = Duration::from_secs(u64::MAX);
876 let payment_id = PaymentId([1; 32]);
878 nodes[0].node.create_refund_builder(
879 "refund".to_string(), 10_000, absolute_expiry, payment_id, Retry::Attempts(0), None
882 expect_recent_payment!(nodes[0], RecentPaymentDetails::AwaitingInvoice, payment_id);
884 match nodes[0].node.create_refund_builder(
885 "refund".to_string(), 10_000, absolute_expiry, payment_id, Retry::Attempts(0), None
887 Ok(_) => panic!("Expected error"),
888 Err(e) => assert_eq!(e, Bolt12SemanticError::DuplicatePaymentId),
891 expect_recent_payment!(nodes[0], RecentPaymentDetails::AwaitingInvoice, payment_id);
895 fn fails_sending_invoice_without_blinded_payment_paths_for_offer() {
896 let mut accept_forward_cfg = test_default_channel_config();
897 accept_forward_cfg.accept_forwards_to_priv_channels = true;
899 // Clearing route_blinding prevents forming any payment paths since the node is unannounced.
900 let mut features = channelmanager::provided_init_features(&accept_forward_cfg);
901 features.set_onion_messages_optional();
902 features.clear_route_blinding();
904 let chanmon_cfgs = create_chanmon_cfgs(6);
905 let node_cfgs = create_node_cfgs(6, &chanmon_cfgs);
907 *node_cfgs[1].override_init_features.borrow_mut() = Some(features);
909 let node_chanmgrs = create_node_chanmgrs(
910 6, &node_cfgs, &[None, Some(accept_forward_cfg), None, None, None, None]
912 let nodes = create_network(6, &node_cfgs, &node_chanmgrs);
914 create_unannounced_chan_between_nodes_with_value(&nodes, 0, 1, 10_000_000, 1_000_000_000);
915 create_unannounced_chan_between_nodes_with_value(&nodes, 2, 3, 10_000_000, 1_000_000_000);
916 create_announced_chan_between_nodes_with_value(&nodes, 1, 2, 10_000_000, 1_000_000_000);
917 create_announced_chan_between_nodes_with_value(&nodes, 1, 4, 10_000_000, 1_000_000_000);
918 create_announced_chan_between_nodes_with_value(&nodes, 1, 5, 10_000_000, 1_000_000_000);
919 create_announced_chan_between_nodes_with_value(&nodes, 2, 4, 10_000_000, 1_000_000_000);
920 create_announced_chan_between_nodes_with_value(&nodes, 2, 5, 10_000_000, 1_000_000_000);
922 let (alice, bob, charlie, david) = (&nodes[0], &nodes[1], &nodes[2], &nodes[3]);
923 let alice_id = alice.node.get_our_node_id();
924 let bob_id = bob.node.get_our_node_id();
925 let charlie_id = charlie.node.get_our_node_id();
926 let david_id = david.node.get_our_node_id();
928 disconnect_peers(alice, &[charlie, david, &nodes[4], &nodes[5]]);
929 disconnect_peers(david, &[bob, &nodes[4], &nodes[5]]);
931 let offer = alice.node
932 .create_offer_builder("coffee".to_string()).unwrap()
933 .amount_msats(10_000_000)
936 let payment_id = PaymentId([1; 32]);
937 david.node.pay_for_offer(&offer, None, None, None, payment_id, Retry::Attempts(0), None)
940 connect_peers(david, bob);
942 let onion_message = david.onion_messenger.next_onion_message_for_peer(bob_id).unwrap();
943 bob.onion_messenger.handle_onion_message(&david_id, &onion_message);
945 connect_peers(alice, charlie);
947 let onion_message = bob.onion_messenger.next_onion_message_for_peer(alice_id).unwrap();
948 alice.onion_messenger.handle_onion_message(&bob_id, &onion_message);
950 let onion_message = alice.onion_messenger.next_onion_message_for_peer(charlie_id).unwrap();
951 charlie.onion_messenger.handle_onion_message(&alice_id, &onion_message);
953 let onion_message = charlie.onion_messenger.next_onion_message_for_peer(david_id).unwrap();
954 david.onion_messenger.handle_onion_message(&charlie_id, &onion_message);
956 let invoice_error = extract_invoice_error(david, &onion_message);
957 assert_eq!(invoice_error, InvoiceError::from(Bolt12SemanticError::MissingPaths));
961 fn fails_sending_invoice_without_blinded_payment_paths_for_refund() {
962 let mut accept_forward_cfg = test_default_channel_config();
963 accept_forward_cfg.accept_forwards_to_priv_channels = true;
965 // Clearing route_blinding prevents forming any payment paths since the node is unannounced.
966 let mut features = channelmanager::provided_init_features(&accept_forward_cfg);
967 features.set_onion_messages_optional();
968 features.clear_route_blinding();
970 let chanmon_cfgs = create_chanmon_cfgs(6);
971 let node_cfgs = create_node_cfgs(6, &chanmon_cfgs);
973 *node_cfgs[1].override_init_features.borrow_mut() = Some(features);
975 let node_chanmgrs = create_node_chanmgrs(
976 6, &node_cfgs, &[None, Some(accept_forward_cfg), None, None, None, None]
978 let nodes = create_network(6, &node_cfgs, &node_chanmgrs);
980 create_unannounced_chan_between_nodes_with_value(&nodes, 0, 1, 10_000_000, 1_000_000_000);
981 create_unannounced_chan_between_nodes_with_value(&nodes, 2, 3, 10_000_000, 1_000_000_000);
982 create_announced_chan_between_nodes_with_value(&nodes, 1, 2, 10_000_000, 1_000_000_000);
983 create_announced_chan_between_nodes_with_value(&nodes, 1, 4, 10_000_000, 1_000_000_000);
984 create_announced_chan_between_nodes_with_value(&nodes, 1, 5, 10_000_000, 1_000_000_000);
985 create_announced_chan_between_nodes_with_value(&nodes, 2, 4, 10_000_000, 1_000_000_000);
986 create_announced_chan_between_nodes_with_value(&nodes, 2, 5, 10_000_000, 1_000_000_000);
988 let (alice, bob, charlie, david) = (&nodes[0], &nodes[1], &nodes[2], &nodes[3]);
990 disconnect_peers(alice, &[charlie, david, &nodes[4], &nodes[5]]);
991 disconnect_peers(david, &[bob, &nodes[4], &nodes[5]]);
993 let absolute_expiry = Duration::from_secs(u64::MAX);
994 let payment_id = PaymentId([1; 32]);
995 let refund = david.node
996 .create_refund_builder(
997 "refund".to_string(), 10_000_000, absolute_expiry, payment_id, Retry::Attempts(0), None
1002 match alice.node.request_refund_payment(&refund) {
1003 Ok(_) => panic!("Expected error"),
1004 Err(e) => assert_eq!(e, Bolt12SemanticError::MissingPaths),
1009 fn fails_paying_invoice_more_than_once() {
1010 let mut accept_forward_cfg = test_default_channel_config();
1011 accept_forward_cfg.accept_forwards_to_priv_channels = true;
1013 let mut features = channelmanager::provided_init_features(&accept_forward_cfg);
1014 features.set_onion_messages_optional();
1015 features.set_route_blinding_optional();
1017 let chanmon_cfgs = create_chanmon_cfgs(6);
1018 let node_cfgs = create_node_cfgs(6, &chanmon_cfgs);
1020 *node_cfgs[1].override_init_features.borrow_mut() = Some(features);
1022 let node_chanmgrs = create_node_chanmgrs(
1023 6, &node_cfgs, &[None, Some(accept_forward_cfg), None, None, None, None]
1025 let nodes = create_network(6, &node_cfgs, &node_chanmgrs);
1027 create_unannounced_chan_between_nodes_with_value(&nodes, 0, 1, 10_000_000, 1_000_000_000);
1028 create_unannounced_chan_between_nodes_with_value(&nodes, 2, 3, 10_000_000, 1_000_000_000);
1029 create_announced_chan_between_nodes_with_value(&nodes, 1, 2, 10_000_000, 1_000_000_000);
1030 create_announced_chan_between_nodes_with_value(&nodes, 1, 4, 10_000_000, 1_000_000_000);
1031 create_announced_chan_between_nodes_with_value(&nodes, 1, 5, 10_000_000, 1_000_000_000);
1032 create_announced_chan_between_nodes_with_value(&nodes, 2, 4, 10_000_000, 1_000_000_000);
1033 create_announced_chan_between_nodes_with_value(&nodes, 2, 5, 10_000_000, 1_000_000_000);
1035 let (alice, bob, charlie, david) = (&nodes[0], &nodes[1], &nodes[2], &nodes[3]);
1036 let alice_id = alice.node.get_our_node_id();
1037 let bob_id = bob.node.get_our_node_id();
1038 let charlie_id = charlie.node.get_our_node_id();
1039 let david_id = david.node.get_our_node_id();
1041 disconnect_peers(alice, &[charlie, david, &nodes[4], &nodes[5]]);
1042 disconnect_peers(david, &[bob, &nodes[4], &nodes[5]]);
1044 let absolute_expiry = Duration::from_secs(u64::MAX);
1045 let payment_id = PaymentId([1; 32]);
1046 let refund = david.node
1047 .create_refund_builder(
1048 "refund".to_string(), 10_000_000, absolute_expiry, payment_id, Retry::Attempts(0), None
1052 expect_recent_payment!(david, RecentPaymentDetails::AwaitingInvoice, payment_id);
1054 // Alice sends the first invoice
1055 alice.node.request_refund_payment(&refund).unwrap();
1057 connect_peers(alice, charlie);
1059 let onion_message = alice.onion_messenger.next_onion_message_for_peer(charlie_id).unwrap();
1060 charlie.onion_messenger.handle_onion_message(&alice_id, &onion_message);
1062 let onion_message = charlie.onion_messenger.next_onion_message_for_peer(david_id).unwrap();
1063 david.onion_messenger.handle_onion_message(&charlie_id, &onion_message);
1065 // David pays the first invoice
1066 let invoice1 = extract_invoice(david, &onion_message);
1068 route_bolt12_payment(david, &[charlie, bob, alice], &invoice1);
1069 expect_recent_payment!(david, RecentPaymentDetails::Pending, payment_id);
1071 claim_bolt12_payment(david, &[charlie, bob, alice]);
1072 expect_recent_payment!(david, RecentPaymentDetails::Fulfilled, payment_id);
1074 disconnect_peers(alice, &[charlie]);
1076 // Alice sends the second invoice
1077 alice.node.request_refund_payment(&refund).unwrap();
1079 connect_peers(alice, charlie);
1080 connect_peers(david, bob);
1082 let onion_message = alice.onion_messenger.next_onion_message_for_peer(charlie_id).unwrap();
1083 charlie.onion_messenger.handle_onion_message(&alice_id, &onion_message);
1085 let onion_message = charlie.onion_messenger.next_onion_message_for_peer(david_id).unwrap();
1086 david.onion_messenger.handle_onion_message(&charlie_id, &onion_message);
1088 let invoice2 = extract_invoice(david, &onion_message);
1089 assert_eq!(invoice1.payer_metadata(), invoice2.payer_metadata());
1091 // David sends an error instead of paying the second invoice
1092 let onion_message = david.onion_messenger.next_onion_message_for_peer(bob_id).unwrap();
1093 bob.onion_messenger.handle_onion_message(&david_id, &onion_message);
1095 let onion_message = bob.onion_messenger.next_onion_message_for_peer(alice_id).unwrap();
1096 alice.onion_messenger.handle_onion_message(&bob_id, &onion_message);
1098 let invoice_error = extract_invoice_error(alice, &onion_message);
1099 assert_eq!(invoice_error, InvoiceError::from_string("DuplicateInvoice".to_string()));