lightningd: channels own the peer.

Channels are within the peer structure, but the peer is freed only
when the last channel is freed.

We also implement channel_set_owner() and make peer_set_owner() a temporary
wrapper.

Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
This commit is contained in:
Rusty Russell
2018-02-12 20:43:04 +10:30
committed by Christian Decker
parent 32411de90e
commit 8c084d57ff
4 changed files with 53 additions and 28 deletions

View File

@@ -9,9 +9,37 @@
#include <lightningd/peer_control.h>
#include <lightningd/subd.h>
void channel_set_owner(struct channel *channel, struct subd *owner)
{
struct subd *old_owner = channel->owner;
channel->owner = owner;
if (old_owner)
subd_release_peer(old_owner, channel2peer(channel));
}
static void destroy_channel(struct channel *channel)
{
/* Free any old owner still hanging around. */
channel_set_owner(channel, NULL);
list_del_from(&channel->peer->channels, &channel->list);
/* Last one out frees the peer */
if (list_empty(&channel->peer->channels))
tal_free(channel->peer);
}
/* This lets us give a more detailed error than just a destructor. */
void free_channel(struct channel *channel, const char *why)
{
if (channel->opening_cmd) {
command_fail(channel->opening_cmd, "%s", why);
channel->opening_cmd = NULL;
}
wallet_channel_delete(channel->peer->ld->wallet, channel->dbid,
channel->peer->dbid);
tal_free(channel);
}
/* FIXME: We have no business knowing this! */