Skip to content

Commit bf8aa1f

Browse files
committed
add slope test
1 parent 86838a6 commit bf8aa1f

File tree

1 file changed

+22
-1
lines changed

1 file changed

+22
-1
lines changed

lightning/src/ln/chan_utils.rs

Lines changed: 22 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1893,7 +1893,7 @@ pub fn get_commitment_transaction_number_obscure_factor(
18931893
mod 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,25 @@ 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+
// We previously rounded down the per output fee in `per_outbound_htlc_counterparty_commit_tx_fee_msat` causing the discrepancy with (commit_tx_fee_sat/num_htlcs),
2438+
// this test guards against this behavior
2439+
2440+
let feerate = 267; // ~250sat/vb, some feerate that causes a big delta if the per output fee is rounded down, here 267 * 172 / 1000 = 45.924
2441+
let num_htlcs = 966; // BOLT #2 maximum number of htlcs on a commit tx
2442+
2443+
let features = ChannelTypeFeatures::anchors_zero_htlc_fee_and_dependencies();
2444+
let got = commit_tx_fee_sat(feerate, 0, &features)
2445+
+ per_outbound_htlc_counterparty_commit_tx_fee_msat(feerate, &features) * num_htlcs / 1000;
2446+
let want = commit_tx_fee_sat(feerate, num_htlcs as usize, &features);
2447+
assert_eq!(got, want);
2448+
2449+
let features = ChannelTypeFeatures::only_static_remote_key();
2450+
let got = commit_tx_fee_sat(feerate, 0, &features) + num_htlcs * per_outbound_htlc_counterparty_commit_tx_fee_msat(feerate, &features) / 1000;
2451+
let want = commit_tx_fee_sat(feerate, num_htlcs as usize, &features) + num_htlcs * feerate as u64 * htlc_success_tx_weight(&features) / 1000;
2452+
assert_eq!(got, want);
2453+
}
24332454
}

0 commit comments

Comments
 (0)