Skip to content

Commit e596d9f

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 8746c18 commit e596d9f

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

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

0 commit comments

Comments
 (0)