Skip to content

Commit d1f56eb

Browse files
committed
lightningd: use the hash table to lookup scids.
This replaces the old "iterate through each peer, then each peer's channel" suboptimality. A bit of care required that we don't expose scids if we're forwarding, but that was already carefully handled. Signed-off-by: Rusty Russell <[email protected]>
1 parent 5e263ba commit d1f56eb

File tree

1 file changed

+22
-37
lines changed

1 file changed

+22
-37
lines changed

lightningd/channel.c

Lines changed: 22 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -816,43 +816,28 @@ struct channel *any_channel_by_scid(struct lightningd *ld,
816816
struct short_channel_id scid,
817817
bool privacy_leak_ok)
818818
{
819-
struct peer *p;
820-
struct channel *chan;
821-
struct peer_node_id_map_iter it;
822-
823-
/* FIXME: Support lookup by scid directly! */
824-
for (p = peer_node_id_map_first(ld->peers, &it);
825-
p;
826-
p = peer_node_id_map_next(ld->peers, &it)) {
827-
list_for_each(&p->channels, chan, list) {
828-
/* BOLT #2:
829-
* - MUST always recognize the `alias` as a
830-
* `short_channel_id` for incoming HTLCs to this
831-
* channel.
832-
*/
833-
if (chan->alias[LOCAL] &&
834-
short_channel_id_eq(scid, *chan->alias[LOCAL]))
835-
return chan;
836-
/* BOLT #2:
837-
* - if `channel_type` has `option_scid_alias` set:
838-
* - MUST NOT allow incoming HTLCs to this channel
839-
* using the real `short_channel_id`
840-
*/
841-
if (!privacy_leak_ok
842-
&& channel_type_has(chan->type, OPT_SCID_ALIAS))
843-
continue;
844-
if (chan->scid
845-
&& short_channel_id_eq(scid, *chan->scid))
846-
return chan;
847-
848-
/* Look through any old pre-splice channel ids */
849-
for (size_t i = 0; i < tal_count(chan->old_scids); i++) {
850-
if (short_channel_id_eq(scid, chan->old_scids[i]))
851-
return chan;
852-
}
853-
}
854-
}
855-
return NULL;
819+
const struct scid_to_channel *scc = channel_scid_map_get(ld->channels_by_scid, scid);
820+
if (!scc)
821+
return NULL;
822+
823+
/* BOLT #2:
824+
* - MUST always recognize the `alias` as a `short_channel_id` for
825+
* incoming HTLCs to this channel.
826+
*/
827+
if (scc->channel->alias[LOCAL]
828+
&& short_channel_id_eq(scid, *scc->channel->alias[LOCAL]))
829+
return scc->channel;
830+
831+
/* BOLT #2:
832+
* - if `channel_type` has `option_scid_alias` set:
833+
* - MUST NOT allow incoming HTLCs to this channel using the real
834+
* `short_channel_id`
835+
*/
836+
/* This means any scids other than the alias (handled above) cannot be exposed */
837+
if (!privacy_leak_ok && channel_type_has(scc->channel->type, OPT_SCID_ALIAS))
838+
return NULL;
839+
840+
return scc->channel;
856841
}
857842

858843
struct channel *channel_by_dbid(struct lightningd *ld, const u64 dbid)

0 commit comments

Comments
 (0)