@@ -1893,7 +1893,7 @@ pub fn get_commitment_transaction_number_obscure_factor(
18931893mod tests {
18941894 use super :: { CounterpartyCommitmentSecrets , ChannelPublicKeys } ;
18951895 use crate :: chain;
1896- use crate :: ln:: chan_utils:: { get_htlc_redeemscript, get_to_countersignatory_with_anchors_redeemscript, CommitmentTransaction , TxCreationKeys , ChannelTransactionParameters , CounterpartyChannelTransactionParameters , HTLCOutputInCommitment } ;
1896+ use crate :: ln:: chan_utils:: { get_htlc_redeemscript, get_to_countersignatory_with_anchors_redeemscript, CommitmentTransaction , TxCreationKeys , ChannelTransactionParameters , CounterpartyChannelTransactionParameters , HTLCOutputInCommitment , commit_tx_fee_sat , htlc_success_tx_weight , per_outbound_htlc_counterparty_commit_tx_fee_msat } ;
18971897 use bitcoin:: secp256k1:: { PublicKey , SecretKey , Secp256k1 } ;
18981898 use crate :: util:: test_utils;
18991899 use crate :: sign:: { ChannelSigner , SignerProvider } ;
@@ -2430,4 +2430,35 @@ mod tests {
24302430 assert ! ( monitor. provide_secret( 281474976710648 , secrets. last( ) . unwrap( ) . clone( ) ) . is_err( ) ) ;
24312431 }
24322432 }
2433+
2434+ #[ test]
2435+ fn test_commit_tx_fee_sat_num_htlcs_slope_per_outbound_htlc_counterparty_commit_tx_fee_msat ( ) {
2436+ // Test that the slope (commit_tx_fee_sat/num_htlcs) is consistent with per_outbound_htlc_counterparty_commit_tx_fee_msat.
2437+ // `got`: floor(feerate * commit_tx_base_weight) + floor(num_htlcs * feerate * htlc_output_weight + num_htlcs * feerate * htlc_tx_weight)
2438+ // `want`: floor(feerate * commit_tx_base_weight + num_htlcs * feerate * htlc_output_weight) + floor(num_htlcs * feerate * htlc_tx_weight)
2439+ // At most, the difference between the two calculations is 1 satoshi, depending on how the num_htlcs * feerate * htlc_output_weight term
2440+ // adds with the two others.
2441+ //
2442+ // In the case of anchor channels, set `htlc_tx_weight` above to zero; the fees are exogenous.
2443+
2444+ let num_htlcs = 966 ; // BOLT #2 maximum number of htlcs on a commit tx
2445+
2446+ let features = ChannelTypeFeatures :: only_static_remote_key ( ) ;
2447+ // Tests the range 1sat/vb - 250sat/vb
2448+ for feerate in 253 ..62500 {
2449+ let got = commit_tx_fee_sat ( feerate, 0 , & features) + num_htlcs * per_outbound_htlc_counterparty_commit_tx_fee_msat ( feerate, & features) / 1000 ;
2450+ let want = commit_tx_fee_sat ( feerate, num_htlcs as usize , & features) + num_htlcs * feerate as u64 * htlc_success_tx_weight ( & features) / 1000 ;
2451+ let diff = u64:: abs_diff ( got, want) ;
2452+ assert ! ( diff <= 1 , "assert failed for feerate: {}, absolute difference was: {}" , feerate, diff) ;
2453+ }
2454+
2455+ let features = ChannelTypeFeatures :: anchors_zero_htlc_fee_and_dependencies ( ) ;
2456+ // Tests the range 1sat/vb - 250sat/vb
2457+ for feerate in 253 ..62500 {
2458+ let got = commit_tx_fee_sat ( feerate, 0 , & features) + num_htlcs * per_outbound_htlc_counterparty_commit_tx_fee_msat ( feerate, & features) / 1000 ;
2459+ let want = commit_tx_fee_sat ( feerate, num_htlcs as usize , & features) ;
2460+ let diff = u64:: checked_sub ( want, got) . expect ( "got should not be greater than want" ) ;
2461+ assert ! ( diff <= 1 , "assert failed for feerate: {}, checked difference was: {}" , feerate, diff) ;
2462+ }
2463+ }
24332464}
0 commit comments