Skip to content

Align get_route's interface with ChannelManager and Invoice #946

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Jun 11, 2021

Conversation

jkczyz
Copy link
Contributor

@jkczyz jkczyz commented Jun 9, 2021

Update get_route's interface to work better with ChannelManager and Invoice.

  • pass result of ChannelManager::list_usable_channels as first_hops without converting
  • accept multi-hop hints for last_hops from Invoice::routes without copying

This is only the interface change for #945. Only the last hop from each RouteHint is considered by get_route for now.

@codecov
Copy link

codecov bot commented Jun 9, 2021

Codecov Report

Merging #946 (d0355b7) into main (a8038a8) will decrease coverage by 0.02%.
The diff coverage is 85.71%.

Impacted file tree graph

@@            Coverage Diff             @@
##             main     #946      +/-   ##
==========================================
- Coverage   90.59%   90.57%   -0.03%     
==========================================
  Files          60       60              
  Lines       30425    30423       -2     
==========================================
- Hits        27564    27556       -8     
- Misses       2861     2867       +6     
Impacted Files Coverage Δ
lightning-invoice/src/utils.rs 84.26% <60.00%> (+0.39%) ⬆️
lightning-invoice/src/de.rs 80.86% <80.00%> (ø)
lightning-invoice/src/lib.rs 88.49% <83.33%> (-0.40%) ⬇️
lightning-invoice/src/ser.rs 92.19% <100.00%> (ø)
lightning/src/routing/router.rs 95.95% <100.00%> (+<0.01%) ⬆️
lightning/src/ln/functional_tests.rs 97.17% <0.00%> (-0.05%) ⬇️
lightning/src/ln/channelmanager.rs 83.85% <0.00%> (-0.05%) ⬇️

Continue to review full report at Codecov.

Legend - Click here to learn more
Δ = absolute <relative> (impact), ø = not affected, ? = missing data
Powered by Codecov. Last update a8038a8...d0355b7. Read the comment docs.

@jkczyz jkczyz force-pushed the 2021-06-get-route-interface branch 2 times, most recently from b883ade to 422f79b Compare June 10, 2021 02:26
@TheBlueMatt TheBlueMatt added this to the 0.0.98 milestone Jun 10, 2021
@TheBlueMatt
Copy link
Collaborator

Lets land this for 0.0.98 to clean up the API which will clean up our docs and sample a good chunk.

@jkczyz jkczyz force-pushed the 2021-06-get-route-interface branch from 422f79b to 2e00de6 Compare June 10, 2021 18:31
Copy link
Collaborator

@TheBlueMatt TheBlueMatt left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This basically looks good.

