Skip to content

Commit

Permalink
lightningd: set channel's local alias at init.
Browse files Browse the repository at this point in the history
Rather than having channeld/dualopend do it, we can set it and tell them.

Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
  • Loading branch information
rustyrussell committed Jan 31, 2024
1 parent 9c80a5c commit dac8964
Show file tree
Hide file tree
Showing 8 changed files with 39 additions and 29 deletions.
18 changes: 11 additions & 7 deletions channeld/channeld.c
Original file line number Diff line number Diff line change
Expand Up @@ -139,6 +139,9 @@ struct peer {
/* Which direction of the channel do we control? */
u16 channel_direction;

/* Local scid alias */
struct short_channel_id local_alias;

/* The scriptpubkey to use for shutting down. */
u32 *final_index;
struct ext_key *final_ext_key;
Expand Down Expand Up @@ -5272,6 +5275,7 @@ static void peer_reconnect(struct peer *peer,
&& next_commitment_number == 1) {
struct tlv_channel_ready_tlvs *tlvs = tlv_channel_ready_tlvs_new(tmpctx);

tlvs->short_channel_id = &peer->local_alias;
status_debug("Retransmitting channel_ready for channel %s",
type_to_string(tmpctx, struct channel_id, &peer->channel_id));
/* Contains per commit point #1, for first post-opening commit */
Expand Down Expand Up @@ -5554,7 +5558,7 @@ static void peer_reconnect(struct peer *peer,
static void handle_funding_depth(struct peer *peer, const u8 *msg)
{
u32 depth;
struct short_channel_id *scid, *alias_local;
struct short_channel_id *scid;
struct tlv_channel_ready_tlvs *tlvs;
struct pubkey point;
bool splicing;
Expand All @@ -5563,7 +5567,6 @@ static void handle_funding_depth(struct peer *peer, const u8 *msg)
if (!fromwire_channeld_funding_depth(tmpctx,
msg,
&scid,
&alias_local,
&depth,
&splicing,
&txid))
Expand Down Expand Up @@ -5594,15 +5597,15 @@ static void handle_funding_depth(struct peer *peer, const u8 *msg)
status_debug("handle_funding_depth: Setting short_channel_ids[LOCAL] to %s",
type_to_string(tmpctx,
struct short_channel_id,
(scid ? scid : alias_local)));
(scid ? scid : &peer->local_alias)));
/* If we know an actual short_channel_id prefer to use
* that, otherwise fill in the alias. From channeld's
* point of view switching from zeroconf to an actual
* funding scid is just a reorg. */
if (scid)
peer->short_channel_ids[LOCAL] = *scid;
else if (alias_local)
peer->short_channel_ids[LOCAL] = *alias_local;
else
peer->short_channel_ids[LOCAL] = peer->local_alias;
}

if (!peer->channel_ready[LOCAL]) {
Expand All @@ -5612,7 +5615,7 @@ static void handle_funding_depth(struct peer *peer, const u8 *msg)
type_to_string(tmpctx, struct pubkey,
&peer->next_local_per_commit));
tlvs = tlv_channel_ready_tlvs_new(tmpctx);
tlvs->short_channel_id = alias_local;
tlvs->short_channel_id = &peer->local_alias;

/* Need to retrieve the first point again, even if we
* moved on, as channel_ready explicitly includes the
Expand Down Expand Up @@ -6166,7 +6169,8 @@ static void init_channel(struct peer *peer)
&pbases,
&reestablish_only,
&peer->experimental_upgrade,
&peer->splice_state->inflights)) {
&peer->splice_state->inflights,
&peer->local_alias)) {
master_badmsg(WIRE_CHANNELD_INIT, msg);
}

Expand Down
3 changes: 1 addition & 2 deletions channeld/channeld_wire.csv
Original file line number Diff line number Diff line change
Expand Up @@ -81,13 +81,12 @@ msgdata,channeld_init,reestablish_only,bool,
msgdata,channeld_init,experimental_upgrade,bool,
msgdata,channeld_init,num_inflights,u16,
msgdata,channeld_init,inflights,inflight,num_inflights
msgdata,channeld_init,scid_alias,short_channel_id,

# master->channeld funding hit new depth(funding locked if >= lock depth)
# alias != NULL if zeroconf and short_channel_id == NULL
# short_channel_id != NULL once we have 3+ confirmations
msgtype,channeld_funding_depth,1002
msgdata,channeld_funding_depth,short_channel_id,?short_channel_id,
msgdata,channeld_funding_depth,alias_local,?short_channel_id,
msgdata,channeld_funding_depth,depth,u32,
msgdata,channeld_funding_depth,splicing,bool,
msgdata,channeld_funding_depth,txid,bitcoin_txid,
Expand Down
10 changes: 7 additions & 3 deletions lightningd/channel.c
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
#include <lightningd/opening_common.h>
#include <lightningd/peer_control.h>
#include <lightningd/subd.h>
#include <sodium/randombytes.h>
#include <wallet/txfilter.h>
#include <wire/peer_wire.h>

Expand Down Expand Up @@ -278,7 +279,10 @@ struct channel *new_unsaved_channel(struct peer *peer,
channel->closing_feerate_range = NULL;
channel->peer_update = NULL;
channel->channel_update = NULL;
channel->alias[LOCAL] = channel->alias[REMOTE] = NULL;
channel->alias[REMOTE] = NULL;
/* We don't even bother checking for clashes. */
channel->alias[LOCAL] = tal(channel, struct short_channel_id);
randombytes_buf(channel->alias[LOCAL], sizeof(struct short_channel_id));

channel->shutdown_scriptpubkey[REMOTE] = NULL;
channel->last_was_revoke = false;
Expand Down Expand Up @@ -397,7 +401,7 @@ struct channel *new_channel(struct peer *peer, u64 dbid,
bool remote_channel_ready,
/* NULL or stolen */
struct short_channel_id *scid,
struct short_channel_id *alias_local STEALS,
struct short_channel_id *alias_local TAKES,
struct short_channel_id *alias_remote STEALS,
struct channel_id *cid,
struct amount_msat our_msat,
Expand Down Expand Up @@ -508,7 +512,7 @@ struct channel *new_channel(struct peer *peer, u64 dbid,
channel->our_funds = our_funds;
channel->remote_channel_ready = remote_channel_ready;
channel->scid = tal_steal(channel, scid);
channel->alias[LOCAL] = tal_steal(channel, alias_local);
channel->alias[LOCAL] = tal_dup_or_null(channel, struct short_channel_id, alias_local);
channel->alias[REMOTE] = tal_steal(channel, alias_remote); /* Haven't gotten one yet. */
channel->cid = *cid;
channel->our_msat = our_msat;
Expand Down
8 changes: 4 additions & 4 deletions lightningd/channel_control.c
Original file line number Diff line number Diff line change
Expand Up @@ -594,7 +594,6 @@ static enum watch_result splice_depth_cb(struct lightningd *ld,
subd_send_msg(inflight->channel->owner,
take(towire_channeld_funding_depth(
NULL, &scid,
inflight->channel->alias[LOCAL],
depth, true, txid)));
}

Expand Down Expand Up @@ -1700,7 +1699,8 @@ bool peer_start_channeld(struct channel *channel,
reestablish_only,
ld->experimental_upgrade_protocol,
cast_const2(const struct inflight **,
inflights));
inflights),
channel->alias[LOCAL]);

/* We don't expect a response: we are triggered by funding_depth_cb. */
subd_send_msg(channel->owner, take(initmsg));
Expand All @@ -1719,7 +1719,7 @@ bool peer_start_channeld(struct channel *channel,
/* Artificial confirmation event for zeroconf */
subd_send_msg(channel->owner,
take(towire_channeld_funding_depth(
NULL, channel->scid, channel->alias[LOCAL], 0, false,
NULL, channel->scid, 0, false,
&txid)));
return true;
}
Expand All @@ -1742,7 +1742,7 @@ void channeld_tell_depth(struct channel *channel,

subd_send_msg(channel->owner,
take(towire_channeld_funding_depth(
NULL, channel->scid, channel->alias[LOCAL], depth,
NULL, channel->scid, depth,
false, txid)));
}

Expand Down
11 changes: 3 additions & 8 deletions lightningd/dual_open_control.c
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,6 @@
#include <lightningd/peer_fd.h>
#include <lightningd/plugin_hook.h>
#include <openingd/dualopend_wiregen.h>
#include <sodium/randombytes.h>

struct commit_rcvd {
struct channel *channel;
Expand Down Expand Up @@ -1465,12 +1464,6 @@ wallet_commit_channel(struct lightningd *ld,
= p2tr_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);

Expand Down Expand Up @@ -4055,6 +4048,7 @@ bool peer_start_dualopend(struct peer *peer,
&channel->local_funding_pubkey,
channel->minimum_depth,
peer->ld->config.require_confirmed_inputs,
channel->alias[LOCAL],
peer->ld->dev_any_channel_type);
subd_send_msg(channel->owner, take(msg));
return true;
Expand Down Expand Up @@ -4168,7 +4162,8 @@ bool peer_restart_dualopend(struct peer *peer,
NULL : &inflight->lease_amt,
channel->type,
channel->req_confirmed_ins[LOCAL],
channel->req_confirmed_ins[REMOTE]);
channel->req_confirmed_ins[REMOTE],
channel->alias[LOCAL]);

subd_send_msg(channel->owner, take(msg));
return true;
Expand Down
8 changes: 4 additions & 4 deletions lightningd/opening_control.c
Original file line number Diff line number Diff line change
Expand Up @@ -112,7 +112,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;
struct short_channel_id local_alias;
struct timeabs timestamp;
bool any_active = peer_any_channel(uc->peer, channel_state_wants_peercomms, NULL);

Expand Down Expand Up @@ -174,8 +174,8 @@ 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));
/* This won't clash, we don't even bother checking */
randombytes_buf(&local_alias, sizeof(local_alias));

