Skip to content

Commit 478235b

Browse files
Read routehints from invoices when sending a payment
1 parent 84600a3 commit 478235b

File tree

1 file changed

+28
-2
lines changed

1 file changed

+28
-2
lines changed

src/cli.rs

Lines changed: 28 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,9 @@ use bitcoin::secp256k1::Secp256k1;
1212
use lightning::chain;
1313
use lightning::ln::channelmanager::{PaymentHash, PaymentPreimage, PaymentSecret};
1414
use lightning::ln::features::InvoiceFeatures;
15-
use lightning::routing::network_graph::NetGraphMsgHandler;
15+
use lightning::routing::network_graph::{NetGraphMsgHandler, RoutingFees};
1616
use lightning::routing::router;
17+
use lightning::routing::router::RouteHint;
1718
use lightning::util::config::UserConfig;
1819
use rand;
1920
use rand::Rng;
@@ -200,6 +201,8 @@ pub(crate) async fn poll_for_user_input(
200201
continue;
201202
}
202203
let invoice = invoice_res.unwrap();
204+
let route_hints: Vec<lightning_invoice::Route> =
205+
invoice.routes().iter().map(|&route| route.clone()).collect();
203206

204207
let amt_pico_btc = invoice.amount_pico_btc();
205208
if amt_pico_btc.is_none() {
@@ -272,6 +275,7 @@ pub(crate) async fn poll_for_user_input(
272275
payment_hash,
273276
payment_secret,
274277
invoice_features_opt,
278+
route_hints,
275279
router.clone(),
276280
channel_manager.clone(),
277281
payment_storage.clone(),
@@ -500,6 +504,7 @@ fn open_channel(
500504
fn send_payment(
501505
payee: PublicKey, amt_msat: u64, final_cltv: u32, payment_hash: PaymentHash,
502506
payment_secret: Option<PaymentSecret>, payee_features: Option<InvoiceFeatures>,
507+
mut route_hints: Vec<lightning_invoice::Route>,
503508
router: Arc<NetGraphMsgHandler<Arc<dyn chain::Access>, Arc<FilesystemLogger>>>,
504509
channel_manager: Arc<ChannelManager>, payment_storage: PaymentInfoStorage,
505510
logger: Arc<FilesystemLogger>,
@@ -508,13 +513,29 @@ fn send_payment(
508513
let first_hops = channel_manager.list_usable_channels();
509514
let payer_pubkey = channel_manager.get_our_node_id();
510515

516+
let mut hints: Vec<RouteHint> = Vec::new();
517+
for route in route_hints.drain(..) {
518+
let route_hops = route.into_inner();
519+
let last_hop = &route_hops[route_hops.len() - 1];
520+
hints.push(RouteHint {
521+
src_node_id: last_hop.pubkey,
522+
short_channel_id: u64::from_be_bytes(last_hop.short_channel_id),
523+
fees: RoutingFees {
524+
base_msat: last_hop.fee_base_msat,
525+
proportional_millionths: last_hop.fee_proportional_millionths,
526+
},
527+
cltv_expiry_delta: last_hop.cltv_expiry_delta,
528+
htlc_minimum_msat: None,
529+
htlc_maximum_msat: None,
530+
})
531+
}
511532
let route = router::get_route(
512533
&payer_pubkey,
513534
&network_graph,
514535
&payee,
515536
payee_features,
516537
Some(&first_hops.iter().collect::<Vec<_>>()),
517-
&vec![],
538+
&hints.iter().collect::<Vec<_>>(),
518539
amt_msat,
519540
final_cltv,
520541
logger,
@@ -567,6 +588,7 @@ fn get_invoice(
567588

568589
// Add route hints to the invoice.
569590
let our_channels = channel_manager.list_usable_channels();
591+
let mut min_final_cltv_expiry = 9;
570592
for channel in our_channels {
571593
let short_channel_id = match channel.short_channel_id {
572594
Some(id) => id.to_be_bytes(),
@@ -576,6 +598,9 @@ fn get_invoice(
576598
Some(info) => info,
577599
None => continue,
578600
};
601+
if forwarding_info.cltv_expiry_delta > min_final_cltv_expiry {
602+
min_final_cltv_expiry = forwarding_info.cltv_expiry_delta;
603+
}
579604
invoice = invoice.route(vec![lightning_invoice::RouteHop {
580605
pubkey: channel.remote_network_id,
581606
short_channel_id,
@@ -584,6 +609,7 @@ fn get_invoice(
584609
cltv_expiry_delta: forwarding_info.cltv_expiry_delta,
585610
}]);
586611
}
612+
invoice = invoice.min_final_cltv_expiry(min_final_cltv_expiry.into());
587613

588614
// Sign the invoice.
589615
let invoice =

0 commit comments

Comments
 (0)