pub fn get_route<L: Deref>(our_node_id: &PublicKey, network: &NetworkGraph, payee: &PublicKey, payee_features: Option<InvoiceFeatures>, first_hops: Option<&[&ChannelDetails]>,
last_hops: &[&RouteHintHop], final_value_msat: u64, final_cltv: u32, logger: L) -> Result<Route, LightningError> where L::Target: Logger {
pub fn get_route<L: Deref>(our_node_id: &PublicKey, network: &NetworkGraph, payee: &PublicKey, payee_features: Option<InvoiceFeatures>, first_hops: Option<&[ChannelDetails]>,
last_hops: Vec<&RouteHint>, final_value_msat: u64, final_cltv: u32, logger: L) -> Result<Route, LightningError> where L::Target: Logger {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can we take a &[&RouteHint]?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I was hoping to make it easy for callers by allowing them to pass the result of Invoice::route_hints directly. I guess they could just take a slice now that route_hints does the conversion, but I'll have to either touch or revert a bunch of code. Any specific reason a slice would be preferred?

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't think there's any reason to need this by-value here? I suppose &Vec is also fine but slice is more general.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Updated to take a slice.

@jkczyz jkczyz force-pushed the 2021-06-get-route-interface branch from 2e00de6 to b1abfb2 Compare June 10, 2021 19:53
Used for extracting more than one field of the same type.
@jkczyz jkczyz force-pushed the 2021-06-get-route-interface branch 2 times, most recently from 199ea3f to 7ea6106 Compare June 10, 2021 23:15
Copy link
Collaborator

@TheBlueMatt TheBlueMatt left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM, can you squash fixups?

@jkczyz jkczyz force-pushed the 2021-06-get-route-interface branch from 7ea6106 to b56fdd0 Compare June 11, 2021 01:41
@@ -343,8 +347,8 @@ fn compute_fees(amount_msat: u64, channel_fees: RoutingFees) -> Option<u64> {
/// The fees on channels from us to next-hops are ignored (as they are assumed to all be
/// equal), however the enabled/disabled bit on such channels as well as the
/// htlc_minimum_msat/htlc_maximum_msat *are* checked as they may change based on the receiving node.
pub fn get_route<L: Deref>(our_node_id: &PublicKey, network: &NetworkGraph, payee: &PublicKey, payee_features: Option<InvoiceFeatures>, first_hops: Option<&[&ChannelDetails]>,
last_hops: &[&RouteHintHop], final_value_msat: u64, final_cltv: u32, logger: L) -> Result<Route, LightningError> where L::Target: Logger {
pub fn get_route<L: Deref>(our_node_id: &PublicKey, network: &NetworkGraph, payee: &PublicKey, payee_features: Option<InvoiceFeatures>, first_hops: Option<&[ChannelDetails]>,
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oops, ugh, the ChannelDetails reference is here mostly for the bindings - the bindings take an array of pointers with some additional metadata, so they can't trivially convert them to [ChannelDetails] without a bunch of copies.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Dropped the commit but it does make the user do more work at the call site. Here is what the guide is looking like:

let amt_pico_btc = invoice.amount_pico_btc()
    .expect("ERROR: invalid invoice: must contain amount to pay");
let amt_msat = amt_pico_btc / 10;
let payer_pubkey = channel_manager.get_our_node_id();
let network_graph = router.network_graph.read().unwrap();
let payee_pubkey = invoice.recover_payee_pub_key();
let payee_features = invoice.features().cloned();
let first_hops = channel_manager.list_usable_channels();
let last_hops = invoice.route_hints();
let final_cltv = invoice.min_final_cltv_expiry() as u32;

let route = router::get_route(
    &payer_pubkey, &network_graph, &payee_pubkey, payee_features,
    Some(&first_hops.iter().collect::<Vec<_>>()), &last_hops,
    amt_msat, final_cltv, logger.clone(),
).expect("ERROR: failed to find route");

let payment_hash = PaymentHash(invoice.payment_hash().clone().into_inner());
let payment_secret = invoice.payment_secret().cloned();

channel_manager.send_payment(&route, payment_hash, &payment_secret)
    .expect("ERROR: failed to send payment");

Later, we may want take a pass through how our interfaces to see how well they mesh together.

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yea, its very not-ideal, I agree. I'm not really sure how to fix it in the bindings context, though, sadly.

@jkczyz jkczyz force-pushed the 2021-06-get-route-interface branch from b56fdd0 to abf1deb Compare June 11, 2021 08:21
Copy link
Collaborator

@TheBlueMatt TheBlueMatt left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Needs squash.

jkczyz added 2 commits June 11, 2021 08:44
Lightning invoices allow for zero or more multi-hop route hints. Update
get_route's interface to accept such hints, although only the last hop
from each is used for the time being.

Moves RouteHint from lightning-invoice crate to lightning crate. Adds a
PrivateRoute wrapper around RouteHint for use in lightning-invoice.
@jkczyz jkczyz force-pushed the 2021-06-get-route-interface branch from abf1deb to d0355b7 Compare June 11, 2021 15:45
@@ -128,7 +123,7 @@ mod test {
&invoice.recover_payee_pub_key(),
Some(invoice.features().unwrap().clone()),
Some(&first_hops.iter().collect::<Vec<_>>()),
&last_hops.iter().collect::<Vec<_>>(),
&last_hops,
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Much nicer

@TheBlueMatt TheBlueMatt merged commit 7b4b753 into lightningdevkit:main Jun 11, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants