lightningd: add find_channel_by_scid

More efficient to search a known peer than the whole set.

Also, move find_channel_by_id() from channel_control.c into channel.c
where we'd expect it.

Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
This commit is contained in:
Rusty Russell 2022-03-23 09:29:20 +10:30
parent cb5dc48cab
commit ba1242af3e
4 changed files with 27 additions and 16 deletions

View File

@ -678,6 +678,29 @@ struct channel *channel_by_cid(struct lightningd *ld,
return NULL;
}
struct channel *find_channel_by_id(const struct peer *peer,
const struct channel_id *cid)
{
struct channel *c;
list_for_each(&peer->channels, c, list) {
if (channel_id_eq(&c->cid, cid))
return c;
}
return NULL;
}
struct channel *find_channel_by_scid(const struct peer *peer,
const struct short_channel_id *scid)
{
struct channel *c;
list_for_each(&peer->channels, c, list) {
if (c->scid && short_channel_id_eq(c->scid, scid))
return c;
}
return NULL;
}
void channel_set_last_tx(struct channel *channel,
struct bitcoin_tx *tx,

View File

@ -418,6 +418,10 @@ struct channel *channel_by_cid(struct lightningd *ld,
struct channel *find_channel_by_id(const struct peer *peer,
const struct channel_id *cid);
/* Find this channel within peer */
struct channel *find_channel_by_scid(const struct peer *peer,
const struct short_channel_id *scid);
void channel_set_last_tx(struct channel *channel,
struct bitcoin_tx *tx,
const struct bitcoin_signature *sig,

View File

@ -907,18 +907,6 @@ void channel_notify_new_block(struct lightningd *ld,
tal_free(to_forget);
}
struct channel *find_channel_by_id(const struct peer *peer,
const struct channel_id *cid)
{
struct channel *c;
list_for_each(&peer->channels, c, list) {
if (channel_id_eq(&c->cid, cid))
return c;
}
return NULL;
}
/* Since this could vanish while we're checking with bitcoind, we need to save
* the details and re-lookup.
*

View File

@ -133,10 +133,6 @@ char *encode_scriptpubkey_to_addr(const tal_t *ctx UNNEEDED,
/* Generated stub for fatal */
void fatal(const char *fmt UNNEEDED, ...)
{ fprintf(stderr, "fatal called!\n"); abort(); }
/* Generated stub for find_channel_by_id */
struct channel *find_channel_by_id(const struct peer *peer UNNEEDED,
const struct channel_id *cid UNNEEDED)
{ fprintf(stderr, "find_channel_by_id called!\n"); abort(); }
/* Generated stub for fromwire_channeld_dev_memleak_reply */
bool fromwire_channeld_dev_memleak_reply(const void *p UNNEEDED, bool *leak UNNEEDED)
{ fprintf(stderr, "fromwire_channeld_dev_memleak_reply called!\n"); abort(); }