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, IntroductionNode};
46 use crate::blinded_path::payment::{Bolt12OfferContext, Bolt12RefundContext, PaymentContext};
47 use crate::events::{Event, MessageSendEventsProvider, PaymentPurpose};
48 use crate::ln::channelmanager::{PaymentId, RecentPaymentDetails, Retry, self};
49 use crate::ln::features::InvoiceRequestFeatures;
50 use crate::ln::functional_test_utils::*;
51 use crate::ln::msgs::{ChannelMessageHandler, Init, NodeAnnouncement, OnionMessage, OnionMessageHandler, RoutingMessageHandler, SocketAddress, UnsignedGossipMessage, UnsignedNodeAnnouncement};
52 use crate::offers::invoice::Bolt12Invoice;
53 use crate::offers::invoice_error::InvoiceError;
54 use crate::offers::invoice_request::{InvoiceRequest, InvoiceRequestFields};
55 use crate::offers::parse::Bolt12SemanticError;
56 use crate::onion_message::messenger::PeeledOnion;
57 use crate::onion_message::offers::OffersMessage;
58 use crate::onion_message::packet::ParsedOnionMessageContents;
59 use crate::routing::gossip::{NodeAlias, NodeId};
60 use crate::sign::{NodeSigner, Recipient};
62 use crate::prelude::*;
64 macro_rules! expect_recent_payment {
65 ($node: expr, $payment_state: path, $payment_id: expr) => {
66 match $node.node.list_recent_payments().first() {
67 Some(&$payment_state { payment_id: actual_payment_id, .. }) => {
68 assert_eq!($payment_id, actual_payment_id);
70 Some(_) => panic!("Unexpected recent payment state"),
71 None => panic!("No recent payments"),
76 fn connect_peers<'a, 'b, 'c>(node_a: &Node<'a, 'b, 'c>, node_b: &Node<'a, 'b, 'c>) {
77 let node_id_a = node_a.node.get_our_node_id();
78 let node_id_b = node_b.node.get_our_node_id();
81 features: node_a.init_features(&node_id_b),
83 remote_network_address: None,
86 features: node_b.init_features(&node_id_a),
88 remote_network_address: None,
91 node_a.node.peer_connected(&node_id_b, &init_b, true).unwrap();
92 node_b.node.peer_connected(&node_id_a, &init_a, false).unwrap();
93 node_a.onion_messenger.peer_connected(&node_id_b, &init_b, true).unwrap();
94 node_b.onion_messenger.peer_connected(&node_id_a, &init_a, false).unwrap();
97 fn disconnect_peers<'a, 'b, 'c>(node_a: &Node<'a, 'b, 'c>, peers: &[&Node<'a, 'b, 'c>]) {
99 node_a.node.peer_disconnected(&node_b.node.get_our_node_id());
100 node_b.node.peer_disconnected(&node_a.node.get_our_node_id());
101 node_a.onion_messenger.peer_disconnected(&node_b.node.get_our_node_id());
102 node_b.onion_messenger.peer_disconnected(&node_a.node.get_our_node_id());
106 fn announce_node_address<'a, 'b, 'c>(
107 node: &Node<'a, 'b, 'c>, peers: &[&Node<'a, 'b, 'c>], address: SocketAddress,
109 let features = node.onion_messenger.provided_node_features()
110 | node.gossip_sync.provided_node_features();
112 let announcement = UnsignedNodeAnnouncement {
115 node_id: NodeId::from_pubkey(&node.keys_manager.get_node_id(Recipient::Node).unwrap()),
117 alias: NodeAlias([0u8; 32]),
118 addresses: vec![address],
119 excess_address_data: Vec::new(),
120 excess_data: Vec::new(),
122 let signature = node.keys_manager.sign_gossip_message(
123 UnsignedGossipMessage::NodeAnnouncement(&announcement)
126 let msg = NodeAnnouncement {
128 contents: announcement
131 node.gossip_sync.handle_node_announcement(&msg).unwrap();
133 peer.gossip_sync.handle_node_announcement(&msg).unwrap();
137 fn route_bolt12_payment<'a, 'b, 'c>(
138 node: &Node<'a, 'b, 'c>, path: &[&Node<'a, 'b, 'c>], invoice: &Bolt12Invoice
140 // Monitor added when handling the invoice onion message.
141 check_added_monitors(node, 1);
143 let mut events = node.node.get_and_clear_pending_msg_events();
144 assert_eq!(events.len(), 1);
145 let ev = remove_first_msg_event_to_node(&path[0].node.get_our_node_id(), &mut events);
147 // Use a fake payment_hash and bypass checking for the PaymentClaimable event since the
148 // invoice contains the payment_hash but it was encrypted inside an onion message.
149 let amount_msats = invoice.amount_msats();
150 let payment_hash = invoice.payment_hash();
151 let args = PassAlongPathArgs::new(node, path, amount_msats, payment_hash, ev)
152 .without_clearing_recipient_events();
153 do_pass_along_path(args);
156 fn claim_bolt12_payment<'a, 'b, 'c>(
157 node: &Node<'a, 'b, 'c>, path: &[&Node<'a, 'b, 'c>], expected_payment_context: PaymentContext
159 let recipient = &path[path.len() - 1];
160 let payment_purpose = match get_event!(recipient, Event::PaymentClaimable) {
161 Event::PaymentClaimable { purpose, .. } => purpose,
162 _ => panic!("No Event::PaymentClaimable"),
164 let payment_preimage = match payment_purpose.preimage() {
165 Some(preimage) => preimage,
166 None => panic!("No preimage in Event::PaymentClaimable"),
168 match payment_purpose {
169 PaymentPurpose::Bolt12OfferPayment { payment_context, .. } => {
170 assert_eq!(PaymentContext::Bolt12Offer(payment_context), expected_payment_context);
172 PaymentPurpose::Bolt12RefundPayment { payment_context, .. } => {
173 assert_eq!(PaymentContext::Bolt12Refund(payment_context), expected_payment_context);
175 _ => panic!("Unexpected payment purpose: {:?}", payment_purpose),
177 claim_payment(node, path, payment_preimage);
180 fn extract_invoice_request<'a, 'b, 'c>(
181 node: &Node<'a, 'b, 'c>, message: &OnionMessage
182 ) -> (InvoiceRequest, Option<BlindedPath>) {
183 match node.onion_messenger.peel_onion_message(message) {
184 Ok(PeeledOnion::Receive(message, _, reply_path)) => match message {
185 ParsedOnionMessageContents::Offers(offers_message) => match offers_message {
186 OffersMessage::InvoiceRequest(invoice_request) => (invoice_request, reply_path),
187 OffersMessage::Invoice(invoice) => panic!("Unexpected invoice: {:?}", invoice),
188 OffersMessage::InvoiceError(error) => panic!("Unexpected invoice_error: {:?}", error),
190 ParsedOnionMessageContents::Custom(message) => panic!("Unexpected custom message: {:?}", message),
192 Ok(PeeledOnion::Forward(_, _)) => panic!("Unexpected onion message forward"),
193 Err(e) => panic!("Failed to process onion message {:?}", e),
197 fn extract_invoice<'a, 'b, 'c>(node: &Node<'a, 'b, 'c>, message: &OnionMessage) -> Bolt12Invoice {
198 match node.onion_messenger.peel_onion_message(message) {
199 Ok(PeeledOnion::Receive(message, _, _)) => match message {
200 ParsedOnionMessageContents::Offers(offers_message) => match offers_message {
201 OffersMessage::InvoiceRequest(invoice_request) => panic!("Unexpected invoice_request: {:?}", invoice_request),
202 OffersMessage::Invoice(invoice) => invoice,
203 OffersMessage::InvoiceError(error) => panic!("Unexpected invoice_error: {:?}", error),
205 ParsedOnionMessageContents::Custom(message) => panic!("Unexpected custom message: {:?}", message),
207 Ok(PeeledOnion::Forward(_, _)) => panic!("Unexpected onion message forward"),
208 Err(e) => panic!("Failed to process onion message {:?}", e),
212 fn extract_invoice_error<'a, 'b, 'c>(
213 node: &Node<'a, 'b, 'c>, message: &OnionMessage
215 match node.onion_messenger.peel_onion_message(message) {
216 Ok(PeeledOnion::Receive(message, _, _)) => match message {
217 ParsedOnionMessageContents::Offers(offers_message) => match offers_message {
218 OffersMessage::InvoiceRequest(invoice_request) => panic!("Unexpected invoice_request: {:?}", invoice_request),
219 OffersMessage::Invoice(invoice) => panic!("Unexpected invoice: {:?}", invoice),
220 OffersMessage::InvoiceError(error) => error,
222 ParsedOnionMessageContents::Custom(message) => panic!("Unexpected custom message: {:?}", message),
224 Ok(PeeledOnion::Forward(_, _)) => panic!("Unexpected onion message forward"),
225 Err(e) => panic!("Failed to process onion message {:?}", e),
229 /// Checks that blinded paths without Tor-only nodes are preferred when constructing an offer.
231 fn prefers_non_tor_nodes_in_blinded_paths() {
232 let mut accept_forward_cfg = test_default_channel_config();
233 accept_forward_cfg.accept_forwards_to_priv_channels = true;
235 let mut features = channelmanager::provided_init_features(&accept_forward_cfg);
236 features.set_onion_messages_optional();
237 features.set_route_blinding_optional();
239 let chanmon_cfgs = create_chanmon_cfgs(6);
240 let node_cfgs = create_node_cfgs(6, &chanmon_cfgs);
242 *node_cfgs[1].override_init_features.borrow_mut() = Some(features);
244 let node_chanmgrs = create_node_chanmgrs(
245 6, &node_cfgs, &[None, Some(accept_forward_cfg), None, None, None, None]
247 let nodes = create_network(6, &node_cfgs, &node_chanmgrs);
249 create_unannounced_chan_between_nodes_with_value(&nodes, 0, 1, 10_000_000, 1_000_000_000);
250 create_unannounced_chan_between_nodes_with_value(&nodes, 2, 3, 10_000_000, 1_000_000_000);
251 create_announced_chan_between_nodes_with_value(&nodes, 1, 2, 10_000_000, 1_000_000_000);
252 create_announced_chan_between_nodes_with_value(&nodes, 1, 4, 10_000_000, 1_000_000_000);
253 create_announced_chan_between_nodes_with_value(&nodes, 1, 5, 10_000_000, 1_000_000_000);
254 create_announced_chan_between_nodes_with_value(&nodes, 2, 4, 10_000_000, 1_000_000_000);
255 create_announced_chan_between_nodes_with_value(&nodes, 2, 5, 10_000_000, 1_000_000_000);
257 // Add an extra channel so that more than one of Bob's peers have MIN_PEER_CHANNELS.
258 create_announced_chan_between_nodes_with_value(&nodes, 4, 5, 10_000_000, 1_000_000_000);
260 let (alice, bob, charlie, david) = (&nodes[0], &nodes[1], &nodes[2], &nodes[3]);
261 let bob_id = bob.node.get_our_node_id();
262 let charlie_id = charlie.node.get_our_node_id();
264 disconnect_peers(alice, &[charlie, david, &nodes[4], &nodes[5]]);
265 disconnect_peers(david, &[bob, &nodes[4], &nodes[5]]);
267 let tor = SocketAddress::OnionV2([255, 254, 253, 252, 251, 250, 249, 248, 247, 246, 38, 7]);
268 announce_node_address(charlie, &[alice, bob, david, &nodes[4], &nodes[5]], tor.clone());
271 .create_offer_builder().unwrap()
272 .amount_msats(10_000_000)
274 assert_ne!(offer.signing_pubkey(), Some(bob_id));
275 assert!(!offer.paths().is_empty());
276 for path in offer.paths() {
277 assert_ne!(path.introduction_node, IntroductionNode::NodeId(bob_id));
278 assert_ne!(path.introduction_node, IntroductionNode::NodeId(charlie_id));
281 // Use a one-hop blinded path when Bob is announced and all his peers are Tor-only.
282 announce_node_address(&nodes[4], &[alice, bob, charlie, david, &nodes[5]], tor.clone());
283 announce_node_address(&nodes[5], &[alice, bob, charlie, david, &nodes[4]], tor.clone());
286 .create_offer_builder().unwrap()
287 .amount_msats(10_000_000)
289 assert_ne!(offer.signing_pubkey(), Some(bob_id));
290 assert!(!offer.paths().is_empty());
291 for path in offer.paths() {
292 assert_eq!(path.introduction_node, IntroductionNode::NodeId(bob_id));
296 /// Checks that blinded paths prefer an introduction node that is the most connected.
298 fn prefers_more_connected_nodes_in_blinded_paths() {
299 let mut accept_forward_cfg = test_default_channel_config();
300 accept_forward_cfg.accept_forwards_to_priv_channels = true;
302 let mut features = channelmanager::provided_init_features(&accept_forward_cfg);
303 features.set_onion_messages_optional();
304 features.set_route_blinding_optional();
306 let chanmon_cfgs = create_chanmon_cfgs(6);
307 let node_cfgs = create_node_cfgs(6, &chanmon_cfgs);
309 *node_cfgs[1].override_init_features.borrow_mut() = Some(features);
311 let node_chanmgrs = create_node_chanmgrs(
312 6, &node_cfgs, &[None, Some(accept_forward_cfg), None, None, None, None]
314 let nodes = create_network(6, &node_cfgs, &node_chanmgrs);
316 create_unannounced_chan_between_nodes_with_value(&nodes, 0, 1, 10_000_000, 1_000_000_000);
317 create_unannounced_chan_between_nodes_with_value(&nodes, 2, 3, 10_000_000, 1_000_000_000);
318 create_announced_chan_between_nodes_with_value(&nodes, 1, 2, 10_000_000, 1_000_000_000);
319 create_announced_chan_between_nodes_with_value(&nodes, 1, 4, 10_000_000, 1_000_000_000);
320 create_announced_chan_between_nodes_with_value(&nodes, 1, 5, 10_000_000, 1_000_000_000);
321 create_announced_chan_between_nodes_with_value(&nodes, 2, 4, 10_000_000, 1_000_000_000);
322 create_announced_chan_between_nodes_with_value(&nodes, 2, 5, 10_000_000, 1_000_000_000);
324 // Add extra channels so that more than one of Bob's peers have MIN_PEER_CHANNELS and one has
325 // more than the others.
326 create_announced_chan_between_nodes_with_value(&nodes, 0, 4, 10_000_000, 1_000_000_000);
327 create_announced_chan_between_nodes_with_value(&nodes, 3, 4, 10_000_000, 1_000_000_000);
329 let (alice, bob, charlie, david) = (&nodes[0], &nodes[1], &nodes[2], &nodes[3]);
330 let bob_id = bob.node.get_our_node_id();
332 disconnect_peers(alice, &[charlie, david, &nodes[4], &nodes[5]]);
333 disconnect_peers(david, &[bob, &nodes[4], &nodes[5]]);
336 .create_offer_builder().unwrap()
337 .amount_msats(10_000_000)
339 assert_ne!(offer.signing_pubkey(), Some(bob_id));
340 assert!(!offer.paths().is_empty());
341 for path in offer.paths() {
342 assert_eq!(path.introduction_node, IntroductionNode::NodeId(nodes[4].node.get_our_node_id()));
346 /// Checks that an offer can be paid through blinded paths and that ephemeral pubkeys are used
347 /// rather than exposing a node's pubkey.
349 fn creates_and_pays_for_offer_using_two_hop_blinded_path() {
350 let mut accept_forward_cfg = test_default_channel_config();
351 accept_forward_cfg.accept_forwards_to_priv_channels = true;
353 let mut features = channelmanager::provided_init_features(&accept_forward_cfg);
354 features.set_onion_messages_optional();
355 features.set_route_blinding_optional();
357 let chanmon_cfgs = create_chanmon_cfgs(6);
358 let node_cfgs = create_node_cfgs(6, &chanmon_cfgs);
360 *node_cfgs[1].override_init_features.borrow_mut() = Some(features);
362 let node_chanmgrs = create_node_chanmgrs(
363 6, &node_cfgs, &[None, Some(accept_forward_cfg), None, None, None, None]
365 let nodes = create_network(6, &node_cfgs, &node_chanmgrs);
367 create_unannounced_chan_between_nodes_with_value(&nodes, 0, 1, 10_000_000, 1_000_000_000);
368 create_unannounced_chan_between_nodes_with_value(&nodes, 2, 3, 10_000_000, 1_000_000_000);
369 create_announced_chan_between_nodes_with_value(&nodes, 1, 2, 10_000_000, 1_000_000_000);
370 create_announced_chan_between_nodes_with_value(&nodes, 1, 4, 10_000_000, 1_000_000_000);
371 create_announced_chan_between_nodes_with_value(&nodes, 1, 5, 10_000_000, 1_000_000_000);
372 create_announced_chan_between_nodes_with_value(&nodes, 2, 4, 10_000_000, 1_000_000_000);
373 create_announced_chan_between_nodes_with_value(&nodes, 2, 5, 10_000_000, 1_000_000_000);
375 let (alice, bob, charlie, david) = (&nodes[0], &nodes[1], &nodes[2], &nodes[3]);
376 let alice_id = alice.node.get_our_node_id();
377 let bob_id = bob.node.get_our_node_id();
378 let charlie_id = charlie.node.get_our_node_id();
379 let david_id = david.node.get_our_node_id();
381 disconnect_peers(alice, &[charlie, david, &nodes[4], &nodes[5]]);
382 disconnect_peers(david, &[bob, &nodes[4], &nodes[5]]);
384 let offer = alice.node
385 .create_offer_builder()
387 .amount_msats(10_000_000)
389 assert_ne!(offer.signing_pubkey(), Some(alice_id));
390 assert!(!offer.paths().is_empty());
391 for path in offer.paths() {
392 assert_eq!(path.introduction_node, IntroductionNode::NodeId(bob_id));
395 let payment_id = PaymentId([1; 32]);
396 david.node.pay_for_offer(&offer, None, None, None, payment_id, Retry::Attempts(0), None)
398 expect_recent_payment!(david, RecentPaymentDetails::AwaitingInvoice, payment_id);
400 connect_peers(david, bob);
402 let onion_message = david.onion_messenger.next_onion_message_for_peer(bob_id).unwrap();
403 bob.onion_messenger.handle_onion_message(&david_id, &onion_message);
405 connect_peers(alice, charlie);
407 let onion_message = bob.onion_messenger.next_onion_message_for_peer(alice_id).unwrap();
408 alice.onion_messenger.handle_onion_message(&bob_id, &onion_message);
410 let (invoice_request, reply_path) = extract_invoice_request(alice, &onion_message);
411 let payment_context = PaymentContext::Bolt12Offer(Bolt12OfferContext {
412 offer_id: offer.id(),
413 invoice_request: InvoiceRequestFields {
414 payer_id: invoice_request.payer_id(),
416 features: InvoiceRequestFeatures::empty(),
418 payer_note_truncated: None,
421 assert_eq!(invoice_request.amount_msats(), None);
422 assert_ne!(invoice_request.payer_id(), david_id);
423 assert_eq!(reply_path.unwrap().introduction_node, IntroductionNode::NodeId(charlie_id));
425 let onion_message = alice.onion_messenger.next_onion_message_for_peer(charlie_id).unwrap();
426 charlie.onion_messenger.handle_onion_message(&alice_id, &onion_message);
428 let onion_message = charlie.onion_messenger.next_onion_message_for_peer(david_id).unwrap();
429 david.onion_messenger.handle_onion_message(&charlie_id, &onion_message);
431 let invoice = extract_invoice(david, &onion_message);
432 assert_eq!(invoice.amount_msats(), 10_000_000);
433 assert_ne!(invoice.signing_pubkey(), alice_id);
434 assert!(!invoice.payment_paths().is_empty());
435 for (_, path) in invoice.payment_paths() {
436 assert_eq!(path.introduction_node, IntroductionNode::NodeId(bob_id));
439 route_bolt12_payment(david, &[charlie, bob, alice], &invoice);
440 expect_recent_payment!(david, RecentPaymentDetails::Pending, payment_id);
442 claim_bolt12_payment(david, &[charlie, bob, alice], payment_context);
443 expect_recent_payment!(david, RecentPaymentDetails::Fulfilled, payment_id);
446 /// Checks that a refund can be paid through blinded paths and that ephemeral pubkeys are used
447 /// rather than exposing a node's pubkey.
449 fn creates_and_pays_for_refund_using_two_hop_blinded_path() {
450 let mut accept_forward_cfg = test_default_channel_config();
451 accept_forward_cfg.accept_forwards_to_priv_channels = true;
453 let mut features = channelmanager::provided_init_features(&accept_forward_cfg);
454 features.set_onion_messages_optional();
455 features.set_route_blinding_optional();
457 let chanmon_cfgs = create_chanmon_cfgs(6);
458 let node_cfgs = create_node_cfgs(6, &chanmon_cfgs);
460 *node_cfgs[1].override_init_features.borrow_mut() = Some(features);
462 let node_chanmgrs = create_node_chanmgrs(
463 6, &node_cfgs, &[None, Some(accept_forward_cfg), None, None, None, None]
465 let nodes = create_network(6, &node_cfgs, &node_chanmgrs);
467 create_unannounced_chan_between_nodes_with_value(&nodes, 0, 1, 10_000_000, 1_000_000_000);
468 create_unannounced_chan_between_nodes_with_value(&nodes, 2, 3, 10_000_000, 1_000_000_000);
469 create_announced_chan_between_nodes_with_value(&nodes, 1, 2, 10_000_000, 1_000_000_000);
470 create_announced_chan_between_nodes_with_value(&nodes, 1, 4, 10_000_000, 1_000_000_000);
471 create_announced_chan_between_nodes_with_value(&nodes, 1, 5, 10_000_000, 1_000_000_000);
472 create_announced_chan_between_nodes_with_value(&nodes, 2, 4, 10_000_000, 1_000_000_000);
473 create_announced_chan_between_nodes_with_value(&nodes, 2, 5, 10_000_000, 1_000_000_000);
475 let (alice, bob, charlie, david) = (&nodes[0], &nodes[1], &nodes[2], &nodes[3]);
476 let alice_id = alice.node.get_our_node_id();
477 let bob_id = bob.node.get_our_node_id();
478 let charlie_id = charlie.node.get_our_node_id();
479 let david_id = david.node.get_our_node_id();
481 disconnect_peers(alice, &[charlie, david, &nodes[4], &nodes[5]]);
482 disconnect_peers(david, &[bob, &nodes[4], &nodes[5]]);
484 let absolute_expiry = Duration::from_secs(u64::MAX);
485 let payment_id = PaymentId([1; 32]);
486 let refund = david.node
487 .create_refund_builder(10_000_000, absolute_expiry, payment_id, Retry::Attempts(0), None)
490 assert_eq!(refund.amount_msats(), 10_000_000);
491 assert_eq!(refund.absolute_expiry(), Some(absolute_expiry));
492 assert_ne!(refund.payer_id(), david_id);
493 assert!(!refund.paths().is_empty());
494 for path in refund.paths() {
495 assert_eq!(path.introduction_node, IntroductionNode::NodeId(charlie_id));
497 expect_recent_payment!(david, RecentPaymentDetails::AwaitingInvoice, payment_id);
499 let payment_context = PaymentContext::Bolt12Refund(Bolt12RefundContext {});
500 let expected_invoice = alice.node.request_refund_payment(&refund).unwrap();
502 connect_peers(alice, charlie);
504 let onion_message = alice.onion_messenger.next_onion_message_for_peer(charlie_id).unwrap();
505 charlie.onion_messenger.handle_onion_message(&alice_id, &onion_message);
507 let onion_message = charlie.onion_messenger.next_onion_message_for_peer(david_id).unwrap();
508 david.onion_messenger.handle_onion_message(&charlie_id, &onion_message);
510 let invoice = extract_invoice(david, &onion_message);
511 assert_eq!(invoice, expected_invoice);
513 assert_eq!(invoice.amount_msats(), 10_000_000);
514 assert_ne!(invoice.signing_pubkey(), alice_id);
515 assert!(!invoice.payment_paths().is_empty());
516 for (_, path) in invoice.payment_paths() {
517 assert_eq!(path.introduction_node, IntroductionNode::NodeId(bob_id));
520 route_bolt12_payment(david, &[charlie, bob, alice], &invoice);
521 expect_recent_payment!(david, RecentPaymentDetails::Pending, payment_id);
523 claim_bolt12_payment(david, &[charlie, bob, alice], payment_context);
524 expect_recent_payment!(david, RecentPaymentDetails::Fulfilled, payment_id);
527 /// Checks that an offer can be paid through a one-hop blinded path and that ephemeral pubkeys are
528 /// used rather than exposing a node's pubkey. However, the node's pubkey is still used as the
529 /// introduction node of the blinded path.
531 fn creates_and_pays_for_offer_using_one_hop_blinded_path() {
532 let chanmon_cfgs = create_chanmon_cfgs(2);
533 let node_cfgs = create_node_cfgs(2, &chanmon_cfgs);
534 let node_chanmgrs = create_node_chanmgrs(2, &node_cfgs, &[None, None]);
535 let nodes = create_network(2, &node_cfgs, &node_chanmgrs);
537 create_announced_chan_between_nodes_with_value(&nodes, 0, 1, 10_000_000, 1_000_000_000);
539 let alice = &nodes[0];
540 let alice_id = alice.node.get_our_node_id();
542 let bob_id = bob.node.get_our_node_id();
544 let offer = alice.node
545 .create_offer_builder().unwrap()
546 .amount_msats(10_000_000)
548 assert_ne!(offer.signing_pubkey(), Some(alice_id));
549 assert!(!offer.paths().is_empty());
550 for path in offer.paths() {
551 assert_eq!(path.introduction_node, IntroductionNode::NodeId(alice_id));
554 let payment_id = PaymentId([1; 32]);
555 bob.node.pay_for_offer(&offer, None, None, None, payment_id, Retry::Attempts(0), None).unwrap();
556 expect_recent_payment!(bob, RecentPaymentDetails::AwaitingInvoice, payment_id);
558 let onion_message = bob.onion_messenger.next_onion_message_for_peer(alice_id).unwrap();
559 alice.onion_messenger.handle_onion_message(&bob_id, &onion_message);
561 let (invoice_request, reply_path) = extract_invoice_request(alice, &onion_message);
562 let payment_context = PaymentContext::Bolt12Offer(Bolt12OfferContext {
563 offer_id: offer.id(),
564 invoice_request: InvoiceRequestFields {
565 payer_id: invoice_request.payer_id(),
567 features: InvoiceRequestFeatures::empty(),
569 payer_note_truncated: None,
572 assert_eq!(invoice_request.amount_msats(), None);
573 assert_ne!(invoice_request.payer_id(), bob_id);
574 assert_eq!(reply_path.unwrap().introduction_node, IntroductionNode::NodeId(bob_id));
576 let onion_message = alice.onion_messenger.next_onion_message_for_peer(bob_id).unwrap();
577 bob.onion_messenger.handle_onion_message(&alice_id, &onion_message);
579 let invoice = extract_invoice(bob, &onion_message);
580 assert_eq!(invoice.amount_msats(), 10_000_000);
581 assert_ne!(invoice.signing_pubkey(), alice_id);
582 assert!(!invoice.payment_paths().is_empty());
583 for (_, path) in invoice.payment_paths() {
584 assert_eq!(path.introduction_node, IntroductionNode::NodeId(alice_id));
587 route_bolt12_payment(bob, &[alice], &invoice);
588 expect_recent_payment!(bob, RecentPaymentDetails::Pending, payment_id);
590 claim_bolt12_payment(bob, &[alice], payment_context);
591 expect_recent_payment!(bob, RecentPaymentDetails::Fulfilled, payment_id);
594 /// Checks that a refund can be paid through a one-hop blinded path and that ephemeral pubkeys are
595 /// used rather than exposing a node's pubkey. However, the node's pubkey is still used as the
596 /// introduction node of the blinded path.
598 fn creates_and_pays_for_refund_using_one_hop_blinded_path() {
599 let chanmon_cfgs = create_chanmon_cfgs(2);
600 let node_cfgs = create_node_cfgs(2, &chanmon_cfgs);
601 let node_chanmgrs = create_node_chanmgrs(2, &node_cfgs, &[None, None]);
602 let nodes = create_network(2, &node_cfgs, &node_chanmgrs);
604 create_announced_chan_between_nodes_with_value(&nodes, 0, 1, 10_000_000, 1_000_000_000);
606 let alice = &nodes[0];
607 let alice_id = alice.node.get_our_node_id();
609 let bob_id = bob.node.get_our_node_id();
611 let absolute_expiry = Duration::from_secs(u64::MAX);
612 let payment_id = PaymentId([1; 32]);
613 let refund = bob.node
614 .create_refund_builder(10_000_000, absolute_expiry, payment_id, Retry::Attempts(0), None)
617 assert_eq!(refund.amount_msats(), 10_000_000);
618 assert_eq!(refund.absolute_expiry(), Some(absolute_expiry));
619 assert_ne!(refund.payer_id(), bob_id);
620 assert!(!refund.paths().is_empty());
621 for path in refund.paths() {
622 assert_eq!(path.introduction_node, IntroductionNode::NodeId(bob_id));
624 expect_recent_payment!(bob, RecentPaymentDetails::AwaitingInvoice, payment_id);
626 let payment_context = PaymentContext::Bolt12Refund(Bolt12RefundContext {});
627 let expected_invoice = alice.node.request_refund_payment(&refund).unwrap();
629 let onion_message = alice.onion_messenger.next_onion_message_for_peer(bob_id).unwrap();
630 bob.onion_messenger.handle_onion_message(&alice_id, &onion_message);
632 let invoice = extract_invoice(bob, &onion_message);
633 assert_eq!(invoice, expected_invoice);
635 assert_eq!(invoice.amount_msats(), 10_000_000);
636 assert_ne!(invoice.signing_pubkey(), alice_id);
637 assert!(!invoice.payment_paths().is_empty());
638 for (_, path) in invoice.payment_paths() {
639 assert_eq!(path.introduction_node, IntroductionNode::NodeId(alice_id));
642 route_bolt12_payment(bob, &[alice], &invoice);
643 expect_recent_payment!(bob, RecentPaymentDetails::Pending, payment_id);
645 claim_bolt12_payment(bob, &[alice], payment_context);
646 expect_recent_payment!(bob, RecentPaymentDetails::Fulfilled, payment_id);
649 /// Checks that an invoice for an offer without any blinded paths can be requested. Note that while
650 /// the requested is sent directly using the node's pubkey, the response and the payment still use
651 /// blinded paths as required by the spec.
653 fn pays_for_offer_without_blinded_paths() {
654 let chanmon_cfgs = create_chanmon_cfgs(2);
655 let node_cfgs = create_node_cfgs(2, &chanmon_cfgs);
656 let node_chanmgrs = create_node_chanmgrs(2, &node_cfgs, &[None, None]);
657 let nodes = create_network(2, &node_cfgs, &node_chanmgrs);
659 create_announced_chan_between_nodes_with_value(&nodes, 0, 1, 10_000_000, 1_000_000_000);
661 let alice = &nodes[0];
662 let alice_id = alice.node.get_our_node_id();
664 let bob_id = bob.node.get_our_node_id();
666 let offer = alice.node
667 .create_offer_builder().unwrap()
669 .amount_msats(10_000_000)
671 assert_eq!(offer.signing_pubkey(), Some(alice_id));
672 assert!(offer.paths().is_empty());
674 let payment_id = PaymentId([1; 32]);
675 bob.node.pay_for_offer(&offer, None, None, None, payment_id, Retry::Attempts(0), None).unwrap();
676 expect_recent_payment!(bob, RecentPaymentDetails::AwaitingInvoice, payment_id);
678 let onion_message = bob.onion_messenger.next_onion_message_for_peer(alice_id).unwrap();
679 alice.onion_messenger.handle_onion_message(&bob_id, &onion_message);
681 let (invoice_request, _) = extract_invoice_request(alice, &onion_message);
682 let payment_context = PaymentContext::Bolt12Offer(Bolt12OfferContext {
683 offer_id: offer.id(),
684 invoice_request: InvoiceRequestFields {
685 payer_id: invoice_request.payer_id(),
687 features: InvoiceRequestFeatures::empty(),
689 payer_note_truncated: None,
693 let onion_message = alice.onion_messenger.next_onion_message_for_peer(bob_id).unwrap();
694 bob.onion_messenger.handle_onion_message(&alice_id, &onion_message);
696 let invoice = extract_invoice(bob, &onion_message);
697 route_bolt12_payment(bob, &[alice], &invoice);
698 expect_recent_payment!(bob, RecentPaymentDetails::Pending, payment_id);
700 claim_bolt12_payment(bob, &[alice], payment_context);
701 expect_recent_payment!(bob, RecentPaymentDetails::Fulfilled, payment_id);
704 /// Checks that a refund without any blinded paths can be paid. Note that while the invoice is sent
705 /// directly using the node's pubkey, the payment still use blinded paths as required by the spec.
707 fn pays_for_refund_without_blinded_paths() {
708 let chanmon_cfgs = create_chanmon_cfgs(2);
709 let node_cfgs = create_node_cfgs(2, &chanmon_cfgs);
710 let node_chanmgrs = create_node_chanmgrs(2, &node_cfgs, &[None, None]);
711 let nodes = create_network(2, &node_cfgs, &node_chanmgrs);
713 create_announced_chan_between_nodes_with_value(&nodes, 0, 1, 10_000_000, 1_000_000_000);
715 let alice = &nodes[0];
716 let alice_id = alice.node.get_our_node_id();
718 let bob_id = bob.node.get_our_node_id();
720 let absolute_expiry = Duration::from_secs(u64::MAX);
721 let payment_id = PaymentId([1; 32]);
722 let refund = bob.node
723 .create_refund_builder(10_000_000, absolute_expiry, payment_id, Retry::Attempts(0), None)
727 assert_eq!(refund.payer_id(), bob_id);
728 assert!(refund.paths().is_empty());
729 expect_recent_payment!(bob, RecentPaymentDetails::AwaitingInvoice, payment_id);
731 let payment_context = PaymentContext::Bolt12Refund(Bolt12RefundContext {});
732 let expected_invoice = alice.node.request_refund_payment(&refund).unwrap();
734 let onion_message = alice.onion_messenger.next_onion_message_for_peer(bob_id).unwrap();
735 bob.onion_messenger.handle_onion_message(&alice_id, &onion_message);
737 let invoice = extract_invoice(bob, &onion_message);
738 assert_eq!(invoice, expected_invoice);
740 route_bolt12_payment(bob, &[alice], &invoice);
741 expect_recent_payment!(bob, RecentPaymentDetails::Pending, payment_id);
743 claim_bolt12_payment(bob, &[alice], payment_context);
744 expect_recent_payment!(bob, RecentPaymentDetails::Fulfilled, payment_id);
747 /// Fails creating an offer when a blinded path cannot be created without exposing the node's id.
749 fn fails_creating_offer_without_blinded_paths() {
750 let chanmon_cfgs = create_chanmon_cfgs(2);
751 let node_cfgs = create_node_cfgs(2, &chanmon_cfgs);
752 let node_chanmgrs = create_node_chanmgrs(2, &node_cfgs, &[None, None]);
753 let nodes = create_network(2, &node_cfgs, &node_chanmgrs);
755 create_unannounced_chan_between_nodes_with_value(&nodes, 0, 1, 10_000_000, 1_000_000_000);
757 match nodes[0].node.create_offer_builder() {
758 Ok(_) => panic!("Expected error"),
759 Err(e) => assert_eq!(e, Bolt12SemanticError::MissingPaths),
763 /// Fails creating a refund when a blinded path cannot be created without exposing the node's id.
765 fn fails_creating_refund_without_blinded_paths() {
766 let chanmon_cfgs = create_chanmon_cfgs(2);
767 let node_cfgs = create_node_cfgs(2, &chanmon_cfgs);
768 let node_chanmgrs = create_node_chanmgrs(2, &node_cfgs, &[None, None]);
769 let nodes = create_network(2, &node_cfgs, &node_chanmgrs);
771 create_unannounced_chan_between_nodes_with_value(&nodes, 0, 1, 10_000_000, 1_000_000_000);
773 let absolute_expiry = Duration::from_secs(u64::MAX);
774 let payment_id = PaymentId([1; 32]);
776 match nodes[0].node.create_refund_builder(
777 10_000, absolute_expiry, payment_id, Retry::Attempts(0), None
779 Ok(_) => panic!("Expected error"),
780 Err(e) => assert_eq!(e, Bolt12SemanticError::MissingPaths),
783 assert!(nodes[0].node.list_recent_payments().is_empty());
786 /// Fails creating an invoice request when the offer contains an unsupported chain.
788 fn fails_creating_invoice_request_for_unsupported_chain() {
789 let chanmon_cfgs = create_chanmon_cfgs(2);
790 let node_cfgs = create_node_cfgs(2, &chanmon_cfgs);
791 let node_chanmgrs = create_node_chanmgrs(2, &node_cfgs, &[None, None]);
792 let nodes = create_network(2, &node_cfgs, &node_chanmgrs);
794 create_announced_chan_between_nodes_with_value(&nodes, 0, 1, 10_000_000, 1_000_000_000);
796 let alice = &nodes[0];
799 let offer = alice.node
800 .create_offer_builder().unwrap()
802 .chain(Network::Signet)
805 let payment_id = PaymentId([1; 32]);
806 match bob.node.pay_for_offer(&offer, None, None, None, payment_id, Retry::Attempts(0), None) {
807 Ok(_) => panic!("Expected error"),
808 Err(e) => assert_eq!(e, Bolt12SemanticError::UnsupportedChain),
812 /// Fails requesting a payment when the refund contains an unsupported chain.
814 fn fails_sending_invoice_with_unsupported_chain_for_refund() {
815 let chanmon_cfgs = create_chanmon_cfgs(2);
816 let node_cfgs = create_node_cfgs(2, &chanmon_cfgs);
817 let node_chanmgrs = create_node_chanmgrs(2, &node_cfgs, &[None, None]);
818 let nodes = create_network(2, &node_cfgs, &node_chanmgrs);
820 create_announced_chan_between_nodes_with_value(&nodes, 0, 1, 10_000_000, 1_000_000_000);
822 let alice = &nodes[0];
825 let absolute_expiry = Duration::from_secs(u64::MAX);
826 let payment_id = PaymentId([1; 32]);
827 let refund = bob.node
828 .create_refund_builder(10_000_000, absolute_expiry, payment_id, Retry::Attempts(0), None)
830 .chain(Network::Signet)
833 match alice.node.request_refund_payment(&refund) {
834 Ok(_) => panic!("Expected error"),
835 Err(e) => assert_eq!(e, Bolt12SemanticError::UnsupportedChain),
839 /// Fails creating an invoice request when a blinded reply path cannot be created without exposing
842 fn fails_creating_invoice_request_without_blinded_reply_path() {
843 let chanmon_cfgs = create_chanmon_cfgs(6);
844 let node_cfgs = create_node_cfgs(6, &chanmon_cfgs);
845 let node_chanmgrs = create_node_chanmgrs(6, &node_cfgs, &[None, None, None, None, None, None]);
846 let nodes = create_network(6, &node_cfgs, &node_chanmgrs);
848 create_unannounced_chan_between_nodes_with_value(&nodes, 0, 1, 10_000_000, 1_000_000_000);
849 create_unannounced_chan_between_nodes_with_value(&nodes, 2, 3, 10_000_000, 1_000_000_000);
850 create_announced_chan_between_nodes_with_value(&nodes, 1, 2, 10_000_000, 1_000_000_000);
851 create_announced_chan_between_nodes_with_value(&nodes, 1, 4, 10_000_000, 1_000_000_000);
852 create_announced_chan_between_nodes_with_value(&nodes, 1, 5, 10_000_000, 1_000_000_000);
854 let (alice, bob, charlie, david) = (&nodes[0], &nodes[1], &nodes[2], &nodes[3]);
856 disconnect_peers(alice, &[charlie, david, &nodes[4], &nodes[5]]);
857 disconnect_peers(david, &[bob, &nodes[4], &nodes[5]]);
859 let offer = alice.node
860 .create_offer_builder().unwrap()
861 .amount_msats(10_000_000)
864 let payment_id = PaymentId([1; 32]);
866 match david.node.pay_for_offer(&offer, None, None, None, payment_id, Retry::Attempts(0), None) {
867 Ok(_) => panic!("Expected error"),
868 Err(e) => assert_eq!(e, Bolt12SemanticError::MissingPaths),
871 assert!(nodes[0].node.list_recent_payments().is_empty());
875 fn fails_creating_invoice_request_with_duplicate_payment_id() {
876 let chanmon_cfgs = create_chanmon_cfgs(6);
877 let node_cfgs = create_node_cfgs(6, &chanmon_cfgs);
878 let node_chanmgrs = create_node_chanmgrs(6, &node_cfgs, &[None, None, None, None, None, None]);
879 let nodes = create_network(6, &node_cfgs, &node_chanmgrs);
881 create_unannounced_chan_between_nodes_with_value(&nodes, 0, 1, 10_000_000, 1_000_000_000);
882 create_unannounced_chan_between_nodes_with_value(&nodes, 2, 3, 10_000_000, 1_000_000_000);
883 create_announced_chan_between_nodes_with_value(&nodes, 1, 2, 10_000_000, 1_000_000_000);
884 create_announced_chan_between_nodes_with_value(&nodes, 1, 4, 10_000_000, 1_000_000_000);
885 create_announced_chan_between_nodes_with_value(&nodes, 1, 5, 10_000_000, 1_000_000_000);
886 create_announced_chan_between_nodes_with_value(&nodes, 2, 4, 10_000_000, 1_000_000_000);
887 create_announced_chan_between_nodes_with_value(&nodes, 2, 5, 10_000_000, 1_000_000_000);
889 let (alice, _bob, charlie, david) = (&nodes[0], &nodes[1], &nodes[2], &nodes[3]);
891 disconnect_peers(alice, &[charlie, david, &nodes[4], &nodes[5]]);
893 let offer = alice.node
894 .create_offer_builder().unwrap()
895 .amount_msats(10_000_000)
898 let payment_id = PaymentId([1; 32]);
900 david.node.pay_for_offer(
901 &offer, None, None, None, payment_id, Retry::Attempts(0), None
904 expect_recent_payment!(david, RecentPaymentDetails::AwaitingInvoice, payment_id);
906 match david.node.pay_for_offer(&offer, None, None, None, payment_id, Retry::Attempts(0), None) {
907 Ok(_) => panic!("Expected error"),
908 Err(e) => assert_eq!(e, Bolt12SemanticError::DuplicatePaymentId),
911 expect_recent_payment!(david, RecentPaymentDetails::AwaitingInvoice, payment_id);
915 fn fails_creating_refund_with_duplicate_payment_id() {
916 let chanmon_cfgs = create_chanmon_cfgs(2);
917 let node_cfgs = create_node_cfgs(2, &chanmon_cfgs);
918 let node_chanmgrs = create_node_chanmgrs(2, &node_cfgs, &[None, None]);
919 let nodes = create_network(2, &node_cfgs, &node_chanmgrs);
921 create_announced_chan_between_nodes_with_value(&nodes, 0, 1, 10_000_000, 1_000_000_000);
923 let absolute_expiry = Duration::from_secs(u64::MAX);
924 let payment_id = PaymentId([1; 32]);
926 nodes[0].node.create_refund_builder(
927 10_000, absolute_expiry, payment_id, Retry::Attempts(0), None
930 expect_recent_payment!(nodes[0], RecentPaymentDetails::AwaitingInvoice, payment_id);
932 match nodes[0].node.create_refund_builder(
933 10_000, absolute_expiry, payment_id, Retry::Attempts(0), None
935 Ok(_) => panic!("Expected error"),
936 Err(e) => assert_eq!(e, Bolt12SemanticError::DuplicatePaymentId),
939 expect_recent_payment!(nodes[0], RecentPaymentDetails::AwaitingInvoice, payment_id);
943 fn fails_sending_invoice_without_blinded_payment_paths_for_offer() {
944 let mut accept_forward_cfg = test_default_channel_config();
945 accept_forward_cfg.accept_forwards_to_priv_channels = true;
947 // Clearing route_blinding prevents forming any payment paths since the node is unannounced.
948 let mut features = channelmanager::provided_init_features(&accept_forward_cfg);
949 features.set_onion_messages_optional();
950 features.clear_route_blinding();
952 let chanmon_cfgs = create_chanmon_cfgs(6);
953 let node_cfgs = create_node_cfgs(6, &chanmon_cfgs);
955 *node_cfgs[1].override_init_features.borrow_mut() = Some(features);
957 let node_chanmgrs = create_node_chanmgrs(
958 6, &node_cfgs, &[None, Some(accept_forward_cfg), None, None, None, None]
960 let nodes = create_network(6, &node_cfgs, &node_chanmgrs);
962 create_unannounced_chan_between_nodes_with_value(&nodes, 0, 1, 10_000_000, 1_000_000_000);
963 create_unannounced_chan_between_nodes_with_value(&nodes, 2, 3, 10_000_000, 1_000_000_000);
964 create_announced_chan_between_nodes_with_value(&nodes, 1, 2, 10_000_000, 1_000_000_000);
965 create_announced_chan_between_nodes_with_value(&nodes, 1, 4, 10_000_000, 1_000_000_000);
966 create_announced_chan_between_nodes_with_value(&nodes, 1, 5, 10_000_000, 1_000_000_000);
967 create_announced_chan_between_nodes_with_value(&nodes, 2, 4, 10_000_000, 1_000_000_000);
968 create_announced_chan_between_nodes_with_value(&nodes, 2, 5, 10_000_000, 1_000_000_000);
970 let (alice, bob, charlie, david) = (&nodes[0], &nodes[1], &nodes[2], &nodes[3]);
971 let alice_id = alice.node.get_our_node_id();
972 let bob_id = bob.node.get_our_node_id();
973 let charlie_id = charlie.node.get_our_node_id();
974 let david_id = david.node.get_our_node_id();
976 disconnect_peers(alice, &[charlie, david, &nodes[4], &nodes[5]]);
977 disconnect_peers(david, &[bob, &nodes[4], &nodes[5]]);
979 let offer = alice.node
980 .create_offer_builder().unwrap()
981 .amount_msats(10_000_000)
984 let payment_id = PaymentId([1; 32]);
985 david.node.pay_for_offer(&offer, None, None, None, payment_id, Retry::Attempts(0), None)
988 connect_peers(david, bob);
990 let onion_message = david.onion_messenger.next_onion_message_for_peer(bob_id).unwrap();
991 bob.onion_messenger.handle_onion_message(&david_id, &onion_message);
993 connect_peers(alice, charlie);
995 let onion_message = bob.onion_messenger.next_onion_message_for_peer(alice_id).unwrap();
996 alice.onion_messenger.handle_onion_message(&bob_id, &onion_message);
998 let onion_message = alice.onion_messenger.next_onion_message_for_peer(charlie_id).unwrap();
999 charlie.onion_messenger.handle_onion_message(&alice_id, &onion_message);
1001 let onion_message = charlie.onion_messenger.next_onion_message_for_peer(david_id).unwrap();
1002 david.onion_messenger.handle_onion_message(&charlie_id, &onion_message);
1004 let invoice_error = extract_invoice_error(david, &onion_message);
1005 assert_eq!(invoice_error, InvoiceError::from(Bolt12SemanticError::MissingPaths));
1009 fn fails_sending_invoice_without_blinded_payment_paths_for_refund() {
1010 let mut accept_forward_cfg = test_default_channel_config();
1011 accept_forward_cfg.accept_forwards_to_priv_channels = true;
1013 // Clearing route_blinding prevents forming any payment paths since the node is unannounced.
1014 let mut features = channelmanager::provided_init_features(&accept_forward_cfg);
1015 features.set_onion_messages_optional();
1016 features.clear_route_blinding();
1018 let chanmon_cfgs = create_chanmon_cfgs(6);
1019 let node_cfgs = create_node_cfgs(6, &chanmon_cfgs);
1021 *node_cfgs[1].override_init_features.borrow_mut() = Some(features);
1023 let node_chanmgrs = create_node_chanmgrs(
1024 6, &node_cfgs, &[None, Some(accept_forward_cfg), None, None, None, None]
1026 let nodes = create_network(6, &node_cfgs, &node_chanmgrs);
1028 create_unannounced_chan_between_nodes_with_value(&nodes, 0, 1, 10_000_000, 1_000_000_000);
1029 create_unannounced_chan_between_nodes_with_value(&nodes, 2, 3, 10_000_000, 1_000_000_000);
1030 create_announced_chan_between_nodes_with_value(&nodes, 1, 2, 10_000_000, 1_000_000_000);
1031 create_announced_chan_between_nodes_with_value(&nodes, 1, 4, 10_000_000, 1_000_000_000);
1032 create_announced_chan_between_nodes_with_value(&nodes, 1, 5, 10_000_000, 1_000_000_000);
1033 create_announced_chan_between_nodes_with_value(&nodes, 2, 4, 10_000_000, 1_000_000_000);
1034 create_announced_chan_between_nodes_with_value(&nodes, 2, 5, 10_000_000, 1_000_000_000);
1036 let (alice, bob, charlie, david) = (&nodes[0], &nodes[1], &nodes[2], &nodes[3]);
1038 disconnect_peers(alice, &[charlie, david, &nodes[4], &nodes[5]]);
1039 disconnect_peers(david, &[bob, &nodes[4], &nodes[5]]);
1041 let absolute_expiry = Duration::from_secs(u64::MAX);
1042 let payment_id = PaymentId([1; 32]);
1043 let refund = david.node
1044 .create_refund_builder(10_000_000, absolute_expiry, payment_id, Retry::Attempts(0), None)
1048 match alice.node.request_refund_payment(&refund) {
1049 Ok(_) => panic!("Expected error"),
1050 Err(e) => assert_eq!(e, Bolt12SemanticError::MissingPaths),
1055 fn fails_paying_invoice_more_than_once() {
1056 let mut accept_forward_cfg = test_default_channel_config();
1057 accept_forward_cfg.accept_forwards_to_priv_channels = true;
1059 let mut features = channelmanager::provided_init_features(&accept_forward_cfg);
1060 features.set_onion_messages_optional();
1061 features.set_route_blinding_optional();
1063 let chanmon_cfgs = create_chanmon_cfgs(6);
1064 let node_cfgs = create_node_cfgs(6, &chanmon_cfgs);
1066 *node_cfgs[1].override_init_features.borrow_mut() = Some(features);
1068 let node_chanmgrs = create_node_chanmgrs(
1069 6, &node_cfgs, &[None, Some(accept_forward_cfg), None, None, None, None]
1071 let nodes = create_network(6, &node_cfgs, &node_chanmgrs);
1073 create_unannounced_chan_between_nodes_with_value(&nodes, 0, 1, 10_000_000, 1_000_000_000);
1074 create_unannounced_chan_between_nodes_with_value(&nodes, 2, 3, 10_000_000, 1_000_000_000);
1075 create_announced_chan_between_nodes_with_value(&nodes, 1, 2, 10_000_000, 1_000_000_000);
1076 create_announced_chan_between_nodes_with_value(&nodes, 1, 4, 10_000_000, 1_000_000_000);
1077 create_announced_chan_between_nodes_with_value(&nodes, 1, 5, 10_000_000, 1_000_000_000);
1078 create_announced_chan_between_nodes_with_value(&nodes, 2, 4, 10_000_000, 1_000_000_000);
1079 create_announced_chan_between_nodes_with_value(&nodes, 2, 5, 10_000_000, 1_000_000_000);
1081 let (alice, bob, charlie, david) = (&nodes[0], &nodes[1], &nodes[2], &nodes[3]);
1082 let alice_id = alice.node.get_our_node_id();
1083 let bob_id = bob.node.get_our_node_id();
1084 let charlie_id = charlie.node.get_our_node_id();
1085 let david_id = david.node.get_our_node_id();
1087 disconnect_peers(alice, &[charlie, david, &nodes[4], &nodes[5]]);
1088 disconnect_peers(david, &[bob, &nodes[4], &nodes[5]]);
1090 let absolute_expiry = Duration::from_secs(u64::MAX);
1091 let payment_id = PaymentId([1; 32]);
1092 let refund = david.node
1093 .create_refund_builder(10_000_000, absolute_expiry, payment_id, Retry::Attempts(0), None)
1096 expect_recent_payment!(david, RecentPaymentDetails::AwaitingInvoice, payment_id);
1098 // Alice sends the first invoice
1099 alice.node.request_refund_payment(&refund).unwrap();
1101 connect_peers(alice, charlie);
1103 let onion_message = alice.onion_messenger.next_onion_message_for_peer(charlie_id).unwrap();
1104 charlie.onion_messenger.handle_onion_message(&alice_id, &onion_message);
1106 let onion_message = charlie.onion_messenger.next_onion_message_for_peer(david_id).unwrap();
1107 david.onion_messenger.handle_onion_message(&charlie_id, &onion_message);
1109 // David pays the first invoice
1110 let payment_context = PaymentContext::Bolt12Refund(Bolt12RefundContext {});
1111 let invoice1 = extract_invoice(david, &onion_message);
1113 route_bolt12_payment(david, &[charlie, bob, alice], &invoice1);
1114 expect_recent_payment!(david, RecentPaymentDetails::Pending, payment_id);
1116 claim_bolt12_payment(david, &[charlie, bob, alice], payment_context);
1117 expect_recent_payment!(david, RecentPaymentDetails::Fulfilled, payment_id);
1119 disconnect_peers(alice, &[charlie]);
1121 // Alice sends the second invoice
1122 alice.node.request_refund_payment(&refund).unwrap();
1124 connect_peers(alice, charlie);
1125 connect_peers(david, bob);
1127 let onion_message = alice.onion_messenger.next_onion_message_for_peer(charlie_id).unwrap();
1128 charlie.onion_messenger.handle_onion_message(&alice_id, &onion_message);
1130 let onion_message = charlie.onion_messenger.next_onion_message_for_peer(david_id).unwrap();
1131 david.onion_messenger.handle_onion_message(&charlie_id, &onion_message);
1133 let invoice2 = extract_invoice(david, &onion_message);
1134 assert_eq!(invoice1.payer_metadata(), invoice2.payer_metadata());
1136 // David sends an error instead of paying the second invoice
1137 let onion_message = david.onion_messenger.next_onion_message_for_peer(bob_id).unwrap();
1138 bob.onion_messenger.handle_onion_message(&david_id, &onion_message);
1140 let onion_message = bob.onion_messenger.next_onion_message_for_peer(alice_id).unwrap();
1141 alice.onion_messenger.handle_onion_message(&bob_id, &onion_message);
1143 let invoice_error = extract_invoice_error(alice, &onion_message);
1144 assert_eq!(invoice_error, InvoiceError::from_string("DuplicateInvoice".to_string()));