Skip to content

Commit 7751cb9

Browse files
committed
Use min mempool feerate for outbound updates on anchor channels
As done with inbound feerate updates, we can afford to commit less in fees, as long as we still may the minimum mempool feerate. This enables users to spend a bit more of their balance, as less funds are being committed to transaction fees.
1 parent 1349ac8 commit 7751cb9

File tree

2 files changed

+30
-7
lines changed

2 files changed

+30
-7
lines changed

lightning/src/ln/channel.rs

+15-4
Original file line numberDiff line numberDiff line change
@@ -5517,10 +5517,15 @@ impl<Signer: WriteableEcdsaChannelSigner> OutboundV1Channel<Signer> {
55175517
let channel_type = Self::get_initial_channel_type(&config, their_features);
55185518
debug_assert!(channel_type.is_subset(&channelmanager::provided_channel_type_features(&config)));
55195519

5520-
let feerate = fee_estimator.bounded_sat_per_1000_weight(ConfirmationTarget::Normal);
5520+
let commitment_conf_target = if channel_type.supports_anchors_zero_fee_htlc_tx() {
5521+
ConfirmationTarget::MempoolMinimum
5522+
} else {
5523+
ConfirmationTarget::Normal
5524+
};
5525+
let commitment_feerate = fee_estimator.bounded_sat_per_1000_weight(commitment_conf_target);
55215526

55225527
let value_to_self_msat = channel_value_satoshis * 1000 - push_msat;
5523-
let commitment_tx_fee = commit_tx_fee_msat(feerate, MIN_AFFORDABLE_HTLC_COUNT, &channel_type);
5528+
let commitment_tx_fee = commit_tx_fee_msat(commitment_feerate, MIN_AFFORDABLE_HTLC_COUNT, &channel_type);
55245529
if value_to_self_msat < commitment_tx_fee {
55255530
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) });
55265531
}
@@ -5614,7 +5619,7 @@ impl<Signer: WriteableEcdsaChannelSigner> OutboundV1Channel<Signer> {
56145619
short_channel_id: None,
56155620
channel_creation_height: current_chain_height,
56165621

5617-
feerate_per_kw: feerate,
5622+
feerate_per_kw: commitment_feerate,
56185623
counterparty_dust_limit_satoshis: 0,
56195624
holder_dust_limit_satoshis: MIN_CHAN_DUST_LIMIT_SATOSHIS,
56205625
counterparty_max_htlc_value_in_flight_msat: 0,
@@ -5768,7 +5773,12 @@ impl<Signer: WriteableEcdsaChannelSigner> OutboundV1Channel<Signer> {
57685773
/// If we receive an error message, it may only be a rejection of the channel type we tried,
57695774
/// not of our ability to open any channel at all. Thus, on error, we should first call this
57705775
/// and see if we get a new `OpenChannel` message, otherwise the channel is failed.
5771-
pub(crate) fn maybe_handle_error_without_close(&mut self, chain_hash: BlockHash) -> Result<msgs::OpenChannel, ()> {
5776+
pub(crate) fn maybe_handle_error_without_close<F: Deref>(
5777+
&mut self, chain_hash: BlockHash, fee_estimator: &LowerBoundedFeeEstimator<F>
5778+
) -> Result<msgs::OpenChannel, ()>
5779+
where
5780+
F::Target: FeeEstimator
5781+
{
57725782
if !self.context.is_outbound() || self.context.channel_state != ChannelState::OurInitSent as u32 { return Err(()); }
57735783
if self.context.channel_type == ChannelTypeFeatures::only_static_remote_key() {
57745784
// We've exhausted our options
@@ -5785,6 +5795,7 @@ impl<Signer: WriteableEcdsaChannelSigner> OutboundV1Channel<Signer> {
57855795
// whatever reason.
57865796
if self.context.channel_type.supports_anchors_zero_fee_htlc_tx() {
57875797
self.context.channel_type.clear_anchors_zero_fee_htlc_tx();
5798+
self.context.feerate_per_kw = fee_estimator.bounded_sat_per_1000_weight(ConfirmationTarget::Normal);
57885799
assert!(!self.context.channel_transaction_parameters.channel_type_features.supports_anchors_nonzero_fee_htlc_tx());
57895800
} else if self.context.channel_type.supports_scid_privacy() {
57905801
self.context.channel_type.clear_scid_privacy();

lightning/src/ln/channelmanager.rs

+15-3
Original file line numberDiff line numberDiff line change
@@ -4235,13 +4235,19 @@ where
42354235
PersistenceNotifierGuard::optionally_notify(&self.total_consistency_lock, &self.persistence_notifier, || {
42364236
let mut should_persist = self.process_background_events();
42374237

4238-
let new_feerate = self.fee_estimator.bounded_sat_per_1000_weight(ConfirmationTarget::Normal);
4238+
let normal_feerate = self.fee_estimator.bounded_sat_per_1000_weight(ConfirmationTarget::Normal);
4239+
let min_mempool_feerate = self.fee_estimator.bounded_sat_per_1000_weight(ConfirmationTarget::MempoolMinimum);
42394240

42404241
let per_peer_state = self.per_peer_state.read().unwrap();
42414242
for (_cp_id, peer_state_mutex) in per_peer_state.iter() {
42424243
let mut peer_state_lock = peer_state_mutex.lock().unwrap();
42434244
let peer_state = &mut *peer_state_lock;
42444245
for (chan_id, chan) in peer_state.channel_by_id.iter_mut() {
4246+
let new_feerate = if chan.context.get_channel_type().supports_anchors_zero_fee_htlc_tx() {
4247+
min_mempool_feerate
4248+
} else {
4249+
normal_feerate
4250+
};
42454251
let chan_needs_persist = self.update_channel_fee(chan_id, chan, new_feerate);
42464252
if chan_needs_persist == NotifyOption::DoPersist { should_persist = NotifyOption::DoPersist; }
42474253
}
@@ -4271,7 +4277,8 @@ where
42714277
PersistenceNotifierGuard::optionally_notify(&self.total_consistency_lock, &self.persistence_notifier, || {
42724278
let mut should_persist = self.process_background_events();
42734279

4274-
let new_feerate = self.fee_estimator.bounded_sat_per_1000_weight(ConfirmationTarget::Normal);
4280+
let normal_feerate = self.fee_estimator.bounded_sat_per_1000_weight(ConfirmationTarget::Normal);
4281+
let min_mempool_feerate = self.fee_estimator.bounded_sat_per_1000_weight(ConfirmationTarget::MempoolMinimum);
42754282

42764283
let mut handle_errors: Vec<(Result<(), _>, _)> = Vec::new();
42774284
let mut timed_out_mpp_htlcs = Vec::new();
@@ -4284,6 +4291,11 @@ where
42844291
let pending_msg_events = &mut peer_state.pending_msg_events;
42854292
let counterparty_node_id = *counterparty_node_id;
42864293
peer_state.channel_by_id.retain(|chan_id, chan| {
4294+
let new_feerate = if chan.context.get_channel_type().supports_anchors_zero_fee_htlc_tx() {
4295+
min_mempool_feerate
4296+
} else {
4297+
normal_feerate
4298+
};
42874299
let chan_needs_persist = self.update_channel_fee(chan_id, chan, new_feerate);
42884300
if chan_needs_persist == NotifyOption::DoPersist { should_persist = NotifyOption::DoPersist; }
42894301

@@ -7250,7 +7262,7 @@ where
72507262
let mut peer_state_lock = peer_state_mutex_opt.unwrap().lock().unwrap();
72517263
let peer_state = &mut *peer_state_lock;
72527264
if let Some(chan) = peer_state.outbound_v1_channel_by_id.get_mut(&msg.channel_id) {
7253-
if let Ok(msg) = chan.maybe_handle_error_without_close(self.genesis_hash) {
7265+
if let Ok(msg) = chan.maybe_handle_error_without_close(self.genesis_hash, &self.fee_estimator) {
72547266
peer_state.pending_msg_events.push(events::MessageSendEvent::SendOpenChannel {
72557267
node_id: *counterparty_node_id,
72567268
msg,

0 commit comments

Comments
 (0)