Skip to content

Commit

Permalink
-f inbound htlc, autoclose only if pending payment
Browse files Browse the repository at this point in the history
  • Loading branch information
Antoine Riard committed Dec 7, 2021
1 parent 0fc0267 commit 464df56
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 14 deletions.
18 changes: 13 additions & 5 deletions lightning/src/ln/channel.rs
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ use ln::features::{ChannelFeatures, ChannelTypeFeatures, InitFeatures};
use ln::msgs;
use ln::msgs::{DecodeError, OptionalField, DataLossProtect};
use ln::script::{self, ShutdownScript};
use ln::channelmanager::{CounterpartyForwardingInfo, PendingHTLCStatus, HTLCSource, HTLCFailReason, HTLCFailureMsg, PendingHTLCInfo, RAACommitmentOrder, BREAKDOWN_TIMEOUT, MIN_CLTV_EXPIRY_DELTA, MAX_LOCAL_BREAKDOWN_TIMEOUT};
use ln::channelmanager::{CounterpartyForwardingInfo, PendingHTLCStatus, PendingInboundPayment, HTLCSource, HTLCFailReason, HTLCFailureMsg, PendingHTLCInfo, RAACommitmentOrder, BREAKDOWN_TIMEOUT, MIN_CLTV_EXPIRY_DELTA, MAX_LOCAL_BREAKDOWN_TIMEOUT};
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};
use ln::chan_utils;
use chain::BestBlock;
Expand All @@ -47,7 +47,7 @@ use prelude::*;
use core::{cmp,mem,fmt};
use core::ops::Deref;
#[cfg(any(test, feature = "fuzztarget", debug_assertions))]
use sync::Mutex;
use sync::{Mutex, MutexGuard};
use bitcoin::hashes::hex::ToHex;

#[cfg(test)]
Expand Down Expand Up @@ -3398,7 +3398,7 @@ impl<Signer: Sign> Channel<Signer> {
/// If the auto-close timer is reached following the triggering of a auto-close condition
/// (i.e a non-satisfying feerate to ensure efficient confirmation), we force-close
/// channel, hopefully narrowing the safety risks for the user funds.
pub fn check_autoclose(&mut self, new_feerate: u32) -> Result<(), ChannelError> {
pub fn check_autoclose(&mut self, new_feerate: u32, pending_inbound_payments: MutexGuard<HashMap<PaymentHash, PendingInboundPayment>>) -> Result<(), ChannelError> {
if self.autoclose_timer > 0 && self.autoclose_timer < AUTOCLOSE_TIMEOUT {
// Again, check at each autoclose tick that mempool feerate hasn't fall back
// more in-sync to the current channel feerate. Otherwise, reset autoclose
Expand All @@ -3410,10 +3410,18 @@ impl<Signer: Sign> Channel<Signer> {
}
}
if self.autoclose_timer == AUTOCLOSE_TIMEOUT {
let inbound_stats = self.get_inbound_pending_htlc_stats(None);
let mut pending_inbound = false;
for htlc in self.pending_inbound_htlcs.iter() {
let exposure_dust_limit_success_sats = (self.get_dust_buffer_feerate(None) as u64 * HTLC_SUCCESS_TX_WEIGHT / 1000) + self.holder_dust_limit_satoshis;
if htlc.amount_msat / 1000 > exposure_dust_limit_success_sats {
if pending_inbound_payments.get(&htlc.payment_hash).is_some() {
pending_inbound = true;
}
}
}
let outbound_stats = self.get_outbound_pending_htlc_stats(None);
// If the channel has pending *included* HTLCs to claim on-chain and `should_autoclose`=y, close the channel
if inbound_stats.on_holder_tx_included_htlcs + outbound_stats.on_holder_tx_included_htlcs > 0 && self.config.should_autoclose {
if (outbound_stats.on_holder_tx_included_htlcs > 0 || pending_inbound) && self.config.should_autoclose {
return Err(ChannelError::Close("Channel has time-sensitive outputs and the auto-close timer has been reached".to_owned()));
}
// Otherwise, do not reset the autoclose timer as a substantial HTLC can be committed
Expand Down
20 changes: 11 additions & 9 deletions lightning/src/ln/channelmanager.rs
Original file line number Diff line number Diff line change
Expand Up @@ -416,7 +416,7 @@ struct PeerState {
///
/// Note that this struct will be removed entirely soon, in favor of storing no inbound payment data
/// and instead encoding it in the payment secret.
struct PendingInboundPayment {
pub(crate) struct PendingInboundPayment {
/// The payment secret that the sender must use for us to accept this payment
payment_secret: PaymentSecret,
/// Time at which this HTLC expires - blocks with a header time above this value will result in
Expand Down Expand Up @@ -3089,15 +3089,17 @@ impl<Signer: Sign, M: Deref, T: Deref, K: Deref, F: Deref, L: Deref> ChannelMana

let mut retain_channel = true;
let mut chan_needs_persist = NotifyOption::SkipPersist;
let res = match chan.check_autoclose(new_feerate) {
Ok(res) => Ok(res),
Err(e) => {
let (drop, res) = convert_chan_err!(self, e, short_to_id, chan, chan_id);
if drop {
retain_channel = false;
chan_needs_persist = NotifyOption::DoPersist;
let res = {
match chan.check_autoclose(new_feerate, self.pending_inbound_payments.lock().unwrap()) {
Ok(res) => Ok(res),
Err(e) => {
let (drop, res) = convert_chan_err!(self, e, short_to_id, chan, chan_id);
if drop {
retain_channel = false;
chan_needs_persist = NotifyOption::DoPersist;
}
Err(res)
}
Err(res)
}
};

Expand Down

0 comments on commit 464df56

Please sign in to comment.