Skip to content

Use cost / path amt limit as the pathfinding score, not cost #3890

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 5 commits into
base: main
Choose a base branch
from

Conversation

TheBlueMatt
Copy link
Collaborator

While walking nodes in our Dijkstra's pathfinding, we may find a
channel which is amount-limited to less than the amount we're
currently trying to send. This is fine, and when we encounter such
nodes we simply limit the amount we'd send in this path if we pick
the channel.

When we encounter such a path, we keep summing the cost across hops
as we go, keeping whatever scores we assigned to channels between
the amount-limited one and the recipient, but using the new limited
amount for any channels we look at later as we walk towards the
sender.

This leads to somewhat inconsistent scores, especially as our
scorer assigns a large portion of its penalties and a portion of
network fees are proportional to the amount. Thus, we end up with a
somewhat higher score than we "should" for this path as later hops
use a high proportional cost. We accepted this as a simple way to
bias against small-value paths and many MPP parts.

Sadly, in practice it appears our bias is not strong enough, as
several users have reported that we often attempt far too many MPP
parts. In practice, if we encounter a channel with a small limit
early in the Dijkstra's pass (towards the end of the path), we may
prefer it over many other paths as we start assigning very low
costs early on before we've accumulated much cost from larger
channels.

Here, we swap the `cost` Dijkstra's score for `cost / path amount`.
This should bias much stronger against many MPP parts by preferring
larger paths proportionally to their amount.

This somewhat better aligns with our goal - if we have to pick
multiple paths, we should be searching for paths the optimize
fee-per-sat-sent, not strictly the fee paid.

However, it might bias us against smaller paths somewhat stronger
than we want - because we're still using the fees/scores calculated
with the sought amount for hops processed already, but are now
dividing by a smaller sent amount when walking further hops, we
will bias "incorrectly" (and fairly strongly) against smaller
parts.

Still, because of the complaints on pathfinding performance due to
too many MPP paths, it seems like a worthwhile tradeoff, as
ultimately MPP splitting is always the domain of heuristics anyway.

I'm somewhat optimistically labeling this "backport 0.1", since we've been doing rather large pathfinding changes in backports anyway, and this directly addresses a major user complaint (with a rather small patch), so it seems worth backporting. However, it does come with some potential for tradeoffs, so open to discussion here.

`RouteGraphNode` is the main heap entry in our dijkstra's next-best
heap. Thus, because its rather constantly being sorted, we care a
good bit about its size as fitting more of them on a cache line can
provide some additional speed.

In 43d250d, we switched from
tracking nodes during pathfinding by their `NodeId` to a "counter"
which allows us to avoid `HashMap`s lookups for much of the
pathfinding process.

Because the `dist` lookup is now quite cheap (its just a `Vec`),
there's no reason to track `NodeId`s in the heap entries. Instead,
we simply fetch the `NodeId` of the node via the `dist` map by
examining its `candidate`'s pointer to its source `NodeId`.

This allows us to remove a `NodeId` in `RouteGraphNode`, moving it
from 64 to 32 bytes. This allows us to expand the `score` field
size in a coming commit without expanding `RouteGraphNode`'s size.

While we were doing the `dist` lookup in
`add_entries_to_cheapest_to_target_node` anyway, the `NodeId`
lookup via the `candidate` may not be free. Still, avoiding
expanding `RouteGraphNode` above 128 bytes in a few commits is a
nice win.
We track the total CLTV from the recipient to the current hop in
`RouteGraphNode` so that we can limit its total during pathfinding.
While its great to use a `u32` for that to match existing CLTV
types, allowing a total CLTV limit of 64K blocks (455 days) is
somewhat absurd, so here we swap the `total_cltv_delta` to a `u16`.

This keeps `RouteGraphNode` to 32 bytes in a coming commit as we
expand `score`.
While walking nodes in our Dijkstra's pathfinding, we may find a
channel which is amount-limited to less than the amount we're
currently trying to send. This is fine, and when we encounter such
nodes we simply limit the amount we'd send in this path if we pick
the channel.

When we encounter such a path, we keep summing the cost across hops
as we go, keeping whatever scores we assigned to channels between
the amount-limited one and the recipient, but using the new limited
amount for any channels we look at later as we walk towards the
sender.

This leads to somewhat inconsistent scores, especially as our
scorer assigns a large portion of its penalties and a portion of
network fees are proportional to the amount. Thus, we end up with a
somewhat higher score than we "should" for this path as later hops
use a high proportional cost. We accepted this as a simple way to
bias against small-value paths and many MPP parts.

