@@ -242,6 +242,60 @@ struct open_attempt *new_channel_open_attempt(struct channel *channel)
242242 return oa ;
243243}
244244
245+ static void chanmap_remove (struct lightningd * ld ,
246+ const struct channel * channel ,
247+ struct short_channel_id scid )
248+ {
249+ struct scid_to_channel * scc = channel_scid_map_get (ld -> channels_by_scid , scid );
250+ assert (scc -> channel == channel );
251+ tal_free (scc );
252+ }
253+
254+ static void destroy_scid_to_channel (struct scid_to_channel * scc ,
255+ struct lightningd * ld )
256+ {
257+ if (!channel_scid_map_del (ld -> channels_by_scid , scc ))
258+ abort ();
259+ }
260+
261+ static void chanmap_add (struct lightningd * ld ,
262+ struct channel * channel ,
263+ struct short_channel_id scid )
264+ {
265+ struct scid_to_channel * scc = tal (channel , struct scid_to_channel );
266+ scc -> channel = channel ;
267+ scc -> scid = scid ;
268+ channel_scid_map_add (ld -> channels_by_scid , scc );
269+ tal_add_destructor2 (scc , destroy_scid_to_channel , ld );
270+ }
271+
272+ static void channel_set_random_local_alias (struct channel * channel )
273+ {
274+ assert (channel -> alias [LOCAL ] == NULL );
275+ channel -> alias [LOCAL ] = tal (channel , struct short_channel_id );
276+ randombytes_buf (channel -> alias [LOCAL ], sizeof (struct short_channel_id ));
277+ /* We don't check for uniqueness. We would crash on a clash, but your machine is
278+ * probably broken beyond repair if it gets two equal 64 bit numbers */
279+ chanmap_add (channel -> peer -> ld , channel , * channel -> alias [LOCAL ]);
280+ }
281+
282+ void channel_set_scid (struct channel * channel , const struct short_channel_id * new_scid )
283+ {
284+ struct lightningd * ld = channel -> peer -> ld ;
285+
286+ /* Get rid of old one (if any) */
287+ if (channel -> scid != NULL ) {
288+ chanmap_remove (ld , channel , * channel -> scid );
289+ channel -> scid = tal_free (channel -> scid );
290+ }
291+
292+ /* Add new one (if any) */
293+ if (new_scid ) {
294+ channel -> scid = tal_dup (channel , struct short_channel_id , new_scid );
295+ chanmap_add (ld , channel , * new_scid );
296+ }
297+ }
298+
245299void channel_add_old_scid (struct channel * channel ,
246300 struct short_channel_id old_scid )
247301{
@@ -253,6 +307,8 @@ void channel_add_old_scid(struct channel *channel,
253307 channel -> old_scids = tal_dup (channel , struct short_channel_id , & old_scid );
254308 else
255309 tal_arr_expand (& channel -> old_scids , old_scid );
310+
311+ chanmap_add (channel -> peer -> ld , channel , old_scid );
256312}
257313
258314struct channel * new_unsaved_channel (struct peer * peer ,
@@ -300,10 +356,8 @@ struct channel *new_unsaved_channel(struct peer *peer,
300356 = CLOSING_FEE_NEGOTIATION_STEP_UNIT_PERCENTAGE ;
301357 channel -> shutdown_wrong_funding = NULL ;
302358 channel -> closing_feerate_range = NULL ;
303- channel -> alias [REMOTE ] = NULL ;
304- /* We don't even bother checking for clashes. */
305- channel -> alias [LOCAL ] = tal (channel , struct short_channel_id );
306- randombytes_buf (channel -> alias [LOCAL ], sizeof (struct short_channel_id ));
359+ channel -> alias [REMOTE ] = channel -> alias [LOCAL ] = NULL ;
360+ channel_set_random_local_alias (channel );
307361
308362 channel -> shutdown_scriptpubkey [REMOTE ] = NULL ;
309363 channel -> last_was_revoke = false;
@@ -555,11 +609,19 @@ struct channel *new_channel(struct peer *peer, u64 dbid,
555609 channel -> scid = tal_steal (channel , scid );
556610 channel -> old_scids = tal_dup_talarr (channel , struct short_channel_id , old_scids );
557611 channel -> alias [LOCAL ] = tal_dup_or_null (channel , struct short_channel_id , alias_local );
612+ /* All these possible short_channel_id variants go in the lookup table! */
613+ /* Stub channels all have the same scid though, *and* get loaded from db! */
614+ if (channel -> scid && !is_stub_scid (* channel -> scid ))
615+ chanmap_add (peer -> ld , channel , * channel -> scid );
616+ if (channel -> alias [LOCAL ])
617+ chanmap_add (peer -> ld , channel , * channel -> alias [LOCAL ]);
618+ for (size_t i = 0 ; i < tal_count (channel -> old_scids ); i ++ )
619+ chanmap_add (peer -> ld , channel , channel -> old_scids [i ]);
620+
558621 /* We always make sure this is set (historical channels from db might not) */
559- if (!channel -> alias [LOCAL ]) {
560- channel -> alias [LOCAL ] = tal (channel , struct short_channel_id );
561- randombytes_buf (channel -> alias [LOCAL ], sizeof (struct short_channel_id ));
562- }
622+ if (!channel -> alias [LOCAL ])
623+ channel_set_random_local_alias (channel );
624+
563625 channel -> alias [REMOTE ] = tal_steal (channel , alias_remote ); /* Haven't gotten one yet. */
564626 channel -> cid = * cid ;
565627 channel -> our_msat = our_msat ;
0 commit comments