Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Negotiate close even if we already consider the channel closed. #4559

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
60 changes: 48 additions & 12 deletions channeld/channeld.c
Original file line number Diff line number Diff line change
Expand Up @@ -918,6 +918,15 @@ static void maybe_send_shutdown(struct peer *peer)
billboard_update(peer);
}

static void send_shutdown_complete(struct peer *peer)
{
/* Now we can tell master shutdown is complete. */
wire_sync_write(MASTER_FD,
take(towire_channeld_shutdown_complete(NULL, peer->pps)));
per_peer_state_fdpass_send(MASTER_FD, peer->pps);
close(MASTER_FD);
}

/* This queues other traffic from the fd until we get reply. */
static u8 *master_wait_sync_reply(const tal_t *ctx,
struct peer *peer,
Expand Down Expand Up @@ -2511,7 +2520,8 @@ static bool capture_premature_msg(const u8 ***shit_lnd_says, const u8 *msg)
}

static void peer_reconnect(struct peer *peer,
const struct secret *last_remote_per_commit_secret)
const struct secret *last_remote_per_commit_secret,
u8 *reestablish_only)
{
struct channel_id channel_id;
/* Note: BOLT #2 uses these names! */
Expand Down Expand Up @@ -2636,6 +2646,13 @@ static void peer_reconnect(struct peer *peer,
bool soft_error = peer->funding_locked[REMOTE]
|| peer->funding_locked[LOCAL];

/* If they sent reestablish, we analyze it for courtesy, but also
* in case *they* are ahead of us! */
if (reestablish_only) {
msg = reestablish_only;
goto got_reestablish;
}

/* Read until they say something interesting (don't forward
* gossip *to* them yet: we might try sending channel_update
* before we've reestablished channel). */
Expand All @@ -2647,6 +2664,7 @@ static void peer_reconnect(struct peer *peer,
msg) ||
capture_premature_msg(&premature_msgs, msg));

got_reestablish:
#if EXPERIMENTAL_FEATURES
recv_tlvs = tlv_channel_reestablish_tlvs_new(tmpctx);
#endif
Expand All @@ -2668,6 +2686,15 @@ static void peer_reconnect(struct peer *peer,
tal_hex(msg, msg));
}

if (!channel_id_eq(&channel_id, &peer->channel_id)) {
peer_failed_err(peer->pps,
&channel_id,
"bad reestablish msg for unknown channel %s: %s",
type_to_string(tmpctx, struct channel_id,
&channel_id),
tal_hex(msg, msg));
}

status_debug("Got reestablish commit=%"PRIu64" revoke=%"PRIu64,
next_commitment_number,
next_revocation_number);
Expand Down Expand Up @@ -2910,6 +2937,19 @@ static void peer_reconnect(struct peer *peer,

#endif /* EXPERIMENTAL_FEATURES */

/* Now stop, we've been polite long enough. */
if (reestablish_only) {
/* If we were successfully closing, we still go to closingd. */
if (shutdown_complete(peer)) {
send_shutdown_complete(peer);
daemon_shutdown();
exit(0);
}
peer_failed_err(peer->pps,
&peer->channel_id,
"Channel is already closed");
}

/* Corner case: we didn't send shutdown before because update_add_htlc
* pending, but now they're cleared by restart, and we're actually
* complete. In that case, their `shutdown` will trigger us. */
Expand Down Expand Up @@ -3400,6 +3440,7 @@ static void init_channel(struct peer *peer)
secp256k1_ecdsa_signature *remote_ann_bitcoin_sig;
bool option_static_remotekey, option_anchor_outputs;
struct penalty_base *pbases;
u8 *reestablish_only;
#if !DEVELOPER
bool dev_fail_process_onionpacket; /* Ignored */
#endif
Expand Down Expand Up @@ -3461,7 +3502,8 @@ static void init_channel(struct peer *peer)
&option_anchor_outputs,
&dev_fast_gossip,
&dev_fail_process_onionpacket,
&pbases)) {
&pbases,
&reestablish_only)) {
master_badmsg(WIRE_CHANNELD_INIT, msg);
}

Expand Down Expand Up @@ -3553,7 +3595,10 @@ static void init_channel(struct peer *peer)

/* OK, now we can process peer messages. */
if (reconnected)
peer_reconnect(peer, &last_remote_per_commit_secret);
peer_reconnect(peer, &last_remote_per_commit_secret,
reestablish_only);
else
assert(!reestablish_only);

/* If we have a messages to send, send them immediately */
if (fwd_msg)
Expand All @@ -3565,15 +3610,6 @@ static void init_channel(struct peer *peer)
billboard_update(peer);
}

static void send_shutdown_complete(struct peer *peer)
{
/* Now we can tell master shutdown is complete. */
wire_sync_write(MASTER_FD,
take(towire_channeld_shutdown_complete(NULL, peer->pps)));
per_peer_state_fdpass_send(MASTER_FD, peer->pps);
close(MASTER_FD);
}

static void try_read_gossip_store(struct peer *peer)
{
u8 *msg = gossip_store_next(tmpctx, peer->pps);
Expand Down
2 changes: 2 additions & 0 deletions channeld/channeld_wire.csv
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,8 @@ msgdata,channeld_init,dev_fast_gossip,bool,
msgdata,channeld_init,dev_fail_process_onionpacket,bool,
msgdata,channeld_init,num_penalty_bases,u32,
msgdata,channeld_init,pbases,penalty_base,num_penalty_bases
msgdata,channeld_init,reestablish_only_len,u16,
msgdata,channeld_init,reestablish_only,u8,reestablish_only_len

# master->channeld funding hit new depth(funding locked if >= lock depth)
msgtype,channeld_funding_depth,1002
Expand Down
14 changes: 11 additions & 3 deletions channeld/channeld_wiregen.c

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading