Skip to content

Commit

Permalink
Take in-flight HTLCs by reference in Router::find_route
Browse files Browse the repository at this point in the history
Useful in upcoming work when for payment retries.
  • Loading branch information
valentinewallace committed Dec 21, 2022
1 parent 195eb0a commit 4d33002
Show file tree
Hide file tree
Showing 5 changed files with 15 additions and 15 deletions.
2 changes: 1 addition & 1 deletion fuzz/src/chanmon_consistency.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<Route, msgs::LightningError> {
Err(msgs::LightningError {
err: String::from("Not implemented"),
Expand Down
2 changes: 1 addition & 1 deletion fuzz/src/full_stack.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<Route, msgs::LightningError> {
Err(msgs::LightningError {
err: String::from("Not implemented"),
Expand Down
12 changes: 6 additions & 6 deletions lightning-invoice/src/payment.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<Route, LightningError> { unimplemented!() }
//! # fn notify_payment_path_failed(&self, path: &[&RouteHop], short_channel_id: u64) { unimplemented!() }
//! # fn notify_payment_path_successful(&self, path: &[&RouteHop]) { unimplemented!() }
Expand Down Expand Up @@ -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, &params, Some(&first_hops.iter().collect::<Vec<_>>()), inflight_htlcs
&payer, &params, Some(&first_hops.iter().collect::<Vec<_>>()), &inflight_htlcs
).map_err(|e| PaymentError::Routing(e))?;

match send_payment(&route) {
Expand Down Expand Up @@ -578,7 +578,7 @@ where
let inflight_htlcs = self.payer.inflight_htlcs();

let route = self.router.find_route(
&payer, &params, Some(&first_hops.iter().collect::<Vec<_>>()), inflight_htlcs
&payer, &params, Some(&first_hops.iter().collect::<Vec<_>>()), &inflight_htlcs
);

if route.is_err() {
Expand Down Expand Up @@ -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<Route, LightningError> {
// Simulate calling the Scorer just as you would in find_route
let route = Self::route_for_value(route_params.final_value_msat);
Expand Down Expand Up @@ -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<Route, LightningError> {
Err(LightningError { err: String::new(), action: ErrorAction::IgnoreError })
}
Expand Down Expand Up @@ -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<Route, LightningError> {
self.0.borrow_mut().pop_front().unwrap()
}
Expand Down
10 changes: 5 additions & 5 deletions lightning/src/routing/router.rs
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ impl<G: Deref<Target = NetworkGraph<L>>, 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<Route, LightningError> {
let random_seed_bytes = {
let mut locked_random_seed_bytes = self.random_seed_bytes.lock().unwrap();
Expand Down Expand Up @@ -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<Route, LightningError>;
/// 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<Route, LightningError> {
self.find_route(payer, route_params, first_hops, inflight_htlcs)
Expand All @@ -128,12 +128,12 @@ pub trait Router {
pub struct ScorerAccountingForInFlightHtlcs<'a, S: Score> {
scorer: &'a mut S,
// Maps a channel's short channel id and its direction to the liquidity used up.
inflight_htlcs: InFlightHtlcs,
inflight_htlcs: &'a InFlightHtlcs,
}

impl<'a, S: Score> ScorerAccountingForInFlightHtlcs<'a, S> {
/// Initialize a new `ScorerAccountingForInFlightHtlcs`.
pub fn new(scorer: &'a mut S, inflight_htlcs: InFlightHtlcs) -> Self {
pub fn new(scorer: &'a mut S, inflight_htlcs: &'a InFlightHtlcs) -> Self {
ScorerAccountingForInFlightHtlcs {
scorer,
inflight_htlcs
Expand Down
4 changes: 2 additions & 2 deletions lightning/src/util/test_utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<Route, msgs::LightningError> {
let logger = TestLogger::new();
find_route(
payer, params, &self.network_graph, first_hops, &logger,
&ScorerAccountingForInFlightHtlcs::new(&mut TestScorer::with_penalty(0), inflight_htlcs),
&ScorerAccountingForInFlightHtlcs::new(&mut TestScorer::with_penalty(0), &inflight_htlcs),
&[42; 32]
)
}
Expand Down

0 comments on commit 4d33002

Please sign in to comment.