diff --git a/lightningd/channel.c b/lightningd/channel.c index 789d75554..4a82ae956 100644 --- a/lightningd/channel.c +++ b/lightningd/channel.c @@ -352,6 +352,8 @@ struct channel *new_channel(struct peer *peer, u64 dbid, bool remote_funding_locked, /* NULL or stolen */ struct short_channel_id *scid, + struct short_channel_id *alias_local STEALS, + struct short_channel_id *alias_remote STEALS, struct channel_id *cid, struct amount_msat our_msat, struct amount_msat msat_to_us_min, @@ -437,6 +439,8 @@ struct channel *new_channel(struct peer *peer, u64 dbid, channel->our_funds = our_funds; channel->remote_funding_locked = remote_funding_locked; channel->scid = tal_steal(channel, scid); + channel->alias[LOCAL] = tal_steal(channel, alias_local); + channel->alias[REMOTE] = tal_steal(channel, alias_remote); /* Haven't gotten one yet. */ channel->cid = *cid; channel->our_msat = our_msat; channel->msat_to_us_min = msat_to_us_min; diff --git a/lightningd/channel.h b/lightningd/channel.h index db81ce8c4..18d9e3606 100644 --- a/lightningd/channel.h +++ b/lightningd/channel.h @@ -135,6 +135,12 @@ struct channel { /* Channel if locked locally. */ struct short_channel_id *scid; + /* Alias used for option_zeroconf, or option_scid_alias, if + * present. LOCAL are all the alias we told the peer about and + * REMOTE is one of the aliases we got from the peer and we'll + * use in a routehint. */ + struct short_channel_id *alias[NUM_SIDES]; + struct channel_id cid; /* Amount going to us, not counting unfinished HTLCs; if we have one. */ @@ -278,6 +284,8 @@ struct channel *new_channel(struct peer *peer, u64 dbid, bool remote_funding_locked, /* NULL or stolen */ struct short_channel_id *scid STEALS, + struct short_channel_id *alias_local STEALS, + struct short_channel_id *alias_remote STEALS, struct channel_id *cid, struct amount_msat our_msatoshi, struct amount_msat msatoshi_to_us_min, diff --git a/lightningd/dual_open_control.c b/lightningd/dual_open_control.c index 86496d13d..595fad42a 100644 --- a/lightningd/dual_open_control.c +++ b/lightningd/dual_open_control.c @@ -33,6 +33,7 @@ #include #include #include +#include struct commit_rcvd { struct channel *channel; @@ -1239,6 +1240,12 @@ wallet_commit_channel(struct lightningd *ld, = p2wpkh_for_keyidx(channel, channel->peer->ld, channel->final_key_idx); + /* Can't have gotten their alias for this channel yet. */ + channel->alias[REMOTE] = NULL; + /* We do generate one ourselves however. */ + channel->alias[LOCAL] = tal(channel, struct short_channel_id); + randombytes_buf(channel->alias[LOCAL], sizeof(struct short_channel_id)); + channel->remote_upfront_shutdown_script = tal_steal(channel, remote_upfront_shutdown_script); diff --git a/lightningd/opening_control.c b/lightningd/opening_control.c index 8449edf9d..57240af93 100644 --- a/lightningd/opening_control.c +++ b/lightningd/opening_control.c @@ -29,6 +29,7 @@ #include #include #include +#include #include void json_add_uncommitted_channel(struct json_stream *response, @@ -99,6 +100,7 @@ wallet_commit_channel(struct lightningd *ld, s64 final_key_idx; u64 static_remotekey_start; u32 lease_start_blockheight = 0; /* No leases on v1 */ + struct short_channel_id *alias_local; /* We cannot both be the fundee *and* have a `fundchannel_start` * command running! @@ -158,6 +160,9 @@ wallet_commit_channel(struct lightningd *ld, else static_remotekey_start = 0x7FFFFFFFFFFFFFFF; + alias_local = tal(NULL, struct short_channel_id); + randombytes_buf(alias_local, sizeof(struct short_channel_id)); + channel = new_channel(uc->peer, uc->dbid, NULL, /* No shachain yet */ CHANNELD_AWAITING_LOCKIN, @@ -174,6 +179,8 @@ wallet_commit_channel(struct lightningd *ld, local_funding, false, /* !remote_funding_locked */ NULL, /* no scid yet */ + alias_local, /* But maybe we have an alias we want to use? */ + NULL, /* They haven't told us an alias yet */ cid, /* The three arguments below are msatoshi_to_us, * msatoshi_to_us_min, and msatoshi_to_us_max. diff --git a/wallet/db.c b/wallet/db.c index 04497400d..5ed908d3e 100644 --- a/wallet/db.c +++ b/wallet/db.c @@ -875,6 +875,14 @@ static struct migration dbmigrations[] = { {SQL("ALTER TABLE forwarded_payments ADD forward_style INTEGER DEFAULT NULL"), NULL}, /* "description" is used for label, so we use "paydescription" here */ {SQL("ALTER TABLE payments ADD paydescription TEXT;"), NULL}, + /* Alias we sent to the remote side, for zeroconf and + * option_scid_alias, can be a list of short_channel_ids if + * required, but keeping it a single SCID for now. */ + {SQL("ALTER TABLE channels ADD alias_local BIGINT DEFAULT NULL"), NULL}, + /* Alias we received from the peer, and which we should be using + * in routehints in invoices. The peer will remember all the + * aliases, but we only ever need one. */ + {SQL("ALTER TABLE channels ADD alias_remote BIGINT DEFAULT NULL"), NULL}, }; /** diff --git a/wallet/test/run-wallet.c b/wallet/test/run-wallet.c index 3c7802e2b..f3f636fce 100644 --- a/wallet/test/run-wallet.c +++ b/wallet/test/run-wallet.c @@ -1589,6 +1589,8 @@ static bool test_channel_inflight_crud(struct lightningd *ld, const tal_t *ctx) funding_sats, AMOUNT_MSAT(0), our_sats, 0, false, + NULL, /* alias[LOCAL] */ + NULL, /* alias[REMOTE] */ &cid, AMOUNT_MSAT(3333333000), AMOUNT_MSAT(33333), diff --git a/wallet/wallet.c b/wallet/wallet.c index 2c583f94b..a6355a62e 100644 --- a/wallet/wallet.c +++ b/wallet/wallet.c @@ -1250,7 +1250,7 @@ static struct channel *wallet_stmt2channel(struct wallet *w, struct db_stmt *stm struct channel_info channel_info; struct fee_states *fee_states; struct height_states *height_states; - struct short_channel_id *scid; + struct short_channel_id *scid, *alias[NUM_SIDES]; struct channel_id cid; struct channel *chan; u64 peer_dbid; @@ -1291,6 +1291,20 @@ static struct channel *wallet_stmt2channel(struct wallet *w, struct db_stmt *stm scid = NULL; } + if (!db_col_is_null(stmt, "alias_local")) { + alias[LOCAL] = tal(tmpctx, struct short_channel_id); + alias[LOCAL]->u64 = db_col_u64(stmt, "alias_local"); + } else { + alias[LOCAL] = NULL; + } + + if (!db_col_is_null(stmt, "alias_remote")) { + alias[REMOTE] = tal(tmpctx, struct short_channel_id); + alias[REMOTE]->u64 = db_col_u64(stmt, "alias_remote"); + } else { + alias[REMOTE] = NULL; + } + ok &= wallet_shachain_load(w, db_col_u64(stmt, "shachain_remote_id"), &wshachain); @@ -1449,6 +1463,8 @@ static struct channel *wallet_stmt2channel(struct wallet *w, struct db_stmt *stm our_funding_sat, db_col_int(stmt, "funding_locked_remote") != 0, scid, + alias[LOCAL], + alias[REMOTE], &cid, our_msat, msat_to_us_min, /* msatoshi_to_us_min */ @@ -1583,6 +1599,8 @@ static bool wallet_channels_load_active(struct wallet *w) ", lease_chan_max_ppt" ", htlc_minimum_msat" ", htlc_maximum_msat" + ", alias_local" + ", alias_remote" " FROM channels" " WHERE state != ?;")); //? 0 db_bind_int(stmt, 0, CLOSED); @@ -1864,8 +1882,10 @@ void wallet_channel_save(struct wallet *w, struct channel *chan) " lease_chan_max_msat=?," // 40 " lease_chan_max_ppt=?," // 41 " htlc_minimum_msat=?," // 42 - " htlc_maximum_msat=?" // 43 - " WHERE id=?")); // 44 + " htlc_maximum_msat=?," // 43 + " alias_local=?," // 44 + " alias_remote=?" // 45 + " WHERE id=?")); // 46 db_bind_u64(stmt, 0, chan->their_shachain.id); if (chan->scid) db_bind_short_channel_id(stmt, 1, chan->scid); @@ -1930,7 +1950,18 @@ void wallet_channel_save(struct wallet *w, struct channel *chan) } db_bind_amount_msat(stmt, 42, &chan->htlc_minimum_msat); db_bind_amount_msat(stmt, 43, &chan->htlc_maximum_msat); - db_bind_u64(stmt, 44, chan->dbid); + + if (chan->alias[LOCAL] != NULL) + db_bind_u64(stmt, 44, chan->alias[LOCAL]->u64); + else + db_bind_null(stmt, 44); + + if (chan->alias[REMOTE] != NULL) + db_bind_u64(stmt, 45, chan->alias[REMOTE]->u64); + else + db_bind_null(stmt, 45); + + db_bind_u64(stmt, 46, chan->dbid); db_exec_prepared_v2(take(stmt)); wallet_channel_config_save(w, &chan->channel_info.their_config);