@@ -254,6 +254,60 @@ struct channel_type *desired_channel_type(const tal_t *ctx,
254254 return channel_type_static_remotekey (ctx );
255255}
256256
257+ static void chanmap_remove (struct lightningd * ld ,
258+ const struct channel * channel ,
259+ struct short_channel_id scid )
260+ {
261+ struct scid_to_channel * scc = channel_scid_map_get (ld -> channels_by_scid , scid );
262+ assert (scc -> channel == channel );
263+ tal_free (scc );
264+ }
265+
266+ static void destroy_scid_to_channel (struct scid_to_channel * scc ,
267+ struct lightningd * ld )
268+ {
269+ if (!channel_scid_map_del (ld -> channels_by_scid , scc ))
270+ abort ();
271+ }
272+
273+ static void chanmap_add (struct lightningd * ld ,
274+ struct channel * channel ,
275+ struct short_channel_id scid )
276+ {
277+ struct scid_to_channel * scc = tal (channel , struct scid_to_channel );
278+ scc -> channel = channel ;
279+ scc -> scid = scid ;
280+ channel_scid_map_add (ld -> channels_by_scid , scc );
281+ tal_add_destructor2 (scc , destroy_scid_to_channel , ld );
282+ }
283+
284+ static void channel_set_random_local_alias (struct channel * channel )
285+ {
286+ assert (channel -> alias [LOCAL ] == NULL );
287+ channel -> alias [LOCAL ] = tal (channel , struct short_channel_id );
288+ randombytes_buf (channel -> alias [LOCAL ], sizeof (struct short_channel_id ));
289+ /* We don't check for uniqueness. We would crash on a clash, but your machine is
290+ * probably broken beyond repair if it gets two equal 64 bit numbers */
291+ chanmap_add (channel -> peer -> ld , channel , * channel -> alias [LOCAL ]);
292+ }
293+
294+ void channel_set_scid (struct channel * channel , const struct short_channel_id * new_scid )
295+ {
296+ struct lightningd * ld = channel -> peer -> ld ;
297+
298+ /* Get rid of old one (if any) */
299+ if (channel -> scid != NULL ) {
300+ chanmap_remove (ld , channel , * channel -> scid );
301+ channel -> scid = tal_free (channel -> scid );
302+ }
303+
304+ /* Add new one (if any) */
305+ if (new_scid ) {
306+ channel -> scid = tal_dup (channel , struct short_channel_id , new_scid );
307+ chanmap_add (ld , channel , * new_scid );
308+ }
309+ }
310+
257311void channel_add_old_scid (struct channel * channel ,
258312 struct short_channel_id old_scid )
259313{
@@ -265,6 +319,8 @@ void channel_add_old_scid(struct channel *channel,
265319 channel -> old_scids = tal_dup (channel , struct short_channel_id , & old_scid );
266320 else
267321 tal_arr_expand (& channel -> old_scids , old_scid );
322+
323+ chanmap_add (channel -> peer -> ld , channel , old_scid );
268324}
269325
270326struct channel * new_unsaved_channel (struct peer * peer ,
@@ -312,10 +368,8 @@ struct channel *new_unsaved_channel(struct peer *peer,
312368 = CLOSING_FEE_NEGOTIATION_STEP_UNIT_PERCENTAGE ;
313369 channel -> shutdown_wrong_funding = NULL ;
314370 channel -> closing_feerate_range = NULL ;
315- channel -> alias [REMOTE ] = NULL ;
316- /* We don't even bother checking for clashes. */
317- channel -> alias [LOCAL ] = tal (channel , struct short_channel_id );
318- randombytes_buf (channel -> alias [LOCAL ], sizeof (struct short_channel_id ));
371+ channel -> alias [REMOTE ] = channel -> alias [LOCAL ] = NULL ;
372+ channel_set_random_local_alias (channel );
319373
320374 channel -> shutdown_scriptpubkey [REMOTE ] = NULL ;
321375 channel -> last_was_revoke = false;
@@ -567,11 +621,19 @@ struct channel *new_channel(struct peer *peer, u64 dbid,
567621 channel -> scid = tal_steal (channel , scid );
568622 channel -> old_scids = tal_dup_talarr (channel , struct short_channel_id , old_scids );
569623 channel -> alias [LOCAL ] = tal_dup_or_null (channel , struct short_channel_id , alias_local );
624+ /* All these possible short_channel_id variants go in the lookup table! */
625+ /* Stub channels all have the same scid though, *and* get loaded from db! */
626+ if (channel -> scid && !is_stub_scid (* channel -> scid ))
627+ chanmap_add (peer -> ld , channel , * channel -> scid );
628+ if (channel -> alias [LOCAL ])
629+ chanmap_add (peer -> ld , channel , * channel -> alias [LOCAL ]);
630+ for (size_t i = 0 ; i < tal_count (channel -> old_scids ); i ++ )
631+ chanmap_add (peer -> ld , channel , channel -> old_scids [i ]);
632+
570633 /* We always make sure this is set (historical channels from db might not) */
571- if (!channel -> alias [LOCAL ]) {
572- channel -> alias [LOCAL ] = tal (channel , struct short_channel_id );
573- randombytes_buf (channel -> alias [LOCAL ], sizeof (struct short_channel_id ));
574- }
634+ if (!channel -> alias [LOCAL ])
635+ channel_set_random_local_alias (channel );
636+
575637 channel -> alias [REMOTE ] = tal_steal (channel , alias_remote ); /* Haven't gotten one yet. */
576638 channel -> cid = * cid ;
577639 channel -> our_msat = our_msat ;
0 commit comments