Merge pull request #1861 from TheBlueMatt/2022-11-tx-connection-idempotency
[rust-lightning] / lightning-invoice / src / utils.rs
1 //! Convenient utilities to create an invoice.
2
3 use crate::{CreationError, Currency, Invoice, InvoiceBuilder, SignOrCreationError};
4 use crate::payment::{Payer, ScoringRouter};
5
6 use crate::{prelude::*, Description, InvoiceDescription, Sha256};
7 use bech32::ToBase32;
8 use bitcoin_hashes::{Hash, sha256};
9 use lightning::chain;
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::ln::msgs::LightningError;
18 use lightning::routing::gossip::{NetworkGraph, NodeId, RoutingFees};
19 use lightning::routing::router::{InFlightHtlcs, Route, RouteHint, RouteHintHop, RouteParameters, find_route, RouteHop, Router};
20 use lightning::routing::scoring::{ChannelUsage, LockableScore, Score};
21 use lightning::util::logger::Logger;
22 use secp256k1::PublicKey;
23 use core::ops::Deref;
24 use core::time::Duration;
25 use crate::sync::Mutex;
26
27 #[cfg(feature = "std")]
28 /// Utility to create an invoice that can be paid to one of multiple nodes, or a "phantom invoice."
29 /// See [`PhantomKeysManager`] for more information on phantom node payments.
30 ///
31 /// `phantom_route_hints` parameter:
32 /// * Contains channel info for all nodes participating in the phantom invoice
33 /// * Entries are retrieved from a call to [`ChannelManager::get_phantom_route_hints`] on each
34 ///   participating node
35 /// * It is fine to cache `phantom_route_hints` and reuse it across invoices, as long as the data is
36 ///   updated when a channel becomes disabled or closes
37 /// * Note that if too many channels are included in [`PhantomRouteHints::channels`], the invoice
38 ///   may be too long for QR code scanning. To fix this, `PhantomRouteHints::channels` may be pared
39 ///   down
40 ///
41 /// `payment_hash` can be specified if you have a specific need for a custom payment hash (see the difference
42 /// between [`ChannelManager::create_inbound_payment`] and [`ChannelManager::create_inbound_payment_for_hash`]).
43 /// If `None` is provided for `payment_hash`, then one will be created.
44 ///
45 /// `invoice_expiry_delta_secs` describes the number of seconds that the invoice is valid for
46 /// in excess of the current time.
47 ///
48 /// Note that the provided `keys_manager`'s `KeysInterface` implementation must support phantom
49 /// invoices in its `sign_invoice` implementation ([`PhantomKeysManager`] satisfies this
50 /// requirement).
51 ///
52 /// [`PhantomKeysManager`]: lightning::chain::keysinterface::PhantomKeysManager
53 /// [`ChannelManager::get_phantom_route_hints`]: lightning::ln::channelmanager::ChannelManager::get_phantom_route_hints
54 /// [`ChannelManager::create_inbound_payment`]: lightning::ln::channelmanager::ChannelManager::create_inbound_payment
55 /// [`ChannelManager::create_inbound_payment_for_hash`]: lightning::ln::channelmanager::ChannelManager::create_inbound_payment_for_hash
56 /// [`PhantomRouteHints::channels`]: lightning::ln::channelmanager::PhantomRouteHints::channels
57 pub fn create_phantom_invoice<K: Deref, L: Deref>(
58         amt_msat: Option<u64>, payment_hash: Option<PaymentHash>, description: String,
59         invoice_expiry_delta_secs: u32, phantom_route_hints: Vec<PhantomRouteHints>, keys_manager: K,
60         logger: L, network: Currency,
61 ) -> Result<Invoice, SignOrCreationError<()>>
62 where
63         K::Target: KeysInterface,
64         L::Target: Logger,
65 {
66         let description = Description::new(description).map_err(SignOrCreationError::CreationError)?;
67         let description = InvoiceDescription::Direct(&description,);
68         _create_phantom_invoice::<K, L>(
69                 amt_msat, payment_hash, description, invoice_expiry_delta_secs, phantom_route_hints,
70                 keys_manager, logger, network,
71         )
72 }
73
74 #[cfg(feature = "std")]
75 /// Utility to create an invoice that can be paid to one of multiple nodes, or a "phantom invoice."
76 /// See [`PhantomKeysManager`] for more information on phantom node payments.
77 ///
78 /// `phantom_route_hints` parameter:
79 /// * Contains channel info for all nodes participating in the phantom invoice
80 /// * Entries are retrieved from a call to [`ChannelManager::get_phantom_route_hints`] on each
81 ///   participating node
82 /// * It is fine to cache `phantom_route_hints` and reuse it across invoices, as long as the data is
83 ///   updated when a channel becomes disabled or closes
84 /// * Note that if too many channels are included in [`PhantomRouteHints::channels`], the invoice
85 ///   may be too long for QR code scanning. To fix this, `PhantomRouteHints::channels` may be pared
86 ///   down
87 ///
88 /// `description_hash` is a SHA-256 hash of the description text
89 ///
90 /// `payment_hash` can be specified if you have a specific need for a custom payment hash (see the difference
91 /// between [`ChannelManager::create_inbound_payment`] and [`ChannelManager::create_inbound_payment_for_hash`]).
92 /// If `None` is provided for `payment_hash`, then one will be created.
93 ///
94 /// `invoice_expiry_delta_secs` describes the number of seconds that the invoice is valid for
95 /// in excess of the current time.
96 ///
97 /// Note that the provided `keys_manager`'s `KeysInterface` implementation must support phantom
98 /// invoices in its `sign_invoice` implementation ([`PhantomKeysManager`] satisfies this
99 /// requirement).
100 ///
101 /// [`PhantomKeysManager`]: lightning::chain::keysinterface::PhantomKeysManager
102 /// [`ChannelManager::get_phantom_route_hints`]: lightning::ln::channelmanager::ChannelManager::get_phantom_route_hints
103 /// [`ChannelManager::create_inbound_payment`]: lightning::ln::channelmanager::ChannelManager::create_inbound_payment
104 /// [`ChannelManager::create_inbound_payment_for_hash`]: lightning::ln::channelmanager::ChannelManager::create_inbound_payment_for_hash
105 /// [`PhantomRouteHints::channels`]: lightning::ln::channelmanager::PhantomRouteHints::channels
106 pub fn create_phantom_invoice_with_description_hash<K: Deref, L: Deref>(
107         amt_msat: Option<u64>, payment_hash: Option<PaymentHash>, invoice_expiry_delta_secs: u32,
108         description_hash: Sha256, phantom_route_hints: Vec<PhantomRouteHints>, keys_manager: K,
109         logger: L, network: Currency
110 ) -> Result<Invoice, SignOrCreationError<()>>
111 where
112         K::Target: KeysInterface,
113         L::Target: Logger,
114 {
115         _create_phantom_invoice::<K, L>(
116                 amt_msat, payment_hash, InvoiceDescription::Hash(&description_hash),
117                 invoice_expiry_delta_secs, phantom_route_hints, keys_manager, logger, network,
118         )
119 }
120
121 #[cfg(feature = "std")]
122 fn _create_phantom_invoice<K: Deref, L: Deref>(
123         amt_msat: Option<u64>, payment_hash: Option<PaymentHash>, description: InvoiceDescription,
124         invoice_expiry_delta_secs: u32, phantom_route_hints: Vec<PhantomRouteHints>, keys_manager: K,
125         logger: L, network: Currency,
126 ) -> Result<Invoice, SignOrCreationError<()>>
127 where
128         K::Target: KeysInterface,
129         L::Target: Logger,
130 {
131         use std::time::{SystemTime, UNIX_EPOCH};
132
133         if phantom_route_hints.len() == 0 {
134                 return Err(SignOrCreationError::CreationError(
135                         CreationError::MissingRouteHints,
136                 ));
137         }
138
139         let invoice = match description {
140                 InvoiceDescription::Direct(description) => {
141                         InvoiceBuilder::new(network).description(description.0.clone())
142                 }
143                 InvoiceDescription::Hash(hash) => InvoiceBuilder::new(network).description_hash(hash.0),
144         };
145
146         // If we ever see performance here being too slow then we should probably take this ExpandedKey as a parameter instead.
147         let keys = ExpandedKey::new(&keys_manager.get_inbound_payment_key_material());
148         let (payment_hash, payment_secret) = if let Some(payment_hash) = payment_hash {
149                 let payment_secret = create_from_hash(
150                         &keys,
151                         amt_msat,
152                         payment_hash,
153                         invoice_expiry_delta_secs,
154                         SystemTime::now()
155                                 .duration_since(UNIX_EPOCH)
156                                 .expect("Time must be > 1970")
157                                 .as_secs(),
158                 )
159                 .map_err(|_| SignOrCreationError::CreationError(CreationError::InvalidAmount))?;
160                 (payment_hash, payment_secret)
161         } else {
162                 create(
163                         &keys,
164                         amt_msat,
165                         invoice_expiry_delta_secs,
166                         &keys_manager,
167                         SystemTime::now()
168                                 .duration_since(UNIX_EPOCH)
169                                 .expect("Time must be > 1970")
170                                 .as_secs(),
171                 )
172                 .map_err(|_| SignOrCreationError::CreationError(CreationError::InvalidAmount))?
173         };
174
175         log_trace!(logger, "Creating phantom invoice from {} participating nodes with payment hash {}",
176                 phantom_route_hints.len(), log_bytes!(payment_hash.0));
177
178         let mut invoice = invoice
179                 .current_timestamp()
180                 .payment_hash(Hash::from_slice(&payment_hash.0).unwrap())
181                 .payment_secret(payment_secret)
182                 .min_final_cltv_expiry(MIN_FINAL_CLTV_EXPIRY.into())
183                 .expiry_time(Duration::from_secs(invoice_expiry_delta_secs.into()));
184         if let Some(amt) = amt_msat {
185                 invoice = invoice.amount_milli_satoshis(amt);
186         }
187
188         for PhantomRouteHints { channels, phantom_scid, real_node_pubkey } in phantom_route_hints {
189                 log_trace!(logger, "Generating phantom route hints for node {}",
190                         log_pubkey!(real_node_pubkey));
191                 let mut route_hints = filter_channels(channels, amt_msat, &logger);
192
193                 // If we have any public channel, the route hints from `filter_channels` will be empty.
194                 // In that case we create a RouteHint on which we will push a single hop with the phantom
195                 // route into the invoice, and let the sender find the path to the `real_node_pubkey`
196                 // node by looking at our public channels.
197                 if route_hints.is_empty() {
198                         route_hints.push(RouteHint(vec![]))
199                 }
200                 for mut route_hint in route_hints {
201                         route_hint.0.push(RouteHintHop {
202                                 src_node_id: real_node_pubkey,
203                                 short_channel_id: phantom_scid,
204                                 fees: RoutingFees {
205                                         base_msat: 0,
206                                         proportional_millionths: 0,
207                                 },
208                                 cltv_expiry_delta: MIN_CLTV_EXPIRY_DELTA,
209                                 htlc_minimum_msat: None,
210                                 htlc_maximum_msat: None,});
211                         invoice = invoice.private_route(route_hint.clone());
212                 }
213         }
214
215         let raw_invoice = match invoice.build_raw() {
216                 Ok(inv) => inv,
217                 Err(e) => return Err(SignOrCreationError::CreationError(e))
218         };
219         let hrp_str = raw_invoice.hrp.to_string();
220         let hrp_bytes = hrp_str.as_bytes();
221         let data_without_signature = raw_invoice.data.to_base32();
222         let signed_raw_invoice = raw_invoice.sign(|_| keys_manager.sign_invoice(hrp_bytes, &data_without_signature, Recipient::PhantomNode));
223         match signed_raw_invoice {
224                 Ok(inv) => Ok(Invoice::from_signed(inv).unwrap()),
225                 Err(e) => Err(SignOrCreationError::SignError(e))
226         }
227 }
228
229 #[cfg(feature = "std")]
230 /// Utility to construct an invoice. Generally, unless you want to do something like a custom
231 /// cltv_expiry, this is what you should be using to create an invoice. The reason being, this
232 /// method stores the invoice's payment secret and preimage in `ChannelManager`, so (a) the user
233 /// doesn't have to store preimage/payment secret information and (b) `ChannelManager` can verify
234 /// that the payment secret is valid when the invoice is paid.
235 ///
236 /// `invoice_expiry_delta_secs` describes the number of seconds that the invoice is valid for
237 /// in excess of the current time.
238 pub fn create_invoice_from_channelmanager<M: Deref, T: Deref, K: Deref, F: Deref, L: Deref>(
239         channelmanager: &ChannelManager<M, T, K, F, L>, keys_manager: K, logger: L,
240         network: Currency, amt_msat: Option<u64>, description: String, invoice_expiry_delta_secs: u32
241 ) -> Result<Invoice, SignOrCreationError<()>>
242 where
243         M::Target: chain::Watch<<K::Target as KeysInterface>::Signer>,
244         T::Target: BroadcasterInterface,
245         K::Target: KeysInterface,
246         F::Target: FeeEstimator,
247         L::Target: Logger,
248 {
249         use std::time::SystemTime;
250         let duration = SystemTime::now().duration_since(SystemTime::UNIX_EPOCH)
251                 .expect("for the foreseeable future this shouldn't happen");
252         create_invoice_from_channelmanager_and_duration_since_epoch(
253                 channelmanager, keys_manager, logger, network, amt_msat, description, duration,
254                 invoice_expiry_delta_secs
255         )
256 }
257
258 #[cfg(feature = "std")]
259 /// Utility to construct an invoice. Generally, unless you want to do something like a custom
260 /// cltv_expiry, this is what you should be using to create an invoice. The reason being, this
261 /// method stores the invoice's payment secret and preimage in `ChannelManager`, so (a) the user
262 /// doesn't have to store preimage/payment secret information and (b) `ChannelManager` can verify
263 /// that the payment secret is valid when the invoice is paid.
264 /// Use this variant if you want to pass the `description_hash` to the invoice.
265 ///
266 /// `invoice_expiry_delta_secs` describes the number of seconds that the invoice is valid for
267 /// in excess of the current time.
268 pub fn create_invoice_from_channelmanager_with_description_hash<M: Deref, T: Deref, K: Deref, F: Deref, L: Deref>(
269         channelmanager: &ChannelManager<M, T, K, F, L>, keys_manager: K, logger: L,
270         network: Currency, amt_msat: Option<u64>, description_hash: Sha256,
271         invoice_expiry_delta_secs: u32
272 ) -> Result<Invoice, SignOrCreationError<()>>
273 where
274         M::Target: chain::Watch<<K::Target as KeysInterface>::Signer>,
275         T::Target: BroadcasterInterface,
276         K::Target: KeysInterface,
277         F::Target: FeeEstimator,
278         L::Target: Logger,
279 {
280         use std::time::SystemTime;
281
282         let duration = SystemTime::now()
283                 .duration_since(SystemTime::UNIX_EPOCH)
284                 .expect("for the foreseeable future this shouldn't happen");
285
286         create_invoice_from_channelmanager_with_description_hash_and_duration_since_epoch(
287                 channelmanager, keys_manager, logger, network, amt_msat,
288                 description_hash, duration, invoice_expiry_delta_secs
289         )
290 }
291
292 /// See [`create_invoice_from_channelmanager_with_description_hash`]
293 /// This version can be used in a `no_std` environment, where [`std::time::SystemTime`] is not
294 /// available and the current time is supplied by the caller.
295 pub fn create_invoice_from_channelmanager_with_description_hash_and_duration_since_epoch<M: Deref, T: Deref, K: Deref, F: Deref, L: Deref>(
296         channelmanager: &ChannelManager<M, T, K, F, L>, keys_manager: K, logger: L,
297         network: Currency, amt_msat: Option<u64>, description_hash: Sha256,
298         duration_since_epoch: Duration, invoice_expiry_delta_secs: u32
299 ) -> Result<Invoice, SignOrCreationError<()>>
300 where
301         M::Target: chain::Watch<<K::Target as KeysInterface>::Signer>,
302         T::Target: BroadcasterInterface,
303         K::Target: KeysInterface,
304         F::Target: FeeEstimator,
305         L::Target: Logger,
306 {
307         _create_invoice_from_channelmanager_and_duration_since_epoch(
308                 channelmanager, keys_manager, logger, network, amt_msat,
309                 InvoiceDescription::Hash(&description_hash),
310                 duration_since_epoch, invoice_expiry_delta_secs
311         )
312 }
313
314 /// See [`create_invoice_from_channelmanager`]
315 /// This version can be used in a `no_std` environment, where [`std::time::SystemTime`] is not
316 /// available and the current time is supplied by the caller.
317 pub fn create_invoice_from_channelmanager_and_duration_since_epoch<M: Deref, T: Deref, K: Deref, F: Deref, L: Deref>(
318         channelmanager: &ChannelManager<M, T, K, F, L>, keys_manager: K, logger: L,
319         network: Currency, amt_msat: Option<u64>, description: String, duration_since_epoch: Duration,
320         invoice_expiry_delta_secs: u32
321 ) -> Result<Invoice, SignOrCreationError<()>>
322 where
323         M::Target: chain::Watch<<K::Target as KeysInterface>::Signer>,
324         T::Target: BroadcasterInterface,
325         K::Target: KeysInterface,
326         F::Target: FeeEstimator,
327         L::Target: Logger,
328 {
329         _create_invoice_from_channelmanager_and_duration_since_epoch(
330                 channelmanager, keys_manager, logger, network, amt_msat,
331                 InvoiceDescription::Direct(
332                         &Description::new(description).map_err(SignOrCreationError::CreationError)?,
333                 ),
334                 duration_since_epoch, invoice_expiry_delta_secs
335         )
336 }
337
338 fn _create_invoice_from_channelmanager_and_duration_since_epoch<M: Deref, T: Deref, K: Deref, F: Deref, L: Deref>(
339         channelmanager: &ChannelManager<M, T, K, F, L>, keys_manager: K, logger: L,
340         network: Currency, amt_msat: Option<u64>, description: InvoiceDescription,
341         duration_since_epoch: Duration, invoice_expiry_delta_secs: u32
342 ) -> Result<Invoice, SignOrCreationError<()>>
343 where
344         M::Target: chain::Watch<<K::Target as KeysInterface>::Signer>,
345         T::Target: BroadcasterInterface,
346         K::Target: KeysInterface,
347         F::Target: FeeEstimator,
348         L::Target: Logger,
349 {
350         // `create_inbound_payment` only returns an error if the amount is greater than the total bitcoin
351         // supply.
352         let (payment_hash, payment_secret) = channelmanager
353                 .create_inbound_payment(amt_msat, invoice_expiry_delta_secs)
354                 .map_err(|()| SignOrCreationError::CreationError(CreationError::InvalidAmount))?;
355         let our_node_pubkey = channelmanager.get_our_node_id();
356         let channels = channelmanager.list_channels();
357
358         log_trace!(logger, "Creating invoice with payment hash {}", log_bytes!(payment_hash.0));
359
360         let invoice = match description {
361                 InvoiceDescription::Direct(description) => {
362                         InvoiceBuilder::new(network).description(description.0.clone())
363                 }
364                 InvoiceDescription::Hash(hash) => InvoiceBuilder::new(network).description_hash(hash.0),
365         };
366
367         let mut invoice = invoice
368                 .duration_since_epoch(duration_since_epoch)
369                 .payee_pub_key(our_node_pubkey)
370                 .payment_hash(Hash::from_slice(&payment_hash.0).unwrap())
371                 .payment_secret(payment_secret)
372                 .basic_mpp()
373                 .min_final_cltv_expiry(MIN_FINAL_CLTV_EXPIRY.into())
374                 .expiry_time(Duration::from_secs(invoice_expiry_delta_secs.into()));
375         if let Some(amt) = amt_msat {
376                 invoice = invoice.amount_milli_satoshis(amt);
377         }
378
379         let route_hints = filter_channels(channels, amt_msat, &logger);
380         for hint in route_hints {
381                 invoice = invoice.private_route(hint);
382         }
383
384         let raw_invoice = match invoice.build_raw() {
385                 Ok(inv) => inv,
386                 Err(e) => return Err(SignOrCreationError::CreationError(e))
387         };
388         let hrp_str = raw_invoice.hrp.to_string();
389         let hrp_bytes = hrp_str.as_bytes();
390         let data_without_signature = raw_invoice.data.to_base32();
391         let signed_raw_invoice = raw_invoice.sign(|_| keys_manager.sign_invoice(hrp_bytes, &data_without_signature, Recipient::Node));
392         match signed_raw_invoice {
393                 Ok(inv) => Ok(Invoice::from_signed(inv).unwrap()),
394                 Err(e) => Err(SignOrCreationError::SignError(e))
395         }
396 }
397
398 /// Filters the `channels` for an invoice, and returns the corresponding `RouteHint`s to include
399 /// in the invoice.
400 ///
401 /// The filtering is based on the following criteria:
402 /// * Only one channel per counterparty node
403 /// * Always select the channel with the highest inbound capacity per counterparty node
404 /// * Prefer channels with capacity at least `min_inbound_capacity_msat` and where the channel
405 ///   `is_usable` (i.e. the peer is connected).
406 /// * If any public channel exists, the returned `RouteHint`s will be empty, and the sender will
407 ///   need to find the path by looking at the public channels instead
408 fn filter_channels<L: Deref>(
409         channels: Vec<ChannelDetails>, min_inbound_capacity_msat: Option<u64>, logger: &L
410 ) -> Vec<RouteHint> where L::Target: Logger {
411         let mut filtered_channels: HashMap<PublicKey, ChannelDetails> = HashMap::new();
412         let min_inbound_capacity = min_inbound_capacity_msat.unwrap_or(0);
413         let mut min_capacity_channel_exists = false;
414         let mut online_channel_exists = false;
415         let mut online_min_capacity_channel_exists = false;
416
417         log_trace!(logger, "Considering {} channels for invoice route hints", channels.len());
418         for channel in channels.into_iter().filter(|chan| chan.is_channel_ready) {
419                 if channel.get_inbound_payment_scid().is_none() || channel.counterparty.forwarding_info.is_none() {
420                         log_trace!(logger, "Ignoring channel {} for invoice route hints", log_bytes!(channel.channel_id));
421                         continue;
422                 }
423
424                 if channel.is_public {
425                         // If any public channel exists, return no hints and let the sender
426                         // look at the public channels instead.
427                         log_trace!(logger, "Not including channels in invoice route hints on account of public channel {}",
428                                 log_bytes!(channel.channel_id));
429                         return vec![]
430                 }
431
432                 if channel.inbound_capacity_msat >= min_inbound_capacity {
433                         if !min_capacity_channel_exists {
434                                 log_trace!(logger, "Channel with enough inbound capacity exists for invoice route hints");
435                                 min_capacity_channel_exists = true;
436                         }
437
438                         if channel.is_usable {
439                                 online_min_capacity_channel_exists = true;
440                         }
441                 }
442
443                 if channel.is_usable {
444                         if !online_channel_exists {
445                                 log_trace!(logger, "Channel with connected peer exists for invoice route hints");
446                                 online_channel_exists = true;
447                         }
448                 }
449
450                 match filtered_channels.entry(channel.counterparty.node_id) {
451                         hash_map::Entry::Occupied(mut entry) => {
452                                 let current_max_capacity = entry.get().inbound_capacity_msat;
453                                 if channel.inbound_capacity_msat < current_max_capacity {
454                                         log_trace!(logger,
455                                                 "Preferring counterparty {} channel {} ({} msats) over {} ({} msats) for invoice route hints",
456                                                 log_pubkey!(channel.counterparty.node_id),
457                                                 log_bytes!(entry.get().channel_id), current_max_capacity,
458                                                 log_bytes!(channel.channel_id), channel.inbound_capacity_msat);
459                                         continue;
460                                 }
461                                 log_trace!(logger,
462                                         "Preferring counterparty {} channel {} ({} msats) over {} ({} msats) for invoice route hints",
463                                         log_pubkey!(channel.counterparty.node_id),
464                                         log_bytes!(channel.channel_id), channel.inbound_capacity_msat,
465                                         log_bytes!(entry.get().channel_id), current_max_capacity);
466                                 entry.insert(channel);
467                         }
468                         hash_map::Entry::Vacant(entry) => {
469                                 entry.insert(channel);
470                         }
471                 }
472         }
473
474         let route_hint_from_channel = |channel: ChannelDetails| {
475                 let forwarding_info = channel.counterparty.forwarding_info.as_ref().unwrap();
476                 RouteHint(vec![RouteHintHop {
477                         src_node_id: channel.counterparty.node_id,
478                         short_channel_id: channel.get_inbound_payment_scid().unwrap(),
479                         fees: RoutingFees {
480                                 base_msat: forwarding_info.fee_base_msat,
481                                 proportional_millionths: forwarding_info.fee_proportional_millionths,
482                         },
483                         cltv_expiry_delta: forwarding_info.cltv_expiry_delta,
484                         htlc_minimum_msat: channel.inbound_htlc_minimum_msat,
485                         htlc_maximum_msat: channel.inbound_htlc_maximum_msat,}])
486         };
487         // If all channels are private, prefer to return route hints which have a higher capacity than
488         // the payment value and where we're currently connected to the channel counterparty.
489         // Even if we cannot satisfy both goals, always ensure we include *some* hints, preferring
490         // those which meet at least one criteria.
491         filtered_channels
492                 .into_iter()
493                 .map(|(_, channel)| channel)
494                 .filter(|channel| {
495                         let has_enough_capacity = channel.inbound_capacity_msat >= min_inbound_capacity;
496                         let include_channel = if online_min_capacity_channel_exists {
497                                 has_enough_capacity && channel.is_usable
498                         } else if min_capacity_channel_exists && online_channel_exists {
499                                 // If there are some online channels and some min_capacity channels, but no
500                                 // online-and-min_capacity channels, just include the min capacity ones and ignore
501                                 // online-ness.
502                                 has_enough_capacity
503                         } else if min_capacity_channel_exists {
504                                 has_enough_capacity
505                         } else if online_channel_exists {
506                                 channel.is_usable
507                         } else { true };
508
509                         if include_channel {
510                                 log_trace!(logger, "Including channel {} in invoice route hints",
511                                         log_bytes!(channel.channel_id));
512                         } else if !has_enough_capacity {
513                                 log_trace!(logger, "Ignoring channel {} without enough capacity for invoice route hints",
514                                         log_bytes!(channel.channel_id));
515                         } else {
516                                 debug_assert!(!channel.is_usable);
517                                 log_trace!(logger, "Ignoring channel {} with disconnected peer",
518                                         log_bytes!(channel.channel_id));
519                         }
520
521                         include_channel
522                 })
523                 .map(route_hint_from_channel)
524                 .collect::<Vec<RouteHint>>()
525 }
526
527 /// A [`Router`] implemented using [`find_route`].
528 pub struct DefaultRouter<G: Deref<Target = NetworkGraph<L>>, L: Deref, S: Deref> where
529         L::Target: Logger,
530         S::Target: for <'a> LockableScore<'a>,
531 {
532         network_graph: G,
533         logger: L,
534         random_seed_bytes: Mutex<[u8; 32]>,
535         scorer: S
536 }
537
538 impl<G: Deref<Target = NetworkGraph<L>>, L: Deref, S: Deref> DefaultRouter<G, L, S> where
539         L::Target: Logger,
540         S::Target: for <'a> LockableScore<'a>,
541 {
542         /// Creates a new router using the given [`NetworkGraph`], a [`Logger`], and a randomness source
543         /// `random_seed_bytes`.
544         pub fn new(network_graph: G, logger: L, random_seed_bytes: [u8; 32], scorer: S) -> Self {
545                 let random_seed_bytes = Mutex::new(random_seed_bytes);
546                 Self { network_graph, logger, random_seed_bytes, scorer }
547         }
548 }
549
550 impl<G: Deref<Target = NetworkGraph<L>>, L: Deref, S: Deref> Router for DefaultRouter<G, L, S> where
551         L::Target: Logger,
552         S::Target: for <'a> LockableScore<'a>,
553 {
554         fn find_route(
555                 &self, payer: &PublicKey, params: &RouteParameters, first_hops: Option<&[&ChannelDetails]>,
556                 inflight_htlcs: InFlightHtlcs
557         ) -> Result<Route, LightningError> {
558                 let random_seed_bytes = {
559                         let mut locked_random_seed_bytes = self.random_seed_bytes.lock().unwrap();
560                         *locked_random_seed_bytes = sha256::Hash::hash(&*locked_random_seed_bytes).into_inner();
561                         *locked_random_seed_bytes
562                 };
563
564                 find_route(
565                         payer, params, &self.network_graph, first_hops, &*self.logger,
566                         &ScorerAccountingForInFlightHtlcs::new(&mut self.scorer.lock(), inflight_htlcs),
567                         &random_seed_bytes
568                 )
569         }
570 }
571
572 impl<G: Deref<Target = NetworkGraph<L>>, L: Deref, S: Deref> ScoringRouter for DefaultRouter<G, L, S> where
573         L::Target: Logger,
574         S::Target: for <'a> LockableScore<'a>,
575 {
576         fn notify_payment_path_failed(&self, path: &[&RouteHop], short_channel_id: u64) {
577                 self.scorer.lock().payment_path_failed(path, short_channel_id);
578         }
579
580         fn notify_payment_path_successful(&self, path: &[&RouteHop]) {
581                 self.scorer.lock().payment_path_successful(path);
582         }
583
584         fn notify_payment_probe_successful(&self, path: &[&RouteHop]) {
585                 self.scorer.lock().probe_successful(path);
586         }
587
588         fn notify_payment_probe_failed(&self, path: &[&RouteHop], short_channel_id: u64) {
589                 self.scorer.lock().probe_failed(path, short_channel_id);
590         }
591 }
592
593 impl<M: Deref, T: Deref, K: Deref, F: Deref, L: Deref> Payer for ChannelManager<M, T, K, F, L>
594 where
595         M::Target: chain::Watch<<K::Target as KeysInterface>::Signer>,
596         T::Target: BroadcasterInterface,
597         K::Target: KeysInterface,
598         F::Target: FeeEstimator,
599         L::Target: Logger,
600 {
601         fn node_id(&self) -> PublicKey {
602                 self.get_our_node_id()
603         }
604
605         fn first_hops(&self) -> Vec<ChannelDetails> {
606                 self.list_usable_channels()
607         }
608
609         fn send_payment(
610                 &self, route: &Route, payment_hash: PaymentHash, payment_secret: &Option<PaymentSecret>,
611                 payment_id: PaymentId
612         ) -> Result<(), PaymentSendFailure> {
613                 self.send_payment(route, payment_hash, payment_secret, payment_id)
614         }
615
616         fn send_spontaneous_payment(
617                 &self, route: &Route, payment_preimage: PaymentPreimage, payment_id: PaymentId,
618         ) -> Result<(), PaymentSendFailure> {
619                 self.send_spontaneous_payment(route, Some(payment_preimage), payment_id).map(|_| ())
620         }
621
622         fn retry_payment(
623                 &self, route: &Route, payment_id: PaymentId
624         ) -> Result<(), PaymentSendFailure> {
625                 self.retry_payment(route, payment_id)
626         }
627
628         fn abandon_payment(&self, payment_id: PaymentId) {
629                 self.abandon_payment(payment_id)
630         }
631
632         fn inflight_htlcs(&self) -> InFlightHtlcs { self.compute_inflight_htlcs() }
633 }
634
635
636 /// Used to store information about all the HTLCs that are inflight across all payment attempts.
637 pub(crate) struct ScorerAccountingForInFlightHtlcs<'a, S: Score> {
638         scorer: &'a mut S,
639         /// Maps a channel's short channel id and its direction to the liquidity used up.
640         inflight_htlcs: InFlightHtlcs,
641 }
642
643 impl<'a, S: Score> ScorerAccountingForInFlightHtlcs<'a, S> {
644         pub(crate) fn new(scorer: &'a mut S, inflight_htlcs: InFlightHtlcs) -> Self {
645                 ScorerAccountingForInFlightHtlcs {
646                         scorer,
647                         inflight_htlcs
648                 }
649         }
650 }
651
652 #[cfg(c_bindings)]
653 impl<'a, S:Score> lightning::util::ser::Writeable for ScorerAccountingForInFlightHtlcs<'a, S> {
654         fn write<W: lightning::util::ser::Writer>(&self, writer: &mut W) -> Result<(), lightning::io::Error> { self.scorer.write(writer) }
655 }
656
657 impl<'a, S: Score> Score for ScorerAccountingForInFlightHtlcs<'a, S> {
658         fn channel_penalty_msat(&self, short_channel_id: u64, source: &NodeId, target: &NodeId, usage: ChannelUsage) -> u64 {
659                 if let Some(used_liqudity) = self.inflight_htlcs.used_liquidity_msat(
660                         source, target, short_channel_id
661                 ) {
662                         let usage = ChannelUsage {
663                                 inflight_htlc_msat: usage.inflight_htlc_msat + used_liqudity,
664                                 ..usage
665                         };
666
667                         self.scorer.channel_penalty_msat(short_channel_id, source, target, usage)
668                 } else {
669                         self.scorer.channel_penalty_msat(short_channel_id, source, target, usage)
670                 }
671         }
672
673         fn payment_path_failed(&mut self, _path: &[&RouteHop], _short_channel_id: u64) { unreachable!() }
674
675         fn payment_path_successful(&mut self, _path: &[&RouteHop]) { unreachable!() }
676
677         fn probe_failed(&mut self, _path: &[&RouteHop], _short_channel_id: u64) { unreachable!() }
678
679         fn probe_successful(&mut self, _path: &[&RouteHop]) { unreachable!() }
680 }
681
682
683 #[cfg(test)]
684 mod test {
685         use core::time::Duration;
686         use crate::{Currency, Description, InvoiceDescription};
687         use bitcoin_hashes::Hash;
688         use bitcoin_hashes::sha256::Hash as Sha256;
689         use lightning::chain::keysinterface::PhantomKeysManager;
690         use lightning::ln::{PaymentPreimage, PaymentHash};
691         use lightning::ln::channelmanager::{self, PhantomRouteHints, MIN_FINAL_CLTV_EXPIRY, PaymentId};
692         use lightning::ln::functional_test_utils::*;
693         use lightning::ln::msgs::ChannelMessageHandler;
694         use lightning::routing::router::{PaymentParameters, RouteParameters, find_route};
695         use lightning::util::events::{MessageSendEvent, MessageSendEventsProvider, Event};
696         use lightning::util::test_utils;
697         use lightning::util::config::UserConfig;
698         use lightning::chain::keysinterface::KeysInterface;
699         use crate::utils::create_invoice_from_channelmanager_and_duration_since_epoch;
700         use std::collections::HashSet;
701
702         #[test]
703         fn test_from_channelmanager() {
704                 let chanmon_cfgs = create_chanmon_cfgs(2);
705                 let node_cfgs = create_node_cfgs(2, &chanmon_cfgs);
706                 let node_chanmgrs = create_node_chanmgrs(2, &node_cfgs, &[None, None]);
707                 let nodes = create_network(2, &node_cfgs, &node_chanmgrs);
708                 create_unannounced_chan_between_nodes_with_value(&nodes, 0, 1, 100000, 10001, channelmanager::provided_init_features(), channelmanager::provided_init_features());
709                 let non_default_invoice_expiry_secs = 4200;
710                 let invoice = create_invoice_from_channelmanager_and_duration_since_epoch(
711                         &nodes[1].node, nodes[1].keys_manager, nodes[1].logger, Currency::BitcoinTestnet,
712                         Some(10_000), "test".to_string(), Duration::from_secs(1234567),
713                         non_default_invoice_expiry_secs).unwrap();
714                 assert_eq!(invoice.amount_pico_btc(), Some(100_000));
715                 assert_eq!(invoice.min_final_cltv_expiry(), MIN_FINAL_CLTV_EXPIRY as u64);
716                 assert_eq!(invoice.description(), InvoiceDescription::Direct(&Description("test".to_string())));
717                 assert_eq!(invoice.expiry_time(), Duration::from_secs(non_default_invoice_expiry_secs.into()));
718
719                 // Invoice SCIDs should always use inbound SCID aliases over the real channel ID, if one is
720                 // available.
721                 let chan = &nodes[1].node.list_usable_channels()[0];
722                 assert_eq!(invoice.route_hints().len(), 1);
723                 assert_eq!(invoice.route_hints()[0].0.len(), 1);
724                 assert_eq!(invoice.route_hints()[0].0[0].short_channel_id, chan.inbound_scid_alias.unwrap());
725
726                 assert_eq!(invoice.route_hints()[0].0[0].htlc_minimum_msat, chan.inbound_htlc_minimum_msat);
727                 assert_eq!(invoice.route_hints()[0].0[0].htlc_maximum_msat, chan.inbound_htlc_maximum_msat);
728
729                 let payment_params = PaymentParameters::from_node_id(invoice.recover_payee_pub_key())
730                         .with_features(invoice.features().unwrap().clone())
731                         .with_route_hints(invoice.route_hints());
732                 let route_params = RouteParameters {
733                         payment_params,
734                         final_value_msat: invoice.amount_milli_satoshis().unwrap(),
735                         final_cltv_expiry_delta: invoice.min_final_cltv_expiry() as u32,
736                 };
737                 let first_hops = nodes[0].node.list_usable_channels();
738                 let network_graph = &node_cfgs[0].network_graph;
739                 let logger = test_utils::TestLogger::new();
740                 let scorer = test_utils::TestScorer::with_penalty(0);
741                 let random_seed_bytes = chanmon_cfgs[1].keys_manager.get_secure_random_bytes();
742                 let route = find_route(
743                         &nodes[0].node.get_our_node_id(), &route_params, &network_graph,
744                         Some(&first_hops.iter().collect::<Vec<_>>()), &logger, &scorer, &random_seed_bytes
745                 ).unwrap();
746
747                 let payment_event = {
748                         let mut payment_hash = PaymentHash([0; 32]);
749                         payment_hash.0.copy_from_slice(&invoice.payment_hash().as_ref()[0..32]);
750                         nodes[0].node.send_payment(&route, payment_hash, &Some(invoice.payment_secret().clone()), PaymentId(payment_hash.0)).unwrap();
751                         let mut added_monitors = nodes[0].chain_monitor.added_monitors.lock().unwrap();
752                         assert_eq!(added_monitors.len(), 1);
753                         added_monitors.clear();
754
755                         let mut events = nodes[0].node.get_and_clear_pending_msg_events();
756                         assert_eq!(events.len(), 1);
757                         SendEvent::from_event(events.remove(0))
758
759                 };
760                 nodes[1].node.handle_update_add_htlc(&nodes[0].node.get_our_node_id(), &payment_event.msgs[0]);
761                 nodes[1].node.handle_commitment_signed(&nodes[0].node.get_our_node_id(), &payment_event.commitment_msg);
762                 let mut added_monitors = nodes[1].chain_monitor.added_monitors.lock().unwrap();
763                 assert_eq!(added_monitors.len(), 1);
764                 added_monitors.clear();
765                 let events = nodes[1].node.get_and_clear_pending_msg_events();
766                 assert_eq!(events.len(), 2);
767         }
768
769         #[test]
770         fn test_create_invoice_with_description_hash() {
771                 let chanmon_cfgs = create_chanmon_cfgs(2);
772                 let node_cfgs = create_node_cfgs(2, &chanmon_cfgs);
773                 let node_chanmgrs = create_node_chanmgrs(2, &node_cfgs, &[None, None]);
774                 let nodes = create_network(2, &node_cfgs, &node_chanmgrs);
775                 let description_hash = crate::Sha256(Hash::hash("Testing description_hash".as_bytes()));
776                 let invoice = crate::utils::create_invoice_from_channelmanager_with_description_hash_and_duration_since_epoch(
777                         &nodes[1].node, nodes[1].keys_manager, nodes[1].logger, Currency::BitcoinTestnet,
778                         Some(10_000), description_hash, Duration::from_secs(1234567), 3600
779                 ).unwrap();
780                 assert_eq!(invoice.amount_pico_btc(), Some(100_000));
781                 assert_eq!(invoice.min_final_cltv_expiry(), MIN_FINAL_CLTV_EXPIRY as u64);
782                 assert_eq!(invoice.description(), InvoiceDescription::Hash(&crate::Sha256(Sha256::hash("Testing description_hash".as_bytes()))));
783         }
784
785         #[test]
786         fn test_hints_includes_single_channels_to_nodes() {
787                 let chanmon_cfgs = create_chanmon_cfgs(3);
788                 let node_cfgs = create_node_cfgs(3, &chanmon_cfgs);
789                 let node_chanmgrs = create_node_chanmgrs(3, &node_cfgs, &[None, None, None]);
790                 let nodes = create_network(3, &node_cfgs, &node_chanmgrs);
791
792                 let chan_1_0 = create_unannounced_chan_between_nodes_with_value(&nodes, 1, 0, 100000, 10001, channelmanager::provided_init_features(), channelmanager::provided_init_features());
793                 let chan_2_0 = create_unannounced_chan_between_nodes_with_value(&nodes, 2, 0, 100000, 10001, channelmanager::provided_init_features(), channelmanager::provided_init_features());
794
795                 let mut scid_aliases = HashSet::new();
796                 scid_aliases.insert(chan_1_0.0.short_channel_id_alias.unwrap());
797                 scid_aliases.insert(chan_2_0.0.short_channel_id_alias.unwrap());
798
799                 match_invoice_routes(Some(5000), &nodes[0], scid_aliases);
800         }
801
802         #[test]
803         fn test_hints_has_only_highest_inbound_capacity_channel() {
804                 let chanmon_cfgs = create_chanmon_cfgs(2);
805                 let node_cfgs = create_node_cfgs(2, &chanmon_cfgs);
806                 let node_chanmgrs = create_node_chanmgrs(2, &node_cfgs, &[None, None]);
807                 let nodes = create_network(2, &node_cfgs, &node_chanmgrs);
808                 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());
809                 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());
810                 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());
811
812                 let mut scid_aliases = HashSet::new();
813                 scid_aliases.insert(chan_1_0_high_inbound_capacity.0.short_channel_id_alias.unwrap());
814
815                 match_invoice_routes(Some(5000), &nodes[0], scid_aliases);
816         }
817
818         #[test]
819         fn test_hints_has_only_online_channels() {
820                 let chanmon_cfgs = create_chanmon_cfgs(4);
821                 let node_cfgs = create_node_cfgs(4, &chanmon_cfgs);
822                 let node_chanmgrs = create_node_chanmgrs(4, &node_cfgs, &[None, None, None, None]);
823                 let nodes = create_network(4, &node_cfgs, &node_chanmgrs);
824                 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());
825                 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());
826                 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());
827
828                 // With all peers connected we should get all hints that have sufficient value
829                 let mut scid_aliases = HashSet::new();
830                 scid_aliases.insert(chan_a.0.short_channel_id_alias.unwrap());
831                 scid_aliases.insert(chan_b.0.short_channel_id_alias.unwrap());
832
833                 match_invoice_routes(Some(1_000_000_000), &nodes[0], scid_aliases.clone());
834
835                 // With only one sufficient-value peer connected we should only get its hint
836                 scid_aliases.remove(&chan_b.0.short_channel_id_alias.unwrap());
837                 nodes[0].node.peer_disconnected(&nodes[2].node.get_our_node_id(), false);
838                 match_invoice_routes(Some(1_000_000_000), &nodes[0], scid_aliases.clone());
839
840                 // If we don't have any sufficient-value peers connected we should get all hints with
841                 // sufficient value, even though there is a connected insufficient-value peer.
842                 scid_aliases.insert(chan_b.0.short_channel_id_alias.unwrap());
843                 nodes[0].node.peer_disconnected(&nodes[1].node.get_our_node_id(), false);
844                 match_invoice_routes(Some(1_000_000_000), &nodes[0], scid_aliases);
845         }
846
847         #[test]
848         fn test_forwarding_info_not_assigned_channel_excluded_from_hints() {
849                 let chanmon_cfgs = create_chanmon_cfgs(3);
850                 let node_cfgs = create_node_cfgs(3, &chanmon_cfgs);
851                 let node_chanmgrs = create_node_chanmgrs(3, &node_cfgs, &[None, None, None]);
852                 let nodes = create_network(3, &node_cfgs, &node_chanmgrs);
853                 let chan_1_0 = create_unannounced_chan_between_nodes_with_value(&nodes, 1, 0, 100000, 10001, channelmanager::provided_init_features(), channelmanager::provided_init_features());
854
855                 // Create an unannonced channel between `nodes[2]` and `nodes[0]`, for which the
856                 // `msgs::ChannelUpdate` is never handled for the node(s). As the `msgs::ChannelUpdate`
857                 // is never handled, the `channel.counterparty.forwarding_info` is never assigned.
858                 let mut private_chan_cfg = UserConfig::default();
859                 private_chan_cfg.channel_handshake_config.announced_channel = false;
860                 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();
861                 let open_channel = get_event_msg!(nodes[2], MessageSendEvent::SendOpenChannel, nodes[0].node.get_our_node_id());
862                 nodes[0].node.handle_open_channel(&nodes[2].node.get_our_node_id(), channelmanager::provided_init_features(), &open_channel);
863                 let accept_channel = get_event_msg!(nodes[0], MessageSendEvent::SendAcceptChannel, nodes[2].node.get_our_node_id());
864                 nodes[2].node.handle_accept_channel(&nodes[0].node.get_our_node_id(), channelmanager::provided_init_features(), &accept_channel);
865
866                 let tx = sign_funding_transaction(&nodes[2], &nodes[0], 1_000_000, temporary_channel_id);
867
868                 let conf_height = core::cmp::max(nodes[2].best_block_info().1 + 1, nodes[0].best_block_info().1 + 1);
869                 confirm_transaction_at(&nodes[2], &tx, conf_height);
870                 connect_blocks(&nodes[2], CHAN_CONFIRM_DEPTH - 1);
871                 confirm_transaction_at(&nodes[0], &tx, conf_height);
872                 connect_blocks(&nodes[0], CHAN_CONFIRM_DEPTH - 1);
873                 let as_channel_ready = get_event_msg!(nodes[2], MessageSendEvent::SendChannelReady, nodes[0].node.get_our_node_id());
874                 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()));
875                 get_event_msg!(nodes[2], MessageSendEvent::SendChannelUpdate, nodes[0].node.get_our_node_id());
876                 nodes[0].node.handle_channel_ready(&nodes[2].node.get_our_node_id(), &as_channel_ready);
877                 get_event_msg!(nodes[0], MessageSendEvent::SendChannelUpdate, nodes[2].node.get_our_node_id());
878                 expect_channel_ready_event(&nodes[0], &nodes[2].node.get_our_node_id());
879                 expect_channel_ready_event(&nodes[2], &nodes[0].node.get_our_node_id());
880
881                 // As `msgs::ChannelUpdate` was never handled for the participating node(s) of the second
882                 // channel, the channel will never be assigned any `counterparty.forwarding_info`.
883                 // Therefore only `chan_1_0` should be included in the hints.
884                 let mut scid_aliases = HashSet::new();
885                 scid_aliases.insert(chan_1_0.0.short_channel_id_alias.unwrap());
886                 match_invoice_routes(Some(5000), &nodes[0], scid_aliases);
887         }
888
889         #[test]
890         fn test_no_hints_if_a_mix_between_public_and_private_channel_exists() {
891                 let chanmon_cfgs = create_chanmon_cfgs(3);
892                 let node_cfgs = create_node_cfgs(3, &chanmon_cfgs);
893                 let node_chanmgrs = create_node_chanmgrs(3, &node_cfgs, &[None, None, None]);
894                 let nodes = create_network(3, &node_cfgs, &node_chanmgrs);
895                 let _chan_1_0 = create_unannounced_chan_between_nodes_with_value(&nodes, 1, 0, 100000, 10001, channelmanager::provided_init_features(), channelmanager::provided_init_features());
896
897                 let chan_2_0 = create_announced_chan_between_nodes_with_value(&nodes, 2, 0, 100000, 10001, channelmanager::provided_init_features(), channelmanager::provided_init_features());
898                 nodes[2].node.handle_channel_update(&nodes[0].node.get_our_node_id(), &chan_2_0.1);
899                 nodes[0].node.handle_channel_update(&nodes[2].node.get_our_node_id(), &chan_2_0.0);
900
901                 // Ensure that the invoice doesn't include any route hints for any of `nodes[0]` channels,
902                 // even though all channels between `nodes[1]` and `nodes[0]` are private, as there is a
903                 // public channel between `nodes[2]` and `nodes[0]`
904                 match_invoice_routes(Some(5000), &nodes[0], HashSet::new());
905         }
906
907         #[test]
908         fn test_only_public_channels_includes_no_channels_in_hints() {
909                 let chanmon_cfgs = create_chanmon_cfgs(3);
910                 let node_cfgs = create_node_cfgs(3, &chanmon_cfgs);
911                 let node_chanmgrs = create_node_chanmgrs(3, &node_cfgs, &[None, None, None]);
912                 let nodes = create_network(3, &node_cfgs, &node_chanmgrs);
913                 let chan_1_0 = create_announced_chan_between_nodes_with_value(&nodes, 1, 0, 100000, 10001, channelmanager::provided_init_features(), channelmanager::provided_init_features());
914                 nodes[0].node.handle_channel_update(&nodes[1].node.get_our_node_id(), &chan_1_0.0);
915                 nodes[1].node.handle_channel_update(&nodes[0].node.get_our_node_id(), &chan_1_0.1);
916
917                 let chan_2_0 = create_announced_chan_between_nodes_with_value(&nodes, 2, 0, 100000, 10001, channelmanager::provided_init_features(), channelmanager::provided_init_features());
918                 nodes[2].node.handle_channel_update(&nodes[0].node.get_our_node_id(), &chan_2_0.1);
919                 nodes[0].node.handle_channel_update(&nodes[2].node.get_our_node_id(), &chan_2_0.0);
920
921                 // As all of `nodes[0]` channels are public, no channels should be included in the hints
922                 match_invoice_routes(Some(5000), &nodes[0], HashSet::new());
923         }
924
925         #[test]
926         fn test_channels_with_lower_inbound_capacity_than_invoice_amt_hints_filtering() {
927                 let chanmon_cfgs = create_chanmon_cfgs(3);
928                 let node_cfgs = create_node_cfgs(3, &chanmon_cfgs);
929                 let node_chanmgrs = create_node_chanmgrs(3, &node_cfgs, &[None, None, None]);
930                 let nodes = create_network(3, &node_cfgs, &node_chanmgrs);
931                 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());
932                 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());
933
934                 // As the invoice amt is 1 msat above chan_1_0's inbound capacity, it shouldn't be included
935                 let mut scid_aliases_99_000_001_msat = HashSet::new();
936                 scid_aliases_99_000_001_msat.insert(chan_2_0.0.short_channel_id_alias.unwrap());
937
938                 match_invoice_routes(Some(99_000_001), &nodes[0], scid_aliases_99_000_001_msat);
939
940                 // As the invoice amt is exactly at chan_1_0's inbound capacity, it should be included
941                 let mut scid_aliases_99_000_000_msat = HashSet::new();
942                 scid_aliases_99_000_000_msat.insert(chan_1_0.0.short_channel_id_alias.unwrap());
943                 scid_aliases_99_000_000_msat.insert(chan_2_0.0.short_channel_id_alias.unwrap());
944
945                 match_invoice_routes(Some(99_000_000), &nodes[0], scid_aliases_99_000_000_msat);
946
947                 // As the invoice amt is above all channels' inbound capacity, they will still be included
948                 let mut scid_aliases_2_000_000_000_msat = HashSet::new();
949                 scid_aliases_2_000_000_000_msat.insert(chan_1_0.0.short_channel_id_alias.unwrap());
950                 scid_aliases_2_000_000_000_msat.insert(chan_2_0.0.short_channel_id_alias.unwrap());
951
952                 match_invoice_routes(Some(2_000_000_000), &nodes[0], scid_aliases_2_000_000_000_msat);
953
954                 // An invoice with no specified amount should include all channels in the route hints.
955                 let mut scid_aliases_no_specified_amount = HashSet::new();
956                 scid_aliases_no_specified_amount.insert(chan_1_0.0.short_channel_id_alias.unwrap());
957                 scid_aliases_no_specified_amount.insert(chan_2_0.0.short_channel_id_alias.unwrap());
958
959                 match_invoice_routes(None, &nodes[0], scid_aliases_no_specified_amount);
960         }
961
962         fn match_invoice_routes<'a, 'b: 'a, 'c: 'b>(
963                 invoice_amt: Option<u64>,
964                 invoice_node: &Node<'a, 'b, 'c>,
965                 mut chan_ids_to_match: HashSet<u64>
966         ) {
967                 let invoice = create_invoice_from_channelmanager_and_duration_since_epoch(
968                         &invoice_node.node, invoice_node.keys_manager, invoice_node.logger,
969                         Currency::BitcoinTestnet, invoice_amt, "test".to_string(), Duration::from_secs(1234567),
970                         3600).unwrap();
971                 let hints = invoice.private_routes();
972
973                 for hint in hints {
974                         let hint_short_chan_id = (hint.0).0[0].short_channel_id;
975                         assert!(chan_ids_to_match.remove(&hint_short_chan_id));
976                 }
977                 assert!(chan_ids_to_match.is_empty(), "Unmatched short channel ids: {:?}", chan_ids_to_match);
978         }
979
980         #[test]
981         #[cfg(feature = "std")]
982         fn test_multi_node_receive() {
983                 do_test_multi_node_receive(true);
984                 do_test_multi_node_receive(false);
985         }
986
987         #[cfg(feature = "std")]
988         fn do_test_multi_node_receive(user_generated_pmt_hash: bool) {
989                 let mut chanmon_cfgs = create_chanmon_cfgs(3);
990                 let seed_1 = [42 as u8; 32];
991                 let seed_2 = [43 as u8; 32];
992                 let cross_node_seed = [44 as u8; 32];
993                 chanmon_cfgs[1].keys_manager.backing = PhantomKeysManager::new(&seed_1, 43, 44, &cross_node_seed);
994                 chanmon_cfgs[2].keys_manager.backing = PhantomKeysManager::new(&seed_2, 43, 44, &cross_node_seed);
995                 let node_cfgs = create_node_cfgs(3, &chanmon_cfgs);
996                 let node_chanmgrs = create_node_chanmgrs(3, &node_cfgs, &[None, None, None]);
997                 let nodes = create_network(3, &node_cfgs, &node_chanmgrs);
998                 let chan_0_1 = create_announced_chan_between_nodes_with_value(&nodes, 0, 1, 100000, 10001, channelmanager::provided_init_features(), channelmanager::provided_init_features());
999                 nodes[0].node.handle_channel_update(&nodes[1].node.get_our_node_id(), &chan_0_1.1);
1000                 nodes[1].node.handle_channel_update(&nodes[0].node.get_our_node_id(), &chan_0_1.0);
1001                 let chan_0_2 = create_announced_chan_between_nodes_with_value(&nodes, 0, 2, 100000, 10001, channelmanager::provided_init_features(), channelmanager::provided_init_features());
1002                 nodes[0].node.handle_channel_update(&nodes[2].node.get_our_node_id(), &chan_0_2.1);
1003                 nodes[2].node.handle_channel_update(&nodes[0].node.get_our_node_id(), &chan_0_2.0);
1004
1005                 let payment_amt = 10_000;
1006                 let route_hints = vec![
1007                         nodes[1].node.get_phantom_route_hints(),
1008                         nodes[2].node.get_phantom_route_hints(),
1009                 ];
1010
1011                 let user_payment_preimage = PaymentPreimage([1; 32]);
1012                 let payment_hash = if user_generated_pmt_hash {
1013                         Some(PaymentHash(Sha256::hash(&user_payment_preimage.0[..]).into_inner()))
1014                 } else {
1015                         None
1016                 };
1017                 let non_default_invoice_expiry_secs = 4200;
1018
1019                 let invoice =
1020                         crate::utils::create_phantom_invoice::<&test_utils::TestKeysInterface, &test_utils::TestLogger>(
1021                                 Some(payment_amt), payment_hash, "test".to_string(), non_default_invoice_expiry_secs,
1022                                 route_hints, &nodes[1].keys_manager, &nodes[1].logger, Currency::BitcoinTestnet
1023                         ).unwrap();
1024                 let (payment_hash, payment_secret) = (PaymentHash(invoice.payment_hash().into_inner()), *invoice.payment_secret());
1025                 let payment_preimage = if user_generated_pmt_hash {
1026                         user_payment_preimage
1027                 } else {
1028                         nodes[1].node.get_payment_preimage(payment_hash, payment_secret).unwrap()
1029                 };
1030
1031                 assert_eq!(invoice.min_final_cltv_expiry(), MIN_FINAL_CLTV_EXPIRY as u64);
1032                 assert_eq!(invoice.description(), InvoiceDescription::Direct(&Description("test".to_string())));
1033                 assert_eq!(invoice.route_hints().len(), 2);
1034                 assert_eq!(invoice.expiry_time(), Duration::from_secs(non_default_invoice_expiry_secs.into()));
1035                 assert!(!invoice.features().unwrap().supports_basic_mpp());
1036
1037                 let payment_params = PaymentParameters::from_node_id(invoice.recover_payee_pub_key())
1038                         .with_features(invoice.features().unwrap().clone())
1039                         .with_route_hints(invoice.route_hints());
1040                 let params = RouteParameters {
1041                         payment_params,
1042                         final_value_msat: invoice.amount_milli_satoshis().unwrap(),
1043                         final_cltv_expiry_delta: invoice.min_final_cltv_expiry() as u32,
1044                 };
1045                 let first_hops = nodes[0].node.list_usable_channels();
1046                 let network_graph = &node_cfgs[0].network_graph;
1047                 let logger = test_utils::TestLogger::new();
1048                 let scorer = test_utils::TestScorer::with_penalty(0);
1049                 let random_seed_bytes = chanmon_cfgs[1].keys_manager.get_secure_random_bytes();
1050                 let route = find_route(
1051                         &nodes[0].node.get_our_node_id(), &params, &network_graph,
1052                         Some(&first_hops.iter().collect::<Vec<_>>()), &logger, &scorer, &random_seed_bytes
1053                 ).unwrap();
1054                 let (payment_event, fwd_idx) = {
1055                         let mut payment_hash = PaymentHash([0; 32]);
1056                         payment_hash.0.copy_from_slice(&invoice.payment_hash().as_ref()[0..32]);
1057                         nodes[0].node.send_payment(&route, payment_hash, &Some(invoice.payment_secret().clone()), PaymentId(payment_hash.0)).unwrap();
1058                         let mut added_monitors = nodes[0].chain_monitor.added_monitors.lock().unwrap();
1059                         assert_eq!(added_monitors.len(), 1);
1060                         added_monitors.clear();
1061
1062                         let mut events = nodes[0].node.get_and_clear_pending_msg_events();
1063                         assert_eq!(events.len(), 1);
1064                         let fwd_idx = match events[0] {
1065                                 MessageSendEvent::UpdateHTLCs { node_id, .. } => {
1066                                         if node_id == nodes[1].node.get_our_node_id() {
1067                                                 1
1068                                         } else { 2 }
1069                                 },
1070                                 _ => panic!("Unexpected event")
1071                         };
1072                         (SendEvent::from_event(events.remove(0)), fwd_idx)
1073                 };
1074                 nodes[fwd_idx].node.handle_update_add_htlc(&nodes[0].node.get_our_node_id(), &payment_event.msgs[0]);
1075                 commitment_signed_dance!(nodes[fwd_idx], nodes[0], &payment_event.commitment_msg, false, true);
1076
1077                 // Note that we have to "forward pending HTLCs" twice before we see the PaymentReceived as
1078                 // this "emulates" the payment taking two hops, providing some privacy to make phantom node
1079                 // payments "look real" by taking more time.
1080                 expect_pending_htlcs_forwardable_ignore!(nodes[fwd_idx]);
1081                 nodes[fwd_idx].node.process_pending_htlc_forwards();
1082                 expect_pending_htlcs_forwardable_ignore!(nodes[fwd_idx]);
1083                 nodes[fwd_idx].node.process_pending_htlc_forwards();
1084
1085                 let payment_preimage_opt = if user_generated_pmt_hash { None } else { Some(payment_preimage) };
1086                 expect_payment_received!(&nodes[fwd_idx], payment_hash, payment_secret, payment_amt, payment_preimage_opt);
1087                 do_claim_payment_along_route(&nodes[0], &vec!(&vec!(&nodes[fwd_idx])[..]), false, payment_preimage);
1088                 let events = nodes[0].node.get_and_clear_pending_events();
1089                 assert_eq!(events.len(), 2);
1090                 match events[0] {
1091                         Event::PaymentSent { payment_preimage: ref ev_preimage, payment_hash: ref ev_hash, ref fee_paid_msat, .. } => {
1092                                 assert_eq!(payment_preimage, *ev_preimage);
1093                                 assert_eq!(payment_hash, *ev_hash);
1094                                 assert_eq!(fee_paid_msat, &Some(0));
1095                         },
1096                         _ => panic!("Unexpected event")
1097                 }
1098                 match events[1] {
1099                         Event::PaymentPathSuccessful { payment_hash: hash, .. } => {
1100                                 assert_eq!(hash, Some(payment_hash));
1101                         },
1102                         _ => panic!("Unexpected event")
1103                 }
1104         }
1105
1106         #[test]
1107         #[cfg(feature = "std")]
1108         fn test_multi_node_hints_has_htlc_min_max_values() {
1109                 let mut chanmon_cfgs = create_chanmon_cfgs(3);
1110                 let seed_1 = [42 as u8; 32];
1111                 let seed_2 = [43 as u8; 32];
1112                 let cross_node_seed = [44 as u8; 32];
1113                 chanmon_cfgs[1].keys_manager.backing = PhantomKeysManager::new(&seed_1, 43, 44, &cross_node_seed);
1114                 chanmon_cfgs[2].keys_manager.backing = PhantomKeysManager::new(&seed_2, 43, 44, &cross_node_seed);
1115                 let node_cfgs = create_node_cfgs(3, &chanmon_cfgs);
1116                 let node_chanmgrs = create_node_chanmgrs(3, &node_cfgs, &[None, None, None]);
1117                 let nodes = create_network(3, &node_cfgs, &node_chanmgrs);
1118
1119                 create_unannounced_chan_between_nodes_with_value(&nodes, 0, 1, 100000, 10001, channelmanager::provided_init_features(), channelmanager::provided_init_features());
1120                 create_unannounced_chan_between_nodes_with_value(&nodes, 0, 2, 100000, 10001, channelmanager::provided_init_features(), channelmanager::provided_init_features());
1121
1122                 let payment_amt = 20_000;
1123                 let (payment_hash, _payment_secret) = nodes[1].node.create_inbound_payment(Some(payment_amt), 3600).unwrap();
1124                 let route_hints = vec![
1125                         nodes[1].node.get_phantom_route_hints(),
1126                         nodes[2].node.get_phantom_route_hints(),
1127                 ];
1128
1129                 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();
1130
1131                 let chan_0_1 = &nodes[1].node.list_usable_channels()[0];
1132                 assert_eq!(invoice.route_hints()[0].0[0].htlc_minimum_msat, chan_0_1.inbound_htlc_minimum_msat);
1133                 assert_eq!(invoice.route_hints()[0].0[0].htlc_maximum_msat, chan_0_1.inbound_htlc_maximum_msat);
1134
1135                 let chan_0_2 = &nodes[2].node.list_usable_channels()[0];
1136                 assert_eq!(invoice.route_hints()[1].0[0].htlc_minimum_msat, chan_0_2.inbound_htlc_minimum_msat);
1137                 assert_eq!(invoice.route_hints()[1].0[0].htlc_maximum_msat, chan_0_2.inbound_htlc_maximum_msat);
1138         }
1139
1140         #[test]
1141         #[cfg(feature = "std")]
1142         fn create_phantom_invoice_with_description_hash() {
1143                 let chanmon_cfgs = create_chanmon_cfgs(3);
1144                 let node_cfgs = create_node_cfgs(3, &chanmon_cfgs);
1145                 let node_chanmgrs = create_node_chanmgrs(3, &node_cfgs, &[None, None, None]);
1146                 let nodes = create_network(3, &node_cfgs, &node_chanmgrs);
1147
1148                 let payment_amt = 20_000;
1149                 let route_hints = vec![
1150                         nodes[1].node.get_phantom_route_hints(),
1151                         nodes[2].node.get_phantom_route_hints(),
1152                 ];
1153
1154                 let description_hash = crate::Sha256(Hash::hash("Description hash phantom invoice".as_bytes()));
1155                 let non_default_invoice_expiry_secs = 4200;
1156                 let invoice = crate::utils::create_phantom_invoice_with_description_hash::<
1157                         &test_utils::TestKeysInterface, &test_utils::TestLogger,
1158                 >(
1159                         Some(payment_amt), None, non_default_invoice_expiry_secs, description_hash,
1160                         route_hints, &nodes[1].keys_manager, &nodes[1].logger, Currency::BitcoinTestnet
1161                 )
1162                 .unwrap();
1163                 assert_eq!(invoice.amount_pico_btc(), Some(200_000));
1164                 assert_eq!(invoice.min_final_cltv_expiry(), MIN_FINAL_CLTV_EXPIRY as u64);
1165                 assert_eq!(invoice.expiry_time(), Duration::from_secs(non_default_invoice_expiry_secs.into()));
1166                 assert_eq!(invoice.description(), InvoiceDescription::Hash(&crate::Sha256(Sha256::hash("Description hash phantom invoice".as_bytes()))));
1167         }
1168
1169         #[test]
1170         #[cfg(feature = "std")]
1171         fn test_multi_node_hints_includes_single_channels_to_participating_nodes() {
1172                 let mut chanmon_cfgs = create_chanmon_cfgs(3);
1173                 let seed_1 = [42 as u8; 32];
1174                 let seed_2 = [43 as u8; 32];
1175                 let cross_node_seed = [44 as u8; 32];
1176                 chanmon_cfgs[1].keys_manager.backing = PhantomKeysManager::new(&seed_1, 43, 44, &cross_node_seed);
1177                 chanmon_cfgs[2].keys_manager.backing = PhantomKeysManager::new(&seed_2, 43, 44, &cross_node_seed);
1178                 let node_cfgs = create_node_cfgs(3, &chanmon_cfgs);
1179                 let node_chanmgrs = create_node_chanmgrs(3, &node_cfgs, &[None, None, None]);
1180                 let nodes = create_network(3, &node_cfgs, &node_chanmgrs);
1181
1182                 let chan_0_1 = create_unannounced_chan_between_nodes_with_value(&nodes, 0, 1, 100000, 10001, channelmanager::provided_init_features(), channelmanager::provided_init_features());
1183                 let chan_0_2 = create_unannounced_chan_between_nodes_with_value(&nodes, 0, 2, 100000, 10001, channelmanager::provided_init_features(), channelmanager::provided_init_features());
1184
1185                 let mut scid_aliases = HashSet::new();
1186                 scid_aliases.insert(chan_0_1.0.short_channel_id_alias.unwrap());
1187                 scid_aliases.insert(chan_0_2.0.short_channel_id_alias.unwrap());
1188
1189                 match_multi_node_invoice_routes(
1190                         Some(10_000),
1191                         &nodes[1],
1192                         vec![&nodes[1], &nodes[2],],
1193                         scid_aliases,
1194                         false
1195                 );
1196         }
1197
1198         #[test]
1199         #[cfg(feature = "std")]
1200         fn test_multi_node_hints_includes_one_channel_of_each_counterparty_nodes_per_participating_node() {
1201                 let mut chanmon_cfgs = create_chanmon_cfgs(4);
1202                 let seed_1 = [42 as u8; 32];
1203                 let seed_2 = [43 as u8; 32];
1204                 let cross_node_seed = [44 as u8; 32];
1205                 chanmon_cfgs[2].keys_manager.backing = PhantomKeysManager::new(&seed_1, 43, 44, &cross_node_seed);
1206                 chanmon_cfgs[3].keys_manager.backing = PhantomKeysManager::new(&seed_2, 43, 44, &cross_node_seed);
1207                 let node_cfgs = create_node_cfgs(4, &chanmon_cfgs);
1208                 let node_chanmgrs = create_node_chanmgrs(4, &node_cfgs, &[None, None, None, None]);
1209                 let nodes = create_network(4, &node_cfgs, &node_chanmgrs);
1210
1211                 let chan_0_2 = create_unannounced_chan_between_nodes_with_value(&nodes, 0, 2, 100000, 10001, channelmanager::provided_init_features(), channelmanager::provided_init_features());
1212                 let chan_0_3 = create_unannounced_chan_between_nodes_with_value(&nodes, 0, 3, 1000000, 10001, channelmanager::provided_init_features(), channelmanager::provided_init_features());
1213                 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());
1214
1215                 let mut scid_aliases = HashSet::new();
1216                 scid_aliases.insert(chan_0_2.0.short_channel_id_alias.unwrap());
1217                 scid_aliases.insert(chan_0_3.0.short_channel_id_alias.unwrap());
1218                 scid_aliases.insert(chan_1_3.0.short_channel_id_alias.unwrap());
1219
1220                 match_multi_node_invoice_routes(
1221                         Some(10_000),
1222                         &nodes[2],
1223                         vec![&nodes[2], &nodes[3],],
1224                         scid_aliases,
1225                         false
1226                 );
1227         }
1228
1229         #[test]
1230         #[cfg(feature = "std")]
1231         fn test_multi_node_forwarding_info_not_assigned_channel_excluded_from_hints() {
1232                 let mut chanmon_cfgs = create_chanmon_cfgs(4);
1233                 let seed_1 = [42 as u8; 32];
1234                 let seed_2 = [43 as u8; 32];
1235                 let cross_node_seed = [44 as u8; 32];
1236                 chanmon_cfgs[2].keys_manager.backing = PhantomKeysManager::new(&seed_1, 43, 44, &cross_node_seed);
1237                 chanmon_cfgs[3].keys_manager.backing = PhantomKeysManager::new(&seed_2, 43, 44, &cross_node_seed);
1238                 let node_cfgs = create_node_cfgs(4, &chanmon_cfgs);
1239                 let node_chanmgrs = create_node_chanmgrs(4, &node_cfgs, &[None, None, None, None]);
1240                 let nodes = create_network(4, &node_cfgs, &node_chanmgrs);
1241
1242                 let chan_0_2 = create_unannounced_chan_between_nodes_with_value(&nodes, 0, 2, 100000, 10001, channelmanager::provided_init_features(), channelmanager::provided_init_features());
1243                 let chan_0_3 = create_unannounced_chan_between_nodes_with_value(&nodes, 0, 3, 1000000, 10001, channelmanager::provided_init_features(), channelmanager::provided_init_features());
1244
1245                 // Create an unannonced channel between `nodes[1]` and `nodes[3]`, for which the
1246                 // `msgs::ChannelUpdate` is never handled for the node(s). As the `msgs::ChannelUpdate`
1247                 // is never handled, the `channel.counterparty.forwarding_info` is never assigned.
1248                 let mut private_chan_cfg = UserConfig::default();
1249                 private_chan_cfg.channel_handshake_config.announced_channel = false;
1250                 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();
1251                 let open_channel = get_event_msg!(nodes[1], MessageSendEvent::SendOpenChannel, nodes[3].node.get_our_node_id());
1252                 nodes[3].node.handle_open_channel(&nodes[1].node.get_our_node_id(), channelmanager::provided_init_features(), &open_channel);
1253                 let accept_channel = get_event_msg!(nodes[3], MessageSendEvent::SendAcceptChannel, nodes[1].node.get_our_node_id());
1254                 nodes[1].node.handle_accept_channel(&nodes[3].node.get_our_node_id(), channelmanager::provided_init_features(), &accept_channel);
1255
1256                 let tx = sign_funding_transaction(&nodes[1], &nodes[3], 1_000_000, temporary_channel_id);
1257
1258                 let conf_height = core::cmp::max(nodes[1].best_block_info().1 + 1, nodes[3].best_block_info().1 + 1);
1259                 confirm_transaction_at(&nodes[1], &tx, conf_height);
1260                 connect_blocks(&nodes[1], CHAN_CONFIRM_DEPTH - 1);
1261                 confirm_transaction_at(&nodes[3], &tx, conf_height);
1262                 connect_blocks(&nodes[3], CHAN_CONFIRM_DEPTH - 1);
1263                 let as_channel_ready = get_event_msg!(nodes[1], MessageSendEvent::SendChannelReady, nodes[3].node.get_our_node_id());
1264                 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()));
1265                 get_event_msg!(nodes[1], MessageSendEvent::SendChannelUpdate, nodes[3].node.get_our_node_id());
1266                 nodes[3].node.handle_channel_ready(&nodes[1].node.get_our_node_id(), &as_channel_ready);
1267                 get_event_msg!(nodes[3], MessageSendEvent::SendChannelUpdate, nodes[1].node.get_our_node_id());
1268                 expect_channel_ready_event(&nodes[1], &nodes[3].node.get_our_node_id());
1269                 expect_channel_ready_event(&nodes[3], &nodes[1].node.get_our_node_id());
1270
1271                 // As `msgs::ChannelUpdate` was never handled for the participating node(s) of the third
1272                 // channel, the channel will never be assigned any `counterparty.forwarding_info`.
1273                 // Therefore only `chan_0_3` should be included in the hints for `nodes[3]`.
1274                 let mut scid_aliases = HashSet::new();
1275                 scid_aliases.insert(chan_0_2.0.short_channel_id_alias.unwrap());
1276                 scid_aliases.insert(chan_0_3.0.short_channel_id_alias.unwrap());
1277
1278                 match_multi_node_invoice_routes(
1279                         Some(10_000),
1280                         &nodes[2],
1281                         vec![&nodes[2], &nodes[3],],
1282                         scid_aliases,
1283                         false
1284                 );
1285         }
1286
1287         #[test]
1288         #[cfg(feature = "std")]
1289         fn test_multi_node_with_only_public_channels_hints_includes_only_phantom_route() {
1290                 let mut chanmon_cfgs = create_chanmon_cfgs(3);
1291                 let seed_1 = [42 as u8; 32];
1292                 let seed_2 = [43 as u8; 32];
1293                 let cross_node_seed = [44 as u8; 32];
1294                 chanmon_cfgs[1].keys_manager.backing = PhantomKeysManager::new(&seed_1, 43, 44, &cross_node_seed);
1295                 chanmon_cfgs[2].keys_manager.backing = PhantomKeysManager::new(&seed_2, 43, 44, &cross_node_seed);
1296                 let node_cfgs = create_node_cfgs(3, &chanmon_cfgs);
1297                 let node_chanmgrs = create_node_chanmgrs(3, &node_cfgs, &[None, None, None]);
1298                 let nodes = create_network(3, &node_cfgs, &node_chanmgrs);
1299
1300                 let chan_0_1 = create_unannounced_chan_between_nodes_with_value(&nodes, 0, 1, 100000, 10001, channelmanager::provided_init_features(), channelmanager::provided_init_features());
1301
1302                 let chan_2_0 = create_announced_chan_between_nodes_with_value(&nodes, 2, 0, 100000, 10001, channelmanager::provided_init_features(), channelmanager::provided_init_features());
1303                 nodes[2].node.handle_channel_update(&nodes[0].node.get_our_node_id(), &chan_2_0.1);
1304                 nodes[0].node.handle_channel_update(&nodes[2].node.get_our_node_id(), &chan_2_0.0);
1305
1306                 // Hints should include `chan_0_1` from as `nodes[1]` only have private channels, but not
1307                 // `chan_0_2` as `nodes[2]` only has public channels.
1308                 let mut scid_aliases = HashSet::new();
1309                 scid_aliases.insert(chan_0_1.0.short_channel_id_alias.unwrap());
1310
1311                 match_multi_node_invoice_routes(
1312                         Some(10_000),
1313                         &nodes[1],
1314                         vec![&nodes[1], &nodes[2],],
1315                         scid_aliases,
1316                         true
1317                 );
1318         }
1319
1320         #[test]
1321         #[cfg(feature = "std")]
1322         fn test_multi_node_with_mixed_public_and_private_channel_hints_includes_only_phantom_route() {
1323                 let mut chanmon_cfgs = create_chanmon_cfgs(4);
1324                 let seed_1 = [42 as u8; 32];
1325                 let seed_2 = [43 as u8; 32];
1326                 let cross_node_seed = [44 as u8; 32];
1327                 chanmon_cfgs[1].keys_manager.backing = PhantomKeysManager::new(&seed_1, 43, 44, &cross_node_seed);
1328                 chanmon_cfgs[2].keys_manager.backing = PhantomKeysManager::new(&seed_2, 43, 44, &cross_node_seed);
1329                 let node_cfgs = create_node_cfgs(4, &chanmon_cfgs);
1330                 let node_chanmgrs = create_node_chanmgrs(4, &node_cfgs, &[None, None, None, None]);
1331                 let nodes = create_network(4, &node_cfgs, &node_chanmgrs);
1332
1333                 let chan_0_2 = create_announced_chan_between_nodes_with_value(&nodes, 0, 2, 100000, 10001, channelmanager::provided_init_features(), channelmanager::provided_init_features());
1334                 nodes[0].node.handle_channel_update(&nodes[2].node.get_our_node_id(), &chan_0_2.1);
1335                 nodes[2].node.handle_channel_update(&nodes[0].node.get_our_node_id(), &chan_0_2.0);
1336                 let _chan_1_2 = create_unannounced_chan_between_nodes_with_value(&nodes, 1, 2, 100000, 10001, channelmanager::provided_init_features(), channelmanager::provided_init_features());
1337
1338                 let chan_0_3 = create_unannounced_chan_between_nodes_with_value(&nodes, 0, 3, 100000, 10001, channelmanager::provided_init_features(), channelmanager::provided_init_features());
1339
1340                 // Hints should include `chan_0_3` from as `nodes[3]` only have private channels, and no
1341                 // channels for `nodes[2]` as it contains a mix of public and private channels.
1342                 let mut scid_aliases = HashSet::new();
1343                 scid_aliases.insert(chan_0_3.0.short_channel_id_alias.unwrap());
1344
1345                 match_multi_node_invoice_routes(
1346                         Some(10_000),
1347                         &nodes[2],
1348                         vec![&nodes[2], &nodes[3],],
1349                         scid_aliases,
1350                         true
1351                 );
1352         }
1353
1354         #[test]
1355         #[cfg(feature = "std")]
1356         fn test_multi_node_hints_has_only_highest_inbound_capacity_channel() {
1357                 let mut chanmon_cfgs = create_chanmon_cfgs(3);
1358                 let seed_1 = [42 as u8; 32];
1359                 let seed_2 = [43 as u8; 32];
1360                 let cross_node_seed = [44 as u8; 32];
1361                 chanmon_cfgs[1].keys_manager.backing = PhantomKeysManager::new(&seed_1, 43, 44, &cross_node_seed);
1362                 chanmon_cfgs[2].keys_manager.backing = PhantomKeysManager::new(&seed_2, 43, 44, &cross_node_seed);
1363                 let node_cfgs = create_node_cfgs(3, &chanmon_cfgs);
1364                 let node_chanmgrs = create_node_chanmgrs(3, &node_cfgs, &[None, None, None]);
1365                 let nodes = create_network(3, &node_cfgs, &node_chanmgrs);
1366
1367                 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());
1368                 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());
1369                 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());
1370                 let chan_0_2 = create_unannounced_chan_between_nodes_with_value(&nodes, 0, 2, 100000, 10001, channelmanager::provided_init_features(), channelmanager::provided_init_features());
1371
1372                 let mut scid_aliases = HashSet::new();
1373                 scid_aliases.insert(chan_0_1_high_inbound_capacity.0.short_channel_id_alias.unwrap());
1374                 scid_aliases.insert(chan_0_2.0.short_channel_id_alias.unwrap());
1375
1376                 match_multi_node_invoice_routes(
1377                         Some(10_000),
1378                         &nodes[1],
1379                         vec![&nodes[1], &nodes[2],],
1380                         scid_aliases,
1381                         false
1382                 );
1383         }
1384
1385         #[test]
1386         #[cfg(feature = "std")]
1387         fn test_multi_node_channels_inbound_capacity_lower_than_invoice_amt_filtering() {
1388                 let mut chanmon_cfgs = create_chanmon_cfgs(4);
1389                 let seed_1 = [42 as u8; 32];
1390                 let seed_2 = [43 as u8; 32];
1391                 let cross_node_seed = [44 as u8; 32];
1392                 chanmon_cfgs[1].keys_manager.backing = PhantomKeysManager::new(&seed_1, 43, 44, &cross_node_seed);
1393                 chanmon_cfgs[2].keys_manager.backing = PhantomKeysManager::new(&seed_2, 43, 44, &cross_node_seed);
1394                 let node_cfgs = create_node_cfgs(4, &chanmon_cfgs);
1395                 let node_chanmgrs = create_node_chanmgrs(4, &node_cfgs, &[None, None, None, None]);
1396                 let nodes = create_network(4, &node_cfgs, &node_chanmgrs);
1397
1398                 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());
1399                 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());
1400                 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());
1401
1402                 // Since the invoice 1 msat above chan_0_3's inbound capacity, it should be filtered out.
1403                 let mut scid_aliases_99_000_001_msat = HashSet::new();
1404                 scid_aliases_99_000_001_msat.insert(chan_0_2.0.short_channel_id_alias.unwrap());
1405                 scid_aliases_99_000_001_msat.insert(chan_1_3.0.short_channel_id_alias.unwrap());
1406
1407                 match_multi_node_invoice_routes(
1408                         Some(99_000_001),
1409                         &nodes[2],
1410                         vec![&nodes[2], &nodes[3],],
1411                         scid_aliases_99_000_001_msat,
1412                         false
1413                 );
1414
1415                 // Since the invoice is exactly at chan_0_3's inbound capacity, it should be included.
1416                 let mut scid_aliases_99_000_000_msat = HashSet::new();
1417                 scid_aliases_99_000_000_msat.insert(chan_0_2.0.short_channel_id_alias.unwrap());
1418                 scid_aliases_99_000_000_msat.insert(chan_0_3.0.short_channel_id_alias.unwrap());
1419                 scid_aliases_99_000_000_msat.insert(chan_1_3.0.short_channel_id_alias.unwrap());
1420
1421                 match_multi_node_invoice_routes(
1422                         Some(99_000_000),
1423                         &nodes[2],
1424                         vec![&nodes[2], &nodes[3],],
1425                         scid_aliases_99_000_000_msat,
1426                         false
1427                 );
1428
1429                 // Since the invoice is above all of `nodes[2]` channels' inbound capacity, all of
1430                 // `nodes[2]` them should be included.
1431                 let mut scid_aliases_300_000_000_msat = HashSet::new();
1432                 scid_aliases_300_000_000_msat.insert(chan_0_2.0.short_channel_id_alias.unwrap());
1433                 scid_aliases_300_000_000_msat.insert(chan_0_3.0.short_channel_id_alias.unwrap());
1434                 scid_aliases_300_000_000_msat.insert(chan_1_3.0.short_channel_id_alias.unwrap());
1435
1436                 match_multi_node_invoice_routes(
1437                         Some(300_000_000),
1438                         &nodes[2],
1439                         vec![&nodes[2], &nodes[3],],
1440                         scid_aliases_300_000_000_msat,
1441                         false
1442                 );
1443
1444                 // Since the no specified amount, all channels should included.
1445                 let mut scid_aliases_no_specified_amount = HashSet::new();
1446                 scid_aliases_no_specified_amount.insert(chan_0_2.0.short_channel_id_alias.unwrap());
1447                 scid_aliases_no_specified_amount.insert(chan_0_3.0.short_channel_id_alias.unwrap());
1448                 scid_aliases_no_specified_amount.insert(chan_1_3.0.short_channel_id_alias.unwrap());
1449
1450                 match_multi_node_invoice_routes(
1451                         None,
1452                         &nodes[2],
1453                         vec![&nodes[2], &nodes[3],],
1454                         scid_aliases_no_specified_amount,
1455                         false
1456                 );
1457         }
1458
1459         #[cfg(feature = "std")]
1460         fn match_multi_node_invoice_routes<'a, 'b: 'a, 'c: 'b>(
1461                 invoice_amt: Option<u64>,
1462                 invoice_node: &Node<'a, 'b, 'c>,
1463                 network_multi_nodes: Vec<&Node<'a, 'b, 'c>>,
1464                 mut chan_ids_to_match: HashSet<u64>,
1465                 nodes_contains_public_channels: bool
1466         ){
1467                 let phantom_route_hints = network_multi_nodes.iter()
1468                         .map(|node| node.node.get_phantom_route_hints())
1469                         .collect::<Vec<PhantomRouteHints>>();
1470                 let phantom_scids = phantom_route_hints.iter()
1471                         .map(|route_hint| route_hint.phantom_scid)
1472                         .collect::<HashSet<u64>>();
1473
1474                 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();
1475
1476                 let invoice_hints = invoice.private_routes();
1477
1478                 for hint in invoice_hints {
1479                         let hints = &(hint.0).0;
1480                         match hints.len() {
1481                                 1 => {
1482                                         assert!(nodes_contains_public_channels);
1483                                         let phantom_scid = hints[0].short_channel_id;
1484                                         assert!(phantom_scids.contains(&phantom_scid));
1485                                 },
1486                                 2 => {
1487                                         let hint_short_chan_id = hints[0].short_channel_id;
1488                                         assert!(chan_ids_to_match.remove(&hint_short_chan_id));
1489                                         let phantom_scid = hints[1].short_channel_id;
1490                                         assert!(phantom_scids.contains(&phantom_scid));
1491                                 },
1492                                 _ => panic!("Incorrect hint length generated")
1493                         }
1494                 }
1495                 assert!(chan_ids_to_match.is_empty(), "Unmatched short channel ids: {:?}", chan_ids_to_match);
1496         }
1497 }