Skip to content

Commit 1b70e6d

Browse files
committed
ln/refactor: add utility to get commitment feerate by channel type
1 parent 82519a2 commit 1b70e6d

File tree

3 files changed

+29
-45
lines changed

3 files changed

+29
-45
lines changed

lightning/src/ln/chan_utils.rs

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,9 @@ use bitcoin::hashes::ripemd160::Hash as Ripemd160;
2525
use bitcoin::hashes::sha256::Hash as Sha256;
2626
use bitcoin::hashes::{Hash, HashEngine};
2727

28-
use crate::chain::chaininterface::fee_for_weight;
28+
use crate::chain::chaininterface::{
29+
fee_for_weight, ConfirmationTarget, FeeEstimator, LowerBoundedFeeEstimator,
30+
};
2931
use crate::chain::package::WEIGHT_REVOKED_OUTPUT;
3032
use crate::ln::msgs::DecodeError;
3133
use crate::sign::EntropySource;
@@ -244,6 +246,22 @@ pub(crate) fn htlc_tx_fees_sat(feerate_per_kw: u32, num_accepted_htlcs: usize, n
244246
htlc_tx_fees_sat
245247
}
246248

249+
/// Returns a fee estimate for the commitment transaction depending on channel type.
250+
pub(super) fn commitment_sat_per_1000_weight_for_type<'a, F: Deref>(
251+
fee_estimator: &'a LowerBoundedFeeEstimator<F>, channel_type: &ChannelTypeFeatures,
252+
) -> u32
253+
where
254+
F::Target: FeeEstimator,
255+
{
256+
if channel_type.supports_anchor_zero_fee_commitments() {
257+
0
258+
} else if channel_type.supports_anchors_zero_fee_htlc_tx() {
259+
fee_estimator.bounded_sat_per_1000_weight(ConfirmationTarget::AnchorChannelFee)
260+
} else {
261+
fee_estimator.bounded_sat_per_1000_weight(ConfirmationTarget::NonAnchorChannelFee)
262+
}
263+
}
264+
247265
// Various functions for key derivation and transaction creation for use within channels. Primarily
248266
// used in Channel and ChannelMonitor.
249267

lightning/src/ln/channel.rs

