Skip to content

Commit 4cf8f5b

Browse files
committed
closingd: allow higher closing fee if anchor_outputs.
This follows lightning/bolts#847. For anchor_outputs, we pass down a max_feerate to closingd, and set the fee ceiling to MAX. It uses that to estimate the desired closing fee. Signed-off-by: Rusty Russell <[email protected]> Changelog-EXPERIMENTAL: Anchor output mutual close allow a fee higher than the final commitment transaction (as per lightning-rfc ElementsProject#847)
1 parent 07ec9ef commit 4cf8f5b

File tree

5 files changed

+86
-24
lines changed

5 files changed

+86
-24
lines changed

closingd/closingd.c

Lines changed: 50 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -584,16 +584,47 @@ static size_t closing_tx_weight_estimate(u8 *scriptpubkey[NUM_SIDES],
584584
static void calc_fee_bounds(size_t expected_weight,
585585
u32 min_feerate,
586586
u32 desired_feerate,
587-
struct amount_sat maxfee,
587+
u32 *max_feerate,
588+
struct amount_sat commitment_fee,
588589
struct amount_sat *minfee,
589-
struct amount_sat *desiredfee)
590+
struct amount_sat *desiredfee,
591+
struct amount_sat *maxfee)
590592
{
591593
*minfee = amount_tx_fee(min_feerate, expected_weight);
592594
*desiredfee = amount_tx_fee(desired_feerate, expected_weight);
593595

596+
/* BOLT-closing-fee_range #2:
597+
* - If the channel does not use `option_anchor_outputs`:
598+
* - MUST set `fee_satoshis` less than or equal to the base fee of
599+
* the final commitment transaction, as calculated in
600+
* [BOLT #3](03-transactions.md#fee-calculation).
601+
*/
602+
if (max_feerate) {
603+
*maxfee = amount_tx_fee(*max_feerate, expected_weight);
604+
605+
status_debug("deriving max fee from rate %u -> %s (not %s)",
606+
*max_feerate,
607+
type_to_string(tmpctx, struct amount_sat, maxfee),
608+
type_to_string(tmpctx, struct amount_sat, &commitment_fee));
609+
610+
/* option_anchor_outputs sets commitment_fee to max, so this
611+
* doesn't do anything */
612+
if (amount_sat_greater(*maxfee, commitment_fee)) {
613+
status_unusual("Maximum feerate %u would give fee %s:"
614+
" we must limit it to the final commitment fee %s",
615+
*max_feerate,
616+
type_to_string(tmpctx, struct amount_sat,
617+
maxfee),
618+
type_to_string(tmpctx, struct amount_sat,
619+
&commitment_fee));
620+
*maxfee = commitment_fee;
621+
}
622+
} else
623+
*maxfee = commitment_fee;
624+
594625
/* Can't exceed maxfee. */
595-
if (amount_sat_greater(*minfee, maxfee))
596-
*minfee = maxfee;
626+
if (amount_sat_greater(*minfee, *maxfee))
627+
*minfee = *maxfee;
597628

598629
if (amount_sat_less(*desiredfee, *minfee)) {
599630
status_unusual("Our ideal fee is %s (%u sats/perkw),"
@@ -603,20 +634,20 @@ static void calc_fee_bounds(size_t expected_weight,
603634
type_to_string(tmpctx, struct amount_sat, minfee));
604635
*desiredfee = *minfee;
605636
}
606-
if (amount_sat_greater(*desiredfee, maxfee)) {
637+
if (amount_sat_greater(*desiredfee, *maxfee)) {
607638
status_unusual("Our ideal fee is %s (%u sats/perkw),"
608639
" but our maximum is %s: using that",
609640
type_to_string(tmpctx, struct amount_sat, desiredfee),
610641
desired_feerate,
611-
type_to_string(tmpctx, struct amount_sat, &maxfee));
612-
*desiredfee = maxfee;
642+
type_to_string(tmpctx, struct amount_sat, maxfee));
643+
*desiredfee = *maxfee;
613644
}
614645

615646
status_debug("Expected closing weight = %zu, fee %s (min %s, max %s)",
616647
expected_weight,
617648
type_to_string(tmpctx, struct amount_sat, desiredfee),
618649
type_to_string(tmpctx, struct amount_sat, minfee),
619-
type_to_string(tmpctx, struct amount_sat, &maxfee));
650+
type_to_string(tmpctx, struct amount_sat, maxfee));
620651
}
621652

