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::Network;
44 use bitcoin::secp256k1::PublicKey;
45 use core::time::Duration;
46 use crate::blinded_path::{BlindedPath, IntroductionNode};
47 use crate::blinded_path::payment::{Bolt12OfferContext, Bolt12RefundContext, PaymentContext};
48 use crate::events::{Event, MessageSendEventsProvider, PaymentPurpose};
49 use crate::ln::channelmanager::{Bolt12PaymentError, MAX_SHORT_LIVED_RELATIVE_EXPIRY, PaymentId, RecentPaymentDetails, Retry, self};
50 use crate::ln::functional_test_utils::*;
51 use crate::ln::msgs::{ChannelMessageHandler, Init, NodeAnnouncement, OnionMessage, OnionMessageHandler, RoutingMessageHandler, SocketAddress, UnsignedGossipMessage, UnsignedNodeAnnouncement};
52 use crate::ln::outbound_payment::IDEMPOTENCY_TIMEOUT_TICKS;
53 use crate::offers::invoice::Bolt12Invoice;
54 use crate::offers::invoice_error::InvoiceError;
55 use crate::offers::invoice_request::{InvoiceRequest, InvoiceRequestFields};
56 use crate::offers::parse::Bolt12SemanticError;
57 use crate::onion_message::messenger::PeeledOnion;
58 use crate::onion_message::offers::OffersMessage;
59 use crate::onion_message::packet::ParsedOnionMessageContents;
60 use crate::routing::gossip::{NodeAlias, NodeId};
61 use crate::sign::{NodeSigner, Recipient};
63 use crate::prelude::*;
65 macro_rules! expect_recent_payment {
66 ($node: expr, $payment_state: path, $payment_id: expr) => {
67 match $node.node.list_recent_payments().first() {
68 Some(&$payment_state { payment_id: actual_payment_id, .. }) => {
69 assert_eq!($payment_id, actual_payment_id);
71 Some(_) => panic!("Unexpected recent payment state"),
72 None => panic!("No recent payments"),
77 fn connect_peers<'a, 'b, 'c>(node_a: &Node<'a, 'b, 'c>, node_b: &Node<'a, 'b, 'c>) {
78 let node_id_a = node_a.node.get_our_node_id();
79 let node_id_b = node_b.node.get_our_node_id();
82 features: node_a.init_features(&node_id_b),
84 remote_network_address: None,
87 features: node_b.init_features(&node_id_a),
89 remote_network_address: None,
92 node_a.node.peer_connected(&node_id_b, &init_b, true).unwrap();
93 node_b.node.peer_connected(&node_id_a, &init_a, false).unwrap();
94 node_a.onion_messenger.peer_connected(&node_id_b, &init_b, true).unwrap();
95 node_b.onion_messenger.peer_connected(&node_id_a, &init_a, false).unwrap();
98 fn disconnect_peers<'a, 'b, 'c>(node_a: &Node<'a, 'b, 'c>, peers: &[&Node<'a, 'b, 'c>]) {
100 node_a.node.peer_disconnected(&node_b.node.get_our_node_id());
101 node_b.node.peer_disconnected(&node_a.node.get_our_node_id());
102 node_a.onion_messenger.peer_disconnected(&node_b.node.get_our_node_id());
103 node_b.onion_messenger.peer_disconnected(&node_a.node.get_our_node_id());
107 fn announce_node_address<'a, 'b, 'c>(
108 node: &Node<'a, 'b, 'c>, peers: &[&Node<'a, 'b, 'c>], address: SocketAddress,
110 let features = node.onion_messenger.provided_node_features()
111 | node.gossip_sync.provided_node_features();
113 let announcement = UnsignedNodeAnnouncement {
116 node_id: NodeId::from_pubkey(&node.keys_manager.get_node_id(Recipient::Node).unwrap()),
118 alias: NodeAlias([0u8; 32]),
119 addresses: vec![address],
120 excess_address_data: Vec::new(),
121 excess_data: Vec::new(),
123 let signature = node.keys_manager.sign_gossip_message(
124 UnsignedGossipMessage::NodeAnnouncement(&announcement)
127 let msg = NodeAnnouncement {
129 contents: announcement
132 node.gossip_sync.handle_node_announcement(&msg).unwrap();
134 peer.gossip_sync.handle_node_announcement(&msg).unwrap();
138 fn resolve_introduction_node<'a, 'b, 'c>(node: &Node<'a, 'b, 'c>, path: &BlindedPath) -> PublicKey {
139 path.public_introduction_node_id(&node.network_graph.read_only())
140 .and_then(|node_id| node_id.as_pubkey().ok())
144 fn route_bolt12_payment<'a, 'b, 'c>(
145 node: &Node<'a, 'b, 'c>, path: &[&Node<'a, 'b, 'c>], invoice: &Bolt12Invoice
147 // Monitor added when handling the invoice onion message.
148 check_added_monitors(node, 1);
150 let mut events = node.node.get_and_clear_pending_msg_events();
151 assert_eq!(events.len(), 1);
152 let ev = remove_first_msg_event_to_node(&path[0].node.get_our_node_id(), &mut events);
154 // Use a fake payment_hash and bypass checking for the PaymentClaimable event since the
155 // invoice contains the payment_hash but it was encrypted inside an onion message.
156 let amount_msats = invoice.amount_msats();
157 let payment_hash = invoice.payment_hash();
158 let args = PassAlongPathArgs::new(node, path, amount_msats, payment_hash, ev)
159 .without_clearing_recipient_events();
160 do_pass_along_path(args);
163 fn claim_bolt12_payment<'a, 'b, 'c>(
164 node: &Node<'a, 'b, 'c>, path: &[&Node<'a, 'b, 'c>], expected_payment_context: PaymentContext
166 let recipient = &path[path.len() - 1];
167 let payment_purpose = match get_event!(recipient, Event::PaymentClaimable) {
168 Event::PaymentClaimable { purpose, .. } => purpose,
169 _ => panic!("No Event::PaymentClaimable"),
171 let payment_preimage = match payment_purpose.preimage() {
172 Some(preimage) => preimage,
173 None => panic!("No preimage in Event::PaymentClaimable"),
175 match payment_purpose {
176 PaymentPurpose::Bolt12OfferPayment { payment_context, .. } => {
177 assert_eq!(PaymentContext::Bolt12Offer(payment_context), expected_payment_context);
179 PaymentPurpose::Bolt12RefundPayment { payment_context, .. } => {
180 assert_eq!(PaymentContext::Bolt12Refund(payment_context), expected_payment_context);
182 _ => panic!("Unexpected payment purpose: {:?}", payment_purpose),
184 claim_payment(node, path, payment_preimage);
187 fn extract_invoice_request<'a, 'b, 'c>(
188 node: &Node<'a, 'b, 'c>, message: &OnionMessage
189 ) -> (InvoiceRequest, BlindedPath) {
190 match node.onion_messenger.peel_onion_message(message) {
191 Ok(PeeledOnion::Receive(message, _, reply_path)) => match message {
192 ParsedOnionMessageContents::Offers(offers_message) => match offers_message {
193 OffersMessage::InvoiceRequest(invoice_request) => (invoice_request, reply_path.unwrap()),
194 OffersMessage::Invoice(invoice) => panic!("Unexpected invoice: {:?}", invoice),
195 OffersMessage::InvoiceError(error) => panic!("Unexpected invoice_error: {:?}", error),
197 ParsedOnionMessageContents::AsyncPayments(message) => panic!("Unexpected async payments message: {:?}", message),
198 ParsedOnionMessageContents::Custom(message) => panic!("Unexpected custom message: {:?}", message),
200 Ok(PeeledOnion::Forward(_, _)) => panic!("Unexpected onion message forward"),
201 Err(e) => panic!("Failed to process onion message {:?}", e),
205 fn extract_invoice<'a, 'b, 'c>(node: &Node<'a, 'b, 'c>, message: &OnionMessage) -> Bolt12Invoice {
206 match node.onion_messenger.peel_onion_message(message) {
207 Ok(PeeledOnion::Receive(message, _, _)) => match message {
208 ParsedOnionMessageContents::Offers(offers_message) => match offers_message {
209 OffersMessage::InvoiceRequest(invoice_request) => panic!("Unexpected invoice_request: {:?}", invoice_request),
210 OffersMessage::Invoice(invoice) => invoice,
211 OffersMessage::InvoiceError(error) => panic!("Unexpected invoice_error: {:?}", error),
213 ParsedOnionMessageContents::AsyncPayments(message) => panic!("Unexpected async payments message: {:?}", message),
214 ParsedOnionMessageContents::Custom(message) => panic!("Unexpected custom message: {:?}", message),
216 Ok(PeeledOnion::Forward(_, _)) => panic!("Unexpected onion message forward"),
217 Err(e) => panic!("Failed to process onion message {:?}", e),
221 fn extract_invoice_error<'a, 'b, 'c>(
222 node: &Node<'a, 'b, 'c>, message: &OnionMessage
224 match node.onion_messenger.peel_onion_message(message) {
225 Ok(PeeledOnion::Receive(message, _, _)) => match message {
226 ParsedOnionMessageContents::Offers(offers_message) => match offers_message {
227 OffersMessage::InvoiceRequest(invoice_request) => panic!("Unexpected invoice_request: {:?}", invoice_request),
228 OffersMessage::Invoice(invoice) => panic!("Unexpected invoice: {:?}", invoice),
229 OffersMessage::InvoiceError(error) => error,
231 ParsedOnionMessageContents::AsyncPayments(message) => panic!("Unexpected async payments message: {:?}", message),
232 ParsedOnionMessageContents::Custom(message) => panic!("Unexpected custom message: {:?}", message),
234 Ok(PeeledOnion::Forward(_, _)) => panic!("Unexpected onion message forward"),
235 Err(e) => panic!("Failed to process onion message {:?}", e),
239 /// Checks that blinded paths without Tor-only nodes are preferred when constructing an offer.
241 fn prefers_non_tor_nodes_in_blinded_paths() {
242 let mut accept_forward_cfg = test_default_channel_config();
243 accept_forward_cfg.accept_forwards_to_priv_channels = true;
245 let mut features = channelmanager::provided_init_features(&accept_forward_cfg);
246 features.set_onion_messages_optional();
247 features.set_route_blinding_optional();
249 let chanmon_cfgs = create_chanmon_cfgs(6);
250 let node_cfgs = create_node_cfgs(6, &chanmon_cfgs);
252 *node_cfgs[1].override_init_features.borrow_mut() = Some(features);
254 let node_chanmgrs = create_node_chanmgrs(
255 6, &node_cfgs, &[None, Some(accept_forward_cfg), None, None, None, None]
257 let nodes = create_network(6, &node_cfgs, &node_chanmgrs);
259 create_unannounced_chan_between_nodes_with_value(&nodes, 0, 1, 10_000_000, 1_000_000_000);
260 create_unannounced_chan_between_nodes_with_value(&nodes, 2, 3, 10_000_000, 1_000_000_000);
261 create_announced_chan_between_nodes_with_value(&nodes, 1, 2, 10_000_000, 1_000_000_000);
262 create_announced_chan_between_nodes_with_value(&nodes, 1, 4, 10_000_000, 1_000_000_000);
263 create_announced_chan_between_nodes_with_value(&nodes, 1, 5, 10_000_000, 1_000_000_000);
264 create_announced_chan_between_nodes_with_value(&nodes, 2, 4, 10_000_000, 1_000_000_000);
265 create_announced_chan_between_nodes_with_value(&nodes, 2, 5, 10_000_000, 1_000_000_000);
267 // Add an extra channel so that more than one of Bob's peers have MIN_PEER_CHANNELS.
268 create_announced_chan_between_nodes_with_value(&nodes, 4, 5, 10_000_000, 1_000_000_000);
270 let (alice, bob, charlie, david) = (&nodes[0], &nodes[1], &nodes[2], &nodes[3]);
271 let bob_id = bob.node.get_our_node_id();
272 let charlie_id = charlie.node.get_our_node_id();
274 disconnect_peers(alice, &[charlie, david, &nodes[4], &nodes[5]]);
275 disconnect_peers(david, &[bob, &nodes[4], &nodes[5]]);
277 let tor = SocketAddress::OnionV2([255, 254, 253, 252, 251, 250, 249, 248, 247, 246, 38, 7]);
278 announce_node_address(charlie, &[alice, bob, david, &nodes[4], &nodes[5]], tor.clone());
281 .create_offer_builder(None).unwrap()
282 .amount_msats(10_000_000)
284 assert_ne!(offer.signing_pubkey(), Some(bob_id));
285 assert!(!offer.paths().is_empty());
286 for path in offer.paths() {
287 let introduction_node_id = resolve_introduction_node(david, &path);
288 assert_ne!(introduction_node_id, bob_id);
289 assert_ne!(introduction_node_id, charlie_id);
292 // Use a one-hop blinded path when Bob is announced and all his peers are Tor-only.
293 announce_node_address(&nodes[4], &[alice, bob, charlie, david, &nodes[5]], tor.clone());
294 announce_node_address(&nodes[5], &[alice, bob, charlie, david, &nodes[4]], tor.clone());
297 .create_offer_builder(None).unwrap()
298 .amount_msats(10_000_000)
300 assert_ne!(offer.signing_pubkey(), Some(bob_id));
301 assert!(!offer.paths().is_empty());
302 for path in offer.paths() {
303 let introduction_node_id = resolve_introduction_node(david, &path);
304 assert_eq!(introduction_node_id, bob_id);
308 /// Checks that blinded paths prefer an introduction node that is the most connected.
310 fn prefers_more_connected_nodes_in_blinded_paths() {
311 let mut accept_forward_cfg = test_default_channel_config();
312 accept_forward_cfg.accept_forwards_to_priv_channels = true;
314 let mut features = channelmanager::provided_init_features(&accept_forward_cfg);
315 features.set_onion_messages_optional();
316 features.set_route_blinding_optional();
318 let chanmon_cfgs = create_chanmon_cfgs(6);
319 let node_cfgs = create_node_cfgs(6, &chanmon_cfgs);
321 *node_cfgs[1].override_init_features.borrow_mut() = Some(features);
323 let node_chanmgrs = create_node_chanmgrs(
324 6, &node_cfgs, &[None, Some(accept_forward_cfg), None, None, None, None]
326 let nodes = create_network(6, &node_cfgs, &node_chanmgrs);
328 create_unannounced_chan_between_nodes_with_value(&nodes, 0, 1, 10_000_000, 1_000_000_000);
329 create_unannounced_chan_between_nodes_with_value(&nodes, 2, 3, 10_000_000, 1_000_000_000);
330 create_announced_chan_between_nodes_with_value(&nodes, 1, 2, 10_000_000, 1_000_000_000);
331 create_announced_chan_between_nodes_with_value(&nodes, 1, 4, 10_000_000, 1_000_000_000);
332 create_announced_chan_between_nodes_with_value(&nodes, 1, 5, 10_000_000, 1_000_000_000);
333 create_announced_chan_between_nodes_with_value(&nodes, 2, 4, 10_000_000, 1_000_000_000);
334 create_announced_chan_between_nodes_with_value(&nodes, 2, 5, 10_000_000, 1_000_000_000);
336 // Add extra channels so that more than one of Bob's peers have MIN_PEER_CHANNELS and one has
337 // more than the others.
338 create_announced_chan_between_nodes_with_value(&nodes, 0, 4, 10_000_000, 1_000_000_000);
339 create_announced_chan_between_nodes_with_value(&nodes, 3, 4, 10_000_000, 1_000_000_000);
341 let (alice, bob, charlie, david) = (&nodes[0], &nodes[1], &nodes[2], &nodes[3]);
342 let bob_id = bob.node.get_our_node_id();
344 disconnect_peers(alice, &[charlie, david, &nodes[4], &nodes[5]]);
345 disconnect_peers(david, &[bob, &nodes[4], &nodes[5]]);
348 .create_offer_builder(None).unwrap()
349 .amount_msats(10_000_000)
351 assert_ne!(offer.signing_pubkey(), Some(bob_id));
352 assert!(!offer.paths().is_empty());
353 for path in offer.paths() {
354 let introduction_node_id = resolve_introduction_node(david, &path);
355 assert_eq!(introduction_node_id, nodes[4].node.get_our_node_id());
359 /// Checks that blinded paths are compact for short-lived offers.
361 fn creates_short_lived_offer() {
362 let chanmon_cfgs = create_chanmon_cfgs(2);
363 let node_cfgs = create_node_cfgs(2, &chanmon_cfgs);
364 let node_chanmgrs = create_node_chanmgrs(2, &node_cfgs, &[None, None]);
365 let nodes = create_network(2, &node_cfgs, &node_chanmgrs);
367 create_announced_chan_between_nodes_with_value(&nodes, 0, 1, 10_000_000, 1_000_000_000);
369 let alice = &nodes[0];
370 let alice_id = alice.node.get_our_node_id();
373 let absolute_expiry = alice.node.duration_since_epoch() + MAX_SHORT_LIVED_RELATIVE_EXPIRY;
374 let offer = alice.node
375 .create_offer_builder(Some(absolute_expiry)).unwrap()
377 assert_eq!(offer.absolute_expiry(), Some(absolute_expiry));
378 assert!(!offer.paths().is_empty());
379 for path in offer.paths() {
380 let introduction_node_id = resolve_introduction_node(bob, &path);
381 assert_eq!(introduction_node_id, alice_id);
382 assert!(matches!(path.introduction_node, IntroductionNode::DirectedShortChannelId(..)));
386 /// Checks that blinded paths are not compact for long-lived offers.
388 fn creates_long_lived_offer() {
389 let chanmon_cfgs = create_chanmon_cfgs(2);
390 let node_cfgs = create_node_cfgs(2, &chanmon_cfgs);
391 let node_chanmgrs = create_node_chanmgrs(2, &node_cfgs, &[None, None]);
392 let nodes = create_network(2, &node_cfgs, &node_chanmgrs);
394 create_announced_chan_between_nodes_with_value(&nodes, 0, 1, 10_000_000, 1_000_000_000);
396 let alice = &nodes[0];
397 let alice_id = alice.node.get_our_node_id();
399 let absolute_expiry = alice.node.duration_since_epoch() + MAX_SHORT_LIVED_RELATIVE_EXPIRY
400 + Duration::from_secs(1);
401 let offer = alice.node
402 .create_offer_builder(Some(absolute_expiry))
405 assert_eq!(offer.absolute_expiry(), Some(absolute_expiry));
406 assert!(!offer.paths().is_empty());
407 for path in offer.paths() {
408 assert_eq!(path.introduction_node, IntroductionNode::NodeId(alice_id));
411 let offer = alice.node
412 .create_offer_builder(None).unwrap()
414 assert_eq!(offer.absolute_expiry(), None);
415 assert!(!offer.paths().is_empty());
416 for path in offer.paths() {
417 assert_eq!(path.introduction_node, IntroductionNode::NodeId(alice_id));
421 /// Checks that blinded paths are compact for short-lived refunds.
423 fn creates_short_lived_refund() {
424 let chanmon_cfgs = create_chanmon_cfgs(2);
425 let node_cfgs = create_node_cfgs(2, &chanmon_cfgs);
426 let node_chanmgrs = create_node_chanmgrs(2, &node_cfgs, &[None, None]);
427 let nodes = create_network(2, &node_cfgs, &node_chanmgrs);
429 create_announced_chan_between_nodes_with_value(&nodes, 0, 1, 10_000_000, 1_000_000_000);
431 let alice = &nodes[0];
433 let bob_id = bob.node.get_our_node_id();
435 let absolute_expiry = bob.node.duration_since_epoch() + MAX_SHORT_LIVED_RELATIVE_EXPIRY;
436 let payment_id = PaymentId([1; 32]);
437 let refund = bob.node
438 .create_refund_builder(10_000_000, absolute_expiry, payment_id, Retry::Attempts(0), None)
441 assert_eq!(refund.absolute_expiry(), Some(absolute_expiry));
442 assert!(!refund.paths().is_empty());
443 for path in refund.paths() {
444 let introduction_node_id = resolve_introduction_node(alice, &path);
445 assert_eq!(introduction_node_id, bob_id);
446 assert!(matches!(path.introduction_node, IntroductionNode::DirectedShortChannelId(..)));
450 /// Checks that blinded paths are not compact for long-lived refunds.
452 fn creates_long_lived_refund() {
453 let chanmon_cfgs = create_chanmon_cfgs(2);
454 let node_cfgs = create_node_cfgs(2, &chanmon_cfgs);
455 let node_chanmgrs = create_node_chanmgrs(2, &node_cfgs, &[None, None]);
456 let nodes = create_network(2, &node_cfgs, &node_chanmgrs);
458 create_announced_chan_between_nodes_with_value(&nodes, 0, 1, 10_000_000, 1_000_000_000);
461 let bob_id = bob.node.get_our_node_id();
463 let absolute_expiry = bob.node.duration_since_epoch() + MAX_SHORT_LIVED_RELATIVE_EXPIRY
464 + Duration::from_secs(1);
465 let payment_id = PaymentId([1; 32]);
466 let refund = bob.node
467 .create_refund_builder(10_000_000, absolute_expiry, payment_id, Retry::Attempts(0), None)
470 assert_eq!(refund.absolute_expiry(), Some(absolute_expiry));
471 assert!(!refund.paths().is_empty());
472 for path in refund.paths() {
473 assert_eq!(path.introduction_node, IntroductionNode::NodeId(bob_id));
477 /// Checks that an offer can be paid through blinded paths and that ephemeral pubkeys are used
478 /// rather than exposing a node's pubkey.
480 fn creates_and_pays_for_offer_using_two_hop_blinded_path() {
481 let mut accept_forward_cfg = test_default_channel_config();
482 accept_forward_cfg.accept_forwards_to_priv_channels = true;
484 let mut features = channelmanager::provided_init_features(&accept_forward_cfg);
485 features.set_onion_messages_optional();
486 features.set_route_blinding_optional();
488 let chanmon_cfgs = create_chanmon_cfgs(6);
489 let node_cfgs = create_node_cfgs(6, &chanmon_cfgs);
491 *node_cfgs[1].override_init_features.borrow_mut() = Some(features);
493 let node_chanmgrs = create_node_chanmgrs(
494 6, &node_cfgs, &[None, Some(accept_forward_cfg), None, None, None, None]
496 let nodes = create_network(6, &node_cfgs, &node_chanmgrs);
498 create_unannounced_chan_between_nodes_with_value(&nodes, 0, 1, 10_000_000, 1_000_000_000);
499 create_unannounced_chan_between_nodes_with_value(&nodes, 2, 3, 10_000_000, 1_000_000_000);
500 create_announced_chan_between_nodes_with_value(&nodes, 1, 2, 10_000_000, 1_000_000_000);
501 create_announced_chan_between_nodes_with_value(&nodes, 1, 4, 10_000_000, 1_000_000_000);
502 create_announced_chan_between_nodes_with_value(&nodes, 1, 5, 10_000_000, 1_000_000_000);
503 create_announced_chan_between_nodes_with_value(&nodes, 2, 4, 10_000_000, 1_000_000_000);
504 create_announced_chan_between_nodes_with_value(&nodes, 2, 5, 10_000_000, 1_000_000_000);
506 let (alice, bob, charlie, david) = (&nodes[0], &nodes[1], &nodes[2], &nodes[3]);
507 let alice_id = alice.node.get_our_node_id();
508 let bob_id = bob.node.get_our_node_id();
509 let charlie_id = charlie.node.get_our_node_id();
510 let david_id = david.node.get_our_node_id();
512 disconnect_peers(alice, &[charlie, david, &nodes[4], &nodes[5]]);
513 disconnect_peers(david, &[bob, &nodes[4], &nodes[5]]);
515 let offer = alice.node
516 .create_offer_builder(None)
518 .amount_msats(10_000_000)
520 assert_ne!(offer.signing_pubkey(), Some(alice_id));
521 assert!(!offer.paths().is_empty());
522 for path in offer.paths() {
523 assert_eq!(path.introduction_node, IntroductionNode::NodeId(bob_id));
526 let payment_id = PaymentId([1; 32]);
527 david.node.pay_for_offer(&offer, None, None, None, payment_id, Retry::Attempts(0), None)
529 expect_recent_payment!(david, RecentPaymentDetails::AwaitingInvoice, payment_id);
531 connect_peers(david, bob);
533 let onion_message = david.onion_messenger.next_onion_message_for_peer(bob_id).unwrap();
534 bob.onion_messenger.handle_onion_message(&david_id, &onion_message);
536 connect_peers(alice, charlie);
538 let onion_message = bob.onion_messenger.next_onion_message_for_peer(alice_id).unwrap();
539 alice.onion_messenger.handle_onion_message(&bob_id, &onion_message);
541 let (invoice_request, reply_path) = extract_invoice_request(alice, &onion_message);
542 let payment_context = PaymentContext::Bolt12Offer(Bolt12OfferContext {
543 offer_id: offer.id(),
544 invoice_request: InvoiceRequestFields {
545 payer_id: invoice_request.payer_id(),
547 payer_note_truncated: None,
550 assert_eq!(invoice_request.amount_msats(), None);
551 assert_ne!(invoice_request.payer_id(), david_id);
552 assert_eq!(reply_path.introduction_node, IntroductionNode::NodeId(charlie_id));
554 let onion_message = alice.onion_messenger.next_onion_message_for_peer(charlie_id).unwrap();
555 charlie.onion_messenger.handle_onion_message(&alice_id, &onion_message);
557 let onion_message = charlie.onion_messenger.next_onion_message_for_peer(david_id).unwrap();
558 david.onion_messenger.handle_onion_message(&charlie_id, &onion_message);
560 let invoice = extract_invoice(david, &onion_message);
561 assert_eq!(invoice.amount_msats(), 10_000_000);
562 assert_ne!(invoice.signing_pubkey(), alice_id);
563 assert!(!invoice.payment_paths().is_empty());
564 for (_, path) in invoice.payment_paths() {
565 assert_eq!(path.introduction_node, IntroductionNode::NodeId(bob_id));
568 route_bolt12_payment(david, &[charlie, bob, alice], &invoice);
569 expect_recent_payment!(david, RecentPaymentDetails::Pending, payment_id);
571 claim_bolt12_payment(david, &[charlie, bob, alice], payment_context);
572 expect_recent_payment!(david, RecentPaymentDetails::Fulfilled, payment_id);
575 /// Checks that a refund can be paid through blinded paths and that ephemeral pubkeys are used
576 /// rather than exposing a node's pubkey.
578 fn creates_and_pays_for_refund_using_two_hop_blinded_path() {
579 let mut accept_forward_cfg = test_default_channel_config();
580 accept_forward_cfg.accept_forwards_to_priv_channels = true;
582 let mut features = channelmanager::provided_init_features(&accept_forward_cfg);
583 features.set_onion_messages_optional();
584 features.set_route_blinding_optional();
586 let chanmon_cfgs = create_chanmon_cfgs(6);
587 let node_cfgs = create_node_cfgs(6, &chanmon_cfgs);
589 *node_cfgs[1].override_init_features.borrow_mut() = Some(features);
591 let node_chanmgrs = create_node_chanmgrs(
592 6, &node_cfgs, &[None, Some(accept_forward_cfg), None, None, None, None]
594 let nodes = create_network(6, &node_cfgs, &node_chanmgrs);
596 create_unannounced_chan_between_nodes_with_value(&nodes, 0, 1, 10_000_000, 1_000_000_000);
597 create_unannounced_chan_between_nodes_with_value(&nodes, 2, 3, 10_000_000, 1_000_000_000);
598 create_announced_chan_between_nodes_with_value(&nodes, 1, 2, 10_000_000, 1_000_000_000);
599 create_announced_chan_between_nodes_with_value(&nodes, 1, 4, 10_000_000, 1_000_000_000);
600 create_announced_chan_between_nodes_with_value(&nodes, 1, 5, 10_000_000, 1_000_000_000);
601 create_announced_chan_between_nodes_with_value(&nodes, 2, 4, 10_000_000, 1_000_000_000);
602 create_announced_chan_between_nodes_with_value(&nodes, 2, 5, 10_000_000, 1_000_000_000);
604 let (alice, bob, charlie, david) = (&nodes[0], &nodes[1], &nodes[2], &nodes[3]);
605 let alice_id = alice.node.get_our_node_id();
606 let bob_id = bob.node.get_our_node_id();
607 let charlie_id = charlie.node.get_our_node_id();
608 let david_id = david.node.get_our_node_id();
610 disconnect_peers(alice, &[charlie, david, &nodes[4], &nodes[5]]);
611 disconnect_peers(david, &[bob, &nodes[4], &nodes[5]]);
613 let absolute_expiry = Duration::from_secs(u64::MAX);
614 let payment_id = PaymentId([1; 32]);
615 let refund = david.node
616 .create_refund_builder(10_000_000, absolute_expiry, payment_id, Retry::Attempts(0), None)
619 assert_eq!(refund.amount_msats(), 10_000_000);
620 assert_eq!(refund.absolute_expiry(), Some(absolute_expiry));
621 assert_ne!(refund.payer_id(), david_id);
622 assert!(!refund.paths().is_empty());
623 for path in refund.paths() {
624 assert_eq!(path.introduction_node, IntroductionNode::NodeId(charlie_id));
626 expect_recent_payment!(david, RecentPaymentDetails::AwaitingInvoice, payment_id);
628 let payment_context = PaymentContext::Bolt12Refund(Bolt12RefundContext {});
629 let expected_invoice = alice.node.request_refund_payment(&refund).unwrap();
631 connect_peers(alice, charlie);
633 let onion_message = alice.onion_messenger.next_onion_message_for_peer(charlie_id).unwrap();
634 charlie.onion_messenger.handle_onion_message(&alice_id, &onion_message);
636 let onion_message = charlie.onion_messenger.next_onion_message_for_peer(david_id).unwrap();
637 david.onion_messenger.handle_onion_message(&charlie_id, &onion_message);
639 let invoice = extract_invoice(david, &onion_message);
640 assert_eq!(invoice, expected_invoice);
642 assert_eq!(invoice.amount_msats(), 10_000_000);
643 assert_ne!(invoice.signing_pubkey(), alice_id);
644 assert!(!invoice.payment_paths().is_empty());
645 for (_, path) in invoice.payment_paths() {
646 assert_eq!(path.introduction_node, IntroductionNode::NodeId(bob_id));
649 route_bolt12_payment(david, &[charlie, bob, alice], &invoice);
650 expect_recent_payment!(david, RecentPaymentDetails::Pending, payment_id);
652 claim_bolt12_payment(david, &[charlie, bob, alice], payment_context);
653 expect_recent_payment!(david, RecentPaymentDetails::Fulfilled, payment_id);
656 /// Checks that an offer can be paid through a one-hop blinded path and that ephemeral pubkeys are
657 /// used rather than exposing a node's pubkey. However, the node's pubkey is still used as the
658 /// introduction node of the blinded path.
660 fn creates_and_pays_for_offer_using_one_hop_blinded_path() {
661 let chanmon_cfgs = create_chanmon_cfgs(2);
662 let node_cfgs = create_node_cfgs(2, &chanmon_cfgs);
663 let node_chanmgrs = create_node_chanmgrs(2, &node_cfgs, &[None, None]);
664 let nodes = create_network(2, &node_cfgs, &node_chanmgrs);
666 create_announced_chan_between_nodes_with_value(&nodes, 0, 1, 10_000_000, 1_000_000_000);
668 let alice = &nodes[0];
669 let alice_id = alice.node.get_our_node_id();
671 let bob_id = bob.node.get_our_node_id();
673 let offer = alice.node
674 .create_offer_builder(None).unwrap()
675 .amount_msats(10_000_000)
677 assert_ne!(offer.signing_pubkey(), Some(alice_id));
678 assert!(!offer.paths().is_empty());
679 for path in offer.paths() {
680 assert_eq!(path.introduction_node, IntroductionNode::NodeId(alice_id));
683 let payment_id = PaymentId([1; 32]);
684 bob.node.pay_for_offer(&offer, None, None, None, payment_id, Retry::Attempts(0), None).unwrap();
685 expect_recent_payment!(bob, RecentPaymentDetails::AwaitingInvoice, payment_id);
687 let onion_message = bob.onion_messenger.next_onion_message_for_peer(alice_id).unwrap();
688 alice.onion_messenger.handle_onion_message(&bob_id, &onion_message);
690 let (invoice_request, reply_path) = extract_invoice_request(alice, &onion_message);
691 let payment_context = PaymentContext::Bolt12Offer(Bolt12OfferContext {
692 offer_id: offer.id(),
693 invoice_request: InvoiceRequestFields {
694 payer_id: invoice_request.payer_id(),
696 payer_note_truncated: None,
699 assert_eq!(invoice_request.amount_msats(), None);
700 assert_ne!(invoice_request.payer_id(), bob_id);
701 assert_eq!(reply_path.introduction_node, IntroductionNode::NodeId(bob_id));
703 let onion_message = alice.onion_messenger.next_onion_message_for_peer(bob_id).unwrap();
704 bob.onion_messenger.handle_onion_message(&alice_id, &onion_message);
706 let invoice = extract_invoice(bob, &onion_message);
707 assert_eq!(invoice.amount_msats(), 10_000_000);
708 assert_ne!(invoice.signing_pubkey(), alice_id);
709 assert!(!invoice.payment_paths().is_empty());
710 for (_, path) in invoice.payment_paths() {
711 assert_eq!(path.introduction_node, IntroductionNode::NodeId(alice_id));
714 route_bolt12_payment(bob, &[alice], &invoice);
715 expect_recent_payment!(bob, RecentPaymentDetails::Pending, payment_id);
717 claim_bolt12_payment(bob, &[alice], payment_context);
718 expect_recent_payment!(bob, RecentPaymentDetails::Fulfilled, payment_id);
721 /// Checks that a refund can be paid through a one-hop blinded path and that ephemeral pubkeys are
722 /// used rather than exposing a node's pubkey. However, the node's pubkey is still used as the
723 /// introduction node of the blinded path.
725 fn creates_and_pays_for_refund_using_one_hop_blinded_path() {
726 let chanmon_cfgs = create_chanmon_cfgs(2);
727 let node_cfgs = create_node_cfgs(2, &chanmon_cfgs);
728 let node_chanmgrs = create_node_chanmgrs(2, &node_cfgs, &[None, None]);
729 let nodes = create_network(2, &node_cfgs, &node_chanmgrs);
731 create_announced_chan_between_nodes_with_value(&nodes, 0, 1, 10_000_000, 1_000_000_000);
733 let alice = &nodes[0];
734 let alice_id = alice.node.get_our_node_id();
736 let bob_id = bob.node.get_our_node_id();
738 let absolute_expiry = Duration::from_secs(u64::MAX);
739 let payment_id = PaymentId([1; 32]);
740 let refund = bob.node
741 .create_refund_builder(10_000_000, absolute_expiry, payment_id, Retry::Attempts(0), None)
744 assert_eq!(refund.amount_msats(), 10_000_000);
745 assert_eq!(refund.absolute_expiry(), Some(absolute_expiry));
746 assert_ne!(refund.payer_id(), bob_id);
747 assert!(!refund.paths().is_empty());
748 for path in refund.paths() {
749 assert_eq!(path.introduction_node, IntroductionNode::NodeId(bob_id));
751 expect_recent_payment!(bob, RecentPaymentDetails::AwaitingInvoice, payment_id);
753 let payment_context = PaymentContext::Bolt12Refund(Bolt12RefundContext {});
754 let expected_invoice = alice.node.request_refund_payment(&refund).unwrap();
756 let onion_message = alice.onion_messenger.next_onion_message_for_peer(bob_id).unwrap();
757 bob.onion_messenger.handle_onion_message(&alice_id, &onion_message);
759 let invoice = extract_invoice(bob, &onion_message);
760 assert_eq!(invoice, expected_invoice);
762 assert_eq!(invoice.amount_msats(), 10_000_000);
763 assert_ne!(invoice.signing_pubkey(), alice_id);
764 assert!(!invoice.payment_paths().is_empty());
765 for (_, path) in invoice.payment_paths() {
766 assert_eq!(path.introduction_node, IntroductionNode::NodeId(alice_id));
769 route_bolt12_payment(bob, &[alice], &invoice);
770 expect_recent_payment!(bob, RecentPaymentDetails::Pending, payment_id);
772 claim_bolt12_payment(bob, &[alice], payment_context);
773 expect_recent_payment!(bob, RecentPaymentDetails::Fulfilled, payment_id);
776 /// Checks that an invoice for an offer without any blinded paths can be requested. Note that while
777 /// the requested is sent directly using the node's pubkey, the response and the payment still use
778 /// blinded paths as required by the spec.
780 fn pays_for_offer_without_blinded_paths() {
781 let chanmon_cfgs = create_chanmon_cfgs(2);
782 let node_cfgs = create_node_cfgs(2, &chanmon_cfgs);
783 let node_chanmgrs = create_node_chanmgrs(2, &node_cfgs, &[None, None]);
784 let nodes = create_network(2, &node_cfgs, &node_chanmgrs);
786 create_announced_chan_between_nodes_with_value(&nodes, 0, 1, 10_000_000, 1_000_000_000);
788 let alice = &nodes[0];
789 let alice_id = alice.node.get_our_node_id();
791 let bob_id = bob.node.get_our_node_id();
793 let offer = alice.node
794 .create_offer_builder(None).unwrap()
796 .amount_msats(10_000_000)
798 assert_eq!(offer.signing_pubkey(), Some(alice_id));
799 assert!(offer.paths().is_empty());
801 let payment_id = PaymentId([1; 32]);
802 bob.node.pay_for_offer(&offer, None, None, None, payment_id, Retry::Attempts(0), None).unwrap();
803 expect_recent_payment!(bob, RecentPaymentDetails::AwaitingInvoice, payment_id);
805 let onion_message = bob.onion_messenger.next_onion_message_for_peer(alice_id).unwrap();
806 alice.onion_messenger.handle_onion_message(&bob_id, &onion_message);
808 let (invoice_request, _) = extract_invoice_request(alice, &onion_message);
809 let payment_context = PaymentContext::Bolt12Offer(Bolt12OfferContext {
810 offer_id: offer.id(),
811 invoice_request: InvoiceRequestFields {
812 payer_id: invoice_request.payer_id(),
814 payer_note_truncated: None,
818 let onion_message = alice.onion_messenger.next_onion_message_for_peer(bob_id).unwrap();
819 bob.onion_messenger.handle_onion_message(&alice_id, &onion_message);
821 let invoice = extract_invoice(bob, &onion_message);
822 route_bolt12_payment(bob, &[alice], &invoice);
823 expect_recent_payment!(bob, RecentPaymentDetails::Pending, payment_id);
825 claim_bolt12_payment(bob, &[alice], payment_context);
826 expect_recent_payment!(bob, RecentPaymentDetails::Fulfilled, payment_id);
829 /// Checks that a refund without any blinded paths can be paid. Note that while the invoice is sent
830 /// directly using the node's pubkey, the payment still use blinded paths as required by the spec.
832 fn pays_for_refund_without_blinded_paths() {
833 let chanmon_cfgs = create_chanmon_cfgs(2);
834 let node_cfgs = create_node_cfgs(2, &chanmon_cfgs);
835 let node_chanmgrs = create_node_chanmgrs(2, &node_cfgs, &[None, None]);
836 let nodes = create_network(2, &node_cfgs, &node_chanmgrs);
838 create_announced_chan_between_nodes_with_value(&nodes, 0, 1, 10_000_000, 1_000_000_000);
840 let alice = &nodes[0];
841 let alice_id = alice.node.get_our_node_id();
843 let bob_id = bob.node.get_our_node_id();
845 let absolute_expiry = Duration::from_secs(u64::MAX);
846 let payment_id = PaymentId([1; 32]);
847 let refund = bob.node
848 .create_refund_builder(10_000_000, absolute_expiry, payment_id, Retry::Attempts(0), None)
852 assert_eq!(refund.payer_id(), bob_id);
853 assert!(refund.paths().is_empty());
854 expect_recent_payment!(bob, RecentPaymentDetails::AwaitingInvoice, payment_id);
856 let payment_context = PaymentContext::Bolt12Refund(Bolt12RefundContext {});
857 let expected_invoice = alice.node.request_refund_payment(&refund).unwrap();
859 let onion_message = alice.onion_messenger.next_onion_message_for_peer(bob_id).unwrap();
860 bob.onion_messenger.handle_onion_message(&alice_id, &onion_message);
862 let invoice = extract_invoice(bob, &onion_message);
863 assert_eq!(invoice, expected_invoice);
865 route_bolt12_payment(bob, &[alice], &invoice);
866 expect_recent_payment!(bob, RecentPaymentDetails::Pending, payment_id);
868 claim_bolt12_payment(bob, &[alice], payment_context);
869 expect_recent_payment!(bob, RecentPaymentDetails::Fulfilled, payment_id);
872 /// Checks that a deferred invoice can be paid asynchronously from an Event::InvoiceReceived.
874 fn pays_bolt12_invoice_asynchronously() {
875 let mut manually_pay_cfg = test_default_channel_config();
876 manually_pay_cfg.manually_handle_bolt12_invoices = true;
878 let chanmon_cfgs = create_chanmon_cfgs(2);
879 let node_cfgs = create_node_cfgs(2, &chanmon_cfgs);
880 let node_chanmgrs = create_node_chanmgrs(2, &node_cfgs, &[None, Some(manually_pay_cfg)]);
881 let nodes = create_network(2, &node_cfgs, &node_chanmgrs);
883 create_announced_chan_between_nodes_with_value(&nodes, 0, 1, 10_000_000, 1_000_000_000);
885 let alice = &nodes[0];
886 let alice_id = alice.node.get_our_node_id();
888 let bob_id = bob.node.get_our_node_id();
890 let offer = alice.node
891 .create_offer_builder(None).unwrap()
892 .amount_msats(10_000_000)
895 let payment_id = PaymentId([1; 32]);
896 bob.node.pay_for_offer(&offer, None, None, None, payment_id, Retry::Attempts(0), None).unwrap();
897 expect_recent_payment!(bob, RecentPaymentDetails::AwaitingInvoice, payment_id);
899 let onion_message = bob.onion_messenger.next_onion_message_for_peer(alice_id).unwrap();
900 alice.onion_messenger.handle_onion_message(&bob_id, &onion_message);
902 let (invoice_request, _) = extract_invoice_request(alice, &onion_message);
903 let payment_context = PaymentContext::Bolt12Offer(Bolt12OfferContext {
904 offer_id: offer.id(),
905 invoice_request: InvoiceRequestFields {
906 payer_id: invoice_request.payer_id(),
908 payer_note_truncated: None,
912 let onion_message = alice.onion_messenger.next_onion_message_for_peer(bob_id).unwrap();
913 bob.onion_messenger.handle_onion_message(&alice_id, &onion_message);
915 let invoice = match get_event!(bob, Event::InvoiceReceived) {
916 Event::InvoiceReceived { payment_id: actual_payment_id, invoice, .. } => {
917 assert_eq!(actual_payment_id, payment_id);
920 _ => panic!("No Event::InvoiceReceived"),
922 assert_eq!(invoice.amount_msats(), 10_000_000);
923 assert_ne!(invoice.signing_pubkey(), alice_id);
924 assert!(!invoice.payment_paths().is_empty());
925 for (_, path) in invoice.payment_paths() {
926 assert_eq!(path.introduction_node, IntroductionNode::NodeId(alice_id));
929 assert!(bob.node.send_payment_for_bolt12_invoice(&invoice).is_ok());
931 bob.node.send_payment_for_bolt12_invoice(&invoice),
932 Err(Bolt12PaymentError::DuplicateInvoice),
935 route_bolt12_payment(bob, &[alice], &invoice);
936 expect_recent_payment!(bob, RecentPaymentDetails::Pending, payment_id);
938 claim_bolt12_payment(bob, &[alice], payment_context);
939 expect_recent_payment!(bob, RecentPaymentDetails::Fulfilled, payment_id);
942 bob.node.send_payment_for_bolt12_invoice(&invoice),
943 Err(Bolt12PaymentError::DuplicateInvoice),
946 for _ in 0..=IDEMPOTENCY_TIMEOUT_TICKS {
947 bob.node.timer_tick_occurred();
951 bob.node.send_payment_for_bolt12_invoice(&invoice),
952 Err(Bolt12PaymentError::UnexpectedInvoice),
956 /// Fails creating an offer when a blinded path cannot be created without exposing the node's id.
958 fn fails_creating_offer_without_blinded_paths() {
959 let chanmon_cfgs = create_chanmon_cfgs(2);
960 let node_cfgs = create_node_cfgs(2, &chanmon_cfgs);
961 let node_chanmgrs = create_node_chanmgrs(2, &node_cfgs, &[None, None]);
962 let nodes = create_network(2, &node_cfgs, &node_chanmgrs);
964 create_unannounced_chan_between_nodes_with_value(&nodes, 0, 1, 10_000_000, 1_000_000_000);
966 match nodes[0].node.create_offer_builder(None) {
967 Ok(_) => panic!("Expected error"),
968 Err(e) => assert_eq!(e, Bolt12SemanticError::MissingPaths),
972 /// Fails creating a refund when a blinded path cannot be created without exposing the node's id.
974 fn fails_creating_refund_without_blinded_paths() {
975 let chanmon_cfgs = create_chanmon_cfgs(2);
976 let node_cfgs = create_node_cfgs(2, &chanmon_cfgs);
977 let node_chanmgrs = create_node_chanmgrs(2, &node_cfgs, &[None, None]);
978 let nodes = create_network(2, &node_cfgs, &node_chanmgrs);
980 create_unannounced_chan_between_nodes_with_value(&nodes, 0, 1, 10_000_000, 1_000_000_000);
982 let absolute_expiry = Duration::from_secs(u64::MAX);
983 let payment_id = PaymentId([1; 32]);
985 match nodes[0].node.create_refund_builder(
986 10_000, absolute_expiry, payment_id, Retry::Attempts(0), None
988 Ok(_) => panic!("Expected error"),
989 Err(e) => assert_eq!(e, Bolt12SemanticError::MissingPaths),
992 assert!(nodes[0].node.list_recent_payments().is_empty());
995 /// Fails creating or paying an offer when a blinded path cannot be created because no peers are
998 fn fails_creating_or_paying_for_offer_without_connected_peers() {
999 let chanmon_cfgs = create_chanmon_cfgs(6);
1000 let node_cfgs = create_node_cfgs(6, &chanmon_cfgs);
1001 let node_chanmgrs = create_node_chanmgrs(6, &node_cfgs, &[None, None, None, None, None, None]);
1002 let nodes = create_network(6, &node_cfgs, &node_chanmgrs);
1004 create_unannounced_chan_between_nodes_with_value(&nodes, 0, 1, 10_000_000, 1_000_000_000);
1005 create_unannounced_chan_between_nodes_with_value(&nodes, 2, 3, 10_000_000, 1_000_000_000);
1006 create_announced_chan_between_nodes_with_value(&nodes, 1, 2, 10_000_000, 1_000_000_000);
1007 create_announced_chan_between_nodes_with_value(&nodes, 1, 4, 10_000_000, 1_000_000_000);
1008 create_announced_chan_between_nodes_with_value(&nodes, 1, 5, 10_000_000, 1_000_000_000);
1009 create_announced_chan_between_nodes_with_value(&nodes, 2, 4, 10_000_000, 1_000_000_000);
1010 create_announced_chan_between_nodes_with_value(&nodes, 2, 5, 10_000_000, 1_000_000_000);
1012 let (alice, bob, charlie, david) = (&nodes[0], &nodes[1], &nodes[2], &nodes[3]);
1014 disconnect_peers(alice, &[bob, charlie, david, &nodes[4], &nodes[5]]);
1015 disconnect_peers(david, &[bob, charlie, &nodes[4], &nodes[5]]);
1017 let absolute_expiry = alice.node.duration_since_epoch() + MAX_SHORT_LIVED_RELATIVE_EXPIRY;
1018 match alice.node.create_offer_builder(Some(absolute_expiry)) {
1019 Ok(_) => panic!("Expected error"),
1020 Err(e) => assert_eq!(e, Bolt12SemanticError::MissingPaths),
1023 let mut args = ReconnectArgs::new(alice, bob);
1024 args.send_channel_ready = (true, true);
1025 reconnect_nodes(args);
1027 let offer = alice.node
1028 .create_offer_builder(Some(absolute_expiry)).unwrap()
1029 .amount_msats(10_000_000)
1032 let payment_id = PaymentId([1; 32]);
1034 match david.node.pay_for_offer(&offer, None, None, None, payment_id, Retry::Attempts(0), None) {
1035 Ok(_) => panic!("Expected error"),
1036 Err(e) => assert_eq!(e, Bolt12SemanticError::MissingPaths),
1039 assert!(nodes[0].node.list_recent_payments().is_empty());
1041 let mut args = ReconnectArgs::new(charlie, david);
1042 args.send_channel_ready = (true, true);
1043 reconnect_nodes(args);
1046 david.node.pay_for_offer(
1047 &offer, None, None, None, payment_id, Retry::Attempts(0), None
1051 expect_recent_payment!(david, RecentPaymentDetails::AwaitingInvoice, payment_id);
1054 /// Fails creating or sending an invoice for a refund when a blinded path cannot be created because
1055 /// no peers are connected.
1057 fn fails_creating_refund_or_sending_invoice_without_connected_peers() {
1058 let mut accept_forward_cfg = test_default_channel_config();
1059 accept_forward_cfg.accept_forwards_to_priv_channels = true;
1061 let mut features = channelmanager::provided_init_features(&accept_forward_cfg);
1062 features.set_onion_messages_optional();
1063 features.set_route_blinding_optional();
1065 let chanmon_cfgs = create_chanmon_cfgs(6);
1066 let node_cfgs = create_node_cfgs(6, &chanmon_cfgs);
1068 *node_cfgs[1].override_init_features.borrow_mut() = Some(features);
1070 let node_chanmgrs = create_node_chanmgrs(
1071 6, &node_cfgs, &[None, Some(accept_forward_cfg), None, None, None, None]
1073 let nodes = create_network(6, &node_cfgs, &node_chanmgrs);
1075 create_unannounced_chan_between_nodes_with_value(&nodes, 0, 1, 10_000_000, 1_000_000_000);
1076 create_unannounced_chan_between_nodes_with_value(&nodes, 2, 3, 10_000_000, 1_000_000_000);
1077 create_announced_chan_between_nodes_with_value(&nodes, 1, 2, 10_000_000, 1_000_000_000);
1078 create_announced_chan_between_nodes_with_value(&nodes, 1, 4, 10_000_000, 1_000_000_000);
1079 create_announced_chan_between_nodes_with_value(&nodes, 1, 5, 10_000_000, 1_000_000_000);
1080 create_announced_chan_between_nodes_with_value(&nodes, 2, 4, 10_000_000, 1_000_000_000);
1081 create_announced_chan_between_nodes_with_value(&nodes, 2, 5, 10_000_000, 1_000_000_000);
1083 let (alice, bob, charlie, david) = (&nodes[0], &nodes[1], &nodes[2], &nodes[3]);
1085 disconnect_peers(alice, &[bob, charlie, david, &nodes[4], &nodes[5]]);
1086 disconnect_peers(david, &[bob, charlie, &nodes[4], &nodes[5]]);
1088 let absolute_expiry = david.node.duration_since_epoch() + MAX_SHORT_LIVED_RELATIVE_EXPIRY;
1089 let payment_id = PaymentId([1; 32]);
1090 match david.node.create_refund_builder(
1091 10_000_000, absolute_expiry, payment_id, Retry::Attempts(0), None
1093 Ok(_) => panic!("Expected error"),
1094 Err(e) => assert_eq!(e, Bolt12SemanticError::MissingPaths),
1097 let mut args = ReconnectArgs::new(charlie, david);
1098 args.send_channel_ready = (true, true);
1099 reconnect_nodes(args);
1101 let refund = david.node
1102 .create_refund_builder(10_000_000, absolute_expiry, payment_id, Retry::Attempts(0), None)
1106 match alice.node.request_refund_payment(&refund) {
1107 Ok(_) => panic!("Expected error"),
1108 Err(e) => assert_eq!(e, Bolt12SemanticError::MissingPaths),
1111 let mut args = ReconnectArgs::new(alice, bob);
1112 args.send_channel_ready = (true, true);
1113 reconnect_nodes(args);
1115 assert!(alice.node.request_refund_payment(&refund).is_ok());
1118 /// Fails creating an invoice request when the offer contains an unsupported chain.
1120 fn fails_creating_invoice_request_for_unsupported_chain() {
1121 let chanmon_cfgs = create_chanmon_cfgs(2);
1122 let node_cfgs = create_node_cfgs(2, &chanmon_cfgs);
1123 let node_chanmgrs = create_node_chanmgrs(2, &node_cfgs, &[None, None]);
1124 let nodes = create_network(2, &node_cfgs, &node_chanmgrs);
1126 create_announced_chan_between_nodes_with_value(&nodes, 0, 1, 10_000_000, 1_000_000_000);
1128 let alice = &nodes[0];
1129 let bob = &nodes[1];
1131 let offer = alice.node
1132 .create_offer_builder(None).unwrap()
1134 .chain(Network::Signet)
1137 let payment_id = PaymentId([1; 32]);
1138 match bob.node.pay_for_offer(&offer, None, None, None, payment_id, Retry::Attempts(0), None) {
1139 Ok(_) => panic!("Expected error"),
1140 Err(e) => assert_eq!(e, Bolt12SemanticError::UnsupportedChain),
1144 /// Fails requesting a payment when the refund contains an unsupported chain.
1146 fn fails_sending_invoice_with_unsupported_chain_for_refund() {
1147 let chanmon_cfgs = create_chanmon_cfgs(2);
1148 let node_cfgs = create_node_cfgs(2, &chanmon_cfgs);
1149 let node_chanmgrs = create_node_chanmgrs(2, &node_cfgs, &[None, None]);
1150 let nodes = create_network(2, &node_cfgs, &node_chanmgrs);
1152 create_announced_chan_between_nodes_with_value(&nodes, 0, 1, 10_000_000, 1_000_000_000);
1154 let alice = &nodes[0];
1155 let bob = &nodes[1];
1157 let absolute_expiry = Duration::from_secs(u64::MAX);
1158 let payment_id = PaymentId([1; 32]);
1159 let refund = bob.node
1160 .create_refund_builder(10_000_000, absolute_expiry, payment_id, Retry::Attempts(0), None)
1162 .chain(Network::Signet)
1165 match alice.node.request_refund_payment(&refund) {
1166 Ok(_) => panic!("Expected error"),
1167 Err(e) => assert_eq!(e, Bolt12SemanticError::UnsupportedChain),
1171 /// Fails creating an invoice request when a blinded reply path cannot be created without exposing
1174 fn fails_creating_invoice_request_without_blinded_reply_path() {
1175 let chanmon_cfgs = create_chanmon_cfgs(6);
1176 let node_cfgs = create_node_cfgs(6, &chanmon_cfgs);
1177 let node_chanmgrs = create_node_chanmgrs(6, &node_cfgs, &[None, None, None, None, None, None]);
1178 let nodes = create_network(6, &node_cfgs, &node_chanmgrs);
1180 create_unannounced_chan_between_nodes_with_value(&nodes, 0, 1, 10_000_000, 1_000_000_000);
1181 create_unannounced_chan_between_nodes_with_value(&nodes, 2, 3, 10_000_000, 1_000_000_000);
1182 create_announced_chan_between_nodes_with_value(&nodes, 1, 2, 10_000_000, 1_000_000_000);
1183 create_announced_chan_between_nodes_with_value(&nodes, 1, 4, 10_000_000, 1_000_000_000);
1184 create_announced_chan_between_nodes_with_value(&nodes, 1, 5, 10_000_000, 1_000_000_000);
1186 let (alice, bob, charlie, david) = (&nodes[0], &nodes[1], &nodes[2], &nodes[3]);
1188 disconnect_peers(alice, &[charlie, david, &nodes[4], &nodes[5]]);
1189 disconnect_peers(david, &[bob, &nodes[4], &nodes[5]]);
1191 let offer = alice.node
1192 .create_offer_builder(None).unwrap()
1193 .amount_msats(10_000_000)
1196 let payment_id = PaymentId([1; 32]);
1198 match david.node.pay_for_offer(&offer, None, None, None, payment_id, Retry::Attempts(0), None) {
1199 Ok(_) => panic!("Expected error"),
1200 Err(e) => assert_eq!(e, Bolt12SemanticError::MissingPaths),
1203 assert!(nodes[0].node.list_recent_payments().is_empty());
1207 fn fails_creating_invoice_request_with_duplicate_payment_id() {
1208 let chanmon_cfgs = create_chanmon_cfgs(6);
1209 let node_cfgs = create_node_cfgs(6, &chanmon_cfgs);
1210 let node_chanmgrs = create_node_chanmgrs(6, &node_cfgs, &[None, None, None, None, None, None]);
1211 let nodes = create_network(6, &node_cfgs, &node_chanmgrs);
1213 create_unannounced_chan_between_nodes_with_value(&nodes, 0, 1, 10_000_000, 1_000_000_000);
1214 create_unannounced_chan_between_nodes_with_value(&nodes, 2, 3, 10_000_000, 1_000_000_000);
1215 create_announced_chan_between_nodes_with_value(&nodes, 1, 2, 10_000_000, 1_000_000_000);
1216 create_announced_chan_between_nodes_with_value(&nodes, 1, 4, 10_000_000, 1_000_000_000);
1217 create_announced_chan_between_nodes_with_value(&nodes, 1, 5, 10_000_000, 1_000_000_000);
1218 create_announced_chan_between_nodes_with_value(&nodes, 2, 4, 10_000_000, 1_000_000_000);
1219 create_announced_chan_between_nodes_with_value(&nodes, 2, 5, 10_000_000, 1_000_000_000);
1221 let (alice, _bob, charlie, david) = (&nodes[0], &nodes[1], &nodes[2], &nodes[3]);
1223 disconnect_peers(alice, &[charlie, david, &nodes[4], &nodes[5]]);
1225 let offer = alice.node
1226 .create_offer_builder(None).unwrap()
1227 .amount_msats(10_000_000)
1230 let payment_id = PaymentId([1; 32]);
1232 david.node.pay_for_offer(
1233 &offer, None, None, None, payment_id, Retry::Attempts(0), None
1236 expect_recent_payment!(david, RecentPaymentDetails::AwaitingInvoice, payment_id);
1238 match david.node.pay_for_offer(&offer, None, None, None, payment_id, Retry::Attempts(0), None) {
1239 Ok(_) => panic!("Expected error"),
1240 Err(e) => assert_eq!(e, Bolt12SemanticError::DuplicatePaymentId),
1243 expect_recent_payment!(david, RecentPaymentDetails::AwaitingInvoice, payment_id);
1247 fn fails_creating_refund_with_duplicate_payment_id() {
1248 let chanmon_cfgs = create_chanmon_cfgs(2);
1249 let node_cfgs = create_node_cfgs(2, &chanmon_cfgs);
1250 let node_chanmgrs = create_node_chanmgrs(2, &node_cfgs, &[None, None]);
1251 let nodes = create_network(2, &node_cfgs, &node_chanmgrs);
1253 create_announced_chan_between_nodes_with_value(&nodes, 0, 1, 10_000_000, 1_000_000_000);
1255 let absolute_expiry = Duration::from_secs(u64::MAX);
1256 let payment_id = PaymentId([1; 32]);
1258 nodes[0].node.create_refund_builder(
1259 10_000, absolute_expiry, payment_id, Retry::Attempts(0), None
1262 expect_recent_payment!(nodes[0], RecentPaymentDetails::AwaitingInvoice, payment_id);
1264 match nodes[0].node.create_refund_builder(
1265 10_000, absolute_expiry, payment_id, Retry::Attempts(0), None
1267 Ok(_) => panic!("Expected error"),
1268 Err(e) => assert_eq!(e, Bolt12SemanticError::DuplicatePaymentId),
1271 expect_recent_payment!(nodes[0], RecentPaymentDetails::AwaitingInvoice, payment_id);
1275 fn fails_sending_invoice_without_blinded_payment_paths_for_offer() {
1276 let mut accept_forward_cfg = test_default_channel_config();
1277 accept_forward_cfg.accept_forwards_to_priv_channels = true;
1279 // Clearing route_blinding prevents forming any payment paths since the node is unannounced.
1280 let mut features = channelmanager::provided_init_features(&accept_forward_cfg);
1281 features.set_onion_messages_optional();
1282 features.clear_route_blinding();
1284 let chanmon_cfgs = create_chanmon_cfgs(6);
1285 let node_cfgs = create_node_cfgs(6, &chanmon_cfgs);
1287 *node_cfgs[1].override_init_features.borrow_mut() = Some(features);
1289 let node_chanmgrs = create_node_chanmgrs(
1290 6, &node_cfgs, &[None, Some(accept_forward_cfg), None, None, None, None]
1292 let nodes = create_network(6, &node_cfgs, &node_chanmgrs);
1294 create_unannounced_chan_between_nodes_with_value(&nodes, 0, 1, 10_000_000, 1_000_000_000);
1295 create_unannounced_chan_between_nodes_with_value(&nodes, 2, 3, 10_000_000, 1_000_000_000);
1296 create_announced_chan_between_nodes_with_value(&nodes, 1, 2, 10_000_000, 1_000_000_000);
1297 create_announced_chan_between_nodes_with_value(&nodes, 1, 4, 10_000_000, 1_000_000_000);
1298 create_announced_chan_between_nodes_with_value(&nodes, 1, 5, 10_000_000, 1_000_000_000);
1299 create_announced_chan_between_nodes_with_value(&nodes, 2, 4, 10_000_000, 1_000_000_000);
1300 create_announced_chan_between_nodes_with_value(&nodes, 2, 5, 10_000_000, 1_000_000_000);
1302 let (alice, bob, charlie, david) = (&nodes[0], &nodes[1], &nodes[2], &nodes[3]);
1303 let alice_id = alice.node.get_our_node_id();
1304 let bob_id = bob.node.get_our_node_id();
1305 let charlie_id = charlie.node.get_our_node_id();
1306 let david_id = david.node.get_our_node_id();
1308 disconnect_peers(alice, &[charlie, david, &nodes[4], &nodes[5]]);
1309 disconnect_peers(david, &[bob, &nodes[4], &nodes[5]]);
1311 let offer = alice.node
1312 .create_offer_builder(None).unwrap()
1313 .amount_msats(10_000_000)
1316 let payment_id = PaymentId([1; 32]);
1317 david.node.pay_for_offer(&offer, None, None, None, payment_id, Retry::Attempts(0), None)
1320 connect_peers(david, bob);
1322 let onion_message = david.onion_messenger.next_onion_message_for_peer(bob_id).unwrap();
1323 bob.onion_messenger.handle_onion_message(&david_id, &onion_message);
1325 connect_peers(alice, charlie);
1327 let onion_message = bob.onion_messenger.next_onion_message_for_peer(alice_id).unwrap();
1328 alice.onion_messenger.handle_onion_message(&bob_id, &onion_message);
1330 let onion_message = alice.onion_messenger.next_onion_message_for_peer(charlie_id).unwrap();
1331 charlie.onion_messenger.handle_onion_message(&alice_id, &onion_message);
1333 let onion_message = charlie.onion_messenger.next_onion_message_for_peer(david_id).unwrap();
1334 david.onion_messenger.handle_onion_message(&charlie_id, &onion_message);
1336 let invoice_error = extract_invoice_error(david, &onion_message);
1337 assert_eq!(invoice_error, InvoiceError::from(Bolt12SemanticError::MissingPaths));
1341 fn fails_sending_invoice_without_blinded_payment_paths_for_refund() {
1342 let mut accept_forward_cfg = test_default_channel_config();
1343 accept_forward_cfg.accept_forwards_to_priv_channels = true;
1345 // Clearing route_blinding prevents forming any payment paths since the node is unannounced.
1346 let mut features = channelmanager::provided_init_features(&accept_forward_cfg);
1347 features.set_onion_messages_optional();
1348 features.clear_route_blinding();
1350 let chanmon_cfgs = create_chanmon_cfgs(6);
1351 let node_cfgs = create_node_cfgs(6, &chanmon_cfgs);
1353 *node_cfgs[1].override_init_features.borrow_mut() = Some(features);
1355 let node_chanmgrs = create_node_chanmgrs(
1356 6, &node_cfgs, &[None, Some(accept_forward_cfg), None, None, None, None]
1358 let nodes = create_network(6, &node_cfgs, &node_chanmgrs);
1360 create_unannounced_chan_between_nodes_with_value(&nodes, 0, 1, 10_000_000, 1_000_000_000);
1361 create_unannounced_chan_between_nodes_with_value(&nodes, 2, 3, 10_000_000, 1_000_000_000);
1362 create_announced_chan_between_nodes_with_value(&nodes, 1, 2, 10_000_000, 1_000_000_000);
1363 create_announced_chan_between_nodes_with_value(&nodes, 1, 4, 10_000_000, 1_000_000_000);
1364 create_announced_chan_between_nodes_with_value(&nodes, 1, 5, 10_000_000, 1_000_000_000);
1365 create_announced_chan_between_nodes_with_value(&nodes, 2, 4, 10_000_000, 1_000_000_000);
1366 create_announced_chan_between_nodes_with_value(&nodes, 2, 5, 10_000_000, 1_000_000_000);
1368 let (alice, bob, charlie, david) = (&nodes[0], &nodes[1], &nodes[2], &nodes[3]);
1370 disconnect_peers(alice, &[charlie, david, &nodes[4], &nodes[5]]);
1371 disconnect_peers(david, &[bob, &nodes[4], &nodes[5]]);
1373 let absolute_expiry = Duration::from_secs(u64::MAX);
1374 let payment_id = PaymentId([1; 32]);
1375 let refund = david.node
1376 .create_refund_builder(10_000_000, absolute_expiry, payment_id, Retry::Attempts(0), None)
1380 match alice.node.request_refund_payment(&refund) {
1381 Ok(_) => panic!("Expected error"),
1382 Err(e) => assert_eq!(e, Bolt12SemanticError::MissingPaths),
1387 fn fails_paying_invoice_more_than_once() {
1388 let mut accept_forward_cfg = test_default_channel_config();
1389 accept_forward_cfg.accept_forwards_to_priv_channels = true;
1391 let mut features = channelmanager::provided_init_features(&accept_forward_cfg);
1392 features.set_onion_messages_optional();
1393 features.set_route_blinding_optional();
1395 let chanmon_cfgs = create_chanmon_cfgs(6);
1396 let node_cfgs = create_node_cfgs(6, &chanmon_cfgs);
1398 *node_cfgs[1].override_init_features.borrow_mut() = Some(features);
1400 let node_chanmgrs = create_node_chanmgrs(
1401 6, &node_cfgs, &[None, Some(accept_forward_cfg), None, None, None, None]
1403 let nodes = create_network(6, &node_cfgs, &node_chanmgrs);
1405 create_unannounced_chan_between_nodes_with_value(&nodes, 0, 1, 10_000_000, 1_000_000_000);
1406 create_unannounced_chan_between_nodes_with_value(&nodes, 2, 3, 10_000_000, 1_000_000_000);
1407 create_announced_chan_between_nodes_with_value(&nodes, 1, 2, 10_000_000, 1_000_000_000);
1408 create_announced_chan_between_nodes_with_value(&nodes, 1, 4, 10_000_000, 1_000_000_000);
1409 create_announced_chan_between_nodes_with_value(&nodes, 1, 5, 10_000_000, 1_000_000_000);
1410 create_announced_chan_between_nodes_with_value(&nodes, 2, 4, 10_000_000, 1_000_000_000);
1411 create_announced_chan_between_nodes_with_value(&nodes, 2, 5, 10_000_000, 1_000_000_000);
1413 let (alice, bob, charlie, david) = (&nodes[0], &nodes[1], &nodes[2], &nodes[3]);
1414 let alice_id = alice.node.get_our_node_id();
1415 let bob_id = bob.node.get_our_node_id();
1416 let charlie_id = charlie.node.get_our_node_id();
1417 let david_id = david.node.get_our_node_id();
1419 disconnect_peers(alice, &[charlie, david, &nodes[4], &nodes[5]]);
1420 disconnect_peers(david, &[bob, &nodes[4], &nodes[5]]);
1422 let absolute_expiry = Duration::from_secs(u64::MAX);
1423 let payment_id = PaymentId([1; 32]);
1424 let refund = david.node
1425 .create_refund_builder(10_000_000, absolute_expiry, payment_id, Retry::Attempts(0), None)
1428 expect_recent_payment!(david, RecentPaymentDetails::AwaitingInvoice, payment_id);
1430 // Alice sends the first invoice
1431 alice.node.request_refund_payment(&refund).unwrap();
1433 connect_peers(alice, charlie);
1435 let onion_message = alice.onion_messenger.next_onion_message_for_peer(charlie_id).unwrap();
1436 charlie.onion_messenger.handle_onion_message(&alice_id, &onion_message);
1438 let onion_message = charlie.onion_messenger.next_onion_message_for_peer(david_id).unwrap();
1439 david.onion_messenger.handle_onion_message(&charlie_id, &onion_message);
1441 // David pays the first invoice
1442 let payment_context = PaymentContext::Bolt12Refund(Bolt12RefundContext {});
1443 let invoice1 = extract_invoice(david, &onion_message);
1445 route_bolt12_payment(david, &[charlie, bob, alice], &invoice1);
1446 expect_recent_payment!(david, RecentPaymentDetails::Pending, payment_id);
1448 claim_bolt12_payment(david, &[charlie, bob, alice], payment_context);
1449 expect_recent_payment!(david, RecentPaymentDetails::Fulfilled, payment_id);
1451 disconnect_peers(alice, &[charlie]);
1453 // Alice sends the second invoice
1454 alice.node.request_refund_payment(&refund).unwrap();
1456 connect_peers(alice, charlie);
1457 connect_peers(david, bob);
1459 let onion_message = alice.onion_messenger.next_onion_message_for_peer(charlie_id).unwrap();
1460 charlie.onion_messenger.handle_onion_message(&alice_id, &onion_message);
1462 let onion_message = charlie.onion_messenger.next_onion_message_for_peer(david_id).unwrap();
1463 david.onion_messenger.handle_onion_message(&charlie_id, &onion_message);
1465 let invoice2 = extract_invoice(david, &onion_message);
1466 assert_eq!(invoice1.payer_metadata(), invoice2.payer_metadata());
1468 // David sends an error instead of paying the second invoice
1469 let onion_message = david.onion_messenger.next_onion_message_for_peer(bob_id).unwrap();
1470 bob.onion_messenger.handle_onion_message(&david_id, &onion_message);
1472 let onion_message = bob.onion_messenger.next_onion_message_for_peer(alice_id).unwrap();
1473 alice.onion_messenger.handle_onion_message(&bob_id, &onion_message);
1475 let invoice_error = extract_invoice_error(alice, &onion_message);
1476 assert_eq!(invoice_error, InvoiceError::from_string("DuplicateInvoice".to_string()));