1 //! Convenient utilities to create an invoice.
3 use {CreationError, Currency, DEFAULT_EXPIRY_TIME, Invoice, InvoiceBuilder, SignOrCreationError};
4 use payment::{Payer, Router};
6 use crate::{prelude::*, Description, InvoiceDescription, Sha256};
8 use bitcoin_hashes::{Hash, sha256};
10 use lightning::chain::chaininterface::{BroadcasterInterface, FeeEstimator};
11 use lightning::chain::keysinterface::{Recipient, KeysInterface, Sign};
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::msgs::LightningError;
17 use lightning::routing::scoring::Score;
18 use lightning::routing::network_graph::{NetworkGraph, RoutingFees};
19 use lightning::routing::router::{Route, RouteHint, RouteHintHop, RouteParameters, find_route};
20 use lightning::util::logger::Logger;
21 use secp256k1::key::PublicKey;
22 use core::convert::TryInto;
24 use core::time::Duration;
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.
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
41 /// `payment_hash` and `payment_secret` can come from [`ChannelManager::create_inbound_payment`] or
42 /// [`ChannelManager::create_inbound_payment_for_hash`]. These values can be retrieved from any
43 /// participating node. Alternatively, [`inbound_payment::create`] or
44 /// [`inbound_payment::create_from_hash`] may be used to retrieve these values without a
47 /// Note that the provided `keys_manager`'s `KeysInterface` implementation must support phantom
48 /// invoices in its `sign_invoice` implementation ([`PhantomKeysManager`] satisfies this
51 /// [`PhantomKeysManager`]: lightning::chain::keysinterface::PhantomKeysManager
52 /// [`ChannelManager::get_phantom_route_hints`]: lightning::ln::channelmanager::ChannelManager::get_phantom_route_hints
53 /// [`inbound_payment::create`]: lightning::ln::inbound_payment::create
54 /// [`inbound_payment::create_from_hash`]: lightning::ln::inbound_payment::create_from_hash
55 /// [`PhantomRouteHints::channels`]: lightning::ln::channelmanager::PhantomRouteHints::channels
56 pub fn create_phantom_invoice<Signer: Sign, K: Deref>(
57 amt_msat: Option<u64>, description: String, payment_hash: PaymentHash, payment_secret: PaymentSecret,
58 phantom_route_hints: Vec<PhantomRouteHints>, keys_manager: K, network: Currency,
59 ) -> Result<Invoice, SignOrCreationError<()>> where K::Target: KeysInterface {
60 let description = Description::new(description).map_err(SignOrCreationError::CreationError)?;
61 let description = InvoiceDescription::Direct(&description,);
62 _create_phantom_invoice::<Signer, K>(
63 amt_msat, description, payment_hash, payment_secret, phantom_route_hints, keys_manager, network,
67 #[cfg(feature = "std")]
68 /// Utility to create an invoice that can be paid to one of multiple nodes, or a "phantom invoice."
69 /// See [`PhantomKeysManager`] for more information on phantom node payments.
71 /// `phantom_route_hints` parameter:
72 /// * Contains channel info for all nodes participating in the phantom invoice
73 /// * Entries are retrieved from a call to [`ChannelManager::get_phantom_route_hints`] on each
74 /// participating node
75 /// * It is fine to cache `phantom_route_hints` and reuse it across invoices, as long as the data is
76 /// updated when a channel becomes disabled or closes
77 /// * Note that if too many channels are included in [`PhantomRouteHints::channels`], the invoice
78 /// may be too long for QR code scanning. To fix this, `PhantomRouteHints::channels` may be pared
81 /// `description_hash` is a SHA-256 hash of the description text
83 /// `payment_hash` and `payment_secret` can come from [`ChannelManager::create_inbound_payment`] or
84 /// [`ChannelManager::create_inbound_payment_for_hash`]. These values can be retrieved from any
85 /// participating node. Alternatively, [`inbound_payment::create`] or
86 /// [`inbound_payment::create_from_hash`] may be used to retrieve these values without a
89 /// Note that the provided `keys_manager`'s `KeysInterface` implementation must support phantom
90 /// invoices in its `sign_invoice` implementation ([`PhantomKeysManager`] satisfies this
93 /// [`PhantomKeysManager`]: lightning::chain::keysinterface::PhantomKeysManager
94 /// [`ChannelManager::get_phantom_route_hints`]: lightning::ln::channelmanager::ChannelManager::get_phantom_route_hints
95 /// [`inbound_payment::create`]: lightning::ln::inbound_payment::create
96 /// [`inbound_payment::create_from_hash`]: lightning::ln::inbound_payment::create_from_hash
97 /// [`PhantomRouteHints::channels`]: lightning::ln::channelmanager::PhantomRouteHints::channels
98 pub fn create_phantom_invoice_with_description_hash<Signer: Sign, K: Deref>(
99 amt_msat: Option<u64>, description_hash: Sha256, payment_hash: PaymentHash,
100 payment_secret: PaymentSecret, phantom_route_hints: Vec<PhantomRouteHints>,
101 keys_manager: K, network: Currency,
102 ) -> Result<Invoice, SignOrCreationError<()>> where K::Target: KeysInterface
105 _create_phantom_invoice::<Signer, K>(
107 InvoiceDescription::Hash(&description_hash),
108 payment_hash, payment_secret, phantom_route_hints, keys_manager, network,
112 #[cfg(feature = "std")]
113 fn _create_phantom_invoice<Signer: Sign, K: Deref>(
114 amt_msat: Option<u64>, description: InvoiceDescription, payment_hash: PaymentHash,
115 payment_secret: PaymentSecret, phantom_route_hints: Vec<PhantomRouteHints>,
116 keys_manager: K, network: Currency,
117 ) -> Result<Invoice, SignOrCreationError<()>> where K::Target: KeysInterface
119 if phantom_route_hints.len() == 0 {
120 return Err(SignOrCreationError::CreationError(
121 CreationError::MissingRouteHints,
124 let invoice = match description {
125 InvoiceDescription::Direct(description) => {
126 InvoiceBuilder::new(network).description(description.0.clone())
128 InvoiceDescription::Hash(hash) => InvoiceBuilder::new(network).description_hash(hash.0),
131 let mut invoice = invoice
133 .payment_hash(Hash::from_slice(&payment_hash.0).unwrap())
134 .payment_secret(payment_secret)
135 .min_final_cltv_expiry(MIN_FINAL_CLTV_EXPIRY.into());
136 if let Some(amt) = amt_msat {
137 invoice = invoice.amount_milli_satoshis(amt);
140 for PhantomRouteHints { channels, phantom_scid, real_node_pubkey } in phantom_route_hints {
141 let mut route_hints = filter_channels(channels, amt_msat);
143 // If we have any public channel, the route hints from `filter_channels` will be empty.
144 // In that case we create a RouteHint on which we will push a single hop with the phantom
145 // route into the invoice, and let the sender find the path to the `real_node_pubkey`
146 // node by looking at our public channels.
147 if route_hints.is_empty() {
148 route_hints.push(RouteHint(vec![]))
150 for mut route_hint in route_hints {
151 route_hint.0.push(RouteHintHop {
152 src_node_id: real_node_pubkey,
153 short_channel_id: phantom_scid,
156 proportional_millionths: 0,
158 cltv_expiry_delta: MIN_CLTV_EXPIRY_DELTA,
159 htlc_minimum_msat: None,
160 htlc_maximum_msat: None,});
161 invoice = invoice.private_route(route_hint.clone());
165 let raw_invoice = match invoice.build_raw() {
167 Err(e) => return Err(SignOrCreationError::CreationError(e))
169 let hrp_str = raw_invoice.hrp.to_string();
170 let hrp_bytes = hrp_str.as_bytes();
171 let data_without_signature = raw_invoice.data.to_base32();
172 let signed_raw_invoice = raw_invoice.sign(|_| keys_manager.sign_invoice(hrp_bytes, &data_without_signature, Recipient::PhantomNode));
173 match signed_raw_invoice {
174 Ok(inv) => Ok(Invoice::from_signed(inv).unwrap()),
175 Err(e) => Err(SignOrCreationError::SignError(e))
179 #[cfg(feature = "std")]
180 /// Utility to construct an invoice. Generally, unless you want to do something like a custom
181 /// cltv_expiry, this is what you should be using to create an invoice. The reason being, this
182 /// method stores the invoice's payment secret and preimage in `ChannelManager`, so (a) the user
183 /// doesn't have to store preimage/payment secret information and (b) `ChannelManager` can verify
184 /// that the payment secret is valid when the invoice is paid.
185 pub fn create_invoice_from_channelmanager<Signer: Sign, M: Deref, T: Deref, K: Deref, F: Deref, L: Deref>(
186 channelmanager: &ChannelManager<Signer, M, T, K, F, L>, keys_manager: K, network: Currency,
187 amt_msat: Option<u64>, description: String
188 ) -> Result<Invoice, SignOrCreationError<()>>
190 M::Target: chain::Watch<Signer>,
191 T::Target: BroadcasterInterface,
192 K::Target: KeysInterface<Signer = Signer>,
193 F::Target: FeeEstimator,
196 use std::time::SystemTime;
197 let duration = SystemTime::now().duration_since(SystemTime::UNIX_EPOCH)
198 .expect("for the foreseeable future this shouldn't happen");
199 create_invoice_from_channelmanager_and_duration_since_epoch(
200 channelmanager, keys_manager, network, amt_msat, description, duration
204 #[cfg(feature = "std")]
205 /// Utility to construct an invoice. Generally, unless you want to do something like a custom
206 /// cltv_expiry, this is what you should be using to create an invoice. The reason being, this
207 /// method stores the invoice's payment secret and preimage in `ChannelManager`, so (a) the user
208 /// doesn't have to store preimage/payment secret information and (b) `ChannelManager` can verify
209 /// that the payment secret is valid when the invoice is paid.
210 /// Use this variant if you want to pass the `description_hash` to the invoice.
211 pub fn create_invoice_from_channelmanager_with_description_hash<Signer: Sign, M: Deref, T: Deref, K: Deref, F: Deref, L: Deref>(
212 channelmanager: &ChannelManager<Signer, M, T, K, F, L>, keys_manager: K, network: Currency,
213 amt_msat: Option<u64>, description_hash: Sha256,
214 ) -> Result<Invoice, SignOrCreationError<()>>
216 M::Target: chain::Watch<Signer>,
217 T::Target: BroadcasterInterface,
218 K::Target: KeysInterface<Signer = Signer>,
219 F::Target: FeeEstimator,
222 use std::time::SystemTime;
224 let duration = SystemTime::now()
225 .duration_since(SystemTime::UNIX_EPOCH)
226 .expect("for the foreseeable future this shouldn't happen");
228 create_invoice_from_channelmanager_with_description_hash_and_duration_since_epoch(
229 channelmanager, keys_manager, network, amt_msat, description_hash, duration,
233 /// See [`create_invoice_from_channelmanager_with_description_hash`]
234 /// This version can be used in a `no_std` environment, where [`std::time::SystemTime`] is not
235 /// available and the current time is supplied by the caller.
236 pub fn create_invoice_from_channelmanager_with_description_hash_and_duration_since_epoch<Signer: Sign, M: Deref, T: Deref, K: Deref, F: Deref, L: Deref>(
237 channelmanager: &ChannelManager<Signer, M, T, K, F, L>, keys_manager: K, network: Currency,
238 amt_msat: Option<u64>, description_hash: Sha256, duration_since_epoch: Duration,
239 ) -> Result<Invoice, SignOrCreationError<()>>
241 M::Target: chain::Watch<Signer>,
242 T::Target: BroadcasterInterface,
243 K::Target: KeysInterface<Signer = Signer>,
244 F::Target: FeeEstimator,
247 _create_invoice_from_channelmanager_and_duration_since_epoch(
248 channelmanager, keys_manager, network, amt_msat,
249 InvoiceDescription::Hash(&description_hash),
250 duration_since_epoch,
254 /// See [`create_invoice_from_channelmanager`]
255 /// This version can be used in a `no_std` environment, where [`std::time::SystemTime`] is not
256 /// available and the current time is supplied by the caller.
257 pub fn create_invoice_from_channelmanager_and_duration_since_epoch<Signer: Sign, M: Deref, T: Deref, K: Deref, F: Deref, L: Deref>(
258 channelmanager: &ChannelManager<Signer, M, T, K, F, L>, keys_manager: K, network: Currency,
259 amt_msat: Option<u64>, description: String, duration_since_epoch: Duration,
260 ) -> Result<Invoice, SignOrCreationError<()>>
262 M::Target: chain::Watch<Signer>,
263 T::Target: BroadcasterInterface,
264 K::Target: KeysInterface<Signer = Signer>,
265 F::Target: FeeEstimator,
268 _create_invoice_from_channelmanager_and_duration_since_epoch(
269 channelmanager, keys_manager, network, amt_msat,
270 InvoiceDescription::Direct(
271 &Description::new(description).map_err(SignOrCreationError::CreationError)?,
273 duration_since_epoch,
277 fn _create_invoice_from_channelmanager_and_duration_since_epoch<Signer: Sign, M: Deref, T: Deref, K: Deref, F: Deref, L: Deref>(
278 channelmanager: &ChannelManager<Signer, M, T, K, F, L>, keys_manager: K, network: Currency,
279 amt_msat: Option<u64>, description: InvoiceDescription, duration_since_epoch: Duration,
280 ) -> Result<Invoice, SignOrCreationError<()>>
282 M::Target: chain::Watch<Signer>,
283 T::Target: BroadcasterInterface,
284 K::Target: KeysInterface<Signer = Signer>,
285 F::Target: FeeEstimator,
288 let route_hints = filter_channels(channelmanager.list_usable_channels(), amt_msat);
290 // `create_inbound_payment` only returns an error if the amount is greater than the total bitcoin
292 let (payment_hash, payment_secret) = channelmanager
293 .create_inbound_payment(amt_msat, DEFAULT_EXPIRY_TIME.try_into().unwrap())
294 .map_err(|()| SignOrCreationError::CreationError(CreationError::InvalidAmount))?;
295 let our_node_pubkey = channelmanager.get_our_node_id();
297 let invoice = match description {
298 InvoiceDescription::Direct(description) => {
299 InvoiceBuilder::new(network).description(description.0.clone())
301 InvoiceDescription::Hash(hash) => InvoiceBuilder::new(network).description_hash(hash.0),
304 let mut invoice = invoice
305 .duration_since_epoch(duration_since_epoch)
306 .payee_pub_key(our_node_pubkey)
307 .payment_hash(Hash::from_slice(&payment_hash.0).unwrap())
308 .payment_secret(payment_secret)
310 .min_final_cltv_expiry(MIN_FINAL_CLTV_EXPIRY.into());
311 if let Some(amt) = amt_msat {
312 invoice = invoice.amount_milli_satoshis(amt);
314 for hint in route_hints {
315 invoice = invoice.private_route(hint);
318 let raw_invoice = match invoice.build_raw() {
320 Err(e) => return Err(SignOrCreationError::CreationError(e))
322 let hrp_str = raw_invoice.hrp.to_string();
323 let hrp_bytes = hrp_str.as_bytes();
324 let data_without_signature = raw_invoice.data.to_base32();
325 let signed_raw_invoice = raw_invoice.sign(|_| keys_manager.sign_invoice(hrp_bytes, &data_without_signature, Recipient::Node));
326 match signed_raw_invoice {
327 Ok(inv) => Ok(Invoice::from_signed(inv).unwrap()),
328 Err(e) => Err(SignOrCreationError::SignError(e))
332 /// Filters the `channels` for an invoice, and returns the corresponding `RouteHint`s to include
335 /// The filtering is based on the following criteria:
336 /// * Only one channel per counterparty node
337 /// * Always select the channel with the highest inbound capacity per counterparty node
338 /// * Filter out channels with a lower inbound capacity than `min_inbound_capacity_msat`, if any
339 /// channel with a higher or equal inbound capacity than `min_inbound_capacity_msat` exists
340 /// * If any public channel exists, the returned `RouteHint`s will be empty, and the sender will
341 /// need to find the path by looking at the public channels instead
342 fn filter_channels(channels: Vec<ChannelDetails>, min_inbound_capacity_msat: Option<u64>) -> Vec<RouteHint>{
343 let mut filtered_channels: HashMap<PublicKey, &ChannelDetails> = HashMap::new();
344 let min_inbound_capacity = min_inbound_capacity_msat.unwrap_or(0);
345 let mut min_capacity_channel_exists = false;
347 for channel in channels.iter() {
348 if channel.get_inbound_payment_scid().is_none() || channel.counterparty.forwarding_info.is_none() {
352 if channel.is_public {
353 // If any public channel exists, return no hints and let the sender
354 // look at the public channels instead.
358 if channel.inbound_capacity_msat >= min_inbound_capacity {
359 min_capacity_channel_exists = true;
361 match filtered_channels.entry(channel.counterparty.node_id) {
362 hash_map::Entry::Occupied(mut entry) => {
363 let current_max_capacity = entry.get().inbound_capacity_msat;
364 if channel.inbound_capacity_msat < current_max_capacity {
367 entry.insert(channel);
369 hash_map::Entry::Vacant(entry) => {
370 entry.insert(channel);
375 let route_hint_from_channel = |channel: &ChannelDetails| {
376 let forwarding_info = channel.counterparty.forwarding_info.as_ref().unwrap();
377 RouteHint(vec![RouteHintHop {
378 src_node_id: channel.counterparty.node_id,
379 short_channel_id: channel.get_inbound_payment_scid().unwrap(),
381 base_msat: forwarding_info.fee_base_msat,
382 proportional_millionths: forwarding_info.fee_proportional_millionths,
384 cltv_expiry_delta: forwarding_info.cltv_expiry_delta,
385 htlc_minimum_msat: None,
386 htlc_maximum_msat: None,}])
388 // If all channels are private, return the route hint for the highest inbound capacity channel
389 // per counterparty node. If channels with an higher inbound capacity than the
390 // min_inbound_capacity exists, filter out the channels with a lower capacity than that.
391 filtered_channels.into_iter()
392 .filter(|(_counterparty_id, channel)| {
393 min_capacity_channel_exists && channel.inbound_capacity_msat >= min_inbound_capacity ||
394 !min_capacity_channel_exists
396 .map(|(_counterparty_id, channel)| route_hint_from_channel(&channel))
397 .collect::<Vec<RouteHint>>()
400 /// A [`Router`] implemented using [`find_route`].
401 pub struct DefaultRouter<G: Deref<Target = NetworkGraph>, L: Deref> where L::Target: Logger {
404 random_seed_bytes: Mutex<[u8; 32]>,
407 impl<G: Deref<Target = NetworkGraph>, L: Deref> DefaultRouter<G, L> where L::Target: Logger {
408 /// Creates a new router using the given [`NetworkGraph`], a [`Logger`], and a randomness source
409 /// `random_seed_bytes`.
410 pub fn new(network_graph: G, logger: L, random_seed_bytes: [u8; 32]) -> Self {
411 let random_seed_bytes = Mutex::new(random_seed_bytes);
412 Self { network_graph, logger, random_seed_bytes }
416 impl<G: Deref<Target = NetworkGraph>, L: Deref, S: Score> Router<S> for DefaultRouter<G, L>
417 where L::Target: Logger {
419 &self, payer: &PublicKey, params: &RouteParameters, _payment_hash: &PaymentHash,
420 first_hops: Option<&[&ChannelDetails]>, scorer: &S
421 ) -> Result<Route, LightningError> {
422 let random_seed_bytes = {
423 let mut locked_random_seed_bytes = self.random_seed_bytes.lock().unwrap();
424 *locked_random_seed_bytes = sha256::Hash::hash(&*locked_random_seed_bytes).into_inner();
425 *locked_random_seed_bytes
427 find_route(payer, params, &*self.network_graph, first_hops, &*self.logger, scorer, &random_seed_bytes)
431 impl<Signer: Sign, M: Deref, T: Deref, K: Deref, F: Deref, L: Deref> Payer for ChannelManager<Signer, M, T, K, F, L>
433 M::Target: chain::Watch<Signer>,
434 T::Target: BroadcasterInterface,
435 K::Target: KeysInterface<Signer = Signer>,
436 F::Target: FeeEstimator,
439 fn node_id(&self) -> PublicKey {
440 self.get_our_node_id()
443 fn first_hops(&self) -> Vec<ChannelDetails> {
444 self.list_usable_channels()
448 &self, route: &Route, payment_hash: PaymentHash, payment_secret: &Option<PaymentSecret>
449 ) -> Result<PaymentId, PaymentSendFailure> {
450 self.send_payment(route, payment_hash, payment_secret)
453 fn send_spontaneous_payment(
454 &self, route: &Route, payment_preimage: PaymentPreimage,
455 ) -> Result<PaymentId, PaymentSendFailure> {
456 self.send_spontaneous_payment(route, Some(payment_preimage))
457 .map(|(_, payment_id)| payment_id)
461 &self, route: &Route, payment_id: PaymentId
462 ) -> Result<(), PaymentSendFailure> {
463 self.retry_payment(route, payment_id)
466 fn abandon_payment(&self, payment_id: PaymentId) {
467 self.abandon_payment(payment_id)
473 use core::time::Duration;
474 use {Currency, Description, InvoiceDescription};
475 use bitcoin_hashes::Hash;
476 use bitcoin_hashes::sha256::Hash as Sha256;
477 use lightning::chain::keysinterface::PhantomKeysManager;
478 use lightning::ln::{PaymentPreimage, PaymentHash};
479 use lightning::ln::channelmanager::{PhantomRouteHints, MIN_FINAL_CLTV_EXPIRY};
480 use lightning::ln::functional_test_utils::*;
481 use lightning::ln::features::InitFeatures;
482 use lightning::ln::msgs::ChannelMessageHandler;
483 use lightning::routing::router::{PaymentParameters, RouteParameters, find_route};
484 use lightning::util::enforcing_trait_impls::EnforcingSigner;
485 use lightning::util::events::{MessageSendEvent, MessageSendEventsProvider, Event};
486 use lightning::util::test_utils;
487 use lightning::util::config::UserConfig;
488 use lightning::chain::keysinterface::KeysInterface;
489 use utils::create_invoice_from_channelmanager_and_duration_since_epoch;
490 use std::collections::HashSet;
493 fn test_from_channelmanager() {
494 let chanmon_cfgs = create_chanmon_cfgs(2);
495 let node_cfgs = create_node_cfgs(2, &chanmon_cfgs);
496 let node_chanmgrs = create_node_chanmgrs(2, &node_cfgs, &[None, None]);
497 let nodes = create_network(2, &node_cfgs, &node_chanmgrs);
498 create_unannounced_chan_between_nodes_with_value(&nodes, 0, 1, 100000, 10001, InitFeatures::known(), InitFeatures::known());
499 let invoice = create_invoice_from_channelmanager_and_duration_since_epoch(
500 &nodes[1].node, nodes[1].keys_manager, Currency::BitcoinTestnet, Some(10_000), "test".to_string(),
501 Duration::from_secs(1234567)).unwrap();
502 assert_eq!(invoice.amount_pico_btc(), Some(100_000));
503 assert_eq!(invoice.min_final_cltv_expiry(), MIN_FINAL_CLTV_EXPIRY as u64);
504 assert_eq!(invoice.description(), InvoiceDescription::Direct(&Description("test".to_string())));
506 // Invoice SCIDs should always use inbound SCID aliases over the real channel ID, if one is
508 assert_eq!(invoice.route_hints().len(), 1);
509 assert_eq!(invoice.route_hints()[0].0.len(), 1);
510 assert_eq!(invoice.route_hints()[0].0[0].short_channel_id,
511 nodes[1].node.list_usable_channels()[0].inbound_scid_alias.unwrap());
513 let payment_params = PaymentParameters::from_node_id(invoice.recover_payee_pub_key())
514 .with_features(invoice.features().unwrap().clone())
515 .with_route_hints(invoice.route_hints());
516 let route_params = RouteParameters {
518 final_value_msat: invoice.amount_milli_satoshis().unwrap(),
519 final_cltv_expiry_delta: invoice.min_final_cltv_expiry() as u32,
521 let first_hops = nodes[0].node.list_usable_channels();
522 let network_graph = node_cfgs[0].network_graph;
523 let logger = test_utils::TestLogger::new();
524 let scorer = test_utils::TestScorer::with_penalty(0);
525 let random_seed_bytes = chanmon_cfgs[1].keys_manager.get_secure_random_bytes();
526 let route = find_route(
527 &nodes[0].node.get_our_node_id(), &route_params, network_graph,
528 Some(&first_hops.iter().collect::<Vec<_>>()), &logger, &scorer, &random_seed_bytes
531 let payment_event = {
532 let mut payment_hash = PaymentHash([0; 32]);
533 payment_hash.0.copy_from_slice(&invoice.payment_hash().as_ref()[0..32]);
534 nodes[0].node.send_payment(&route, payment_hash, &Some(invoice.payment_secret().clone())).unwrap();
535 let mut added_monitors = nodes[0].chain_monitor.added_monitors.lock().unwrap();
536 assert_eq!(added_monitors.len(), 1);
537 added_monitors.clear();
539 let mut events = nodes[0].node.get_and_clear_pending_msg_events();
540 assert_eq!(events.len(), 1);
541 SendEvent::from_event(events.remove(0))
544 nodes[1].node.handle_update_add_htlc(&nodes[0].node.get_our_node_id(), &payment_event.msgs[0]);
545 nodes[1].node.handle_commitment_signed(&nodes[0].node.get_our_node_id(), &payment_event.commitment_msg);
546 let mut added_monitors = nodes[1].chain_monitor.added_monitors.lock().unwrap();
547 assert_eq!(added_monitors.len(), 1);
548 added_monitors.clear();
549 let events = nodes[1].node.get_and_clear_pending_msg_events();
550 assert_eq!(events.len(), 2);
554 fn test_create_invoice_with_description_hash() {
555 let chanmon_cfgs = create_chanmon_cfgs(2);
556 let node_cfgs = create_node_cfgs(2, &chanmon_cfgs);
557 let node_chanmgrs = create_node_chanmgrs(2, &node_cfgs, &[None, None]);
558 let nodes = create_network(2, &node_cfgs, &node_chanmgrs);
559 let description_hash = crate::Sha256(Hash::hash("Testing description_hash".as_bytes()));
560 let invoice = ::utils::create_invoice_from_channelmanager_with_description_hash_and_duration_since_epoch(
561 &nodes[1].node, nodes[1].keys_manager, Currency::BitcoinTestnet, Some(10_000),
562 description_hash, Duration::from_secs(1234567),
564 assert_eq!(invoice.amount_pico_btc(), Some(100_000));
565 assert_eq!(invoice.min_final_cltv_expiry(), MIN_FINAL_CLTV_EXPIRY as u64);
566 assert_eq!(invoice.description(), InvoiceDescription::Hash(&crate::Sha256(Sha256::hash("Testing description_hash".as_bytes()))));
570 fn test_hints_includes_single_channels_to_nodes() {
571 let chanmon_cfgs = create_chanmon_cfgs(3);
572 let node_cfgs = create_node_cfgs(3, &chanmon_cfgs);
573 let node_chanmgrs = create_node_chanmgrs(3, &node_cfgs, &[None, None, None]);
574 let nodes = create_network(3, &node_cfgs, &node_chanmgrs);
576 let chan_1_0 = create_unannounced_chan_between_nodes_with_value(&nodes, 1, 0, 100000, 10001, InitFeatures::known(), InitFeatures::known());
577 let chan_2_0 = create_unannounced_chan_between_nodes_with_value(&nodes, 2, 0, 100000, 10001, InitFeatures::known(), InitFeatures::known());
579 let mut scid_aliases = HashSet::new();
580 scid_aliases.insert(chan_1_0.0.short_channel_id_alias.unwrap());
581 scid_aliases.insert(chan_2_0.0.short_channel_id_alias.unwrap());
583 match_invoice_routes(Some(5000), &nodes[0], scid_aliases);
587 fn test_hints_has_only_highest_inbound_capacity_channel() {
588 let chanmon_cfgs = create_chanmon_cfgs(2);
589 let node_cfgs = create_node_cfgs(2, &chanmon_cfgs);
590 let node_chanmgrs = create_node_chanmgrs(2, &node_cfgs, &[None, None]);
591 let nodes = create_network(2, &node_cfgs, &node_chanmgrs);
592 let _chan_1_0_low_inbound_capacity = create_unannounced_chan_between_nodes_with_value(&nodes, 1, 0, 100_000, 0, InitFeatures::known(), InitFeatures::known());
593 let chan_1_0_high_inbound_capacity = create_unannounced_chan_between_nodes_with_value(&nodes, 1, 0, 10_000_000, 0, InitFeatures::known(), InitFeatures::known());
594 let _chan_1_0_medium_inbound_capacity = create_unannounced_chan_between_nodes_with_value(&nodes, 1, 0, 1_000_000, 0, InitFeatures::known(), InitFeatures::known());
596 let mut scid_aliases = HashSet::new();
597 scid_aliases.insert(chan_1_0_high_inbound_capacity.0.short_channel_id_alias.unwrap());
599 match_invoice_routes(Some(5000), &nodes[0], scid_aliases);
603 fn test_forwarding_info_not_assigned_channel_excluded_from_hints() {
604 let chanmon_cfgs = create_chanmon_cfgs(3);
605 let node_cfgs = create_node_cfgs(3, &chanmon_cfgs);
606 let node_chanmgrs = create_node_chanmgrs(3, &node_cfgs, &[None, None, None]);
607 let nodes = create_network(3, &node_cfgs, &node_chanmgrs);
608 let chan_1_0 = create_unannounced_chan_between_nodes_with_value(&nodes, 1, 0, 100000, 10001, InitFeatures::known(), InitFeatures::known());
610 // Create an unannonced channel between `nodes[2]` and `nodes[0]`, for which the
611 // `msgs::ChannelUpdate` is never handled for the node(s). As the `msgs::ChannelUpdate`
612 // is never handled, the `channel.counterparty.forwarding_info` is never assigned.
613 let mut private_chan_cfg = UserConfig::default();
614 private_chan_cfg.channel_options.announced_channel = false;
615 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();
616 let open_channel = get_event_msg!(nodes[2], MessageSendEvent::SendOpenChannel, nodes[0].node.get_our_node_id());
617 nodes[0].node.handle_open_channel(&nodes[2].node.get_our_node_id(), InitFeatures::known(), &open_channel);
618 let accept_channel = get_event_msg!(nodes[0], MessageSendEvent::SendAcceptChannel, nodes[2].node.get_our_node_id());
619 nodes[2].node.handle_accept_channel(&nodes[0].node.get_our_node_id(), InitFeatures::known(), &accept_channel);
621 let tx = sign_funding_transaction(&nodes[2], &nodes[0], 1_000_000, temporary_channel_id);
623 let conf_height = core::cmp::max(nodes[2].best_block_info().1 + 1, nodes[0].best_block_info().1 + 1);
624 confirm_transaction_at(&nodes[2], &tx, conf_height);
625 connect_blocks(&nodes[2], CHAN_CONFIRM_DEPTH - 1);
626 confirm_transaction_at(&nodes[0], &tx, conf_height);
627 connect_blocks(&nodes[0], CHAN_CONFIRM_DEPTH - 1);
628 let as_funding_locked = get_event_msg!(nodes[2], MessageSendEvent::SendFundingLocked, nodes[0].node.get_our_node_id());
629 nodes[2].node.handle_funding_locked(&nodes[0].node.get_our_node_id(), &get_event_msg!(nodes[0], MessageSendEvent::SendFundingLocked, nodes[2].node.get_our_node_id()));
630 get_event_msg!(nodes[2], MessageSendEvent::SendChannelUpdate, nodes[0].node.get_our_node_id());
631 nodes[0].node.handle_funding_locked(&nodes[2].node.get_our_node_id(), &as_funding_locked);
632 get_event_msg!(nodes[0], MessageSendEvent::SendChannelUpdate, nodes[2].node.get_our_node_id());
634 // As `msgs::ChannelUpdate` was never handled for the participating node(s) of the second
635 // channel, the channel will never be assigned any `counterparty.forwarding_info`.
636 // Therefore only `chan_1_0` should be included in the hints.
637 let mut scid_aliases = HashSet::new();
638 scid_aliases.insert(chan_1_0.0.short_channel_id_alias.unwrap());
639 match_invoice_routes(Some(5000), &nodes[0], scid_aliases);
643 fn test_no_hints_if_a_mix_between_public_and_private_channel_exists() {
644 let chanmon_cfgs = create_chanmon_cfgs(3);
645 let node_cfgs = create_node_cfgs(3, &chanmon_cfgs);
646 let node_chanmgrs = create_node_chanmgrs(3, &node_cfgs, &[None, None, None]);
647 let nodes = create_network(3, &node_cfgs, &node_chanmgrs);
648 let _chan_1_0 = create_unannounced_chan_between_nodes_with_value(&nodes, 1, 0, 100000, 10001, InitFeatures::known(), InitFeatures::known());
650 let chan_2_0 = create_announced_chan_between_nodes_with_value(&nodes, 2, 0, 100000, 10001, InitFeatures::known(), InitFeatures::known());
651 nodes[2].node.handle_channel_update(&nodes[0].node.get_our_node_id(), &chan_2_0.1);
652 nodes[0].node.handle_channel_update(&nodes[2].node.get_our_node_id(), &chan_2_0.0);
654 // Ensure that the invoice doesn't include any route hints for any of `nodes[0]` channels,
655 // even though all channels between `nodes[1]` and `nodes[0]` are private, as there is a
656 // public channel between `nodes[2]` and `nodes[0]`
657 match_invoice_routes(Some(5000), &nodes[0], HashSet::new());
661 fn test_only_public_channels_includes_no_channels_in_hints() {
662 let chanmon_cfgs = create_chanmon_cfgs(3);
663 let node_cfgs = create_node_cfgs(3, &chanmon_cfgs);
664 let node_chanmgrs = create_node_chanmgrs(3, &node_cfgs, &[None, None, None]);
665 let nodes = create_network(3, &node_cfgs, &node_chanmgrs);
666 let chan_1_0 = create_announced_chan_between_nodes_with_value(&nodes, 1, 0, 100000, 10001, InitFeatures::known(), InitFeatures::known());
667 nodes[0].node.handle_channel_update(&nodes[1].node.get_our_node_id(), &chan_1_0.0);
668 nodes[1].node.handle_channel_update(&nodes[0].node.get_our_node_id(), &chan_1_0.1);
670 let chan_2_0 = create_announced_chan_between_nodes_with_value(&nodes, 2, 0, 100000, 10001, InitFeatures::known(), InitFeatures::known());
671 nodes[2].node.handle_channel_update(&nodes[0].node.get_our_node_id(), &chan_2_0.1);
672 nodes[0].node.handle_channel_update(&nodes[2].node.get_our_node_id(), &chan_2_0.0);
674 // As all of `nodes[0]` channels are public, no channels should be included in the hints
675 match_invoice_routes(Some(5000), &nodes[0], HashSet::new());
679 fn test_channels_with_lower_inbound_capacity_than_invoice_amt_hints_filtering() {
680 let chanmon_cfgs = create_chanmon_cfgs(3);
681 let node_cfgs = create_node_cfgs(3, &chanmon_cfgs);
682 let node_chanmgrs = create_node_chanmgrs(3, &node_cfgs, &[None, None, None]);
683 let nodes = create_network(3, &node_cfgs, &node_chanmgrs);
684 let chan_1_0 = create_unannounced_chan_between_nodes_with_value(&nodes, 1, 0, 100_000, 0, InitFeatures::known(), InitFeatures::known());
685 let chan_2_0 = create_unannounced_chan_between_nodes_with_value(&nodes, 2, 0, 1_000_000, 0, InitFeatures::known(), InitFeatures::known());
687 // As the invoice amt is 1 msat above chan_1_0's inbound capacity, it shouldn't be included
688 let mut scid_aliases_99_000_001_msat = HashSet::new();
689 scid_aliases_99_000_001_msat.insert(chan_2_0.0.short_channel_id_alias.unwrap());
691 match_invoice_routes(Some(99_000_001), &nodes[0], scid_aliases_99_000_001_msat);
693 // As the invoice amt is exactly at chan_1_0's inbound capacity, it should be included
694 let mut scid_aliases_99_000_000_msat = HashSet::new();
695 scid_aliases_99_000_000_msat.insert(chan_1_0.0.short_channel_id_alias.unwrap());
696 scid_aliases_99_000_000_msat.insert(chan_2_0.0.short_channel_id_alias.unwrap());
698 match_invoice_routes(Some(99_000_000), &nodes[0], scid_aliases_99_000_000_msat);
700 // As the invoice amt is above all channels' inbound capacity, they will still be included
701 let mut scid_aliases_2_000_000_000_msat = HashSet::new();
702 scid_aliases_2_000_000_000_msat.insert(chan_1_0.0.short_channel_id_alias.unwrap());
703 scid_aliases_2_000_000_000_msat.insert(chan_2_0.0.short_channel_id_alias.unwrap());
705 match_invoice_routes(Some(2_000_000_000), &nodes[0], scid_aliases_2_000_000_000_msat);
707 // An invoice with no specified amount should include all channels in the route hints.
708 let mut scid_aliases_no_specified_amount = HashSet::new();
709 scid_aliases_no_specified_amount.insert(chan_1_0.0.short_channel_id_alias.unwrap());
710 scid_aliases_no_specified_amount.insert(chan_2_0.0.short_channel_id_alias.unwrap());
712 match_invoice_routes(None, &nodes[0], scid_aliases_no_specified_amount);
715 fn match_invoice_routes<'a, 'b: 'a, 'c: 'b>(
716 invoice_amt: Option<u64>,
717 invoice_node: &Node<'a, 'b, 'c>,
718 mut chan_ids_to_match: HashSet<u64>
720 let invoice = create_invoice_from_channelmanager_and_duration_since_epoch(
721 &invoice_node.node, invoice_node.keys_manager, Currency::BitcoinTestnet, invoice_amt, "test".to_string(),
722 Duration::from_secs(1234567)).unwrap();
723 let hints = invoice.private_routes();
726 let hint_short_chan_id = (hint.0).0[0].short_channel_id;
727 assert!(chan_ids_to_match.remove(&hint_short_chan_id));
729 assert!(chan_ids_to_match.is_empty(), "Unmatched short channel ids: {:?}", chan_ids_to_match);
733 #[cfg(feature = "std")]
734 fn test_multi_node_receive() {
735 do_test_multi_node_receive(true);
736 do_test_multi_node_receive(false);
739 #[cfg(feature = "std")]
740 fn do_test_multi_node_receive(user_generated_pmt_hash: bool) {
741 let mut chanmon_cfgs = create_chanmon_cfgs(3);
742 let seed_1 = [42 as u8; 32];
743 let seed_2 = [43 as u8; 32];
744 let cross_node_seed = [44 as u8; 32];
745 chanmon_cfgs[1].keys_manager.backing = PhantomKeysManager::new(&seed_1, 43, 44, &cross_node_seed);
746 chanmon_cfgs[2].keys_manager.backing = PhantomKeysManager::new(&seed_2, 43, 44, &cross_node_seed);
747 let node_cfgs = create_node_cfgs(3, &chanmon_cfgs);
748 let node_chanmgrs = create_node_chanmgrs(3, &node_cfgs, &[None, None, None]);
749 let nodes = create_network(3, &node_cfgs, &node_chanmgrs);
750 let chan_0_1 = create_announced_chan_between_nodes_with_value(&nodes, 0, 1, 100000, 10001, InitFeatures::known(), InitFeatures::known());
751 nodes[0].node.handle_channel_update(&nodes[1].node.get_our_node_id(), &chan_0_1.1);
752 nodes[1].node.handle_channel_update(&nodes[0].node.get_our_node_id(), &chan_0_1.0);
753 let chan_0_2 = create_announced_chan_between_nodes_with_value(&nodes, 0, 2, 100000, 10001, InitFeatures::known(), InitFeatures::known());
754 nodes[0].node.handle_channel_update(&nodes[2].node.get_our_node_id(), &chan_0_2.1);
755 nodes[2].node.handle_channel_update(&nodes[0].node.get_our_node_id(), &chan_0_2.0);
757 let payment_amt = 10_000;
758 let (payment_preimage, payment_hash, payment_secret) = {
759 if user_generated_pmt_hash {
760 let payment_preimage = PaymentPreimage([1; 32]);
761 let payment_hash = PaymentHash(Sha256::hash(&payment_preimage.0[..]).into_inner());
762 let payment_secret = nodes[1].node.create_inbound_payment_for_hash(payment_hash, Some(payment_amt), 3600).unwrap();
763 (payment_preimage, payment_hash, payment_secret)
765 let (payment_hash, payment_secret) = nodes[1].node.create_inbound_payment(Some(payment_amt), 3600).unwrap();
766 let payment_preimage = nodes[1].node.get_payment_preimage(payment_hash, payment_secret).unwrap();
767 (payment_preimage, payment_hash, payment_secret)
770 let route_hints = vec![
771 nodes[1].node.get_phantom_route_hints(),
772 nodes[2].node.get_phantom_route_hints(),
774 let invoice = ::utils::create_phantom_invoice::<EnforcingSigner, &test_utils::TestKeysInterface>(Some(payment_amt), "test".to_string(), payment_hash, payment_secret, route_hints, &nodes[1].keys_manager, Currency::BitcoinTestnet).unwrap();
776 assert_eq!(invoice.min_final_cltv_expiry(), MIN_FINAL_CLTV_EXPIRY as u64);
777 assert_eq!(invoice.description(), InvoiceDescription::Direct(&Description("test".to_string())));
778 assert_eq!(invoice.route_hints().len(), 2);
779 assert!(!invoice.features().unwrap().supports_basic_mpp());
781 let payment_params = PaymentParameters::from_node_id(invoice.recover_payee_pub_key())
782 .with_features(invoice.features().unwrap().clone())
783 .with_route_hints(invoice.route_hints());
784 let params = RouteParameters {
786 final_value_msat: invoice.amount_milli_satoshis().unwrap(),
787 final_cltv_expiry_delta: invoice.min_final_cltv_expiry() as u32,
789 let first_hops = nodes[0].node.list_usable_channels();
790 let network_graph = node_cfgs[0].network_graph;
791 let logger = test_utils::TestLogger::new();
792 let scorer = test_utils::TestScorer::with_penalty(0);
793 let random_seed_bytes = chanmon_cfgs[1].keys_manager.get_secure_random_bytes();
794 let route = find_route(
795 &nodes[0].node.get_our_node_id(), ¶ms, network_graph,
796 Some(&first_hops.iter().collect::<Vec<_>>()), &logger, &scorer, &random_seed_bytes
798 let (payment_event, fwd_idx) = {
799 let mut payment_hash = PaymentHash([0; 32]);
800 payment_hash.0.copy_from_slice(&invoice.payment_hash().as_ref()[0..32]);
801 nodes[0].node.send_payment(&route, payment_hash, &Some(invoice.payment_secret().clone())).unwrap();
802 let mut added_monitors = nodes[0].chain_monitor.added_monitors.lock().unwrap();
803 assert_eq!(added_monitors.len(), 1);
804 added_monitors.clear();
806 let mut events = nodes[0].node.get_and_clear_pending_msg_events();
807 assert_eq!(events.len(), 1);
808 let fwd_idx = match events[0] {
809 MessageSendEvent::UpdateHTLCs { node_id, .. } => {
810 if node_id == nodes[1].node.get_our_node_id() {
814 _ => panic!("Unexpected event")
816 (SendEvent::from_event(events.remove(0)), fwd_idx)
818 nodes[fwd_idx].node.handle_update_add_htlc(&nodes[0].node.get_our_node_id(), &payment_event.msgs[0]);
819 commitment_signed_dance!(nodes[fwd_idx], nodes[0], &payment_event.commitment_msg, false, true);
821 // Note that we have to "forward pending HTLCs" twice before we see the PaymentReceived as
822 // this "emulates" the payment taking two hops, providing some privacy to make phantom node
823 // payments "look real" by taking more time.
824 expect_pending_htlcs_forwardable_ignore!(nodes[fwd_idx]);
825 nodes[fwd_idx].node.process_pending_htlc_forwards();
826 expect_pending_htlcs_forwardable_ignore!(nodes[fwd_idx]);
827 nodes[fwd_idx].node.process_pending_htlc_forwards();
829 let payment_preimage_opt = if user_generated_pmt_hash { None } else { Some(payment_preimage) };
830 expect_payment_received!(&nodes[fwd_idx], payment_hash, payment_secret, payment_amt, payment_preimage_opt);
831 do_claim_payment_along_route(&nodes[0], &vec!(&vec!(&nodes[fwd_idx])[..]), false, payment_preimage);
832 let events = nodes[0].node.get_and_clear_pending_events();
833 assert_eq!(events.len(), 2);
835 Event::PaymentSent { payment_preimage: ref ev_preimage, payment_hash: ref ev_hash, ref fee_paid_msat, .. } => {
836 assert_eq!(payment_preimage, *ev_preimage);
837 assert_eq!(payment_hash, *ev_hash);
838 assert_eq!(fee_paid_msat, &Some(0));
840 _ => panic!("Unexpected event")
843 Event::PaymentPathSuccessful { payment_hash: hash, .. } => {
844 assert_eq!(hash, Some(payment_hash));
846 _ => panic!("Unexpected event")
851 #[cfg(feature = "std")]
852 fn create_phantom_invoice_with_description_hash() {
853 let chanmon_cfgs = create_chanmon_cfgs(3);
854 let node_cfgs = create_node_cfgs(3, &chanmon_cfgs);
855 let node_chanmgrs = create_node_chanmgrs(3, &node_cfgs, &[None, None, None]);
856 let nodes = create_network(3, &node_cfgs, &node_chanmgrs);
858 let payment_amt = 20_000;
859 let (payment_hash, payment_secret) = nodes[1].node.create_inbound_payment(Some(payment_amt), 3600).unwrap();
860 let route_hints = vec![
861 nodes[1].node.get_phantom_route_hints(),
862 nodes[2].node.get_phantom_route_hints(),
865 let description_hash = crate::Sha256(Hash::hash("Description hash phantom invoice".as_bytes()));
866 let invoice = ::utils::create_phantom_invoice_with_description_hash::<EnforcingSigner,&test_utils::TestKeysInterface>(Some(payment_amt), description_hash, payment_hash, payment_secret, route_hints, &nodes[1].keys_manager, Currency::BitcoinTestnet).unwrap();
868 assert_eq!(invoice.amount_pico_btc(), Some(200_000));
869 assert_eq!(invoice.min_final_cltv_expiry(), MIN_FINAL_CLTV_EXPIRY as u64);
870 assert_eq!(invoice.description(), InvoiceDescription::Hash(&crate::Sha256(Sha256::hash("Description hash phantom invoice".as_bytes()))));
874 #[cfg(feature = "std")]
875 fn test_multi_node_hints_includes_single_channels_to_participating_nodes() {
876 let mut chanmon_cfgs = create_chanmon_cfgs(3);
877 let seed_1 = [42 as u8; 32];
878 let seed_2 = [43 as u8; 32];
879 let cross_node_seed = [44 as u8; 32];
880 chanmon_cfgs[1].keys_manager.backing = PhantomKeysManager::new(&seed_1, 43, 44, &cross_node_seed);
881 chanmon_cfgs[2].keys_manager.backing = PhantomKeysManager::new(&seed_2, 43, 44, &cross_node_seed);
882 let node_cfgs = create_node_cfgs(3, &chanmon_cfgs);
883 let node_chanmgrs = create_node_chanmgrs(3, &node_cfgs, &[None, None, None]);
884 let nodes = create_network(3, &node_cfgs, &node_chanmgrs);
886 let chan_0_1 = create_unannounced_chan_between_nodes_with_value(&nodes, 0, 1, 100000, 10001, InitFeatures::known(), InitFeatures::known());
887 let chan_0_2 = create_unannounced_chan_between_nodes_with_value(&nodes, 0, 2, 100000, 10001, InitFeatures::known(), InitFeatures::known());
889 let mut scid_aliases = HashSet::new();
890 scid_aliases.insert(chan_0_1.0.short_channel_id_alias.unwrap());
891 scid_aliases.insert(chan_0_2.0.short_channel_id_alias.unwrap());
893 match_multi_node_invoice_routes(
896 vec![&nodes[1], &nodes[2],],
903 #[cfg(feature = "std")]
904 fn test_multi_node_hints_includes_one_channel_of_each_counterparty_nodes_per_participating_node() {
905 let mut chanmon_cfgs = create_chanmon_cfgs(4);
906 let seed_1 = [42 as u8; 32];
907 let seed_2 = [43 as u8; 32];
908 let cross_node_seed = [44 as u8; 32];
909 chanmon_cfgs[2].keys_manager.backing = PhantomKeysManager::new(&seed_1, 43, 44, &cross_node_seed);
910 chanmon_cfgs[3].keys_manager.backing = PhantomKeysManager::new(&seed_2, 43, 44, &cross_node_seed);
911 let node_cfgs = create_node_cfgs(4, &chanmon_cfgs);
912 let node_chanmgrs = create_node_chanmgrs(4, &node_cfgs, &[None, None, None, None]);
913 let nodes = create_network(4, &node_cfgs, &node_chanmgrs);
915 let chan_0_2 = create_unannounced_chan_between_nodes_with_value(&nodes, 0, 2, 100000, 10001, InitFeatures::known(), InitFeatures::known());
916 let chan_0_3 = create_unannounced_chan_between_nodes_with_value(&nodes, 0, 3, 1000000, 10001, InitFeatures::known(), InitFeatures::known());
917 let chan_1_3 = create_unannounced_chan_between_nodes_with_value(&nodes, 1, 3, 3_000_000, 10005, InitFeatures::known(), InitFeatures::known());
919 let mut scid_aliases = HashSet::new();
920 scid_aliases.insert(chan_0_2.0.short_channel_id_alias.unwrap());
921 scid_aliases.insert(chan_0_3.0.short_channel_id_alias.unwrap());
922 scid_aliases.insert(chan_1_3.0.short_channel_id_alias.unwrap());
924 match_multi_node_invoice_routes(
927 vec![&nodes[2], &nodes[3],],
934 #[cfg(feature = "std")]
935 fn test_multi_node_forwarding_info_not_assigned_channel_excluded_from_hints() {
936 let mut chanmon_cfgs = create_chanmon_cfgs(4);
937 let seed_1 = [42 as u8; 32];
938 let seed_2 = [43 as u8; 32];
939 let cross_node_seed = [44 as u8; 32];
940 chanmon_cfgs[2].keys_manager.backing = PhantomKeysManager::new(&seed_1, 43, 44, &cross_node_seed);
941 chanmon_cfgs[3].keys_manager.backing = PhantomKeysManager::new(&seed_2, 43, 44, &cross_node_seed);
942 let node_cfgs = create_node_cfgs(4, &chanmon_cfgs);
943 let node_chanmgrs = create_node_chanmgrs(4, &node_cfgs, &[None, None, None, None]);
944 let nodes = create_network(4, &node_cfgs, &node_chanmgrs);
946 let chan_0_2 = create_unannounced_chan_between_nodes_with_value(&nodes, 0, 2, 100000, 10001, InitFeatures::known(), InitFeatures::known());
947 let chan_0_3 = create_unannounced_chan_between_nodes_with_value(&nodes, 0, 3, 1000000, 10001, InitFeatures::known(), InitFeatures::known());
949 // Create an unannonced channel between `nodes[1]` and `nodes[3]`, for which the
950 // `msgs::ChannelUpdate` is never handled for the node(s). As the `msgs::ChannelUpdate`
951 // is never handled, the `channel.counterparty.forwarding_info` is never assigned.
952 let mut private_chan_cfg = UserConfig::default();
953 private_chan_cfg.channel_options.announced_channel = false;
954 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();
955 let open_channel = get_event_msg!(nodes[1], MessageSendEvent::SendOpenChannel, nodes[3].node.get_our_node_id());
956 nodes[3].node.handle_open_channel(&nodes[1].node.get_our_node_id(), InitFeatures::known(), &open_channel);
957 let accept_channel = get_event_msg!(nodes[3], MessageSendEvent::SendAcceptChannel, nodes[1].node.get_our_node_id());
958 nodes[1].node.handle_accept_channel(&nodes[3].node.get_our_node_id(), InitFeatures::known(), &accept_channel);
960 let tx = sign_funding_transaction(&nodes[1], &nodes[3], 1_000_000, temporary_channel_id);
962 let conf_height = core::cmp::max(nodes[1].best_block_info().1 + 1, nodes[3].best_block_info().1 + 1);
963 confirm_transaction_at(&nodes[1], &tx, conf_height);
964 connect_blocks(&nodes[1], CHAN_CONFIRM_DEPTH - 1);
965 confirm_transaction_at(&nodes[3], &tx, conf_height);
966 connect_blocks(&nodes[3], CHAN_CONFIRM_DEPTH - 1);
967 let as_funding_locked = get_event_msg!(nodes[1], MessageSendEvent::SendFundingLocked, nodes[3].node.get_our_node_id());
968 nodes[1].node.handle_funding_locked(&nodes[3].node.get_our_node_id(), &get_event_msg!(nodes[3], MessageSendEvent::SendFundingLocked, nodes[1].node.get_our_node_id()));
969 get_event_msg!(nodes[1], MessageSendEvent::SendChannelUpdate, nodes[3].node.get_our_node_id());
970 nodes[3].node.handle_funding_locked(&nodes[1].node.get_our_node_id(), &as_funding_locked);
971 get_event_msg!(nodes[3], MessageSendEvent::SendChannelUpdate, nodes[1].node.get_our_node_id());
973 // As `msgs::ChannelUpdate` was never handled for the participating node(s) of the third
974 // channel, the channel will never be assigned any `counterparty.forwarding_info`.
975 // Therefore only `chan_0_3` should be included in the hints for `nodes[3]`.
976 let mut scid_aliases = HashSet::new();
977 scid_aliases.insert(chan_0_2.0.short_channel_id_alias.unwrap());
978 scid_aliases.insert(chan_0_3.0.short_channel_id_alias.unwrap());
980 match_multi_node_invoice_routes(
983 vec![&nodes[2], &nodes[3],],
990 #[cfg(feature = "std")]
991 fn test_multi_node_with_only_public_channels_hints_includes_only_phantom_route() {
992 let mut chanmon_cfgs = create_chanmon_cfgs(3);
993 let seed_1 = [42 as u8; 32];
994 let seed_2 = [43 as u8; 32];
995 let cross_node_seed = [44 as u8; 32];
996 chanmon_cfgs[1].keys_manager.backing = PhantomKeysManager::new(&seed_1, 43, 44, &cross_node_seed);
997 chanmon_cfgs[2].keys_manager.backing = PhantomKeysManager::new(&seed_2, 43, 44, &cross_node_seed);
998 let node_cfgs = create_node_cfgs(3, &chanmon_cfgs);
999 let node_chanmgrs = create_node_chanmgrs(3, &node_cfgs, &[None, None, None]);
1000 let nodes = create_network(3, &node_cfgs, &node_chanmgrs);
1002 let chan_0_1 = create_unannounced_chan_between_nodes_with_value(&nodes, 0, 1, 100000, 10001, InitFeatures::known(), InitFeatures::known());
1004 let chan_2_0 = create_announced_chan_between_nodes_with_value(&nodes, 2, 0, 100000, 10001, InitFeatures::known(), InitFeatures::known());
1005 nodes[2].node.handle_channel_update(&nodes[0].node.get_our_node_id(), &chan_2_0.1);
1006 nodes[0].node.handle_channel_update(&nodes[2].node.get_our_node_id(), &chan_2_0.0);
1008 // Hints should include `chan_0_1` from as `nodes[1]` only have private channels, but not
1009 // `chan_0_2` as `nodes[2]` only has public channels.
1010 let mut scid_aliases = HashSet::new();
1011 scid_aliases.insert(chan_0_1.0.short_channel_id_alias.unwrap());
1013 match_multi_node_invoice_routes(
1016 vec![&nodes[1], &nodes[2],],
1023 #[cfg(feature = "std")]
1024 fn test_multi_node_with_mixed_public_and_private_channel_hints_includes_only_phantom_route() {
1025 let mut chanmon_cfgs = create_chanmon_cfgs(4);
1026 let seed_1 = [42 as u8; 32];
1027 let seed_2 = [43 as u8; 32];
1028 let cross_node_seed = [44 as u8; 32];
1029 chanmon_cfgs[1].keys_manager.backing = PhantomKeysManager::new(&seed_1, 43, 44, &cross_node_seed);
1030 chanmon_cfgs[2].keys_manager.backing = PhantomKeysManager::new(&seed_2, 43, 44, &cross_node_seed);
1031 let node_cfgs = create_node_cfgs(4, &chanmon_cfgs);
1032 let node_chanmgrs = create_node_chanmgrs(4, &node_cfgs, &[None, None, None, None]);
1033 let nodes = create_network(4, &node_cfgs, &node_chanmgrs);
1035 let chan_0_2 = create_announced_chan_between_nodes_with_value(&nodes, 0, 2, 100000, 10001, InitFeatures::known(), InitFeatures::known());
1036 nodes[0].node.handle_channel_update(&nodes[2].node.get_our_node_id(), &chan_0_2.1);
1037 nodes[2].node.handle_channel_update(&nodes[0].node.get_our_node_id(), &chan_0_2.0);
1038 let _chan_1_2 = create_unannounced_chan_between_nodes_with_value(&nodes, 1, 2, 100000, 10001, InitFeatures::known(), InitFeatures::known());
1040 let chan_0_3 = create_unannounced_chan_between_nodes_with_value(&nodes, 0, 3, 100000, 10001, InitFeatures::known(), InitFeatures::known());
1042 // Hints should include `chan_0_3` from as `nodes[3]` only have private channels, and no
1043 // channels for `nodes[2]` as it contains a mix of public and private channels.
1044 let mut scid_aliases = HashSet::new();
1045 scid_aliases.insert(chan_0_3.0.short_channel_id_alias.unwrap());
1047 match_multi_node_invoice_routes(
1050 vec![&nodes[2], &nodes[3],],
1057 #[cfg(feature = "std")]
1058 fn test_multi_node_hints_has_only_highest_inbound_capacity_channel() {
1059 let mut chanmon_cfgs = create_chanmon_cfgs(3);
1060 let seed_1 = [42 as u8; 32];
1061 let seed_2 = [43 as u8; 32];
1062 let cross_node_seed = [44 as u8; 32];
1063 chanmon_cfgs[1].keys_manager.backing = PhantomKeysManager::new(&seed_1, 43, 44, &cross_node_seed);
1064 chanmon_cfgs[2].keys_manager.backing = PhantomKeysManager::new(&seed_2, 43, 44, &cross_node_seed);
1065 let node_cfgs = create_node_cfgs(3, &chanmon_cfgs);
1066 let node_chanmgrs = create_node_chanmgrs(3, &node_cfgs, &[None, None, None]);
1067 let nodes = create_network(3, &node_cfgs, &node_chanmgrs);
1069 let _chan_0_1_low_inbound_capacity = create_unannounced_chan_between_nodes_with_value(&nodes, 0, 1, 100_000, 0, InitFeatures::known(), InitFeatures::known());
1070 let chan_0_1_high_inbound_capacity = create_unannounced_chan_between_nodes_with_value(&nodes, 0, 1, 10_000_000, 0, InitFeatures::known(), InitFeatures::known());
1071 let _chan_0_1_medium_inbound_capacity = create_unannounced_chan_between_nodes_with_value(&nodes, 0, 1, 1_000_000, 0, InitFeatures::known(), InitFeatures::known());
1072 let chan_0_2 = create_unannounced_chan_between_nodes_with_value(&nodes, 0, 2, 100000, 10001, InitFeatures::known(), InitFeatures::known());
1074 let mut scid_aliases = HashSet::new();
1075 scid_aliases.insert(chan_0_1_high_inbound_capacity.0.short_channel_id_alias.unwrap());
1076 scid_aliases.insert(chan_0_2.0.short_channel_id_alias.unwrap());
1078 match_multi_node_invoice_routes(
1081 vec![&nodes[1], &nodes[2],],
1088 #[cfg(feature = "std")]
1089 fn test_multi_node_channels_inbound_capacity_lower_than_invoice_amt_filtering() {
1090 let mut chanmon_cfgs = create_chanmon_cfgs(4);
1091 let seed_1 = [42 as u8; 32];
1092 let seed_2 = [43 as u8; 32];
1093 let cross_node_seed = [44 as u8; 32];
1094 chanmon_cfgs[1].keys_manager.backing = PhantomKeysManager::new(&seed_1, 43, 44, &cross_node_seed);
1095 chanmon_cfgs[2].keys_manager.backing = PhantomKeysManager::new(&seed_2, 43, 44, &cross_node_seed);
1096 let node_cfgs = create_node_cfgs(4, &chanmon_cfgs);
1097 let node_chanmgrs = create_node_chanmgrs(4, &node_cfgs, &[None, None, None, None]);
1098 let nodes = create_network(4, &node_cfgs, &node_chanmgrs);
1100 let chan_0_2 = create_unannounced_chan_between_nodes_with_value(&nodes, 0, 2, 1_000_000, 0, InitFeatures::known(), InitFeatures::known());
1101 let chan_0_3 = create_unannounced_chan_between_nodes_with_value(&nodes, 0, 3, 100_000, 0, InitFeatures::known(), InitFeatures::known());
1102 let chan_1_3 = create_unannounced_chan_between_nodes_with_value(&nodes, 1, 3, 200_000, 0, InitFeatures::known(), InitFeatures::known());
1104 // Since the invoice 1 msat above chan_0_3's inbound capacity, it should be filtered out.
1105 let mut scid_aliases_99_000_001_msat = HashSet::new();
1106 scid_aliases_99_000_001_msat.insert(chan_0_2.0.short_channel_id_alias.unwrap());
1107 scid_aliases_99_000_001_msat.insert(chan_1_3.0.short_channel_id_alias.unwrap());
1109 match_multi_node_invoice_routes(
1112 vec![&nodes[2], &nodes[3],],
1113 scid_aliases_99_000_001_msat,
1117 // Since the invoice is exactly at chan_0_3's inbound capacity, it should be included.
1118 let mut scid_aliases_99_000_000_msat = HashSet::new();
1119 scid_aliases_99_000_000_msat.insert(chan_0_2.0.short_channel_id_alias.unwrap());
1120 scid_aliases_99_000_000_msat.insert(chan_0_3.0.short_channel_id_alias.unwrap());
1121 scid_aliases_99_000_000_msat.insert(chan_1_3.0.short_channel_id_alias.unwrap());
1123 match_multi_node_invoice_routes(
1126 vec![&nodes[2], &nodes[3],],
1127 scid_aliases_99_000_000_msat,
1131 // Since the invoice is above all of `nodes[2]` channels' inbound capacity, all of
1132 // `nodes[2]` them should be included.
1133 let mut scid_aliases_300_000_000_msat = HashSet::new();
1134 scid_aliases_300_000_000_msat.insert(chan_0_2.0.short_channel_id_alias.unwrap());
1135 scid_aliases_300_000_000_msat.insert(chan_0_3.0.short_channel_id_alias.unwrap());
1136 scid_aliases_300_000_000_msat.insert(chan_1_3.0.short_channel_id_alias.unwrap());
1138 match_multi_node_invoice_routes(
1141 vec![&nodes[2], &nodes[3],],
1142 scid_aliases_300_000_000_msat,
1146 // Since the no specified amount, all channels should included.
1147 let mut scid_aliases_no_specified_amount = HashSet::new();
1148 scid_aliases_no_specified_amount.insert(chan_0_2.0.short_channel_id_alias.unwrap());
1149 scid_aliases_no_specified_amount.insert(chan_0_3.0.short_channel_id_alias.unwrap());
1150 scid_aliases_no_specified_amount.insert(chan_1_3.0.short_channel_id_alias.unwrap());
1152 match_multi_node_invoice_routes(
1155 vec![&nodes[2], &nodes[3],],
1156 scid_aliases_no_specified_amount,
1161 #[cfg(feature = "std")]
1162 fn match_multi_node_invoice_routes<'a, 'b: 'a, 'c: 'b>(
1163 invoice_amt: Option<u64>,
1164 invoice_node: &Node<'a, 'b, 'c>,
1165 network_multi_nodes: Vec<&Node<'a, 'b, 'c>>,
1166 mut chan_ids_to_match: HashSet<u64>,
1167 nodes_contains_public_channels: bool
1169 let (payment_hash, payment_secret) = invoice_node.node.create_inbound_payment(invoice_amt, 3600).unwrap();
1170 let phantom_route_hints = network_multi_nodes.iter()
1171 .map(|node| node.node.get_phantom_route_hints())
1172 .collect::<Vec<PhantomRouteHints>>();
1173 let phantom_scids = phantom_route_hints.iter()
1174 .map(|route_hint| route_hint.phantom_scid)
1175 .collect::<HashSet<u64>>();
1177 let invoice = ::utils::create_phantom_invoice::<EnforcingSigner, &test_utils::TestKeysInterface>(invoice_amt, "test".to_string(), payment_hash, payment_secret, phantom_route_hints, &invoice_node.keys_manager, Currency::BitcoinTestnet).unwrap();
1179 let invoice_hints = invoice.private_routes();
1181 for hint in invoice_hints {
1182 let hints = &(hint.0).0;
1185 assert!(nodes_contains_public_channels);
1186 let phantom_scid = hints[0].short_channel_id;
1187 assert!(phantom_scids.contains(&phantom_scid));
1190 let hint_short_chan_id = hints[0].short_channel_id;
1191 assert!(chan_ids_to_match.remove(&hint_short_chan_id));
1192 let phantom_scid = hints[1].short_channel_id;
1193 assert!(phantom_scids.contains(&phantom_scid));
1195 _ => panic!("Incorrect hint length generated")
1198 assert!(chan_ids_to_match.is_empty(), "Unmatched short channel ids: {:?}", chan_ids_to_match);