lightningd: require local_alias in new_channel().

We allowed NULL for stub channels, but just don't put the stub scid
into the hash tables.  This cleans up all the callers to make it
clear this is a non-optional parameter.

We opencode channel_set_random_local_alias, since there's only one caller now.

Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
This commit is contained in:
Rusty Russell
2025-08-14 12:40:11 +09:30
parent bb4dda6f5a
commit 5e263bac78
7 changed files with 35 additions and 40 deletions

View File

@@ -280,16 +280,6 @@ static void chanmap_add(struct lightningd *ld,
tal_add_destructor2(scc, destroy_scid_to_channel, ld);
}
static void channel_set_random_local_alias(struct channel *channel)
{
assert(channel->alias[LOCAL] == NULL);
channel->alias[LOCAL] = tal(channel, struct short_channel_id);
*channel->alias[LOCAL] = random_scid();
/* We don't check for uniqueness. We would crash on a clash, but your machine is
* probably broken beyond repair if it gets two equal 64 bit numbers */
chanmap_add(channel->peer->ld, channel, *channel->alias[LOCAL]);
}
void channel_set_scid(struct channel *channel, const struct short_channel_id *new_scid)
{
struct lightningd *ld = channel->peer->ld;
@@ -367,8 +357,12 @@ struct channel *new_unsaved_channel(struct peer *peer,
= CLOSING_FEE_NEGOTIATION_STEP_UNIT_PERCENTAGE;
channel->shutdown_wrong_funding = NULL;
channel->closing_feerate_range = NULL;
channel->alias[REMOTE] = channel->alias[LOCAL] = NULL;
channel_set_random_local_alias(channel);
channel->alias[REMOTE] = NULL;
channel->alias[LOCAL] = tal(channel, struct short_channel_id);
*channel->alias[LOCAL] = random_scid();
/* We don't check for uniqueness. We would crash on a clash, but your machine is
* probably broken beyond repair if it gets two equal 64 bit numbers */
chanmap_add(channel->peer->ld, channel, *channel->alias[LOCAL]);
channel->shutdown_scriptpubkey[REMOTE] = NULL;
channel->last_was_revoke = false;
@@ -489,9 +483,9 @@ struct channel *new_channel(struct peer *peer, u64 dbid,
struct amount_sat our_funds,
bool remote_channel_ready,
/* NULL or stolen */
struct short_channel_id *scid,
struct short_channel_id *scid TAKES,
struct short_channel_id *old_scids TAKES,
struct short_channel_id *alias_local TAKES,
struct short_channel_id alias_local,
struct short_channel_id *alias_remote STEALS,
struct channel_id *cid,
struct amount_msat our_msat,
@@ -617,22 +611,18 @@ struct channel *new_channel(struct peer *peer, u64 dbid,
channel->push = push;
channel->our_funds = our_funds;
channel->remote_channel_ready = remote_channel_ready;
channel->scid = tal_steal(channel, scid);
channel->scid = tal_dup_or_null(channel, struct short_channel_id, scid);
channel->old_scids = tal_dup_talarr(channel, struct short_channel_id, old_scids);
channel->alias[LOCAL] = tal_dup_or_null(channel, struct short_channel_id, alias_local);
channel->alias[LOCAL] = tal_dup(channel, struct short_channel_id, &alias_local);
/* All these possible short_channel_id variants go in the lookup table! */
/* Stub channels all have the same scid though, *and* get loaded from db! */
if (channel->scid && !is_stub_scid(*channel->scid))
chanmap_add(peer->ld, channel, *channel->scid);
if (channel->alias[LOCAL])
if (!is_stub_scid(*channel->alias[LOCAL]))
chanmap_add(peer->ld, channel, *channel->alias[LOCAL]);
for (size_t i = 0; i < tal_count(channel->old_scids); i++)
chanmap_add(peer->ld, channel, channel->old_scids[i]);
/* We always make sure this is set (historical channels from db might not) */
if (!channel->alias[LOCAL])
channel_set_random_local_alias(channel);
channel->alias[REMOTE] = tal_steal(channel, alias_remote); /* Haven't gotten one yet. */
channel->cid = *cid;
channel->our_msat = our_msat;
@@ -739,7 +729,7 @@ struct channel *new_channel(struct peer *peer, u64 dbid,
}
/* scid is NULL when opening a new channel so we don't
* need to set error in that case as well */
if (scid && is_stub_scid(*scid))
if (channel->scid && is_stub_scid(*channel->scid))
channel->error = towire_errorfmt(peer->ld,
&channel->cid,
"We can't be together anymore.");