Sadly, in practice it appears our bias is not strong enough, as
several users have reported that we often attempt far too many MPP
parts. In practice, if we encounter a channel with a small limit
early in the Dijkstra's pass (towards the end of the path), we may
prefer it over many other paths as we start assigning very low
costs early on before we've accumulated much cost from larger
channels.

Here, we swap the `cost` Dijkstra's score for `cost / path amount`.
This should bias much stronger against many MPP parts by preferring
larger paths proportionally to their amount.

This somewhat better aligns with our goal - if we have to pick
multiple paths, we should be searching for paths the optimize
fee-per-sat-sent, not strictly the fee paid.

However, it might bias us against smaller paths somewhat stronger
than we want - because we're still using the fees/scores calculated
with the sought amount for hops processed already, but are now
dividing by a smaller sent amount when walking further hops, we
will bias "incorrectly" (and fairly strongly) against smaller
parts.

Still, because of the complaints on pathfinding performance due to
too many MPP paths, it seems like a worthwhile tradeoff, as
ultimately MPP splitting is always the domain of heuristics anyway.
@ldk-reviews-bot
Copy link

ldk-reviews-bot commented Jun 25, 2025

👋 Thanks for assigning @tnull as a reviewer!
I'll wait for their review and will help manage the review process.
Once they submit their review, I'll check if a second reviewer would be helpful.

@ldk-reviews-bot
Copy link

🔔 1st Reminder

Hey @tnull @valentinewallace! This PR has been waiting for your review.
Please take a look when you have a chance. If you're unable to review, please let us know so we can find another reviewer.

1 similar comment
@ldk-reviews-bot
Copy link

🔔 1st Reminder

Hey @tnull @valentinewallace! This PR has been waiting for your review.
Please take a look when you have a chance. If you're unable to review, please let us know so we can find another reviewer.

@ldk-reviews-bot
Copy link

🔔 2nd Reminder

Hey @tnull @valentinewallace! This PR has been waiting for your review.
Please take a look when you have a chance. If you're unable to review, please let us know so we can find another reviewer.

1 similar comment
@ldk-reviews-bot
Copy link

🔔 2nd Reminder

Hey @tnull @valentinewallace! This PR has been waiting for your review.
Please take a look when you have a chance. If you're unable to review, please let us know so we can find another reviewer.

Copy link
Contributor

@valentinewallace valentinewallace left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Concept ACK, can we add a test?

Copy link
Contributor

@tnull tnull left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hmm, re:

Here, we swap the `cost` Dijkstra's score for `cost / path amount`.
This should bias much stronger against many MPP parts by preferring
larger paths proportionally to their amount.

I vaguely recall some discussion some years back, but even after some digging was unable to find the corresponding PR to reestablish context unfortunately.

.checked_sub(2*MEDIAN_HOP_CLTV_EXPIRY_DELTA)
.unwrap_or(payment_params.max_total_cltv_expiry_delta - final_cltv_expiry_delta)
.try_into()
.unwrap_or(u16::MAX);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If we already do this, can we adjust the field type in PaymentParameters, and maybe also final_cltv_expiry_delta? It's kind of odd to have a more precise type in the API, just to always shrink it to u16 in practice, no?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Did it for the backport. I guess we could do it differently for 0.2 but maybe I'll do that as a followup to this for 0.2?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Did it for the backport. I guess we could do it differently for 0.2 but maybe I'll do that as a followup to this for 0.2?

Could also do it in this PR and use the current version in a new PR against 0.1? But, up to you as long as it doesn't get lost. Mind opening an issue?

@TheBlueMatt
Copy link
Collaborator Author

I vaguely recall some discussion some years back, but even after some digging was unable to find the corresponding PR to reestablish context unfortunately.

Same, I know we've discussed it before...

if fee_cost == u64::MAX {
u64::MAX.into()
} else {
((fee_cost as u128) << 64) / self.get_value_msat() as u128
Copy link
Contributor

@tnull tnull Jul 3, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Are we certain the divisor can't ever be 0 here? Should we throw in a + 1 or use checked_div for good measure?

u128::MAX
};
let new_cost = if new_fee_cost != u64::MAX {
((new_fee_cost as u128) << 64) / value_contribution_msat as u128
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Same here, could value_contribution_msat ever become 0? How do we protect against it?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

MPP routing appears to create significantly more shards than is required
4 participants