Skip to content

Commit ed86950

Browse files
committed
f - fall back to two-hop payment paths
1 parent fa5bae1 commit ed86950

File tree

1 file changed

+26
-15
lines changed

1 file changed

+26
-15
lines changed

lightning/src/routing/router.rs

Lines changed: 26 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -100,7 +100,7 @@ impl<G: Deref<Target = NetworkGraph<L>> + Clone, L: Deref, S: Deref, SP: Sized,
100100
const MIN_PEER_CHANNELS: usize = 3;
101101

102102
let network_graph = self.network_graph.deref().read_only();
103-
let paths = first_hops.into_iter()
103+
let counterparty_channels = first_hops.into_iter()
104104
.filter(|details| details.counterparty.features.supports_route_blinding())
105105
.filter(|details| amount_msats <= details.inbound_capacity_msat)
106106
.filter(|details| amount_msats >= details.inbound_htlc_minimum_msat.unwrap_or(0))
@@ -147,7 +147,9 @@ impl<G: Deref<Target = NetworkGraph<L>> + Clone, L: Deref, S: Deref, SP: Sized,
147147
htlc_maximum_msat: details.inbound_htlc_maximum_msat.unwrap_or(u64::MAX),
148148
};
149149
Some((forward_node, counterparty_channels))
150-
})
150+
});
151+
152+
let three_hop_paths = counterparty_channels.clone()
151153
// Pair counterparties with their other channels
152154
.flat_map(|(forward_node, counterparty_channels)|
153155
counterparty_channels
@@ -198,20 +200,29 @@ impl<G: Deref<Target = NetworkGraph<L>> + Clone, L: Deref, S: Deref, SP: Sized,
198200
tlvs.clone(), u64::MAX, entropy_source, secp_ctx
199201
)
200202
})
201-
.take(MAX_PAYMENT_PATHS)
202-
.collect::<Result<Vec<_>, _>>();
203+
.take(MAX_PAYMENT_PATHS);
203204

204-
match paths {
205-
Ok(paths) if !paths.is_empty() => Ok(paths),
206-
_ => {
207-
if network_graph.nodes().contains_key(&NodeId::from_pubkey(&recipient)) {
208-
BlindedPath::one_hop_for_payment(recipient, tlvs, entropy_source, secp_ctx)
209-
.map(|path| vec![path])
210-
} else {
211-
Err(())
212-
}
213-
},
214-
}
205+
let two_hop_paths = counterparty_channels
206+
.map(|(forward_node, _)| {
207+
BlindedPath::new_for_payment(
208+
&[forward_node], recipient, tlvs.clone(), u64::MAX, entropy_source, secp_ctx
209+
)
210+
})
211+
.take(MAX_PAYMENT_PATHS);
212+
213+
three_hop_paths
214+
.collect::<Result<Vec<_>, _>>().ok()
215+
.and_then(|paths| (!paths.is_empty()).then(|| paths))
216+
.or_else(|| two_hop_paths.collect::<Result<Vec<_>, _>>().ok())
217+
.and_then(|paths| (!paths.is_empty()).then(|| paths))
218+
.or_else(|| network_graph
219+
.node(&NodeId::from_pubkey(&recipient)).ok_or(())
220+
.and_then(|_|
221+
BlindedPath::one_hop_for_payment(recipient, tlvs, entropy_source, secp_ctx))
222+
.map(|path| vec![path])
223+
.ok()
224+
)
225+
.ok_or(())
215226
}
216227
}
217228

0 commit comments

Comments
 (0)