From: Valentine Wallace Date: Mon, 19 Dec 2022 05:26:58 +0000 (-0500) Subject: Take in-flight HTLCs by reference in Router::find_route X-Git-Tag: v0.0.114-beta~73^2~4 X-Git-Url: http://git.bitcoin.ninja/?a=commitdiff_plain;h=3a1982c74111f749853c307a192a7fb276612076;p=rust-lightning Take in-flight HTLCs by reference in Router::find_route Useful in upcoming work when for payment retries. --- diff --git a/fuzz/src/chanmon_consistency.rs b/fuzz/src/chanmon_consistency.rs index f2abb6111..e1bf9cb54 100644 --- a/fuzz/src/chanmon_consistency.rs +++ b/fuzz/src/chanmon_consistency.rs @@ -90,7 +90,7 @@ struct FuzzRouter {} impl Router for FuzzRouter { fn find_route( &self, _payer: &PublicKey, _params: &RouteParameters, _first_hops: Option<&[&ChannelDetails]>, - _inflight_htlcs: InFlightHtlcs + _inflight_htlcs: &InFlightHtlcs ) -> Result { Err(msgs::LightningError { err: String::from("Not implemented"), diff --git a/fuzz/src/full_stack.rs b/fuzz/src/full_stack.rs index 26b17fb7f..2d9be101e 100644 --- a/fuzz/src/full_stack.rs +++ b/fuzz/src/full_stack.rs @@ -132,7 +132,7 @@ struct FuzzRouter {} impl Router for FuzzRouter { fn find_route( &self, _payer: &PublicKey, _params: &RouteParameters, _first_hops: Option<&[&ChannelDetails]>, - _inflight_htlcs: InFlightHtlcs + _inflight_htlcs: &InFlightHtlcs ) -> Result { Err(msgs::LightningError { err: String::from("Not implemented"), diff --git a/lightning-invoice/src/payment.rs b/lightning-invoice/src/payment.rs index 03dc9361f..9fb472d3b 100644 --- a/lightning-invoice/src/payment.rs +++ b/lightning-invoice/src/payment.rs @@ -76,7 +76,7 @@ //! # impl Router for FakeRouter { //! # fn find_route( //! # &self, payer: &PublicKey, params: &RouteParameters, -//! # first_hops: Option<&[&ChannelDetails]>, _inflight_htlcs: InFlightHtlcs +//! # first_hops: Option<&[&ChannelDetails]>, _inflight_htlcs: &InFlightHtlcs //! # ) -> Result { unimplemented!() } //! # fn notify_payment_path_failed(&self, path: &[&RouteHop], short_channel_id: u64) { unimplemented!() } //! # fn notify_payment_path_successful(&self, path: &[&RouteHop]) { unimplemented!() } @@ -510,7 +510,7 @@ where let first_hops = self.payer.first_hops(); let inflight_htlcs = self.payer.inflight_htlcs(); let route = self.router.find_route( - &payer, ¶ms, Some(&first_hops.iter().collect::>()), inflight_htlcs + &payer, ¶ms, Some(&first_hops.iter().collect::>()), &inflight_htlcs ).map_err(|e| PaymentError::Routing(e))?; match send_payment(&route) { @@ -578,7 +578,7 @@ where let inflight_htlcs = self.payer.inflight_htlcs(); let route = self.router.find_route( - &payer, ¶ms, Some(&first_hops.iter().collect::>()), inflight_htlcs + &payer, ¶ms, Some(&first_hops.iter().collect::>()), &inflight_htlcs ); if route.is_err() { @@ -1670,7 +1670,7 @@ mod tests { impl Router for TestRouter { fn find_route( &self, payer: &PublicKey, route_params: &RouteParameters, - _first_hops: Option<&[&ChannelDetails]>, inflight_htlcs: InFlightHtlcs + _first_hops: Option<&[&ChannelDetails]>, inflight_htlcs: &InFlightHtlcs ) -> Result { // Simulate calling the Scorer just as you would in find_route let route = Self::route_for_value(route_params.final_value_msat); @@ -1723,7 +1723,7 @@ mod tests { impl Router for FailingRouter { fn find_route( &self, _payer: &PublicKey, _params: &RouteParameters, _first_hops: Option<&[&ChannelDetails]>, - _inflight_htlcs: InFlightHtlcs, + _inflight_htlcs: &InFlightHtlcs, ) -> Result { Err(LightningError { err: String::new(), action: ErrorAction::IgnoreError }) } @@ -2011,7 +2011,7 @@ mod tests { impl Router for ManualRouter { fn find_route( &self, _payer: &PublicKey, _params: &RouteParameters, _first_hops: Option<&[&ChannelDetails]>, - _inflight_htlcs: InFlightHtlcs + _inflight_htlcs: &InFlightHtlcs ) -> Result { self.0.borrow_mut().pop_front().unwrap() } diff --git a/lightning/src/routing/router.rs b/lightning/src/routing/router.rs index 6fc3d4afa..3f14b0c6a 100644 --- a/lightning/src/routing/router.rs +++ b/lightning/src/routing/router.rs @@ -61,7 +61,7 @@ impl>, L: Deref, S: Deref> Router for DefaultR { fn find_route( &self, payer: &PublicKey, params: &RouteParameters, first_hops: Option<&[&ChannelDetails]>, - inflight_htlcs: InFlightHtlcs + inflight_htlcs: &InFlightHtlcs ) -> Result { let random_seed_bytes = { let mut locked_random_seed_bytes = self.random_seed_bytes.lock().unwrap(); @@ -98,13 +98,13 @@ pub trait Router { /// Finds a [`Route`] between `payer` and `payee` for a payment with the given values. fn find_route( &self, payer: &PublicKey, route_params: &RouteParameters, - first_hops: Option<&[&ChannelDetails]>, inflight_htlcs: InFlightHtlcs + first_hops: Option<&[&ChannelDetails]>, inflight_htlcs: &InFlightHtlcs ) -> Result; /// Finds a [`Route`] between `payer` and `payee` for a payment with the given values. Includes /// `PaymentHash` and `PaymentId` to be able to correlate the request with a specific payment. fn find_route_with_id( &self, payer: &PublicKey, route_params: &RouteParameters, - first_hops: Option<&[&ChannelDetails]>, inflight_htlcs: InFlightHtlcs, + first_hops: Option<&[&ChannelDetails]>, inflight_htlcs: &InFlightHtlcs, _payment_hash: PaymentHash, _payment_id: PaymentId ) -> Result { self.find_route(payer, route_params, first_hops, inflight_htlcs) @@ -125,15 +125,15 @@ pub trait Router { /// [`find_route`]. /// /// [`Score`]: crate::routing::scoring::Score -pub struct ScorerAccountingForInFlightHtlcs { +pub struct ScorerAccountingForInFlightHtlcs<'a, S: Score> { scorer: S, // Maps a channel's short channel id and its direction to the liquidity used up. - inflight_htlcs: InFlightHtlcs, + inflight_htlcs: &'a InFlightHtlcs, } -impl ScorerAccountingForInFlightHtlcs { +impl<'a, S: Score> ScorerAccountingForInFlightHtlcs<'a, S> { /// Initialize a new `ScorerAccountingForInFlightHtlcs`. - pub fn new(scorer: S, inflight_htlcs: InFlightHtlcs) -> Self { + pub fn new(scorer: S, inflight_htlcs: &'a InFlightHtlcs) -> Self { ScorerAccountingForInFlightHtlcs { scorer, inflight_htlcs @@ -142,11 +142,11 @@ impl ScorerAccountingForInFlightHtlcs { } #[cfg(c_bindings)] -impl Writeable for ScorerAccountingForInFlightHtlcs { +impl<'a, S: Score> Writeable for ScorerAccountingForInFlightHtlcs<'a, S> { fn write(&self, writer: &mut W) -> Result<(), io::Error> { self.scorer.write(writer) } } -impl Score for ScorerAccountingForInFlightHtlcs { +impl<'a, S: Score> Score for ScorerAccountingForInFlightHtlcs<'a, S> { fn channel_penalty_msat(&self, short_channel_id: u64, source: &NodeId, target: &NodeId, usage: ChannelUsage) -> u64 { if let Some(used_liquidity) = self.inflight_htlcs.used_liquidity_msat( source, target, short_channel_id diff --git a/lightning/src/util/test_utils.rs b/lightning/src/util/test_utils.rs index 53e8e1d45..9ca517964 100644 --- a/lightning/src/util/test_utils.rs +++ b/lightning/src/util/test_utils.rs @@ -86,12 +86,12 @@ impl<'a> TestRouter<'a> { impl<'a> Router for TestRouter<'a> { fn find_route( &self, payer: &PublicKey, params: &RouteParameters, first_hops: Option<&[&channelmanager::ChannelDetails]>, - inflight_htlcs: InFlightHtlcs + inflight_htlcs: &InFlightHtlcs ) -> Result { let logger = TestLogger::new(); find_route( payer, params, &self.network_graph, first_hops, &logger, - &ScorerAccountingForInFlightHtlcs::new(TestScorer::with_penalty(0), inflight_htlcs), + &ScorerAccountingForInFlightHtlcs::new(TestScorer::with_penalty(0), &inflight_htlcs), &[42; 32] ) }