@@ -584,16 +584,47 @@ static size_t closing_tx_weight_estimate(u8 *scriptpubkey[NUM_SIDES],
584
584
static void calc_fee_bounds (size_t expected_weight ,
585
585
u32 min_feerate ,
586
586
u32 desired_feerate ,
587
- struct amount_sat maxfee ,
587
+ u32 * max_feerate ,
588
+ struct amount_sat commitment_fee ,
588
589
struct amount_sat * minfee ,
589
- struct amount_sat * desiredfee )
590
+ struct amount_sat * desiredfee ,
591
+ struct amount_sat * maxfee )
590
592
{
591
593
* minfee = amount_tx_fee (min_feerate , expected_weight );
592
594
* desiredfee = amount_tx_fee (desired_feerate , expected_weight );
593
595
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
+
594
625
/* Can't exceed maxfee. */
595
- if (amount_sat_greater (* minfee , maxfee ))
596
- * minfee = maxfee ;
626
+ if (amount_sat_greater (* minfee , * maxfee ))
627
+ * minfee = * maxfee ;
597
628
598
629
if (amount_sat_less (* desiredfee , * minfee )) {
599
630
status_unusual ("Our ideal fee is %s (%u sats/perkw),"
@@ -603,20 +634,20 @@ static void calc_fee_bounds(size_t expected_weight,
603
634
type_to_string (tmpctx , struct amount_sat , minfee ));
604
635
* desiredfee = * minfee ;
605
636
}
606
- if (amount_sat_greater (* desiredfee , maxfee )) {
637
+ if (amount_sat_greater (* desiredfee , * maxfee )) {
607
638
status_unusual ("Our ideal fee is %s (%u sats/perkw),"
608
639
" but our maximum is %s: using that" ,
609
640
type_to_string (tmpctx , struct amount_sat , desiredfee ),
610
641
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 ;
613
644
}
614
645
615
646
status_debug ("Expected closing weight = %zu, fee %s (min %s, max %s)" ,
616
647
expected_weight ,
617
648
type_to_string (tmpctx , struct amount_sat , desiredfee ),
618
649
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 ));
620
651
}
621
652
622
653
/* 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[])
799
830
u16 funding_txout ;
800
831
struct amount_sat funding , out [NUM_SIDES ];
801
832
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 ;
804
836
struct feerange feerange ;
805
837
enum side opener ;
806
838
u8 * scriptpubkey [NUM_SIDES ], * funding_wscript ;
@@ -830,7 +862,7 @@ int main(int argc, char *argv[])
830
862
& out [LOCAL ],
831
863
& out [REMOTE ],
832
864
& our_dust_limit ,
833
- & min_feerate , & initial_feerate ,
865
+ & min_feerate , & initial_feerate , & max_feerate ,
834
866
& commitment_fee ,
835
867
& scriptpubkey [LOCAL ],
836
868
& scriptpubkey [REMOTE ],
@@ -852,8 +884,9 @@ int main(int argc, char *argv[])
852
884
calc_fee_bounds (closing_tx_weight_estimate (scriptpubkey ,
853
885
funding_wscript ,
854
886
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 );
857
890
858
891
/* Write values into tlv for updated closing fee neg */
859
892
their_feerange = tal (ctx , struct tlv_closing_signed_tlvs_fee_range * );
@@ -862,7 +895,7 @@ int main(int argc, char *argv[])
862
895
if (use_quickclose ) {
863
896
our_feerange = tal (ctx , struct tlv_closing_signed_tlvs_fee_range );
864
897
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 ;
866
899
} else
867
900
our_feerange = NULL ;
868
901
@@ -892,7 +925,7 @@ int main(int argc, char *argv[])
892
925
"Negotiating closing fee between %s and %s satoshi (ideal %s) "
893
926
"using step %s" ,
894
927
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 ),
896
929
type_to_string (tmpctx , struct amount_sat , & offer [LOCAL ]),
897
930
fee_negotiation_step_str );
898
931
@@ -955,7 +988,7 @@ int main(int argc, char *argv[])
955
988
}
956
989
957
990
/* 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 );
959
992
960
993
/* Apply (and check) opener offer now. */
961
994
adjust_feerange (& feerange , offer [opener ], opener );
@@ -1014,6 +1047,7 @@ int main(int argc, char *argv[])
1014
1047
tal_free (wrong_funding );
1015
1048
tal_free (our_feerange );
1016
1049
tal_free (their_feerange );
1050
+ tal_free (max_feerate );
1017
1051
closing_dev_memleak (ctx , scriptpubkey , funding_wscript );
1018
1052
#endif
1019
1053
0 commit comments