Browse Source

funding: explicitly mark which side offered the anchor.

The channel funding code needs to know who offered the anchor, as they
are responsible for paying fees until the other side is able to.  This
is actually a hack, but at least now it's internal to funding and not
passed in at every funding_delta() call.

Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
ppa-0.6.1
Rusty Russell 9 years ago
parent
commit
85147347e2
  1. 9
      daemon/packets.c
  2. 17
      daemon/peer.c
  3. 21
      funding.c
  4. 10
      funding.h

9
daemon/packets.c

@ -448,8 +448,7 @@ Pkt *accept_pkt_htlc_add(const tal_t *ctx,
} }
cur->cstate = copy_funding(cur, peer->cstate); cur->cstate = copy_funding(cur, peer->cstate);
if (!funding_delta(peer->them.offer_anchor == CMD_OPEN_WITH_ANCHOR, if (!funding_delta(peer->anchor.satoshis,
peer->anchor.satoshis,
0, cur->htlc->msatoshis, 0, cur->htlc->msatoshis,
&cur->cstate->b, &cur->cstate->a)) { &cur->cstate->b, &cur->cstate->a)) {
err = pkt_err(ctx, "Cannot afford %"PRIu64" milli-satoshis", 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 */ /* Removing it should not fail: we regain HTLC amount */
cur->cstate = copy_funding(cur, peer->cstate); cur->cstate = copy_funding(cur, peer->cstate);
if (!funding_delta(peer->us.offer_anchor == CMD_OPEN_WITH_ANCHOR, if (!funding_delta(peer->anchor.satoshis,
peer->anchor.satoshis,
0, -cur->htlc->msatoshis, 0, -cur->htlc->msatoshis,
&cur->cstate->a, &cur->cstate->b)) { &cur->cstate->a, &cur->cstate->b)) {
fatal("Unexpected failure fulfilling HTLC of %"PRIu64 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 */ /* Removing it should not fail: they gain HTLC amount */
cur->cstate = copy_funding(cur, peer->cstate); cur->cstate = copy_funding(cur, peer->cstate);
if (!funding_delta(peer->us.offer_anchor == CMD_OPEN_WITH_ANCHOR, if (!funding_delta(peer->anchor.satoshis,
peer->anchor.satoshis,
-cur->htlc->msatoshis, -cur->htlc->msatoshis,
-cur->htlc->msatoshis, -cur->htlc->msatoshis,
&cur->cstate->a, &cur->cstate->b)) { &cur->cstate->a, &cur->cstate->b)) {

17
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. */ /* We don't need a deep copy here, just fee levels. */
cstate = *peer->cstate; cstate = *peer->cstate;
if (!adjust_fee(peer->us.offer_anchor == CMD_OPEN_WITH_ANCHOR, if (!adjust_fee(peer->anchor.satoshis, fee_satoshis,
peer->anchor.satoshis,
fee_satoshis,
&cstate.a, &cstate.b)) &cstate.a, &cstate.b))
return false; return false;
@ -1322,9 +1320,7 @@ static void check_htlc_expiry(struct peer *peer, void *unused)
cstate = copy_funding(peer, peer->cstate); cstate = copy_funding(peer, peer->cstate);
/* This should never fail! */ /* This should never fail! */
if (!funding_delta(peer->them.offer_anchor if (!funding_delta(peer->anchor.satoshis,
== CMD_OPEN_WITH_ANCHOR,
peer->anchor.satoshis,
0, 0,
-htlc->msatoshis, -htlc->msatoshis,
&cstate->b, &cstate->a)) { &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 /* Can we even offer this much? We check now, just before we
* execute. */ * execute. */
cstate = copy_funding(newhtlc, peer->cstate); cstate = copy_funding(newhtlc, peer->cstate);
if (!funding_delta(peer->us.offer_anchor == CMD_OPEN_WITH_ANCHOR, if (!funding_delta(peer->anchor.satoshis,
peer->anchor.satoshis,
0, newhtlc->htlc->msatoshis, 0, newhtlc->htlc->msatoshis,
&cstate->a, &cstate->b)) { &cstate->a, &cstate->b)) {
command_fail(newhtlc->jsoncmd, command_fail(newhtlc->jsoncmd,
@ -1508,8 +1503,7 @@ static void do_fullfill(struct peer *peer,
cstate = copy_funding(fulfillhtlc, peer->cstate); cstate = copy_funding(fulfillhtlc, peer->cstate);
/* This should never fail! */ /* This should never fail! */
if (!funding_delta(peer->them.offer_anchor == CMD_OPEN_WITH_ANCHOR, if (!funding_delta(peer->anchor.satoshis,
peer->anchor.satoshis,
-htlc->msatoshis, -htlc->msatoshis,
-htlc->msatoshis, -htlc->msatoshis,
&cstate->b, &cstate->a)) { &cstate->b, &cstate->a)) {
@ -1599,8 +1593,7 @@ static void do_failhtlc(struct peer *peer,
cstate = copy_funding(failhtlc, peer->cstate); cstate = copy_funding(failhtlc, peer->cstate);
/* This should never fail! */ /* This should never fail! */
if (!funding_delta(peer->them.offer_anchor == CMD_OPEN_WITH_ANCHOR, if (!funding_delta(peer->anchor.satoshis,
peer->anchor.satoshis,
0, 0,
-htlc->msatoshis, -htlc->msatoshis,
&cstate->b, &cstate->a)) { &cstate->b, &cstate->a)) {

21
funding.c

@ -45,8 +45,7 @@ static uint64_t htlcs_total(const struct channel_htlc *htlcs)
return total; return total;
} }
static bool change_funding(bool a_is_funder, static bool change_funding(uint64_t anchor_satoshis,
uint64_t anchor_satoshis,
int64_t delta_a_msat, int64_t delta_a_msat,
int64_t htlc_msat, int64_t htlc_msat,
uint64_t a, uint64_t b, uint64_t fee, 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) assert(a + b + htlcs_total(a_side->htlcs) + htlcs_total(b_side->htlcs)
== anchor_satoshis * 1000); == anchor_satoshis * 1000);
assert(a_side->offered_anchor != b_side->offered_anchor);
/* B gets whatever A gives. */ /* B gets whatever A gives. */
delta_b_msat = -delta_a_msat; delta_b_msat = -delta_a_msat;
@ -76,7 +76,7 @@ static bool change_funding(bool a_is_funder,
b += delta_b_msat; b += delta_b_msat;
/* Take off fee from both parties if possible. */ /* 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, got_fees = subtract_fees(&a, &b, &a_fee, &b_fee,
delta_b_msat < 0, fee); delta_b_msat < 0, fee);
else else
@ -94,8 +94,7 @@ static bool change_funding(bool a_is_funder,
return true; return true;
} }
bool funding_delta(bool a_is_funder, bool funding_delta(uint64_t anchor_satoshis,
uint64_t anchor_satoshis,
int64_t delta_a_msat, int64_t delta_a_msat,
int64_t htlc_msat, int64_t htlc_msat,
struct channel_oneside *a_side, 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; b = b_side->pay_msat + b_side->fee_msat;
fee = a_side->fee_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, delta_a_msat, htlc_msat,
a, b, fee, a, b, fee,
a_side, b_side); a_side, b_side);
@ -134,19 +133,19 @@ struct channel_state *initial_funding(const tal_t *ctx,
/* Initially, all goes back to funder. */ /* Initially, all goes back to funder. */
state->a.pay_msat = anchor_satoshis * 1000 - fee * 1000; state->a.pay_msat = anchor_satoshis * 1000 - fee * 1000;
state->a.fee_msat = 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 B (not A) is funder, invert. */
if (!am_funder) if (!am_funder)
invert_cstate(state); invert_cstate(state);
/* Make sure it checks out. */ /* Make sure it checks out. */
assert(funding_delta(am_funder, anchor_satoshis, 0, 0, assert(funding_delta(anchor_satoshis, 0, 0, &state->a, &state->b));
&state->a, &state->b));
return state; return state;
} }
bool adjust_fee(bool a_is_funder, bool adjust_fee(uint64_t anchor_satoshis,
uint64_t anchor_satoshis,
uint64_t fee_satoshis, uint64_t fee_satoshis,
struct channel_oneside *a_side, struct channel_oneside *a_side,
struct channel_oneside *b_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; b = b_side->pay_msat + b_side->fee_msat;
/* No HTLC or delta, just fee recalculate. */ /* 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, 0, 0, a, b, fee_satoshis * 1000,
a_side, b_side); a_side, b_side);
} }

10
funding.h

@ -15,6 +15,8 @@ struct channel_htlc {
struct channel_oneside { struct channel_oneside {
/* Payment and fee is in millisatoshi. */ /* Payment and fee is in millisatoshi. */
uint32_t pay_msat, fee_msat; uint32_t pay_msat, fee_msat;
/* Did we offer the anchor? */
bool offered_anchor;
/* Use tal_count to get the number */ /* Use tal_count to get the number */
struct channel_htlc *htlcs; 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? * funding_delta: With this change, what's the new state?
* @a_is_funder: is A paying for the anchor?
* @anchor_satoshis: The anchor amount. * @anchor_satoshis: The anchor amount.
* @delta_a: How many millisatoshi A changes (-ve => A pay B, +ve => B pays A) * @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) * @htlc: Millisatoshi 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.
*/ */
bool funding_delta(bool a_is_funder, bool funding_delta(uint64_t anchor_satoshis,
uint64_t anchor_satoshis,
int64_t delta_a_msat, int64_t delta_a_msat,
int64_t htlc_msat, int64_t htlc_msat,
struct channel_oneside *a_side, struct channel_oneside *a_side,
@ -63,14 +63,12 @@ bool funding_delta(bool a_is_funder,
/** /**
* adjust_fee: Change fee. * adjust_fee: Change fee.
* @a_is_funder: is A paying for the anchor?
* @anchor_satoshis: The anchor amount. * @anchor_satoshis: The anchor amount.
* @fee_satoshis: The new fee amount. * @fee_satoshis: The new fee amount.
* @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.
*/ */
bool adjust_fee(bool a_is_funder, bool adjust_fee(uint64_t anchor_satoshis,
uint64_t anchor_satoshis,
uint64_t fee_satoshis, uint64_t fee_satoshis,
struct channel_oneside *a_side, struct channel_oneside *a_side,
struct channel_oneside *b_side); struct channel_oneside *b_side);

Loading…
Cancel
Save