1 //! Convenient utilities to create an invoice.
3 use crate::{CreationError, Currency, Invoice, InvoiceBuilder, SignOrCreationError};
4 use crate::payment::Payer;
6 use crate::{prelude::*, Description, InvoiceDescription, Sha256};
8 use bitcoin_hashes::Hash;
10 use lightning::chain::chaininterface::{BroadcasterInterface, FeeEstimator};
11 use lightning::chain::keysinterface::{Recipient, KeysInterface};
12 use lightning::ln::{PaymentHash, PaymentPreimage, PaymentSecret};
13 use lightning::ln::channelmanager::{ChannelDetails, ChannelManager, PaymentId, PaymentSendFailure, MIN_FINAL_CLTV_EXPIRY};
14 #[cfg(feature = "std")]
15 use lightning::ln::channelmanager::{PhantomRouteHints, MIN_CLTV_EXPIRY_DELTA};
16 use lightning::ln::inbound_payment::{create, create_from_hash, ExpandedKey};
17 use lightning::routing::gossip::RoutingFees;
18 use lightning::routing::router::{InFlightHtlcs, Route, RouteHint, RouteHintHop};
19 use lightning::util::logger::Logger;
20 use secp256k1::PublicKey;
22 use core::time::Duration;
24 #[cfg(feature = "std")]
25 /// Utility to create an invoice that can be paid to one of multiple nodes, or a "phantom invoice."
26 /// See [`PhantomKeysManager`] for more information on phantom node payments.
28 /// `phantom_route_hints` parameter:
29 /// * Contains channel info for all nodes participating in the phantom invoice
30 /// * Entries are retrieved from a call to [`ChannelManager::get_phantom_route_hints`] on each
31 /// participating node
32 /// * It is fine to cache `phantom_route_hints` and reuse it across invoices, as long as the data is
33 /// updated when a channel becomes disabled or closes
34 /// * Note that if too many channels are included in [`PhantomRouteHints::channels`], the invoice
35 /// may be too long for QR code scanning. To fix this, `PhantomRouteHints::channels` may be pared
38 /// `payment_hash` can be specified if you have a specific need for a custom payment hash (see the difference
39 /// between [`ChannelManager::create_inbound_payment`] and [`ChannelManager::create_inbound_payment_for_hash`]).
40 /// If `None` is provided for `payment_hash`, then one will be created.
42 /// `invoice_expiry_delta_secs` describes the number of seconds that the invoice is valid for
43 /// in excess of the current time.
45 /// Note that the provided `keys_manager`'s `KeysInterface` implementation must support phantom
46 /// invoices in its `sign_invoice` implementation ([`PhantomKeysManager`] satisfies this
49 /// [`PhantomKeysManager`]: lightning::chain::keysinterface::PhantomKeysManager
50 /// [`ChannelManager::get_phantom_route_hints`]: lightning::ln::channelmanager::ChannelManager::get_phantom_route_hints
51 /// [`ChannelManager::create_inbound_payment`]: lightning::ln::channelmanager::ChannelManager::create_inbound_payment
52 /// [`ChannelManager::create_inbound_payment_for_hash`]: lightning::ln::channelmanager::ChannelManager::create_inbound_payment_for_hash
53 /// [`PhantomRouteHints::channels`]: lightning::ln::channelmanager::PhantomRouteHints::channels
54 pub fn create_phantom_invoice<K: Deref, L: Deref>(
55 amt_msat: Option<u64>, payment_hash: Option<PaymentHash>, description: String,
56 invoice_expiry_delta_secs: u32, phantom_route_hints: Vec<PhantomRouteHints>, keys_manager: K,
57 logger: L, network: Currency,
58 ) -> Result<Invoice, SignOrCreationError<()>>
60 K::Target: KeysInterface,
63 let description = Description::new(description).map_err(SignOrCreationError::CreationError)?;
64 let description = InvoiceDescription::Direct(&description,);
65 _create_phantom_invoice::<K, L>(
66 amt_msat, payment_hash, description, invoice_expiry_delta_secs, phantom_route_hints,
67 keys_manager, logger, network,
71 #[cfg(feature = "std")]
72 /// Utility to create an invoice that can be paid to one of multiple nodes, or a "phantom invoice."
73 /// See [`PhantomKeysManager`] for more information on phantom node payments.
75 /// `phantom_route_hints` parameter:
76 /// * Contains channel info for all nodes participating in the phantom invoice
77 /// * Entries are retrieved from a call to [`ChannelManager::get_phantom_route_hints`] on each
78 /// participating node
79 /// * It is fine to cache `phantom_route_hints` and reuse it across invoices, as long as the data is
80 /// updated when a channel becomes disabled or closes
81 /// * Note that if too many channels are included in [`PhantomRouteHints::channels`], the invoice
82 /// may be too long for QR code scanning. To fix this, `PhantomRouteHints::channels` may be pared
85 /// `description_hash` is a SHA-256 hash of the description text
87 /// `payment_hash` can be specified if you have a specific need for a custom payment hash (see the difference
88 /// between [`ChannelManager::create_inbound_payment`] and [`ChannelManager::create_inbound_payment_for_hash`]).
89 /// If `None` is provided for `payment_hash`, then one will be created.
91 /// `invoice_expiry_delta_secs` describes the number of seconds that the invoice is valid for
92 /// in excess of the current time.
94 /// Note that the provided `keys_manager`'s `KeysInterface` implementation must support phantom
95 /// invoices in its `sign_invoice` implementation ([`PhantomKeysManager`] satisfies this
98 /// [`PhantomKeysManager`]: lightning::chain::keysinterface::PhantomKeysManager
99 /// [`ChannelManager::get_phantom_route_hints`]: lightning::ln::channelmanager::ChannelManager::get_phantom_route_hints
100 /// [`ChannelManager::create_inbound_payment`]: lightning::ln::channelmanager::ChannelManager::create_inbound_payment
101 /// [`ChannelManager::create_inbound_payment_for_hash`]: lightning::ln::channelmanager::ChannelManager::create_inbound_payment_for_hash
102 /// [`PhantomRouteHints::channels`]: lightning::ln::channelmanager::PhantomRouteHints::channels
103 pub fn create_phantom_invoice_with_description_hash<K: Deref, L: Deref>(
104 amt_msat: Option<u64>, payment_hash: Option<PaymentHash>, invoice_expiry_delta_secs: u32,
105 description_hash: Sha256, phantom_route_hints: Vec<PhantomRouteHints>, keys_manager: K,
106 logger: L, network: Currency
107 ) -> Result<Invoice, SignOrCreationError<()>>
109 K::Target: KeysInterface,
112 _create_phantom_invoice::<K, L>(
113 amt_msat, payment_hash, InvoiceDescription::Hash(&description_hash),
114 invoice_expiry_delta_secs, phantom_route_hints, keys_manager, logger, network,
118 #[cfg(feature = "std")]
119 fn _create_phantom_invoice<K: Deref, L: Deref>(
120 amt_msat: Option<u64>, payment_hash: Option<PaymentHash>, description: InvoiceDescription,
121 invoice_expiry_delta_secs: u32, phantom_route_hints: Vec<PhantomRouteHints>, keys_manager: K,
122 logger: L, network: Currency,
123 ) -> Result<Invoice, SignOrCreationError<()>>
125 K::Target: KeysInterface,
128 use std::time::{SystemTime, UNIX_EPOCH};
130 if phantom_route_hints.len() == 0 {
131 return Err(SignOrCreationError::CreationError(
132 CreationError::MissingRouteHints,
136 let invoice = match description {
137 InvoiceDescription::Direct(description) => {
138 InvoiceBuilder::new(network).description(description.0.clone())
140 InvoiceDescription::Hash(hash) => InvoiceBuilder::new(network).description_hash(hash.0),
143 // If we ever see performance here being too slow then we should probably take this ExpandedKey as a parameter instead.
144 let keys = ExpandedKey::new(&keys_manager.get_inbound_payment_key_material());
145 let (payment_hash, payment_secret) = if let Some(payment_hash) = payment_hash {
146 let payment_secret = create_from_hash(
150 invoice_expiry_delta_secs,
152 .duration_since(UNIX_EPOCH)
153 .expect("Time must be > 1970")
156 .map_err(|_| SignOrCreationError::CreationError(CreationError::InvalidAmount))?;
157 (payment_hash, payment_secret)
162 invoice_expiry_delta_secs,
165 .duration_since(UNIX_EPOCH)
166 .expect("Time must be > 1970")
169 .map_err(|_| SignOrCreationError::CreationError(CreationError::InvalidAmount))?
172 log_trace!(logger, "Creating phantom invoice from {} participating nodes with payment hash {}",
173 phantom_route_hints.len(), log_bytes!(payment_hash.0));
175 let mut invoice = invoice
177 .payment_hash(Hash::from_slice(&payment_hash.0).unwrap())
178 .payment_secret(payment_secret)
179 .min_final_cltv_expiry(MIN_FINAL_CLTV_EXPIRY.into())
180 .expiry_time(Duration::from_secs(invoice_expiry_delta_secs.into()));
181 if let Some(amt) = amt_msat {
182 invoice = invoice.amount_milli_satoshis(amt);
185 for PhantomRouteHints { channels, phantom_scid, real_node_pubkey } in phantom_route_hints {
186 log_trace!(logger, "Generating phantom route hints for node {}",
187 log_pubkey!(real_node_pubkey));
188 let mut route_hints = filter_channels(channels, amt_msat, &logger);
190 // If we have any public channel, the route hints from `filter_channels` will be empty.
191 // In that case we create a RouteHint on which we will push a single hop with the phantom
192 // route into the invoice, and let the sender find the path to the `real_node_pubkey`
193 // node by looking at our public channels.
194 if route_hints.is_empty() {
195 route_hints.push(RouteHint(vec![]))
197 for mut route_hint in route_hints {
198 route_hint.0.push(RouteHintHop {
199 src_node_id: real_node_pubkey,
200 short_channel_id: phantom_scid,
203 proportional_millionths: 0,
205 cltv_expiry_delta: MIN_CLTV_EXPIRY_DELTA,
206 htlc_minimum_msat: None,
207 htlc_maximum_msat: None,});
208 invoice = invoice.private_route(route_hint.clone());
212 let raw_invoice = match invoice.build_raw() {
214 Err(e) => return Err(SignOrCreationError::CreationError(e))
216 let hrp_str = raw_invoice.hrp.to_string();
217 let hrp_bytes = hrp_str.as_bytes();
218 let data_without_signature = raw_invoice.data.to_base32();
219 let signed_raw_invoice = raw_invoice.sign(|_| keys_manager.sign_invoice(hrp_bytes, &data_without_signature, Recipient::PhantomNode));
220 match signed_raw_invoice {
221 Ok(inv) => Ok(Invoice::from_signed(inv).unwrap()),
222 Err(e) => Err(SignOrCreationError::SignError(e))
226 #[cfg(feature = "std")]
227 /// Utility to construct an invoice. Generally, unless you want to do something like a custom
228 /// cltv_expiry, this is what you should be using to create an invoice. The reason being, this
229 /// method stores the invoice's payment secret and preimage in `ChannelManager`, so (a) the user
230 /// doesn't have to store preimage/payment secret information and (b) `ChannelManager` can verify
231 /// that the payment secret is valid when the invoice is paid.
233 /// `invoice_expiry_delta_secs` describes the number of seconds that the invoice is valid for
234 /// in excess of the current time.
235 pub fn create_invoice_from_channelmanager<M: Deref, T: Deref, K: Deref, F: Deref, L: Deref>(
236 channelmanager: &ChannelManager<M, T, K, F, L>, keys_manager: K, logger: L,
237 network: Currency, amt_msat: Option<u64>, description: String, invoice_expiry_delta_secs: u32
238 ) -> Result<Invoice, SignOrCreationError<()>>
240 M::Target: chain::Watch<<K::Target as KeysInterface>::Signer>,
241 T::Target: BroadcasterInterface,
242 K::Target: KeysInterface,
243 F::Target: FeeEstimator,
246 use std::time::SystemTime;
247 let duration = SystemTime::now().duration_since(SystemTime::UNIX_EPOCH)
248 .expect("for the foreseeable future this shouldn't happen");
249 create_invoice_from_channelmanager_and_duration_since_epoch(
250 channelmanager, keys_manager, logger, network, amt_msat, description, duration,
251 invoice_expiry_delta_secs
255 #[cfg(feature = "std")]
256 /// Utility to construct an invoice. Generally, unless you want to do something like a custom
257 /// cltv_expiry, this is what you should be using to create an invoice. The reason being, this
258 /// method stores the invoice's payment secret and preimage in `ChannelManager`, so (a) the user
259 /// doesn't have to store preimage/payment secret information and (b) `ChannelManager` can verify
260 /// that the payment secret is valid when the invoice is paid.
261 /// Use this variant if you want to pass the `description_hash` to the invoice.
263 /// `invoice_expiry_delta_secs` describes the number of seconds that the invoice is valid for
264 /// in excess of the current time.
265 pub fn create_invoice_from_channelmanager_with_description_hash<M: Deref, T: Deref, K: Deref, F: Deref, L: Deref>(
266 channelmanager: &ChannelManager<M, T, K, F, L>, keys_manager: K, logger: L,
267 network: Currency, amt_msat: Option<u64>, description_hash: Sha256,
268 invoice_expiry_delta_secs: u32
269 ) -> Result<Invoice, SignOrCreationError<()>>
271 M::Target: chain::Watch<<K::Target as KeysInterface>::Signer>,
272 T::Target: BroadcasterInterface,
273 K::Target: KeysInterface,
274 F::Target: FeeEstimator,
277 use std::time::SystemTime;
279 let duration = SystemTime::now()
280 .duration_since(SystemTime::UNIX_EPOCH)
281 .expect("for the foreseeable future this shouldn't happen");
283 create_invoice_from_channelmanager_with_description_hash_and_duration_since_epoch(
284 channelmanager, keys_manager, logger, network, amt_msat,
285 description_hash, duration, invoice_expiry_delta_secs
289 /// See [`create_invoice_from_channelmanager_with_description_hash`]
290 /// This version can be used in a `no_std` environment, where [`std::time::SystemTime`] is not
291 /// available and the current time is supplied by the caller.
292 pub fn create_invoice_from_channelmanager_with_description_hash_and_duration_since_epoch<M: Deref, T: Deref, K: Deref, F: Deref, L: Deref>(
293 channelmanager: &ChannelManager<M, T, K, F, L>, keys_manager: K, logger: L,
294 network: Currency, amt_msat: Option<u64>, description_hash: Sha256,
295 duration_since_epoch: Duration, invoice_expiry_delta_secs: u32
296 ) -> Result<Invoice, SignOrCreationError<()>>
298 M::Target: chain::Watch<<K::Target as KeysInterface>::Signer>,
299 T::Target: BroadcasterInterface,
300 K::Target: KeysInterface,
301 F::Target: FeeEstimator,
304 _create_invoice_from_channelmanager_and_duration_since_epoch(
305 channelmanager, keys_manager, logger, network, amt_msat,
306 InvoiceDescription::Hash(&description_hash),
307 duration_since_epoch, invoice_expiry_delta_secs
311 /// See [`create_invoice_from_channelmanager`]
312 /// This version can be used in a `no_std` environment, where [`std::time::SystemTime`] is not
313 /// available and the current time is supplied by the caller.
314 pub fn create_invoice_from_channelmanager_and_duration_since_epoch<M: Deref, T: Deref, K: Deref, F: Deref, L: Deref>(
315 channelmanager: &ChannelManager<M, T, K, F, L>, keys_manager: K, logger: L,
316 network: Currency, amt_msat: Option<u64>, description: String, duration_since_epoch: Duration,
317 invoice_expiry_delta_secs: u32
318 ) -> Result<Invoice, SignOrCreationError<()>>
320 M::Target: chain::Watch<<K::Target as KeysInterface>::Signer>,
321 T::Target: BroadcasterInterface,
322 K::Target: KeysInterface,
323 F::Target: FeeEstimator,
326 _create_invoice_from_channelmanager_and_duration_since_epoch(
327 channelmanager, keys_manager, logger, network, amt_msat,
328 InvoiceDescription::Direct(
329 &Description::new(description).map_err(SignOrCreationError::CreationError)?,
331 duration_since_epoch, invoice_expiry_delta_secs
335 fn _create_invoice_from_channelmanager_and_duration_since_epoch<M: Deref, T: Deref, K: Deref, F: Deref, L: Deref>(
336 channelmanager: &ChannelManager<M, T, K, F, L>, keys_manager: K, logger: L,
337 network: Currency, amt_msat: Option<u64>, description: InvoiceDescription,
338 duration_since_epoch: Duration, invoice_expiry_delta_secs: u32
339 ) -> Result<Invoice, SignOrCreationError<()>>
341 M::Target: chain::Watch<<K::Target as KeysInterface>::Signer>,
342 T::Target: BroadcasterInterface,
343 K::Target: KeysInterface,
344 F::Target: FeeEstimator,
347 // `create_inbound_payment` only returns an error if the amount is greater than the total bitcoin
349 let (payment_hash, payment_secret) = channelmanager
350 .create_inbound_payment(amt_msat, invoice_expiry_delta_secs)
351 .map_err(|()| SignOrCreationError::CreationError(CreationError::InvalidAmount))?;
352 let our_node_pubkey = channelmanager.get_our_node_id();
353 let channels = channelmanager.list_channels();
355 log_trace!(logger, "Creating invoice with payment hash {}", log_bytes!(payment_hash.0));
357 let invoice = match description {
358 InvoiceDescription::Direct(description) => {
359 InvoiceBuilder::new(network).description(description.0.clone())
361 InvoiceDescription::Hash(hash) => InvoiceBuilder::new(network).description_hash(hash.0),
364 let mut invoice = invoice
365 .duration_since_epoch(duration_since_epoch)
366 .payee_pub_key(our_node_pubkey)
367 .payment_hash(Hash::from_slice(&payment_hash.0).unwrap())
368 .payment_secret(payment_secret)
370 .min_final_cltv_expiry(MIN_FINAL_CLTV_EXPIRY.into())
371 .expiry_time(Duration::from_secs(invoice_expiry_delta_secs.into()));
372 if let Some(amt) = amt_msat {
373 invoice = invoice.amount_milli_satoshis(amt);
376 let route_hints = filter_channels(channels, amt_msat, &logger);
377 for hint in route_hints {
378 invoice = invoice.private_route(hint);
381 let raw_invoice = match invoice.build_raw() {
383 Err(e) => return Err(SignOrCreationError::CreationError(e))
385 let hrp_str = raw_invoice.hrp.to_string();
386 let hrp_bytes = hrp_str.as_bytes();
387 let data_without_signature = raw_invoice.data.to_base32();
388 let signed_raw_invoice = raw_invoice.sign(|_| keys_manager.sign_invoice(hrp_bytes, &data_without_signature, Recipient::Node));
389 match signed_raw_invoice {
390 Ok(inv) => Ok(Invoice::from_signed(inv).unwrap()),
391 Err(e) => Err(SignOrCreationError::SignError(e))
395 /// Filters the `channels` for an invoice, and returns the corresponding `RouteHint`s to include
398 /// The filtering is based on the following criteria:
399 /// * Only one channel per counterparty node
400 /// * Always select the channel with the highest inbound capacity per counterparty node
401 /// * Prefer channels with capacity at least `min_inbound_capacity_msat` and where the channel
402 /// `is_usable` (i.e. the peer is connected).
403 /// * If any public channel exists, the returned `RouteHint`s will be empty, and the sender will
404 /// need to find the path by looking at the public channels instead
405 fn filter_channels<L: Deref>(
406 channels: Vec<ChannelDetails>, min_inbound_capacity_msat: Option<u64>, logger: &L
407 ) -> Vec<RouteHint> where L::Target: Logger {
408 let mut filtered_channels: HashMap<PublicKey, ChannelDetails> = HashMap::new();
409 let min_inbound_capacity = min_inbound_capacity_msat.unwrap_or(0);
410 let mut min_capacity_channel_exists = false;
411 let mut online_channel_exists = false;
412 let mut online_min_capacity_channel_exists = false;
414 log_trace!(logger, "Considering {} channels for invoice route hints", channels.len());
415 for channel in channels.into_iter().filter(|chan| chan.is_channel_ready) {
416 if channel.get_inbound_payment_scid().is_none() || channel.counterparty.forwarding_info.is_none() {
417 log_trace!(logger, "Ignoring channel {} for invoice route hints", log_bytes!(channel.channel_id));
421 if channel.is_public {
422 // If any public channel exists, return no hints and let the sender
423 // look at the public channels instead.
424 log_trace!(logger, "Not including channels in invoice route hints on account of public channel {}",
425 log_bytes!(channel.channel_id));
429 if channel.inbound_capacity_msat >= min_inbound_capacity {
430 if !min_capacity_channel_exists {
431 log_trace!(logger, "Channel with enough inbound capacity exists for invoice route hints");
432 min_capacity_channel_exists = true;
435 if channel.is_usable {
436 online_min_capacity_channel_exists = true;
440 if channel.is_usable {
441 if !online_channel_exists {
442 log_trace!(logger, "Channel with connected peer exists for invoice route hints");
443 online_channel_exists = true;
447 match filtered_channels.entry(channel.counterparty.node_id) {
448 hash_map::Entry::Occupied(mut entry) => {
449 let current_max_capacity = entry.get().inbound_capacity_msat;
450 if channel.inbound_capacity_msat < current_max_capacity {
452 "Preferring counterparty {} channel {} ({} msats) over {} ({} msats) for invoice route hints",
453 log_pubkey!(channel.counterparty.node_id),
454 log_bytes!(entry.get().channel_id), current_max_capacity,
455 log_bytes!(channel.channel_id), channel.inbound_capacity_msat);
459 "Preferring counterparty {} channel {} ({} msats) over {} ({} msats) for invoice route hints",
460 log_pubkey!(channel.counterparty.node_id),
461 log_bytes!(channel.channel_id), channel.inbound_capacity_msat,
462 log_bytes!(entry.get().channel_id), current_max_capacity);
463 entry.insert(channel);
465 hash_map::Entry::Vacant(entry) => {
466 entry.insert(channel);
471 let route_hint_from_channel = |channel: ChannelDetails| {
472 let forwarding_info = channel.counterparty.forwarding_info.as_ref().unwrap();
473 RouteHint(vec![RouteHintHop {
474 src_node_id: channel.counterparty.node_id,
475 short_channel_id: channel.get_inbound_payment_scid().unwrap(),
477 base_msat: forwarding_info.fee_base_msat,
478 proportional_millionths: forwarding_info.fee_proportional_millionths,
480 cltv_expiry_delta: forwarding_info.cltv_expiry_delta,
481 htlc_minimum_msat: channel.inbound_htlc_minimum_msat,
482 htlc_maximum_msat: channel.inbound_htlc_maximum_msat,}])
484 // If all channels are private, prefer to return route hints which have a higher capacity than
485 // the payment value and where we're currently connected to the channel counterparty.
486 // Even if we cannot satisfy both goals, always ensure we include *some* hints, preferring
487 // those which meet at least one criteria.
490 .map(|(_, channel)| channel)
492 let has_enough_capacity = channel.inbound_capacity_msat >= min_inbound_capacity;
493 let include_channel = if online_min_capacity_channel_exists {
494 has_enough_capacity && channel.is_usable
495 } else if min_capacity_channel_exists && online_channel_exists {
496 // If there are some online channels and some min_capacity channels, but no
497 // online-and-min_capacity channels, just include the min capacity ones and ignore
500 } else if min_capacity_channel_exists {
502 } else if online_channel_exists {
507 log_trace!(logger, "Including channel {} in invoice route hints",
508 log_bytes!(channel.channel_id));
509 } else if !has_enough_capacity {
510 log_trace!(logger, "Ignoring channel {} without enough capacity for invoice route hints",
511 log_bytes!(channel.channel_id));
513 debug_assert!(!channel.is_usable);
514 log_trace!(logger, "Ignoring channel {} with disconnected peer",
515 log_bytes!(channel.channel_id));
520 .map(route_hint_from_channel)
521 .collect::<Vec<RouteHint>>()
524 impl<M: Deref, T: Deref, K: Deref, F: Deref, L: Deref> Payer for ChannelManager<M, T, K, F, L>
526 M::Target: chain::Watch<<K::Target as KeysInterface>::Signer>,
527 T::Target: BroadcasterInterface,
528 K::Target: KeysInterface,
529 F::Target: FeeEstimator,
532 fn node_id(&self) -> PublicKey {
533 self.get_our_node_id()
536 fn first_hops(&self) -> Vec<ChannelDetails> {
537 self.list_usable_channels()
541 &self, route: &Route, payment_hash: PaymentHash, payment_secret: &Option<PaymentSecret>,
542 payment_id: PaymentId
543 ) -> Result<(), PaymentSendFailure> {
544 self.send_payment(route, payment_hash, payment_secret, payment_id)
547 fn send_spontaneous_payment(
548 &self, route: &Route, payment_preimage: PaymentPreimage, payment_id: PaymentId,
549 ) -> Result<(), PaymentSendFailure> {
550 self.send_spontaneous_payment(route, Some(payment_preimage), payment_id).map(|_| ())
554 &self, route: &Route, payment_id: PaymentId
555 ) -> Result<(), PaymentSendFailure> {
556 self.retry_payment(route, payment_id)
559 fn abandon_payment(&self, payment_id: PaymentId) {
560 self.abandon_payment(payment_id)
563 fn inflight_htlcs(&self) -> InFlightHtlcs { self.compute_inflight_htlcs() }
568 use core::time::Duration;
569 use crate::{Currency, Description, InvoiceDescription};
570 use bitcoin_hashes::Hash;
571 use bitcoin_hashes::sha256::Hash as Sha256;
572 use lightning::chain::keysinterface::PhantomKeysManager;
573 use lightning::ln::{PaymentPreimage, PaymentHash};
574 use lightning::ln::channelmanager::{self, PhantomRouteHints, MIN_FINAL_CLTV_EXPIRY, PaymentId};
575 use lightning::ln::functional_test_utils::*;
576 use lightning::ln::msgs::ChannelMessageHandler;
577 use lightning::routing::router::{PaymentParameters, RouteParameters, find_route};
578 use lightning::util::events::{MessageSendEvent, MessageSendEventsProvider, Event};
579 use lightning::util::test_utils;
580 use lightning::util::config::UserConfig;
581 use lightning::chain::keysinterface::KeysInterface;
582 use crate::utils::create_invoice_from_channelmanager_and_duration_since_epoch;
583 use std::collections::HashSet;
586 fn test_from_channelmanager() {
587 let chanmon_cfgs = create_chanmon_cfgs(2);
588 let node_cfgs = create_node_cfgs(2, &chanmon_cfgs);
589 let node_chanmgrs = create_node_chanmgrs(2, &node_cfgs, &[None, None]);
590 let nodes = create_network(2, &node_cfgs, &node_chanmgrs);
591 create_unannounced_chan_between_nodes_with_value(&nodes, 0, 1, 100000, 10001, channelmanager::provided_init_features(), channelmanager::provided_init_features());
592 let non_default_invoice_expiry_secs = 4200;
593 let invoice = create_invoice_from_channelmanager_and_duration_since_epoch(
594 &nodes[1].node, nodes[1].keys_manager, nodes[1].logger, Currency::BitcoinTestnet,
595 Some(10_000), "test".to_string(), Duration::from_secs(1234567),
596 non_default_invoice_expiry_secs).unwrap();
597 assert_eq!(invoice.amount_pico_btc(), Some(100_000));
598 assert_eq!(invoice.min_final_cltv_expiry(), MIN_FINAL_CLTV_EXPIRY as u64);
599 assert_eq!(invoice.description(), InvoiceDescription::Direct(&Description("test".to_string())));
600 assert_eq!(invoice.expiry_time(), Duration::from_secs(non_default_invoice_expiry_secs.into()));
602 // Invoice SCIDs should always use inbound SCID aliases over the real channel ID, if one is
604 let chan = &nodes[1].node.list_usable_channels()[0];
605 assert_eq!(invoice.route_hints().len(), 1);
606 assert_eq!(invoice.route_hints()[0].0.len(), 1);
607 assert_eq!(invoice.route_hints()[0].0[0].short_channel_id, chan.inbound_scid_alias.unwrap());
609 assert_eq!(invoice.route_hints()[0].0[0].htlc_minimum_msat, chan.inbound_htlc_minimum_msat);
610 assert_eq!(invoice.route_hints()[0].0[0].htlc_maximum_msat, chan.inbound_htlc_maximum_msat);
612 let payment_params = PaymentParameters::from_node_id(invoice.recover_payee_pub_key())
613 .with_features(invoice.features().unwrap().clone())
614 .with_route_hints(invoice.route_hints());
615 let route_params = RouteParameters {
617 final_value_msat: invoice.amount_milli_satoshis().unwrap(),
618 final_cltv_expiry_delta: invoice.min_final_cltv_expiry() as u32,
620 let first_hops = nodes[0].node.list_usable_channels();
621 let network_graph = &node_cfgs[0].network_graph;
622 let logger = test_utils::TestLogger::new();
623 let scorer = test_utils::TestScorer::with_penalty(0);
624 let random_seed_bytes = chanmon_cfgs[1].keys_manager.get_secure_random_bytes();
625 let route = find_route(
626 &nodes[0].node.get_our_node_id(), &route_params, &network_graph,
627 Some(&first_hops.iter().collect::<Vec<_>>()), &logger, &scorer, &random_seed_bytes
630 let payment_event = {
631 let mut payment_hash = PaymentHash([0; 32]);
632 payment_hash.0.copy_from_slice(&invoice.payment_hash().as_ref()[0..32]);
633 nodes[0].node.send_payment(&route, payment_hash, &Some(invoice.payment_secret().clone()), PaymentId(payment_hash.0)).unwrap();
634 let mut added_monitors = nodes[0].chain_monitor.added_monitors.lock().unwrap();
635 assert_eq!(added_monitors.len(), 1);
636 added_monitors.clear();
638 let mut events = nodes[0].node.get_and_clear_pending_msg_events();
639 assert_eq!(events.len(), 1);
640 SendEvent::from_event(events.remove(0))
643 nodes[1].node.handle_update_add_htlc(&nodes[0].node.get_our_node_id(), &payment_event.msgs[0]);
644 nodes[1].node.handle_commitment_signed(&nodes[0].node.get_our_node_id(), &payment_event.commitment_msg);
645 let mut added_monitors = nodes[1].chain_monitor.added_monitors.lock().unwrap();
646 assert_eq!(added_monitors.len(), 1);
647 added_monitors.clear();
648 let events = nodes[1].node.get_and_clear_pending_msg_events();
649 assert_eq!(events.len(), 2);
653 fn test_create_invoice_with_description_hash() {
654 let chanmon_cfgs = create_chanmon_cfgs(2);
655 let node_cfgs = create_node_cfgs(2, &chanmon_cfgs);
656 let node_chanmgrs = create_node_chanmgrs(2, &node_cfgs, &[None, None]);
657 let nodes = create_network(2, &node_cfgs, &node_chanmgrs);
658 let description_hash = crate::Sha256(Hash::hash("Testing description_hash".as_bytes()));
659 let invoice = crate::utils::create_invoice_from_channelmanager_with_description_hash_and_duration_since_epoch(
660 &nodes[1].node, nodes[1].keys_manager, nodes[1].logger, Currency::BitcoinTestnet,
661 Some(10_000), description_hash, Duration::from_secs(1234567), 3600
663 assert_eq!(invoice.amount_pico_btc(), Some(100_000));
664 assert_eq!(invoice.min_final_cltv_expiry(), MIN_FINAL_CLTV_EXPIRY as u64);
665 assert_eq!(invoice.description(), InvoiceDescription::Hash(&crate::Sha256(Sha256::hash("Testing description_hash".as_bytes()))));
669 fn test_hints_includes_single_channels_to_nodes() {
670 let chanmon_cfgs = create_chanmon_cfgs(3);
671 let node_cfgs = create_node_cfgs(3, &chanmon_cfgs);
672 let node_chanmgrs = create_node_chanmgrs(3, &node_cfgs, &[None, None, None]);
673 let nodes = create_network(3, &node_cfgs, &node_chanmgrs);
675 let chan_1_0 = create_unannounced_chan_between_nodes_with_value(&nodes, 1, 0, 100000, 10001, channelmanager::provided_init_features(), channelmanager::provided_init_features());
676 let chan_2_0 = create_unannounced_chan_between_nodes_with_value(&nodes, 2, 0, 100000, 10001, channelmanager::provided_init_features(), channelmanager::provided_init_features());
678 let mut scid_aliases = HashSet::new();
679 scid_aliases.insert(chan_1_0.0.short_channel_id_alias.unwrap());
680 scid_aliases.insert(chan_2_0.0.short_channel_id_alias.unwrap());
682 match_invoice_routes(Some(5000), &nodes[0], scid_aliases);
686 fn test_hints_has_only_highest_inbound_capacity_channel() {
687 let chanmon_cfgs = create_chanmon_cfgs(2);
688 let node_cfgs = create_node_cfgs(2, &chanmon_cfgs);
689 let node_chanmgrs = create_node_chanmgrs(2, &node_cfgs, &[None, None]);
690 let nodes = create_network(2, &node_cfgs, &node_chanmgrs);
691 let _chan_1_0_low_inbound_capacity = create_unannounced_chan_between_nodes_with_value(&nodes, 1, 0, 100_000, 0, channelmanager::provided_init_features(), channelmanager::provided_init_features());
692 let chan_1_0_high_inbound_capacity = create_unannounced_chan_between_nodes_with_value(&nodes, 1, 0, 10_000_000, 0, channelmanager::provided_init_features(), channelmanager::provided_init_features());
693 let _chan_1_0_medium_inbound_capacity = create_unannounced_chan_between_nodes_with_value(&nodes, 1, 0, 1_000_000, 0, channelmanager::provided_init_features(), channelmanager::provided_init_features());
695 let mut scid_aliases = HashSet::new();
696 scid_aliases.insert(chan_1_0_high_inbound_capacity.0.short_channel_id_alias.unwrap());
698 match_invoice_routes(Some(5000), &nodes[0], scid_aliases);
702 fn test_hints_has_only_online_channels() {
703 let chanmon_cfgs = create_chanmon_cfgs(4);
704 let node_cfgs = create_node_cfgs(4, &chanmon_cfgs);
705 let node_chanmgrs = create_node_chanmgrs(4, &node_cfgs, &[None, None, None, None]);
706 let nodes = create_network(4, &node_cfgs, &node_chanmgrs);
707 let chan_a = create_unannounced_chan_between_nodes_with_value(&nodes, 1, 0, 10_000_000, 0, channelmanager::provided_init_features(), channelmanager::provided_init_features());
708 let chan_b = create_unannounced_chan_between_nodes_with_value(&nodes, 2, 0, 10_000_000, 0, channelmanager::provided_init_features(), channelmanager::provided_init_features());
709 let _chan_c = create_unannounced_chan_between_nodes_with_value(&nodes, 3, 0, 1_000_000, 0, channelmanager::provided_init_features(), channelmanager::provided_init_features());
711 // With all peers connected we should get all hints that have sufficient value
712 let mut scid_aliases = HashSet::new();
713 scid_aliases.insert(chan_a.0.short_channel_id_alias.unwrap());
714 scid_aliases.insert(chan_b.0.short_channel_id_alias.unwrap());
716 match_invoice_routes(Some(1_000_000_000), &nodes[0], scid_aliases.clone());
718 // With only one sufficient-value peer connected we should only get its hint
719 scid_aliases.remove(&chan_b.0.short_channel_id_alias.unwrap());
720 nodes[0].node.peer_disconnected(&nodes[2].node.get_our_node_id(), false);
721 match_invoice_routes(Some(1_000_000_000), &nodes[0], scid_aliases.clone());
723 // If we don't have any sufficient-value peers connected we should get all hints with
724 // sufficient value, even though there is a connected insufficient-value peer.
725 scid_aliases.insert(chan_b.0.short_channel_id_alias.unwrap());
726 nodes[0].node.peer_disconnected(&nodes[1].node.get_our_node_id(), false);
727 match_invoice_routes(Some(1_000_000_000), &nodes[0], scid_aliases);
731 fn test_forwarding_info_not_assigned_channel_excluded_from_hints() {
732 let chanmon_cfgs = create_chanmon_cfgs(3);
733 let node_cfgs = create_node_cfgs(3, &chanmon_cfgs);
734 let node_chanmgrs = create_node_chanmgrs(3, &node_cfgs, &[None, None, None]);
735 let nodes = create_network(3, &node_cfgs, &node_chanmgrs);
736 let chan_1_0 = create_unannounced_chan_between_nodes_with_value(&nodes, 1, 0, 100000, 10001, channelmanager::provided_init_features(), channelmanager::provided_init_features());
738 // Create an unannonced channel between `nodes[2]` and `nodes[0]`, for which the
739 // `msgs::ChannelUpdate` is never handled for the node(s). As the `msgs::ChannelUpdate`
740 // is never handled, the `channel.counterparty.forwarding_info` is never assigned.
741 let mut private_chan_cfg = UserConfig::default();
742 private_chan_cfg.channel_handshake_config.announced_channel = false;
743 let temporary_channel_id = nodes[2].node.create_channel(nodes[0].node.get_our_node_id(), 1_000_000, 500_000_000, 42, Some(private_chan_cfg)).unwrap();
744 let open_channel = get_event_msg!(nodes[2], MessageSendEvent::SendOpenChannel, nodes[0].node.get_our_node_id());
745 nodes[0].node.handle_open_channel(&nodes[2].node.get_our_node_id(), channelmanager::provided_init_features(), &open_channel);
746 let accept_channel = get_event_msg!(nodes[0], MessageSendEvent::SendAcceptChannel, nodes[2].node.get_our_node_id());
747 nodes[2].node.handle_accept_channel(&nodes[0].node.get_our_node_id(), channelmanager::provided_init_features(), &accept_channel);
749 let tx = sign_funding_transaction(&nodes[2], &nodes[0], 1_000_000, temporary_channel_id);
751 let conf_height = core::cmp::max(nodes[2].best_block_info().1 + 1, nodes[0].best_block_info().1 + 1);
752 confirm_transaction_at(&nodes[2], &tx, conf_height);
753 connect_blocks(&nodes[2], CHAN_CONFIRM_DEPTH - 1);
754 confirm_transaction_at(&nodes[0], &tx, conf_height);
755 connect_blocks(&nodes[0], CHAN_CONFIRM_DEPTH - 1);
756 let as_channel_ready = get_event_msg!(nodes[2], MessageSendEvent::SendChannelReady, nodes[0].node.get_our_node_id());
757 nodes[2].node.handle_channel_ready(&nodes[0].node.get_our_node_id(), &get_event_msg!(nodes[0], MessageSendEvent::SendChannelReady, nodes[2].node.get_our_node_id()));
758 get_event_msg!(nodes[2], MessageSendEvent::SendChannelUpdate, nodes[0].node.get_our_node_id());
759 nodes[0].node.handle_channel_ready(&nodes[2].node.get_our_node_id(), &as_channel_ready);
760 get_event_msg!(nodes[0], MessageSendEvent::SendChannelUpdate, nodes[2].node.get_our_node_id());
761 expect_channel_ready_event(&nodes[0], &nodes[2].node.get_our_node_id());
762 expect_channel_ready_event(&nodes[2], &nodes[0].node.get_our_node_id());
764 // As `msgs::ChannelUpdate` was never handled for the participating node(s) of the second
765 // channel, the channel will never be assigned any `counterparty.forwarding_info`.
766 // Therefore only `chan_1_0` should be included in the hints.
767 let mut scid_aliases = HashSet::new();
768 scid_aliases.insert(chan_1_0.0.short_channel_id_alias.unwrap());
769 match_invoice_routes(Some(5000), &nodes[0], scid_aliases);
773 fn test_no_hints_if_a_mix_between_public_and_private_channel_exists() {
774 let chanmon_cfgs = create_chanmon_cfgs(3);
775 let node_cfgs = create_node_cfgs(3, &chanmon_cfgs);
776 let node_chanmgrs = create_node_chanmgrs(3, &node_cfgs, &[None, None, None]);
777 let nodes = create_network(3, &node_cfgs, &node_chanmgrs);
778 let _chan_1_0 = create_unannounced_chan_between_nodes_with_value(&nodes, 1, 0, 100000, 10001, channelmanager::provided_init_features(), channelmanager::provided_init_features());
780 let chan_2_0 = create_announced_chan_between_nodes_with_value(&nodes, 2, 0, 100000, 10001, channelmanager::provided_init_features(), channelmanager::provided_init_features());
781 nodes[2].node.handle_channel_update(&nodes[0].node.get_our_node_id(), &chan_2_0.1);
782 nodes[0].node.handle_channel_update(&nodes[2].node.get_our_node_id(), &chan_2_0.0);
784 // Ensure that the invoice doesn't include any route hints for any of `nodes[0]` channels,
785 // even though all channels between `nodes[1]` and `nodes[0]` are private, as there is a
786 // public channel between `nodes[2]` and `nodes[0]`
787 match_invoice_routes(Some(5000), &nodes[0], HashSet::new());
791 fn test_only_public_channels_includes_no_channels_in_hints() {
792 let chanmon_cfgs = create_chanmon_cfgs(3);
793 let node_cfgs = create_node_cfgs(3, &chanmon_cfgs);
794 let node_chanmgrs = create_node_chanmgrs(3, &node_cfgs, &[None, None, None]);
795 let nodes = create_network(3, &node_cfgs, &node_chanmgrs);
796 let chan_1_0 = create_announced_chan_between_nodes_with_value(&nodes, 1, 0, 100000, 10001, channelmanager::provided_init_features(), channelmanager::provided_init_features());
797 nodes[0].node.handle_channel_update(&nodes[1].node.get_our_node_id(), &chan_1_0.0);
798 nodes[1].node.handle_channel_update(&nodes[0].node.get_our_node_id(), &chan_1_0.1);
800 let chan_2_0 = create_announced_chan_between_nodes_with_value(&nodes, 2, 0, 100000, 10001, channelmanager::provided_init_features(), channelmanager::provided_init_features());
801 nodes[2].node.handle_channel_update(&nodes[0].node.get_our_node_id(), &chan_2_0.1);
802 nodes[0].node.handle_channel_update(&nodes[2].node.get_our_node_id(), &chan_2_0.0);
804 // As all of `nodes[0]` channels are public, no channels should be included in the hints
805 match_invoice_routes(Some(5000), &nodes[0], HashSet::new());
809 fn test_channels_with_lower_inbound_capacity_than_invoice_amt_hints_filtering() {
810 let chanmon_cfgs = create_chanmon_cfgs(3);
811 let node_cfgs = create_node_cfgs(3, &chanmon_cfgs);
812 let node_chanmgrs = create_node_chanmgrs(3, &node_cfgs, &[None, None, None]);
813 let nodes = create_network(3, &node_cfgs, &node_chanmgrs);
814 let chan_1_0 = create_unannounced_chan_between_nodes_with_value(&nodes, 1, 0, 100_000, 0, channelmanager::provided_init_features(), channelmanager::provided_init_features());
815 let chan_2_0 = create_unannounced_chan_between_nodes_with_value(&nodes, 2, 0, 1_000_000, 0, channelmanager::provided_init_features(), channelmanager::provided_init_features());
817 // As the invoice amt is 1 msat above chan_1_0's inbound capacity, it shouldn't be included
818 let mut scid_aliases_99_000_001_msat = HashSet::new();
819 scid_aliases_99_000_001_msat.insert(chan_2_0.0.short_channel_id_alias.unwrap());
821 match_invoice_routes(Some(99_000_001), &nodes[0], scid_aliases_99_000_001_msat);
823 // As the invoice amt is exactly at chan_1_0's inbound capacity, it should be included
824 let mut scid_aliases_99_000_000_msat = HashSet::new();
825 scid_aliases_99_000_000_msat.insert(chan_1_0.0.short_channel_id_alias.unwrap());
826 scid_aliases_99_000_000_msat.insert(chan_2_0.0.short_channel_id_alias.unwrap());
828 match_invoice_routes(Some(99_000_000), &nodes[0], scid_aliases_99_000_000_msat);
830 // As the invoice amt is above all channels' inbound capacity, they will still be included
831 let mut scid_aliases_2_000_000_000_msat = HashSet::new();
832 scid_aliases_2_000_000_000_msat.insert(chan_1_0.0.short_channel_id_alias.unwrap());
833 scid_aliases_2_000_000_000_msat.insert(chan_2_0.0.short_channel_id_alias.unwrap());
835 match_invoice_routes(Some(2_000_000_000), &nodes[0], scid_aliases_2_000_000_000_msat);
837 // An invoice with no specified amount should include all channels in the route hints.
838 let mut scid_aliases_no_specified_amount = HashSet::new();
839 scid_aliases_no_specified_amount.insert(chan_1_0.0.short_channel_id_alias.unwrap());
840 scid_aliases_no_specified_amount.insert(chan_2_0.0.short_channel_id_alias.unwrap());
842 match_invoice_routes(None, &nodes[0], scid_aliases_no_specified_amount);
845 fn match_invoice_routes<'a, 'b: 'a, 'c: 'b>(
846 invoice_amt: Option<u64>,
847 invoice_node: &Node<'a, 'b, 'c>,
848 mut chan_ids_to_match: HashSet<u64>
850 let invoice = create_invoice_from_channelmanager_and_duration_since_epoch(
851 &invoice_node.node, invoice_node.keys_manager, invoice_node.logger,
852 Currency::BitcoinTestnet, invoice_amt, "test".to_string(), Duration::from_secs(1234567),
854 let hints = invoice.private_routes();
857 let hint_short_chan_id = (hint.0).0[0].short_channel_id;
858 assert!(chan_ids_to_match.remove(&hint_short_chan_id));
860 assert!(chan_ids_to_match.is_empty(), "Unmatched short channel ids: {:?}", chan_ids_to_match);
864 #[cfg(feature = "std")]
865 fn test_multi_node_receive() {
866 do_test_multi_node_receive(true);
867 do_test_multi_node_receive(false);
870 #[cfg(feature = "std")]
871 fn do_test_multi_node_receive(user_generated_pmt_hash: bool) {
872 let mut chanmon_cfgs = create_chanmon_cfgs(3);
873 let seed_1 = [42 as u8; 32];
874 let seed_2 = [43 as u8; 32];
875 let cross_node_seed = [44 as u8; 32];
876 chanmon_cfgs[1].keys_manager.backing = PhantomKeysManager::new(&seed_1, 43, 44, &cross_node_seed);
877 chanmon_cfgs[2].keys_manager.backing = PhantomKeysManager::new(&seed_2, 43, 44, &cross_node_seed);
878 let node_cfgs = create_node_cfgs(3, &chanmon_cfgs);
879 let node_chanmgrs = create_node_chanmgrs(3, &node_cfgs, &[None, None, None]);
880 let nodes = create_network(3, &node_cfgs, &node_chanmgrs);
881 let chan_0_1 = create_announced_chan_between_nodes_with_value(&nodes, 0, 1, 100000, 10001, channelmanager::provided_init_features(), channelmanager::provided_init_features());
882 nodes[0].node.handle_channel_update(&nodes[1].node.get_our_node_id(), &chan_0_1.1);
883 nodes[1].node.handle_channel_update(&nodes[0].node.get_our_node_id(), &chan_0_1.0);
884 let chan_0_2 = create_announced_chan_between_nodes_with_value(&nodes, 0, 2, 100000, 10001, channelmanager::provided_init_features(), channelmanager::provided_init_features());
885 nodes[0].node.handle_channel_update(&nodes[2].node.get_our_node_id(), &chan_0_2.1);
886 nodes[2].node.handle_channel_update(&nodes[0].node.get_our_node_id(), &chan_0_2.0);
888 let payment_amt = 10_000;
889 let route_hints = vec![
890 nodes[1].node.get_phantom_route_hints(),
891 nodes[2].node.get_phantom_route_hints(),
894 let user_payment_preimage = PaymentPreimage([1; 32]);
895 let payment_hash = if user_generated_pmt_hash {
896 Some(PaymentHash(Sha256::hash(&user_payment_preimage.0[..]).into_inner()))
900 let non_default_invoice_expiry_secs = 4200;
903 crate::utils::create_phantom_invoice::<&test_utils::TestKeysInterface, &test_utils::TestLogger>(
904 Some(payment_amt), payment_hash, "test".to_string(), non_default_invoice_expiry_secs,
905 route_hints, &nodes[1].keys_manager, &nodes[1].logger, Currency::BitcoinTestnet
907 let (payment_hash, payment_secret) = (PaymentHash(invoice.payment_hash().into_inner()), *invoice.payment_secret());
908 let payment_preimage = if user_generated_pmt_hash {
909 user_payment_preimage
911 nodes[1].node.get_payment_preimage(payment_hash, payment_secret).unwrap()
914 assert_eq!(invoice.min_final_cltv_expiry(), MIN_FINAL_CLTV_EXPIRY as u64);
915 assert_eq!(invoice.description(), InvoiceDescription::Direct(&Description("test".to_string())));
916 assert_eq!(invoice.route_hints().len(), 2);
917 assert_eq!(invoice.expiry_time(), Duration::from_secs(non_default_invoice_expiry_secs.into()));
918 assert!(!invoice.features().unwrap().supports_basic_mpp());
920 let payment_params = PaymentParameters::from_node_id(invoice.recover_payee_pub_key())
921 .with_features(invoice.features().unwrap().clone())
922 .with_route_hints(invoice.route_hints());
923 let params = RouteParameters {
925 final_value_msat: invoice.amount_milli_satoshis().unwrap(),
926 final_cltv_expiry_delta: invoice.min_final_cltv_expiry() as u32,
928 let first_hops = nodes[0].node.list_usable_channels();
929 let network_graph = &node_cfgs[0].network_graph;
930 let logger = test_utils::TestLogger::new();
931 let scorer = test_utils::TestScorer::with_penalty(0);
932 let random_seed_bytes = chanmon_cfgs[1].keys_manager.get_secure_random_bytes();
933 let route = find_route(
934 &nodes[0].node.get_our_node_id(), ¶ms, &network_graph,
935 Some(&first_hops.iter().collect::<Vec<_>>()), &logger, &scorer, &random_seed_bytes
937 let (payment_event, fwd_idx) = {
938 let mut payment_hash = PaymentHash([0; 32]);
939 payment_hash.0.copy_from_slice(&invoice.payment_hash().as_ref()[0..32]);
940 nodes[0].node.send_payment(&route, payment_hash, &Some(invoice.payment_secret().clone()), PaymentId(payment_hash.0)).unwrap();
941 let mut added_monitors = nodes[0].chain_monitor.added_monitors.lock().unwrap();
942 assert_eq!(added_monitors.len(), 1);
943 added_monitors.clear();
945 let mut events = nodes[0].node.get_and_clear_pending_msg_events();
946 assert_eq!(events.len(), 1);
947 let fwd_idx = match events[0] {
948 MessageSendEvent::UpdateHTLCs { node_id, .. } => {
949 if node_id == nodes[1].node.get_our_node_id() {
953 _ => panic!("Unexpected event")
955 (SendEvent::from_event(events.remove(0)), fwd_idx)
957 nodes[fwd_idx].node.handle_update_add_htlc(&nodes[0].node.get_our_node_id(), &payment_event.msgs[0]);
958 commitment_signed_dance!(nodes[fwd_idx], nodes[0], &payment_event.commitment_msg, false, true);
960 // Note that we have to "forward pending HTLCs" twice before we see the PaymentClaimable as
961 // this "emulates" the payment taking two hops, providing some privacy to make phantom node
962 // payments "look real" by taking more time.
963 expect_pending_htlcs_forwardable_ignore!(nodes[fwd_idx]);
964 nodes[fwd_idx].node.process_pending_htlc_forwards();
965 expect_pending_htlcs_forwardable_ignore!(nodes[fwd_idx]);
966 nodes[fwd_idx].node.process_pending_htlc_forwards();
968 let payment_preimage_opt = if user_generated_pmt_hash { None } else { Some(payment_preimage) };
969 expect_payment_claimable!(&nodes[fwd_idx], payment_hash, payment_secret, payment_amt, payment_preimage_opt, route.paths[0].last().unwrap().pubkey);
970 do_claim_payment_along_route(&nodes[0], &vec!(&vec!(&nodes[fwd_idx])[..]), false, payment_preimage);
971 let events = nodes[0].node.get_and_clear_pending_events();
972 assert_eq!(events.len(), 2);
974 Event::PaymentSent { payment_preimage: ref ev_preimage, payment_hash: ref ev_hash, ref fee_paid_msat, .. } => {
975 assert_eq!(payment_preimage, *ev_preimage);
976 assert_eq!(payment_hash, *ev_hash);
977 assert_eq!(fee_paid_msat, &Some(0));
979 _ => panic!("Unexpected event")
982 Event::PaymentPathSuccessful { payment_hash: hash, .. } => {
983 assert_eq!(hash, Some(payment_hash));
985 _ => panic!("Unexpected event")
990 #[cfg(feature = "std")]
991 fn test_multi_node_hints_has_htlc_min_max_values() {
992 let mut chanmon_cfgs = create_chanmon_cfgs(3);
993 let seed_1 = [42 as u8; 32];
994 let seed_2 = [43 as u8; 32];
995 let cross_node_seed = [44 as u8; 32];
996 chanmon_cfgs[1].keys_manager.backing = PhantomKeysManager::new(&seed_1, 43, 44, &cross_node_seed);
997 chanmon_cfgs[2].keys_manager.backing = PhantomKeysManager::new(&seed_2, 43, 44, &cross_node_seed);
998 let node_cfgs = create_node_cfgs(3, &chanmon_cfgs);
999 let node_chanmgrs = create_node_chanmgrs(3, &node_cfgs, &[None, None, None]);
1000 let nodes = create_network(3, &node_cfgs, &node_chanmgrs);
1002 create_unannounced_chan_between_nodes_with_value(&nodes, 0, 1, 100000, 10001, channelmanager::provided_init_features(), channelmanager::provided_init_features());
1003 create_unannounced_chan_between_nodes_with_value(&nodes, 0, 2, 100000, 10001, channelmanager::provided_init_features(), channelmanager::provided_init_features());
1005 let payment_amt = 20_000;
1006 let (payment_hash, _payment_secret) = nodes[1].node.create_inbound_payment(Some(payment_amt), 3600).unwrap();
1007 let route_hints = vec![
1008 nodes[1].node.get_phantom_route_hints(),
1009 nodes[2].node.get_phantom_route_hints(),
1012 let invoice = crate::utils::create_phantom_invoice::<&test_utils::TestKeysInterface, &test_utils::TestLogger>(Some(payment_amt), Some(payment_hash), "test".to_string(), 3600, route_hints, &nodes[1].keys_manager, &nodes[1].logger, Currency::BitcoinTestnet).unwrap();
1014 let chan_0_1 = &nodes[1].node.list_usable_channels()[0];
1015 assert_eq!(invoice.route_hints()[0].0[0].htlc_minimum_msat, chan_0_1.inbound_htlc_minimum_msat);
1016 assert_eq!(invoice.route_hints()[0].0[0].htlc_maximum_msat, chan_0_1.inbound_htlc_maximum_msat);
1018 let chan_0_2 = &nodes[2].node.list_usable_channels()[0];
1019 assert_eq!(invoice.route_hints()[1].0[0].htlc_minimum_msat, chan_0_2.inbound_htlc_minimum_msat);
1020 assert_eq!(invoice.route_hints()[1].0[0].htlc_maximum_msat, chan_0_2.inbound_htlc_maximum_msat);
1024 #[cfg(feature = "std")]
1025 fn create_phantom_invoice_with_description_hash() {
1026 let chanmon_cfgs = create_chanmon_cfgs(3);
1027 let node_cfgs = create_node_cfgs(3, &chanmon_cfgs);
1028 let node_chanmgrs = create_node_chanmgrs(3, &node_cfgs, &[None, None, None]);
1029 let nodes = create_network(3, &node_cfgs, &node_chanmgrs);
1031 let payment_amt = 20_000;
1032 let route_hints = vec![
1033 nodes[1].node.get_phantom_route_hints(),
1034 nodes[2].node.get_phantom_route_hints(),
1037 let description_hash = crate::Sha256(Hash::hash("Description hash phantom invoice".as_bytes()));
1038 let non_default_invoice_expiry_secs = 4200;
1039 let invoice = crate::utils::create_phantom_invoice_with_description_hash::<
1040 &test_utils::TestKeysInterface, &test_utils::TestLogger,
1042 Some(payment_amt), None, non_default_invoice_expiry_secs, description_hash,
1043 route_hints, &nodes[1].keys_manager, &nodes[1].logger, Currency::BitcoinTestnet
1046 assert_eq!(invoice.amount_pico_btc(), Some(200_000));
1047 assert_eq!(invoice.min_final_cltv_expiry(), MIN_FINAL_CLTV_EXPIRY as u64);
1048 assert_eq!(invoice.expiry_time(), Duration::from_secs(non_default_invoice_expiry_secs.into()));
1049 assert_eq!(invoice.description(), InvoiceDescription::Hash(&crate::Sha256(Sha256::hash("Description hash phantom invoice".as_bytes()))));
1053 #[cfg(feature = "std")]
1054 fn test_multi_node_hints_includes_single_channels_to_participating_nodes() {
1055 let mut chanmon_cfgs = create_chanmon_cfgs(3);
1056 let seed_1 = [42 as u8; 32];
1057 let seed_2 = [43 as u8; 32];
1058 let cross_node_seed = [44 as u8; 32];
1059 chanmon_cfgs[1].keys_manager.backing = PhantomKeysManager::new(&seed_1, 43, 44, &cross_node_seed);
1060 chanmon_cfgs[2].keys_manager.backing = PhantomKeysManager::new(&seed_2, 43, 44, &cross_node_seed);
1061 let node_cfgs = create_node_cfgs(3, &chanmon_cfgs);
1062 let node_chanmgrs = create_node_chanmgrs(3, &node_cfgs, &[None, None, None]);
1063 let nodes = create_network(3, &node_cfgs, &node_chanmgrs);
1065 let chan_0_1 = create_unannounced_chan_between_nodes_with_value(&nodes, 0, 1, 100000, 10001, channelmanager::provided_init_features(), channelmanager::provided_init_features());
1066 let chan_0_2 = create_unannounced_chan_between_nodes_with_value(&nodes, 0, 2, 100000, 10001, channelmanager::provided_init_features(), channelmanager::provided_init_features());
1068 let mut scid_aliases = HashSet::new();
1069 scid_aliases.insert(chan_0_1.0.short_channel_id_alias.unwrap());
1070 scid_aliases.insert(chan_0_2.0.short_channel_id_alias.unwrap());
1072 match_multi_node_invoice_routes(
1075 vec![&nodes[1], &nodes[2],],
1082 #[cfg(feature = "std")]
1083 fn test_multi_node_hints_includes_one_channel_of_each_counterparty_nodes_per_participating_node() {
1084 let mut chanmon_cfgs = create_chanmon_cfgs(4);
1085 let seed_1 = [42 as u8; 32];
1086 let seed_2 = [43 as u8; 32];
1087 let cross_node_seed = [44 as u8; 32];
1088 chanmon_cfgs[2].keys_manager.backing = PhantomKeysManager::new(&seed_1, 43, 44, &cross_node_seed);
1089 chanmon_cfgs[3].keys_manager.backing = PhantomKeysManager::new(&seed_2, 43, 44, &cross_node_seed);
1090 let node_cfgs = create_node_cfgs(4, &chanmon_cfgs);
1091 let node_chanmgrs = create_node_chanmgrs(4, &node_cfgs, &[None, None, None, None]);
1092 let nodes = create_network(4, &node_cfgs, &node_chanmgrs);
1094 let chan_0_2 = create_unannounced_chan_between_nodes_with_value(&nodes, 0, 2, 100000, 10001, channelmanager::provided_init_features(), channelmanager::provided_init_features());
1095 let chan_0_3 = create_unannounced_chan_between_nodes_with_value(&nodes, 0, 3, 1000000, 10001, channelmanager::provided_init_features(), channelmanager::provided_init_features());
1096 let chan_1_3 = create_unannounced_chan_between_nodes_with_value(&nodes, 1, 3, 3_000_000, 10005, channelmanager::provided_init_features(), channelmanager::provided_init_features());
1098 let mut scid_aliases = HashSet::new();
1099 scid_aliases.insert(chan_0_2.0.short_channel_id_alias.unwrap());
1100 scid_aliases.insert(chan_0_3.0.short_channel_id_alias.unwrap());
1101 scid_aliases.insert(chan_1_3.0.short_channel_id_alias.unwrap());
1103 match_multi_node_invoice_routes(
1106 vec![&nodes[2], &nodes[3],],
1113 #[cfg(feature = "std")]
1114 fn test_multi_node_forwarding_info_not_assigned_channel_excluded_from_hints() {
1115 let mut chanmon_cfgs = create_chanmon_cfgs(4);
1116 let seed_1 = [42 as u8; 32];
1117 let seed_2 = [43 as u8; 32];
1118 let cross_node_seed = [44 as u8; 32];
1119 chanmon_cfgs[2].keys_manager.backing = PhantomKeysManager::new(&seed_1, 43, 44, &cross_node_seed);
1120 chanmon_cfgs[3].keys_manager.backing = PhantomKeysManager::new(&seed_2, 43, 44, &cross_node_seed);
1121 let node_cfgs = create_node_cfgs(4, &chanmon_cfgs);
1122 let node_chanmgrs = create_node_chanmgrs(4, &node_cfgs, &[None, None, None, None]);
1123 let nodes = create_network(4, &node_cfgs, &node_chanmgrs);
1125 let chan_0_2 = create_unannounced_chan_between_nodes_with_value(&nodes, 0, 2, 100000, 10001, channelmanager::provided_init_features(), channelmanager::provided_init_features());
1126 let chan_0_3 = create_unannounced_chan_between_nodes_with_value(&nodes, 0, 3, 1000000, 10001, channelmanager::provided_init_features(), channelmanager::provided_init_features());
1128 // Create an unannonced channel between `nodes[1]` and `nodes[3]`, for which the
1129 // `msgs::ChannelUpdate` is never handled for the node(s). As the `msgs::ChannelUpdate`
1130 // is never handled, the `channel.counterparty.forwarding_info` is never assigned.
1131 let mut private_chan_cfg = UserConfig::default();
1132 private_chan_cfg.channel_handshake_config.announced_channel = false;
1133 let temporary_channel_id = nodes[1].node.create_channel(nodes[3].node.get_our_node_id(), 1_000_000, 500_000_000, 42, Some(private_chan_cfg)).unwrap();
1134 let open_channel = get_event_msg!(nodes[1], MessageSendEvent::SendOpenChannel, nodes[3].node.get_our_node_id());
1135 nodes[3].node.handle_open_channel(&nodes[1].node.get_our_node_id(), channelmanager::provided_init_features(), &open_channel);
1136 let accept_channel = get_event_msg!(nodes[3], MessageSendEvent::SendAcceptChannel, nodes[1].node.get_our_node_id());
1137 nodes[1].node.handle_accept_channel(&nodes[3].node.get_our_node_id(), channelmanager::provided_init_features(), &accept_channel);
1139 let tx = sign_funding_transaction(&nodes[1], &nodes[3], 1_000_000, temporary_channel_id);
1141 let conf_height = core::cmp::max(nodes[1].best_block_info().1 + 1, nodes[3].best_block_info().1 + 1);
1142 confirm_transaction_at(&nodes[1], &tx, conf_height);
1143 connect_blocks(&nodes[1], CHAN_CONFIRM_DEPTH - 1);
1144 confirm_transaction_at(&nodes[3], &tx, conf_height);
1145 connect_blocks(&nodes[3], CHAN_CONFIRM_DEPTH - 1);
1146 let as_channel_ready = get_event_msg!(nodes[1], MessageSendEvent::SendChannelReady, nodes[3].node.get_our_node_id());
1147 nodes[1].node.handle_channel_ready(&nodes[3].node.get_our_node_id(), &get_event_msg!(nodes[3], MessageSendEvent::SendChannelReady, nodes[1].node.get_our_node_id()));
1148 get_event_msg!(nodes[1], MessageSendEvent::SendChannelUpdate, nodes[3].node.get_our_node_id());
1149 nodes[3].node.handle_channel_ready(&nodes[1].node.get_our_node_id(), &as_channel_ready);
1150 get_event_msg!(nodes[3], MessageSendEvent::SendChannelUpdate, nodes[1].node.get_our_node_id());
1151 expect_channel_ready_event(&nodes[1], &nodes[3].node.get_our_node_id());
1152 expect_channel_ready_event(&nodes[3], &nodes[1].node.get_our_node_id());
1154 // As `msgs::ChannelUpdate` was never handled for the participating node(s) of the third
1155 // channel, the channel will never be assigned any `counterparty.forwarding_info`.
1156 // Therefore only `chan_0_3` should be included in the hints for `nodes[3]`.
1157 let mut scid_aliases = HashSet::new();
1158 scid_aliases.insert(chan_0_2.0.short_channel_id_alias.unwrap());
1159 scid_aliases.insert(chan_0_3.0.short_channel_id_alias.unwrap());
1161 match_multi_node_invoice_routes(
1164 vec![&nodes[2], &nodes[3],],
1171 #[cfg(feature = "std")]
1172 fn test_multi_node_with_only_public_channels_hints_includes_only_phantom_route() {
1173 let mut chanmon_cfgs = create_chanmon_cfgs(3);
1174 let seed_1 = [42 as u8; 32];
1175 let seed_2 = [43 as u8; 32];
1176 let cross_node_seed = [44 as u8; 32];
1177 chanmon_cfgs[1].keys_manager.backing = PhantomKeysManager::new(&seed_1, 43, 44, &cross_node_seed);
1178 chanmon_cfgs[2].keys_manager.backing = PhantomKeysManager::new(&seed_2, 43, 44, &cross_node_seed);
1179 let node_cfgs = create_node_cfgs(3, &chanmon_cfgs);
1180 let node_chanmgrs = create_node_chanmgrs(3, &node_cfgs, &[None, None, None]);
1181 let nodes = create_network(3, &node_cfgs, &node_chanmgrs);
1183 let chan_0_1 = create_unannounced_chan_between_nodes_with_value(&nodes, 0, 1, 100000, 10001, channelmanager::provided_init_features(), channelmanager::provided_init_features());
1185 let chan_2_0 = create_announced_chan_between_nodes_with_value(&nodes, 2, 0, 100000, 10001, channelmanager::provided_init_features(), channelmanager::provided_init_features());
1186 nodes[2].node.handle_channel_update(&nodes[0].node.get_our_node_id(), &chan_2_0.1);
1187 nodes[0].node.handle_channel_update(&nodes[2].node.get_our_node_id(), &chan_2_0.0);
1189 // Hints should include `chan_0_1` from as `nodes[1]` only have private channels, but not
1190 // `chan_0_2` as `nodes[2]` only has public channels.
1191 let mut scid_aliases = HashSet::new();
1192 scid_aliases.insert(chan_0_1.0.short_channel_id_alias.unwrap());
1194 match_multi_node_invoice_routes(
1197 vec![&nodes[1], &nodes[2],],
1204 #[cfg(feature = "std")]
1205 fn test_multi_node_with_mixed_public_and_private_channel_hints_includes_only_phantom_route() {
1206 let mut chanmon_cfgs = create_chanmon_cfgs(4);
1207 let seed_1 = [42 as u8; 32];
1208 let seed_2 = [43 as u8; 32];
1209 let cross_node_seed = [44 as u8; 32];
1210 chanmon_cfgs[1].keys_manager.backing = PhantomKeysManager::new(&seed_1, 43, 44, &cross_node_seed);
1211 chanmon_cfgs[2].keys_manager.backing = PhantomKeysManager::new(&seed_2, 43, 44, &cross_node_seed);
1212 let node_cfgs = create_node_cfgs(4, &chanmon_cfgs);
1213 let node_chanmgrs = create_node_chanmgrs(4, &node_cfgs, &[None, None, None, None]);
1214 let nodes = create_network(4, &node_cfgs, &node_chanmgrs);
1216 let chan_0_2 = create_announced_chan_between_nodes_with_value(&nodes, 0, 2, 100000, 10001, channelmanager::provided_init_features(), channelmanager::provided_init_features());
1217 nodes[0].node.handle_channel_update(&nodes[2].node.get_our_node_id(), &chan_0_2.1);
1218 nodes[2].node.handle_channel_update(&nodes[0].node.get_our_node_id(), &chan_0_2.0);
1219 let _chan_1_2 = create_unannounced_chan_between_nodes_with_value(&nodes, 1, 2, 100000, 10001, channelmanager::provided_init_features(), channelmanager::provided_init_features());
1221 let chan_0_3 = create_unannounced_chan_between_nodes_with_value(&nodes, 0, 3, 100000, 10001, channelmanager::provided_init_features(), channelmanager::provided_init_features());
1223 // Hints should include `chan_0_3` from as `nodes[3]` only have private channels, and no
1224 // channels for `nodes[2]` as it contains a mix of public and private channels.
1225 let mut scid_aliases = HashSet::new();
1226 scid_aliases.insert(chan_0_3.0.short_channel_id_alias.unwrap());
1228 match_multi_node_invoice_routes(
1231 vec![&nodes[2], &nodes[3],],
1238 #[cfg(feature = "std")]
1239 fn test_multi_node_hints_has_only_highest_inbound_capacity_channel() {
1240 let mut chanmon_cfgs = create_chanmon_cfgs(3);
1241 let seed_1 = [42 as u8; 32];
1242 let seed_2 = [43 as u8; 32];
1243 let cross_node_seed = [44 as u8; 32];
1244 chanmon_cfgs[1].keys_manager.backing = PhantomKeysManager::new(&seed_1, 43, 44, &cross_node_seed);
1245 chanmon_cfgs[2].keys_manager.backing = PhantomKeysManager::new(&seed_2, 43, 44, &cross_node_seed);
1246 let node_cfgs = create_node_cfgs(3, &chanmon_cfgs);
1247 let node_chanmgrs = create_node_chanmgrs(3, &node_cfgs, &[None, None, None]);
1248 let nodes = create_network(3, &node_cfgs, &node_chanmgrs);
1250 let _chan_0_1_low_inbound_capacity = create_unannounced_chan_between_nodes_with_value(&nodes, 0, 1, 100_000, 0, channelmanager::provided_init_features(), channelmanager::provided_init_features());
1251 let chan_0_1_high_inbound_capacity = create_unannounced_chan_between_nodes_with_value(&nodes, 0, 1, 10_000_000, 0, channelmanager::provided_init_features(), channelmanager::provided_init_features());
1252 let _chan_0_1_medium_inbound_capacity = create_unannounced_chan_between_nodes_with_value(&nodes, 0, 1, 1_000_000, 0, channelmanager::provided_init_features(), channelmanager::provided_init_features());
1253 let chan_0_2 = create_unannounced_chan_between_nodes_with_value(&nodes, 0, 2, 100000, 10001, channelmanager::provided_init_features(), channelmanager::provided_init_features());
1255 let mut scid_aliases = HashSet::new();
1256 scid_aliases.insert(chan_0_1_high_inbound_capacity.0.short_channel_id_alias.unwrap());
1257 scid_aliases.insert(chan_0_2.0.short_channel_id_alias.unwrap());
1259 match_multi_node_invoice_routes(
1262 vec![&nodes[1], &nodes[2],],
1269 #[cfg(feature = "std")]
1270 fn test_multi_node_channels_inbound_capacity_lower_than_invoice_amt_filtering() {
1271 let mut chanmon_cfgs = create_chanmon_cfgs(4);
1272 let seed_1 = [42 as u8; 32];
1273 let seed_2 = [43 as u8; 32];
1274 let cross_node_seed = [44 as u8; 32];
1275 chanmon_cfgs[1].keys_manager.backing = PhantomKeysManager::new(&seed_1, 43, 44, &cross_node_seed);
1276 chanmon_cfgs[2].keys_manager.backing = PhantomKeysManager::new(&seed_2, 43, 44, &cross_node_seed);
1277 let node_cfgs = create_node_cfgs(4, &chanmon_cfgs);
1278 let node_chanmgrs = create_node_chanmgrs(4, &node_cfgs, &[None, None, None, None]);
1279 let nodes = create_network(4, &node_cfgs, &node_chanmgrs);
1281 let chan_0_2 = create_unannounced_chan_between_nodes_with_value(&nodes, 0, 2, 1_000_000, 0, channelmanager::provided_init_features(), channelmanager::provided_init_features());
1282 let chan_0_3 = create_unannounced_chan_between_nodes_with_value(&nodes, 0, 3, 100_000, 0, channelmanager::provided_init_features(), channelmanager::provided_init_features());
1283 let chan_1_3 = create_unannounced_chan_between_nodes_with_value(&nodes, 1, 3, 200_000, 0, channelmanager::provided_init_features(), channelmanager::provided_init_features());
1285 // Since the invoice 1 msat above chan_0_3's inbound capacity, it should be filtered out.
1286 let mut scid_aliases_99_000_001_msat = HashSet::new();
1287 scid_aliases_99_000_001_msat.insert(chan_0_2.0.short_channel_id_alias.unwrap());
1288 scid_aliases_99_000_001_msat.insert(chan_1_3.0.short_channel_id_alias.unwrap());
1290 match_multi_node_invoice_routes(
1293 vec![&nodes[2], &nodes[3],],
1294 scid_aliases_99_000_001_msat,
1298 // Since the invoice is exactly at chan_0_3's inbound capacity, it should be included.
1299 let mut scid_aliases_99_000_000_msat = HashSet::new();
1300 scid_aliases_99_000_000_msat.insert(chan_0_2.0.short_channel_id_alias.unwrap());
1301 scid_aliases_99_000_000_msat.insert(chan_0_3.0.short_channel_id_alias.unwrap());
1302 scid_aliases_99_000_000_msat.insert(chan_1_3.0.short_channel_id_alias.unwrap());
1304 match_multi_node_invoice_routes(
1307 vec![&nodes[2], &nodes[3],],
1308 scid_aliases_99_000_000_msat,
1312 // Since the invoice is above all of `nodes[2]` channels' inbound capacity, all of
1313 // `nodes[2]` them should be included.
1314 let mut scid_aliases_300_000_000_msat = HashSet::new();
1315 scid_aliases_300_000_000_msat.insert(chan_0_2.0.short_channel_id_alias.unwrap());
1316 scid_aliases_300_000_000_msat.insert(chan_0_3.0.short_channel_id_alias.unwrap());
1317 scid_aliases_300_000_000_msat.insert(chan_1_3.0.short_channel_id_alias.unwrap());
1319 match_multi_node_invoice_routes(
1322 vec![&nodes[2], &nodes[3],],
1323 scid_aliases_300_000_000_msat,
1327 // Since the no specified amount, all channels should included.
1328 let mut scid_aliases_no_specified_amount = HashSet::new();
1329 scid_aliases_no_specified_amount.insert(chan_0_2.0.short_channel_id_alias.unwrap());
1330 scid_aliases_no_specified_amount.insert(chan_0_3.0.short_channel_id_alias.unwrap());
1331 scid_aliases_no_specified_amount.insert(chan_1_3.0.short_channel_id_alias.unwrap());
1333 match_multi_node_invoice_routes(
1336 vec![&nodes[2], &nodes[3],],
1337 scid_aliases_no_specified_amount,
1342 #[cfg(feature = "std")]
1343 fn match_multi_node_invoice_routes<'a, 'b: 'a, 'c: 'b>(
1344 invoice_amt: Option<u64>,
1345 invoice_node: &Node<'a, 'b, 'c>,
1346 network_multi_nodes: Vec<&Node<'a, 'b, 'c>>,
1347 mut chan_ids_to_match: HashSet<u64>,
1348 nodes_contains_public_channels: bool
1350 let phantom_route_hints = network_multi_nodes.iter()
1351 .map(|node| node.node.get_phantom_route_hints())
1352 .collect::<Vec<PhantomRouteHints>>();
1353 let phantom_scids = phantom_route_hints.iter()
1354 .map(|route_hint| route_hint.phantom_scid)
1355 .collect::<HashSet<u64>>();
1357 let invoice = crate::utils::create_phantom_invoice::<&test_utils::TestKeysInterface, &test_utils::TestLogger>(invoice_amt, None, "test".to_string(), 3600, phantom_route_hints, &invoice_node.keys_manager, &invoice_node.logger, Currency::BitcoinTestnet).unwrap();
1359 let invoice_hints = invoice.private_routes();
1361 for hint in invoice_hints {
1362 let hints = &(hint.0).0;
1365 assert!(nodes_contains_public_channels);
1366 let phantom_scid = hints[0].short_channel_id;
1367 assert!(phantom_scids.contains(&phantom_scid));
1370 let hint_short_chan_id = hints[0].short_channel_id;
1371 assert!(chan_ids_to_match.remove(&hint_short_chan_id));
1372 let phantom_scid = hints[1].short_channel_id;
1373 assert!(phantom_scids.contains(&phantom_scid));
1375 _ => panic!("Incorrect hint length generated")
1378 assert!(chan_ids_to_match.is_empty(), "Unmatched short channel ids: {:?}", chan_ids_to_match);