Skip to content

Commit 8c468c1

Browse files
committed
daemon: use fee rates rather than absolute fees (BOLT #2)
And divide fees as specified there. We still use fixed values rather than floating, and we don't send or handle update_fee messages. Signed-off-by: Rusty Russell <[email protected]>
1 parent ba2854e commit 8c468c1

File tree

11 files changed

+497
-330
lines changed

11 files changed

+497
-330
lines changed

daemon/lightningd.c

Lines changed: 29 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -82,15 +82,15 @@ static void config_register_opts(struct lightningd_state *dstate)
8282
opt_register_arg("--forever-confirms", opt_set_u32, opt_show_u32,
8383
&dstate->config.forever_confirms,
8484
"Confirmations after which we consider a reorg impossible");
85-
opt_register_arg("--commit-fee", opt_set_u64, opt_show_u64,
86-
&dstate->config.commitment_fee,
87-
"Satoshis to offer for commitment transaction fee");
88-
opt_register_arg("--min-commit-fee", opt_set_u64, opt_show_u64,
89-
&dstate->config.commitment_fee_min,
90-
"Minimum satoshis to accept for commitment transaction fee");
91-
opt_register_arg("--closing-fee", opt_set_u64, opt_show_u64,
92-
&dstate->config.closing_fee,
93-
"Satoshis to use for mutual close transaction fee");
85+
opt_register_arg("--commit-fee-rate", opt_set_u64, opt_show_u64,
86+
&dstate->config.commitment_fee_rate,
87+
"Satoshis to offer for commitment transaction fee (per kb)");
88+
opt_register_arg("--min-commit-fee-rate", opt_set_u64, opt_show_u64,
89+
&dstate->config.commitment_fee_rate_min,
90+
"Minimum satoshis to accept for commitment transaction fee (per kb)");
91+
opt_register_arg("--closing-fee-rate", opt_set_u64, opt_show_u64,
92+
&dstate->config.closing_fee_rate,
93+
"Satoshis to use for mutual close transaction fee (per kb)");
9494
opt_register_arg("--min-expiry", opt_set_u32, opt_show_u32,
9595
&dstate->config.min_expiry,
9696
"Minimum number of seconds to accept an HTLC before expiry");
@@ -128,14 +128,14 @@ static void default_config(struct config *config)
128128

129129
/* FIXME: These should float with bitcoind's recommendations! */
130130

131-
/* Pay hefty fee (10x current suggested minimum). */
132-
config->commitment_fee = 50000;
131+
/* Pay hefty fee (double historic high of ~100k). */
132+
config->commitment_fee_rate = 200000;
133133

134-
/* Don't accept less than double the current standard fee. */
135-
config->commitment_fee_min = 10000;
134+
/* Don't accept less than double the average 2-block fee. */
135+
config->commitment_fee_rate_min = 50000;
136136

137137
/* Use this for mutual close. */
138-
config->closing_fee = 10000;
138+
config->closing_fee_rate = 20000;
139139

140140
/* Don't bother me unless I have 6 hours to collect. */
141141
config->min_expiry = 6 * HOURS;
@@ -149,24 +149,22 @@ static void check_config(struct lightningd_state *dstate)
149149
{
150150
/* BOLT #2:
151151
* The sender MUST set `close_fee` lower than or equal to the
152-
* fee of the final commitment transaction, and MUST set
153-
* `close_fee` to an even number of satoshis.
152+
* fee of the final commitment transaction.
154153
*/
155-
if (dstate->config.closing_fee > dstate->config.commitment_fee)
156-
fatal("Closing fee %"PRIu64
157-
" can't exceed commitment fee %"PRIu64,
158-
dstate->config.closing_fee,
159-
dstate->config.commitment_fee);
160-
161-
if (dstate->config.closing_fee & 1)
162-
fatal("Closing fee %"PRIu64 "must be even.",
163-
dstate->config.closing_fee);
164-
165-
if (dstate->config.commitment_fee_min > dstate->config.commitment_fee)
166-
fatal("Minumum fee %"PRIu64
167-
" can't exceed commitment fee %"PRIu64,
168-
dstate->config.commitment_fee_min,
169-
dstate->config.commitment_fee);
154+
155+
/* We do this by ensuring it's less than the minimum we would accept. */
156+
if (dstate->config.closing_fee_rate > dstate->config.commitment_fee_rate_min)
157+
fatal("Closing fee rate %"PRIu64
158+
" can't exceed minimum commitment fee rate %"PRIu64,
159+
dstate->config.closing_fee_rate,
160+
dstate->config.commitment_fee_rate_min);
161+
162+
if (dstate->config.commitment_fee_rate_min
163+
> dstate->config.commitment_fee_rate)
164+
fatal("Minumum fee rate %"PRIu64
165+
" can't exceed commitment fee rate %"PRIu64,
166+
dstate->config.commitment_fee_rate_min,
167+
dstate->config.commitment_fee_rate);
170168
}
171169

172170
static struct lightningd_state *lightningd_state(void)

daemon/lightningd.h

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -29,14 +29,14 @@ struct config {
2929
/* How many blocks until we stop watching a close commit? */
3030
u32 forever_confirms;
3131

32-
/* What are we prepared to pay in commitment fee (satoshis). */
33-
u64 commitment_fee;
32+
/* What are we prepared to pay in commitment fee (satoshis/kb). */
33+
u64 commitment_fee_rate;
3434

3535
/* How little are we prepared to have them pay? */
36-
u64 commitment_fee_min;
36+
u64 commitment_fee_rate_min;
3737

38-
/* What fee we use for the closing transaction (satoshis) */
39-
u64 closing_fee;
38+
/* What fee we use for the closing transaction (satoshis/kb) */
39+
u64 closing_fee_rate;
4040

4141
/* Minimum/maximum time for an expiring HTLC (seconds). */
4242
u32 min_expiry, max_expiry;

daemon/packets.c

Lines changed: 21 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -81,7 +81,7 @@ Pkt *pkt_open(const tal_t *ctx, const struct peer *peer,
8181
locktime__init(o->delay);
8282
o->delay->locktime_case = LOCKTIME__LOCKTIME_SECONDS;
8383
o->delay->seconds = rel_locktime_to_seconds(&peer->us.locktime);
84-
o->commitment_fee = peer->us.commit_fee;
84+
o->initial_fee_rate = peer->us.commit_fee_rate;
8585
if (anchor == OPEN_CHANNEL__ANCHOR_OFFER__WILL_CREATE_ANCHOR)
8686
assert(peer->us.offer_anchor == CMD_OPEN_WITH_ANCHOR);
8787
else {
@@ -289,8 +289,8 @@ Pkt *accept_pkt_open(const tal_t *ctx,
289289
return pkt_err(ctx, "Delay too great");
290290
if (o->min_depth > peer->dstate->config.anchor_confirms_max)
291291
return pkt_err(ctx, "min_depth too great");
292-
if (o->commitment_fee < peer->dstate->config.commitment_fee_min)
293-
return pkt_err(ctx, "Commitment fee too low");
292+
if (o->initial_fee_rate < peer->dstate->config.commitment_fee_rate_min)
293+
return pkt_err(ctx, "Commitment fee rate too low");
294294
if (o->anch == OPEN_CHANNEL__ANCHOR_OFFER__WILL_CREATE_ANCHOR)
295295
peer->them.offer_anchor = CMD_OPEN_WITH_ANCHOR;
296296
else if (o->anch == OPEN_CHANNEL__ANCHOR_OFFER__WONT_CREATE_ANCHOR)
@@ -304,7 +304,7 @@ Pkt *accept_pkt_open(const tal_t *ctx,
304304
if (!proto_to_rel_locktime(o->delay, &peer->them.locktime))
305305
return pkt_err(ctx, "Malformed locktime");
306306
peer->them.mindepth = o->min_depth;
307-
peer->them.commit_fee = o->commitment_fee;
307+
peer->them.commit_fee_rate = o->initial_fee_rate;
308308
if (!proto_to_pubkey(peer->dstate->secpctx,
309309
o->commit_key, &peer->them.commitkey))
310310
return pkt_err(ctx, "Bad commitkey");
@@ -326,7 +326,6 @@ Pkt *accept_pkt_anchor(const tal_t *ctx,
326326
const Pkt *pkt)
327327
{
328328
const OpenAnchor *a = pkt->open_anchor;
329-
u64 commitfee;
330329

331330
/* They must be offering anchor for us to try accepting */
332331
assert(peer->us.offer_anchor == CMD_OPEN_WITHOUT_ANCHOR);
@@ -336,15 +335,13 @@ Pkt *accept_pkt_anchor(const tal_t *ctx,
336335
peer->anchor.index = a->output_index;
337336
peer->anchor.satoshis = a->amount;
338337

339-
/* Create funder's cstate, invert to get ours. */
340-
commitfee = commit_fee(peer->them.commit_fee, peer->us.commit_fee);
338+
/* Create our cstate. */
341339
peer->cstate = initial_funding(peer,
342-
peer->us.offer_anchor,
340+
peer->us.offer_anchor == CMD_OPEN_WITH_ANCHOR,
343341
peer->anchor.satoshis,
344-
commitfee);
342+
peer->us.commit_fee_rate);
345343
if (!peer->cstate)
346344
return pkt_err(ctx, "Insufficient funds for fee");
347-
invert_cstate(peer->cstate);
348345

349346
/* Now we can make initial (unsigned!) commit txs. */
350347
make_commit_txs(peer, peer,
@@ -449,18 +446,14 @@ Pkt *accept_pkt_htlc_add(const tal_t *ctx,
449446
}
450447

451448
cur->cstate = copy_funding(cur, peer->cstate);
452-
if (!funding_delta(peer->anchor.satoshis,
453-
0, cur->stage.add.htlc.msatoshis,
454-
&cur->cstate->b, &cur->cstate->a)) {
449+
if (!funding_b_add_htlc(cur->cstate,
450+
cur->stage.add.htlc.msatoshis,
451+
&cur->stage.add.htlc.expiry,
452+
&cur->stage.add.htlc.rhash)) {
455453
err = pkt_err(ctx, "Cannot afford %"PRIu64" milli-satoshis",
456454
cur->stage.add.htlc.msatoshis);
457455
goto fail;
458456
}
459-
/* Add the htlc to their side of channel. */
460-
funding_add_htlc(&cur->cstate->b,
461-
cur->stage.add.htlc.msatoshis,
462-
&cur->stage.add.htlc.expiry,
463-
&cur->stage.add.htlc.rhash);
464457
peer_add_htlc_expiry(peer, &cur->stage.add.htlc.expiry);
465458

466459
peer_get_revocation_hash(peer, peer->commit_tx_counter+1,
@@ -498,7 +491,6 @@ Pkt *accept_pkt_htlc_fail(const tal_t *ctx, struct peer *peer, const Pkt *pkt)
498491
Pkt *err;
499492
size_t i;
500493
struct sha256 rhash;
501-
struct channel_htlc *htlc;
502494

503495
proto_to_sha256(f->revocation_hash, &cur->their_revocation_hash);
504496
proto_to_sha256(f->r_hash, &rhash);
@@ -511,17 +503,10 @@ Pkt *accept_pkt_htlc_fail(const tal_t *ctx, struct peer *peer, const Pkt *pkt)
511503

512504
cur->stage.fail.fail = HTLC_FAIL;
513505
cur->stage.fail.index = i;
514-
htlc = &peer->cstate->a.htlcs[i];
515506

516-
/* Removing it should not fail: we regain HTLC amount */
507+
/* We regain HTLC amount */
517508
cur->cstate = copy_funding(cur, peer->cstate);
518-
if (!funding_delta(peer->anchor.satoshis,
519-
0, -htlc->msatoshis,
520-
&cur->cstate->a, &cur->cstate->b)) {
521-
fatal("Unexpected failure fulfilling HTLC of %"PRIu64
522-
" milli-satoshis", htlc->msatoshis);
523-
}
524-
funding_remove_htlc(&cur->cstate->a, i);
509+
funding_a_fail_htlc(cur->cstate, i);
525510
/* FIXME: Remove timer. */
526511

527512
peer_get_revocation_hash(peer, peer->commit_tx_counter+1,
@@ -550,7 +535,6 @@ Pkt *accept_pkt_htlc_fulfill(const tal_t *ctx,
550535
Pkt *err;
551536
size_t i;
552537
struct sha256 rhash;
553-
struct channel_htlc *htlc;
554538

555539
cur->stage.fulfill.fulfill = HTLC_FULFILL;
556540
proto_to_sha256(f->r, &cur->stage.fulfill.r);
@@ -564,19 +548,10 @@ Pkt *accept_pkt_htlc_fulfill(const tal_t *ctx,
564548
}
565549
cur->stage.fulfill.index = i;
566550

567-
htlc = &peer->cstate->a.htlcs[i];
568-
569-
/* Removing it should not fail: they gain HTLC amount */
551+
/* Removing it: they gain HTLC amount */
570552
cur->cstate = copy_funding(cur, peer->cstate);
571-
if (!funding_delta(peer->anchor.satoshis,
572-
-htlc->msatoshis,
573-
-htlc->msatoshis,
574-
&cur->cstate->a, &cur->cstate->b)) {
575-
fatal("Unexpected failure fulfilling HTLC of %"PRIu64
576-
" milli-satoshis", htlc->msatoshis);
577-
}
578-
funding_remove_htlc(&cur->cstate->a, i);
579-
553+
funding_a_fulfill_htlc(cur->cstate, i);
554+
580555
peer_get_revocation_hash(peer, peer->commit_tx_counter+1,
581556
&cur->our_revocation_hash);
582557

@@ -729,13 +704,16 @@ Pkt *accept_pkt_close_sig(const tal_t *ctx, struct peer *peer, const Pkt *pkt,
729704
struct bitcoin_signature theirsig;
730705

731706
/* BOLT #2:
732-
707+
*
733708
* The sender MUST set `close_fee` lower than or equal to the fee of the
734709
* final commitment transaction, and MUST set `close_fee` to an even
735710
* number of satoshis.
736711
*/
737-
if (c->close_fee & 1 || c->close_fee > peer->them.commit_fee)
712+
if ((c->close_fee & 1)
713+
|| c->close_fee > commit_tx_fee(peer->them.commit,
714+
peer->anchor.satoshis)) {
738715
return pkt_err(ctx, "Invalid close fee");
716+
}
739717

740718
/* FIXME: Don't accept tiny fee at all? */
741719

0 commit comments

Comments
 (0)