Browse Source

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 <rusty@rustcorp.com.au>
ppa-0.6.1
Rusty Russell 10 years ago
parent
commit
a09c0a9fa7
  1. 15
      funding.c
  2. 2
      funding.h
  3. 2
      test-cli/gather_updates.c

15
funding.c

@ -41,10 +41,12 @@ bool funding_delta(const OpenChannel *oa,
const OpenChannel *ob, const OpenChannel *ob,
const OpenAnchor *anchor, const OpenAnchor *anchor,
int64_t delta_a, int64_t delta_a,
int64_t htlc,
struct channel_oneside *a_side, struct channel_oneside *a_side,
struct channel_oneside *b_side) struct channel_oneside *b_side)
{ {
uint64_t a, b, a_fee, b_fee; uint64_t a, b, a_fee, b_fee;
int64_t delta_b;
uint64_t fee; uint64_t fee;
bool got_fees; bool got_fees;
@ -57,20 +59,25 @@ bool funding_delta(const OpenChannel *oa,
if (is_funder(oa) == is_funder(ob)) if (is_funder(oa) == is_funder(ob))
return false; 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? */ /* Transferring more than we have? */
if (delta_a > 0 && delta_a > b) if (delta_b < 0 && -delta_b > b)
return false; return false;
if (delta_a < 0 && -delta_a > a) if (delta_a < 0 && -delta_a > a)
return false; return false;
/* Adjust amounts. */ /* Adjust amounts. */
a += delta_a; a += delta_a;
b -= delta_a; b += delta_b;
/* Take off fee from both parties if possible. */ /* Take off fee from both parties if possible. */
if (is_funder(oa)) if (is_funder(oa))
got_fees = subtract_fees(&a, &b, &a_fee, &b_fee, got_fees = subtract_fees(&a, &b, &a_fee, &b_fee,
delta_a > 0, fee); delta_b < 0, fee);
else else
got_fees = subtract_fees(&b, &a, &b_fee, &a_fee, got_fees = subtract_fees(&b, &a, &b_fee, &a_fee,
delta_a < 0, fee); delta_a < 0, fee);
@ -106,7 +113,7 @@ struct channel_state *initial_funding(const tal_t *ctx,
invert_cstate(state); invert_cstate(state);
/* This checks we only have 1 anchor, and is nice code reuse. */ /* 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 tal_free(state);
return state; return state;
} }

2
funding.h

@ -35,6 +35,7 @@ struct channel_state *initial_funding(const tal_t *ctx,
* @b: B's openchannel offer * @b: B's openchannel offer
* @anchor: The anchor offer (A or B) * @anchor: The anchor offer (A or B)
* @delta_a: How much A changes (-ve => A pay B, +ve => B pays A) * @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. * @a_side: channel a's state to update.
* @b_side: channel b'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 OpenChannel *b,
const OpenAnchor *anchor, const OpenAnchor *anchor,
int64_t delta_a, int64_t delta_a,
int64_t htlc,
struct channel_oneside *a_side, struct channel_oneside *a_side,
struct channel_oneside *b_side); struct channel_oneside *b_side);

2
test-cli/gather_updates.c

@ -92,7 +92,7 @@ struct channel_state *gather_updates(const tal_t *ctx,
get_rhash(pkt->update->revocation_hash, get_rhash(pkt->update->revocation_hash,
&old_our_rhash, our_rhash); &old_our_rhash, our_rhash);
} }
if (!funding_delta(o1, o2, oa, delta, if (!funding_delta(o1, o2, oa, delta, 0,
&cstate->a, &cstate->b)) &cstate->a, &cstate->b))
errx(1, "Impossible funding update %lli %s", errx(1, "Impossible funding update %lli %s",
(long long)delta, *argv); (long long)delta, *argv);

Loading…
Cancel
Save