Skip to content

Commit 5e263ba

Browse files
committed
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 <[email protected]>
1 parent bb4dda6 commit 5e263ba

File tree

7 files changed

+35
-40
lines changed

7 files changed

+35
-40
lines changed

lightningd/channel.c

Lines changed: 12 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -280,16 +280,6 @@ static void chanmap_add(struct lightningd *ld,
280280
tal_add_destructor2(scc, destroy_scid_to_channel, ld);
281281
}
282282

283-
static void channel_set_random_local_alias(struct channel *channel)
284-
{
285-
assert(channel->alias[LOCAL] == NULL);
286-
channel->alias[LOCAL] = tal(channel, struct short_channel_id);
287-
*channel->alias[LOCAL] = random_scid();
288-
/* We don't check for uniqueness. We would crash on a clash, but your machine is
289-
* probably broken beyond repair if it gets two equal 64 bit numbers */
290-
chanmap_add(channel->peer->ld, channel, *channel->alias[LOCAL]);
291-
}
292-
293283
void channel_set_scid(struct channel *channel, const struct short_channel_id *new_scid)
294284
{
295285
struct lightningd *ld = channel->peer->ld;
@@ -367,8 +357,12 @@ struct channel *new_unsaved_channel(struct peer *peer,
367357
= CLOSING_FEE_NEGOTIATION_STEP_UNIT_PERCENTAGE;
368358
channel->shutdown_wrong_funding = NULL;
369359
channel->closing_feerate_range = NULL;
370-
channel->alias[REMOTE] = channel->alias[LOCAL] = NULL;
371-
channel_set_random_local_alias(channel);
360+
channel->alias[REMOTE] = NULL;
361+
channel->alias[LOCAL] = tal(channel, struct short_channel_id);
362+
*channel->alias[LOCAL] = random_scid();
363+
/* We don't check for uniqueness. We would crash on a clash, but your machine is
364+
* probably broken beyond repair if it gets two equal 64 bit numbers */
365+
chanmap_add(channel->peer->ld, channel, *channel->alias[LOCAL]);
372366

373367
channel->shutdown_scriptpubkey[REMOTE] = NULL;
374368
channel->last_was_revoke = false;
@@ -489,9 +483,9 @@ struct channel *new_channel(struct peer *peer, u64 dbid,
489483
struct amount_sat our_funds,
490484
bool remote_channel_ready,
491485
/* NULL or stolen */
492-
struct short_channel_id *scid,
486+
struct short_channel_id *scid TAKES,
493487
struct short_channel_id *old_scids TAKES,
494-
struct short_channel_id *alias_local TAKES,
488+
struct short_channel_id alias_local,
495489
struct short_channel_id *alias_remote STEALS,
496490
struct channel_id *cid,
497491
struct amount_msat our_msat,
@@ -617,22 +611,18 @@ struct channel *new_channel(struct peer *peer, u64 dbid,
617611
channel->push = push;
618612
channel->our_funds = our_funds;
619613
channel->remote_channel_ready = remote_channel_ready;
620-
channel->scid = tal_steal(channel, scid);
614+
channel->scid = tal_dup_or_null(channel, struct short_channel_id, scid);
621615
channel->old_scids = tal_dup_talarr(channel, struct short_channel_id, old_scids);
622-
channel->alias[LOCAL] = tal_dup_or_null(channel, struct short_channel_id, alias_local);
616+
channel->alias[LOCAL] = tal_dup(channel, struct short_channel_id, &alias_local);
623617
/* All these possible short_channel_id variants go in the lookup table! */
624618
/* Stub channels all have the same scid though, *and* get loaded from db! */
625619
if (channel->scid && !is_stub_scid(*channel->scid))
626620
chanmap_add(peer->ld, channel, *channel->scid);
627-
if (channel->alias[LOCAL])
621+
if (!is_stub_scid(*channel->alias[LOCAL]))
628622
chanmap_add(peer->ld, channel, *channel->alias[LOCAL]);
629623
for (size_t i = 0; i < tal_count(channel->old_scids); i++)
630624
chanmap_add(peer->ld, channel, channel->old_scids[i]);
631625

632-
/* We always make sure this is set (historical channels from db might not) */
633-
if (!channel->alias[LOCAL])
634-
channel_set_random_local_alias(channel);
635-
636626
channel->alias[REMOTE] = tal_steal(channel, alias_remote); /* Haven't gotten one yet. */
637627
channel->cid = *cid;
638628
channel->our_msat = our_msat;
@@ -739,7 +729,7 @@ struct channel *new_channel(struct peer *peer, u64 dbid,
739729
}
740730
/* scid is NULL when opening a new channel so we don't
741731
* need to set error in that case as well */
742-
if (scid && is_stub_scid(*scid))
732+
if (channel->scid && is_stub_scid(*channel->scid))
743733
channel->error = towire_errorfmt(peer->ld,
744734
&channel->cid,
745735
"We can't be together anymore.");

lightningd/channel.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -393,9 +393,9 @@ struct channel *new_channel(struct peer *peer, u64 dbid,
393393
struct amount_sat our_funds,
394394
bool remote_channel_ready,
395395
/* NULL or stolen */
396-
struct short_channel_id *scid STEALS,
396+
struct short_channel_id *scid TAKES,
397397
struct short_channel_id *old_scids TAKES,
398-
struct short_channel_id *alias_local STEALS,
398+
struct short_channel_id alias_local,
399399
struct short_channel_id *alias_remote STEALS,
400400
struct channel_id *cid,
401401
struct amount_msat our_msatoshi,

lightningd/opening_control.c

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -109,6 +109,7 @@ wallet_commit_channel(struct lightningd *ld,
109109
struct timeabs timestamp;
110110
struct channel_stats zero_channel_stats;
111111
enum addrtype addrtype;
112+
struct short_channel_id local_alias;
112113

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

175+
local_alias = random_scid();
176+
174177
channel = new_channel(uc->peer, uc->dbid,
175178
NULL, /* No shachain yet */
176179
CHANNELD_AWAITING_LOCKIN,
@@ -189,7 +192,7 @@ wallet_commit_channel(struct lightningd *ld,
189192
false, /* !remote_channel_ready */
190193
NULL, /* no scid yet */
191194
NULL, /* no old scids */
192-
NULL, /* assign random local alias */
195+
local_alias, /* random local alias */
193196
NULL, /* They haven't told us an alias yet */
194197
cid,
195198
/* The three arguments below are msatoshi_to_us,
@@ -1485,7 +1488,7 @@ static struct channel *stub_chan(struct command *cmd,
14851488
struct peer *peer;
14861489
struct pubkey localFundingPubkey;
14871490
struct pubkey pk;
1488-
struct short_channel_id *scid;
1491+
struct short_channel_id scid;
14891492
u32 blockht;
14901493
u32 feerate;
14911494
struct channel_stats zero_channel_stats;
@@ -1563,10 +1566,9 @@ static struct channel *stub_chan(struct command *cmd,
15631566
channel_info->old_remote_per_commit = pk;
15641567

15651568
blockht = 100;
1566-
scid = tal(cmd, struct short_channel_id);
15671569

15681570
/*To indicate this is an stub channel we keep it's scid to 1x1x1.*/
1569-
if (!mk_short_channel_id(scid, 1, 1, 1))
1571+
if (!mk_short_channel_id(&scid, 1, 1, 1))
15701572
fatal("Failed to make short channel 1x1x1!");
15711573

15721574
memset(&zero_channel_stats, 0, sizeof(zero_channel_stats));
@@ -1587,9 +1589,9 @@ static struct channel *stub_chan(struct command *cmd,
15871589
AMOUNT_MSAT(0),
15881590
AMOUNT_SAT(0),
15891591
true, /* remote_channel_ready */
1590-
scid,
1591-
NULL,
1592+
&scid,
15921593
NULL,
1594+
scid,
15931595
NULL,
15941596
&cid,
15951597
/* The three arguments below are msatoshi_to_us,

lightningd/test/run-invoice-select-inchan.c

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -135,6 +135,9 @@ void channel_set_last_tx(struct channel *channel UNNEEDED,
135135
struct bitcoin_tx *tx UNNEEDED,
136136
const struct bitcoin_signature *sig UNNEEDED)
137137
{ fprintf(stderr, "channel_set_last_tx called!\n"); abort(); }
138+
/* Generated stub for channel_set_scid */
139+
void channel_set_scid(struct channel *channel UNNEEDED, const struct short_channel_id *new_scid UNNEEDED)
140+
{ fprintf(stderr, "channel_set_scid called!\n"); abort(); }
138141
/* Generated stub for channel_state_name */
139142
const char *channel_state_name(const struct channel *channel UNNEEDED)
140143
{ fprintf(stderr, "channel_state_name called!\n"); abort(); }

wallet/test/run-db.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -189,7 +189,7 @@ struct channel *new_channel(struct peer *peer UNNEEDED, u64 dbid UNNEEDED,
189189
/* NULL or stolen */
190190
struct short_channel_id *scid STEALS UNNEEDED,
191191
struct short_channel_id *old_scids TAKES UNNEEDED,
192-
struct short_channel_id *alias_local STEALS UNNEEDED,
192+
struct short_channel_id alias_local UNNEEDED,
193193
struct short_channel_id *alias_remote STEALS UNNEEDED,
194194
struct channel_id *cid UNNEEDED,
195195
struct amount_msat our_msatoshi UNNEEDED,

wallet/test/run-wallet.c

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1983,6 +1983,7 @@ static bool test_channel_inflight_crud(struct lightningd *ld, const tal_t *ctx)
19831983
struct wally_psbt *funding_psbt;
19841984
struct channel_info *channel_info = tal(w, struct channel_info);
19851985
struct basepoints basepoints;
1986+
struct short_channel_id alias_local = random_scid();
19861987
secp256k1_ecdsa_signature *lease_commit_sig;
19871988
u32 feerate, lease_blockheight_start;
19881989
u64 dbid;
@@ -2034,7 +2035,7 @@ static bool test_channel_inflight_crud(struct lightningd *ld, const tal_t *ctx)
20342035
our_sats,
20352036
0, NULL,
20362037
NULL, /* old scids */
2037-
NULL, /* alias[LOCAL] */
2038+
alias_local,
20382039
NULL, /* alias[REMOTE] */
20392040
&cid,
20402041
AMOUNT_MSAT(3333333000),

wallet/wallet.c

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1779,7 +1779,7 @@ static struct channel *wallet_stmt2channel(struct wallet *w, struct db_stmt *stm
17791779
struct channel_info channel_info;
17801780
struct fee_states *fee_states;
17811781
struct height_states *height_states;
1782-
struct short_channel_id *scid, *alias[NUM_SIDES], *old_scids;
1782+
struct short_channel_id *scid, alias_local, *alias_remote, *old_scids;
17831783
struct channel_id cid;
17841784
struct channel *chan;
17851785
u64 peer_dbid;
@@ -1819,9 +1819,8 @@ static struct channel *wallet_stmt2channel(struct wallet *w, struct db_stmt *stm
18191819

18201820
scid = db_col_optional_scid(tmpctx, stmt, "scid");
18211821
old_scids = db_col_short_channel_id_arr(tmpctx, stmt, "old_scids");
1822-
alias[LOCAL] = tal(tmpctx, struct short_channel_id);
1823-
*alias[LOCAL] = db_col_short_channel_id(stmt, "alias_local");
1824-
alias[REMOTE] = db_col_optional_scid(tmpctx, stmt, "alias_remote");
1822+
alias_local = db_col_short_channel_id(stmt, "alias_local");
1823+
alias_remote = db_col_optional_scid(tmpctx, stmt, "alias_remote");
18251824

18261825
ok &= wallet_shachain_load(w, db_col_u64(stmt, "shachain_remote_id"),
18271826
&wshachain);
@@ -1976,7 +1975,7 @@ static struct channel *wallet_stmt2channel(struct wallet *w, struct db_stmt *stm
19761975
if (scid)
19771976
remote_update->scid = *scid;
19781977
else
1979-
remote_update->scid = *alias[LOCAL];
1978+
remote_update->scid = alias_local;
19801979
remote_update->fee_base = db_col_int(stmt, "remote_feerate_base");
19811980
remote_update->fee_ppm = db_col_int(stmt, "remote_feerate_ppm");
19821981
remote_update->cltv_delta = db_col_int(stmt, "remote_cltv_expiry_delta");
@@ -2033,10 +2032,10 @@ static struct channel *wallet_stmt2channel(struct wallet *w, struct db_stmt *stm
20332032
push_msat,
20342033
our_funding_sat,
20352034
db_col_int(stmt, "funding_locked_remote") != 0,
2036-
scid,
2035+
take(scid),
20372036
old_scids,
2038-
alias[LOCAL],
2039-
alias[REMOTE],
2037+
alias_local,
2038+
alias_remote,
20402039
&cid,
20412040
our_msat,
20422041
msat_to_us_min, /* msatoshi_to_us_min */

0 commit comments

Comments
 (0)