@@ -734,6 +734,8 @@ where
734
734
previous_utxo: anchor_utxo,
735
735
satisfaction_weight: commitment_tx. weight( ) as u64 + ANCHOR_INPUT_WITNESS_WEIGHT + EMPTY_SCRIPT_SIG_WEIGHT ,
736
736
} ] ;
737
+ #[ cfg( debug_assertions) ]
738
+ let must_spend_amount = must_spend. iter ( ) . map ( |input| input. previous_utxo . value ) . sum :: < u64 > ( ) ;
737
739
738
740
log_debug ! ( self . logger, "Peforming coin selection for commitment package (commitment and anchor transaction) targeting {} sat/kW" ,
739
741
package_target_feerate_sat_per_1000_weight) ;
@@ -747,10 +749,13 @@ where
747
749
input : vec ! [ anchor_descriptor. unsigned_tx_input( ) ] ,
748
750
output : vec ! [ ] ,
749
751
} ;
752
+
753
+ #[ cfg( debug_assertions) ]
754
+ let total_satisfaction_weight = ANCHOR_INPUT_WITNESS_WEIGHT + EMPTY_SCRIPT_SIG_WEIGHT +
755
+ coin_selection. confirmed_utxos . iter ( ) . map ( |utxo| utxo. satisfaction_weight ) . sum :: < u64 > ( ) ;
750
756
#[ cfg( debug_assertions) ]
751
- let total_satisfaction_weight =
752
- coin_selection. confirmed_utxos . iter ( ) . map ( |utxo| utxo. satisfaction_weight ) . sum :: < u64 > ( ) +
753
- ANCHOR_INPUT_WITNESS_WEIGHT + EMPTY_SCRIPT_SIG_WEIGHT ;
757
+ let total_input_amount = must_spend_amount +
758
+ coin_selection. confirmed_utxos . iter ( ) . map ( |utxo| utxo. output . value ) . sum :: < u64 > ( ) ;
754
759
755
760
self . process_coin_selection ( & mut anchor_tx, coin_selection) ;
756
761
let anchor_txid = anchor_tx. txid ( ) ;
@@ -773,6 +778,16 @@ where
773
778
// never underestimate.
774
779
assert ! ( expected_signed_tx_weight >= signed_tx_weight &&
775
780
expected_signed_tx_weight - ( expected_signed_tx_weight / 100 ) <= signed_tx_weight) ;
781
+
782
+ let expected_package_fee = fee_for_weight ( package_target_feerate_sat_per_1000_weight,
783
+ signed_tx_weight + commitment_tx. weight ( ) as u64 ) ;
784
+ let package_fee = total_input_amount -
785
+ anchor_tx. output . iter ( ) . map ( |output| output. value ) . sum :: < u64 > ( ) ;
786
+ // Our fee should be within a 5% error margin of the expected fee based on the
787
+ // feerate and transaction weight and we should never pay less than required.
788
+ let fee_error_margin = expected_package_fee * 5 / 100 ;
789
+ assert ! ( package_fee >= expected_package_fee &&
790
+ package_fee - fee_error_margin <= expected_package_fee) ;
776
791
}
777
792
778
793
log_info ! ( self . logger, "Broadcasting anchor transaction {} to bump channel close with txid {}" ,
@@ -812,16 +827,24 @@ where
812
827
813
828
log_debug ! ( self . logger, "Peforming coin selection for HTLC transaction targeting {} sat/kW" ,
814
829
target_feerate_sat_per_1000_weight) ;
830
+
815
831
#[ cfg( debug_assertions) ]
816
832
let must_spend_satisfaction_weight =
817
833
must_spend. iter ( ) . map ( |input| input. satisfaction_weight ) . sum :: < u64 > ( ) ;
834
+ #[ cfg( debug_assertions) ]
835
+ let must_spend_amount = must_spend. iter ( ) . map ( |input| input. previous_utxo . value ) . sum :: < u64 > ( ) ;
836
+
818
837
let coin_selection = self . utxo_source . select_confirmed_utxos (
819
838
claim_id, must_spend, & htlc_tx. output , target_feerate_sat_per_1000_weight,
820
839
) ?;
840
+
841
+ #[ cfg( debug_assertions) ]
842
+ let total_satisfaction_weight = must_spend_satisfaction_weight +
843
+ coin_selection. confirmed_utxos . iter ( ) . map ( |utxo| utxo. satisfaction_weight ) . sum :: < u64 > ( ) ;
821
844
#[ cfg( debug_assertions) ]
822
- let total_satisfaction_weight =
823
- coin_selection. confirmed_utxos . iter ( ) . map ( |utxo| utxo. satisfaction_weight ) . sum :: < u64 > ( ) +
824
- must_spend_satisfaction_weight ;
845
+ let total_input_amount = must_spend_amount +
846
+ coin_selection. confirmed_utxos . iter ( ) . map ( |utxo| utxo. output . value ) . sum :: < u64 > ( ) ;
847
+
825
848
self . process_coin_selection ( & mut htlc_tx, coin_selection) ;
826
849
827
850
#[ cfg( debug_assertions) ]
@@ -846,6 +869,15 @@ where
846
869
// never underestimate.
847
870
assert ! ( expected_signed_tx_weight >= signed_tx_weight &&
848
871
expected_signed_tx_weight - ( expected_signed_tx_weight / 100 ) <= signed_tx_weight) ;
872
+
873
+ let expected_signed_tx_fee = fee_for_weight ( target_feerate_sat_per_1000_weight, signed_tx_weight) ;
874
+ let signed_tx_fee = total_input_amount -
875
+ htlc_tx. output . iter ( ) . map ( |output| output. value ) . sum :: < u64 > ( ) ;
876
+ // Our fee should be within a 5% error margin of the expected fee based on the
877
+ // feerate and transaction weight and we should never pay less than required.
878
+ let fee_error_margin = expected_signed_tx_fee * 5 / 100 ;
879
+ assert ! ( signed_tx_fee >= expected_signed_tx_fee &&
880
+ signed_tx_fee - fee_error_margin <= expected_signed_tx_fee) ;
849
881
}
850
882
851
883
log_info ! ( self . logger, "Broadcasting {}" , log_tx!( htlc_tx) ) ;
0 commit comments