Skip to content

Commit 6c972cd

Browse files
committed
close: Do not publish unilateral when witnessing a close onchain
Changelog-Changed: close: We no longer attempt to publish a unilateral close that'd fail anyway when we witness a close onchain.
1 parent 76ad48c commit 6c972cd

File tree

4 files changed

+35
-9
lines changed

4 files changed

+35
-9
lines changed

lightningd/channel.c

Lines changed: 17 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -902,22 +902,35 @@ void channel_fail_permanent(struct channel *channel,
902902
struct lightningd *ld = channel->peer->ld;
903903
va_list ap;
904904
char *why;
905+
/* Do we want to rebroadcast close transactions? If we're
906+
* witnessing the close on-chain there is no point in doing
907+
* this. */
908+
bool rebroadcast;
905909

906910
va_start(ap, fmt);
907911
why = tal_vfmt(tmpctx, fmt, ap);
908912
va_end(ap);
909913

910-
log_unusual(channel->log, "Peer permanent failure in %s: %s",
911-
channel_state_name(channel), why);
914+
log_unusual(channel->log,
915+
"Peer permanent failure in %s: %s (reason=%s)",
916+
channel_state_name(channel), why,
917+
channel_change_state_reason_str(reason));
912918

913919
/* We can have multiple errors, eg. onchaind failures. */
914920
if (!channel->error)
915921
channel->error = towire_errorfmt(channel,
916922
&channel->cid, "%s", why);
917923

918924
channel_set_owner(channel, NULL);
919-
/* Drop non-cooperatively (unilateral) to chain. */
920-
drop_to_chain(ld, channel, false);
925+
926+
/* Drop non-cooperatively (unilateral) to chain. If we detect
927+
* the close from the blockchain (i.e., reason is
928+
* REASON_ONCHAIN, or FUNDING_SPEND_SEEN) then we can observe
929+
* passively, and not broadcast our own unilateral close, as
930+
* it doesn't stand a chance anyway. */
931+
rebroadcast = !(channel->state == ONCHAIN ||
932+
channel->state == FUNDING_SPEND_SEEN);
933+
drop_to_chain(ld, channel, false, rebroadcast);
921934

922935
if (channel_state_wants_onchain_fail(channel->state))
923936
channel_set_state(channel,

lightningd/closing_control.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -303,7 +303,7 @@ static void peer_closing_complete(struct channel *channel, const u8 *msg)
303303
return;
304304

305305
/* Channel gets dropped to chain cooperatively. */
306-
drop_to_chain(channel->peer->ld, channel, true);
306+
drop_to_chain(channel->peer->ld, channel, true, true /* rebroadcast */);
307307
channel_set_state(channel,
308308
CLOSINGD_SIGEXCHANGE,
309309
CLOSINGD_COMPLETE,

lightningd/peer_control.c

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -348,7 +348,7 @@ static enum watch_result closed_inflight_depth_cb(struct lightningd *ld,
348348
}
349349

350350
void drop_to_chain(struct lightningd *ld, struct channel *channel,
351-
bool cooperative)
351+
bool cooperative, bool rebroadcast)
352352
{
353353
struct channel_inflight *inflight;
354354
const char *cmd_id;
@@ -387,6 +387,10 @@ void drop_to_chain(struct lightningd *ld, struct channel *channel,
387387
log_broken(channel->log,
388388
"Cannot broadcast our commitment tx:"
389389
" it's invalid! (ancient channel?)");
390+
} else if (!rebroadcast && !cooperative) {
391+
log_unusual(channel->log,
392+
"Not dropping our unilateral close onchain since "
393+
"we already saw theirs confirm.");
390394
} else {
391395
struct bitcoin_tx *tx COMPILER_WANTS_INIT("gcc 12.3.0");
392396

@@ -448,10 +452,10 @@ void resend_closing_transactions(struct lightningd *ld)
448452
case CLOSED:
449453
continue;
450454
case CLOSINGD_COMPLETE:
451-
drop_to_chain(ld, channel, true);
455+
drop_to_chain(ld, channel, true, true);
452456
continue;
453457
case AWAITING_UNILATERAL:
454-
drop_to_chain(ld, channel, false);
458+
drop_to_chain(ld, channel, false, true);
455459
continue;
456460
}
457461
abort();

lightningd/peer_control.h

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -109,7 +109,16 @@ void peer_set_dbid(struct peer *peer, u64 dbid);
109109
/* At startup, re-send any transactions we want bitcoind to have */
110110
void resend_closing_transactions(struct lightningd *ld);
111111

112-
void drop_to_chain(struct lightningd *ld, struct channel *channel, bool cooperative);
112+
/**
113+
* Initiate the close of a channel.
114+
*
115+
* @param rebroadcast: Whether we should be broadcasting our
116+
* commitment transaction in order to close the channel, or not.
117+
*/
118+
void drop_to_chain(struct lightningd *ld,
119+
struct channel *channel,
120+
bool cooperative,
121+
bool rebroadcast);
113122

114123
void update_channel_from_inflight(struct lightningd *ld,
115124
struct channel *channel,

0 commit comments

Comments
 (0)