Skip to content

Commit baf9731

Browse files
authored
Merge pull request #2415 from wpaulino/update-fee-anchors
Add min mempool estimate for feerate updates on anchor channels
2 parents ba8af22 + 7751cb9 commit baf9731

File tree

4 files changed

+73
-25
lines changed

4 files changed

+73
-25
lines changed

fuzz/src/chanmon_consistency.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -78,7 +78,7 @@ impl FeeEstimator for FuzzEstimator {
7878
// Background feerate which is <= the minimum Normal feerate.
7979
match conf_target {
8080
ConfirmationTarget::HighPriority => MAX_FEE,
81-
ConfirmationTarget::Background => 253,
81+
ConfirmationTarget::Background|ConfirmationTarget::MempoolMinimum => 253,
8282
ConfirmationTarget::Normal => cmp::min(self.ret_val.load(atomic::Ordering::Acquire), MAX_FEE),
8383
}
8484
}

lightning/src/chain/chaininterface.rs

+13-4
Original file line numberDiff line numberDiff line change
@@ -44,15 +44,24 @@ pub trait BroadcasterInterface {
4444
fn broadcast_transactions(&self, txs: &[&Transaction]);
4545
}
4646

47-
/// An enum that represents the speed at which we want a transaction to confirm used for feerate
47+
/// An enum that represents the priority at which we want a transaction to confirm used for feerate
4848
/// estimation.
4949
#[derive(Clone, Copy, Debug, Hash, PartialEq, Eq)]
5050
pub enum ConfirmationTarget {
51-
/// We are happy with this transaction confirming slowly when feerate drops some.
51+
/// We'd like a transaction to confirm in the future, but don't want to commit most of the fees
52+
/// required to do so yet. The remaining fees will come via a Child-Pays-For-Parent (CPFP) fee
53+
/// bump of the transaction.
54+
///
55+
/// The feerate returned should be the absolute minimum feerate required to enter most node
56+
/// mempools across the network. Note that if you are not able to obtain this feerate estimate,
57+
/// you should likely use the furthest-out estimate allowed by your fee estimator.
58+
MempoolMinimum,
59+
/// We are happy with a transaction confirming slowly, at least within a day or so worth of
60+
/// blocks.
5261
Background,
53-
/// We'd like this transaction to confirm without major delay, but 12-18 blocks is fine.
62+
/// We'd like a transaction to confirm without major delayed, i.e., within the next 12-24 blocks.
5463
Normal,
55-
/// We'd like this transaction to confirm in the next few blocks.
64+
/// We'd like a transaction to confirm in the next few blocks.
5665
HighPriority,
5766
}
5867

lightning/src/ln/channel.rs

+44-17
Original file line numberDiff line numberDiff line change
@@ -2045,20 +2045,35 @@ struct CommitmentTxInfoCached {
20452045
}
20462046