Lines changed: 7 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -41,10 +41,11 @@ use crate::ln::chan_utils;
4141
#[cfg(splicing)]
4242
use crate::ln::chan_utils::FUNDING_TRANSACTION_WITNESS_WEIGHT;
4343
use crate::ln::chan_utils::{
44-
get_commitment_transaction_number_obscure_factor, htlc_success_tx_weight,
45-
htlc_timeout_tx_weight, max_htlcs, ChannelPublicKeys, ChannelTransactionParameters,
46-
ClosingTransaction, CommitmentTransaction, CounterpartyChannelTransactionParameters,
47-
CounterpartyCommitmentSecrets, HTLCOutputInCommitment, HolderCommitmentTransaction,
44+
commitment_sat_per_1000_weight_for_type, get_commitment_transaction_number_obscure_factor,
45+
htlc_success_tx_weight, htlc_timeout_tx_weight, max_htlcs, ChannelPublicKeys,
46+
ChannelTransactionParameters, ClosingTransaction, CommitmentTransaction,
47+
CounterpartyChannelTransactionParameters, CounterpartyCommitmentSecrets,
48+
HTLCOutputInCommitment, HolderCommitmentTransaction,
4849
};
4950
use crate::ln::channel_state::{
5051
ChannelShutdownState, CounterpartyForwardingInfo, InboundHTLCDetails, InboundHTLCStateDetails,
@@ -3416,16 +3417,7 @@ where
34163417
debug_assert!(!channel_type.supports_any_optional_bits());
34173418
debug_assert!(!channel_type.requires_unknown_bits_from(&channelmanager::provided_channel_type_features(&config)));
34183419

3419-
let commitment_feerate = if channel_type.supports_anchor_zero_fee_commitments() {
3420-
0
3421-
} else {
3422-
let commitment_conf_target = if channel_type.supports_anchors_zero_fee_htlc_tx() {
3423-
ConfirmationTarget::AnchorChannelFee
3424-
} else {
3425-
ConfirmationTarget::NonAnchorChannelFee
3426-
};
3427-
fee_estimator.bounded_sat_per_1000_weight(commitment_conf_target)
3428-
};
3420+
let commitment_feerate = commitment_sat_per_1000_weight_for_type(&fee_estimator, &channel_type);
34293421

34303422
let value_to_self_msat = channel_value_satoshis * 1000 - push_msat;
34313423
let commit_tx_fee_sat = SpecTxBuilder {}.commit_tx_fee_sat(commitment_feerate, MIN_AFFORDABLE_HTLC_COUNT, &channel_type);
@@ -5462,20 +5454,7 @@ where
54625454

54635455
let next_channel_type = get_initial_channel_type(user_config, &eligible_features);
54645456

5465-
// Note that we can't get `anchor_zero_fee_commitments` type here, which requires zero
5466-
// fees, because we downgrade from this channel type first. If there were a superior
5467-
// channel type that downgrades to `anchor_zero_fee_commitments`, we'd need to handle
5468-
// fee setting differently here. If we proceeded to open a `anchor_zero_fee_commitments`
5469-
// channel with non-zero fees, we could produce a non-standard commitment transaction that
5470-
// puts us at risk of losing funds. We would expect our peer to reject such a channel
5471-
// open, but we don't want to rely on their validation.
5472-
assert!(!next_channel_type.supports_anchor_zero_fee_commitments());
5473-
let conf_target = if next_channel_type.supports_anchors_zero_fee_htlc_tx() {
5474-
ConfirmationTarget::AnchorChannelFee
5475-
} else {
5476-
ConfirmationTarget::NonAnchorChannelFee
5477-
};
5478-
self.feerate_per_kw = fee_estimator.bounded_sat_per_1000_weight(conf_target);
5457+
self.feerate_per_kw = commitment_sat_per_1000_weight_for_type(&fee_estimator, &next_channel_type);
54795458
funding.channel_transaction_parameters.channel_type_features = next_channel_type;
54805459

54815460
Ok(())

lightning/src/ln/channelmanager.rs

Lines changed: 3 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,7 @@ use crate::events::{
5656
InboundChannelFunds, PaymentFailureReason, ReplayEvent,
5757
};
5858
use crate::events::{FundingInfo, PaidBolt12Invoice};
59+
use crate::ln::chan_utils::commitment_sat_per_1000_weight_for_type;
5960
// Since this struct is returned in `list_channels` methods, expose it here in case users want to
6061
// construct one themselves.
6162
use crate::ln::channel::PendingV2Channel;
@@ -6988,21 +6989,14 @@ where
69886989
PersistenceNotifierGuard::optionally_notify(self, || {
69896990
let mut should_persist = NotifyOption::SkipPersistNoEvents;
69906991

6991-
let non_anchor_feerate = self.fee_estimator.bounded_sat_per_1000_weight(ConfirmationTarget::NonAnchorChannelFee);
6992-
let anchor_feerate = self.fee_estimator.bounded_sat_per_1000_weight(ConfirmationTarget::AnchorChannelFee);
6993-
69946992
let per_peer_state = self.per_peer_state.read().unwrap();
69956993
for (_cp_id, peer_state_mutex) in per_peer_state.iter() {
69966994
let mut peer_state_lock = peer_state_mutex.lock().unwrap();
69976995
let peer_state = &mut *peer_state_lock;
69986996
for (chan_id, chan) in peer_state.channel_by_id.iter_mut()
69996997
.filter_map(|(chan_id, chan)| chan.as_funded_mut().map(|chan| (chan_id, chan)))
70006998
{
7001-
let new_feerate = if chan.funding.get_channel_type().supports_anchors_zero_fee_htlc_tx() {
7002-
anchor_feerate
7003-
} else {
7004-
non_anchor_feerate
7005-
};
6999+
let new_feerate = commitment_sat_per_1000_weight_for_type(&self.fee_estimator, chan.funding.get_channel_type());
70067000
let chan_needs_persist = self.update_channel_fee(chan_id, chan, new_feerate);
70077001
if chan_needs_persist == NotifyOption::DoPersist { should_persist = NotifyOption::DoPersist; }
70087002
}
@@ -7038,9 +7032,6 @@ where
70387032
PersistenceNotifierGuard::optionally_notify(self, || {
70397033
let mut should_persist = NotifyOption::SkipPersistNoEvents;
70407034

7041-
let non_anchor_feerate = self.fee_estimator.bounded_sat_per_1000_weight(ConfirmationTarget::NonAnchorChannelFee);
7042-
let anchor_feerate = self.fee_estimator.bounded_sat_per_1000_weight(ConfirmationTarget::AnchorChannelFee);
7043-
70447035
let mut handle_errors: Vec<(Result<(), _>, _)> = Vec::new();
70457036
let mut timed_out_mpp_htlcs = Vec::new();
70467037
let mut pending_peers_awaiting_removal = Vec::new();
@@ -7056,11 +7047,7 @@ where
70567047
peer_state.channel_by_id.retain(|chan_id, chan| {
70577048
match chan.as_funded_mut() {
70587049
Some(funded_chan) => {
7059-
let new_feerate = if funded_chan.funding.get_channel_type().supports_anchors_zero_fee_htlc_tx() {
7060-
anchor_feerate
7061-
} else {
7062-
non_anchor_feerate
7063-
};
7050+
let new_feerate = commitment_sat_per_1000_weight_for_type(&self.fee_estimator, funded_chan.funding.get_channel_type());
70647051
let chan_needs_persist = self.update_channel_fee(chan_id, funded_chan, new_feerate);
70657052
if chan_needs_persist == NotifyOption::DoPersist { should_persist = NotifyOption::DoPersist; }
70667053

0 commit comments

Comments
 (0)