From 3589cf6713c6b19c64a99abf948effb8f6d79664 Mon Sep 17 00:00:00 2001 From: Rusty Russell Date: Mon, 2 Oct 2023 09:29:50 +1030 Subject: [PATCH] lightningd/channel.h: rename channel_unsaved to the more explicit channel_state_uncommitted. Signed-off-by: Rusty Russell --- lightningd/channel.c | 3 ++- lightningd/channel.h | 26 +++++++++++++++++++++----- lightningd/channel_control.c | 2 +- lightningd/closing_control.c | 2 +- lightningd/dual_open_control.c | 12 ++++++------ lightningd/peer_control.c | 12 ++++++------ wallet/walletrpc.c | 2 +- 7 files changed, 38 insertions(+), 21 deletions(-) diff --git a/lightningd/channel.c b/lightningd/channel.c index e072a7b4a..a048c3158 100644 --- a/lightningd/channel.c +++ b/lightningd/channel.c @@ -968,7 +968,8 @@ void channel_internal_error(struct channel *channel, const char *fmt, ...) channel_cleanup_commands(channel, why); - if (channel_unsaved(channel)) { + /* Nothing ventured, nothing lost! */ + if (channel_state_uncommitted(channel)) { channel_set_owner(channel, NULL); delete_channel(channel); tal_free(why); diff --git a/lightningd/channel.h b/lightningd/channel.h index 7d2bd6edd..bcda5a547 100644 --- a/lightningd/channel.h +++ b/lightningd/channel.h @@ -551,16 +551,32 @@ static inline bool channel_state_closed(enum channel_state state) abort(); } -static inline bool channel_unsaved(const struct channel *channel) +/* Not even int the database yet? */ +static inline bool channel_state_uncommitted(const struct channel *channel) { - return channel->state == DUALOPEND_OPEN_INIT - && channel->dbid == 0; + switch (channel->state) { + case DUALOPEND_OPEN_INIT: + return channel->dbid == 0; + case CHANNELD_AWAITING_LOCKIN: + case DUALOPEND_AWAITING_LOCKIN: + case CHANNELD_NORMAL: + case CHANNELD_AWAITING_SPLICE: + case CLOSINGD_SIGEXCHANGE: + case CHANNELD_SHUTTING_DOWN: + case CLOSINGD_COMPLETE: + case AWAITING_UNILATERAL: + case FUNDING_SPEND_SEEN: + case ONCHAIN: + case CLOSED: + return false; + } + abort(); } /* Established enough, that we could reach out to peer to discuss */ static inline bool channel_wants_peercomms(const struct channel *channel) { - if (channel_unsaved(channel)) + if (channel_state_uncommitted(channel)) return false; switch (channel->state) { @@ -585,7 +601,7 @@ static inline bool channel_wants_peercomms(const struct channel *channel) /* Established enough, that we have to fail onto chain */ static inline bool channel_wants_onchain_fail(const struct channel *channel) { - if (channel_unsaved(channel)) + if (channel_state_uncommitted(channel)) return false; switch (channel->state) { diff --git a/lightningd/channel_control.c b/lightningd/channel_control.c index bbd8f9549..ab788fe32 100644 --- a/lightningd/channel_control.c +++ b/lightningd/channel_control.c @@ -1585,7 +1585,7 @@ void channel_notify_new_block(struct lightningd *ld, peer; peer = peer_node_id_map_next(ld->peers, &it)) { list_for_each(&peer->channels, channel, list) { - if (channel_unsaved(channel)) + if (channel_state_uncommitted(channel)) continue; if (is_fundee_should_forget(ld, channel, block_height)) { tal_arr_expand(&to_forget, channel); diff --git a/lightningd/closing_control.c b/lightningd/closing_control.c index 2ae178bb6..ae95e807b 100644 --- a/lightningd/closing_control.c +++ b/lightningd/closing_control.c @@ -620,7 +620,7 @@ static struct command_result *param_channel_or_peer(struct command *cmd, if ((*sc)->uc) return NULL; - (*sc)->unsaved_channel = peer_any_channel(peer, channel_unsaved, &more_than_one); + (*sc)->unsaved_channel = peer_any_channel(peer, channel_state_uncommitted, &more_than_one); if ((*sc)->unsaved_channel) { if (more_than_one) goto more_than_one; diff --git a/lightningd/dual_open_control.c b/lightningd/dual_open_control.c index e30f5b7e7..4b74125ad 100644 --- a/lightningd/dual_open_control.c +++ b/lightningd/dual_open_control.c @@ -58,7 +58,7 @@ static void channel_disconnect(struct channel *channel, void channel_unsaved_close_conn(struct channel *channel, const char *why) { /* Gotta be unsaved */ - assert(channel_unsaved(channel)); + assert(channel_state_uncommitted(channel)); log_info(channel->log, "Unsaved peer failed." " Disconnecting and deleting channel. Reason: %s", why); @@ -77,7 +77,7 @@ static void channel_saved_err_broken_reconn(struct channel *channel, const char *errmsg; /* We only reconnect to 'saved' channel peers */ - assert(!channel_unsaved(channel)); + assert(!channel_state_uncommitted(channel)); va_start(ap, fmt); errmsg = tal_vfmt(tmpctx, fmt, ap); @@ -97,7 +97,7 @@ static void channel_err_broken(struct channel *channel, errmsg = tal_vfmt(tmpctx, fmt, ap); va_end(ap); - if (channel_unsaved(channel)) { + if (channel_state_uncommitted(channel)) { log_broken(channel->log, "%s", errmsg); channel_unsaved_close_conn(channel, errmsg); } else @@ -1374,7 +1374,7 @@ static void handle_peer_wants_to_close(struct subd *dualopend, OPT_ANCHORS_ZERO_FEE_HTLC_TX); /* We shouldn't get this message while we're waiting to finish */ - if (channel_unsaved(channel)) { + if (channel_state_uncommitted(channel)) { log_broken(dualopend->ld->log, "Channel in wrong state for" " shutdown, still has uncommitted" " channel pending."); @@ -3604,7 +3604,7 @@ static void dualopen_errmsg(struct channel *channel, /* Clean up any in-progress open attempts */ channel_cleanup_commands(channel, desc); - if (channel_unsaved(channel)) { + if (channel_state_uncommitted(channel)) { log_info(channel->log, "%s", "Unsaved peer failed." " Deleting channel."); delete_channel(channel); @@ -3769,7 +3769,7 @@ bool peer_restart_dualopend(struct peer *peer, u32 *local_shutdown_script_wallet_index; u8 *msg; - if (channel_unsaved(channel)) + if (channel_state_uncommitted(channel)) return peer_start_dualopend(peer, peer_fd, channel); hsmfd = hsm_get_client_fd(peer->ld, &peer->id, channel->dbid, diff --git a/lightningd/peer_control.c b/lightningd/peer_control.c index 371f76b88..c266555b3 100644 --- a/lightningd/peer_control.c +++ b/lightningd/peer_control.c @@ -179,7 +179,7 @@ static void peer_channels_cleanup(struct lightningd *ld, if (channel_wants_peercomms(c)) { channel_cleanup_commands(c, "Disconnected"); channel_fail_transient(c, true, "Disconnected"); - } else if (channel_unsaved(c)) { + } else if (channel_state_uncommitted(c)) { channel_unsaved_close_conn(c, "Disconnected"); } } @@ -397,7 +397,7 @@ void channel_errmsg(struct channel *channel, /* Clean up any in-progress open attempts */ channel_cleanup_commands(channel, desc); - if (channel_unsaved(channel)) { + if (channel_state_uncommitted(channel)) { log_info(channel->log, "%s", "Unsaved peer failed." " Deleting channel."); delete_channel(channel); @@ -2162,7 +2162,7 @@ static void json_add_peer(struct lightningd *ld, json_add_uncommitted_channel(response, p->uncommitted_channel, NULL); list_for_each(&p->channels, channel, list) { - if (channel_unsaved(channel)) + if (channel_state_uncommitted(channel)) json_add_unsaved_channel(response, channel, NULL); else json_add_channel(ld, response, NULL, channel, NULL); @@ -2281,7 +2281,7 @@ static void json_add_peerchannels(struct lightningd *ld, json_add_uncommitted_channel(response, peer->uncommitted_channel, peer); list_for_each(&peer->channels, channel, list) { - if (channel_unsaved(channel)) + if (channel_state_uncommitted(channel)) json_add_unsaved_channel(response, channel, peer); else json_add_channel(ld, response, NULL, channel, peer); @@ -2379,7 +2379,7 @@ static void setup_peer(struct peer *peer, u32 delay) bool connect = false; list_for_each(&peer->channels, channel, list) { - if (channel_unsaved(channel)) + if (channel_state_uncommitted(channel)) continue; /* Watching lockin may be unnecessary, but it's harmless. */ channel_watch_funding(ld, channel); @@ -3300,7 +3300,7 @@ static struct command_result *json_dev_forget_channel(struct command *cmd, "or `dev-fail` instead."); } - if (!channel_unsaved(forget->channel)) + if (!channel_state_uncommitted(forget->channel)) bitcoind_getutxout(cmd->ld->topology->bitcoind, &forget->channel->funding, process_dev_forget_channel, forget); diff --git a/wallet/walletrpc.c b/wallet/walletrpc.c index a6d76a812..ef3e9afd5 100644 --- a/wallet/walletrpc.c +++ b/wallet/walletrpc.c @@ -382,7 +382,7 @@ static struct command_result *json_listfunds(struct command *cmd, struct channel *c; list_for_each(&p->channels, c, list) { /* We don't print out uncommitted channels */ - if (channel_unsaved(c)) + if (channel_state_uncommitted(c)) continue; json_object_start(response, NULL); json_add_node_id(response, "peer_id", &p->id);