Skip to content

Commit a529010

Browse files
committed
f - fall back to two-hop payment paths
1 parent bc3dffc commit a529010

File tree

1 file changed

+26
-15
lines changed

1 file changed

+26
-15
lines changed

lightning/src/routing/router.rs

+26-15
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))
@@ -142,7 +142,9 @@ impl<G: Deref<Target = NetworkGraph<L>> + Clone, L: Deref, S: Deref, SP: Sized,
142142
htlc_maximum_msat: details.inbound_htlc_maximum_msat.unwrap_or(u64::MAX),
143143
};
144144
Some((forward_node, counterparty_channels))
145-
})
145+
});
146+
147+
let three_hop_paths = counterparty_channels.clone()
146148
// Pair counterparties with their other channels
147149
.flat_map(|(forward_node, counterparty_channels)|
148150
counterparty_channels
@@ -194,20 +196,29 @@ impl<G: Deref<Target = NetworkGraph<L>> + Clone, L: Deref, S: Deref, SP: Sized,
194196
tlvs.clone(), u64::MAX, entropy_source, secp_ctx
195197
))
196198
})
197-
.take(MAX_PAYMENT_PATHS)
198-
.collect::<Result<Vec<_>, _>>();
199+
.take(MAX_PAYMENT_PATHS);
199200

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

0 commit comments

Comments
 (0)