diff --git a/lightningd/channel_control.c b/lightningd/channel_control.c index c88491603..07465c395 100644 --- a/lightningd/channel_control.c +++ b/lightningd/channel_control.c @@ -235,18 +235,11 @@ static void peer_start_closingd_after_shutdown(struct channel *channel, channel_set_state(channel, CHANNELD_SHUTTING_DOWN, CLOSINGD_SIGEXCHANGE); } -static void handle_error_channel(struct channel *channel, - const u8 *msg) +static void forget(struct channel *channel) { struct command **forgets = tal_steal(tmpctx, channel->forgets); channel->forgets = tal_arr(channel, struct command *, 0); - if (!fromwire_channel_send_error_reply(msg)) { - channel_internal_error(channel, "bad send_error_reply: %s", - tal_hex(tmpctx, msg)); - return; - } - /* Forget the channel. */ delete_channel(channel); @@ -262,6 +255,35 @@ static void handle_error_channel(struct channel *channel, tal_free(forgets); } +static void handle_error_channel(struct channel *channel, + const u8 *msg) +{ + if (!fromwire_channel_send_error_reply(msg)) { + channel_internal_error(channel, "bad send_error_reply: %s", + tal_hex(tmpctx, msg)); + return; + } + + forget(channel); +} + +void forget_channel(struct channel *channel, const char *why) +{ + struct channel_id cid; + + derive_channel_id(&cid, &channel->funding_txid, + channel->funding_outnum); + channel->error = towire_errorfmt(channel, &cid, "%s", why); + + /* If the peer is connected, we let them know. Otherwise + * we just directly remove the channel */ + if (channel->owner) + subd_send_msg(channel->owner, + take(towire_channel_send_error(NULL, why))); + else + forget(channel); +} + static unsigned channel_msg(struct subd *sd, const u8 *msg, const int *fds) { enum channel_wire_type t = fromwire_peektype(msg); diff --git a/lightningd/channel_control.h b/lightningd/channel_control.h index 0e50ce7f3..10542e77f 100644 --- a/lightningd/channel_control.h +++ b/lightningd/channel_control.h @@ -30,4 +30,7 @@ struct command_result *cancel_channel_before_broadcast(struct command *cmd, struct peer *peer, const jsmntok_t *cidtok); +/* Forget a channel. Deletes the channel and handles all + * associated waiting commands, if present. Notifies peer if available */ +void forget_channel(struct channel *channel, const char *err_msg); #endif /* LIGHTNING_LIGHTNINGD_CHANNEL_CONTROL_H */