Skip to content

Commit 0d4eeed

Browse files
committed
lightningd: don't populate local alias if NULL, have the caller do it.
We were populating local aliases for stub channels, which means putting them in the htable and thus risking that we could try to route through them. Instead, NULL means "caller will populate", and for the other cases make the caller specify the real scid. We opencode channel_set_random_local_alias, since there's only one caller now. Signed-off-by: Rusty Russell <[email protected]>
1 parent 52c6ee2 commit 0d4eeed

File tree

2 files changed

+15
-18
lines changed

2 files changed

+15
-18
lines changed

lightningd/channel.c

Lines changed: 7 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -268,16 +268,6 @@ static void chanmap_add(struct lightningd *ld,
268268
tal_add_destructor2(scc, destroy_scid_to_channel, ld);
269269
}
270270

271-
static void channel_set_random_local_alias(struct channel *channel)
272-
{
273-
assert(channel->alias[LOCAL] == NULL);
274-
channel->alias[LOCAL] = tal(channel, struct short_channel_id);
275-
*channel->alias[LOCAL] = random_scid();
276-
/* We don't check for uniqueness. We would crash on a clash, but your machine is
277-
* probably broken beyond repair if it gets two equal 64 bit numbers */
278-
chanmap_add(channel->peer->ld, channel, *channel->alias[LOCAL]);
279-
}
280-
281271
void channel_set_scid(struct channel *channel, const struct short_channel_id *new_scid)
282272
{
283273
struct lightningd *ld = channel->peer->ld;
@@ -355,8 +345,12 @@ struct channel *new_unsaved_channel(struct peer *peer,
355345
= CLOSING_FEE_NEGOTIATION_STEP_UNIT_PERCENTAGE;
356346
channel->shutdown_wrong_funding = NULL;
357347
channel->closing_feerate_range = NULL;
358-
channel->alias[REMOTE] = channel->alias[LOCAL] = NULL;
359-
channel_set_random_local_alias(channel);
348+
channel->alias[REMOTE] = NULL;
349+
channel->alias[LOCAL] = tal(channel, struct short_channel_id);
350+
*channel->alias[LOCAL] = random_scid();
351+
/* We don't check for uniqueness. We would crash on a clash, but your machine is
352+
* probably broken beyond repair if it gets two equal 64 bit numbers */
353+
chanmap_add(channel->peer->ld, channel, *channel->alias[LOCAL]);
360354

361355
channel->shutdown_scriptpubkey[REMOTE] = NULL;
362356
channel->last_was_revoke = false;
@@ -611,15 +605,12 @@ struct channel *new_channel(struct peer *peer, u64 dbid,
611605
/* All these possible short_channel_id variants go in the lookup table! */
612606
if (channel->scid)
613607
chanmap_add(peer->ld, channel, *channel->scid);
608+
/* This is always set, *EXCEPT* for stub channels! */
614609
if (channel->alias[LOCAL])
615610
chanmap_add(peer->ld, channel, *channel->alias[LOCAL]);
616611
for (size_t i = 0; i < tal_count(channel->old_scids); i++)
617612
chanmap_add(peer->ld, channel, channel->old_scids[i]);
618613

619-
/* We always make sure this is set (historical channels from db might not) */
620-
if (!channel->alias[LOCAL])
621-
channel_set_random_local_alias(channel);
622-
623614
channel->alias[REMOTE] = tal_steal(channel, alias_remote); /* Haven't gotten one yet. */
624615
channel->cid = *cid;
625616
channel->our_msat = our_msat;

lightningd/opening_control.c

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -110,6 +110,7 @@ wallet_commit_channel(struct lightningd *ld,
110110
struct timeabs timestamp;
111111
struct channel_stats zero_channel_stats;
112112
enum addrtype addrtype;
113+
struct short_channel_id local_alias;
113114

114115
/* We can't have any payments yet */
115116
memset(&zero_channel_stats, 0, sizeof(zero_channel_stats));
@@ -172,6 +173,8 @@ wallet_commit_channel(struct lightningd *ld,
172173
else
173174
static_remotekey_start = 0x7FFFFFFFFFFFFFFF;
174175

176+
local_alias = random_scid();
177+
175178
channel = new_channel(uc->peer, uc->dbid,
176179
NULL, /* No shachain yet */
177180
CHANNELD_AWAITING_LOCKIN,
@@ -190,7 +193,7 @@ wallet_commit_channel(struct lightningd *ld,
190193
false, /* !remote_channel_ready */
191194
NULL, /* no scid yet */
192195
NULL, /* no old scids */
193-
NULL, /* assign random local alias */
196+
&local_alias, /* random local alias */
194197
NULL, /* They haven't told us an alias yet */
195198
cid,
196199
/* The three arguments below are msatoshi_to_us,
@@ -1621,11 +1624,14 @@ static struct channel *stub_chan(struct command *cmd,
16211624
&zero_channel_stats,
16221625
tal_arr(NULL, struct channel_state_change *, 0));
16231626

1624-
/* We fill in scid manually here, so it doesn't go in the hash table! */
1627+
/* We fill in scid & local alias manually here, so it doesn't go in the hash table! */
16251628
channel->scid = tal(cmd, struct short_channel_id);
16261629
/*To indicate this is an stub channel we keep it's scid to 1x1x1.*/
16271630
if (!mk_short_channel_id(channel->scid, 1, 1, 1))
16281631
fatal("Failed to make short channel 1x1x1!");
1632+
/* Every channel is assumed to have a non-NULL local alias */
1633+
channel->alias[LOCAL] = tal(channel, struct short_channel_id);
1634+
*channel->alias[LOCAL] = *channel->scid;
16291635

16301636
/* We don't want to gossip about this, ever. */
16311637
channel->channel_gossip = tal_free(channel->channel_gossip);

0 commit comments

Comments
 (0)