@@ -45,7 +45,7 @@ use crate::ln::chan_utils::{
45
45
HolderCommitmentTransaction, ChannelTransactionParameters,
46
46
CounterpartyChannelTransactionParameters, max_htlcs,
47
47
get_commitment_transaction_number_obscure_factor,
48
- ClosingTransaction, commit_tx_fee_sat,
48
+ ClosingTransaction,
49
49
};
50
50
#[cfg(splicing)]
51
51
use crate::ln::chan_utils::FUNDING_TRANSACTION_WITNESS_WEIGHT;
@@ -3865,14 +3865,14 @@ impl<SP: Deref> ChannelContext<SP> where SP::Target: SignerProvider {
3865
3865
/// Builds stats on a potential commitment transaction build, without actually building the
3866
3866
/// commitment transaction. See `build_commitment_transaction` for further docs.
3867
3867
#[inline]
3868
- fn build_commitment_stats(&self, funding: &FundingScope, local: bool, generated_by_local: bool) -> CommitmentStats {
3868
+ fn build_commitment_stats(&self, funding: &FundingScope, local: bool, generated_by_local: bool, feerate_per_kw: Option<u32>, fee_buffer_nondust_htlcs: Option<usize> ) -> CommitmentStats {
3869
3869
let broadcaster_dust_limit_sat = if local { self.holder_dust_limit_satoshis } else { self.counterparty_dust_limit_satoshis };
3870
3870
let mut nondust_htlc_count = 0;
3871
3871
let mut remote_htlc_total_msat = 0;
3872
3872
let mut local_htlc_total_msat = 0;
3873
3873
let mut value_to_self_msat_offset = 0;
3874
3874
3875
- let feerate_per_kw = self.get_commitment_feerate(funding, generated_by_local);
3875
+ let feerate_per_kw = feerate_per_kw.unwrap_or_else(|| self.get_commitment_feerate(funding, generated_by_local) );
3876
3876
3877
3877
for htlc in self.pending_inbound_htlcs.iter() {
3878
3878
if htlc.state.included_in_commitment(generated_by_local) {
@@ -3935,7 +3935,7 @@ impl<SP: Deref> ChannelContext<SP> where SP::Target: SignerProvider {
3935
3935
);
3936
3936
3937
3937
CommitmentStats {
3938
- total_fee_sat: builder.commit_tx_fee_sat(feerate_per_kw, nondust_htlc_count, &funding.channel_transaction_parameters.channel_type_features),
3938
+ total_fee_sat: builder.commit_tx_fee_sat(feerate_per_kw, nondust_htlc_count + fee_buffer_nondust_htlcs.unwrap_or(0) , &funding.channel_transaction_parameters.channel_type_features),
3939
3939
local_balance_before_fee_msat,
3940
3940
remote_balance_before_fee_msat,
3941
3941
}
@@ -3961,7 +3961,7 @@ impl<SP: Deref> ChannelContext<SP> where SP::Target: SignerProvider {
3961
3961
let broadcaster_dust_limit_sat = if local { self.holder_dust_limit_satoshis } else { self.counterparty_dust_limit_satoshis };
3962
3962
let feerate_per_kw = self.get_commitment_feerate(funding, generated_by_local);
3963
3963
3964
- let stats = self.build_commitment_stats(funding, local, generated_by_local);
3964
+ let stats = self.build_commitment_stats(funding, local, generated_by_local, None, None );
3965
3965
let CommitmentStats {
3966
3966
total_fee_sat,
3967
3967
local_balance_before_fee_msat,
@@ -6656,13 +6656,9 @@ impl<SP: Deref> FundedChannel<SP> where
6656
6656
// Before proposing a feerate update, check that we can actually afford the new fee.
6657
6657
let dust_exposure_limiting_feerate = self.context.get_dust_exposure_limiting_feerate(&fee_estimator);
6658
6658
let htlc_stats = self.context.get_pending_htlc_stats(&self.funding, Some(feerate_per_kw), dust_exposure_limiting_feerate);
6659
- let commitment_data = self.context.build_commitment_transaction(
6660
- &self.funding, self.holder_commitment_point.transaction_number(),
6661
- &self.holder_commitment_point.current_point(), true, true, logger,
6662
- );
6663
- let buffer_fee_msat = commit_tx_fee_sat(feerate_per_kw, commitment_data.tx.nondust_htlcs().len() + htlc_stats.on_holder_tx_outbound_holding_cell_htlcs_count as usize + CONCURRENT_INBOUND_HTLC_FEE_BUFFER as usize, self.funding.get_channel_type()) * 1000;
6664
- let holder_balance_msat = commitment_data.stats.local_balance_before_fee_msat - htlc_stats.outbound_holding_cell_msat;
6665
- if holder_balance_msat < buffer_fee_msat + self.funding.counterparty_selected_channel_reserve_satoshis.unwrap() * 1000 {
6659
+ let stats = self.context.build_commitment_stats(&self.funding, true, true, Some(feerate_per_kw), Some(htlc_stats.on_holder_tx_outbound_holding_cell_htlcs_count as usize + CONCURRENT_INBOUND_HTLC_FEE_BUFFER as usize));
6660
+ let holder_balance_msat = stats.local_balance_before_fee_msat - htlc_stats.outbound_holding_cell_msat;
6661
+ if holder_balance_msat < stats.total_fee_sat * 1000 + self.funding.counterparty_selected_channel_reserve_satoshis.unwrap() * 1000 {
6666
6662
//TODO: auto-close after a number of failures?
6667
6663
log_debug!(logger, "Cannot afford to send new feerate at {}", feerate_per_kw);
6668
6664
return None;
@@ -11614,13 +11610,13 @@ mod tests {
11614
11610
use crate::ln::channel_keys::{RevocationKey, RevocationBasepoint};
11615
11611
use crate::ln::channelmanager::{self, HTLCSource, PaymentId};
11616
11612
use crate::ln::channel::InitFeatures;
11617
- use crate::ln::channel::{AwaitingChannelReadyFlags, ChannelState, FundedChannel, InboundHTLCOutput, OutboundV1Channel, InboundV1Channel, OutboundHTLCOutput, InboundHTLCState, OutboundHTLCState, HTLCCandidate, HTLCInitiator, HTLCUpdateAwaitingACK, commit_tx_fee_sat };
11613
+ use crate::ln::channel::{AwaitingChannelReadyFlags, ChannelState, FundedChannel, InboundHTLCOutput, OutboundV1Channel, InboundV1Channel, OutboundHTLCOutput, InboundHTLCState, OutboundHTLCState, HTLCCandidate, HTLCInitiator, HTLCUpdateAwaitingACK};
11618
11614
use crate::ln::channel::{MAX_FUNDING_SATOSHIS_NO_WUMBO, TOTAL_BITCOIN_SUPPLY_SATOSHIS, MIN_THEIR_CHAN_RESERVE_SATOSHIS};
11619
11615
use crate::types::features::{ChannelFeatures, ChannelTypeFeatures, NodeFeatures};
11620
11616
use crate::ln::msgs;
11621
11617
use crate::ln::msgs::{ChannelUpdate, UnsignedChannelUpdate, MAX_VALUE_MSAT};
11622
11618
use crate::ln::script::ShutdownScript;
11623
- use crate::ln::chan_utils::{self, htlc_success_tx_weight, htlc_timeout_tx_weight};
11619
+ use crate::ln::chan_utils::{self, commit_tx_fee_sat, htlc_success_tx_weight, htlc_timeout_tx_weight};
11624
11620
use crate::chain::BestBlock;
11625
11621
use crate::chain::chaininterface::{FeeEstimator, LowerBoundedFeeEstimator, ConfirmationTarget};
11626
11622
use crate::sign::{ChannelSigner, InMemorySigner, EntropySource, SignerProvider};
0 commit comments