From a09c0a9fa78299036ea91766727e3ac36eee22ab Mon Sep 17 00:00:00 2001 From: Rusty Russell Date: Fri, 7 Aug 2015 12:45:30 +0930 Subject: [PATCH] funding: take into account HTLC add/remove. Enhance funding_delta() to have an HTLC delta as well as an A->B delta. Signed-off-by: Rusty Russell --- funding.c | 15 +++++++++++---- funding.h | 2 ++ test-cli/gather_updates.c | 2 +- 3 files changed, 14 insertions(+), 5 deletions(-) diff --git a/funding.c b/funding.c index 5ee0fcc18..83216d37a 100644 --- a/funding.c +++ b/funding.c @@ -41,10 +41,12 @@ bool funding_delta(const OpenChannel *oa, const OpenChannel *ob, const OpenAnchor *anchor, int64_t delta_a, + int64_t htlc, struct channel_oneside *a_side, struct channel_oneside *b_side) { uint64_t a, b, a_fee, b_fee; + int64_t delta_b; uint64_t fee; bool got_fees; @@ -57,20 +59,25 @@ bool funding_delta(const OpenChannel *oa, if (is_funder(oa) == is_funder(ob)) return false; + /* B gets whatever A gives. */ + delta_b = -delta_a; + /* A also pays for the htlc (if any). */ + delta_a -= htlc; + /* Transferring more than we have? */ - if (delta_a > 0 && delta_a > b) + if (delta_b < 0 && -delta_b > b) return false; if (delta_a < 0 && -delta_a > a) return false; /* Adjust amounts. */ a += delta_a; - b -= delta_a; + b += delta_b; /* Take off fee from both parties if possible. */ if (is_funder(oa)) got_fees = subtract_fees(&a, &b, &a_fee, &b_fee, - delta_a > 0, fee); + delta_b < 0, fee); else got_fees = subtract_fees(&b, &a, &b_fee, &a_fee, delta_a < 0, fee); @@ -106,7 +113,7 @@ struct channel_state *initial_funding(const tal_t *ctx, invert_cstate(state); /* This checks we only have 1 anchor, and is nice code reuse. */ - if (!funding_delta(a, b, anchor, 0, &state->a, &state->b)) + if (!funding_delta(a, b, anchor, 0, 0, &state->a, &state->b)) return tal_free(state); return state; } diff --git a/funding.h b/funding.h index d2c8c8e14..c669e3199 100644 --- a/funding.h +++ b/funding.h @@ -35,6 +35,7 @@ struct channel_state *initial_funding(const tal_t *ctx, * @b: B's openchannel offer * @anchor: The anchor offer (A or B) * @delta_a: How much A changes (-ve => A pay B, +ve => B pays A) + * @htlc: How much A is putting into a HTLC (-ve if htlc is cancelled) * @a_side: channel a's state to update. * @b_side: channel b's state to update. */ @@ -42,6 +43,7 @@ bool funding_delta(const OpenChannel *a, const OpenChannel *b, const OpenAnchor *anchor, int64_t delta_a, + int64_t htlc, struct channel_oneside *a_side, struct channel_oneside *b_side); diff --git a/test-cli/gather_updates.c b/test-cli/gather_updates.c index 44b3d3894..2a9f337e8 100644 --- a/test-cli/gather_updates.c +++ b/test-cli/gather_updates.c @@ -92,7 +92,7 @@ struct channel_state *gather_updates(const tal_t *ctx, get_rhash(pkt->update->revocation_hash, &old_our_rhash, our_rhash); } - if (!funding_delta(o1, o2, oa, delta, + if (!funding_delta(o1, o2, oa, delta, 0, &cstate->a, &cstate->b)) errx(1, "Impossible funding update %lli %s", (long long)delta, *argv);