Skip to content

Commit fe6a48e

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 3e9d796 commit fe6a48e

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
@@ -278,16 +278,6 @@ static void chanmap_add(struct lightningd *ld,
278278
tal_add_destructor2(scc, destroy_scid_to_channel, ld);
279279
}
280280

281-
static void channel_set_random_local_alias(struct channel *channel)
282-
{
283-
assert(channel->alias[LOCAL] == NULL);
284-
channel->alias[LOCAL] = tal(channel, struct short_channel_id);
285-
*channel->alias[LOCAL] = random_scid();
286-
/* We don't check for uniqueness. We would crash on a clash, but your machine is
287-
* probably broken beyond repair if it gets two equal 64 bit numbers */
288-
chanmap_add(channel->peer->ld, channel, *channel->alias[LOCAL]);
289-
}
290-
291281
void channel_set_scid(struct channel *channel, const struct short_channel_id *new_scid)
292282
{
293283
struct lightningd *ld = channel->peer->ld;
@@ -365,8 +355,12 @@ struct channel *new_unsaved_channel(struct peer *peer,
365355
= CLOSING_FEE_NEGOTIATION_STEP_UNIT_PERCENTAGE;
366356
channel->shutdown_wrong_funding = NULL;
367357
channel->closing_feerate_range = NULL;
368-
channel->alias[REMOTE] = channel->alias[LOCAL] = NULL;
369-
channel_set_random_local_alias(channel);
358+
channel->alias[REMOTE] = NULL;
359+
channel->alias[LOCAL] = tal(channel, struct short_channel_id);
360+
*channel->alias[LOCAL] = random_scid();
361+
/* We don't check for uniqueness. We would crash on a clash, but your machine is
362+
* probably broken beyond repair if it gets two equal 64 bit numbers */
363+
chanmap_add(channel->peer->ld, channel, *channel->alias[LOCAL]);
370364

371365
channel->shutdown_scriptpubkey[REMOTE] = NULL;
372366
channel->last_was_revoke = false;
@@ -487,9 +481,9 @@ struct channel *new_channel(struct peer *peer, u64 dbid,
487481
struct amount_sat our_funds,
488482
bool remote_channel_ready,
489483
/* NULL or stolen */
490-
struct short_channel_id *scid,
484+
struct short_channel_id *scid TAKES,
491485
struct short_channel_id *old_scids TAKES,
492-
struct short_channel_id *alias_local TAKES,
486+
struct short_channel_id alias_local,
493487
struct short_channel_id *alias_remote STEALS,
494488
struct channel_id *cid,
495489
struct amount_msat our_msat,
@@ -615,22 +609,18 @@ struct channel *new_channel(struct peer *peer, u64 dbid,
615609
channel->push = push;
616610
channel->our_funds = our_funds;
617611
channel->remote_channel_ready = remote_channel_ready;
618-
channel->scid = tal_steal(channel, scid);
612+
channel->scid = tal_dup_or_null(channel, struct short_channel_id, scid);
619613
channel->old_scids = tal_dup_talarr(channel, struct short_channel_id, old_scids);
620-
channel->alias[LOCAL] = tal_dup_or_null(channel, struct short_channel_id, alias_local);
614+
channel->alias[LOCAL] = tal_dup(channel, struct short_channel_id, &alias_local);
621615
/* All these possible short_channel_id variants go in the lookup table! */
622616
/* Stub channels all have the same scid though, *and* get loaded from db! */
623617
if (channel->scid && !is_stub_scid(*channel->scid))
624618
chanmap_add(peer->ld, channel, *channel->scid);
625-
if (channel->alias[LOCAL])
619+
if (!is_stub_scid(*channel->alias[LOCAL]))
626620
chanmap_add(peer->ld, channel, *channel->alias[LOCAL]);
627621
for (size_t i = 0; i < tal_count(channel->old_scids); i++)
628622
chanmap_add(peer->ld, channel, channel->old_scids[i]);
629623

630-
/* We always make sure this is set (historical channels from db might not) */
631-
if (!channel->alias[LOCAL])
632-
channel_set_random_local_alias(channel);
633-
634624
channel->alias[REMOTE] = tal_steal(channel, alias_remote); /* Haven't gotten one yet. */
635625
channel->cid = *cid;
636626
channel->our_msat = our_msat;
@@ -737,7 +727,7 @@ struct channel *new_channel(struct peer *peer, u64 dbid,
737727
}
738728
/* scid is NULL when opening a new channel so we don't
739729
* need to set error in that case as well */
740-
if (scid && is_stub_scid(*scid))
730+
if (channel->scid && is_stub_scid(*channel->scid))
741731
channel->error = towire_errorfmt(peer->ld,
742732
&channel->cid,
743733
"We can't be together anymore.");

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
@@ -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
@@ -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)