Skip to content

Commit 2ceab8f

Browse files
author
Antoine Riard
committed
Check the new feerate should be at least a 30% increase before to start timer
1 parent 2e3fd85 commit 2ceab8f

File tree

2 files changed

+16
-7
lines changed

2 files changed

+16
-7
lines changed

lightning/src/ln/channel.rs

+13-5
Original file line numberDiff line numberDiff line change
@@ -2725,7 +2725,14 @@ impl<Signer: Sign> Channel<Signer> {
27252725
}
27262726

27272727
/// Trigger the autoclose timer if it's in the starting position
2728-
pub fn maybe_trigger_autoclose_timer(&mut self) -> bool {
2728+
pub fn maybe_trigger_autoclose_timer(&mut self, current_feerate: u32, new_feerate: u32, background_feerate: u32) -> bool {
2729+
2730+
// Verify the new feerate is at least superior by 30% of current feerate before to
2731+
// start a autoclose timer.
2732+
if new_feerate < current_feerate * 1300 / 1000 && new_feerate < background_feerate {
2733+
return false;
2734+
}
2735+
27292736
// Start an auto-close timer, if the channel feerate doesn't increase before its
27302737
// expiration (i.e this outbound feerate update has been committed on both sides),
27312738
// the channel will be marked as unsafe and force-closed.
@@ -2985,7 +2992,7 @@ impl<Signer: Sign> Channel<Signer> {
29852992
/// Adds a pending update to this channel. See the doc for send_htlc for
29862993
/// further details on the optionness of the return value.
29872994
/// You MUST call send_commitment prior to any other calls on this Channel
2988-
fn send_update_fee(&mut self, feerate_per_kw: u32) -> Option<msgs::UpdateFee> {
2995+
fn send_update_fee(&mut self, feerate_per_kw: u32, background_feerate: u32) -> Option<msgs::UpdateFee> {
29892996
if !self.is_outbound() {
29902997
panic!("Cannot send fee from inbound channel");
29912998
}
@@ -3001,7 +3008,7 @@ impl<Signer: Sign> Channel<Signer> {
30013008
return None;
30023009
}
30033010

3004-
self.maybe_trigger_autoclose_timer();
3011+
self.maybe_trigger_autoclose_timer(self.get_feerate(), feerate_per_kw, background_feerate);
30053012

30063013
debug_assert!(self.pending_update_fee.is_none());
30073014
self.pending_update_fee = Some((feerate_per_kw, FeeUpdateState::Outbound));
@@ -3012,8 +3019,8 @@ impl<Signer: Sign> Channel<Signer> {
30123019
})
30133020
}
30143021

3015-
pub fn send_update_fee_and_commit<L: Deref>(&mut self, feerate_per_kw: u32, logger: &L) -> Result<Option<(msgs::UpdateFee, msgs::CommitmentSigned, ChannelMonitorUpdate)>, ChannelError> where L::Target: Logger {
3016-
match self.send_update_fee(feerate_per_kw) {
3022+
pub fn send_update_fee_and_commit<L: Deref>(&mut self, feerate_per_kw: u32, background_feerate: u32, logger: &L) -> Result<Option<(msgs::UpdateFee, msgs::CommitmentSigned, ChannelMonitorUpdate)>, ChannelError> where L::Target: Logger {
3023+
match self.send_update_fee(feerate_per_kw, background_feerate) {
30173024
Some(update_fee) => {
30183025
let (commitment_signed, monitor_update) = self.send_commitment_no_status_check(logger)?;
30193026
Ok(Some((update_fee, commitment_signed, monitor_update)))
@@ -3164,6 +3171,7 @@ impl<Signer: Sign> Channel<Signer> {
31643171
if self.channel_state & (ChannelState::PeerDisconnected as u32) == ChannelState::PeerDisconnected as u32 {
31653172
return Err(ChannelError::Close("Peer sent update_fee when we needed a channel_reestablish".to_owned()));
31663173
}
3174+
31673175
Channel::<Signer>::check_remote_fee(fee_estimator, msg.feerate_per_kw)?;
31683176
let feerate_over_dust_buffer = msg.feerate_per_kw > self.get_dust_buffer_feerate();
31693177

lightning/src/ln/channelmanager.rs

+3-2
Original file line numberDiff line numberDiff line change
@@ -2828,8 +2828,9 @@ impl<Signer: Sign, M: Deref, T: Deref, K: Deref, F: Deref, L: Deref> ChannelMana
28282828
// If the channel is inbound and we detect a relevant feerate increase, we assume
28292829
// that our counterparty, reasonably implemented, should send us an `update_fee`
28302830
// soon. Monitoring this situation, we start an autoclose timer.
2831+
let background_feerate = self.fee_estimator.get_est_sat_per_1000_weight(ConfirmationTarget::Background);
28312832
if !chan.is_outbound() {
2832-
if chan.maybe_trigger_autoclose_timer() {
2833+
if chan.maybe_trigger_autoclose_timer(chan.get_feerate(), new_feerate, background_feerate) {
28332834
return (true, NotifyOption::DoPersist, Ok(()));
28342835
} else {
28352836
return (true, NotifyOption::SkipPersist, Ok(()));
@@ -2844,7 +2845,7 @@ impl<Signer: Sign, M: Deref, T: Deref, K: Deref, F: Deref, L: Deref> ChannelMana
28442845
log_bytes!(chan_id[..]), chan.get_feerate(), new_feerate);
28452846

28462847
let mut retain_channel = true;
2847-
let res = match chan.send_update_fee_and_commit(new_feerate, &self.logger) {
2848+
let res = match chan.send_update_fee_and_commit(new_feerate, background_feerate, &self.logger) {
28482849
Ok(res) => Ok(res),
28492850
Err(e) => {
28502851
let (drop, res) = convert_chan_err!(self, e, short_to_id, chan, chan_id);

0 commit comments

Comments
 (0)