Skip to content

Commit d912287

Browse files
committed
lightningd: save previous short_channel_ids during splice, and keep in db.
There can be any number of these, and it will be useful to allow routing by older scids (when other nodes haven't seen our gossip, or even before we *can* announce the new post-splice channel). Signed-off-by: Rusty Russell <[email protected]>
1 parent 21d9f08 commit d912287

File tree

8 files changed

+39
-2
lines changed

8 files changed

+39
-2
lines changed

lightningd/channel.c

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -242,6 +242,19 @@ struct open_attempt *new_channel_open_attempt(struct channel *channel)
242242
return oa;
243243
}
244244

245+
void channel_add_old_scid(struct channel *channel,
246+
struct short_channel_id old_scid)
247+
{
248+
/* If this is not public, we skip */
249+
if (!(channel->channel_flags & CHANNEL_FLAGS_ANNOUNCE_CHANNEL))
250+
return;
251+
252+
if (!channel->old_scids)
253+
channel->old_scids = tal_dup(channel, struct short_channel_id, &old_scid);
254+
else
255+
tal_arr_expand(&channel->old_scids, old_scid);
256+
}
257+
245258
struct channel *new_unsaved_channel(struct peer *peer,
246259
u32 feerate_base,
247260
u32 feerate_ppm)
@@ -275,6 +288,7 @@ struct channel *new_unsaved_channel(struct peer *peer,
275288
channel->last_htlc_sigs = NULL;
276289
channel->remote_channel_ready = false;
277290
channel->scid = NULL;
291+
channel->old_scids = NULL;
278292
channel->next_index[LOCAL] = 1;
279293
channel->next_index[REMOTE] = 1;
280294
channel->next_htlc_id = 0;
@@ -411,6 +425,7 @@ struct channel *new_channel(struct peer *peer, u64 dbid,
411425
bool remote_channel_ready,
412426
/* NULL or stolen */
413427
struct short_channel_id *scid,
428+
struct short_channel_id *old_scids TAKES,
414429
struct short_channel_id *alias_local TAKES,
415430
struct short_channel_id *alias_remote STEALS,
416431
struct channel_id *cid,
@@ -538,6 +553,7 @@ struct channel *new_channel(struct peer *peer, u64 dbid,
538553
channel->our_funds = our_funds;
539554
channel->remote_channel_ready = remote_channel_ready;
540555
channel->scid = tal_steal(channel, scid);
556+
channel->old_scids = tal_dup_talarr(channel, struct short_channel_id, old_scids);
541557
channel->alias[LOCAL] = tal_dup_or_null(channel, struct short_channel_id, alias_local);
542558
/* We always make sure this is set (historical channels from db might not) */
543559
if (!channel->alias[LOCAL]) {

lightningd/channel.h

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -214,6 +214,8 @@ struct channel {
214214
bool remote_channel_ready;
215215
/* Channel if locked locally. */
216216
struct short_channel_id *scid;
217+
/* Old scids if we were spliced */
218+
struct short_channel_id *old_scids;
217219

218220
/* Alias used for option_zeroconf, or option_scid_alias, if
219221
* present. LOCAL are all the alias we told the peer about and
@@ -388,6 +390,7 @@ struct channel *new_channel(struct peer *peer, u64 dbid,
388390
bool remote_channel_ready,
389391
/* NULL or stolen */
390392
struct short_channel_id *scid STEALS,
393+
struct short_channel_id *old_scids TAKES,
391394
struct short_channel_id *alias_local STEALS,
392395
struct short_channel_id *alias_remote STEALS,
393396
struct channel_id *cid,
@@ -487,6 +490,10 @@ u32 channel_last_funding_feerate(const struct channel *channel);
487490
/* Only set completely_eliminate for never-existed channels */
488491
void delete_channel(struct channel *channel STEALS, bool completely_eliminate);
489492

493+
/* Add a historic (public) short_channel_id to this channel */
494+
void channel_add_old_scid(struct channel *channel,
495+
struct short_channel_id old_scid);
496+
490497
const char *channel_state_name(const struct channel *channel);
491498
const char *channel_state_str(enum channel_state state);
492499

lightningd/channel_control.c

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -815,12 +815,16 @@ bool depthcb_update_scid(struct channel *channel,
815815
lockin_has_completed(channel, false);
816816

817817
} else {
818+
struct short_channel_id old_scid = *channel->scid;
819+
818820
/* We freaked out if required when original was
819821
* removed, so just update now */
820822
log_info(channel->log, "Short channel id changed from %s->%s",
821823
fmt_short_channel_id(tmpctx, *channel->scid),
822824
fmt_short_channel_id(tmpctx, scid));
823825
*channel->scid = scid;
826+
/* In case we broadcast it before (e.g. splice!) */
827+
channel_add_old_scid(channel, old_scid);
824828
channel_gossip_scid_changed(channel);
825829
}
826830

lightningd/opening_control.c

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -189,6 +189,7 @@ wallet_commit_channel(struct lightningd *ld,
189189
local_funding,
190190
false, /* !remote_channel_ready */
191191
NULL, /* no scid yet */
192+
NULL, /* no old scids */
192193
NULL, /* assign random local alias */
193194
NULL, /* They haven't told us an alias yet */
194195
cid,
@@ -1579,6 +1580,7 @@ static struct channel *stub_chan(struct command *cmd,
15791580
AMOUNT_SAT(0),
15801581
true, /* remote_channel_ready */
15811582
scid,
1583+
NULL,
15821584
scid,
15831585
scid,
15841586
&cid,

wallet/db.c

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1042,6 +1042,7 @@ static struct migration dbmigrations[] = {
10421042
{NULL, NULL}, /* Old, incorrect channel_htlcs_wait_indexes migration */
10431043
{SQL("ALTER TABLE channel_funding_inflights ADD locked_scid BIGINT DEFAULT 0;"), NULL},
10441044
{NULL, migrate_initialize_channel_htlcs_wait_indexes_and_fixup_forwards},
1045+
{SQL("ALTER TABLE channels ADD old_scids BLOB DEFAULT NULL;"), NULL},
10451046
};
10461047

10471048
/**

wallet/test/run-db.c

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -188,6 +188,7 @@ struct channel *new_channel(struct peer *peer UNNEEDED, u64 dbid UNNEEDED,
188188
bool remote_channel_ready UNNEEDED,
189189
/* NULL or stolen */
190190
struct short_channel_id *scid STEALS UNNEEDED,
191+
struct short_channel_id *old_scids TAKES UNNEEDED,
191192
struct short_channel_id *alias_local STEALS UNNEEDED,
192193
struct short_channel_id *alias_remote STEALS UNNEEDED,
193194
struct channel_id *cid UNNEEDED,

wallet/test/run-wallet.c

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2026,7 +2026,8 @@ static bool test_channel_inflight_crud(struct lightningd *ld, const tal_t *ctx)
20262026
&outpoint,
20272027
funding_sats, AMOUNT_MSAT(0),
20282028
our_sats,
2029-
0, false,
2029+
0, NULL,
2030+
NULL, /* old scids */
20302031
NULL, /* alias[LOCAL] */
20312032
NULL, /* alias[REMOTE] */
20322033
&cid,

wallet/wallet.c

Lines changed: 6 additions & 1 deletion
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];
1777+
struct short_channel_id *scid, *alias[NUM_SIDES], *old_scids;
17781778
struct channel_id cid;
17791779
struct channel *chan;
17801780
u64 peer_dbid;
@@ -1813,6 +1813,7 @@ static struct channel *wallet_stmt2channel(struct wallet *w, struct db_stmt *stm
18131813
}
18141814

18151815
scid = db_col_optional_scid(tmpctx, stmt, "scid");
1816+
old_scids = db_col_short_channel_id_arr(tmpctx, stmt, "old_scids");
18161817
alias[LOCAL] = db_col_optional_scid(tmpctx, stmt, "alias_local");
18171818
alias[REMOTE] = db_col_optional_scid(tmpctx, stmt, "alias_remote");
18181819

@@ -2027,6 +2028,7 @@ static struct channel *wallet_stmt2channel(struct wallet *w, struct db_stmt *stm
20272028
our_funding_sat,
20282029
db_col_int(stmt, "funding_locked_remote") != 0,
20292030
scid,
2031+
old_scids,
20302032
alias[LOCAL],
20312033
alias[REMOTE],
20322034
&cid,
@@ -2241,6 +2243,7 @@ static bool wallet_channels_load_active(struct wallet *w)
22412243
" id"
22422244
", peer_id"
22432245
", scid"
2246+
", old_scids"
22442247
", full_channel_id"
22452248
", channel_config_local"
22462249
", channel_config_remote"
@@ -2521,6 +2524,7 @@ void wallet_channel_save(struct wallet *w, struct channel *chan)
25212524
stmt = db_prepare_v2(w->db, SQL("UPDATE channels SET"
25222525
" shachain_remote_id=?,"
25232526
" scid=?,"
2527+
" old_scids=?,"
25242528
" full_channel_id=?,"
25252529
" state=?,"
25262530
" funder=?,"
@@ -2580,6 +2584,7 @@ void wallet_channel_save(struct wallet *w, struct channel *chan)
25802584
else
25812585
db_bind_null(stmt);
25822586

2587+
db_bind_short_channel_id_arr(stmt, chan->old_scids);
25832588
db_bind_channel_id(stmt, &chan->cid);
25842589
db_bind_int(stmt, channel_state_in_db(chan->state));
25852590
db_bind_int(stmt, chan->opener);

0 commit comments

Comments
 (0)