From d5c30ee30bab5535ca6a0a1a50f1646679e8ad1f Mon Sep 17 00:00:00 2001 From: Rusty Russell Date: Fri, 22 Jan 2016 06:41:47 +1030 Subject: [PATCH] funding: funding_find_htlc and funding_remove_htlc. Helpers for daemon. Signed-off-by: Rusty Russell --- funding.c | 22 ++++++++++++++++++++++ funding.h | 18 ++++++++++++++++++ 2 files changed, 40 insertions(+) diff --git a/funding.c b/funding.c index 04715390d..b25ae25e3 100644 --- a/funding.c +++ b/funding.c @@ -1,5 +1,6 @@ #include "funding.h" #include +#include #include static bool subtract_fees(uint64_t *funder, uint64_t *non_funder, @@ -154,6 +155,27 @@ void funding_add_htlc(struct channel_oneside *creator, creator->htlcs[n].rhash = *rhash; } +size_t funding_find_htlc(struct channel_oneside *creator, + const struct sha256 *rhash) +{ + size_t i; + + for (i = 0; i < tal_count(creator->htlcs); i++) { + if (structeq(&creator->htlcs[i].rhash, rhash)) + break; + } + return i; +} + +void funding_remove_htlc(struct channel_oneside *creator, size_t i) +{ + size_t n = tal_count(creator->htlcs); + assert(i < n); + memmove(creator->htlcs + i, creator->htlcs + i + 1, + (n - i - 1) * sizeof(*creator->htlcs)); + tal_resize(&creator->htlcs, n-1); +} + struct channel_state *copy_funding(const tal_t *ctx, const struct channel_state *cstate) { diff --git a/funding.h b/funding.h index b7c28ac60..616257dc7 100644 --- a/funding.h +++ b/funding.h @@ -85,4 +85,22 @@ void funding_add_htlc(struct channel_oneside *creator, u32 msatoshis, const struct abs_locktime *expiry, const struct sha256 *rhash); +/** + * funding_find_htlc: find an HTLC on this side of the channel. + * @creator: channel_state->a or channel_state->b, whichever originated htlc + * @rhash: hash of redeem secret + * + * Returns a number < tal_count(creator->htlcs), or == tal_count(creator->htlcs) + * on fail. + */ +size_t funding_find_htlc(struct channel_oneside *creator, + const struct sha256 *rhash); + +/** + * funding_remove_htlc: remove an HTLC from this side of the channel. + * @creator: channel_state->a or channel_state->b, whichever originated htlc + * @i: index returned from funding_find_htlc. + */ +void funding_remove_htlc(struct channel_oneside *creator, size_t i); + #endif /* LIGHTNING_FUNDING_H */