Skip to content

Commit 137346c

Browse files
fixup! Don't include below-dust inbound HTLCs in commit tx fee calculation
1 parent 3b6000d commit 137346c

File tree

1 file changed

+45
-2
lines changed

1 file changed

+45
-2
lines changed

lightning/src/ln/channel.rs

Lines changed: 45 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4653,7 +4653,7 @@ mod tests {
46534653
use ln::features::InitFeatures;
46544654
use ln::msgs::{OptionalField, DataLossProtect, DecodeError};
46554655
use ln::chan_utils;
4656-
use ln::chan_utils::{ChannelPublicKeys, HolderCommitmentTransaction, CounterpartyChannelTransactionParameters};
4656+
use ln::chan_utils::{ChannelPublicKeys, HolderCommitmentTransaction, CounterpartyChannelTransactionParameters, HTLC_SUCCESS_TX_WEIGHT, HTLC_TIMEOUT_TX_WEIGHT};
46574657
use chain::chaininterface::{FeeEstimator,ConfirmationTarget};
46584658
use chain::keysinterface::{InMemoryChannelKeys, KeysInterface};
46594659
use chain::transaction::OutPoint;
@@ -4737,7 +4737,7 @@ mod tests {
47374737
}
47384738

47394739
#[test]
4740-
fn test_commit_tx_fees_with_dust() {
4740+
fn test_holder_vs_counterparty_dust_limit() {
47414741
// Test that when calculating the local and remote commitment transaction fees, the correct
47424742
// dust limits are used.
47434743
let feeest = TestFeeEstimator{fee_est: 15000};
@@ -4803,6 +4803,49 @@ mod tests {
48034803
assert_eq!(remote_commit_tx_fee, remote_commit_fee_3_htlcs);
48044804
}
48054805

4806+
#[test]
4807+
fn test_timeout_vs_success_htlc_dust_limit() {
4808+
// Make sure that when `next_remote_commit_tx_fee_msat` and `next_local_commit_tx_fee_msat`
4809+
// calculate the real dust limits for HTLCs (i.e. the dust limit given by the counterparty
4810+
// *plus* the fees paid for the HTLC) they don't swap `HTLC_SUCCESS_TX_WEIGHT` for
4811+
// `HTLC_TIMEOUT_TX_WEIGHT`, and vice versa.
4812+
let fee_est = TestFeeEstimator{fee_est: 253 };
4813+
let secp_ctx = Secp256k1::new();
4814+
let seed = [42; 32];
4815+
let network = Network::Testnet;
4816+
let keys_provider = test_utils::TestKeysInterface::new(&seed, network);
4817+
4818+
let node_id = PublicKey::from_secret_key(&secp_ctx, &SecretKey::from_slice(&[42; 32]).unwrap());
4819+
let config = UserConfig::default();
4820+
let mut chan = Channel::<EnforcingChannelKeys>::new_outbound(&&fee_est, &&keys_provider, node_id, 10000000, 100000, 42, &config).unwrap();
4821+
4822+
let commitment_tx_fee_0_htlcs = chan.commit_tx_fee_msat(0);
4823+
let commitment_tx_fee_1_htlc = chan.commit_tx_fee_msat(1);
4824+
4825+
// If HTLC_SUCCESS_TX_WEIGHT and HTLC_TIMEOUT_TX_WEIGHT were swapped: then this HTLC would be
4826+
// counted as dust when it shouldn't be.
4827+
let htlc_amt_above_timeout = ((253 * HTLC_TIMEOUT_TX_WEIGHT / 1000) + chan.holder_dust_limit_satoshis + 1) * 1000;
4828+
let commitment_tx_fee = chan.next_local_commit_tx_fee_msat(htlc_amt_above_timeout, true, None);
4829+
assert_eq!(commitment_tx_fee, commitment_tx_fee_1_htlc);
4830+
4831+
// If swapped: this HTLC would be counted as non-dust when it shouldn't be.
4832+
let dust_htlc_amt_below_success = ((253 * HTLC_SUCCESS_TX_WEIGHT / 1000) + chan.holder_dust_limit_satoshis - 1) * 1000;
4833+
let commitment_tx_fee = chan.next_local_commit_tx_fee_msat(dust_htlc_amt_below_success, false, None);
4834+
assert_eq!(commitment_tx_fee, commitment_tx_fee_0_htlcs);
4835+
4836+
chan.channel_transaction_parameters.is_outbound_from_holder = false;
4837+
4838+
// If swapped: this HTLC would be counted as non-dust when it shouldn't be.
4839+
let dust_htlc_amt_above_timeout = ((253 * HTLC_TIMEOUT_TX_WEIGHT / 1000) + chan.counterparty_dust_limit_satoshis + 1) * 1000;
4840+
let commitment_tx_fee = chan.next_remote_commit_tx_fee_msat(dust_htlc_amt_above_timeout, true, None);
4841+
assert_eq!(commitment_tx_fee, commitment_tx_fee_0_htlcs);
4842+
4843+
// If swapped: this HTLC would be counted as dust when it shouldn't be.
4844+
let htlc_amt_below_success = ((253 * HTLC_SUCCESS_TX_WEIGHT / 1000) + chan.counterparty_dust_limit_satoshis - 1) * 1000;
4845+
let commitment_tx_fee = chan.next_remote_commit_tx_fee_msat(htlc_amt_below_success, false, None);
4846+
assert_eq!(commitment_tx_fee, commitment_tx_fee_1_htlc);
4847+
}
4848+
48064849
#[test]
48074850
fn channel_reestablish_no_updates() {
48084851
let feeest = TestFeeEstimator{fee_est: 15000};

0 commit comments

Comments
 (0)