channel = new_channel(uc->peer, uc->dbid,
NULL, /* No shachain yet */
Expand All @@ -194,7 +194,7 @@ wallet_commit_channel(struct lightningd *ld,
local_funding,
false, /* !remote_channel_ready */
NULL, /* no scid yet */
alias_local, /* But maybe we have an alias we want to use? */
&local_alias,
NULL, /* They haven't told us an alias yet */
cid,
/* The three arguments below are msatoshi_to_us,
Expand Down
8 changes: 7 additions & 1 deletion openingd/dualopend.c
Original file line number Diff line number Diff line change
Expand Up @@ -227,6 +227,9 @@ struct state {
/* Does this negotation require confirmed inputs? */
bool require_confirmed_inputs[NUM_SIDES];

/* Our alias */
struct short_channel_id local_alias;

bool dev_accept_any_channel_type;
};

Expand Down Expand Up @@ -3869,6 +3872,7 @@ static void send_channel_ready(struct state *state)
/* Figure out the next local commit */
hsm_per_commitment_point(1, &next_local_per_commit);

tlvs->short_channel_id = &state->local_alias;
msg = towire_channel_ready(NULL, &state->channel_id,
&next_local_per_commit, tlvs);
peer_write(state->pps, take(msg));
Expand Down Expand Up @@ -4382,6 +4386,7 @@ int main(int argc, char *argv[])
&state->our_funding_pubkey,
&state->minimum_depth,
&state->require_confirmed_inputs[LOCAL],
&state->local_alias,
&state->dev_accept_any_channel_type)) {
/*~ Initially we're not associated with a channel, but
* handle_peer_gossip_or_error compares this. */
Expand Down Expand Up @@ -4446,7 +4451,8 @@ int main(int argc, char *argv[])
&requested_lease,
&state->channel_type,
&state->require_confirmed_inputs[LOCAL],
&state->require_confirmed_inputs[REMOTE])) {
&state->require_confirmed_inputs[REMOTE],
&state->local_alias)) {

bool ok;

Expand Down
2 changes: 2 additions & 0 deletions openingd/dualopend_wire.csv
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ msgdata,dualopend_init,our_funding_pubkey,pubkey,
# Constraints in case the other end tries to open a channel.
msgdata,dualopend_init,minimum_depth,u32,
msgdata,dualopend_init,require_confirmed_inputs,bool,
msgdata,dualopend_init,local_alias,short_channel_id,
msgdata,dualopend_init,dev_accept_any_channel_type,bool,

# master-dualopend: peer has reconnected
Expand Down Expand Up @@ -77,6 +78,7 @@ msgdata,dualopend_reinit,requested_lease,?amount_sat,
msgdata,dualopend_reinit,channel_type,channel_type,
msgdata,dualopend_reinit,we_require_confirmed_inputs,bool,
msgdata,dualopend_reinit,they_require_confirmed_inputs,bool,
msgdata,dualopend_reinit,local_alias,short_channel_id,

# dualopend->master: they offered channel, should we continue?
msgtype,dualopend_got_offer,7005
Expand Down

0 comments on commit dac8964

Please sign in to comment.