@@ -584,16 +584,47 @@ static size_t closing_tx_weight_estimate(u8 *scriptpubkey[NUM_SIDES],
584584static 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
0 commit comments