Skip to content

Commit 6ecacc4

Browse files
committed
ln/refactor: add utility to get commitment feerate by channel type
1 parent 95ca0dc commit 6ecacc4

File tree

3 files changed

+29
-50
lines changed

3 files changed

+29
-50
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<F: Deref>(
251+
fee_estimator: &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,
@@ -3398,16 +3399,7 @@ where
33983399
debug_assert!(!channel_type.supports_any_optional_bits());
33993400
debug_assert!(!channel_type.requires_unknown_bits_from(&channelmanager::provided_channel_type_features(&config)));
34003401

3401-
let commitment_feerate = if channel_type.supports_anchor_zero_fee_commitments() {
3402-
0
3403-
} else {
3404-
let commitment_conf_target = if channel_type.supports_anchors_zero_fee_htlc_tx() {
3405-
ConfirmationTarget::AnchorChannelFee
3406-
} else {
3407-
ConfirmationTarget::NonAnchorChannelFee
3408-
};
3409-
fee_estimator.bounded_sat_per_1000_weight(commitment_conf_target)
3410-
};
3402+
let commitment_feerate = commitment_sat_per_1000_weight_for_type(&fee_estimator, &channel_type);
34113403

34123404
let value_to_self_msat = channel_value_satoshis * 1000 - push_msat;
34133405
let commit_tx_fee_sat = SpecTxBuilder {}.commit_tx_fee_sat(commitment_feerate, MIN_AFFORDABLE_HTLC_COUNT, &channel_type);
@@ -5449,20 +5441,7 @@ where
54495441

54505442
let next_channel_type = get_initial_channel_type(user_config, &eligible_features);
54515443

5452-
// Note that we can't get `anchor_zero_fee_commitments` type here, which requires zero
5453-
// fees, because we downgrade from this channel type first. If there were a superior
5454-
// channel type that downgrades to `anchor_zero_fee_commitments`, we'd need to handle
5455-
// fee setting differently here. If we proceeded to open a `anchor_zero_fee_commitments`
5456-
// channel with non-zero fees, we could produce a non-standard commitment transaction that
5457-
// puts us at risk of losing funds. We would expect our peer to reject such a channel
5458-
// open, but we don't want to rely on their validation.
5459-
assert!(!next_channel_type.supports_anchor_zero_fee_commitments());
5460-
let conf_target = if next_channel_type.supports_anchors_zero_fee_htlc_tx() {
5461-
ConfirmationTarget::AnchorChannelFee
5462-
} else {
5463-
ConfirmationTarget::NonAnchorChannelFee
5464-
};
5465-
self.feerate_per_kw = fee_estimator.bounded_sat_per_1000_weight(conf_target);
5444+
self.feerate_per_kw = commitment_sat_per_1000_weight_for_type(&fee_estimator, &next_channel_type);
54665445
funding.channel_transaction_parameters.channel_type_features = next_channel_type;
54675446

54685447
Ok(())

lightning/src/ln/channelmanager.rs

Lines changed: 3 additions & 21 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;
@@ -7216,22 +7217,14 @@ where
72167217
PersistenceNotifierGuard::optionally_notify(self, || {
72177218
let mut should_persist = NotifyOption::SkipPersistNoEvents;
72187219

7219-
let non_anchor_feerate = self.fee_estimator.bounded_sat_per_1000_weight(ConfirmationTarget::NonAnchorChannelFee);
7220-
let anchor_feerate = self.fee_estimator.bounded_sat_per_1000_weight(ConfirmationTarget::AnchorChannelFee);
7221-
72227220
let per_peer_state = self.per_peer_state.read().unwrap();
72237221
for (_cp_id, peer_state_mutex) in per_peer_state.iter() {
72247222
let mut peer_state_lock = peer_state_mutex.lock().unwrap();
72257223
let peer_state = &mut *peer_state_lock;
72267224
for (chan_id, chan) in peer_state.channel_by_id.iter_mut()
72277225
.filter_map(|(chan_id, chan)| chan.as_funded_mut().map(|chan| (chan_id, chan)))
72287226
{
7229-
let is_anchors_chan = chan.funding.get_channel_type().supports_anchors_zero_fee_htlc_tx();
7230-
let new_feerate = if is_anchors_chan {
7231-
anchor_feerate
7232-
} else {
7233-
non_anchor_feerate
7234-
};
7227+
let new_feerate = commitment_sat_per_1000_weight_for_type(&self.fee_estimator, chan.funding.get_channel_type());
72357228
let chan_needs_persist = self.update_channel_fee(chan_id, chan, new_feerate);
72367229
if chan_needs_persist == NotifyOption::DoPersist { should_persist = NotifyOption::DoPersist; }
72377230
}
@@ -7266,13 +7259,6 @@ where
72667259
PersistenceNotifierGuard::optionally_notify(self, || {
72677260
let mut should_persist = NotifyOption::SkipPersistNoEvents;
72687261

7269-
let non_anchor_feerate = self
7270-
.fee_estimator
7271-
.bounded_sat_per_1000_weight(ConfirmationTarget::NonAnchorChannelFee);
7272-
let anchor_feerate = self
7273-
.fee_estimator
7274-
.bounded_sat_per_1000_weight(ConfirmationTarget::AnchorChannelFee);
7275-
72767262
let mut handle_errors: Vec<(Result<(), _>, _)> = Vec::new();
72777263
let mut timed_out_mpp_htlcs = Vec::new();
72787264
let mut pending_peers_awaiting_removal = Vec::new();
@@ -7287,11 +7273,7 @@ where
72877273
peer_state.channel_by_id.retain(|chan_id, chan| {
72887274
match chan.as_funded_mut() {
72897275
Some(funded_chan) => {
7290-
let new_feerate = if funded_chan.funding.get_channel_type().supports_anchors_zero_fee_htlc_tx() {
7291-
anchor_feerate
7292-
} else {
7293-
non_anchor_feerate
7294-
};
7276+
let new_feerate = commitment_sat_per_1000_weight_for_type(&self.fee_estimator, funded_chan.funding.get_channel_type());
72957277
let chan_needs_persist = self.update_channel_fee(chan_id, funded_chan, new_feerate);
72967278
if chan_needs_persist == NotifyOption::DoPersist { should_persist = NotifyOption::DoPersist; }
72977279

0 commit comments

Comments
 (0)