Browse Source

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 <decker.christian@gmail.com>
ppa-0.6.1
Christian Decker 7 years ago
committed by Rusty Russell
parent
commit
aef5780f36
  1. 43
      lightningd/channel.c
  2. 4
      lightningd/channel.h

43
lightningd/channel.c

@ -19,34 +19,53 @@ void channel_set_owner(struct channel *channel, struct subd *owner)
subd_release_channel(old_owner, channel); 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_map_iter outi;
struct htlc_out *hout; struct htlc_out *hout;
struct htlc_in_map_iter ini;
struct htlc_in *hin;
struct lightningd *ld = channel->peer->ld; struct lightningd *ld = channel->peer->ld;
for (hout = htlc_out_map_first(&ld->htlcs_out, &outi); for (hout = htlc_out_map_first(&ld->htlcs_out, &outi);
hout; hout;
hout = htlc_out_map_next(&ld->htlcs_out, &outi)) { hout = htlc_out_map_next(&ld->htlcs_out, &outi)) {
if (hout->key.channel != channel) if (hout->key.channel == channel)
continue; return hout;
fatal("Freeing channel %s has hout %s",
channel_state_name(channel),
htlc_state_name(hout->hstate));
} }
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); for (hin = htlc_in_map_first(&ld->htlcs_in, &ini);
hin; hin;
hin = htlc_in_map_next(&ld->htlcs_in, &ini)) { hin = htlc_in_map_next(&ld->htlcs_in, &ini)) {
if (hin->key.channel != channel) if (hin->key.channel == channel)
continue; 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", fatal("Freeing channel %s has hin %s",
channel_state_name(channel), channel_state_name(channel),
htlc_state_name(hin->hstate)); htlc_state_name(hin->hstate));
}
/* Free any old owner still hanging around. */ /* Free any old owner still hanging around. */
channel_set_owner(channel, NULL); channel_set_owner(channel, NULL);

4
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, void channel_set_billboard(struct channel *channel, bool perm,
const char *str TAKES); 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 */ #endif /* LIGHTNING_LIGHTNINGD_CHANNEL_H */

Loading…
Cancel
Save