Skip to content

Commit b3bfecd

Browse files
committed
Reload pending payments from ChannelMonitor HTLC data on reload
If we go to send a payment, add the HTLC(s) to the channel(s), commit the ChannelMonitor updates to disk, and then crash, we'll come back up with no pending payments but HTLC(s) ready to be claim/failed. This makes it rather impractical to write a payment sender/retryer, as you cannot guarantee atomicity - you cannot guarantee you'll have retry data persisted even if the HTLC(s) are actually pending. Because ChannelMonitors are *the* atomically-persisted data in LDK, we lean on their current HTLC data to figure out what HTLC(s) are a part of an outbound payment, rebuilding the pending payments list on reload.
1 parent 53389a2 commit b3bfecd

File tree

2 files changed

+157
-2
lines changed

2 files changed

+157
-2
lines changed

lightning/src/chain/channelmonitor.rs

+93
Original file line numberDiff line numberDiff line change
@@ -1522,6 +1522,99 @@ impl<Signer: Sign> ChannelMonitor<Signer> {
15221522

15231523
res
15241524
}
1525+
1526+
/// Gets the set of outbound HTLCs which are pending resolution in this channel.
1527+
/// This is used to reconstruct pending outbound payments on restart in the ChannelManager.
1528+
pub(crate) fn get_pending_htlcs(&self) -> HashMap<HTLCSource, HTLCOutputInCommitment> {
1529+
let mut res = HashMap::new();
1530+
let us = self.inner.lock().unwrap();
1531+
1532+
macro_rules! walk_htlcs {
1533+
($holder_commitment: expr, $htlc_iter: expr) => {
1534+
for (htlc, source) in $htlc_iter {
1535+
if us.htlcs_resolved_on_chain.iter().any(|v| Some(v.input_idx) == htlc.transaction_output_index) {
1536+
// We should assert that funding_spend_confirmed is_some() here, but we
1537+
// have some unit tests which violate HTLC transaction CSVs entirely and
1538+
// would fail.
1539+
} else if htlc.offered == $holder_commitment {
1540+
// If the payment was outbound, check if there's an HTLCUpdate
1541+
// indicating we have spent this HTLC with a timeout, claiming it back
1542+
// and awaiting confirmations on it.
1543+
let htlc_update_confd = us.onchain_events_awaiting_threshold_conf.iter().find_map(|event| {
1544+
if let OnchainEvent::HTLCUpdate { input_idx: Some(input_idx), .. } = event.event {
1545+
// If the HTLC was timed out, we wait for ANTI_REORG_DELAY blocks
1546+
// before considering it "no longer pending" - this matches when we
1547+
// provide the ChannelManager an HTLC failure event.
1548+
if Some(input_idx) == htlc.transaction_output_index &&
1549+
us.best_block.height() >= event.height + ANTI_REORG_DELAY - 1
1550+
{ Some(()) } else { None }
1551+
} else if let OnchainEvent::HTLCSpendConfirmation { input_idx, .. } = event.event {
1552+
// If the HTLC was fulfilled with a preimage, we consider the HTLC
1553+
// immediately non-pending, matching when we provide ChannelManager
1554+
// the preimage.
1555+
if Some(input_idx) == htlc.transaction_output_index {
1556+
Some(())
1557+
} else { None }
1558+
} else { None }
1559+
});
1560+
if htlc_update_confd.is_none() {
1561+
res.insert(source.clone(), htlc.clone());
1562+
}
1563+
}
1564+
}
1565+
}
1566+
}
1567+
1568+
// We're only concerned with the confirmation count of HTLC transactions, and don't
1569+
// actually care how many confirmations a commitment transaction may or may not have. Thus,
1570+
// we look for both a FundingSpendConfirmation event, or at funding_spend_confirmed.
1571+
let confirmed_txid = us.funding_spend_confirmed.or_else(|| {
1572+
us.onchain_events_awaiting_threshold_conf.iter().find_map(|event| {
1573+
if let OnchainEvent::FundingSpendConfirmation { .. } = event.event {
1574+
Some(event.txid)
1575+
} else { None }
1576+
})
1577+
});
1578+
if let Some(txid) = confirmed_txid {
1579+
if Some(txid) == us.current_counterparty_commitment_txid || Some(txid) == us.prev_counterparty_commitment_txid {
1580+
walk_htlcs!(false, us.counterparty_claimable_outpoints.get(&txid).unwrap().iter().find_map(|(a, b)| {
1581+
if let &Some(ref source) = b {
1582+
Some((a, &**source))
1583+
} else { None }
1584+
}));
1585+
} else if txid == us.current_holder_commitment_tx.txid {
1586+
walk_htlcs!(true, us.current_holder_commitment_tx.htlc_outputs.iter().find_map(|(a, _, c)| {
1587+
if let Some(source) = c { Some((a, source)) } else { None }
1588+
}));
1589+
} else if let Some(prev_commitment) = &us.prev_holder_signed_commitment_tx {
1590+
if txid == prev_commitment.txid {
1591+
walk_htlcs!(true, prev_commitment.htlc_outputs.iter().find_map(|(a, _, c)| {
1592+
if let Some(source) = c { Some((a, source)) } else { None }
1593+
}));
1594+
}
1595+
}
1596+
} else {
1597+
macro_rules! check_htlc_fails {
1598+
($txid: expr, $commitment_tx: expr) => {
1599+
if let Some(ref latest_outpoints) = us.counterparty_claimable_outpoints.get($txid) {
1600+
for &(ref htlc, ref source_option) in latest_outpoints.iter() {
1601+
if let &Some(ref source) = source_option {
1602+
res.insert((**source).clone(), htlc.clone());
1603+
}
1604+
}
1605+
}
1606+
}
1607+
}
1608+
if let Some(ref txid) = us.current_counterparty_commitment_txid {
1609+
check_htlc_fails!(txid, "current");
1610+
}
1611+
if let Some(ref txid) = us.prev_counterparty_commitment_txid {
1612+
check_htlc_fails!(txid, "previous");
1613+
}
1614+
}
1615+
1616+
res
1617+
}
15251618
}
15261619