20472047
impl<Signer: WriteableEcdsaChannelSigner> Channel<Signer> {
2048-
fn check_remote_fee<F: Deref, L: Deref>(fee_estimator: &LowerBoundedFeeEstimator<F>,
2049-
feerate_per_kw: u32, cur_feerate_per_kw: Option<u32>, logger: &L)
2050-
-> Result<(), ChannelError> where F::Target: FeeEstimator, L::Target: Logger,
2048+
fn check_remote_fee<F: Deref, L: Deref>(
2049+
channel_type: &ChannelTypeFeatures, fee_estimator: &LowerBoundedFeeEstimator<F>,
2050+
feerate_per_kw: u32, cur_feerate_per_kw: Option<u32>, logger: &L
2051+
) -> Result<(), ChannelError> where F::Target: FeeEstimator, L::Target: Logger,
20512052
{
20522053
// We only bound the fee updates on the upper side to prevent completely absurd feerates,
20532054
// always accepting up to 25 sat/vByte or 10x our fee estimator's "High Priority" fee.
20542055
// We generally don't care too much if they set the feerate to something very high, but it
2055-
// could result in the channel being useless due to everything being dust.
2056-
let upper_limit = cmp::max(250 * 25,
2057-
fee_estimator.bounded_sat_per_1000_weight(ConfirmationTarget::HighPriority) as u64 * 10);
2058-
if feerate_per_kw as u64 > upper_limit {
2059-
return Err(ChannelError::Close(format!("Peer's feerate much too high. Actual: {}. Our expected upper limit: {}", feerate_per_kw, upper_limit)));
2060-
}
2061-
let lower_limit = fee_estimator.bounded_sat_per_1000_weight(ConfirmationTarget::Background);
2056+
// could result in the channel being useless due to everything being dust. This doesn't
2057+
// apply to channels supporting anchor outputs since HTLC transactions are pre-signed with a
2058+
// zero fee, so their fee is no longer considered to determine dust limits.
2059+
if !channel_type.supports_anchors_zero_fee_htlc_tx() {
2060+
let upper_limit = cmp::max(250 * 25,
2061+
fee_estimator.bounded_sat_per_1000_weight(ConfirmationTarget::HighPriority) as u64 * 10);
2062+
if feerate_per_kw as u64 > upper_limit {
2063+
return Err(ChannelError::Close(format!("Peer's feerate much too high. Actual: {}. Our expected upper limit: {}", feerate_per_kw, upper_limit)));
2064+
}
2065+
}
2066+
2067+
// We can afford to use a lower bound with anchors than previously since we can now bump
2068+
// fees when broadcasting our commitment. However, we must still make sure we meet the
2069+
// minimum mempool feerate, until package relay is deployed, such that we can ensure the
2070+
// commitment transaction propagates throughout node mempools on its own.
2071+
let lower_limit_conf_target = if channel_type.supports_anchors_zero_fee_htlc_tx() {
2072+
ConfirmationTarget::MempoolMinimum
2073+
} else {
2074+
ConfirmationTarget::Background
2075+
};
2076+
let lower_limit = fee_estimator.bounded_sat_per_1000_weight(lower_limit_conf_target);
20622077
// Some fee estimators round up to the next full sat/vbyte (ie 250 sats per kw), causing
20632078
// occasional issues with feerate disagreements between an initiator that wants a feerate
20642079
// of 1.1 sat/vbyte and a receiver that wants 1.1 rounded up to 2. Thus, we always add 250
@@ -3688,7 +3703,7 @@ impl<Signer: WriteableEcdsaChannelSigner> Channel<Signer> {
36883703
if self.context.channel_state & (ChannelState::PeerDisconnected as u32) == ChannelState::PeerDisconnected as u32 {
36893704
return Err(ChannelError::Close("Peer sent update_fee when we needed a channel_reestablish".to_owned()));
36903705
}
3691-
Channel::<Signer>::check_remote_fee(fee_estimator, msg.feerate_per_kw, Some(self.context.feerate_per_kw), logger)?;
3706+
Channel::<Signer>::check_remote_fee(&self.context.channel_type, fee_estimator, msg.feerate_per_kw, Some(self.context.feerate_per_kw), logger)?;
36923707
let feerate_over_dust_buffer = msg.feerate_per_kw > self.context.get_dust_buffer_feerate(None);
36933708

36943709
self.context.pending_update_fee = Some((msg.feerate_per_kw, FeeUpdateState::RemoteAnnounced));
@@ -5502,10 +5517,15 @@ impl<Signer: WriteableEcdsaChannelSigner> OutboundV1Channel<Signer> {
55025517
let channel_type = Self::get_initial_channel_type(&config, their_features);
55035518
debug_assert!(channel_type.is_subset(&channelmanager::provided_channel_type_features(&config)));
55045519

5505-
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);
55065526

55075527
let value_to_self_msat = channel_value_satoshis * 1000 - push_msat;
5508-
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);
55095529
if value_to_self_msat < commitment_tx_fee {
55105530
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) });
55115531
}
@@ -5599,7 +5619,7 @@ impl<Signer: WriteableEcdsaChannelSigner> OutboundV1Channel<Signer> {
55995619
short_channel_id: None,
56005620
channel_creation_height: current_chain_height,
56015621

5602-
feerate_per_kw: feerate,
5622+
feerate_per_kw: commitment_feerate,
56035623
counterparty_dust_limit_satoshis: 0,
56045624
holder_dust_limit_satoshis: MIN_CHAN_DUST_LIMIT_SATOSHIS,
56055625
counterparty_max_htlc_value_in_flight_msat: 0,
@@ -5753,7 +5773,12 @@ impl<Signer: WriteableEcdsaChannelSigner> OutboundV1Channel<Signer> {
57535773
/// If we receive an error message, it may only be a rejection of the channel type we tried,
57545774
/// not of our ability to open any channel at all. Thus, on error, we should first call this
57555775
/// and see if we get a new `OpenChannel` message, otherwise the channel is failed.
5756-
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+
{
57575782
if !self.context.is_outbound() || self.context.channel_state != ChannelState::OurInitSent as u32 { return Err(()); }
57585783
if self.context.channel_type == ChannelTypeFeatures::only_static_remote_key() {
57595784
// We've exhausted our options
@@ -5770,6 +5795,7 @@ impl<Signer: WriteableEcdsaChannelSigner> OutboundV1Channel<Signer> {
57705795
// whatever reason.
57715796
if self.context.channel_type.supports_anchors_zero_fee_htlc_tx() {
57725797
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);
57735799
assert!(!self.context.channel_transaction_parameters.channel_type_features.supports_anchors_nonzero_fee_htlc_tx());
57745800
} else if self.context.channel_type.supports_scid_privacy() {
57755801
self.context.channel_type.clear_scid_privacy();
@@ -6039,7 +6065,7 @@ impl<Signer: WriteableEcdsaChannelSigner> InboundV1Channel<Signer> {
60396065
if msg.htlc_minimum_msat >= full_channel_value_msat {
60406066
return Err(ChannelError::Close(format!("Minimum htlc value ({}) was larger than full channel value ({})", msg.htlc_minimum_msat, full_channel_value_msat)));
60416067
}
6042-
Channel::<Signer>::check_remote_fee(fee_estimator, msg.feerate_per_kw, None, logger)?;
6068+
Channel::<Signer>::check_remote_fee(&channel_type, fee_estimator, msg.feerate_per_kw, None, logger)?;
60436069

60446070
let max_counterparty_selected_contest_delay = u16::min(config.channel_handshake_limits.their_to_self_delay, MAX_LOCAL_BREAKDOWN_TIMEOUT);
60456071
if msg.to_self_delay > max_counterparty_selected_contest_delay {
@@ -7441,7 +7467,8 @@ mod tests {
74417467
// arithmetic, causing a panic with debug assertions enabled.
74427468
let fee_est = TestFeeEstimator { fee_est: 42 };
74437469
let bounded_fee_estimator = LowerBoundedFeeEstimator::new(&fee_est);
7444-
assert!(Channel::<InMemorySigner>::check_remote_fee(&bounded_fee_estimator,
7470+
assert!(Channel::<InMemorySigner>::check_remote_fee(
7471+
&ChannelTypeFeatures::only_static_remote_key(), &bounded_fee_estimator,
74457472
u32::max_value(), None, &&test_utils::TestLogger::new()).is_err());
74467473
}
74477474

lightning/src/ln/channelmanager.rs

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

4261-
let new_feerate = self.fee_estimator.bounded_sat_per_1000_weight(ConfirmationTarget::Normal);
4261+
let normal_feerate = self.fee_estimator.bounded_sat_per_1000_weight(ConfirmationTarget::Normal);
4262+
let min_mempool_feerate = self.fee_estimator.bounded_sat_per_1000_weight(ConfirmationTarget::MempoolMinimum);
42624263

42634264
let per_peer_state = self.per_peer_state.read().unwrap();
42644265
for (_cp_id, peer_state_mutex) in per_peer_state.iter() {
42654266
let mut peer_state_lock = peer_state_mutex.lock().unwrap();
42664267
let peer_state = &mut *peer_state_lock;
42674268
for (chan_id, chan) in peer_state.channel_by_id.iter_mut() {
4269+
let new_feerate = if chan.context.get_channel_type().supports_anchors_zero_fee_htlc_tx() {
4270+
min_mempool_feerate
4271+
} else {
4272+
normal_feerate
4273+
};
42684274
let chan_needs_persist = self.update_channel_fee(chan_id, chan, new_feerate);
42694275
if chan_needs_persist == NotifyOption::DoPersist { should_persist = NotifyOption::DoPersist; }
42704276
}
@@ -4294,7 +4300,8 @@ where
42944300
PersistenceNotifierGuard::optionally_notify(&self.total_consistency_lock, &self.persistence_notifier, || {
42954301
let mut should_persist = self.process_background_events();
42964302

4297-
let new_feerate = self.fee_estimator.bounded_sat_per_1000_weight(ConfirmationTarget::Normal);
4303+
let normal_feerate = self.fee_estimator.bounded_sat_per_1000_weight(ConfirmationTarget::Normal);
4304+
let min_mempool_feerate = self.fee_estimator.bounded_sat_per_1000_weight(ConfirmationTarget::MempoolMinimum);
42984305

42994306
let mut handle_errors: Vec<(Result<(), _>, _)> = Vec::new();
43004307
let mut timed_out_mpp_htlcs = Vec::new();
@@ -4307,6 +4314,11 @@ where
43074314
let pending_msg_events = &mut peer_state.pending_msg_events;
43084315
let counterparty_node_id = *counterparty_node_id;
43094316
peer_state.channel_by_id.retain(|chan_id, chan| {
4317+
let new_feerate = if chan.context.get_channel_type().supports_anchors_zero_fee_htlc_tx() {
4318+
min_mempool_feerate
4319+
} else {
4320+
normal_feerate
4321+
};
43104322
let chan_needs_persist = self.update_channel_fee(chan_id, chan, new_feerate);
43114323
if chan_needs_persist == NotifyOption::DoPersist { should_persist = NotifyOption::DoPersist; }
43124324

@@ -7278,7 +7290,7 @@ where
72787290
let mut peer_state_lock = peer_state_mutex_opt.unwrap().lock().unwrap();
72797291
let peer_state = &mut *peer_state_lock;
72807292
if let Some(chan) = peer_state.outbound_v1_channel_by_id.get_mut(&msg.channel_id) {
7281-
if let Ok(msg) = chan.maybe_handle_error_without_close(self.genesis_hash) {
7293+
if let Ok(msg) = chan.maybe_handle_error_without_close(self.genesis_hash, &self.fee_estimator) {
72827294
peer_state.pending_msg_events.push(events::MessageSendEvent::SendOpenChannel {
72837295
node_id: *counterparty_node_id,
72847296
msg,

0 commit comments

Comments
 (0)