Skip to content

Commit 495403d

Browse files
committed
lightningd: prefer to forward on hinted channel
When receiving a forward request lightningd selects the best channel with the next peer based on its ability to add an HTLC and its spendable amount. If two forwarding request arrive at the same time pointing to the same peer we can have a race condition in which both HTLCs compete concurrently for the same spendable amount. There shouldn't be a race condition if both requests are supposed to be allocated on different parallel channels, hence instead of selecting the "best channel" we select the hinted channel unless it cannot forward, in that case we fallback to the "best channel". Changelog-Fixed: racy forwarding requests in the presence of parallel channels. Signed-off-by: Lagrang3 <[email protected]>
1 parent 9d88ce3 commit 495403d

File tree

1 file changed

+10
-0
lines changed

1 file changed

+10
-0
lines changed

lightningd/peer_htlcs.c

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -668,6 +668,16 @@ static struct channel *best_channel(struct lightningd *ld,
668668
struct amount_msat amt_to_forward,
669669
struct channel *hint)
670670
{
671+
/* Prefer using hint if it can forward the request. */
672+
if (hint && channel_state_can_add_htlc(hint->state)) {
673+
struct amount_msat spendable;
674+
spendable = channel_amount_spendable(hint);
675+
if (amount_msat_greater_eq(
676+
amt_to_forward,
677+
hint->channel_info.their_config.htlc_minimum) &&
678+
amount_msat_less_eq(amt_to_forward, spendable))
679+
return hint;
680+
}
671681
struct amount_msat best_spendable = AMOUNT_MSAT(0);
672682
struct channel *channel, *best = hint;
673683

0 commit comments

Comments
 (0)