15271620
/// Compares a broadcasted commitment transaction's HTLCs with those in the latest state,

lightning/src/ln/channelmanager.rs

+64-2
Original file line numberDiff line numberDiff line change
@@ -145,7 +145,7 @@ pub(super) enum HTLCForwardInfo {
145145
}
146146

147147
/// Tracks the inbound corresponding to an outbound HTLC
148-
#[derive(Clone, PartialEq)]
148+
#[derive(Clone, Hash, PartialEq, Eq)]
149149
pub(crate) struct HTLCPreviousHopData {
150150
short_channel_id: u64,
151151
htlc_id: u64,
@@ -189,7 +189,8 @@ impl Readable for PaymentId {
189189
}
190190
}
191191
/// Tracks the inbound corresponding to an outbound HTLC
192-
#[derive(Clone, PartialEq)]
192+
#[allow(clippy::derive_hash_xor_eq)] // Our Hash is faithful to the data, we just don't have SecretKey::hash
193+
#[derive(Clone, PartialEq, Eq)]
193194
pub(crate) enum HTLCSource {
194195
PreviousHopData(HTLCPreviousHopData),
195196
OutboundRoute {
@@ -202,6 +203,24 @@ pub(crate) enum HTLCSource {
202203
payment_secret: Option<PaymentSecret>,
203204
},
204205
}
206+
impl core::hash::Hash for HTLCSource {
207+
fn hash<H: core::hash::Hasher>(&self, hasher: &mut H) {
208+
match self {
209+
HTLCSource::PreviousHopData(prev_hop_data) => {
210+
0u8.hash(hasher);
211+
prev_hop_data.hash(hasher);
212+
},
213+
HTLCSource::OutboundRoute { path, session_priv, payment_id, payment_secret, first_hop_htlc_msat } => {
214+
1u8.hash(hasher);
215+
path.hash(hasher);
216+
session_priv[..].hash(hasher);
217+
payment_id.hash(hasher);
218+
payment_secret.hash(hasher);
219+
first_hop_htlc_msat.hash(hasher);
220+
},
221+
}
222+
}
223+
}
205224
#[cfg(test)]
206225
impl HTLCSource {
207226
pub fn dummy() -> Self {
@@ -5846,6 +5865,49 @@ impl<'a, Signer: Sign, M: Deref, T: Deref, K: Deref, F: Deref, L: Deref>
58465865
outbounds.insert(id, PendingOutboundPayment::Legacy { session_privs });
58475866
}
58485867
pending_outbound_payments = Some(outbounds);
5868+
} else {
5869+
// If we're tracking pending payments, ensure we haven't lost any by looking at the
5870+
// ChannelMonitor data for any channels for which we do not have authorative state
5871+
// (i.e. those for which we just force-closed above or we otherwise don't have a
5872+
// corresponding `Channel` at all).
5873+
// This avoids several edge-cases where we would otherwise "forget" about pending
5874+
// payments which are still in-flight via their on-chain state.
5875+
// We only rebuild the pending payments map if we were most recently serialized by
5876+
// 0.0.102+
5877+
for (_, monitor) in args.channel_monitors {
5878+
if by_id.get(&monitor.get_funding_txo().0.to_channel_id()).is_none() {
5879+
for (htlc_source, htlc) in monitor.get_pending_htlcs() {
5880+
if let HTLCSource::OutboundRoute { payment_id, session_priv, path, payment_secret, .. } = htlc_source {
5881+
if path.is_empty() {
5882+
log_error!(args.logger, "Got an empty path for a pending payment");
5883+
return Err(DecodeError::InvalidValue);
5884+
}
5885+
let path_amt = path.last().unwrap().fee_msat;
5886+
let mut session_priv_bytes = [0; 32];
5887+
session_priv_bytes[..].copy_from_slice(&session_priv[..]);
5888+
match pending_outbound_payments.as_mut().unwrap().entry(payment_id) {
5889+
hash_map::Entry::Occupied(mut entry) => {
5890+
let readded = entry.get_mut().insert(session_priv_bytes, path_amt);
5891+
log_info!(args.logger, "{} a pending payment path for {} msat for session priv {} on an existing pending payment with payment hash {}",
5892+
if readded { "Added" } else { "Had" }, path_amt, log_bytes!(session_priv_bytes), log_bytes!(htlc.payment_hash.0));
5893+
},
5894+
hash_map::Entry::Vacant(entry) => {
5895+
entry.insert(PendingOutboundPayment::Retryable {
5896+
session_privs: [session_priv_bytes].iter().map(|a| *a).collect(),
5897+
payment_hash: htlc.payment_hash,
5898+
payment_secret,
5899+
pending_amt_msat: path_amt,
5900+
total_msat: path_amt,
5901+
starting_block_height: best_block_height,
5902+
});
5903+
log_info!(args.logger, "Added a pending payment for {} msat with payment hash {} for path with session priv {}",
5904+
path_amt, log_bytes!(htlc.payment_hash.0), log_bytes!(session_priv_bytes));
5905+
}
5906+
}
5907+
}
5908+
}
5909+
}
5910+
}
58495911
}
58505912

58515913
let mut secp_ctx = Secp256k1::new();

0 commit comments

Comments
 (0)