lightningd: add explicit "connected" flag.

We currently intuit this by whether there's a subdaemon owning it.
But we're about to change the rules and allow connectd to hold idle
connections, so we need an explicit flag.

Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
This commit is contained in:
Rusty Russell 2022-03-22 19:19:13 +10:30
parent 915a591873
commit b99c04e605
5 changed files with 19 additions and 26 deletions

View file

@ -38,6 +38,8 @@ void channel_set_owner(struct channel *channel, struct subd *owner)
* Only transfer to connectd if connectd is
* there to be transferred to.
*/
assert(channel->peer->connected);
channel->peer->connected = false;
if (channel->peer->ld->connectd) {
u8 *msg;
msg = towire_connectd_peer_disconnected(

View file

@ -139,20 +139,13 @@ static struct command_result *json_connect(struct command *cmd,
/* If we know about peer, see if it's already connected. */
peer = peer_by_id(cmd->ld, &id);
if (peer) {
struct channel *channel = peer_active_channel(peer);
if (!channel)
channel = peer_unsaved_channel(peer);
if (peer->uncommitted_channel
|| (channel && channel->connected)) {
log_debug(cmd->ld->log, "Already connected via %s",
type_to_string(tmpctx, struct wireaddr_internal, &peer->addr));
return connect_cmd_succeed(cmd, peer,
peer->connected_incoming,
&peer->addr);
}
if (peer && peer->connected) {
log_debug(cmd->ld->log, "Already connected via %s",
type_to_string(tmpctx, struct wireaddr_internal,
&peer->addr));
return connect_cmd_succeed(cmd, peer,
peer->connected_incoming,
&peer->addr);
}
/* Was there parseable host name? */

View file

@ -193,6 +193,8 @@ void handle_reestablish(struct lightningd *ld,
"Unknown channel for reestablish");
log_debug(ld->log, "Reestablish on UNKNOWN channel %s",
type_to_string(tmpctx, struct channel_id, channel_id));
if (peer)
peer->connected = false;
/* Unless we're shutting down */
if (ld->connectd)
subd_send_msg(ld->connectd,

View file

@ -100,6 +100,7 @@ struct peer *new_peer(struct lightningd *ld, u64 dbid,
peer->their_features = NULL;
list_head_init(&peer->channels);
peer->direction = node_id_idx(&peer->ld->id, &peer->id);
peer->connected = false;
#if DEVELOPER
peer->ignore_htlcs = false;
#endif
@ -1067,6 +1068,7 @@ static void peer_connected_hook_final(struct peer_connected_hook_payload *payloa
send_error:
log_debug(ld->log, "Telling connectd to send error %s",
tal_hex(tmpctx, error));
peer->connected = false;
/* Get connectd to send error and close. */
subd_send_msg(ld->connectd,
take(towire_connectd_peer_final_msg(NULL, &peer->id,
@ -1213,6 +1215,7 @@ void peer_connected(struct lightningd *ld, const u8 *msg, int peer_fd)
if (!peer)
peer = new_peer(ld, 0, &id, &hook_payload->addr,
hook_payload->incoming);
peer->connected = true;
tal_steal(peer, hook_payload);
hook_payload->peer = peer;
@ -1487,27 +1490,17 @@ static void json_add_peer(struct lightningd *ld,
struct peer *p,
const enum log_level *ll)
{
bool connected;
struct channel *channel;
json_object_start(response, NULL);
json_add_node_id(response, "id", &p->id);
/* Channel is also connected if uncommitted channel */
if (p->uncommitted_channel)
connected = true;
else {
channel = peer_active_channel(p);
if (!channel)
channel = peer_unsaved_channel(p);
connected = channel && channel->connected;
}
json_add_bool(response, "connected", connected);
json_add_bool(response, "connected", p->connected);
/* If it's not connected, features are unreliable: we don't
* store them in the database, and they would only reflect
* their features *last* time they connected. */
if (connected) {
if (p->connected) {
json_array_start(response, "netaddr");
json_add_string(response, NULL,
type_to_string(tmpctx,

View file

@ -30,6 +30,9 @@ struct peer {
/* Our channels */
struct list_head channels;
/* Are we connected? */
bool connected;
/* Our (only) uncommitted channel, still opening. */
struct uncommitted_channel *uncommitted_channel;