X-Git-Url: http://git.bitcoin.ninja/index.cgi?a=blobdiff_plain;f=src%2Fcli.rs;h=0144c001022662d7da6890cda460b24b30edc243;hb=0a08f4a623389f9e78464bf3649a3a82bd3d5152;hp=9589211261592d936c6232477506fa39356f09a6;hpb=678119951c5b3460d24da64d0f2d755ef4f198e6;p=ldk-sample diff --git a/src/cli.rs b/src/cli.rs index 9589211..0144c00 100644 --- a/src/cli.rs +++ b/src/cli.rs @@ -12,8 +12,9 @@ use bitcoin::secp256k1::Secp256k1; use lightning::chain; use lightning::ln::channelmanager::{PaymentHash, PaymentPreimage, PaymentSecret}; use lightning::ln::features::InvoiceFeatures; -use lightning::routing::network_graph::NetGraphMsgHandler; +use lightning::routing::network_graph::{NetGraphMsgHandler, RoutingFees}; use lightning::routing::router; +use lightning::routing::router::RouteHint; use lightning::util::config::UserConfig; use rand; use rand::Rng; @@ -200,6 +201,8 @@ pub(crate) async fn poll_for_user_input( continue; } let invoice = invoice_res.unwrap(); + let route_hints: Vec = + invoice.routes().iter().map(|&route| route.clone()).collect(); let amt_pico_btc = invoice.amount_pico_btc(); if amt_pico_btc.is_none() { @@ -272,6 +275,7 @@ pub(crate) async fn poll_for_user_input( payment_hash, payment_secret, invoice_features_opt, + route_hints, router.clone(), channel_manager.clone(), payment_storage.clone(), @@ -500,6 +504,7 @@ fn open_channel( fn send_payment( payee: PublicKey, amt_msat: u64, final_cltv: u32, payment_hash: PaymentHash, payment_secret: Option, payee_features: Option, + mut route_hints: Vec, router: Arc, Arc>>, channel_manager: Arc, payment_storage: PaymentInfoStorage, logger: Arc, @@ -508,13 +513,29 @@ fn send_payment( let first_hops = channel_manager.list_usable_channels(); let payer_pubkey = channel_manager.get_our_node_id(); + let mut hints: Vec = Vec::new(); + for route in route_hints.drain(..) { + let route_hops = route.into_inner(); + let last_hop = &route_hops[route_hops.len() - 1]; + hints.push(RouteHint { + src_node_id: last_hop.pubkey, + short_channel_id: u64::from_be_bytes(last_hop.short_channel_id), + fees: RoutingFees { + base_msat: last_hop.fee_base_msat, + proportional_millionths: last_hop.fee_proportional_millionths, + }, + cltv_expiry_delta: last_hop.cltv_expiry_delta, + htlc_minimum_msat: None, + htlc_maximum_msat: None, + }) + } let route = router::get_route( &payer_pubkey, &network_graph, &payee, payee_features, Some(&first_hops.iter().collect::>()), - &vec![], + &hints.iter().collect::>(), amt_msat, final_cltv, logger, @@ -567,6 +588,7 @@ fn get_invoice( // Add route hints to the invoice. let our_channels = channel_manager.list_usable_channels(); + let mut min_final_cltv_expiry = 9; for channel in our_channels { let short_channel_id = match channel.short_channel_id { Some(id) => id.to_be_bytes(), @@ -576,6 +598,9 @@ fn get_invoice( Some(info) => info, None => continue, }; + if forwarding_info.cltv_expiry_delta > min_final_cltv_expiry { + min_final_cltv_expiry = forwarding_info.cltv_expiry_delta; + } invoice = invoice.route(vec![lightning_invoice::RouteHop { pubkey: channel.remote_network_id, short_channel_id, @@ -584,6 +609,7 @@ fn get_invoice( cltv_expiry_delta: forwarding_info.cltv_expiry_delta, }]); } + invoice = invoice.min_final_cltv_expiry(min_final_cltv_expiry.into()); // Sign the invoice. let invoice =