Skip to content

Commit 464df56

Browse files
author
Antoine Riard
committed
-f inbound htlc, autoclose only if pending payment
1 parent 0fc0267 commit 464df56

File tree

2 files changed

+24
-14
lines changed

2 files changed

+24
-14
lines changed

lightning/src/ln/channel.rs

+13-5
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ use ln::features::{ChannelFeatures, ChannelTypeFeatures, InitFeatures};
2727
use ln::msgs;
2828
use ln::msgs::{DecodeError, OptionalField, DataLossProtect};
2929
use ln::script::{self, ShutdownScript};
30-
use ln::channelmanager::{CounterpartyForwardingInfo, PendingHTLCStatus, HTLCSource, HTLCFailReason, HTLCFailureMsg, PendingHTLCInfo, RAACommitmentOrder, BREAKDOWN_TIMEOUT, MIN_CLTV_EXPIRY_DELTA, MAX_LOCAL_BREAKDOWN_TIMEOUT};
30+
use ln::channelmanager::{CounterpartyForwardingInfo, PendingHTLCStatus, PendingInboundPayment, HTLCSource, HTLCFailReason, HTLCFailureMsg, PendingHTLCInfo, RAACommitmentOrder, BREAKDOWN_TIMEOUT, MIN_CLTV_EXPIRY_DELTA, MAX_LOCAL_BREAKDOWN_TIMEOUT};
3131
use ln::chan_utils::{CounterpartyCommitmentSecrets, TxCreationKeys, HTLCOutputInCommitment, HTLC_SUCCESS_TX_WEIGHT, HTLC_TIMEOUT_TX_WEIGHT, make_funding_redeemscript, ChannelPublicKeys, CommitmentTransaction, HolderCommitmentTransaction, ChannelTransactionParameters, CounterpartyChannelTransactionParameters, MAX_HTLCS, get_commitment_transaction_number_obscure_factor, ClosingTransaction};
3232
use ln::chan_utils;
3333
use chain::BestBlock;
@@ -47,7 +47,7 @@ use prelude::*;
4747
use core::{cmp,mem,fmt};
4848
use core::ops::Deref;
4949
#[cfg(any(test, feature = "fuzztarget", debug_assertions))]
50-
use sync::Mutex;
50+
use sync::{Mutex, MutexGuard};
5151
use bitcoin::hashes::hex::ToHex;
5252

5353
#[cfg(test)]
@@ -3398,7 +3398,7 @@ impl<Signer: Sign> Channel<Signer> {
33983398
/// If the auto-close timer is reached following the triggering of a auto-close condition
33993399
/// (i.e a non-satisfying feerate to ensure efficient confirmation), we force-close
34003400
/// channel, hopefully narrowing the safety risks for the user funds.
3401-
pub fn check_autoclose(&mut self, new_feerate: u32) -> Result<(), ChannelError> {
3401+
pub fn check_autoclose(&mut self, new_feerate: u32, pending_inbound_payments: MutexGuard<HashMap<PaymentHash, PendingInboundPayment>>) -> Result<(), ChannelError> {
34023402
if self.autoclose_timer > 0 && self.autoclose_timer < AUTOCLOSE_TIMEOUT {
34033403
// Again, check at each autoclose tick that mempool feerate hasn't fall back
34043404
// more in-sync to the current channel feerate. Otherwise, reset autoclose
@@ -3410,10 +3410,18 @@ impl<Signer: Sign> Channel<Signer> {
34103410
}
34113411
}
34123412
if self.autoclose_timer == AUTOCLOSE_TIMEOUT {
3413-
let inbound_stats = self.get_inbound_pending_htlc_stats(None);
3413+
let mut pending_inbound = false;
3414+
for htlc in self.pending_inbound_htlcs.iter() {
3415+
let exposure_dust_limit_success_sats = (self.get_dust_buffer_feerate(None) as u64 * HTLC_SUCCESS_TX_WEIGHT / 1000) + self.holder_dust_limit_satoshis;
3416+
if htlc.amount_msat / 1000 > exposure_dust_limit_success_sats {
3417+
if pending_inbound_payments.get(&htlc.payment_hash).is_some() {
3418+
pending_inbound = true;
3419+
}
3420+
}
3421+
}
34143422
let outbound_stats = self.get_outbound_pending_htlc_stats(None);
34153423
// If the channel has pending *included* HTLCs to claim on-chain and `should_autoclose`=y, close the channel
3416-
if inbound_stats.on_holder_tx_included_htlcs + outbound_stats.on_holder_tx_included_htlcs > 0 && self.config.should_autoclose {
3424+
if (outbound_stats.on_holder_tx_included_htlcs > 0 || pending_inbound) && self.config.should_autoclose {
34173425
return Err(ChannelError::Close("Channel has time-sensitive outputs and the auto-close timer has been reached".to_owned()));
34183426
}
34193427
// Otherwise, do not reset the autoclose timer as a substantial HTLC can be committed

lightning/src/ln/channelmanager.rs

+11-9
Original file line numberDiff line numberDiff line change
@@ -416,7 +416,7 @@ struct PeerState {
416416
///
417417
/// Note that this struct will be removed entirely soon, in favor of storing no inbound payment data
418418
/// and instead encoding it in the payment secret.
419-
struct PendingInboundPayment {
419+
pub(crate) struct PendingInboundPayment {
420420
/// The payment secret that the sender must use for us to accept this payment
421421
payment_secret: PaymentSecret,
422422
/// Time at which this HTLC expires - blocks with a header time above this value will result in
@@ -3089,15 +3089,17 @@ impl<Signer: Sign, M: Deref, T: Deref, K: Deref, F: Deref, L: Deref> ChannelMana
30893089

30903090
let mut retain_channel = true;
30913091
let mut chan_needs_persist = NotifyOption::SkipPersist;
3092-
let res = match chan.check_autoclose(new_feerate) {
3093-
Ok(res) => Ok(res),
3094-
Err(e) => {
3095-
let (drop, res) = convert_chan_err!(self, e, short_to_id, chan, chan_id);
3096-
if drop {
3097-
retain_channel = false;
3098-
chan_needs_persist = NotifyOption::DoPersist;
3092+
let res = {
3093+
match chan.check_autoclose(new_feerate, self.pending_inbound_payments.lock().unwrap()) {
3094+
Ok(res) => Ok(res),
3095+
Err(e) => {
3096+
let (drop, res) = convert_chan_err!(self, e, short_to_id, chan, chan_id);
3097+
if drop {
3098+
retain_channel = false;
3099+
chan_needs_persist = NotifyOption::DoPersist;
3100+
}
3101+
Err(res)
30993102
}
3100-
Err(res)
31013103
}
31023104
};
31033105

0 commit comments

Comments
 (0)