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