@@ -40,7 +40,7 @@ use crate::ln::chan_utils;
40
40
#[cfg(splicing)]
41
41
use crate::ln::chan_utils::FUNDING_TRANSACTION_WITNESS_WEIGHT;
42
42
use crate::ln::chan_utils::{
43
- commit_tx_fee_sat, get_commitment_transaction_number_obscure_factor, htlc_success_tx_weight,
43
+ get_commitment_transaction_number_obscure_factor, htlc_success_tx_weight,
44
44
htlc_timeout_tx_weight, max_htlcs, ChannelPublicKeys, ChannelTransactionParameters,
45
45
ClosingTransaction, CommitmentTransaction, CounterpartyChannelTransactionParameters,
46
46
CounterpartyCommitmentSecrets, HTLCOutputInCommitment, HolderCommitmentTransaction,
@@ -69,6 +69,7 @@ use crate::ln::script::{self, ShutdownScript};
69
69
use crate::ln::types::ChannelId;
70
70
use crate::routing::gossip::NodeId;
71
71
use crate::sign::ecdsa::EcdsaChannelSigner;
72
+ use crate::sign::tx_builder::{SpecTxBuilder, TxBuilder};
72
73
use crate::sign::{ChannelSigner, EntropySource, NodeSigner, Recipient, SignerProvider};
73
74
use crate::types::features::{ChannelTypeFeatures, InitFeatures};
74
75
use crate::types::payment::{PaymentHash, PaymentPreimage};
@@ -3124,12 +3125,12 @@ where
3124
3125
0
3125
3126
};
3126
3127
let funders_amount_msat = open_channel_fields.funding_satoshis * 1000 - msg_push_msat;
3127
- let commitment_tx_fee = commit_tx_fee_sat(open_channel_fields.commitment_feerate_sat_per_1000_weight, MIN_AFFORDABLE_HTLC_COUNT, &channel_type);
3128
- if (funders_amount_msat / 1000).saturating_sub(anchor_outputs_value) < commitment_tx_fee {
3129
- return Err(ChannelError::close(format!("Funding amount ({} sats) can't even pay fee for initial commitment transaction fee of {} sats.", (funders_amount_msat / 1000).saturating_sub(anchor_outputs_value), commitment_tx_fee )));
3128
+ let commit_tx_fee_sat = SpecTxBuilder {}. commit_tx_fee_sat(open_channel_fields.commitment_feerate_sat_per_1000_weight, MIN_AFFORDABLE_HTLC_COUNT, &channel_type);
3129
+ if (funders_amount_msat / 1000).saturating_sub(anchor_outputs_value) < commit_tx_fee_sat {
3130
+ return Err(ChannelError::close(format!("Funding amount ({} sats) can't even pay fee for initial commitment transaction fee of {} sats.", (funders_amount_msat / 1000).saturating_sub(anchor_outputs_value), commit_tx_fee_sat )));
3130
3131
}
3131
3132
3132
- let to_remote_satoshis = funders_amount_msat / 1000 - commitment_tx_fee - anchor_outputs_value;
3133
+ let to_remote_satoshis = funders_amount_msat / 1000 - commit_tx_fee_sat - anchor_outputs_value;
3133
3134
// While it's reasonable for us to not meet the channel reserve initially (if they don't
3134
3135
// want to push much to us), our counterparty should always have more than our reserve.
3135
3136
if to_remote_satoshis < holder_selected_channel_reserve_satoshis {
@@ -3402,9 +3403,9 @@ where
3402
3403
};
3403
3404
3404
3405
let value_to_self_msat = channel_value_satoshis * 1000 - push_msat;
3405
- let commitment_tx_fee = commit_tx_fee_sat(commitment_feerate, MIN_AFFORDABLE_HTLC_COUNT, &channel_type) * 1000 ;
3406
- if value_to_self_msat.saturating_sub(anchor_outputs_value_msat) < commitment_tx_fee {
3407
- return Err(APIError::APIMisuseError{ err: format!("Funding amount ({}) can't even pay fee for initial commitment transaction fee of {}.", value_to_self_msat / 1000, commitment_tx_fee / 1000 ) });
3406
+ let commit_tx_fee_sat = SpecTxBuilder {}. commit_tx_fee_sat(commitment_feerate, MIN_AFFORDABLE_HTLC_COUNT, &channel_type);
3407
+ if value_to_self_msat.saturating_sub(anchor_outputs_value_msat) < commit_tx_fee_sat * 1000 {
3408
+ return Err(APIError::APIMisuseError{ err: format!("Funding amount ({}) can't even pay fee for initial commitment transaction fee of {}.", value_to_self_msat / 1000, commit_tx_fee_sat ) });
3408
3409
}
3409
3410
3410
3411
let mut secp_ctx = Secp256k1::new();
@@ -4504,7 +4505,7 @@ where
4504
4505
broadcaster_max_commitment_tx_output.1 = cmp::max(broadcaster_max_commitment_tx_output.1, value_to_remote_msat);
4505
4506
}
4506
4507
4507
- let commit_tx_fee_sat = commit_tx_fee_sat(feerate_per_kw, non_dust_htlc_count + fee_buffer_nondust_htlcs.unwrap_or(0), &funding.channel_transaction_parameters.channel_type_features);
4508
+ let commit_tx_fee_sat = SpecTxBuilder {}. commit_tx_fee_sat(feerate_per_kw, non_dust_htlc_count + fee_buffer_nondust_htlcs.unwrap_or(0), &funding.channel_transaction_parameters.channel_type_features);
4508
4509
let total_anchors_sat = if funding.channel_transaction_parameters.channel_type_features.supports_anchors_zero_fee_htlc_tx() { ANCHOR_OUTPUT_VALUE_SATOSHI * 2 } else { 0 };
4509
4510
4510
4511
// We MUST use saturating subs here, as the funder's balance is not guaranteed to be greater
@@ -4753,7 +4754,7 @@ where
4753
4754
{
4754
4755
let counterparty_dust_limit_timeout_sat = htlc_timeout_dust_limit + context.counterparty_dust_limit_satoshis;
4755
4756
let holder_dust_limit_success_sat = htlc_success_dust_limit + context.holder_dust_limit_satoshis;
4756
- for ref htlc in context.pending_inbound_htlcs.iter() {
4757
+ for htlc in context.pending_inbound_htlcs.iter() {
4757
4758
pending_inbound_htlcs_value_msat += htlc.amount_msat;
4758
4759
if htlc.amount_msat / 1000 < counterparty_dust_limit_timeout_sat {
4759
4760
on_counterparty_tx_dust_exposure_msat += htlc.amount_msat;
@@ -4773,7 +4774,7 @@ where
4773
4774
{
4774
4775
let counterparty_dust_limit_success_sat = htlc_success_dust_limit + context.counterparty_dust_limit_satoshis;
4775
4776
let holder_dust_limit_timeout_sat = htlc_timeout_dust_limit + context.holder_dust_limit_satoshis;
4776
- for ref htlc in context.pending_outbound_htlcs.iter() {
4777
+ for htlc in context.pending_outbound_htlcs.iter() {
4777
4778
pending_outbound_htlcs_value_msat += htlc.amount_msat;
4778
4779
if htlc.amount_msat / 1000 < counterparty_dust_limit_success_sat {
4779
4780
on_counterparty_tx_dust_exposure_msat += htlc.amount_msat;
@@ -4810,10 +4811,14 @@ where
4810
4811
.unwrap_or(self.feerate_per_kw)
4811
4812
.checked_sub(dust_exposure_limiting_feerate);
4812
4813
let extra_nondust_htlc_on_counterparty_tx_dust_exposure_msat = excess_feerate_opt.map(|excess_feerate| {
4813
- let extra_htlc_dust_exposure = on_counterparty_tx_dust_exposure_msat
4814
- + chan_utils::commit_and_htlc_tx_fees_sat(excess_feerate, on_counterparty_tx_accepted_nondust_htlcs + 1, on_counterparty_tx_offered_nondust_htlcs, funding.get_channel_type()) * 1000;
4815
- on_counterparty_tx_dust_exposure_msat
4816
- += chan_utils::commit_and_htlc_tx_fees_sat(excess_feerate, on_counterparty_tx_accepted_nondust_htlcs, on_counterparty_tx_offered_nondust_htlcs, funding.get_channel_type()) * 1000;
4814
+ let extra_htlc_commit_tx_fee_sat = SpecTxBuilder {}.commit_tx_fee_sat(excess_feerate, on_counterparty_tx_accepted_nondust_htlcs + 1 + on_counterparty_tx_offered_nondust_htlcs, funding.get_channel_type());
4815
+ let extra_htlc_htlc_tx_fees_sat = chan_utils::htlc_tx_fees_sat(excess_feerate, on_counterparty_tx_accepted_nondust_htlcs + 1, on_counterparty_tx_offered_nondust_htlcs, funding.get_channel_type());
4816
+
4817
+ let commit_tx_fee_sat = SpecTxBuilder {}.commit_tx_fee_sat(excess_feerate, on_counterparty_tx_accepted_nondust_htlcs + on_counterparty_tx_offered_nondust_htlcs, funding.get_channel_type());
4818
+ let htlc_tx_fees_sat = chan_utils::htlc_tx_fees_sat(excess_feerate, on_counterparty_tx_accepted_nondust_htlcs, on_counterparty_tx_offered_nondust_htlcs, funding.get_channel_type());
4819
+
4820
+ let extra_htlc_dust_exposure = on_counterparty_tx_dust_exposure_msat + (extra_htlc_commit_tx_fee_sat + extra_htlc_htlc_tx_fees_sat) * 1000;
4821
+ on_counterparty_tx_dust_exposure_msat += (commit_tx_fee_sat + htlc_tx_fees_sat) * 1000;
4817
4822
extra_htlc_dust_exposure
4818
4823
});
4819
4824
@@ -5158,12 +5163,12 @@ where
5158
5163
}
5159
5164
5160
5165
let num_htlcs = included_htlcs + addl_htlcs;
5161
- let res = commit_tx_fee_sat(context.feerate_per_kw, num_htlcs, funding.get_channel_type()) * 1000;
5166
+ let commit_tx_fee_msat = SpecTxBuilder {}. commit_tx_fee_sat(context.feerate_per_kw, num_htlcs, funding.get_channel_type()) * 1000;
5162
5167
#[cfg(any(test, fuzzing))]
5163
5168
{
5164
- let mut fee = res ;
5169
+ let mut fee = commit_tx_fee_msat ;
5165
5170
if fee_spike_buffer_htlc.is_some() {
5166
- fee = commit_tx_fee_sat(context.feerate_per_kw, num_htlcs - 1, funding.get_channel_type()) * 1000;
5171
+ fee = SpecTxBuilder {}. commit_tx_fee_sat(context.feerate_per_kw, num_htlcs - 1, funding.get_channel_type()) * 1000;
5167
5172
}
5168
5173
let total_pending_htlcs = context.pending_inbound_htlcs.len() + context.pending_outbound_htlcs.len()
5169
5174
+ context.holding_cell_htlc_updates.len();
@@ -5182,7 +5187,7 @@ where
5182
5187
};
5183
5188
*funding.next_local_commitment_tx_fee_info_cached.lock().unwrap() = Some(commitment_tx_info);
5184
5189
}
5185
- res
5190
+ commit_tx_fee_msat
5186
5191
}
5187
5192
5188
5193
/// Get the commitment tx fee for the remote's next commitment transaction based on the number of
@@ -5256,12 +5261,12 @@ where
5256
5261
}
5257
5262
5258
5263
let num_htlcs = included_htlcs + addl_htlcs;
5259
- let res = commit_tx_fee_sat(context.feerate_per_kw, num_htlcs, funding.get_channel_type()) * 1000;
5264
+ let commit_tx_fee_msat = SpecTxBuilder {}. commit_tx_fee_sat(context.feerate_per_kw, num_htlcs, funding.get_channel_type()) * 1000;
5260
5265
#[cfg(any(test, fuzzing))]
5261
5266
if let Some(htlc) = &htlc {
5262
- let mut fee = res ;
5267
+ let mut fee = commit_tx_fee_msat ;
5263
5268
if fee_spike_buffer_htlc.is_some() {
5264
- fee = commit_tx_fee_sat(context.feerate_per_kw, num_htlcs - 1, funding.get_channel_type()) * 1000;
5269
+ fee = SpecTxBuilder {}. commit_tx_fee_sat(context.feerate_per_kw, num_htlcs - 1, funding.get_channel_type()) * 1000;
5265
5270
}
5266
5271
let total_pending_htlcs = context.pending_inbound_htlcs.len() + context.pending_outbound_htlcs.len();
5267
5272
let commitment_tx_info = CommitmentTxInfoCached {
@@ -5279,7 +5284,7 @@ where
5279
5284
};
5280
5285
*funding.next_remote_commitment_tx_fee_info_cached.lock().unwrap() = Some(commitment_tx_info);
5281
5286
}
5282
- res
5287
+ commit_tx_fee_msat
5283
5288
}
5284
5289
5285
5290
#[rustfmt::skip]
@@ -10574,8 +10579,7 @@ where
10574
10579
&& info.next_holder_htlc_id == self.context.next_holder_htlc_id
10575
10580
&& info.next_counterparty_htlc_id == self.context.next_counterparty_htlc_id
10576
10581
&& info.feerate == self.context.feerate_per_kw {
10577
- let actual_fee = commit_tx_fee_sat(self.context.feerate_per_kw, counterparty_commitment_tx.nondust_htlcs().len(), self.funding.get_channel_type()) * 1000;
10578
- assert_eq!(actual_fee, info.fee);
10582
+ assert_eq!(commitment_data.stats.commit_tx_fee_sat, info.fee);
10579
10583
}
10580
10584
}
10581
10585
}
@@ -13194,11 +13198,13 @@ mod tests {
13194
13198
use crate::chain::chaininterface::LowerBoundedFeeEstimator;
13195
13199
use crate::chain::transaction::OutPoint;
13196
13200
use crate::chain::BestBlock;
13197
- use crate::ln::chan_utils::{self, htlc_success_tx_weight, htlc_timeout_tx_weight};
13201
+ use crate::ln::chan_utils::{
13202
+ self, commit_tx_fee_sat, htlc_success_tx_weight, htlc_timeout_tx_weight,
13203
+ };
13198
13204
use crate::ln::channel::{
13199
- commit_tx_fee_sat, AwaitingChannelReadyFlags, ChannelState, FundedChannel, HTLCCandidate,
13200
- HTLCInitiator, HTLCUpdateAwaitingACK, InboundHTLCOutput, InboundHTLCState,
13201
- InboundV1Channel, OutboundHTLCOutput, OutboundHTLCState, OutboundV1Channel,
13205
+ AwaitingChannelReadyFlags, ChannelState, FundedChannel, HTLCCandidate, HTLCInitiator ,
13206
+ HTLCUpdateAwaitingACK, InboundHTLCOutput, InboundHTLCState, InboundV1Channel ,
13207
+ OutboundHTLCOutput, OutboundHTLCState, OutboundV1Channel,
13202
13208
};
13203
13209
use crate::ln::channel::{
13204
13210
MAX_FUNDING_SATOSHIS_NO_WUMBO, MIN_THEIR_CHAN_RESERVE_SATOSHIS,
0 commit comments