Skip to content

Commit dbe667d

Browse files
committed
Enhance documentation for find_route function, detailing parameters, routing behavior, and error handling
1 parent ed9e157 commit dbe667d

File tree

1 file changed

+74
-29
lines changed

1 file changed

+74
-29
lines changed

lightning/src/routing/router.rs

Lines changed: 74 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -2461,48 +2461,93 @@ fn sort_first_hop_channels(
24612461

24622462
/// Finds a route from us (payer) to the given target node (payee).
24632463
///
2464-
/// If the payee provided features in their invoice, they should be provided via the `payee` field
2465-
/// in the given [`RouteParameters::payment_params`].
2466-
/// Without this, MPP will only be used if the payee's features are available in the network graph.
2464+
/// This function computes one or more payment paths for sending a payment to the payee while
2465+
/// respecting all constraints like fees, CLTV expiry deltas, channel liquidity, and the payee's
2466+
/// feature requirements. You'll get multiple paths back if multi-path payments (MPP) are needed.
24672467
///
2468-
/// Private routing paths between a public node and the target may be included in the `payee` field
2469-
/// of [`RouteParameters::payment_params`].
2468+
/// # Parameters
24702469
///
2471-
/// If some channels aren't announced, it may be useful to fill in `first_hops` with the results
2472-
/// from [`ChannelManager::list_usable_channels`]. If it is filled in, the view of these channels
2473-
/// from `network_graph` will be ignored, and only those in `first_hops` will be used.
2470+
/// * `our_node_pubkey` - The public key of the payer (us).
2471+
/// * `route_params` - Payment parameters including the final value, payee information, feature
2472+
/// requirements from any invoice, and routing constraints like max fees and path count.
2473+
/// * `network_graph` - The network graph with all known channels and nodes.
2474+
/// * `first_hops` - Channels directly connected to us. If you provide this, it takes priority
2475+
/// over what's in the network graph, which is useful for unannounced channels. You can get
2476+
/// this from [`ChannelManager::list_usable_channels`].
2477+
/// * `logger` - For logging routing progress and debug info.
2478+
/// * `scorer` - The implementation that ranks and selects paths based on reliability, liquidity,
2479+
/// and congestion metrics.
2480+
/// * `score_params` - Parameters for tuning the scorer's behavior.
2481+
/// * `random_seed_bytes` - A random seed for adding privacy-enhancing offsets to CLTV values.
24742482
///
2475-
/// The fees on channels from us to the next hop are ignored as they are assumed to all be equal.
2476-
/// However, the enabled/disabled bit on such channels as well as the `htlc_minimum_msat` /
2477-
/// `htlc_maximum_msat` *are* checked as they may change based on the receiving node.
2483+
/// # Payee Information
24782484
///
2479-
/// Routes are constructed by searching the provided [`NetworkGraph`] for paths from the payer
2480-
/// to the payee that satisfy the constraints in [`RouteParameters`]. Fees are accumulated
2481-
/// backwards from the payee to the payer, and CLTV expiry deltas are aggregated hop-by-hop.
2485+
/// How we handle the payee depends on what they provided:
2486+
/// * Bolt11 invoices: Include features via [`RouteParameters::payment_params`] to enable MPP.
2487+
/// Without features, MPP only works if the payee is already known in the network graph.
2488+
/// * Bolt12 invoices: May include blinded paths for privacy. We unblind these in the returned
2489+
/// [`Route`] so you can actually construct the payment.
2490+
/// * BOLT11 route hints: Private hops near the payee get included in the final route.
24822491
///
2483-
/// Channel selection and path ranking are influenced by the provided [`ScoreLookUp`]
2484-
/// implementation, which may apply penalties based on historical reliability,
2485-
/// liquidity hints, or in-flight HTLCs.
2492+
/// # First Hop Behavior
24862493
///
2487-
/// If `first_hops` is provided, those channels are used as the only outbound candidates
2488-
/// from the payer, overriding the corresponding entries in the [`NetworkGraph`].
2494+
/// Fees on channels from us to the next hop are ignored during routing since we assume all
2495+
/// first-hop channels cost the same. However, we do check the channel's enabled/disabled status
2496+
/// and HTLC limits (htlc_minimum_msat and htlc_maximum_msat) since those can vary.
2497+
///
2498+
/// # How It Works
2499+
///
2500+
/// We use a modified Dijkstra's algorithm, searching backwards from the payee to us:
2501+
///
2502+
/// 1. Fee calculation: We total up fees at each hop from the payee going back to the payer,
2503+
/// computing them based on the amount being routed. This gives us accurate estimates because
2504+
/// the amount increases as we move backward toward the payer.
2505+
/// 2. CLTV deltas: Each hop's CLTV expiry delta is aggregated hop-by-hop to ensure we have
2506+
/// enough time for on-chain resolution if needed.
2507+
/// 3. Penalties: The scorer's penalties (from your provided scorer) are factored directly into
2508+
/// the pathfinding algorithm itself, not just applied afterward. This means channels with
2509+
/// higher penalties are naturally less likely to be selected during the search.
2510+
/// 4. Path construction: We build multiple candidate paths while staying within the fee limit
2511+
/// and respecting all liquidity and feature constraints.
2512+
/// 5. Value provisioning: We collect paths until the total value is roughly 3x the requested
2513+
/// payment (this gives us room to find cheaper combinations), then we pick the cheapest set.
2514+
///
2515+
/// # Scoring and Channel Selection
2516+
///
2517+
/// The scorer you provide influences which paths we select during the actual pathfinding. The
2518+
/// scorer assigns penalties to channels based on factors like:
2519+
/// * How reliable the channel has been historically
2520+
/// * Known liquidity constraints and in-flight HTLCs
2521+
/// * Estimated network congestion
2522+
///
2523+
/// These penalties are built into the algorithm's scoring, so channels with higher penalties are
2524+
/// naturally deprioritized as we search for paths, letting us favor more reliable and liquid
2525+
/// options.
2526+
///
2527+
/// # Returns
2528+
///
2529+
/// On success, you get a [`Route`] with one or more payment paths. The route respects all
2530+
/// your constraints and is ready to use for payment construction.
24892531
///
24902532
/// # Errors
24912533
///
2492-
/// Returns an error if:
2493-
/// - The payer and payee are the same node.
2494-
/// - The payment amount is zero or exceeds the maximum representable value.
2495-
/// - The total CLTV expiry constraint cannot accommodate the payee's required final CLTV delta.
2496-
/// - No route satisfying liquidity, feature, and policy constraints can be found.
2534+
/// The function returns an error if:
2535+
/// * The payer and payee are the same node (that doesn't make sense).
2536+
/// * The payment amount is zero or larger than [`MAX_VALUE_MSAT`].
2537+
/// * The total CLTV delta constraint is too tight for the payee's final CLTV delta.
2538+
/// * No route can be found that meets all the liquidity, feature, and policy constraints.
24972539
///
24982540
/// # Panics
24992541
///
2500-
/// Panics if first_hops contains channels without `short_channel_id`s;
2501-
/// [`ChannelManager::list_usable_channels`] will never include such channels.
2542+
/// This will panic if `first_hops` contains channels without a short_channel_id. This shouldn't
2543+
/// happen in practice since [`ChannelManager::list_usable_channels`] always provides proper IDs,
2544+
/// so it's safe to pass that output directly.
2545+
///
2546+
/// # See Also
25022547
///
2503-
/// [`ChannelManager::list_usable_channels`]: crate::ln::channelmanager::ChannelManager::list_usable_channels
2504-
/// [`Event::PaymentPathFailed`]: crate::events::Event::PaymentPathFailed
2505-
/// [`NetworkGraph`]: crate::routing::gossip::NetworkGraph
2548+
/// [`ChannelManager::list_usable_channels`] to get the first_hops list.
2549+
/// [`NetworkGraph`] for more on the network graph parameter.
2550+
/// [`RouteParameters`] for available routing constraints.
25062551
#[rustfmt::skip]
25072552
pub fn find_route<L: Logger, GL: Logger, S: ScoreLookUp>(
25082553
our_node_pubkey: &PublicKey, route_params: &RouteParameters,

0 commit comments

Comments
 (0)