diff --git a/daemon/packets.c b/daemon/packets.c index 447c9d5af..142fc406d 100644 --- a/daemon/packets.c +++ b/daemon/packets.c @@ -448,8 +448,7 @@ Pkt *accept_pkt_htlc_add(const tal_t *ctx, } cur->cstate = copy_funding(cur, peer->cstate); - if (!funding_delta(peer->them.offer_anchor == CMD_OPEN_WITH_ANCHOR, - peer->anchor.satoshis, + if (!funding_delta(peer->anchor.satoshis, 0, cur->htlc->msatoshis, &cur->cstate->b, &cur->cstate->a)) { err = pkt_err(ctx, "Cannot afford %"PRIu64" milli-satoshis", @@ -510,8 +509,7 @@ Pkt *accept_pkt_htlc_fail(const tal_t *ctx, struct peer *peer, const Pkt *pkt) /* Removing it should not fail: we regain HTLC amount */ cur->cstate = copy_funding(cur, peer->cstate); - if (!funding_delta(peer->us.offer_anchor == CMD_OPEN_WITH_ANCHOR, - peer->anchor.satoshis, + if (!funding_delta(peer->anchor.satoshis, 0, -cur->htlc->msatoshis, &cur->cstate->a, &cur->cstate->b)) { fatal("Unexpected failure fulfilling HTLC of %"PRIu64 @@ -560,8 +558,7 @@ Pkt *accept_pkt_htlc_fulfill(const tal_t *ctx, /* Removing it should not fail: they gain HTLC amount */ cur->cstate = copy_funding(cur, peer->cstate); - if (!funding_delta(peer->us.offer_anchor == CMD_OPEN_WITH_ANCHOR, - peer->anchor.satoshis, + if (!funding_delta(peer->anchor.satoshis, -cur->htlc->msatoshis, -cur->htlc->msatoshis, &cur->cstate->a, &cur->cstate->b)) { diff --git a/daemon/peer.c b/daemon/peer.c index 94019aa0f..1dc464b5f 100644 --- a/daemon/peer.c +++ b/daemon/peer.c @@ -834,9 +834,7 @@ bool peer_create_close_tx(struct peer *peer, u64 fee_satoshis) /* We don't need a deep copy here, just fee levels. */ cstate = *peer->cstate; - if (!adjust_fee(peer->us.offer_anchor == CMD_OPEN_WITH_ANCHOR, - peer->anchor.satoshis, - fee_satoshis, + if (!adjust_fee(peer->anchor.satoshis, fee_satoshis, &cstate.a, &cstate.b)) return false; @@ -1322,9 +1320,7 @@ static void check_htlc_expiry(struct peer *peer, void *unused) cstate = copy_funding(peer, peer->cstate); /* This should never fail! */ - if (!funding_delta(peer->them.offer_anchor - == CMD_OPEN_WITH_ANCHOR, - peer->anchor.satoshis, + if (!funding_delta(peer->anchor.satoshis, 0, -htlc->msatoshis, &cstate->b, &cstate->a)) { @@ -1371,8 +1367,7 @@ static void do_newhtlc(struct peer *peer, struct newhtlc *newhtlc) /* Can we even offer this much? We check now, just before we * execute. */ cstate = copy_funding(newhtlc, peer->cstate); - if (!funding_delta(peer->us.offer_anchor == CMD_OPEN_WITH_ANCHOR, - peer->anchor.satoshis, + if (!funding_delta(peer->anchor.satoshis, 0, newhtlc->htlc->msatoshis, &cstate->a, &cstate->b)) { command_fail(newhtlc->jsoncmd, @@ -1508,8 +1503,7 @@ static void do_fullfill(struct peer *peer, cstate = copy_funding(fulfillhtlc, peer->cstate); /* This should never fail! */ - if (!funding_delta(peer->them.offer_anchor == CMD_OPEN_WITH_ANCHOR, - peer->anchor.satoshis, + if (!funding_delta(peer->anchor.satoshis, -htlc->msatoshis, -htlc->msatoshis, &cstate->b, &cstate->a)) { @@ -1599,8 +1593,7 @@ static void do_failhtlc(struct peer *peer, cstate = copy_funding(failhtlc, peer->cstate); /* This should never fail! */ - if (!funding_delta(peer->them.offer_anchor == CMD_OPEN_WITH_ANCHOR, - peer->anchor.satoshis, + if (!funding_delta(peer->anchor.satoshis, 0, -htlc->msatoshis, &cstate->b, &cstate->a)) { diff --git a/funding.c b/funding.c index 927bb9fd6..0c1b474e7 100644 --- a/funding.c +++ b/funding.c @@ -45,8 +45,7 @@ static uint64_t htlcs_total(const struct channel_htlc *htlcs) return total; } -static bool change_funding(bool a_is_funder, - uint64_t anchor_satoshis, +static bool change_funding(uint64_t anchor_satoshis, int64_t delta_a_msat, int64_t htlc_msat, uint64_t a, uint64_t b, uint64_t fee, @@ -59,6 +58,7 @@ static bool change_funding(bool a_is_funder, assert(a + b + htlcs_total(a_side->htlcs) + htlcs_total(b_side->htlcs) == anchor_satoshis * 1000); + assert(a_side->offered_anchor != b_side->offered_anchor); /* B gets whatever A gives. */ delta_b_msat = -delta_a_msat; @@ -76,7 +76,7 @@ static bool change_funding(bool a_is_funder, b += delta_b_msat; /* Take off fee from both parties if possible. */ - if (a_is_funder) + if (a_side->offered_anchor) got_fees = subtract_fees(&a, &b, &a_fee, &b_fee, delta_b_msat < 0, fee); else @@ -94,8 +94,7 @@ static bool change_funding(bool a_is_funder, return true; } -bool funding_delta(bool a_is_funder, - uint64_t anchor_satoshis, +bool funding_delta(uint64_t anchor_satoshis, int64_t delta_a_msat, int64_t htlc_msat, struct channel_oneside *a_side, @@ -109,7 +108,7 @@ bool funding_delta(bool a_is_funder, b = b_side->pay_msat + b_side->fee_msat; fee = a_side->fee_msat + b_side->fee_msat; - return change_funding(a_is_funder, anchor_satoshis, + return change_funding(anchor_satoshis, delta_a_msat, htlc_msat, a, b, fee, a_side, b_side); @@ -134,19 +133,19 @@ struct channel_state *initial_funding(const tal_t *ctx, /* Initially, all goes back to funder. */ state->a.pay_msat = anchor_satoshis * 1000 - fee * 1000; state->a.fee_msat = fee * 1000; + state->a.offered_anchor = true; + state->b.offered_anchor = false; /* If B (not A) is funder, invert. */ if (!am_funder) invert_cstate(state); /* Make sure it checks out. */ - assert(funding_delta(am_funder, anchor_satoshis, 0, 0, - &state->a, &state->b)); + assert(funding_delta(anchor_satoshis, 0, 0, &state->a, &state->b)); return state; } -bool adjust_fee(bool a_is_funder, - uint64_t anchor_satoshis, +bool adjust_fee(uint64_t anchor_satoshis, uint64_t fee_satoshis, struct channel_oneside *a_side, struct channel_oneside *b_side) @@ -157,7 +156,7 @@ bool adjust_fee(bool a_is_funder, b = b_side->pay_msat + b_side->fee_msat; /* No HTLC or delta, just fee recalculate. */ - return change_funding(a_is_funder, anchor_satoshis, + return change_funding(anchor_satoshis, 0, 0, a, b, fee_satoshis * 1000, a_side, b_side); } diff --git a/funding.h b/funding.h index 9e5a78971..66dac8f6d 100644 --- a/funding.h +++ b/funding.h @@ -15,6 +15,8 @@ struct channel_htlc { struct channel_oneside { /* Payment and fee is in millisatoshi. */ uint32_t pay_msat, fee_msat; + /* Did we offer the anchor? */ + bool offered_anchor; /* Use tal_count to get the number */ struct channel_htlc *htlcs; }; @@ -47,15 +49,13 @@ struct channel_state *copy_funding(const tal_t *ctx, /** * funding_delta: With this change, what's the new state? - * @a_is_funder: is A paying for the anchor? * @anchor_satoshis: The anchor amount. * @delta_a: How many millisatoshi A changes (-ve => A pay B, +ve => B pays A) * @htlc: Millisatoshi 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. */ -bool funding_delta(bool a_is_funder, - uint64_t anchor_satoshis, +bool funding_delta(uint64_t anchor_satoshis, int64_t delta_a_msat, int64_t htlc_msat, struct channel_oneside *a_side, @@ -63,14 +63,12 @@ bool funding_delta(bool a_is_funder, /** * adjust_fee: Change fee. - * @a_is_funder: is A paying for the anchor? * @anchor_satoshis: The anchor amount. * @fee_satoshis: The new fee amount. * @a_side: channel a's state to update. * @b_side: channel b's state to update. */ -bool adjust_fee(bool a_is_funder, - uint64_t anchor_satoshis, +bool adjust_fee(uint64_t anchor_satoshis, uint64_t fee_satoshis, struct channel_oneside *a_side, struct channel_oneside *b_side);