From 7d1a43d5d5a41ffec9d87629e04d102e2becfadd Mon Sep 17 00:00:00 2001 From: Dusty Daemon Date: Fri, 4 Oct 2024 15:05:55 -0400 Subject: [PATCH] channeld: Fix `tx_abort` encoding Switch to using same message format for `tx_abort` that wire_error and wire_warning use. Changelog-None --- channeld/channeld.c | 17 +++++++++++++---- channeld/channeld_wire.csv | 2 +- common/wire_error.c | 37 +++++++++++++++++++++++++++++++++++++ common/wire_error.h | 24 ++++++++++++++++++++++++ 4 files changed, 75 insertions(+), 5 deletions(-) diff --git a/channeld/channeld.c b/channeld/channeld.c index 923450044..baf8ebb56 100644 --- a/channeld/channeld.c +++ b/channeld/channeld.c @@ -1709,7 +1709,8 @@ static void check_tx_abort(struct peer *peer, const u8 *msg) struct inflight *inflight = last_inflight(peer); struct bitcoin_outpoint *outpoint; struct channel_id channel_id; - u8 *reason; + u8 *data; + char *reason; if (fromwire_peektype(msg) != WIRE_TX_ABORT) return; @@ -1721,7 +1722,7 @@ static void check_tx_abort(struct peer *peer, const u8 *msg) tal_hex(tmpctx, msg)); } - if (!fromwire_tx_abort(tmpctx, msg, &channel_id, &reason)) + if (!fromwire_tx_abort(tmpctx, msg, &channel_id, &data)) peer_failed_warn(peer->pps, &peer->channel_id, "bad tx_abort %s", tal_hex(msg, msg)); @@ -1736,10 +1737,17 @@ static void check_tx_abort(struct peer *peer, const u8 *msg) status_info("Send tx_abort to master"); + reason = sanitize_error(tmpctx, msg, &peer->channel_id); + + status_info("Peer initiated tx_abort for reason: %s", reason); + wire_sync_write(MASTER_FD, take(towire_channeld_splice_abort(NULL, false, outpoint, - (char*)reason))); + tal_fmt(tmpctx, + "Peer aborted" + " for reason: %s", + reason)))); /* Give master a chance to pass the fd along */ status_info("Delaying closing of master fd by 1 second"); @@ -1769,7 +1777,8 @@ static void splice_abort(struct peer *peer, const char *fmt, ...) status_info("We are initiating tx_abort for reason: %s", reason); peer_write(peer->pps, - take(towire_tx_abort(NULL, &peer->channel_id, (u8*)reason))); + take(towire_abortfmt(NULL, &peer->channel_id, "%s", + reason))); do { msg = peer_read(tmpctx, peer->pps); diff --git a/channeld/channeld_wire.csv b/channeld/channeld_wire.csv index 592a8b65a..0f05342b1 100644 --- a/channeld/channeld_wire.csv +++ b/channeld/channeld_wire.csv @@ -287,7 +287,7 @@ msgdata,channeld_splice_state_error,state_error,wirestring, msgtype,channeld_splice_abort,7223 msgdata,channeld_splice_abort,did_i_initiate,bool, msgdata,channeld_splice_abort,inflight_outpoint,?bitcoin_outpoint, -msgdata,channeld_splice_abort,reason,?wirestring, +msgdata,channeld_splice_abort,reason,wirestring, # master->channeld: Please enter stfu mode msgtype,channeld_stfu,7224 diff --git a/common/wire_error.c b/common/wire_error.c index 97e121ee7..512a1cf6b 100644 --- a/common/wire_error.c +++ b/common/wire_error.c @@ -72,6 +72,43 @@ u8 *towire_warningfmt(const tal_t *ctx, return msg; } +u8 *towire_abortfmtv(const tal_t *ctx, + const struct channel_id *channel, + const char *fmt, + va_list ap) +{ + /* BOLT #1: + * + * The channel is referred to by `channel_id`, unless `channel_id` is + * 0 (i.e. all bytes are 0), in which case it refers to all + * channels. */ + static const struct channel_id all_channels; + char *estr; + u8 *msg; + + estr = tal_vfmt(ctx, fmt, ap); + /* We need tal_len to work, so we use copy. */ + msg = towire_tx_abort(ctx, channel ? channel : &all_channels, + (u8 *)tal_dup_arr(estr, char, estr, strlen(estr), 0)); + tal_free(estr); + + return msg; +} + +u8 *towire_abortfmt(const tal_t *ctx, + const struct channel_id *channel, + const char *fmt, ...) +{ + va_list ap; + u8 *msg; + + va_start(ap, fmt); + msg = towire_abortfmtv(ctx, channel, fmt, ap); + va_end(ap); + + return msg; +} + bool channel_id_is_all(const struct channel_id *channel_id) { return memeqzero(channel_id, sizeof(*channel_id)); diff --git a/common/wire_error.h b/common/wire_error.h index 7797295bb..6fe932513 100644 --- a/common/wire_error.h +++ b/common/wire_error.h @@ -54,6 +54,30 @@ u8 *towire_warningfmtv(const tal_t *ctx, const char *fmt, va_list ap); +/** + * towire_abortfmt - helper to turn string into WIRE_TX_ABORT. + * + * @ctx: context to allocate from + * @channel: specific channel to complain about, or NULL for all. + * @fmt: format for warning. + */ +u8 *towire_abortfmt(const tal_t *ctx, + const struct channel_id *channel, + const char *fmt, ...) PRINTF_FMT(3,4); + +/** + * towire_abortfmtv - helper to turn string into WIRE_TX_ABORT. + * + * @ctx: context to allocate from + * @channel: specific channel to complain about, or NULL for all. + * @fmt: format for warning. + * @ap: accumulated varargs. + */ +u8 *towire_abortfmtv(const tal_t *ctx, + const struct channel_id *channel, + const char *fmt, + va_list ap); + /* BOLT #1: * * The channel is referred to by `channel_id`, unless `channel_id` is 0