From 592bfd7c58e73eaa5cfd5032d982883878510037 Mon Sep 17 00:00:00 2001 From: Jeffrey Czyz Date: Thu, 11 Nov 2021 21:47:59 -0600 Subject: [PATCH] Add PaymentHash parameter to Router::find_route Implementations of Router may need the payment hash in order to look up pre-computed routes from a probe for a given payment. Add a PaymentHash parameter to Router::find_route to allow for this. --- lightning-invoice/src/payment.rs | 37 +++++++++++++++----------------- lightning-invoice/src/utils.rs | 4 ++-- 2 files changed, 19 insertions(+), 22 deletions(-) diff --git a/lightning-invoice/src/payment.rs b/lightning-invoice/src/payment.rs index 1e450296b..85786a90a 100644 --- a/lightning-invoice/src/payment.rs +++ b/lightning-invoice/src/payment.rs @@ -65,7 +65,7 @@ //! # struct FakeRouter {}; //! # impl Router for FakeRouter { //! # fn find_route( -//! # &self, payer: &PublicKey, params: &RouteParameters, +//! # &self, payer: &PublicKey, params: &RouteParameters, payment_hash: &PaymentHash, //! # first_hops: Option<&[&ChannelDetails]>, scorer: &S //! # ) -> Result { unimplemented!() } //! # } @@ -180,8 +180,8 @@ pub trait Payer { pub trait Router { /// Finds a [`Route`] between `payer` and `payee` for a payment with the given values. fn find_route( - &self, payer: &PublicKey, params: &RouteParameters, first_hops: Option<&[&ChannelDetails]>, - scorer: &S + &self, payer: &PublicKey, params: &RouteParameters, payment_hash: &PaymentHash, + first_hops: Option<&[&ChannelDetails]>, scorer: &S ) -> Result; } @@ -329,10 +329,8 @@ where let payer = self.payer.node_id(); let first_hops = self.payer.first_hops(); let route = self.router.find_route( - &payer, - params, - Some(&first_hops.iter().collect::>()), - &self.scorer.lock(), + &payer, params, &payment_hash, Some(&first_hops.iter().collect::>()), + &self.scorer.lock() ).map_err(|e| PaymentError::Routing(e))?; match send_payment(&route) { @@ -392,7 +390,10 @@ where let payer = self.payer.node_id(); let first_hops = self.payer.first_hops(); - let route = self.router.find_route(&payer, ¶ms, Some(&first_hops.iter().collect::>()), &self.scorer.lock()); + let route = self.router.find_route( + &payer, ¶ms, &payment_hash, Some(&first_hops.iter().collect::>()), + &self.scorer.lock() + ); if route.is_err() { log_trace!(self.logger, "Failed to find a route for payment {}; not retrying (attempts: {})", log_bytes!(payment_hash.0), attempts); return Err(()); @@ -1187,11 +1188,8 @@ mod tests { impl Router for TestRouter { fn find_route( - &self, - _payer: &PublicKey, - params: &RouteParameters, - _first_hops: Option<&[&ChannelDetails]>, - _scorer: &S, + &self, _payer: &PublicKey, params: &RouteParameters, _payment_hash: &PaymentHash, + _first_hops: Option<&[&ChannelDetails]>, _scorer: &S ) -> Result { Ok(Route { payee: Some(params.payee.clone()), ..Self::route_for_value(params.final_value_msat) @@ -1203,11 +1201,8 @@ mod tests { impl Router for FailingRouter { fn find_route( - &self, - _payer: &PublicKey, - _params: &RouteParameters, - _first_hops: Option<&[&ChannelDetails]>, - _scorer: &S, + &self, _payer: &PublicKey, _params: &RouteParameters, _payment_hash: &PaymentHash, + _first_hops: Option<&[&ChannelDetails]>, _scorer: &S ) -> Result { Err(LightningError { err: String::new(), action: ErrorAction::IgnoreError }) } @@ -1370,8 +1365,10 @@ mod tests { struct ManualRouter(RefCell>>); impl Router for ManualRouter { - fn find_route(&self, _payer: &PublicKey, _params: &RouteParameters, _first_hops: Option<&[&ChannelDetails]>, _scorer: &S) - -> Result { + fn find_route( + &self, _payer: &PublicKey, _params: &RouteParameters, _payment_hash: &PaymentHash, + _first_hops: Option<&[&ChannelDetails]>, _scorer: &S + ) -> Result { self.0.borrow_mut().pop_front().unwrap() } } diff --git a/lightning-invoice/src/utils.rs b/lightning-invoice/src/utils.rs index 641a09ad7..ad94b0804 100644 --- a/lightning-invoice/src/utils.rs +++ b/lightning-invoice/src/utils.rs @@ -112,8 +112,8 @@ impl DefaultRouter where G: Deref, L:: impl Router for DefaultRouter where G: Deref, L::Target: Logger { fn find_route( - &self, payer: &PublicKey, params: &RouteParameters, first_hops: Option<&[&ChannelDetails]>, - scorer: &S + &self, payer: &PublicKey, params: &RouteParameters, _payment_hash: &PaymentHash, + first_hops: Option<&[&ChannelDetails]>, scorer: &S ) -> Result { find_route(payer, params, &*self.network_graph, first_hops, &*self.logger, scorer) } -- 2.39.5