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::StaticInvoice(invoice) => panic!("Unexpected static invoice: {:?}", invoice),
196 OffersMessage::InvoiceError(error) => panic!("Unexpected invoice_error: {:?}", error),
198 ParsedOnionMessageContents::AsyncPayments(message) => panic!("Unexpected async payments message: {:?}", message),
199 ParsedOnionMessageContents::Custom(message) => panic!("Unexpected custom message: {:?}", message),
201 Ok(PeeledOnion::Forward(_, _)) => panic!("Unexpected onion message forward"),
202 Err(e) => panic!("Failed to process onion message {:?}", e),
206 fn extract_invoice<'a, 'b, 'c>(node: &Node<'a, 'b, 'c>, message: &OnionMessage) -> Bolt12Invoice {
207 match node.onion_messenger.peel_onion_message(message) {
208 Ok(PeeledOnion::Receive(message, _, _)) => match message {
209 ParsedOnionMessageContents::Offers(offers_message) => match offers_message {
210 OffersMessage::InvoiceRequest(invoice_request) => panic!("Unexpected invoice_request: {:?}", invoice_request),
211 OffersMessage::Invoice(invoice) => invoice,
212 OffersMessage::StaticInvoice(invoice) => panic!("Unexpected static invoice: {:?}", invoice),
213 OffersMessage::InvoiceError(error) => panic!("Unexpected invoice_error: {:?}", error),
215 ParsedOnionMessageContents::AsyncPayments(message) => panic!("Unexpected async payments message: {:?}", message),
216 ParsedOnionMessageContents::Custom(message) => panic!("Unexpected custom message: {:?}", message),
218 Ok(PeeledOnion::Forward(_, _)) => panic!("Unexpected onion message forward"),
219 Err(e) => panic!("Failed to process onion message {:?}", e),
223 fn extract_invoice_error<'a, 'b, 'c>(
224 node: &Node<'a, 'b, 'c>, message: &OnionMessage
226 match node.onion_messenger.peel_onion_message(message) {
227 Ok(PeeledOnion::Receive(message, _, _)) => match message {
228 ParsedOnionMessageContents::Offers(offers_message) => match offers_message {
229 OffersMessage::InvoiceRequest(invoice_request) => panic!("Unexpected invoice_request: {:?}", invoice_request),
230 OffersMessage::Invoice(invoice) => panic!("Unexpected invoice: {:?}", invoice),
231 OffersMessage::StaticInvoice(invoice) => panic!("Unexpected invoice: {:?}", invoice),
232 OffersMessage::InvoiceError(error) => error,
234 ParsedOnionMessageContents::AsyncPayments(message) => panic!("Unexpected async payments message: {:?}", message),
235 ParsedOnionMessageContents::Custom(message) => panic!("Unexpected custom message: {:?}", message),
237 Ok(PeeledOnion::Forward(_, _)) => panic!("Unexpected onion message forward"),
238 Err(e) => panic!("Failed to process onion message {:?}", e),
242 /// Checks that blinded paths without Tor-only nodes are preferred when constructing an offer.
244 fn prefers_non_tor_nodes_in_blinded_paths() {
245 let mut accept_forward_cfg = test_default_channel_config();
246 accept_forward_cfg.accept_forwards_to_priv_channels = true;
248 let mut features = channelmanager::provided_init_features(&accept_forward_cfg);
249 features.set_onion_messages_optional();
250 features.set_route_blinding_optional();
252 let chanmon_cfgs = create_chanmon_cfgs(6);
253 let node_cfgs = create_node_cfgs(6, &chanmon_cfgs);
255 *node_cfgs[1].override_init_features.borrow_mut() = Some(features);
257 let node_chanmgrs = create_node_chanmgrs(
258 6, &node_cfgs, &[None, Some(accept_forward_cfg), None, None, None, None]
260 let nodes = create_network(6, &node_cfgs, &node_chanmgrs);
262 create_unannounced_chan_between_nodes_with_value(&nodes, 0, 1, 10_000_000, 1_000_000_000);
263 create_unannounced_chan_between_nodes_with_value(&nodes, 2, 3, 10_000_000, 1_000_000_000);
264 create_announced_chan_between_nodes_with_value(&nodes, 1, 2, 10_000_000, 1_000_000_000);
265 create_announced_chan_between_nodes_with_value(&nodes, 1, 4, 10_000_000, 1_000_000_000);
266 create_announced_chan_between_nodes_with_value(&nodes, 1, 5, 10_000_000, 1_000_000_000);
267 create_announced_chan_between_nodes_with_value(&nodes, 2, 4, 10_000_000, 1_000_000_000);
268 create_announced_chan_between_nodes_with_value(&nodes, 2, 5, 10_000_000, 1_000_000_000);
270 // Add an extra channel so that more than one of Bob's peers have MIN_PEER_CHANNELS.
271 create_announced_chan_between_nodes_with_value(&nodes, 4, 5, 10_000_000, 1_000_000_000);
273 let (alice, bob, charlie, david) = (&nodes[0], &nodes[1], &nodes[2], &nodes[3]);
274 let bob_id = bob.node.get_our_node_id();
275 let charlie_id = charlie.node.get_our_node_id();
277 disconnect_peers(alice, &[charlie, david, &nodes[4], &nodes[5]]);
278 disconnect_peers(david, &[bob, &nodes[4], &nodes[5]]);
280 let tor = SocketAddress::OnionV2([255, 254, 253, 252, 251, 250, 249, 248, 247, 246, 38, 7]);
281 announce_node_address(charlie, &[alice, bob, david, &nodes[4], &nodes[5]], tor.clone());
284 .create_offer_builder(None).unwrap()
285 .amount_msats(10_000_000)
287 assert_ne!(offer.signing_pubkey(), Some(bob_id));
288 assert!(!offer.paths().is_empty());
289 for path in offer.paths() {
290 let introduction_node_id = resolve_introduction_node(david, &path);
291 assert_ne!(introduction_node_id, bob_id);
292 assert_ne!(introduction_node_id, charlie_id);
295 // Use a one-hop blinded path when Bob is announced and all his peers are Tor-only.
296 announce_node_address(&nodes[4], &[alice, bob, charlie, david, &nodes[5]], tor.clone());
297 announce_node_address(&nodes[5], &[alice, bob, charlie, david, &nodes[4]], tor.clone());
300 .create_offer_builder(None).unwrap()
301 .amount_msats(10_000_000)
303 assert_ne!(offer.signing_pubkey(), Some(bob_id));
304 assert!(!offer.paths().is_empty());
305 for path in offer.paths() {
306 let introduction_node_id = resolve_introduction_node(david, &path);
307 assert_eq!(introduction_node_id, bob_id);
311 /// Checks that blinded paths prefer an introduction node that is the most connected.
313 fn prefers_more_connected_nodes_in_blinded_paths() {
314 let mut accept_forward_cfg = test_default_channel_config();
315 accept_forward_cfg.accept_forwards_to_priv_channels = true;
317 let mut features = channelmanager::provided_init_features(&accept_forward_cfg);
318 features.set_onion_messages_optional();
319 features.set_route_blinding_optional();
321 let chanmon_cfgs = create_chanmon_cfgs(6);
322 let node_cfgs = create_node_cfgs(6, &chanmon_cfgs);
324 *node_cfgs[1].override_init_features.borrow_mut() = Some(features);
326 let node_chanmgrs = create_node_chanmgrs(
327 6, &node_cfgs, &[None, Some(accept_forward_cfg), None, None, None, None]
329 let nodes = create_network(6, &node_cfgs, &node_chanmgrs);
331 create_unannounced_chan_between_nodes_with_value(&nodes, 0, 1, 10_000_000, 1_000_000_000);
332 create_unannounced_chan_between_nodes_with_value(&nodes, 2, 3, 10_000_000, 1_000_000_000);
333 create_announced_chan_between_nodes_with_value(&nodes, 1, 2, 10_000_000, 1_000_000_000);
334 create_announced_chan_between_nodes_with_value(&nodes, 1, 4, 10_000_000, 1_000_000_000);
335 create_announced_chan_between_nodes_with_value(&nodes, 1, 5, 10_000_000, 1_000_000_000);
336 create_announced_chan_between_nodes_with_value(&nodes, 2, 4, 10_000_000, 1_000_000_000);
337 create_announced_chan_between_nodes_with_value(&nodes, 2, 5, 10_000_000, 1_000_000_000);
339 // Add extra channels so that more than one of Bob's peers have MIN_PEER_CHANNELS and one has
340 // more than the others.
341 create_announced_chan_between_nodes_with_value(&nodes, 0, 4, 10_000_000, 1_000_000_000);
342 create_announced_chan_between_nodes_with_value(&nodes, 3, 4, 10_000_000, 1_000_000_000);
344 let (alice, bob, charlie, david) = (&nodes[0], &nodes[1], &nodes[2], &nodes[3]);
345 let bob_id = bob.node.get_our_node_id();
347 disconnect_peers(alice, &[charlie, david, &nodes[4], &nodes[5]]);
348 disconnect_peers(david, &[bob, &nodes[4], &nodes[5]]);
351 .create_offer_builder(None).unwrap()
352 .amount_msats(10_000_000)
354 assert_ne!(offer.signing_pubkey(), Some(bob_id));
355 assert!(!offer.paths().is_empty());
356 for path in offer.paths() {
357 let introduction_node_id = resolve_introduction_node(david, &path);
358 assert_eq!(introduction_node_id, nodes[4].node.get_our_node_id());
362 /// Checks that blinded paths are compact for short-lived offers.
364 fn creates_short_lived_offer() {
365 let chanmon_cfgs = create_chanmon_cfgs(2);
366 let node_cfgs = create_node_cfgs(2, &chanmon_cfgs);
367 let node_chanmgrs = create_node_chanmgrs(2, &node_cfgs, &[None, None]);
368 let nodes = create_network(2, &node_cfgs, &node_chanmgrs);
370 create_announced_chan_between_nodes_with_value(&nodes, 0, 1, 10_000_000, 1_000_000_000);
372 let alice = &nodes[0];
373 let alice_id = alice.node.get_our_node_id();
376 let absolute_expiry = alice.node.duration_since_epoch() + MAX_SHORT_LIVED_RELATIVE_EXPIRY;
377 let offer = alice.node
378 .create_offer_builder(Some(absolute_expiry)).unwrap()
380 assert_eq!(offer.absolute_expiry(), Some(absolute_expiry));
381 assert!(!offer.paths().is_empty());
382 for path in offer.paths() {
383 let introduction_node_id = resolve_introduction_node(bob, &path);
384 assert_eq!(introduction_node_id, alice_id);
385 assert!(matches!(path.introduction_node, IntroductionNode::DirectedShortChannelId(..)));
389 /// Checks that blinded paths are not compact for long-lived offers.
391 fn creates_long_lived_offer() {
392 let chanmon_cfgs = create_chanmon_cfgs(2);
393 let node_cfgs = create_node_cfgs(2, &chanmon_cfgs);
394 let node_chanmgrs = create_node_chanmgrs(2, &node_cfgs, &[None, None]);
395 let nodes = create_network(2, &node_cfgs, &node_chanmgrs);
397 create_announced_chan_between_nodes_with_value(&nodes, 0, 1, 10_000_000, 1_000_000_000);
399 let alice = &nodes[0];
400 let alice_id = alice.node.get_our_node_id();
402 let absolute_expiry = alice.node.duration_since_epoch() + MAX_SHORT_LIVED_RELATIVE_EXPIRY
403 + Duration::from_secs(1);
404 let offer = alice.node
405 .create_offer_builder(Some(absolute_expiry))
408 assert_eq!(offer.absolute_expiry(), Some(absolute_expiry));
409 assert!(!offer.paths().is_empty());
410 for path in offer.paths() {
411 assert_eq!(path.introduction_node, IntroductionNode::NodeId(alice_id));
414 let offer = alice.node
415 .create_offer_builder(None).unwrap()
417 assert_eq!(offer.absolute_expiry(), None);
418 assert!(!offer.paths().is_empty());
419 for path in offer.paths() {
420 assert_eq!(path.introduction_node, IntroductionNode::NodeId(alice_id));
424 /// Checks that blinded paths are compact for short-lived refunds.
426 fn creates_short_lived_refund() {
427 let chanmon_cfgs = create_chanmon_cfgs(2);
428 let node_cfgs = create_node_cfgs(2, &chanmon_cfgs);
429 let node_chanmgrs = create_node_chanmgrs(2, &node_cfgs, &[None, None]);
430 let nodes = create_network(2, &node_cfgs, &node_chanmgrs);
432 create_announced_chan_between_nodes_with_value(&nodes, 0, 1, 10_000_000, 1_000_000_000);
434 let alice = &nodes[0];
436 let bob_id = bob.node.get_our_node_id();
438 let absolute_expiry = bob.node.duration_since_epoch() + MAX_SHORT_LIVED_RELATIVE_EXPIRY;
439 let payment_id = PaymentId([1; 32]);
440 let refund = bob.node
441 .create_refund_builder(10_000_000, absolute_expiry, payment_id, Retry::Attempts(0), None)
444 assert_eq!(refund.absolute_expiry(), Some(absolute_expiry));
445 assert!(!refund.paths().is_empty());
446 for path in refund.paths() {
447 let introduction_node_id = resolve_introduction_node(alice, &path);
448 assert_eq!(introduction_node_id, bob_id);
449 assert!(matches!(path.introduction_node, IntroductionNode::DirectedShortChannelId(..)));
453 /// Checks that blinded paths are not compact for long-lived refunds.
455 fn creates_long_lived_refund() {
456 let chanmon_cfgs = create_chanmon_cfgs(2);
457 let node_cfgs = create_node_cfgs(2, &chanmon_cfgs);
458 let node_chanmgrs = create_node_chanmgrs(2, &node_cfgs, &[None, None]);
459 let nodes = create_network(2, &node_cfgs, &node_chanmgrs);
461 create_announced_chan_between_nodes_with_value(&nodes, 0, 1, 10_000_000, 1_000_000_000);
464 let bob_id = bob.node.get_our_node_id();
466 let absolute_expiry = bob.node.duration_since_epoch() + MAX_SHORT_LIVED_RELATIVE_EXPIRY
467 + Duration::from_secs(1);
468 let payment_id = PaymentId([1; 32]);
469 let refund = bob.node
470 .create_refund_builder(10_000_000, absolute_expiry, payment_id, Retry::Attempts(0), None)
473 assert_eq!(refund.absolute_expiry(), Some(absolute_expiry));
474 assert!(!refund.paths().is_empty());
475 for path in refund.paths() {
476 assert_eq!(path.introduction_node, IntroductionNode::NodeId(bob_id));
480 /// Checks that an offer can be paid through blinded paths and that ephemeral pubkeys are used
481 /// rather than exposing a node's pubkey.
483 fn creates_and_pays_for_offer_using_two_hop_blinded_path() {
484 let mut accept_forward_cfg = test_default_channel_config();
485 accept_forward_cfg.accept_forwards_to_priv_channels = true;
487 let mut features = channelmanager::provided_init_features(&accept_forward_cfg);
488 features.set_onion_messages_optional();
489 features.set_route_blinding_optional();
491 let chanmon_cfgs = create_chanmon_cfgs(6);
492 let node_cfgs = create_node_cfgs(6, &chanmon_cfgs);
494 *node_cfgs[1].override_init_features.borrow_mut() = Some(features);
496 let node_chanmgrs = create_node_chanmgrs(
497 6, &node_cfgs, &[None, Some(accept_forward_cfg), None, None, None, None]
499 let nodes = create_network(6, &node_cfgs, &node_chanmgrs);
501 create_unannounced_chan_between_nodes_with_value(&nodes, 0, 1, 10_000_000, 1_000_000_000);
502 create_unannounced_chan_between_nodes_with_value(&nodes, 2, 3, 10_000_000, 1_000_000_000);
503 create_announced_chan_between_nodes_with_value(&nodes, 1, 2, 10_000_000, 1_000_000_000);
504 create_announced_chan_between_nodes_with_value(&nodes, 1, 4, 10_000_000, 1_000_000_000);
505 create_announced_chan_between_nodes_with_value(&nodes, 1, 5, 10_000_000, 1_000_000_000);
506 create_announced_chan_between_nodes_with_value(&nodes, 2, 4, 10_000_000, 1_000_000_000);
507 create_announced_chan_between_nodes_with_value(&nodes, 2, 5, 10_000_000, 1_000_000_000);
509 let (alice, bob, charlie, david) = (&nodes[0], &nodes[1], &nodes[2], &nodes[3]);
510 let alice_id = alice.node.get_our_node_id();
511 let bob_id = bob.node.get_our_node_id();
512 let charlie_id = charlie.node.get_our_node_id();
513 let david_id = david.node.get_our_node_id();
515 disconnect_peers(alice, &[charlie, david, &nodes[4], &nodes[5]]);
516 disconnect_peers(david, &[bob, &nodes[4], &nodes[5]]);
518 let offer = alice.node
519 .create_offer_builder(None)
521 .amount_msats(10_000_000)
523 assert_ne!(offer.signing_pubkey(), Some(alice_id));
524 assert!(!offer.paths().is_empty());
525 for path in offer.paths() {
526 assert_eq!(path.introduction_node, IntroductionNode::NodeId(bob_id));
529 let payment_id = PaymentId([1; 32]);
530 david.node.pay_for_offer(&offer, None, None, None, payment_id, Retry::Attempts(0), None)
532 expect_recent_payment!(david, RecentPaymentDetails::AwaitingInvoice, payment_id);
534 connect_peers(david, bob);
536 let onion_message = david.onion_messenger.next_onion_message_for_peer(bob_id).unwrap();
537 bob.onion_messenger.handle_onion_message(&david_id, &onion_message);
539 connect_peers(alice, charlie);
541 let onion_message = bob.onion_messenger.next_onion_message_for_peer(alice_id).unwrap();
542 alice.onion_messenger.handle_onion_message(&bob_id, &onion_message);
544 let (invoice_request, reply_path) = extract_invoice_request(alice, &onion_message);
545 let payment_context = PaymentContext::Bolt12Offer(Bolt12OfferContext {
546 offer_id: offer.id(),
547 invoice_request: InvoiceRequestFields {
548 payer_id: invoice_request.payer_id(),
550 payer_note_truncated: None,
553 assert_eq!(invoice_request.amount_msats(), None);
554 assert_ne!(invoice_request.payer_id(), david_id);
555 assert_eq!(reply_path.introduction_node, IntroductionNode::NodeId(charlie_id));
557 let onion_message = alice.onion_messenger.next_onion_message_for_peer(charlie_id).unwrap();
558 charlie.onion_messenger.handle_onion_message(&alice_id, &onion_message);
560 let onion_message = charlie.onion_messenger.next_onion_message_for_peer(david_id).unwrap();
561 david.onion_messenger.handle_onion_message(&charlie_id, &onion_message);
563 let invoice = extract_invoice(david, &onion_message);
564 assert_eq!(invoice.amount_msats(), 10_000_000);
565 assert_ne!(invoice.signing_pubkey(), alice_id);
566 assert!(!invoice.payment_paths().is_empty());
567 for (_, path) in invoice.payment_paths() {
568 assert_eq!(path.introduction_node, IntroductionNode::NodeId(bob_id));
571 route_bolt12_payment(david, &[charlie, bob, alice], &invoice);
572 expect_recent_payment!(david, RecentPaymentDetails::Pending, payment_id);
574 claim_bolt12_payment(david, &[charlie, bob, alice], payment_context);
575 expect_recent_payment!(david, RecentPaymentDetails::Fulfilled, payment_id);
578 /// Checks that a refund can be paid through blinded paths and that ephemeral pubkeys are used
579 /// rather than exposing a node's pubkey.
581 fn creates_and_pays_for_refund_using_two_hop_blinded_path() {
582 let mut accept_forward_cfg = test_default_channel_config();
583 accept_forward_cfg.accept_forwards_to_priv_channels = true;
585 let mut features = channelmanager::provided_init_features(&accept_forward_cfg);
586 features.set_onion_messages_optional();
587 features.set_route_blinding_optional();
589 let chanmon_cfgs = create_chanmon_cfgs(6);
590 let node_cfgs = create_node_cfgs(6, &chanmon_cfgs);
592 *node_cfgs[1].override_init_features.borrow_mut() = Some(features);
594 let node_chanmgrs = create_node_chanmgrs(
595 6, &node_cfgs, &[None, Some(accept_forward_cfg), None, None, None, None]
597 let nodes = create_network(6, &node_cfgs, &node_chanmgrs);
599 create_unannounced_chan_between_nodes_with_value(&nodes, 0, 1, 10_000_000, 1_000_000_000);
600 create_unannounced_chan_between_nodes_with_value(&nodes, 2, 3, 10_000_000, 1_000_000_000);
601 create_announced_chan_between_nodes_with_value(&nodes, 1, 2, 10_000_000, 1_000_000_000);
602 create_announced_chan_between_nodes_with_value(&nodes, 1, 4, 10_000_000, 1_000_000_000);
603 create_announced_chan_between_nodes_with_value(&nodes, 1, 5, 10_000_000, 1_000_000_000);
604 create_announced_chan_between_nodes_with_value(&nodes, 2, 4, 10_000_000, 1_000_000_000);
605 create_announced_chan_between_nodes_with_value(&nodes, 2, 5, 10_000_000, 1_000_000_000);
607 let (alice, bob, charlie, david) = (&nodes[0], &nodes[1], &nodes[2], &nodes[3]);
608 let alice_id = alice.node.get_our_node_id();
609 let bob_id = bob.node.get_our_node_id();
610 let charlie_id = charlie.node.get_our_node_id();
611 let david_id = david.node.get_our_node_id();
613 disconnect_peers(alice, &[charlie, david, &nodes[4], &nodes[5]]);
614 disconnect_peers(david, &[bob, &nodes[4], &nodes[5]]);
616 let absolute_expiry = Duration::from_secs(u64::MAX);
617 let payment_id = PaymentId([1; 32]);
618 let refund = david.node
619 .create_refund_builder(10_000_000, absolute_expiry, payment_id, Retry::Attempts(0), None)
622 assert_eq!(refund.amount_msats(), 10_000_000);
623 assert_eq!(refund.absolute_expiry(), Some(absolute_expiry));
624 assert_ne!(refund.payer_id(), david_id);
625 assert!(!refund.paths().is_empty());
626 for path in refund.paths() {
627 assert_eq!(path.introduction_node, IntroductionNode::NodeId(charlie_id));
629 expect_recent_payment!(david, RecentPaymentDetails::AwaitingInvoice, payment_id);
631 let payment_context = PaymentContext::Bolt12Refund(Bolt12RefundContext {});
632 let expected_invoice = alice.node.request_refund_payment(&refund).unwrap();
634 connect_peers(alice, charlie);
636 let onion_message = alice.onion_messenger.next_onion_message_for_peer(charlie_id).unwrap();
637 charlie.onion_messenger.handle_onion_message(&alice_id, &onion_message);
639 let onion_message = charlie.onion_messenger.next_onion_message_for_peer(david_id).unwrap();
640 david.onion_messenger.handle_onion_message(&charlie_id, &onion_message);
642 let invoice = extract_invoice(david, &onion_message);
643 assert_eq!(invoice, expected_invoice);
645 assert_eq!(invoice.amount_msats(), 10_000_000);
646 assert_ne!(invoice.signing_pubkey(), alice_id);
647 assert!(!invoice.payment_paths().is_empty());
648 for (_, path) in invoice.payment_paths() {
649 assert_eq!(path.introduction_node, IntroductionNode::NodeId(bob_id));
652 route_bolt12_payment(david, &[charlie, bob, alice], &invoice);
653 expect_recent_payment!(david, RecentPaymentDetails::Pending, payment_id);
655 claim_bolt12_payment(david, &[charlie, bob, alice], payment_context);
656 expect_recent_payment!(david, RecentPaymentDetails::Fulfilled, payment_id);
659 /// Checks that an offer can be paid through a one-hop blinded path and that ephemeral pubkeys are
660 /// used rather than exposing a node's pubkey. However, the node's pubkey is still used as the
661 /// introduction node of the blinded path.
663 fn creates_and_pays_for_offer_using_one_hop_blinded_path() {
664 let chanmon_cfgs = create_chanmon_cfgs(2);
665 let node_cfgs = create_node_cfgs(2, &chanmon_cfgs);
666 let node_chanmgrs = create_node_chanmgrs(2, &node_cfgs, &[None, None]);
667 let nodes = create_network(2, &node_cfgs, &node_chanmgrs);
669 create_announced_chan_between_nodes_with_value(&nodes, 0, 1, 10_000_000, 1_000_000_000);
671 let alice = &nodes[0];
672 let alice_id = alice.node.get_our_node_id();
674 let bob_id = bob.node.get_our_node_id();
676 let offer = alice.node
677 .create_offer_builder(None).unwrap()
678 .amount_msats(10_000_000)
680 assert_ne!(offer.signing_pubkey(), Some(alice_id));
681 assert!(!offer.paths().is_empty());
682 for path in offer.paths() {
683 assert_eq!(path.introduction_node, IntroductionNode::NodeId(alice_id));
686 let payment_id = PaymentId([1; 32]);
687 bob.node.pay_for_offer(&offer, None, None, None, payment_id, Retry::Attempts(0), None).unwrap();
688 expect_recent_payment!(bob, RecentPaymentDetails::AwaitingInvoice, payment_id);
690 let onion_message = bob.onion_messenger.next_onion_message_for_peer(alice_id).unwrap();
691 alice.onion_messenger.handle_onion_message(&bob_id, &onion_message);
693 let (invoice_request, reply_path) = extract_invoice_request(alice, &onion_message);
694 let payment_context = PaymentContext::Bolt12Offer(Bolt12OfferContext {
695 offer_id: offer.id(),
696 invoice_request: InvoiceRequestFields {
697 payer_id: invoice_request.payer_id(),
699 payer_note_truncated: None,
702 assert_eq!(invoice_request.amount_msats(), None);
703 assert_ne!(invoice_request.payer_id(), bob_id);
704 assert_eq!(reply_path.introduction_node, IntroductionNode::NodeId(bob_id));
706 let onion_message = alice.onion_messenger.next_onion_message_for_peer(bob_id).unwrap();
707 bob.onion_messenger.handle_onion_message(&alice_id, &onion_message);
709 let invoice = extract_invoice(bob, &onion_message);
710 assert_eq!(invoice.amount_msats(), 10_000_000);
711 assert_ne!(invoice.signing_pubkey(), alice_id);
712 assert!(!invoice.payment_paths().is_empty());
713 for (_, path) in invoice.payment_paths() {
714 assert_eq!(path.introduction_node, IntroductionNode::NodeId(alice_id));
717 route_bolt12_payment(bob, &[alice], &invoice);
718 expect_recent_payment!(bob, RecentPaymentDetails::Pending, payment_id);
720 claim_bolt12_payment(bob, &[alice], payment_context);
721 expect_recent_payment!(bob, RecentPaymentDetails::Fulfilled, payment_id);
724 /// Checks that a refund can be paid through a one-hop blinded path and that ephemeral pubkeys are
725 /// used rather than exposing a node's pubkey. However, the node's pubkey is still used as the
726 /// introduction node of the blinded path.
728 fn creates_and_pays_for_refund_using_one_hop_blinded_path() {
729 let chanmon_cfgs = create_chanmon_cfgs(2);
730 let node_cfgs = create_node_cfgs(2, &chanmon_cfgs);
731 let node_chanmgrs = create_node_chanmgrs(2, &node_cfgs, &[None, None]);
732 let nodes = create_network(2, &node_cfgs, &node_chanmgrs);
734 create_announced_chan_between_nodes_with_value(&nodes, 0, 1, 10_000_000, 1_000_000_000);
736 let alice = &nodes[0];
737 let alice_id = alice.node.get_our_node_id();
739 let bob_id = bob.node.get_our_node_id();
741 let absolute_expiry = Duration::from_secs(u64::MAX);
742 let payment_id = PaymentId([1; 32]);
743 let refund = bob.node
744 .create_refund_builder(10_000_000, absolute_expiry, payment_id, Retry::Attempts(0), None)
747 assert_eq!(refund.amount_msats(), 10_000_000);
748 assert_eq!(refund.absolute_expiry(), Some(absolute_expiry));
749 assert_ne!(refund.payer_id(), bob_id);
750 assert!(!refund.paths().is_empty());
751 for path in refund.paths() {
752 assert_eq!(path.introduction_node, IntroductionNode::NodeId(bob_id));
754 expect_recent_payment!(bob, RecentPaymentDetails::AwaitingInvoice, payment_id);
756 let payment_context = PaymentContext::Bolt12Refund(Bolt12RefundContext {});
757 let expected_invoice = alice.node.request_refund_payment(&refund).unwrap();
759 let onion_message = alice.onion_messenger.next_onion_message_for_peer(bob_id).unwrap();
760 bob.onion_messenger.handle_onion_message(&alice_id, &onion_message);
762 let invoice = extract_invoice(bob, &onion_message);
763 assert_eq!(invoice, expected_invoice);
765 assert_eq!(invoice.amount_msats(), 10_000_000);
766 assert_ne!(invoice.signing_pubkey(), alice_id);
767 assert!(!invoice.payment_paths().is_empty());
768 for (_, path) in invoice.payment_paths() {
769 assert_eq!(path.introduction_node, IntroductionNode::NodeId(alice_id));
772 route_bolt12_payment(bob, &[alice], &invoice);
773 expect_recent_payment!(bob, RecentPaymentDetails::Pending, payment_id);
775 claim_bolt12_payment(bob, &[alice], payment_context);
776 expect_recent_payment!(bob, RecentPaymentDetails::Fulfilled, payment_id);
779 /// Checks that an invoice for an offer without any blinded paths can be requested. Note that while
780 /// the requested is sent directly using the node's pubkey, the response and the payment still use
781 /// blinded paths as required by the spec.
783 fn pays_for_offer_without_blinded_paths() {
784 let chanmon_cfgs = create_chanmon_cfgs(2);
785 let node_cfgs = create_node_cfgs(2, &chanmon_cfgs);
786 let node_chanmgrs = create_node_chanmgrs(2, &node_cfgs, &[None, None]);
787 let nodes = create_network(2, &node_cfgs, &node_chanmgrs);
789 create_announced_chan_between_nodes_with_value(&nodes, 0, 1, 10_000_000, 1_000_000_000);
791 let alice = &nodes[0];
792 let alice_id = alice.node.get_our_node_id();
794 let bob_id = bob.node.get_our_node_id();
796 let offer = alice.node
797 .create_offer_builder(None).unwrap()
799 .amount_msats(10_000_000)
801 assert_eq!(offer.signing_pubkey(), Some(alice_id));
802 assert!(offer.paths().is_empty());
804 let payment_id = PaymentId([1; 32]);
805 bob.node.pay_for_offer(&offer, None, None, None, payment_id, Retry::Attempts(0), None).unwrap();
806 expect_recent_payment!(bob, RecentPaymentDetails::AwaitingInvoice, payment_id);
808 let onion_message = bob.onion_messenger.next_onion_message_for_peer(alice_id).unwrap();
809 alice.onion_messenger.handle_onion_message(&bob_id, &onion_message);
811 let (invoice_request, _) = extract_invoice_request(alice, &onion_message);
812 let payment_context = PaymentContext::Bolt12Offer(Bolt12OfferContext {
813 offer_id: offer.id(),
814 invoice_request: InvoiceRequestFields {
815 payer_id: invoice_request.payer_id(),
817 payer_note_truncated: None,
821 let onion_message = alice.onion_messenger.next_onion_message_for_peer(bob_id).unwrap();
822 bob.onion_messenger.handle_onion_message(&alice_id, &onion_message);
824 let invoice = extract_invoice(bob, &onion_message);
825 route_bolt12_payment(bob, &[alice], &invoice);
826 expect_recent_payment!(bob, RecentPaymentDetails::Pending, payment_id);
828 claim_bolt12_payment(bob, &[alice], payment_context);
829 expect_recent_payment!(bob, RecentPaymentDetails::Fulfilled, payment_id);
832 /// Checks that a refund without any blinded paths can be paid. Note that while the invoice is sent
833 /// directly using the node's pubkey, the payment still use blinded paths as required by the spec.
835 fn pays_for_refund_without_blinded_paths() {
836 let chanmon_cfgs = create_chanmon_cfgs(2);
837 let node_cfgs = create_node_cfgs(2, &chanmon_cfgs);
838 let node_chanmgrs = create_node_chanmgrs(2, &node_cfgs, &[None, None]);
839 let nodes = create_network(2, &node_cfgs, &node_chanmgrs);
841 create_announced_chan_between_nodes_with_value(&nodes, 0, 1, 10_000_000, 1_000_000_000);
843 let alice = &nodes[0];
844 let alice_id = alice.node.get_our_node_id();
846 let bob_id = bob.node.get_our_node_id();
848 let absolute_expiry = Duration::from_secs(u64::MAX);
849 let payment_id = PaymentId([1; 32]);
850 let refund = bob.node
851 .create_refund_builder(10_000_000, absolute_expiry, payment_id, Retry::Attempts(0), None)
855 assert_eq!(refund.payer_id(), bob_id);
856 assert!(refund.paths().is_empty());
857 expect_recent_payment!(bob, RecentPaymentDetails::AwaitingInvoice, payment_id);
859 let payment_context = PaymentContext::Bolt12Refund(Bolt12RefundContext {});
860 let expected_invoice = alice.node.request_refund_payment(&refund).unwrap();
862 let onion_message = alice.onion_messenger.next_onion_message_for_peer(bob_id).unwrap();
863 bob.onion_messenger.handle_onion_message(&alice_id, &onion_message);
865 let invoice = extract_invoice(bob, &onion_message);
866 assert_eq!(invoice, expected_invoice);
868 route_bolt12_payment(bob, &[alice], &invoice);
869 expect_recent_payment!(bob, RecentPaymentDetails::Pending, payment_id);
871 claim_bolt12_payment(bob, &[alice], payment_context);
872 expect_recent_payment!(bob, RecentPaymentDetails::Fulfilled, payment_id);
875 /// Checks that a deferred invoice can be paid asynchronously from an Event::InvoiceReceived.
877 fn pays_bolt12_invoice_asynchronously() {
878 let mut manually_pay_cfg = test_default_channel_config();
879 manually_pay_cfg.manually_handle_bolt12_invoices = true;
881 let chanmon_cfgs = create_chanmon_cfgs(2);
882 let node_cfgs = create_node_cfgs(2, &chanmon_cfgs);
883 let node_chanmgrs = create_node_chanmgrs(2, &node_cfgs, &[None, Some(manually_pay_cfg)]);
884 let nodes = create_network(2, &node_cfgs, &node_chanmgrs);
886 create_announced_chan_between_nodes_with_value(&nodes, 0, 1, 10_000_000, 1_000_000_000);
888 let alice = &nodes[0];
889 let alice_id = alice.node.get_our_node_id();
891 let bob_id = bob.node.get_our_node_id();
893 let offer = alice.node
894 .create_offer_builder(None).unwrap()
895 .amount_msats(10_000_000)
898 let payment_id = PaymentId([1; 32]);
899 bob.node.pay_for_offer(&offer, None, None, None, payment_id, Retry::Attempts(0), None).unwrap();
900 expect_recent_payment!(bob, RecentPaymentDetails::AwaitingInvoice, payment_id);
902 let onion_message = bob.onion_messenger.next_onion_message_for_peer(alice_id).unwrap();
903 alice.onion_messenger.handle_onion_message(&bob_id, &onion_message);
905 let (invoice_request, _) = extract_invoice_request(alice, &onion_message);
906 let payment_context = PaymentContext::Bolt12Offer(Bolt12OfferContext {
907 offer_id: offer.id(),
908 invoice_request: InvoiceRequestFields {
909 payer_id: invoice_request.payer_id(),
911 payer_note_truncated: None,
915 let onion_message = alice.onion_messenger.next_onion_message_for_peer(bob_id).unwrap();
916 bob.onion_messenger.handle_onion_message(&alice_id, &onion_message);
918 let invoice = match get_event!(bob, Event::InvoiceReceived) {
919 Event::InvoiceReceived { payment_id: actual_payment_id, invoice, .. } => {
920 assert_eq!(actual_payment_id, payment_id);
923 _ => panic!("No Event::InvoiceReceived"),
925 assert_eq!(invoice.amount_msats(), 10_000_000);
926 assert_ne!(invoice.signing_pubkey(), alice_id);
927 assert!(!invoice.payment_paths().is_empty());
928 for (_, path) in invoice.payment_paths() {
929 assert_eq!(path.introduction_node, IntroductionNode::NodeId(alice_id));
932 assert!(bob.node.send_payment_for_bolt12_invoice(&invoice).is_ok());
934 bob.node.send_payment_for_bolt12_invoice(&invoice),
935 Err(Bolt12PaymentError::DuplicateInvoice),
938 route_bolt12_payment(bob, &[alice], &invoice);
939 expect_recent_payment!(bob, RecentPaymentDetails::Pending, payment_id);
941 claim_bolt12_payment(bob, &[alice], payment_context);
942 expect_recent_payment!(bob, RecentPaymentDetails::Fulfilled, payment_id);
945 bob.node.send_payment_for_bolt12_invoice(&invoice),
946 Err(Bolt12PaymentError::DuplicateInvoice),
949 for _ in 0..=IDEMPOTENCY_TIMEOUT_TICKS {
950 bob.node.timer_tick_occurred();
954 bob.node.send_payment_for_bolt12_invoice(&invoice),
955 Err(Bolt12PaymentError::UnexpectedInvoice),
959 /// Fails creating an offer when a blinded path cannot be created without exposing the node's id.
961 fn fails_creating_offer_without_blinded_paths() {
962 let chanmon_cfgs = create_chanmon_cfgs(2);
963 let node_cfgs = create_node_cfgs(2, &chanmon_cfgs);
964 let node_chanmgrs = create_node_chanmgrs(2, &node_cfgs, &[None, None]);
965 let nodes = create_network(2, &node_cfgs, &node_chanmgrs);
967 create_unannounced_chan_between_nodes_with_value(&nodes, 0, 1, 10_000_000, 1_000_000_000);
969 match nodes[0].node.create_offer_builder(None) {
970 Ok(_) => panic!("Expected error"),
971 Err(e) => assert_eq!(e, Bolt12SemanticError::MissingPaths),
975 /// Fails creating a refund when a blinded path cannot be created without exposing the node's id.
977 fn fails_creating_refund_without_blinded_paths() {
978 let chanmon_cfgs = create_chanmon_cfgs(2);
979 let node_cfgs = create_node_cfgs(2, &chanmon_cfgs);
980 let node_chanmgrs = create_node_chanmgrs(2, &node_cfgs, &[None, None]);
981 let nodes = create_network(2, &node_cfgs, &node_chanmgrs);
983 create_unannounced_chan_between_nodes_with_value(&nodes, 0, 1, 10_000_000, 1_000_000_000);
985 let absolute_expiry = Duration::from_secs(u64::MAX);
986 let payment_id = PaymentId([1; 32]);
988 match nodes[0].node.create_refund_builder(
989 10_000, absolute_expiry, payment_id, Retry::Attempts(0), None
991 Ok(_) => panic!("Expected error"),
992 Err(e) => assert_eq!(e, Bolt12SemanticError::MissingPaths),
995 assert!(nodes[0].node.list_recent_payments().is_empty());
998 /// Fails creating or paying an offer when a blinded path cannot be created because no peers are
1001 fn fails_creating_or_paying_for_offer_without_connected_peers() {
1002 let chanmon_cfgs = create_chanmon_cfgs(6);
1003 let node_cfgs = create_node_cfgs(6, &chanmon_cfgs);
1004 let node_chanmgrs = create_node_chanmgrs(6, &node_cfgs, &[None, None, None, None, None, None]);
1005 let nodes = create_network(6, &node_cfgs, &node_chanmgrs);
1007 create_unannounced_chan_between_nodes_with_value(&nodes, 0, 1, 10_000_000, 1_000_000_000);
1008 create_unannounced_chan_between_nodes_with_value(&nodes, 2, 3, 10_000_000, 1_000_000_000);
1009 create_announced_chan_between_nodes_with_value(&nodes, 1, 2, 10_000_000, 1_000_000_000);
1010 create_announced_chan_between_nodes_with_value(&nodes, 1, 4, 10_000_000, 1_000_000_000);
1011 create_announced_chan_between_nodes_with_value(&nodes, 1, 5, 10_000_000, 1_000_000_000);
1012 create_announced_chan_between_nodes_with_value(&nodes, 2, 4, 10_000_000, 1_000_000_000);
1013 create_announced_chan_between_nodes_with_value(&nodes, 2, 5, 10_000_000, 1_000_000_000);
1015 let (alice, bob, charlie, david) = (&nodes[0], &nodes[1], &nodes[2], &nodes[3]);
1017 disconnect_peers(alice, &[bob, charlie, david, &nodes[4], &nodes[5]]);
1018 disconnect_peers(david, &[bob, charlie, &nodes[4], &nodes[5]]);
1020 let absolute_expiry = alice.node.duration_since_epoch() + MAX_SHORT_LIVED_RELATIVE_EXPIRY;
1021 match alice.node.create_offer_builder(Some(absolute_expiry)) {
1022 Ok(_) => panic!("Expected error"),
1023 Err(e) => assert_eq!(e, Bolt12SemanticError::MissingPaths),
1026 let mut args = ReconnectArgs::new(alice, bob);
1027 args.send_channel_ready = (true, true);
1028 reconnect_nodes(args);
1030 let offer = alice.node
1031 .create_offer_builder(Some(absolute_expiry)).unwrap()
1032 .amount_msats(10_000_000)
1035 let payment_id = PaymentId([1; 32]);
1037 match david.node.pay_for_offer(&offer, None, None, None, payment_id, Retry::Attempts(0), None) {
1038 Ok(_) => panic!("Expected error"),
1039 Err(e) => assert_eq!(e, Bolt12SemanticError::MissingPaths),
1042 assert!(nodes[0].node.list_recent_payments().is_empty());
1044 let mut args = ReconnectArgs::new(charlie, david);
1045 args.send_channel_ready = (true, true);
1046 reconnect_nodes(args);
1049 david.node.pay_for_offer(
1050 &offer, None, None, None, payment_id, Retry::Attempts(0), None
1054 expect_recent_payment!(david, RecentPaymentDetails::AwaitingInvoice, payment_id);
1057 /// Fails creating or sending an invoice for a refund when a blinded path cannot be created because
1058 /// no peers are connected.
1060 fn fails_creating_refund_or_sending_invoice_without_connected_peers() {
1061 let mut accept_forward_cfg = test_default_channel_config();
1062 accept_forward_cfg.accept_forwards_to_priv_channels = true;
1064 let mut features = channelmanager::provided_init_features(&accept_forward_cfg);
1065 features.set_onion_messages_optional();
1066 features.set_route_blinding_optional();
1068 let chanmon_cfgs = create_chanmon_cfgs(6);
1069 let node_cfgs = create_node_cfgs(6, &chanmon_cfgs);
1071 *node_cfgs[1].override_init_features.borrow_mut() = Some(features);
1073 let node_chanmgrs = create_node_chanmgrs(
1074 6, &node_cfgs, &[None, Some(accept_forward_cfg), None, None, None, None]
1076 let nodes = create_network(6, &node_cfgs, &node_chanmgrs);
1078 create_unannounced_chan_between_nodes_with_value(&nodes, 0, 1, 10_000_000, 1_000_000_000);
1079 create_unannounced_chan_between_nodes_with_value(&nodes, 2, 3, 10_000_000, 1_000_000_000);
1080 create_announced_chan_between_nodes_with_value(&nodes, 1, 2, 10_000_000, 1_000_000_000);
1081 create_announced_chan_between_nodes_with_value(&nodes, 1, 4, 10_000_000, 1_000_000_000);
1082 create_announced_chan_between_nodes_with_value(&nodes, 1, 5, 10_000_000, 1_000_000_000);
1083 create_announced_chan_between_nodes_with_value(&nodes, 2, 4, 10_000_000, 1_000_000_000);
1084 create_announced_chan_between_nodes_with_value(&nodes, 2, 5, 10_000_000, 1_000_000_000);
1086 let (alice, bob, charlie, david) = (&nodes[0], &nodes[1], &nodes[2], &nodes[3]);
1088 disconnect_peers(alice, &[bob, charlie, david, &nodes[4], &nodes[5]]);
1089 disconnect_peers(david, &[bob, charlie, &nodes[4], &nodes[5]]);
1091 let absolute_expiry = david.node.duration_since_epoch() + MAX_SHORT_LIVED_RELATIVE_EXPIRY;
1092 let payment_id = PaymentId([1; 32]);
1093 match david.node.create_refund_builder(
1094 10_000_000, absolute_expiry, payment_id, Retry::Attempts(0), None
1096 Ok(_) => panic!("Expected error"),
1097 Err(e) => assert_eq!(e, Bolt12SemanticError::MissingPaths),
1100 let mut args = ReconnectArgs::new(charlie, david);
1101 args.send_channel_ready = (true, true);
1102 reconnect_nodes(args);
1104 let refund = david.node
1105 .create_refund_builder(10_000_000, absolute_expiry, payment_id, Retry::Attempts(0), None)
1109 match alice.node.request_refund_payment(&refund) {
1110 Ok(_) => panic!("Expected error"),
1111 Err(e) => assert_eq!(e, Bolt12SemanticError::MissingPaths),
1114 let mut args = ReconnectArgs::new(alice, bob);
1115 args.send_channel_ready = (true, true);
1116 reconnect_nodes(args);
1118 assert!(alice.node.request_refund_payment(&refund).is_ok());
1121 /// Fails creating an invoice request when the offer contains an unsupported chain.
1123 fn fails_creating_invoice_request_for_unsupported_chain() {
1124 let chanmon_cfgs = create_chanmon_cfgs(2);
1125 let node_cfgs = create_node_cfgs(2, &chanmon_cfgs);
1126 let node_chanmgrs = create_node_chanmgrs(2, &node_cfgs, &[None, None]);
1127 let nodes = create_network(2, &node_cfgs, &node_chanmgrs);
1129 create_announced_chan_between_nodes_with_value(&nodes, 0, 1, 10_000_000, 1_000_000_000);
1131 let alice = &nodes[0];
1132 let bob = &nodes[1];
1134 let offer = alice.node
1135 .create_offer_builder(None).unwrap()
1137 .chain(Network::Signet)
1140 let payment_id = PaymentId([1; 32]);
1141 match bob.node.pay_for_offer(&offer, None, None, None, payment_id, Retry::Attempts(0), None) {
1142 Ok(_) => panic!("Expected error"),
1143 Err(e) => assert_eq!(e, Bolt12SemanticError::UnsupportedChain),
1147 /// Fails requesting a payment when the refund contains an unsupported chain.
1149 fn fails_sending_invoice_with_unsupported_chain_for_refund() {
1150 let chanmon_cfgs = create_chanmon_cfgs(2);
1151 let node_cfgs = create_node_cfgs(2, &chanmon_cfgs);
1152 let node_chanmgrs = create_node_chanmgrs(2, &node_cfgs, &[None, None]);
1153 let nodes = create_network(2, &node_cfgs, &node_chanmgrs);
1155 create_announced_chan_between_nodes_with_value(&nodes, 0, 1, 10_000_000, 1_000_000_000);
1157 let alice = &nodes[0];
1158 let bob = &nodes[1];
1160 let absolute_expiry = Duration::from_secs(u64::MAX);
1161 let payment_id = PaymentId([1; 32]);
1162 let refund = bob.node
1163 .create_refund_builder(10_000_000, absolute_expiry, payment_id, Retry::Attempts(0), None)
1165 .chain(Network::Signet)
1168 match alice.node.request_refund_payment(&refund) {
1169 Ok(_) => panic!("Expected error"),
1170 Err(e) => assert_eq!(e, Bolt12SemanticError::UnsupportedChain),
1174 /// Fails creating an invoice request when a blinded reply path cannot be created without exposing
1177 fn fails_creating_invoice_request_without_blinded_reply_path() {
1178 let chanmon_cfgs = create_chanmon_cfgs(6);
1179 let node_cfgs = create_node_cfgs(6, &chanmon_cfgs);
1180 let node_chanmgrs = create_node_chanmgrs(6, &node_cfgs, &[None, None, None, None, None, None]);
1181 let nodes = create_network(6, &node_cfgs, &node_chanmgrs);
1183 create_unannounced_chan_between_nodes_with_value(&nodes, 0, 1, 10_000_000, 1_000_000_000);
1184 create_unannounced_chan_between_nodes_with_value(&nodes, 2, 3, 10_000_000, 1_000_000_000);
1185 create_announced_chan_between_nodes_with_value(&nodes, 1, 2, 10_000_000, 1_000_000_000);
1186 create_announced_chan_between_nodes_with_value(&nodes, 1, 4, 10_000_000, 1_000_000_000);
1187 create_announced_chan_between_nodes_with_value(&nodes, 1, 5, 10_000_000, 1_000_000_000);
1189 let (alice, bob, charlie, david) = (&nodes[0], &nodes[1], &nodes[2], &nodes[3]);
1191 disconnect_peers(alice, &[charlie, david, &nodes[4], &nodes[5]]);
1192 disconnect_peers(david, &[bob, &nodes[4], &nodes[5]]);
1194 let offer = alice.node
1195 .create_offer_builder(None).unwrap()
1196 .amount_msats(10_000_000)
1199 let payment_id = PaymentId([1; 32]);
1201 match david.node.pay_for_offer(&offer, None, None, None, payment_id, Retry::Attempts(0), None) {
1202 Ok(_) => panic!("Expected error"),
1203 Err(e) => assert_eq!(e, Bolt12SemanticError::MissingPaths),
1206 assert!(nodes[0].node.list_recent_payments().is_empty());
1210 fn fails_creating_invoice_request_with_duplicate_payment_id() {
1211 let chanmon_cfgs = create_chanmon_cfgs(6);
1212 let node_cfgs = create_node_cfgs(6, &chanmon_cfgs);
1213 let node_chanmgrs = create_node_chanmgrs(6, &node_cfgs, &[None, None, None, None, None, None]);
1214 let nodes = create_network(6, &node_cfgs, &node_chanmgrs);
1216 create_unannounced_chan_between_nodes_with_value(&nodes, 0, 1, 10_000_000, 1_000_000_000);
1217 create_unannounced_chan_between_nodes_with_value(&nodes, 2, 3, 10_000_000, 1_000_000_000);
1218 create_announced_chan_between_nodes_with_value(&nodes, 1, 2, 10_000_000, 1_000_000_000);
1219 create_announced_chan_between_nodes_with_value(&nodes, 1, 4, 10_000_000, 1_000_000_000);
1220 create_announced_chan_between_nodes_with_value(&nodes, 1, 5, 10_000_000, 1_000_000_000);
1221 create_announced_chan_between_nodes_with_value(&nodes, 2, 4, 10_000_000, 1_000_000_000);
1222 create_announced_chan_between_nodes_with_value(&nodes, 2, 5, 10_000_000, 1_000_000_000);
1224 let (alice, _bob, charlie, david) = (&nodes[0], &nodes[1], &nodes[2], &nodes[3]);
1226 disconnect_peers(alice, &[charlie, david, &nodes[4], &nodes[5]]);
1228 let offer = alice.node
1229 .create_offer_builder(None).unwrap()
1230 .amount_msats(10_000_000)
1233 let payment_id = PaymentId([1; 32]);
1235 david.node.pay_for_offer(
1236 &offer, None, None, None, payment_id, Retry::Attempts(0), None
1239 expect_recent_payment!(david, RecentPaymentDetails::AwaitingInvoice, payment_id);
1241 match david.node.pay_for_offer(&offer, None, None, None, payment_id, Retry::Attempts(0), None) {
1242 Ok(_) => panic!("Expected error"),
1243 Err(e) => assert_eq!(e, Bolt12SemanticError::DuplicatePaymentId),
1246 expect_recent_payment!(david, RecentPaymentDetails::AwaitingInvoice, payment_id);
1250 fn fails_creating_refund_with_duplicate_payment_id() {
1251 let chanmon_cfgs = create_chanmon_cfgs(2);
1252 let node_cfgs = create_node_cfgs(2, &chanmon_cfgs);
1253 let node_chanmgrs = create_node_chanmgrs(2, &node_cfgs, &[None, None]);
1254 let nodes = create_network(2, &node_cfgs, &node_chanmgrs);
1256 create_announced_chan_between_nodes_with_value(&nodes, 0, 1, 10_000_000, 1_000_000_000);
1258 let absolute_expiry = Duration::from_secs(u64::MAX);
1259 let payment_id = PaymentId([1; 32]);
1261 nodes[0].node.create_refund_builder(
1262 10_000, absolute_expiry, payment_id, Retry::Attempts(0), None
1265 expect_recent_payment!(nodes[0], RecentPaymentDetails::AwaitingInvoice, payment_id);
1267 match nodes[0].node.create_refund_builder(
1268 10_000, absolute_expiry, payment_id, Retry::Attempts(0), None
1270 Ok(_) => panic!("Expected error"),
1271 Err(e) => assert_eq!(e, Bolt12SemanticError::DuplicatePaymentId),
1274 expect_recent_payment!(nodes[0], RecentPaymentDetails::AwaitingInvoice, payment_id);
1278 fn fails_sending_invoice_without_blinded_payment_paths_for_offer() {
1279 let mut accept_forward_cfg = test_default_channel_config();
1280 accept_forward_cfg.accept_forwards_to_priv_channels = true;
1282 // Clearing route_blinding prevents forming any payment paths since the node is unannounced.
1283 let mut features = channelmanager::provided_init_features(&accept_forward_cfg);
1284 features.set_onion_messages_optional();
1285 features.clear_route_blinding();
1287 let chanmon_cfgs = create_chanmon_cfgs(6);
1288 let node_cfgs = create_node_cfgs(6, &chanmon_cfgs);
1290 *node_cfgs[1].override_init_features.borrow_mut() = Some(features);
1292 let node_chanmgrs = create_node_chanmgrs(
1293 6, &node_cfgs, &[None, Some(accept_forward_cfg), None, None, None, None]
1295 let nodes = create_network(6, &node_cfgs, &node_chanmgrs);
1297 create_unannounced_chan_between_nodes_with_value(&nodes, 0, 1, 10_000_000, 1_000_000_000);
1298 create_unannounced_chan_between_nodes_with_value(&nodes, 2, 3, 10_000_000, 1_000_000_000);
1299 create_announced_chan_between_nodes_with_value(&nodes, 1, 2, 10_000_000, 1_000_000_000);
1300 create_announced_chan_between_nodes_with_value(&nodes, 1, 4, 10_000_000, 1_000_000_000);
1301 create_announced_chan_between_nodes_with_value(&nodes, 1, 5, 10_000_000, 1_000_000_000);
1302 create_announced_chan_between_nodes_with_value(&nodes, 2, 4, 10_000_000, 1_000_000_000);
1303 create_announced_chan_between_nodes_with_value(&nodes, 2, 5, 10_000_000, 1_000_000_000);
1305 let (alice, bob, charlie, david) = (&nodes[0], &nodes[1], &nodes[2], &nodes[3]);
1306 let alice_id = alice.node.get_our_node_id();
1307 let bob_id = bob.node.get_our_node_id();
1308 let charlie_id = charlie.node.get_our_node_id();
1309 let david_id = david.node.get_our_node_id();
1311 disconnect_peers(alice, &[charlie, david, &nodes[4], &nodes[5]]);
1312 disconnect_peers(david, &[bob, &nodes[4], &nodes[5]]);
1314 let offer = alice.node
1315 .create_offer_builder(None).unwrap()
1316 .amount_msats(10_000_000)
1319 let payment_id = PaymentId([1; 32]);
1320 david.node.pay_for_offer(&offer, None, None, None, payment_id, Retry::Attempts(0), None)
1323 connect_peers(david, bob);
1325 let onion_message = david.onion_messenger.next_onion_message_for_peer(bob_id).unwrap();
1326 bob.onion_messenger.handle_onion_message(&david_id, &onion_message);
1328 connect_peers(alice, charlie);
1330 let onion_message = bob.onion_messenger.next_onion_message_for_peer(alice_id).unwrap();
1331 alice.onion_messenger.handle_onion_message(&bob_id, &onion_message);
1333 let onion_message = alice.onion_messenger.next_onion_message_for_peer(charlie_id).unwrap();
1334 charlie.onion_messenger.handle_onion_message(&alice_id, &onion_message);
1336 let onion_message = charlie.onion_messenger.next_onion_message_for_peer(david_id).unwrap();
1337 david.onion_messenger.handle_onion_message(&charlie_id, &onion_message);
1339 let invoice_error = extract_invoice_error(david, &onion_message);
1340 assert_eq!(invoice_error, InvoiceError::from(Bolt12SemanticError::MissingPaths));
1344 fn fails_sending_invoice_without_blinded_payment_paths_for_refund() {
1345 let mut accept_forward_cfg = test_default_channel_config();
1346 accept_forward_cfg.accept_forwards_to_priv_channels = true;
1348 // Clearing route_blinding prevents forming any payment paths since the node is unannounced.
1349 let mut features = channelmanager::provided_init_features(&accept_forward_cfg);
1350 features.set_onion_messages_optional();
1351 features.clear_route_blinding();
1353 let chanmon_cfgs = create_chanmon_cfgs(6);
1354 let node_cfgs = create_node_cfgs(6, &chanmon_cfgs);
1356 *node_cfgs[1].override_init_features.borrow_mut() = Some(features);
1358 let node_chanmgrs = create_node_chanmgrs(
1359 6, &node_cfgs, &[None, Some(accept_forward_cfg), None, None, None, None]
1361 let nodes = create_network(6, &node_cfgs, &node_chanmgrs);
1363 create_unannounced_chan_between_nodes_with_value(&nodes, 0, 1, 10_000_000, 1_000_000_000);
1364 create_unannounced_chan_between_nodes_with_value(&nodes, 2, 3, 10_000_000, 1_000_000_000);
1365 create_announced_chan_between_nodes_with_value(&nodes, 1, 2, 10_000_000, 1_000_000_000);
1366 create_announced_chan_between_nodes_with_value(&nodes, 1, 4, 10_000_000, 1_000_000_000);
1367 create_announced_chan_between_nodes_with_value(&nodes, 1, 5, 10_000_000, 1_000_000_000);
1368 create_announced_chan_between_nodes_with_value(&nodes, 2, 4, 10_000_000, 1_000_000_000);
1369 create_announced_chan_between_nodes_with_value(&nodes, 2, 5, 10_000_000, 1_000_000_000);
1371 let (alice, bob, charlie, david) = (&nodes[0], &nodes[1], &nodes[2], &nodes[3]);
1373 disconnect_peers(alice, &[charlie, david, &nodes[4], &nodes[5]]);
1374 disconnect_peers(david, &[bob, &nodes[4], &nodes[5]]);
1376 let absolute_expiry = Duration::from_secs(u64::MAX);
1377 let payment_id = PaymentId([1; 32]);
1378 let refund = david.node
1379 .create_refund_builder(10_000_000, absolute_expiry, payment_id, Retry::Attempts(0), None)
1383 match alice.node.request_refund_payment(&refund) {
1384 Ok(_) => panic!("Expected error"),
1385 Err(e) => assert_eq!(e, Bolt12SemanticError::MissingPaths),
1390 fn fails_paying_invoice_more_than_once() {
1391 let mut accept_forward_cfg = test_default_channel_config();
1392 accept_forward_cfg.accept_forwards_to_priv_channels = true;
1394 let mut features = channelmanager::provided_init_features(&accept_forward_cfg);
1395 features.set_onion_messages_optional();
1396 features.set_route_blinding_optional();
1398 let chanmon_cfgs = create_chanmon_cfgs(6);
1399 let node_cfgs = create_node_cfgs(6, &chanmon_cfgs);
1401 *node_cfgs[1].override_init_features.borrow_mut() = Some(features);
1403 let node_chanmgrs = create_node_chanmgrs(
1404 6, &node_cfgs, &[None, Some(accept_forward_cfg), None, None, None, None]
1406 let nodes = create_network(6, &node_cfgs, &node_chanmgrs);
1408 create_unannounced_chan_between_nodes_with_value(&nodes, 0, 1, 10_000_000, 1_000_000_000);
1409 create_unannounced_chan_between_nodes_with_value(&nodes, 2, 3, 10_000_000, 1_000_000_000);
1410 create_announced_chan_between_nodes_with_value(&nodes, 1, 2, 10_000_000, 1_000_000_000);
1411 create_announced_chan_between_nodes_with_value(&nodes, 1, 4, 10_000_000, 1_000_000_000);
1412 create_announced_chan_between_nodes_with_value(&nodes, 1, 5, 10_000_000, 1_000_000_000);
1413 create_announced_chan_between_nodes_with_value(&nodes, 2, 4, 10_000_000, 1_000_000_000);
1414 create_announced_chan_between_nodes_with_value(&nodes, 2, 5, 10_000_000, 1_000_000_000);
1416 let (alice, bob, charlie, david) = (&nodes[0], &nodes[1], &nodes[2], &nodes[3]);
1417 let alice_id = alice.node.get_our_node_id();
1418 let bob_id = bob.node.get_our_node_id();
1419 let charlie_id = charlie.node.get_our_node_id();
1420 let david_id = david.node.get_our_node_id();
1422 disconnect_peers(alice, &[charlie, david, &nodes[4], &nodes[5]]);
1423 disconnect_peers(david, &[bob, &nodes[4], &nodes[5]]);
1425 let absolute_expiry = Duration::from_secs(u64::MAX);
1426 let payment_id = PaymentId([1; 32]);
1427 let refund = david.node
1428 .create_refund_builder(10_000_000, absolute_expiry, payment_id, Retry::Attempts(0), None)
1431 expect_recent_payment!(david, RecentPaymentDetails::AwaitingInvoice, payment_id);
1433 // Alice sends the first invoice
1434 alice.node.request_refund_payment(&refund).unwrap();
1436 connect_peers(alice, charlie);
1438 let onion_message = alice.onion_messenger.next_onion_message_for_peer(charlie_id).unwrap();
1439 charlie.onion_messenger.handle_onion_message(&alice_id, &onion_message);
1441 let onion_message = charlie.onion_messenger.next_onion_message_for_peer(david_id).unwrap();
1442 david.onion_messenger.handle_onion_message(&charlie_id, &onion_message);
1444 // David pays the first invoice
1445 let payment_context = PaymentContext::Bolt12Refund(Bolt12RefundContext {});
1446 let invoice1 = extract_invoice(david, &onion_message);
1448 route_bolt12_payment(david, &[charlie, bob, alice], &invoice1);
1449 expect_recent_payment!(david, RecentPaymentDetails::Pending, payment_id);
1451 claim_bolt12_payment(david, &[charlie, bob, alice], payment_context);
1452 expect_recent_payment!(david, RecentPaymentDetails::Fulfilled, payment_id);
1454 disconnect_peers(alice, &[charlie]);
1456 // Alice sends the second invoice
1457 alice.node.request_refund_payment(&refund).unwrap();
1459 connect_peers(alice, charlie);
1460 connect_peers(david, bob);
1462 let onion_message = alice.onion_messenger.next_onion_message_for_peer(charlie_id).unwrap();
1463 charlie.onion_messenger.handle_onion_message(&alice_id, &onion_message);
1465 let onion_message = charlie.onion_messenger.next_onion_message_for_peer(david_id).unwrap();
1466 david.onion_messenger.handle_onion_message(&charlie_id, &onion_message);
1468 let invoice2 = extract_invoice(david, &onion_message);
1469 assert_eq!(invoice1.payer_metadata(), invoice2.payer_metadata());
1471 // David sends an error instead of paying the second invoice
1472 let onion_message = david.onion_messenger.next_onion_message_for_peer(bob_id).unwrap();
1473 bob.onion_messenger.handle_onion_message(&david_id, &onion_message);
1475 let onion_message = bob.onion_messenger.next_onion_message_for_peer(alice_id).unwrap();
1476 alice.onion_messenger.handle_onion_message(&bob_id, &onion_message);
1478 let invoice_error = extract_invoice_error(alice, &onion_message);
1479 assert_eq!(invoice_error, InvoiceError::from_string("DuplicateInvoice".to_string()));