2015-07-29 08:44:28 +02:00
|
|
|
#include "funding.h"
|
|
|
|
#include <assert.h>
|
2015-08-07 05:15:30 +02:00
|
|
|
#include <string.h>
|
2015-07-29 08:44:28 +02:00
|
|
|
|
2015-08-07 05:15:30 +02:00
|
|
|
static bool is_funder(const OpenChannel *o)
|
2015-07-29 08:44:28 +02:00
|
|
|
{
|
2015-08-07 05:15:30 +02:00
|
|
|
return o->anch == OPEN_CHANNEL__ANCHOR_OFFER__WILL_CREATE_ANCHOR;
|
|
|
|
}
|
2015-07-29 08:46:24 +02:00
|
|
|
|
2015-08-07 05:15:30 +02:00
|
|
|
static bool subtract_fees(uint64_t *funder, uint64_t *non_funder,
|
|
|
|
uint64_t *funder_fee, uint64_t *non_funder_fee,
|
|
|
|
bool non_funder_paying, uint64_t fee)
|
|
|
|
{
|
2015-09-24 07:30:47 +02:00
|
|
|
/* Funder gets 1 millisatsoshi rounding benefit! */
|
2015-08-07 05:15:30 +02:00
|
|
|
*non_funder_fee = fee - fee / 2;
|
2015-07-29 08:46:24 +02:00
|
|
|
|
2015-08-07 05:15:30 +02:00
|
|
|
if (*non_funder < *non_funder_fee) {
|
2015-07-29 08:46:24 +02:00
|
|
|
/*
|
|
|
|
* This happens initially, as funder has all the money.
|
2015-08-07 05:15:30 +02:00
|
|
|
* That's OK, but don't let non-funder spend if they can't
|
2015-07-29 08:46:24 +02:00
|
|
|
* cover fee.
|
|
|
|
*/
|
2015-08-07 05:15:30 +02:00
|
|
|
if (non_funder_paying)
|
2015-07-29 08:46:24 +02:00
|
|
|
return false;
|
|
|
|
|
|
|
|
/* Pay everything they can, funder pays rest. */
|
2015-08-07 05:15:30 +02:00
|
|
|
*non_funder_fee = *non_funder;
|
2015-07-29 08:46:24 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Funder must always ensure they can pay their share. */
|
2015-08-07 05:15:30 +02:00
|
|
|
*funder_fee = fee - *non_funder_fee;
|
|
|
|
if (*funder < *funder_fee)
|
|
|
|
return false;
|
|
|
|
|
|
|
|
*non_funder -= *non_funder_fee;
|
|
|
|
*funder -= *funder_fee;
|
|
|
|
return true;
|
|
|
|
}
|
2015-08-07 05:15:30 +02:00
|
|
|
|
2015-09-24 07:30:47 +02:00
|
|
|
/* Total, in millisatoshi. */
|
|
|
|
static uint32_t htlcs_total(UpdateAddHtlc *const *htlcs)
|
2015-08-07 05:15:30 +02:00
|
|
|
{
|
|
|
|
size_t i, n = tal_count(htlcs);
|
2015-09-24 07:30:47 +02:00
|
|
|
uint32_t total = 0;
|
2015-08-07 05:15:30 +02:00
|
|
|
|
|
|
|
for (i = 0; i < n; i++)
|
2015-09-24 07:30:47 +02:00
|
|
|
total += htlcs[i]->amount_msat;
|
2015-08-07 05:15:30 +02:00
|
|
|
return total;
|
|
|
|
}
|
|
|
|
|
2015-08-07 05:15:30 +02:00
|
|
|
bool funding_delta(const OpenChannel *oa,
|
|
|
|
const OpenChannel *ob,
|
|
|
|
const OpenAnchor *anchor,
|
2015-09-24 07:30:47 +02:00
|
|
|
int64_t delta_a_msat,
|
|
|
|
int64_t htlc_msat,
|
2015-08-07 05:15:30 +02:00
|
|
|
struct channel_oneside *a_side,
|
|
|
|
struct channel_oneside *b_side)
|
|
|
|
{
|
|
|
|
uint64_t a, b, a_fee, b_fee;
|
2015-09-24 07:30:47 +02:00
|
|
|
int64_t delta_b_msat;
|
2015-08-07 05:15:30 +02:00
|
|
|
uint64_t fee;
|
|
|
|
bool got_fees;
|
|
|
|
|
2015-09-24 07:30:47 +02:00
|
|
|
a = a_side->pay_msat + a_side->fee_msat;
|
|
|
|
b = b_side->pay_msat + b_side->fee_msat;
|
|
|
|
fee = a_side->fee_msat + b_side->fee_msat;
|
2015-08-07 05:15:30 +02:00
|
|
|
assert(a + b + htlcs_total(a_side->htlcs) + htlcs_total(b_side->htlcs)
|
2015-09-24 07:30:47 +02:00
|
|
|
== anchor->amount * 1000);
|
2015-08-07 05:15:30 +02:00
|
|
|
|
|
|
|
/* Only one can be funder. */
|
|
|
|
if (is_funder(oa) == is_funder(ob))
|
2015-07-29 08:46:24 +02:00
|
|
|
return false;
|
|
|
|
|
2015-08-07 05:15:30 +02:00
|
|
|
/* B gets whatever A gives. */
|
2015-09-24 07:30:47 +02:00
|
|
|
delta_b_msat = -delta_a_msat;
|
2015-08-07 05:15:30 +02:00
|
|
|
/* A also pays for the htlc (if any). */
|
2015-09-24 07:30:47 +02:00
|
|
|
delta_a_msat -= htlc_msat;
|
2015-08-07 05:15:30 +02:00
|
|
|
|
2015-08-07 05:15:30 +02:00
|
|
|
/* Transferring more than we have? */
|
2015-09-24 07:30:47 +02:00
|
|
|
if (delta_b_msat < 0 && -delta_b_msat > b)
|
2015-08-07 05:15:30 +02:00
|
|
|
return false;
|
2015-09-24 07:30:47 +02:00
|
|
|
if (delta_a_msat < 0 && -delta_a_msat > a)
|
2015-08-07 05:15:30 +02:00
|
|
|
return false;
|
|
|
|
|
|
|
|
/* Adjust amounts. */
|
2015-09-24 07:30:47 +02:00
|
|
|
a += delta_a_msat;
|
|
|
|
b += delta_b_msat;
|
2015-07-29 08:46:24 +02:00
|
|
|
|
2015-08-07 05:15:30 +02:00
|
|
|
/* Take off fee from both parties if possible. */
|
|
|
|
if (is_funder(oa))
|
|
|
|
got_fees = subtract_fees(&a, &b, &a_fee, &b_fee,
|
2015-09-24 07:30:47 +02:00
|
|
|
delta_b_msat < 0, fee);
|
2015-08-07 05:15:30 +02:00
|
|
|
else
|
|
|
|
got_fees = subtract_fees(&b, &a, &b_fee, &a_fee,
|
2015-09-24 07:30:47 +02:00
|
|
|
delta_a_msat < 0, fee);
|
2015-08-07 05:15:30 +02:00
|
|
|
|
|
|
|
if (!got_fees)
|
|
|
|
return false;
|
|
|
|
|
|
|
|
/* Now we know we're succeeding, update caller's state */
|
2015-09-24 07:30:47 +02:00
|
|
|
a_side->pay_msat = a;
|
|
|
|
b_side->pay_msat = b;
|
|
|
|
a_side->fee_msat = a_fee;
|
|
|
|
b_side->fee_msat = b_fee;
|
2015-07-29 08:44:28 +02:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2015-08-07 05:15:30 +02:00
|
|
|
struct channel_state *initial_funding(const tal_t *ctx,
|
|
|
|
const OpenChannel *a,
|
|
|
|
const OpenChannel *b,
|
|
|
|
const OpenAnchor *anchor,
|
|
|
|
uint64_t fee)
|
2015-07-29 08:44:28 +02:00
|
|
|
{
|
2015-08-07 05:15:30 +02:00
|
|
|
struct channel_state *state = talz(ctx, struct channel_state);
|
|
|
|
|
2015-08-07 05:15:30 +02:00
|
|
|
state->a.htlcs = tal_arr(state, UpdateAddHtlc *, 0);
|
|
|
|
state->b.htlcs = tal_arr(state, UpdateAddHtlc *, 0);
|
|
|
|
|
2015-08-07 05:15:30 +02:00
|
|
|
if (fee > anchor->amount)
|
|
|
|
return tal_free(state);
|
|
|
|
|
2015-09-24 07:30:47 +02:00
|
|
|
if (anchor->amount > (1ULL << 32) / 1000)
|
|
|
|
return tal_free(state);
|
|
|
|
|
2015-08-07 05:15:30 +02:00
|
|
|
/* Initially, all goes back to funder. */
|
2015-09-24 07:30:47 +02:00
|
|
|
state->a.pay_msat = anchor->amount * 1000 - fee * 1000;
|
|
|
|
state->a.fee_msat = fee * 1000;
|
2015-08-07 05:15:30 +02:00
|
|
|
|
|
|
|
/* If B (not A) is funder, invert. */
|
|
|
|
if (is_funder(b))
|
|
|
|
invert_cstate(state);
|
2015-07-29 08:44:28 +02:00
|
|
|
|
2015-08-07 05:15:30 +02:00
|
|
|
/* This checks we only have 1 anchor, and is nice code reuse. */
|
2015-08-07 05:15:30 +02:00
|
|
|
if (!funding_delta(a, b, anchor, 0, 0, &state->a, &state->b))
|
2015-08-07 05:15:30 +02:00
|
|
|
return tal_free(state);
|
|
|
|
return state;
|
2015-07-29 08:44:28 +02:00
|
|
|
}
|
2015-07-29 08:46:24 +02:00
|
|
|
|
|
|
|
/* We take the minimum. If one side offers too little, it should be rejected */
|
|
|
|
uint64_t commit_fee(const OpenChannel *a, const OpenChannel *b)
|
|
|
|
{
|
|
|
|
if (a->commitment_fee < b->commitment_fee)
|
|
|
|
return a->commitment_fee;
|
|
|
|
return b->commitment_fee;
|
|
|
|
}
|
2015-08-07 05:15:30 +02:00
|
|
|
|
|
|
|
void invert_cstate(struct channel_state *cstate)
|
|
|
|
{
|
|
|
|
struct channel_oneside tmp;
|
|
|
|
|
|
|
|
tmp = cstate->a;
|
|
|
|
cstate->a = cstate->b;
|
|
|
|
cstate->b = tmp;
|
|
|
|
}
|