622653
/* We've received one offer; if we're opener, that means we've already sent one
@@ -799,8 +830,9 @@ int main(int argc, char *argv[])
799830
u16 funding_txout;
800831
struct amount_sat funding, out[NUM_SIDES];
801832
struct amount_sat our_dust_limit;
802-
struct amount_sat min_fee_to_accept, commitment_fee, offer[NUM_SIDES];
803-
u32 min_feerate, initial_feerate;
833+
struct amount_sat min_fee_to_accept, commitment_fee, offer[NUM_SIDES],
834+
max_fee_to_accept;
835+
u32 min_feerate, initial_feerate, *max_feerate;
804836
struct feerange feerange;
805837
enum side opener;
806838
u8 *scriptpubkey[NUM_SIDES], *funding_wscript;
@@ -830,7 +862,7 @@ int main(int argc, char *argv[])
830862
&out[LOCAL],
831863
&out[REMOTE],
832864
&our_dust_limit,
833-
&min_feerate, &initial_feerate,
865+
&min_feerate, &initial_feerate, &max_feerate,
834866
&commitment_fee,
835867
&scriptpubkey[LOCAL],
836868
&scriptpubkey[REMOTE],
@@ -852,8 +884,9 @@ int main(int argc, char *argv[])
852884
calc_fee_bounds(closing_tx_weight_estimate(scriptpubkey,
853885
funding_wscript,
854886
out, funding, our_dust_limit),
855-
min_feerate, initial_feerate, commitment_fee,
856-
&min_fee_to_accept, &offer[LOCAL]);
887+
min_feerate, initial_feerate, max_feerate,
888+
commitment_fee,
889+
&min_fee_to_accept, &offer[LOCAL], &max_fee_to_accept);
857890

858891
/* Write values into tlv for updated closing fee neg */
859892
their_feerange = tal(ctx, struct tlv_closing_signed_tlvs_fee_range *);
@@ -862,7 +895,7 @@ int main(int argc, char *argv[])
862895
if (use_quickclose) {
863896
our_feerange = tal(ctx, struct tlv_closing_signed_tlvs_fee_range);
864897
our_feerange->min_fee_satoshis = min_fee_to_accept;
865-
our_feerange->max_fee_satoshis = commitment_fee;
898+
our_feerange->max_fee_satoshis = max_fee_to_accept;
866899
} else
867900
our_feerange = NULL;
868901

@@ -892,7 +925,7 @@ int main(int argc, char *argv[])
892925
"Negotiating closing fee between %s and %s satoshi (ideal %s) "
893926
"using step %s",
894927
type_to_string(tmpctx, struct amount_sat, &min_fee_to_accept),
895-
type_to_string(tmpctx, struct amount_sat, &commitment_fee),
928+
type_to_string(tmpctx, struct amount_sat, &max_fee_to_accept),
896929
type_to_string(tmpctx, struct amount_sat, &offer[LOCAL]),
897930
fee_negotiation_step_str);
898931

@@ -955,7 +988,7 @@ int main(int argc, char *argv[])
955988
}
956989

957990
/* Now we have first two points, we can init fee range. */
958-
init_feerange(&feerange, commitment_fee, offer);
991+
init_feerange(&feerange, max_fee_to_accept, offer);
959992

960993
/* Apply (and check) opener offer now. */
961994
adjust_feerange(&feerange, offer[opener], opener);
@@ -1014,6 +1047,7 @@ int main(int argc, char *argv[])
10141047
tal_free(wrong_funding);
10151048
tal_free(our_feerange);
10161049
tal_free(their_feerange);
1050+
tal_free(max_feerate);
10171051
closing_dev_memleak(ctx, scriptpubkey, funding_wscript);
10181052
#endif
10191053

closingd/closingd_wire.csv

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ msgdata,closingd_init,remote_sat,amount_sat,
1919
msgdata,closingd_init,our_dust_limit,amount_sat,
2020
msgdata,closingd_init,min_feerate_perksipa,u32,
2121
msgdata,closingd_init,preferred_feerate_perksipa,u32,
22+
msgdata,closingd_init,max_feerate_perksipa,?u32,
2223
msgdata,closingd_init,fee_limit_satoshi,amount_sat,
2324
msgdata,closingd_init,local_scriptpubkey_len,u16,
2425
msgdata,closingd_init,local_scriptpubkey,u8,local_scriptpubkey_len

closingd/closingd_wiregen.c

Lines changed: 15 additions & 3 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

closingd/closingd_wiregen.h

Lines changed: 3 additions & 3 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

lightningd/closing_control.c

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -197,7 +197,7 @@ void peer_start_closingd(struct channel *channel,
197197
struct per_peer_state *pps)
198198
{
199199
u8 *initmsg;
200-
u32 feerate;
200+
u32 feerate, *max_feerate;
201201
struct amount_msat their_msat;
202202
struct amount_sat feelimit;
203203
int hsmfd;
@@ -256,6 +256,19 @@ void peer_start_closingd(struct channel *channel,
256256
feerate = feerate_floor();
257257
}
258258

259+
/* We use a feerate if anchor_outputs, otherwise max fee is set by
260+
* the final unilateral. */
261+
if (channel->option_anchor_outputs) {
262+
max_feerate = tal(tmpctx, u32);
263+
/* Aim for reasonable max, but use final if we don't know. */
264+
*max_feerate = unilateral_feerate(ld->topology);
265+
if (!*max_feerate)
266+
*max_feerate = final_commit_feerate;
267+
/* No other limit on fees */
268+
feelimit = AMOUNT_SAT(-1ULL);
269+
} else
270+
max_feerate = NULL;
271+
259272
/* BOLT #3:
260273
*
261274
* Each node offering a signature:
@@ -288,7 +301,9 @@ void peer_start_closingd(struct channel *channel,
288301
amount_msat_to_sat_round_down(channel->our_msat),
289302
amount_msat_to_sat_round_down(their_msat),
290303
channel->our_config.dust_limit,
291-
feerate_min(ld, NULL), feerate, feelimit,
304+
feerate_min(ld, NULL), feerate,
305+
max_feerate,
306+
feelimit,
292307
channel->shutdown_scriptpubkey[LOCAL],
293308
channel->shutdown_scriptpubkey[REMOTE],
294309
channel->closing_fee_negotiation_step,

0 commit comments

Comments
 (0)