Skip to content

Commit 1e34de0

Browse files
authored
Merge pull request #3938 from carlaKC/3789-offchain-followups
#3884 followups
2 parents 1102d5c + d7790d2 commit 1e34de0

File tree

3 files changed

+27
-10
lines changed

3 files changed

+27
-10
lines changed

lightning/src/ln/chan_utils.rs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -260,8 +260,9 @@ pub(crate) fn htlc_tx_fees_sat(feerate_per_kw: u32, num_accepted_htlcs: usize, n
260260
num_accepted_htlcs as u64 * htlc_success_tx_fee_sat + num_offered_htlcs as u64 * htlc_timeout_tx_fee_sat
261261
}
262262

263-
/// Returns a fee estimate for the commitment transaction depending on channel type.
264-
pub(super) fn commitment_sat_per_1000_weight_for_type<F: Deref>(
263+
/// Returns a fee estimate for the commitment transaction that we would ideally like to set,
264+
/// depending on channel type.
265+
pub(super) fn selected_commitment_sat_per_1000_weight<F: Deref>(
265266
fee_estimator: &LowerBoundedFeeEstimator<F>, channel_type: &ChannelTypeFeatures,
266267
) -> u32
267268
where

lightning/src/ln/channel.rs

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -41,8 +41,8 @@ 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-
commitment_sat_per_1000_weight_for_type, get_commitment_transaction_number_obscure_factor,
45-
max_htlcs, second_stage_tx_fees_sat, ChannelPublicKeys, ChannelTransactionParameters,
44+
get_commitment_transaction_number_obscure_factor, max_htlcs, second_stage_tx_fees_sat,
45+
selected_commitment_sat_per_1000_weight, ChannelPublicKeys, ChannelTransactionParameters,
4646
ClosingTransaction, CommitmentTransaction, CounterpartyChannelTransactionParameters,
4747
CounterpartyCommitmentSecrets, HTLCOutputInCommitment, HolderCommitmentTransaction,
4848
};
@@ -3392,7 +3392,9 @@ where
33923392
debug_assert!(!channel_type.supports_any_optional_bits());
33933393
debug_assert!(!channel_type.requires_unknown_bits_from(&channelmanager::provided_channel_type_features(&config)));
33943394

3395-
let commitment_feerate = commitment_sat_per_1000_weight_for_type(&fee_estimator, &channel_type);
3395+
let commitment_feerate = selected_commitment_sat_per_1000_weight(
3396+
&fee_estimator, &channel_type,
3397+
);
33963398

33973399
let value_to_self_msat = channel_value_satoshis * 1000 - push_msat;
33983400
let commit_tx_fee_sat = SpecTxBuilder {}.commit_tx_fee_sat(commitment_feerate, MIN_AFFORDABLE_HTLC_COUNT, &channel_type);
@@ -5042,7 +5044,7 @@ where
50425044
/// Get the commitment tx fee for the local's (i.e. our) next commitment transaction based on the
50435045
/// number of pending HTLCs that are on track to be in our next commitment tx.
50445046
///
5045-
/// Optionally includes the `HTLCCandidate` given by `htlc` and an additional non-dust HTLC if
5047+
/// Includes the `HTLCCandidate` given by `htlc` and an additional non-dust HTLC if
50465048
/// `fee_spike_buffer_htlc` is `Some`.
50475049
///
50485050
/// The first extra HTLC is useful for determining whether we can accept a further HTLC, the
@@ -5460,7 +5462,9 @@ where
54605462

54615463
let next_channel_type = get_initial_channel_type(user_config, &eligible_features);
54625464

5463-
self.feerate_per_kw = commitment_sat_per_1000_weight_for_type(&fee_estimator, &next_channel_type);
5465+
self.feerate_per_kw = selected_commitment_sat_per_1000_weight(
5466+
&fee_estimator, &next_channel_type,
5467+
);
54645468
funding.channel_transaction_parameters.channel_type_features = next_channel_type;
54655469

54665470
Ok(())

lightning/src/ln/channelmanager.rs

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -56,7 +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;
59+
use crate::ln::chan_utils::selected_commitment_sat_per_1000_weight;
6060
// Since this struct is returned in `list_channels` methods, expose it here in case users want to
6161
// construct one themselves.
6262
use crate::ln::channel::PendingV2Channel;
@@ -7166,6 +7166,7 @@ where
71667166
pub fn maybe_update_chan_fees(&self) {
71677167
PersistenceNotifierGuard::optionally_notify(self, || {
71687168
let mut should_persist = NotifyOption::SkipPersistNoEvents;
7169+
let mut feerate_cache = new_hash_map();
71697170

71707171
let per_peer_state = self.per_peer_state.read().unwrap();
71717172
for (_cp_id, peer_state_mutex) in per_peer_state.iter() {
@@ -7174,7 +7175,12 @@ where
71747175
for (chan_id, chan) in peer_state.channel_by_id.iter_mut()
71757176
.filter_map(|(chan_id, chan)| chan.as_funded_mut().map(|chan| (chan_id, chan)))
71767177
{
7177-
let new_feerate = commitment_sat_per_1000_weight_for_type(&self.fee_estimator, chan.funding.get_channel_type());
7178+
let channel_type = chan.funding.get_channel_type();
7179+
let new_feerate = feerate_cache.get(channel_type).copied().or_else(|| {
7180+
let feerate = selected_commitment_sat_per_1000_weight(&self.fee_estimator, &channel_type);
7181+
feerate_cache.insert(channel_type.clone(), feerate);
7182+
Some(feerate)
7183+
}).unwrap();
71787184
let chan_needs_persist = self.update_channel_fee(chan_id, chan, new_feerate);
71797185
if chan_needs_persist == NotifyOption::DoPersist { should_persist = NotifyOption::DoPersist; }
71807186
}
@@ -7212,6 +7218,7 @@ where
72127218
let mut handle_errors: Vec<(Result<(), _>, _)> = Vec::new();
72137219
let mut timed_out_mpp_htlcs = Vec::new();
72147220
let mut pending_peers_awaiting_removal = Vec::new();
7221+
let mut feerate_cache = new_hash_map();
72157222

72167223
{
72177224
let per_peer_state = self.per_peer_state.read().unwrap();
@@ -7223,7 +7230,12 @@ where
72237230
peer_state.channel_by_id.retain(|chan_id, chan| {
72247231
match chan.as_funded_mut() {
72257232
Some(funded_chan) => {
7226-
let new_feerate = commitment_sat_per_1000_weight_for_type(&self.fee_estimator, funded_chan.funding.get_channel_type());
7233+
let channel_type = funded_chan.funding.get_channel_type();
7234+
let new_feerate = feerate_cache.get(channel_type).copied().or_else(|| {
7235+
let feerate = selected_commitment_sat_per_1000_weight(&self.fee_estimator, &channel_type);
7236+
feerate_cache.insert(channel_type.clone(), feerate);
7237+
Some(feerate)
7238+
}).unwrap();
72277239
let chan_needs_persist = self.update_channel_fee(chan_id, funded_chan, new_feerate);
72287240
if chan_needs_persist == NotifyOption::DoPersist { should_persist = NotifyOption::DoPersist; }
72297241

0 commit comments

Comments
 (0)