-/// A [`Router`] implemented using [`find_route`].
-pub struct DefaultRouter<G: Deref<Target = NetworkGraph<L>>, L: Deref, S: Deref> where
- L::Target: Logger,
- S::Target: for <'a> LockableScore<'a>,
-{
- network_graph: G,
- logger: L,
- random_seed_bytes: Mutex<[u8; 32]>,
- scorer: S
-}
-
-impl<G: Deref<Target = NetworkGraph<L>>, L: Deref, S: Deref> DefaultRouter<G, L, S> where
- L::Target: Logger,
- S::Target: for <'a> LockableScore<'a>,
-{
- /// Creates a new router using the given [`NetworkGraph`], a [`Logger`], and a randomness source
- /// `random_seed_bytes`.
- pub fn new(network_graph: G, logger: L, random_seed_bytes: [u8; 32], scorer: S) -> Self {
- let random_seed_bytes = Mutex::new(random_seed_bytes);
- Self { network_graph, logger, random_seed_bytes, scorer }
- }
-}
-
-impl<G: Deref<Target = NetworkGraph<L>>, L: Deref, S: Deref> Router for DefaultRouter<G, L, S> where
- L::Target: Logger,
- S::Target: for <'a> LockableScore<'a>,
-{
- fn find_route(
- &self, payer: &PublicKey, params: &RouteParameters, _payment_hash: &PaymentHash,
- first_hops: Option<&[&ChannelDetails]>, inflight_htlcs: InFlightHtlcs
- ) -> Result<Route, LightningError> {
- let random_seed_bytes = {
- let mut locked_random_seed_bytes = self.random_seed_bytes.lock().unwrap();
- *locked_random_seed_bytes = sha256::Hash::hash(&*locked_random_seed_bytes).into_inner();
- *locked_random_seed_bytes
- };
-
- find_route(
- payer, params, &self.network_graph, first_hops, &*self.logger,
- &ScorerAccountingForInFlightHtlcs::new(&mut self.scorer.lock(), inflight_htlcs),
- &random_seed_bytes
- )
- }
-
- fn notify_payment_path_failed(&self, path: Vec<&RouteHop>, short_channel_id: u64) {
- self.scorer.lock().payment_path_failed(&path, short_channel_id);
- }
-
- fn notify_payment_path_successful(&self, path: Vec<&RouteHop>) {
- self.scorer.lock().payment_path_successful(&path);
- }
-
- fn notify_payment_probe_successful(&self, path: Vec<&RouteHop>) {
- self.scorer.lock().probe_successful(&path);
- }
-
- fn notify_payment_probe_failed(&self, path: Vec<&RouteHop>, short_channel_id: u64) {
- self.scorer.lock().probe_failed(&path, short_channel_id);
- }
-}
-
-impl<Signer: Sign, M: Deref, T: Deref, K: Deref, F: Deref, L: Deref> Payer for ChannelManager<Signer, M, T, K, F, L>
-where
- M::Target: chain::Watch<Signer>,
- T::Target: BroadcasterInterface,
- K::Target: KeysInterface<Signer = Signer>,
- F::Target: FeeEstimator,
- L::Target: Logger,
-{
- fn node_id(&self) -> PublicKey {
- self.get_our_node_id()
- }
-
- fn first_hops(&self) -> Vec<ChannelDetails> {
- self.list_usable_channels()
- }
-
- fn send_payment(
- &self, route: &Route, payment_hash: PaymentHash, payment_secret: &Option<PaymentSecret>
- ) -> Result<PaymentId, PaymentSendFailure> {
- self.send_payment(route, payment_hash, payment_secret)
- }
-
- fn send_spontaneous_payment(
- &self, route: &Route, payment_preimage: PaymentPreimage,
- ) -> Result<PaymentId, PaymentSendFailure> {
- self.send_spontaneous_payment(route, Some(payment_preimage))
- .map(|(_, payment_id)| payment_id)
- }
-
- fn retry_payment(
- &self, route: &Route, payment_id: PaymentId
- ) -> Result<(), PaymentSendFailure> {
- self.retry_payment(route, payment_id)
- }
-
- fn abandon_payment(&self, payment_id: PaymentId) {
- self.abandon_payment(payment_id)
- }
-}
-
-
-/// Used to store information about all the HTLCs that are inflight across all payment attempts.
-pub(crate) 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,
-}
-
-impl<'a, S: Score> ScorerAccountingForInFlightHtlcs<'a, S> {
- pub(crate) fn new(scorer: &'a mut S, inflight_htlcs: InFlightHtlcs) -> Self {
- ScorerAccountingForInFlightHtlcs {
- scorer,
- inflight_htlcs
- }
+/// prefer_current_channel chooses a channel to use for route hints between a currently selected and candidate
+/// channel based on the inbound capacity of each channel and the minimum inbound capacity requested for the hints,
+/// returning true if the current channel should be preferred over the candidate channel.
+/// * If no minimum amount is requested, the channel with the most inbound is chosen to maximize the chances that a
+/// payment of any size will succeed.
+/// * If we have channels with inbound above our minimum requested inbound (plus a 10% scaling factor, expressed as a
+/// percentage) then we choose the lowest inbound channel with above this amount. If we have sufficient inbound
+/// channels, we don't want to deplete our larger channels with small payments (the off-chain version of "grinding
+/// our change").
+/// * If no channel above our minimum amount exists, then we just prefer the channel with the most inbound to give
+/// payments the best chance of succeeding in multiple parts.
+fn prefer_current_channel(min_inbound_capacity_msat: Option<u64>, current_channel: u64,
+ candidate_channel: u64) -> bool {
+
+ // If no min amount is given for the hints, err of the side of caution and choose the largest channel inbound to
+ // maximize chances of any payment succeeding.
+ if min_inbound_capacity_msat.is_none() {
+ return current_channel > candidate_channel