Skip to content

Commit 58ef5e2

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 5656bec commit 58ef5e2

File tree

7 files changed

+34
-39
lines changed

7 files changed

+34
-39
lines changed

lightningd/channel.c

Lines changed: 11 additions & 21 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;
@@ -477,9 +471,9 @@ struct channel *new_channel(struct peer *peer, u64 dbid,
477471
struct amount_sat our_funds,
478472
bool remote_channel_ready,
479473
/* NULL or stolen */
480-
struct short_channel_id *scid,
474+
struct short_channel_id *scid TAKES,
481475
struct short_channel_id *old_scids TAKES,
482-
struct short_channel_id *alias_local TAKES,
476+
struct short_channel_id alias_local,
483477
struct short_channel_id *alias_remote STEALS,
484478
struct channel_id *cid,
485479
struct amount_msat our_msat,
@@ -605,22 +599,18 @@ struct channel *new_channel(struct peer *peer, u64 dbid,
605599
channel->push = push;
606600
channel->our_funds = our_funds;
607601
channel->remote_channel_ready = remote_channel_ready;
608-
channel->scid = tal_steal(channel, scid);
602+
channel->scid = tal_dup_or_null(channel, struct short_channel_id, scid);
609603
channel->old_scids = tal_dup_talarr(channel, struct short_channel_id, old_scids);
610-
channel->alias[LOCAL] = tal_dup_or_null(channel, struct short_channel_id, alias_local);
604+
channel->alias[LOCAL] = tal_dup(channel, struct short_channel_id, &alias_local);
611605
/* All these possible short_channel_id variants go in the lookup table! */
612606
/* Stub channels all have the same scid though, *and* get loaded from db! */
613607
if (channel->scid && !is_stub_scid(*channel->scid))
614608
chanmap_add(peer->ld, channel, *channel->scid);
615-
if (channel->alias[LOCAL])
609+
if (!is_stub_scid(*channel->alias[LOCAL]))
616610
chanmap_add(peer->ld, channel, *channel->alias[LOCAL]);
617611
for (size_t i = 0; i < tal_count(channel->old_scids); i++)
618612
chanmap_add(peer->ld, channel, channel->old_scids[i]);
619613

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

lightningd/channel.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -390,9 +390,9 @@ struct channel *new_channel(struct peer *peer, u64 dbid,
390390
struct amount_sat our_funds,
391391
bool remote_channel_ready,
392392
/* NULL or stolen */
393-
struct short_channel_id *scid STEALS,
393+
struct short_channel_id *scid TAKES,
394394
struct short_channel_id *old_scids TAKES,
395-
struct short_channel_id *alias_local STEALS,
395+
struct short_channel_id alias_local,
396396
struct short_channel_id *alias_remote STEALS,
397397
struct channel_id *cid,
398398
struct amount_msat our_msatoshi,

lightningd/opening_control.c

Lines changed: 8 additions & 6 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,
@@ -1477,7 +1480,7 @@ static struct channel *stub_chan(struct command *cmd,
14771480
struct peer *peer;
14781481
struct pubkey localFundingPubkey;
14791482
struct pubkey pk;
1480-
struct short_channel_id *scid;
1483+
struct short_channel_id scid;
14811484
u32 blockht;
14821485
u32 feerate;
14831486
struct channel_stats zero_channel_stats;
@@ -1555,10 +1558,9 @@ static struct channel *stub_chan(struct command *cmd,
15551558
channel_info->old_remote_per_commit = pk;
15561559

15571560
blockht = 100;
1558-
scid = tal(cmd, struct short_channel_id);
15591561

15601562
/*To indicate this is an stub channel we keep it's scid to 1x1x1.*/
1561-
if (!mk_short_channel_id(scid, 1, 1, 1))
1563+
if (!mk_short_channel_id(&scid, 1, 1, 1))
15621564
fatal("Failed to make short channel 1x1x1!");
15631565

15641566
memset(&zero_channel_stats, 0, sizeof(zero_channel_stats));
@@ -1579,9 +1581,9 @@ static struct channel *stub_chan(struct command *cmd,
15791581
AMOUNT_MSAT(0),
15801582
AMOUNT_SAT(0),
15811583
true, /* remote_channel_ready */
1582-
scid,
1583-
NULL,
1584+
&scid,
15841585
NULL,
1586+
scid,
15851587
NULL,
15861588
&cid,
15871589
/* 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
@@ -1774,7 +1774,7 @@ static struct channel *wallet_stmt2channel(struct wallet *w, struct db_stmt *stm
17741774
struct channel_info channel_info;
17751775
struct fee_states *fee_states;
17761776
struct height_states *height_states;
1777-
struct short_channel_id *scid, *alias[NUM_SIDES], *old_scids;
1777+
struct short_channel_id *scid, alias_local, *alias_remote, *old_scids;
17781778
struct channel_id cid;
17791779
struct channel *chan;
17801780
u64 peer_dbid;
@@ -1814,9 +1814,8 @@ static struct channel *wallet_stmt2channel(struct wallet *w, struct db_stmt *stm
18141814

18151815
scid = db_col_optional_scid(tmpctx, stmt, "scid");
18161816
old_scids = db_col_short_channel_id_arr(tmpctx, stmt, "old_scids");
1817-
alias[LOCAL] = tal(tmpctx, struct short_channel_id);
1818-
*alias[LOCAL] = db_col_short_channel_id(stmt, "alias_local");
1819-
alias[REMOTE] = db_col_optional_scid(tmpctx, stmt, "alias_remote");
1817+
alias_local = db_col_short_channel_id(stmt, "alias_local");
1818+
alias_remote = db_col_optional_scid(tmpctx, stmt, "alias_remote");
18201819

18211820
ok &= wallet_shachain_load(w, db_col_u64(stmt, "shachain_remote_id"),
18221821
&wshachain);
@@ -1971,7 +1970,7 @@ static struct channel *wallet_stmt2channel(struct wallet *w, struct db_stmt *stm
19711970
if (scid)
19721971
remote_update->scid = *scid;
19731972
else
1974-
remote_update->scid = *alias[LOCAL];
1973+
remote_update->scid = alias_local;
19751974
remote_update->fee_base = db_col_int(stmt, "remote_feerate_base");
19761975
remote_update->fee_ppm = db_col_int(stmt, "remote_feerate_ppm");
19771976
remote_update->cltv_delta = db_col_int(stmt, "remote_cltv_expiry_delta");
@@ -2028,10 +2027,10 @@ static struct channel *wallet_stmt2channel(struct wallet *w, struct db_stmt *stm
20282027
push_msat,
20292028
our_funding_sat,
20302029
db_col_int(stmt, "funding_locked_remote") != 0,
2031-
scid,
2030+
take(scid),
20322031
old_scids,
2033-
alias[LOCAL],
2034-
alias[REMOTE],
2032+
alias_local,
2033+
alias_remote,
20352034
&cid,
20362035
our_msat,
20372036
msat_to_us_min, /* msatoshi_to_us_min */

0 commit comments

Comments
 (0)