From 912ac25270582637b799392c8de5fbb50147350c Mon Sep 17 00:00:00 2001 From: Rusty Russell Date: Sat, 16 Jul 2022 14:19:29 +0930 Subject: [PATCH] lightningd: remove 'connected' flag from channel structure. It's directly a product of "does it have a current owner subdaemon" and "does that subdaemon talk to peers", so create a helper function which just evaluates that instead. Signed-off-by: Rusty Russell --- lightningd/channel.c | 13 +++++++------ lightningd/channel.h | 6 ++---- lightningd/connect_control.c | 7 ++----- lightningd/dual_open_control.c | 3 --- lightningd/opening_control.c | 3 --- wallet/test/run-wallet.c | 1 - wallet/wallet.c | 2 -- wallet/walletrpc.c | 3 +-- 8 files changed, 12 insertions(+), 26 deletions(-) diff --git a/lightningd/channel.c b/lightningd/channel.c index 76b6a34ed..735b9ab68 100644 --- a/lightningd/channel.c +++ b/lightningd/channel.c @@ -22,14 +22,14 @@ void channel_set_owner(struct channel *channel, struct subd *owner) { struct subd *old_owner = channel->owner; + bool was_connected = channel_is_connected(channel); channel->owner = owner; if (old_owner) { subd_release_channel(old_owner, channel); - if (channel->connected) + if (was_connected && !channel_is_connected(channel)) maybe_disconnect_peer(channel->peer->ld, channel->peer); } - channel->connected = (owner && owner->talks_to_peer); } struct htlc_out *channel_has_htlc_out(struct channel *channel) @@ -239,8 +239,6 @@ struct channel *new_unsaved_channel(struct peer *peer, channel->channel_update = NULL; channel->alias[LOCAL] = channel->alias[REMOTE] = NULL; - /* Channel is connected! */ - channel->connected = true; channel->shutdown_scriptpubkey[REMOTE] = NULL; channel->last_was_revoke = false; channel->last_sent_commit = NULL; @@ -376,7 +374,6 @@ struct channel *new_channel(struct peer *peer, u64 dbid, u32 first_blocknum, u32 min_possible_feerate, u32 max_possible_feerate, - bool connected, const struct basepoints *local_basepoints, const struct pubkey *local_funding_pubkey, const struct pubkey *future_per_commitment_point, @@ -485,7 +482,6 @@ struct channel *new_channel(struct peer *peer, u64 dbid, channel->first_blocknum = first_blocknum; channel->min_possible_feerate = min_possible_feerate; channel->max_possible_feerate = max_possible_feerate; - channel->connected = connected; channel->local_basepoints = *local_basepoints; channel->local_funding_pubkey = *local_funding_pubkey; channel->future_per_commitment_point @@ -980,6 +976,11 @@ void channel_fail_reconnect(struct channel *channel, const char *fmt, ...) va_end(ap); } +bool channel_is_connected(const struct channel *channel) +{ + return channel->owner && channel->owner->talks_to_peer; +} + const struct short_channel_id * channel_scid_or_local_alias(const struct channel *chan) { diff --git a/lightningd/channel.h b/lightningd/channel.h index 8851bd72f..c872bd1cc 100644 --- a/lightningd/channel.h +++ b/lightningd/channel.h @@ -205,9 +205,6 @@ struct channel { /* Feerate range */ u32 min_possible_feerate, max_possible_feerate; - /* Does gossipd need to know if the owner dies? (ie. not onchaind) */ - bool connected; - /* Do we have an "impossible" future per_commitment_point from * peer via option_data_loss_protect? */ const struct pubkey *future_per_commitment_point; @@ -267,6 +264,8 @@ struct channel { struct scb_chan *scb; }; +bool channel_is_connected(const struct channel *channel); + /* For v2 opens, a channel that has not yet been committed/saved to disk */ struct channel *new_unsaved_channel(struct peer *peer, u32 feerate_base, @@ -317,7 +316,6 @@ struct channel *new_channel(struct peer *peer, u64 dbid, u32 first_blocknum, u32 min_possible_feerate, u32 max_possible_feerate, - bool connected, const struct basepoints *local_basepoints, const struct pubkey *local_funding_pubkey, const struct pubkey *future_per_commitment_point, diff --git a/lightningd/connect_control.c b/lightningd/connect_control.c index 3ef116a76..7f0965afb 100644 --- a/lightningd/connect_control.c +++ b/lightningd/connect_control.c @@ -677,12 +677,9 @@ void maybe_disconnect_peer(struct lightningd *ld, struct peer *peer) if (peer->uncommitted_channel) return; - list_for_each(&peer->channels, channel, list) { - if (!channel->owner) - continue; - if (channel->owner->talks_to_peer) + list_for_each(&peer->channels, channel, list) + if (channel_is_connected(channel)) return; - } /* If shutting down, connectd no longer exists */ if (!ld->connectd) { diff --git a/lightningd/dual_open_control.c b/lightningd/dual_open_control.c index d3b9a0c5d..fd501641a 100644 --- a/lightningd/dual_open_control.c +++ b/lightningd/dual_open_control.c @@ -1234,9 +1234,6 @@ wallet_commit_channel(struct lightningd *ld, channel->scb->funding_sats = total_funding; channel->scb->type = channel_type_dup(channel->scb, channel->type); - /* We are connected */ - channel->connected = true; - if (our_upfront_shutdown_script) channel->shutdown_scriptpubkey[LOCAL] = tal_steal(channel, our_upfront_shutdown_script); diff --git a/lightningd/opening_control.c b/lightningd/opening_control.c index 987f82a1c..8438155c5 100644 --- a/lightningd/opening_control.c +++ b/lightningd/opening_control.c @@ -202,8 +202,6 @@ wallet_commit_channel(struct lightningd *ld, * in theory, but it's only used for timing out. */ get_network_blockheight(ld->topology), feerate, feerate, - /* We are connected */ - true, &uc->local_basepoints, &uc->local_funding_pubkey, NULL, @@ -1363,7 +1361,6 @@ static struct channel *stub_chan(struct command *cmd, get_network_blockheight(ld->topology), FEERATE_FLOOR, funding_sats.satoshis / MINIMUM_TX_WEIGHT * 1000 /* Raw: convert to feerate */, - false, &basepoints, &localFundingPubkey, NULL, diff --git a/wallet/test/run-wallet.c b/wallet/test/run-wallet.c index 94cb6a29e..c16ffc6df 100644 --- a/wallet/test/run-wallet.c +++ b/wallet/test/run-wallet.c @@ -1608,7 +1608,6 @@ static bool test_channel_inflight_crud(struct lightningd *ld, const tal_t *ctx) 100, /* first_blocknum */ 100, /* min_possible_feerate */ 10000, /* max_possible_feerate */ - false, &basepoints, &pk, NULL, 1000, 100, diff --git a/wallet/wallet.c b/wallet/wallet.c index 6f6c6b699..b9d670ad2 100644 --- a/wallet/wallet.c +++ b/wallet/wallet.c @@ -1500,8 +1500,6 @@ static struct channel *wallet_stmt2channel(struct wallet *w, struct db_stmt *stm db_col_u64(stmt, "first_blocknum"), db_col_int(stmt, "min_possible_feerate"), db_col_int(stmt, "max_possible_feerate"), - /* Not connected */ - false, &local_basepoints, &local_funding_pubkey, future_per_commitment_point, db_col_int(stmt, "feerate_base"), diff --git a/wallet/walletrpc.c b/wallet/walletrpc.c index e7be2d410..809bd8ef4 100644 --- a/wallet/walletrpc.c +++ b/wallet/walletrpc.c @@ -342,9 +342,8 @@ static struct command_result *json_listfunds(struct command *cmd, continue; json_object_start(response, NULL); json_add_node_id(response, "peer_id", &p->id); - /* Mirrors logic in listpeers */ json_add_bool(response, "connected", - channel_active(c) && c->connected); + channel_is_connected(c)); json_add_string(response, "state", channel_state_name(c)); if (c->scid)