Skip to content
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

pay: Annotate both local alias and real scid with channel hints #6428

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
43 changes: 32 additions & 11 deletions lightningd/pay.c
Original file line number Diff line number Diff line change
Expand Up @@ -830,32 +830,53 @@ static struct command_result *check_invoice_request_usage(struct command *cmd,
return NULL;
}

static struct channel *find_channel_for_htlc_add(struct lightningd *ld,
const struct node_id *node,
const struct short_channel_id *scid_or_alias)
static struct channel *
find_channel_for_htlc_add(struct lightningd *ld, const struct node_id *node,
const struct short_channel_id *scid_or_alias,
const struct amount_msat *amount)
{
struct channel *channel;
struct short_channel_id *scid;
struct peer *peer = peer_by_id(ld, node);
if (!peer)
return NULL;

channel = find_channel_by_scid(peer, scid_or_alias);
if (channel && channel_can_add_htlc(channel))
return channel;
if (channel && channel_can_add_htlc(channel)) {
goto found;
}

channel = find_channel_by_alias(peer, scid_or_alias, LOCAL);
if (channel && channel_can_add_htlc(channel))
return channel;
if (channel && channel_can_add_htlc(channel)) {
goto found;
}

/* We used to ignore scid: now all-zero means "any" */
if (!channel && (ld->deprecated_apis || memeqzero(scid_or_alias, sizeof(*scid_or_alias)))) {
if (!channel && (ld->deprecated_apis ||
memeqzero(scid_or_alias, sizeof(*scid_or_alias)))) {
list_for_each(&peer->channels, channel, list) {
if (channel_can_add_htlc(channel)) {
return channel;
if (channel_can_add_htlc(channel) &&
amount_msat_greater(channel->our_msat, *amount)) {
goto found;
}
}
}

log_debug(ld->log, "No channel found for selector %s (%s)",
short_channel_id_to_str(tmpctx, scid_or_alias),
type_to_string(tmpctx, struct amount_msat, amount));
return NULL;

found:
scid = channel->scid ? channel->scid : channel->alias[LOCAL];
log_debug(
ld->log, "Selected channel %s (%s) for selector %s (%s)",
short_channel_id_to_str(tmpctx, scid),
type_to_string(tmpctx, struct amount_msat, &channel->our_msat),
short_channel_id_to_str(tmpctx, scid_or_alias),
type_to_string(tmpctx, struct amount_msat, amount));

return channel;
}

/* destination/route_channels/route_nodes are NULL (and path_secrets may be NULL)
Expand Down Expand Up @@ -1048,7 +1069,7 @@ send_payment_core(struct lightningd *ld,
return invreq_err;

channel = find_channel_for_htlc_add(ld, &first_hop->node_id,
&first_hop->scid);
&first_hop->scid, &msat);
if (!channel) {
struct json_stream *data
= json_stream_fail(cmd, PAY_TRY_OTHER_ROUTE,
Expand Down
30 changes: 22 additions & 8 deletions plugins/libplugin-pay.c
Original file line number Diff line number Diff line change
Expand Up @@ -2360,7 +2360,6 @@ local_channel_hints_listpeerchannels(struct command *cmd, const char *buffer,
chans = json_to_listpeers_channels(tmpctx, buffer, toks);

for (size_t i = 0; i < tal_count(chans); i++) {
struct short_channel_id scid;
bool enabled;
u16 htlc_budget;

Expand All @@ -2369,11 +2368,6 @@ local_channel_hints_listpeerchannels(struct command *cmd, const char *buffer,
* state. */
enabled = chans[i]->connected && streq(chans[i]->state, "CHANNELD_NORMAL");

if (chans[i]->scid != NULL)
scid = *chans[i]->scid;
else
scid = *chans[i]->alias[LOCAL];

/* Take the configured number of max_htlcs and
* subtract any HTLCs that might already be added to
* the channel. This is a best effort estimate and
Expand All @@ -2384,8 +2378,28 @@ local_channel_hints_listpeerchannels(struct command *cmd, const char *buffer,
else
htlc_budget = chans[i]->max_accepted_htlcs - chans[i]->num_htlcs;

channel_hints_update(p, scid, chans[i]->direction, enabled, true,
&chans[i]->spendable_msat, &htlc_budget);
/* If we have both a scid and a local alias we want to
* use the scid, and mark the alias as
* unusable. Otherwise `getroute` might return the
* alias, which we resolve correctly, but our
* channel_hints would be off after updates, since
* we'd only ever update one of the aliases. Causing
* the other to be considered usable.
*/
if (chans[i]->scid != NULL) {
channel_hints_update(
p, *chans[i]->scid, chans[i]->direction, enabled,
true, &chans[i]->spendable_msat, &htlc_budget);
channel_hints_update(p, *chans[i]->alias[LOCAL],
chans[i]->direction,
false /* not enabled */, true,
&AMOUNT_MSAT(0), &htlc_budget);
} else {
channel_hints_update(p, *chans[i]->alias[LOCAL],
chans[i]->direction, enabled, true,
&chans[i]->spendable_msat,
&htlc_budget);
}
}

payment_continue(p);
Expand Down