invoice: Consider aliases too when selecting routehints

This commit is contained in:
Christian Decker
2022-04-28 17:55:15 +02:00
parent 78c9c6a9e0
commit 1ae3dba529
7 changed files with 65 additions and 5 deletions

View File

@@ -664,6 +664,18 @@ struct channel *find_channel_by_scid(const struct peer *peer,
return NULL; return NULL;
} }
struct channel *find_channel_by_alias(const struct peer *peer,
const struct short_channel_id *alias,
enum side side)
{
struct channel *c;
list_for_each(&peer->channels, c, list) {
if (c->alias[side] && short_channel_id_eq(c->alias[side], alias))
return c;
}
return NULL;
}
void channel_set_last_tx(struct channel *channel, void channel_set_last_tx(struct channel *channel,
struct bitcoin_tx *tx, struct bitcoin_tx *tx,
const struct bitcoin_signature *sig, const struct bitcoin_signature *sig,

View File

@@ -417,6 +417,11 @@ struct channel *find_channel_by_id(const struct peer *peer,
struct channel *find_channel_by_scid(const struct peer *peer, struct channel *find_channel_by_scid(const struct peer *peer,
const struct short_channel_id *scid); const struct short_channel_id *scid);
/* Find a channel by its alias, either local or remote. */
struct channel *find_channel_by_alias(const struct peer *peer,
const struct short_channel_id *alias,
enum side side);
void channel_set_last_tx(struct channel *channel, void channel_set_last_tx(struct channel *channel,
struct bitcoin_tx *tx, struct bitcoin_tx *tx,
const struct bitcoin_signature *sig, const struct bitcoin_signature *sig,

View File

@@ -106,6 +106,15 @@ routehint_candidates(const tal_t *ctx,
/* Check channel is in CHANNELD_NORMAL */ /* Check channel is in CHANNELD_NORMAL */
candidate.c = find_channel_by_scid(peer, &r->short_channel_id); candidate.c = find_channel_by_scid(peer, &r->short_channel_id);
/* Try seeing if we should be using a remote alias
* instead. The `listpeers` result may have returned
* the REMOTE alias, because it is the only scid we
* have, and it is mandatory once the channel is in
* CHANNELD_NORMAL. */
if (!candidate.c)
candidate.c = find_channel_by_alias(peer, &r->short_channel_id, REMOTE);
if (!candidate.c) { if (!candidate.c) {
log_debug(ld->log, "%s: channel not found in peer %s", log_debug(ld->log, "%s: channel not found in peer %s",
type_to_string(tmpctx, type_to_string(tmpctx,

View File

@@ -187,6 +187,11 @@ bool feature_negotiated(const struct feature_set *our_features UNNEEDED,
/* Generated stub for featurebits_or */ /* Generated stub for featurebits_or */
u8 *featurebits_or(const tal_t *ctx UNNEEDED, const u8 *f1 TAKES UNNEEDED, const u8 *f2 TAKES UNNEEDED) u8 *featurebits_or(const tal_t *ctx UNNEEDED, const u8 *f1 TAKES UNNEEDED, const u8 *f2 TAKES UNNEEDED)
{ fprintf(stderr, "featurebits_or called!\n"); abort(); } { fprintf(stderr, "featurebits_or called!\n"); abort(); }
/* Generated stub for find_channel_by_alias */
struct channel *find_channel_by_alias(const struct peer *peer UNNEEDED,
const struct short_channel_id *alias UNNEEDED,
enum side side UNNEEDED)
{ fprintf(stderr, "find_channel_by_alias called!\n"); abort(); }
/* Generated stub for find_channel_by_id */ /* Generated stub for find_channel_by_id */
struct channel *find_channel_by_id(const struct peer *peer UNNEEDED, struct channel *find_channel_by_id(const struct peer *peer UNNEEDED,
const struct channel_id *cid UNNEEDED) const struct channel_id *cid UNNEEDED)

View File

@@ -3228,9 +3228,17 @@ static struct command_result *direct_pay_listpeers(struct command *cmd,
if (!streq(chan->state, "CHANNELD_NORMAL")) if (!streq(chan->state, "CHANNELD_NORMAL"))
continue; continue;
/* Must have either a local alias for zeroconf
* channels or a final scid. */
assert(chan->alias[LOCAL] || chan->scid);
d->chan = tal(d, struct short_channel_id_dir); d->chan = tal(d, struct short_channel_id_dir);
d->chan->scid = *chan->scid; if (chan->scid) {
d->chan->dir = *chan->direction; d->chan->scid = *chan->scid;
d->chan->dir = *chan->direction;
} else {
d->chan->scid = *chan->alias[LOCAL];
d->chan->dir = 0; /* Don't care. */
}
} }
} }
cont: cont:

View File

@@ -1581,10 +1581,10 @@ static struct listpeers_channel *json_to_listpeers_channel(const tal_t *ctx,
*scidtok = *scidtok =
json_get_member(buffer, tok, "short_channel_id"), json_get_member(buffer, tok, "short_channel_id"),
*dirtok = json_get_member(buffer, tok, "direction"), *dirtok = json_get_member(buffer, tok, "direction"),
*tmsattok = *tmsattok = json_get_member(buffer, tok, "total_msat"),
json_get_member(buffer, tok, "total_msat"),
*smsattok = *smsattok =
json_get_member(buffer, tok, "spendable_msat"); json_get_member(buffer, tok, "spendable_msat"),
*aliastok = json_get_member(buffer, tok, "alias");
if (privtok == NULL || privtok->type != JSMN_PRIMITIVE || if (privtok == NULL || privtok->type != JSMN_PRIMITIVE ||
statetok == NULL || statetok->type != JSMN_STRING || statetok == NULL || statetok->type != JSMN_STRING ||
@@ -1611,6 +1611,25 @@ static struct listpeers_channel *json_to_listpeers_channel(const tal_t *ctx,
chan->scid = NULL; chan->scid = NULL;
chan->direction = NULL; chan->direction = NULL;
} }
if (aliastok != NULL) {
const jsmntok_t *loctok =
json_get_member(buffer, aliastok, "local"),
*remtok =
json_get_member(buffer, aliastok, "remote");
if (loctok) {
chan->alias[LOCAL] = tal(chan, struct short_channel_id);
json_to_short_channel_id(buffer, loctok,
chan->alias[LOCAL]);
} else
chan->alias[LOCAL] = NULL;
if (remtok) {
chan->alias[REMOTE] = tal(chan, struct short_channel_id);
json_to_short_channel_id(buffer, loctok,
chan->alias[REMOTE]);
} else
chan->alias[REMOTE] = NULL;
}
json_to_msat(buffer, tmsattok, &chan->total_msat); json_to_msat(buffer, tmsattok, &chan->total_msat);
json_to_msat(buffer, smsattok, &chan->spendable_msat); json_to_msat(buffer, smsattok, &chan->spendable_msat);

View File

@@ -10,6 +10,7 @@
#include <ccan/timer/timer.h> #include <ccan/timer/timer.h>
#include <common/errcode.h> #include <common/errcode.h>
#include <common/features.h> #include <common/features.h>
#include <common/htlc.h>
#include <common/json.h> #include <common/json.h>
#include <common/json_command.h> #include <common/json_command.h>
#include <common/json_helpers.h> #include <common/json_helpers.h>
@@ -318,6 +319,7 @@ struct listpeers_channel {
bool private; bool private;
struct bitcoin_txid funding_txid; struct bitcoin_txid funding_txid;
const char *state; const char *state;
struct short_channel_id *alias[NUM_SIDES];
struct short_channel_id *scid; struct short_channel_id *scid;
int *direction; int *direction;
struct amount_msat total_msat; struct amount_msat total_msat;