From aef5780f36f8c36613e685068085a279df75f3f1 Mon Sep 17 00:00:00 2001 From: Christian Decker Date: Wed, 28 Feb 2018 23:23:45 +0100 Subject: [PATCH] htlc: Extracted htlc detection from the channel destructor Let's have a simple function that allows us to check whether a channel still has an HTLC open. Signed-off-by: Christian Decker --- lightningd/channel.c | 43 +++++++++++++++++++++++++++++++------------ lightningd/channel.h | 4 ++++ 2 files changed, 35 insertions(+), 12 deletions(-) diff --git a/lightningd/channel.c b/lightningd/channel.c index 2b19df278..b89b63804 100644 --- a/lightningd/channel.c +++ b/lightningd/channel.c @@ -19,34 +19,53 @@ void channel_set_owner(struct channel *channel, struct subd *owner) subd_release_channel(old_owner, channel); } -static void destroy_channel(struct channel *channel) +struct htlc_out *channel_has_htlc_out(struct channel *channel) { - /* Must not have any HTLCs! */ struct htlc_out_map_iter outi; struct htlc_out *hout; - struct htlc_in_map_iter ini; - struct htlc_in *hin; struct lightningd *ld = channel->peer->ld; for (hout = htlc_out_map_first(&ld->htlcs_out, &outi); hout; hout = htlc_out_map_next(&ld->htlcs_out, &outi)) { - if (hout->key.channel != channel) - continue; - fatal("Freeing channel %s has hout %s", - channel_state_name(channel), - htlc_state_name(hout->hstate)); + if (hout->key.channel == channel) + return hout; } + return NULL; +} + +struct htlc_in *channel_has_htlc_in(struct channel *channel) +{ + struct htlc_in_map_iter ini; + struct htlc_in *hin; + struct lightningd *ld = channel->peer->ld; + for (hin = htlc_in_map_first(&ld->htlcs_in, &ini); hin; hin = htlc_in_map_next(&ld->htlcs_in, &ini)) { - if (hin->key.channel != channel) - continue; + if (hin->key.channel == channel) + return hin; + } + + return NULL; +} + +static void destroy_channel(struct channel *channel) +{ + /* Must not have any HTLCs! */ + struct htlc_out *hout = channel_has_htlc_out(channel); + struct htlc_in *hin = channel_has_htlc_in(channel); + + if (hout) + fatal("Freeing channel %s has hout %s", + channel_state_name(channel), + htlc_state_name(hout->hstate)); + + if (hin) fatal("Freeing channel %s has hin %s", channel_state_name(channel), htlc_state_name(hin->hstate)); - } /* Free any old owner still hanging around. */ channel_set_owner(channel, NULL); diff --git a/lightningd/channel.h b/lightningd/channel.h index f4bb0d0bf..01ff66f4e 100644 --- a/lightningd/channel.h +++ b/lightningd/channel.h @@ -199,4 +199,8 @@ void derive_channel_seed(struct lightningd *ld, struct privkey *seed, void channel_set_billboard(struct channel *channel, bool perm, const char *str TAKES); + +struct htlc_in *channel_has_htlc_in(struct channel *channel); +struct htlc_out *channel_has_htlc_out(struct channel *channel); + #endif /* LIGHTNING_LIGHTNINGD_CHANNEL_H */