mirror of
https://github.com/aljazceru/lightning.git
synced 2026-01-05 23:24:21 +01:00
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>
This commit is contained in:
15
funding.c
15
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;
|
||||
}
|
||||
|
||||
@@ -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);
|
||||
|
||||
|
||||
@@ -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);
|
||||
|
||||
Reference in New Issue
Block a user