2016-06-30 01:38:10 +02:00
|
|
|
#include "channel.h"
|
2015-07-29 08:44:28 +02:00
|
|
|
#include <assert.h>
|
2016-05-26 07:55:24 +02:00
|
|
|
#include <ccan/array_size/array_size.h>
|
2016-03-08 01:01:15 +01:00
|
|
|
#include <ccan/mem/mem.h>
|
2016-01-21 21:11:47 +01:00
|
|
|
#include <ccan/structeq/structeq.h>
|
2015-08-07 05:15:30 +02:00
|
|
|
#include <string.h>
|
2015-07-29 08:44:28 +02:00
|
|
|
|
2016-03-24 02:42:43 +01:00
|
|
|
uint64_t fee_by_feerate(size_t txsize, uint32_t fee_rate)
|
|
|
|
{
|
|
|
|
/* BOLT #2:
|
|
|
|
*
|
2016-05-02 08:28:56 +02:00
|
|
|
* The fee for a transaction MUST be calculated by multiplying this
|
|
|
|
* bytecount by the fee rate, dividing by 1000 and truncating
|
|
|
|
* (rounding down) the result to an even number of satoshis.
|
2016-03-24 02:42:43 +01:00
|
|
|
*/
|
|
|
|
return txsize * fee_rate / 2000 * 2;
|
|
|
|
}
|
2015-08-07 05:15:30 +02:00
|
|
|
|
2016-03-24 02:42:43 +01:00
|
|
|
static uint64_t calculate_fee_msat(size_t num_nondust_htlcs,
|
|
|
|
uint32_t fee_rate)
|
|
|
|
{
|
|
|
|
uint64_t bytes;
|
|
|
|
|
|
|
|
/* BOLT #2:
|
|
|
|
*
|
|
|
|
* A node MUST use the formula 338 + 32 bytes for every
|
|
|
|
* non-dust HTLC as the bytecount for calculating commitment
|
|
|
|
* transaction fees. Note that the fee requirement is
|
|
|
|
* unchanged, even if the elimination of dust HTLC outputs has
|
|
|
|
* caused a non-zero fee already.
|
|
|
|
*/
|
|
|
|
bytes = 338 + 32 * num_nondust_htlcs;
|
|
|
|
|
|
|
|
/* milli-satoshis */
|
|
|
|
return fee_by_feerate(bytes, fee_rate) * 1000;
|
2015-08-07 05:15:30 +02:00
|
|
|
}
|
2015-08-07 05:15:30 +02:00
|
|
|
|
2015-09-24 07:30:47 +02:00
|
|
|
/* Total, in millisatoshi. */
|
2016-06-30 01:38:11 +02:00
|
|
|
static uint64_t htlcs_total(struct htlc **htlcs)
|
2015-08-07 05:15:30 +02:00
|
|
|
{
|
|
|
|
size_t i, n = tal_count(htlcs);
|
2016-01-21 21:11:47 +01:00
|
|
|
uint64_t total = 0;
|
2015-08-07 05:15:30 +02:00
|
|
|
|
|
|
|
for (i = 0; i < n; i++)
|
2016-06-30 01:38:11 +02:00
|
|
|
total += htlcs[i]->msatoshis;
|
2015-08-07 05:15:30 +02:00
|
|
|
return total;
|
|
|
|
}
|
|
|
|
|
2016-03-24 02:42:43 +01:00
|
|
|
/* Pay this much fee, if possible. Return amount unpaid. */
|
|
|
|
static uint64_t pay_fee(struct channel_oneside *side, uint64_t fee_msat)
|
|
|
|
{
|
|
|
|
if (side->pay_msat >= fee_msat) {
|
|
|
|
side->pay_msat -= fee_msat;
|
|
|
|
side->fee_msat += fee_msat;
|
|
|
|
return 0;
|
|
|
|
} else {
|
|
|
|
uint64_t remainder = fee_msat - side->pay_msat;
|
|
|
|
side->fee_msat += side->pay_msat;
|
|
|
|
side->pay_msat = 0;
|
|
|
|
return remainder;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Charge the fee as per BOLT #2 */
|
|
|
|
static void recalculate_fees(struct channel_oneside *a,
|
|
|
|
struct channel_oneside *b,
|
|
|
|
uint64_t fee_msat)
|
|
|
|
{
|
|
|
|
uint64_t remainder;
|
|
|
|
|
|
|
|
/* Fold in fees, to recalcuate again below. */
|
|
|
|
a->pay_msat += a->fee_msat;
|
|
|
|
b->pay_msat += b->fee_msat;
|
|
|
|
a->fee_msat = b->fee_msat = 0;
|
|
|
|
|
|
|
|
/* BOLT #2:
|
|
|
|
*
|
|
|
|
* 1. If each nodes can afford half the fee from their
|
|
|
|
* to-`final_key` output, reduce the two to-`final_key`
|
|
|
|
* outputs accordingly.
|
|
|
|
*
|
|
|
|
* 2. Otherwise, reduce the to-`final_key` output of one node
|
|
|
|
* which cannot afford the fee to zero (resulting in that
|
|
|
|
* entire output paying fees). If the remaining
|
|
|
|
* to-`final_key` output is greater than the fee remaining,
|
|
|
|
* reduce it accordingly, otherwise reduce it to zero to
|
|
|
|
* pay as much fee as possible.
|
|
|
|
*/
|
|
|
|
remainder = pay_fee(a, fee_msat / 2) + pay_fee(b, fee_msat / 2);
|
|
|
|
|
|
|
|
/* If there's anything left, the other side tries to pay for it. */
|
|
|
|
remainder = pay_fee(a, remainder);
|
|
|
|
pay_fee(b, remainder);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* a transfers htlc_msat to a HTLC (gains it, if -ve) */
|
2016-03-15 07:37:30 +01:00
|
|
|
static bool change_funding(uint64_t anchor_satoshis,
|
2016-03-24 02:42:43 +01:00
|
|
|
uint32_t fee_rate,
|
2016-01-21 21:11:47 +01:00
|
|
|
int64_t htlc_msat,
|
2016-03-24 02:42:43 +01:00
|
|
|
struct channel_oneside *a,
|
|
|
|
struct channel_oneside *b,
|
|
|
|
size_t num_nondust_htlcs)
|
2015-08-07 05:15:30 +02:00
|
|
|
{
|
2016-03-24 02:42:43 +01:00
|
|
|
uint64_t fee_msat;
|
2015-08-07 05:15:30 +02:00
|
|
|
|
2016-03-24 02:42:43 +01:00
|
|
|
assert(a->pay_msat + a->fee_msat
|
|
|
|
+ b->pay_msat + b->fee_msat
|
|
|
|
+ htlcs_total(a->htlcs) + htlcs_total(b->htlcs)
|
2016-01-21 21:11:47 +01:00
|
|
|
== anchor_satoshis * 1000);
|
2015-08-07 05:15:30 +02:00
|
|
|
|
2016-03-24 02:42:43 +01:00
|
|
|
fee_msat = calculate_fee_msat(num_nondust_htlcs, fee_rate);
|
2015-07-29 08:46:24 +02:00
|
|
|
|
2016-03-24 02:42:43 +01:00
|
|
|
/* If A is paying, can it afford it? */
|
|
|
|
if (htlc_msat > 0) {
|
|
|
|
if (htlc_msat + fee_msat / 2 > a->pay_msat + a->fee_msat)
|
|
|
|
return false;
|
|
|
|
}
|
2015-08-07 05:15:30 +02:00
|
|
|
|
2016-03-24 02:42:43 +01:00
|
|
|
/* OK, now adjust funds for A, then recalculate fees. */
|
|
|
|
a->pay_msat -= htlc_msat;
|
|
|
|
recalculate_fees(a, b, fee_msat);
|
2015-08-07 05:15:30 +02:00
|
|
|
|
2016-03-24 02:42:43 +01:00
|
|
|
assert(a->pay_msat + a->fee_msat
|
|
|
|
+ b->pay_msat + b->fee_msat
|
|
|
|
+ htlcs_total(a->htlcs) + htlcs_total(b->htlcs) + htlc_msat
|
|
|
|
== anchor_satoshis * 1000);
|
2015-07-29 08:44:28 +02:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2016-06-30 01:38:10 +02:00
|
|
|
struct channel_state *initial_cstate(const tal_t *ctx,
|
2016-01-21 21:11:47 +01:00
|
|
|
uint64_t anchor_satoshis,
|
2016-05-26 07:55:24 +02:00
|
|
|
uint32_t fee_rate,
|
|
|
|
enum channel_side funding)
|
2015-07-29 08:44:28 +02:00
|
|
|
{
|
2016-03-24 02:42:43 +01:00
|
|
|
uint64_t fee_msat;
|
2016-03-24 02:41:26 +01:00
|
|
|
struct channel_state *cstate = talz(ctx, struct channel_state);
|
2016-05-26 07:55:24 +02:00
|
|
|
struct channel_oneside *funder, *fundee;
|
2015-08-07 05:15:30 +02:00
|
|
|
|
2016-03-24 02:42:43 +01:00
|
|
|
cstate->fee_rate = fee_rate;
|
|
|
|
cstate->anchor = anchor_satoshis;
|
2016-03-31 08:42:20 +02:00
|
|
|
cstate->changes = 0;
|
2016-03-24 02:42:43 +01:00
|
|
|
|
|
|
|
/* Anchor must fit in 32 bit. */
|
|
|
|
if (anchor_satoshis >= (1ULL << 32) / 1000)
|
2016-03-24 02:41:26 +01:00
|
|
|
return tal_free(cstate);
|
2015-08-07 05:15:30 +02:00
|
|
|
|
2016-03-24 02:42:43 +01:00
|
|
|
fee_msat = calculate_fee_msat(0, fee_rate);
|
|
|
|
if (fee_msat > anchor_satoshis * 1000)
|
2016-03-24 02:41:26 +01:00
|
|
|
return tal_free(cstate);
|
2016-03-24 02:42:43 +01:00
|
|
|
|
2016-05-26 07:55:24 +02:00
|
|
|
funder = &cstate->side[funding];
|
|
|
|
fundee = &cstate->side[!funding];
|
|
|
|
|
|
|
|
/* Neither side has HTLCs. */
|
2016-06-30 01:38:11 +02:00
|
|
|
funder->htlcs = tal_arr(cstate, struct htlc *, 0);
|
|
|
|
fundee->htlcs = tal_arr(cstate, struct htlc *, 0);
|
2015-08-07 05:15:30 +02:00
|
|
|
|
2016-05-26 07:55:24 +02:00
|
|
|
/* Initially, all goes back to funder. */
|
|
|
|
funder->pay_msat = anchor_satoshis * 1000 - fee_msat;
|
|
|
|
funder->fee_msat = fee_msat;
|
2015-07-29 08:44:28 +02:00
|
|
|
|
2016-01-21 21:11:47 +01:00
|
|
|
/* Make sure it checks out. */
|
2016-05-26 07:55:24 +02:00
|
|
|
assert(change_funding(anchor_satoshis, fee_rate, 0, funder, fundee, 0));
|
|
|
|
assert(funder->fee_msat == fee_msat);
|
|
|
|
assert(fundee->fee_msat == 0);
|
|
|
|
|
2016-03-24 02:41:26 +01:00
|
|
|
return cstate;
|
2015-07-29 08:44:28 +02:00
|
|
|
}
|
2015-07-29 08:46:24 +02:00
|
|
|
|
2016-03-24 02:42:43 +01:00
|
|
|
/* Dust is defined as an output < 546*minRelayTxFee/1000.
|
|
|
|
* minRelayTxFee defaults to 1000 satoshi. */
|
|
|
|
bool is_dust_amount(uint64_t satoshis)
|
2016-01-21 21:11:47 +01:00
|
|
|
{
|
2016-03-24 02:42:43 +01:00
|
|
|
return satoshis < 546;
|
|
|
|
}
|
2016-01-21 21:11:47 +01:00
|
|
|
|
2016-06-30 01:38:11 +02:00
|
|
|
static size_t count_nondust_htlcs(struct htlc **htlcs)
|
2016-03-24 02:42:43 +01:00
|
|
|
{
|
|
|
|
size_t i, n = tal_count(htlcs), nondust = 0;
|
2016-01-21 21:11:47 +01:00
|
|
|
|
2016-03-24 02:42:43 +01:00
|
|
|
for (i = 0; i < n; i++)
|
2016-06-30 01:38:11 +02:00
|
|
|
if (!is_dust_amount(htlcs[i]->msatoshis / 1000))
|
2016-03-24 02:42:43 +01:00
|
|
|
nondust++;
|
|
|
|
return nondust;
|
2016-01-21 21:11:47 +01:00
|
|
|
}
|
2016-03-24 02:42:43 +01:00
|
|
|
|
|
|
|
static size_t total_nondust_htlcs(const struct channel_state *cstate)
|
2015-07-29 08:46:24 +02:00
|
|
|
{
|
2016-05-26 07:55:24 +02:00
|
|
|
return count_nondust_htlcs(cstate->side[OURS].htlcs)
|
|
|
|
+ count_nondust_htlcs(cstate->side[THEIRS].htlcs);
|
2015-07-29 08:46:24 +02:00
|
|
|
}
|
2015-08-07 05:15:30 +02:00
|
|
|
|
2016-03-24 02:42:43 +01:00
|
|
|
void adjust_fee(struct channel_state *cstate, uint32_t fee_rate)
|
|
|
|
{
|
|
|
|
uint64_t fee_msat;
|
|
|
|
|
|
|
|
fee_msat = calculate_fee_msat(total_nondust_htlcs(cstate), fee_rate);
|
|
|
|
|
2016-05-26 07:55:24 +02:00
|
|
|
recalculate_fees(&cstate->side[OURS], &cstate->side[THEIRS], fee_msat);
|
2016-03-31 08:42:20 +02:00
|
|
|
cstate->changes++;
|
2016-03-24 02:42:43 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
bool force_fee(struct channel_state *cstate, uint64_t fee)
|
|
|
|
{
|
|
|
|
/* Beware overflow! */
|
|
|
|
if (fee > 0xFFFFFFFFFFFFFFFFULL / 1000)
|
|
|
|
return false;
|
2016-05-26 07:55:24 +02:00
|
|
|
recalculate_fees(&cstate->side[OURS], &cstate->side[THEIRS], fee * 1000);
|
2016-03-31 08:42:20 +02:00
|
|
|
cstate->changes++;
|
2016-05-26 07:55:24 +02:00
|
|
|
return cstate->side[OURS].fee_msat + cstate->side[THEIRS].fee_msat == fee * 1000;
|
2016-03-24 02:42:43 +01:00
|
|
|
}
|
2016-01-21 21:11:47 +01:00
|
|
|
|
2016-03-24 02:42:43 +01:00
|
|
|
/* Add a HTLC to @creator if it can afford it. */
|
2016-06-30 01:38:11 +02:00
|
|
|
bool cstate_add_htlc(struct channel_state *cstate,
|
|
|
|
struct htlc *htlc,
|
|
|
|
enum channel_side side)
|
2016-01-21 21:11:47 +01:00
|
|
|
{
|
2016-03-24 02:42:43 +01:00
|
|
|
size_t n, nondust;
|
2016-05-26 07:55:24 +02:00
|
|
|
struct channel_oneside *creator, *recipient;
|
2016-03-24 02:42:43 +01:00
|
|
|
|
2016-05-26 07:55:24 +02:00
|
|
|
creator = &cstate->side[side];
|
|
|
|
recipient = &cstate->side[!side];
|
|
|
|
|
2016-03-24 02:42:43 +01:00
|
|
|
/* Remember to count the new one in total txsize if not dust! */
|
|
|
|
nondust = total_nondust_htlcs(cstate);
|
2016-06-30 01:38:11 +02:00
|
|
|
if (!is_dust_amount(htlc->msatoshis / 1000))
|
2016-03-24 02:42:43 +01:00
|
|
|
nondust++;
|
|
|
|
|
|
|
|
if (!change_funding(cstate->anchor, cstate->fee_rate,
|
2016-06-30 01:38:11 +02:00
|
|
|
htlc->msatoshis, creator, recipient, nondust))
|
|
|
|
return false;
|
2016-03-24 02:42:43 +01:00
|
|
|
|
|
|
|
n = tal_count(creator->htlcs);
|
2016-01-21 21:11:47 +01:00
|
|
|
tal_resize(&creator->htlcs, n+1);
|
|
|
|
|
2016-06-30 01:38:11 +02:00
|
|
|
creator->htlcs[n] = htlc;
|
|
|
|
memcheck(&creator->htlcs[n]->msatoshis,
|
|
|
|
sizeof(creator->htlcs[n]->msatoshis));
|
|
|
|
memcheck(&creator->htlcs[n]->rhash, sizeof(creator->htlcs[n]->rhash));
|
2016-03-31 08:42:20 +02:00
|
|
|
cstate->changes++;
|
2016-06-30 01:38:11 +02:00
|
|
|
return true;
|
2016-03-24 02:42:43 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Remove htlc from creator, credit it to beneficiary. */
|
2016-03-31 08:42:20 +02:00
|
|
|
static void remove_htlc(struct channel_state *cstate,
|
2016-05-26 07:55:24 +02:00
|
|
|
enum channel_side creator,
|
|
|
|
enum channel_side beneficiary,
|
2016-06-30 01:38:11 +02:00
|
|
|
struct htlc *htlc)
|
2016-03-24 02:42:43 +01:00
|
|
|
{
|
|
|
|
size_t nondust;
|
2016-06-30 01:38:11 +02:00
|
|
|
size_t i, n = tal_count(cstate->side[creator].htlcs);
|
2016-03-24 02:42:43 +01:00
|
|
|
|
|
|
|
/* Remember to remove this one in total txsize if not dust! */
|
|
|
|
nondust = total_nondust_htlcs(cstate);
|
2016-06-28 23:19:20 +02:00
|
|
|
if (!is_dust_amount(htlc->msatoshis / 1000)) {
|
2016-03-24 02:42:43 +01:00
|
|
|
assert(nondust > 0);
|
|
|
|
nondust--;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Can't fail since msatoshis is positive. */
|
|
|
|
if (!change_funding(cstate->anchor, cstate->fee_rate,
|
2016-06-28 23:19:20 +02:00
|
|
|
-(int64_t)htlc->msatoshis,
|
2016-05-26 07:55:24 +02:00
|
|
|
&cstate->side[beneficiary],
|
|
|
|
&cstate->side[!beneficiary], nondust))
|
2016-03-24 02:42:43 +01:00
|
|
|
abort();
|
|
|
|
|
|
|
|
/* Actually remove the HTLC. */
|
2016-06-30 01:38:11 +02:00
|
|
|
for (i = 0; i < tal_count(cstate->side[creator].htlcs); i++) {
|
|
|
|
if (cstate->side[creator].htlcs[i] == htlc) {
|
|
|
|
memmove(cstate->side[creator].htlcs + i,
|
|
|
|
cstate->side[creator].htlcs + i + 1,
|
|
|
|
(n - i - 1) * sizeof(htlc));
|
|
|
|
tal_resize(&cstate->side[creator].htlcs, n-1);
|
|
|
|
cstate->changes++;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
abort();
|
2016-03-24 02:42:43 +01:00
|
|
|
}
|
|
|
|
|
2016-06-30 01:38:10 +02:00
|
|
|
void cstate_fail_htlc(struct channel_state *cstate,
|
2016-06-30 01:38:11 +02:00
|
|
|
struct htlc *htlc,
|
|
|
|
enum channel_side side)
|
2016-03-24 02:42:43 +01:00
|
|
|
{
|
2016-06-28 23:19:20 +02:00
|
|
|
remove_htlc(cstate, side, side, htlc);
|
2016-03-24 02:42:43 +01:00
|
|
|
}
|
|
|
|
|
2016-06-30 01:38:10 +02:00
|
|
|
void cstate_fulfill_htlc(struct channel_state *cstate,
|
2016-06-30 01:38:11 +02:00
|
|
|
struct htlc *htlc,
|
|
|
|
enum channel_side side)
|
2016-03-24 02:42:43 +01:00
|
|
|
{
|
2016-06-28 23:19:20 +02:00
|
|
|
remove_htlc(cstate, side, !side, htlc);
|
2016-03-24 02:42:43 +01:00
|
|
|
}
|
|
|
|
|
2016-06-30 01:38:11 +02:00
|
|
|
struct htlc *cstate_find_htlc(const struct channel_state *cstate,
|
|
|
|
const struct sha256 *rhash,
|
|
|
|
enum channel_side side)
|
2016-01-21 21:11:47 +01:00
|
|
|
{
|
|
|
|
size_t i;
|
|
|
|
|
2016-05-26 07:55:24 +02:00
|
|
|
for (i = 0; i < tal_count(cstate->side[side].htlcs); i++) {
|
2016-06-30 01:38:11 +02:00
|
|
|
if (structeq(&cstate->side[side].htlcs[i]->rhash, rhash))
|
|
|
|
return cstate->side[side].htlcs[i];
|
2016-01-21 21:11:47 +01:00
|
|
|
}
|
2016-06-30 01:38:11 +02:00
|
|
|
return NULL;
|
2016-01-21 21:11:47 +01:00
|
|
|
}
|
|
|
|
|
2016-06-30 01:38:11 +02:00
|
|
|
struct htlc *cstate_htlc_by_id(const struct channel_state *cstate,
|
|
|
|
uint64_t id,
|
|
|
|
enum channel_side side)
|
2016-03-30 08:25:03 +02:00
|
|
|
{
|
|
|
|
size_t i;
|
|
|
|
|
2016-05-26 07:55:24 +02:00
|
|
|
for (i = 0; i < tal_count(cstate->side[side].htlcs); i++) {
|
2016-06-30 01:38:11 +02:00
|
|
|
if (cstate->side[side].htlcs[i]->id == id)
|
|
|
|
return cstate->side[side].htlcs[i];
|
2016-03-30 08:25:03 +02:00
|
|
|
}
|
2016-06-28 23:19:20 +02:00
|
|
|
return NULL;
|
2016-03-30 08:25:03 +02:00
|
|
|
}
|
|
|
|
|
2016-06-30 01:38:10 +02:00
|
|
|
struct channel_state *copy_cstate(const tal_t *ctx,
|
2016-06-30 01:38:11 +02:00
|
|
|
const struct channel_state *cstate)
|
2016-01-21 21:11:47 +01:00
|
|
|
{
|
|
|
|
struct channel_state *cs = tal_dup(ctx, struct channel_state, cstate);
|
2016-06-30 01:38:11 +02:00
|
|
|
size_t i;
|
2016-01-21 21:11:47 +01:00
|
|
|
|
2016-06-30 01:38:10 +02:00
|
|
|
for (i = 0; i < ARRAY_SIZE(cs->side); i++) {
|
2016-06-30 01:38:11 +02:00
|
|
|
cs->side[i].htlcs = tal_dup_arr(cs, struct htlc *,
|
2016-05-26 07:55:24 +02:00
|
|
|
cs->side[i].htlcs,
|
|
|
|
tal_count(cs->side[i].htlcs), 0);
|
2016-06-30 01:38:10 +02:00
|
|
|
}
|
2016-01-21 21:11:47 +01:00
|
|
|
return cs;
|
|
|
|
}
|