@@ -584,16 +584,58 @@ 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 ,
589
+ enum side opener ,
588
590
struct amount_sat * minfee ,
589
- struct amount_sat * desiredfee )
591
+ struct amount_sat * desiredfee ,
592
+ struct amount_sat * maxfee )
590
593
{
591
594
* minfee = amount_tx_fee (min_feerate , expected_weight );
592
595
* desiredfee = amount_tx_fee (desired_feerate , expected_weight );
593
596
597
+ /* BOLT-closing-fee_range #2:
598
+ * - if it is not the funder:
599
+ * - SHOULD set `max_fee_satoshis` to at least the `max_fee_satoshis`
600
+ * received
601
+ *...
602
+ * Note that the non-funder is not paying the fee, so there is
603
+ * no reason for it to have a maximum feerate.
604
+ */
605
+ if (opener == REMOTE ) {
606
+ * maxfee = AMOUNT_SAT (-1ULL );
607
+ /* BOLT-closing-fee_range #2:
608
+ * - If the channel does not use `option_anchor_outputs`:
609
+ * - MUST set `fee_satoshis` less than or equal to the base fee of
610
+ * the final commitment transaction, as calculated in
611
+ * [BOLT #3](03-transactions.md#fee-calculation).
612
+ */
613
+ } else if (max_feerate ) {
614
+ * maxfee = amount_tx_fee (* max_feerate , expected_weight );
615
+
616
+ status_debug ("deriving max fee from rate %u -> %s (not %s)" ,
617
+ * max_feerate ,
618
+ type_to_string (tmpctx , struct amount_sat , maxfee ),
619
+ type_to_string (tmpctx , struct amount_sat , & commitment_fee ));
620
+
621
+ /* option_anchor_outputs sets commitment_fee to max, so this
622
+ * doesn't do anything */
623
+ if (amount_sat_greater (* maxfee , commitment_fee )) {
624
+ status_unusual ("Maximum feerate %u would give fee %s:"
625
+ " we must limit it to the final commitment fee %s" ,
626
+ * max_feerate ,
627
+ type_to_string (tmpctx , struct amount_sat ,
628
+ maxfee ),
629
+ type_to_string (tmpctx , struct amount_sat ,
630
+ & commitment_fee ));
631
+ * maxfee = commitment_fee ;
632
+ }
633
+ } else
634
+ * maxfee = commitment_fee ;
635
+
594
636
/* Can't exceed maxfee. */
595
- if (amount_sat_greater (* minfee , maxfee ))
596
- * minfee = maxfee ;
637
+ if (amount_sat_greater (* minfee , * maxfee ))
638
+ * minfee = * maxfee ;
597
639
598
640
if (amount_sat_less (* desiredfee , * minfee )) {
599
641
status_unusual ("Our ideal fee is %s (%u sats/perkw),"
@@ -603,20 +645,20 @@ static void calc_fee_bounds(size_t expected_weight,
603
645
type_to_string (tmpctx , struct amount_sat , minfee ));
604
646
* desiredfee = * minfee ;
605
647
}
606
- if (amount_sat_greater (* desiredfee , maxfee )) {
648
+ if (amount_sat_greater (* desiredfee , * maxfee )) {
607
649
status_unusual ("Our ideal fee is %s (%u sats/perkw),"
608
650
" but our maximum is %s: using that" ,
609
651
type_to_string (tmpctx , struct amount_sat , desiredfee ),
610
652
desired_feerate ,
611
- type_to_string (tmpctx , struct amount_sat , & maxfee ));
612
- * desiredfee = maxfee ;
653
+ type_to_string (tmpctx , struct amount_sat , maxfee ));
654
+ * desiredfee = * maxfee ;
613
655
}
614
656
615
657
status_debug ("Expected closing weight = %zu, fee %s (min %s, max %s)" ,
616
658
expected_weight ,
617
659
type_to_string (tmpctx , struct amount_sat , desiredfee ),
618
660
type_to_string (tmpctx , struct amount_sat , minfee ),
619
- type_to_string (tmpctx , struct amount_sat , & maxfee ));
661
+ type_to_string (tmpctx , struct amount_sat , maxfee ));
620
662
}
621
663
622
664
/* We've received one offer; if we're opener, that means we've already sent one
@@ -799,8 +841,9 @@ int main(int argc, char *argv[])
799
841
u16 funding_txout ;
800
842
struct amount_sat funding , out [NUM_SIDES ];
801
843
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 ;
844
+ struct amount_sat min_fee_to_accept , commitment_fee , offer [NUM_SIDES ],
845
+ max_fee_to_accept ;
846
+ u32 min_feerate , initial_feerate , * max_feerate ;
804
847
struct feerange feerange ;
805
848
enum side opener ;
806
849
u8 * scriptpubkey [NUM_SIDES ], * funding_wscript ;
@@ -830,7 +873,7 @@ int main(int argc, char *argv[])
830
873
& out [LOCAL ],
831
874
& out [REMOTE ],
832
875
& our_dust_limit ,
833
- & min_feerate , & initial_feerate ,
876
+ & min_feerate , & initial_feerate , & max_feerate ,
834
877
& commitment_fee ,
835
878
& scriptpubkey [LOCAL ],
836
879
& scriptpubkey [REMOTE ],
@@ -852,8 +895,9 @@ int main(int argc, char *argv[])
852
895
calc_fee_bounds (closing_tx_weight_estimate (scriptpubkey ,
853
896
funding_wscript ,
854
897
out , funding , our_dust_limit ),
855
- min_feerate , initial_feerate , commitment_fee ,
856
- & min_fee_to_accept , & offer [LOCAL ]);
898
+ min_feerate , initial_feerate , max_feerate ,
899
+ commitment_fee , opener ,
900
+ & min_fee_to_accept , & offer [LOCAL ], & max_fee_to_accept );
857
901
858
902
/* Write values into tlv for updated closing fee neg */
859
903
their_feerange = tal (ctx , struct tlv_closing_signed_tlvs_fee_range * );
@@ -862,19 +906,7 @@ int main(int argc, char *argv[])
862
906
if (use_quickclose ) {
863
907
our_feerange = tal (ctx , struct tlv_closing_signed_tlvs_fee_range );
864
908
our_feerange -> min_fee_satoshis = min_fee_to_accept ;
865
-
866
- /* BOLT-closing-fee_range #2:
867
- * - if it is not the funder:
868
- * - SHOULD set `max_fee_satoshis` to at least the
869
- * `max_fee_satoshis` received
870
- *...
871
- * Note that the non-funder is not paying the fee, so there is
872
- * no reason for it to have a maximum feerate.
873
- */
874
- if (opener == REMOTE )
875
- our_feerange -> max_fee_satoshis = AMOUNT_SAT (-1ULL );
876
- else
877
- our_feerange -> max_fee_satoshis = commitment_fee ;
909
+ our_feerange -> max_fee_satoshis = max_fee_to_accept ;
878
910
} else
879
911
our_feerange = NULL ;
880
912
@@ -904,7 +936,7 @@ int main(int argc, char *argv[])
904
936
"Negotiating closing fee between %s and %s satoshi (ideal %s) "
905
937
"using step %s" ,
906
938
type_to_string (tmpctx , struct amount_sat , & min_fee_to_accept ),
907
- type_to_string (tmpctx , struct amount_sat , & commitment_fee ),
939
+ type_to_string (tmpctx , struct amount_sat , & max_fee_to_accept ),
908
940
type_to_string (tmpctx , struct amount_sat , & offer [LOCAL ]),
909
941
fee_negotiation_step_str );
910
942
@@ -967,7 +999,7 @@ int main(int argc, char *argv[])
967
999
}
968
1000
969
1001
/* Now we have first two points, we can init fee range. */
970
- init_feerange (& feerange , commitment_fee , offer );
1002
+ init_feerange (& feerange , max_fee_to_accept , offer );
971
1003
972
1004
/* Apply (and check) opener offer now. */
973
1005
adjust_feerange (& feerange , offer [opener ], opener );
@@ -1026,6 +1058,7 @@ int main(int argc, char *argv[])
1026
1058
tal_free (wrong_funding );
1027
1059
tal_free (our_feerange );
1028
1060
tal_free (their_feerange );
1061
+ tal_free (max_feerate );
1029
1062
closing_dev_memleak (ctx , scriptpubkey , funding_wscript );
1030
1063
#endif
1031
1064
0 commit comments