Skip to content

Commit b94d50b

Browse files
committed
Inform ChannelManager when fulfilled HTLCs are finalized
When an HTLC has been failed, we track it up until the point there exists no broadcastable commitment transaction which has the HTLC present, at which point Channel returns the HTLCSource back to the ChannelManager, which fails the HTLC backwards appropriately. When an HTLC is fulfilled, however, we fulfill on the backwards path immediately. This is great for claiming upstream HTLCs, but when we want to track pending payments, we need to ensure we can check with ChannelMonitor data to rebuild pending payments. In order to do so, we need an event similar to the HTLC failure event, but for fulfills instead. Specifically, if we force-close a channel, we remove its off-chain `Channel` object entirely, at which point, on reload, we may notice HTLC(s) which are not present in our pending payments map (as they may have received a payment preimage, but not fully committed to it). Thus, we'd conclude we still have a retryable payment, which is untrue. This commit does so, informing the ChannelManager via a new return element where appropriate of the HTLCSource corresponding to the failed HTLC.
1 parent ec11967 commit b94d50b

File tree

2 files changed

+45
-16
lines changed

2 files changed

+45
-16
lines changed

lightning/src/ln/channel.rs

Lines changed: 26 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -345,6 +345,7 @@ pub(super) struct RAAUpdates {
345345
pub commitment_update: Option<msgs::CommitmentUpdate>,
346346
pub to_forward_htlcs: Vec<(PendingHTLCInfo, u64)>,
347347
pub failed_htlcs: Vec<(HTLCSource, PaymentHash, HTLCFailReason)>,
348+
pub finalized_claim_htlcs: Vec<HTLCSource>,
348349
pub monitor_update: ChannelMonitorUpdate,
349350
pub holding_cell_failed_htlcs: Vec<(HTLCSource, PaymentHash)>,
350351
}
@@ -356,6 +357,7 @@ pub(super) struct MonitorRestoreUpdates {
356357
pub order: RAACommitmentOrder,
357358
pub forwards: Vec<(PendingHTLCInfo, u64)>,
358359
pub failures: Vec<(HTLCSource, PaymentHash, HTLCFailReason)>,
360+
pub finalized_claims: Vec<HTLCSource>,
359361
pub funding_broadcastable: Option<Transaction>,
360362
pub funding_locked: Option<msgs::FundingLocked>,
361363
}
@@ -427,6 +429,7 @@ pub(super) struct Channel<Signer: Sign> {
427429
monitor_pending_commitment_signed: bool,
428430
monitor_pending_forwards: Vec<(PendingHTLCInfo, u64)>,
429431
monitor_pending_failures: Vec<(HTLCSource, PaymentHash, HTLCFailReason)>,
432+
monitor_pending_finalized_fulfills: Vec<HTLCSource>,
430433

431434
// pending_update_fee is filled when sending and receiving update_fee.
432435
//
@@ -713,6 +716,7 @@ impl<Signer: Sign> Channel<Signer> {
713716
monitor_pending_commitment_signed: false,
714717
monitor_pending_forwards: Vec::new(),
715718
monitor_pending_failures: Vec::new(),
719+
monitor_pending_finalized_fulfills: Vec::new(),
716720

717721
#[cfg(debug_assertions)]
718722
holder_max_commitment_tx_output: Mutex::new((channel_value_satoshis * 1000 - push_msat, push_msat)),
@@ -976,6 +980,7 @@ impl<Signer: Sign> Channel<Signer> {
976980
monitor_pending_commitment_signed: false,
977981
monitor_pending_forwards: Vec::new(),
978982
monitor_pending_failures: Vec::new(),
983+
monitor_pending_finalized_fulfills: Vec::new(),
979984

980985
#[cfg(debug_assertions)]
981986
holder_max_commitment_tx_output: Mutex::new((msg.push_msat, msg.funding_satoshis * 1000 - msg.push_msat)),
@@ -2798,6 +2803,7 @@ impl<Signer: Sign> Channel<Signer> {
27982803
log_trace!(logger, "Updating HTLCs on receipt of RAA in channel {}...", log_bytes!(self.channel_id()));
27992804
let mut to_forward_infos = Vec::new();
28002805
let mut revoked_htlcs = Vec::new();
2806+
let mut finalized_claim_htlcs = Vec::new();
28012807
let mut update_fail_htlcs = Vec::new();
28022808
let mut update_fail_malformed_htlcs = Vec::new();
28032809
let mut require_commitment = false;
@@ -2824,6 +2830,7 @@ impl<Signer: Sign> Channel<Signer> {
28242830
if let Some(reason) = fail_reason.clone() { // We really want take() here, but, again, non-mut ref :(
28252831
revoked_htlcs.push((htlc.source.clone(), htlc.payment_hash, reason));
28262832
} else {
2833+
finalized_claim_htlcs.push(htlc.source.clone());
28272834
// They fulfilled, so we sent them money
28282835
value_to_self_msat_diff -= htlc.amount_msat as i64;
28292836
}
@@ -2920,9 +2927,10 @@ impl<Signer: Sign> Channel<Signer> {
29202927
}
29212928
self.monitor_pending_forwards.append(&mut to_forward_infos);
29222929
self.monitor_pending_failures.append(&mut revoked_htlcs);
2930+
self.monitor_pending_finalized_fulfills.append(&mut finalized_claim_htlcs);
29232931
log_debug!(logger, "Received a valid revoke_and_ack for channel {} but awaiting a monitor update resolution to reply.", log_bytes!(self.channel_id()));
29242932
return Ok(RAAUpdates {
2925-
commitment_update: None,
2933+
commitment_update: None, finalized_claim_htlcs: Vec::new(),
29262934
to_forward_htlcs: Vec::new(), failed_htlcs: Vec::new(),
29272935
monitor_update,
29282936
holding_cell_failed_htlcs: Vec::new()
@@ -2947,6 +2955,7 @@ impl<Signer: Sign> Channel<Signer> {
29472955

29482956
Ok(RAAUpdates {
29492957
commitment_update: Some(commitment_update),
2958+
finalized_claim_htlcs,
29502959
to_forward_htlcs: to_forward_infos,
29512960
failed_htlcs: revoked_htlcs,
29522961
monitor_update,
@@ -2973,13 +2982,15 @@ impl<Signer: Sign> Channel<Signer> {
29732982
update_fee: None,
29742983
commitment_signed
29752984
}),
2985+
finalized_claim_htlcs,
29762986
to_forward_htlcs: to_forward_infos, failed_htlcs: revoked_htlcs,
29772987
monitor_update, holding_cell_failed_htlcs: htlcs_to_fail
29782988
})
29792989
} else {
29802990
log_debug!(logger, "Received a valid revoke_and_ack for channel {} with no reply necessary.", log_bytes!(self.channel_id()));
29812991
Ok(RAAUpdates {
29822992
commitment_update: None,
2993+
finalized_claim_htlcs,
29832994
to_forward_htlcs: to_forward_infos, failed_htlcs: revoked_htlcs,
29842995
monitor_update, holding_cell_failed_htlcs: htlcs_to_fail
29852996
})
@@ -3097,11 +3108,16 @@ impl<Signer: Sign> Channel<Signer> {
30973108
/// which failed. The messages which were generated from that call which generated the
30983109
/// monitor update failure must *not* have been sent to the remote end, and must instead
30993110
/// have been dropped. They will be regenerated when monitor_updating_restored is called.
3100-
pub fn monitor_update_failed(&mut self, resend_raa: bool, resend_commitment: bool, mut pending_forwards: Vec<(PendingHTLCInfo, u64)>, mut pending_fails: Vec<(HTLCSource, PaymentHash, HTLCFailReason)>) {
3111+
pub fn monitor_update_failed(&mut self, resend_raa: bool, resend_commitment: bool,
3112+
mut pending_forwards: Vec<(PendingHTLCInfo, u64)>,
3113+
mut pending_fails: Vec<(HTLCSource, PaymentHash, HTLCFailReason)>,
3114+
mut pending_finalized_claims: Vec<HTLCSource>
3115+
) {
31013116
self.monitor_pending_revoke_and_ack |= resend_raa;
31023117
self.monitor_pending_commitment_signed |= resend_commitment;
31033118
self.monitor_pending_forwards.append(&mut pending_forwards);
31043119
self.monitor_pending_failures.append(&mut pending_fails);
3120+
self.monitor_pending_finalized_fulfills.append(&mut pending_finalized_claims);
31053121
self.channel_state |= ChannelState::MonitorUpdateFailed as u32;
31063122
}
31073123

@@ -3135,13 +3151,15 @@ impl<Signer: Sign> Channel<Signer> {
31353151
mem::swap(&mut forwards, &mut self.monitor_pending_forwards);
31363152
let mut failures = Vec::new();
31373153
mem::swap(&mut failures, &mut self.monitor_pending_failures);
3154+
let mut finalized_claims = Vec::new();
3155+
mem::swap(&mut finalized_claims, &mut self.monitor_pending_finalized_fulfills);
31383156

31393157
if self.channel_state & (ChannelState::PeerDisconnected as u32) != 0 {
31403158
self.monitor_pending_revoke_and_ack = false;
31413159
self.monitor_pending_commitment_signed = false;
31423160
return MonitorRestoreUpdates {
31433161
raa: None, commitment_update: None, order: RAACommitmentOrder::RevokeAndACKFirst,
3144-
forwards, failures, funding_broadcastable, funding_locked
3162+
forwards, failures, finalized_claims, funding_broadcastable, funding_locked
31453163
};
31463164
}
31473165

@@ -3160,7 +3178,7 @@ impl<Signer: Sign> Channel<Signer> {
31603178
if commitment_update.is_some() { "a" } else { "no" }, if raa.is_some() { "an" } else { "no" },
31613179
match order { RAACommitmentOrder::CommitmentFirst => "commitment", RAACommitmentOrder::RevokeAndACKFirst => "RAA"});
31623180
MonitorRestoreUpdates {
3163-
raa, commitment_update, order, forwards, failures, funding_broadcastable, funding_locked
3181+
raa, commitment_update, order, forwards, failures, finalized_claims, funding_broadcastable, funding_locked
31643182
}
31653183
}
31663184

@@ -5221,6 +5239,7 @@ impl<Signer: Sign> Writeable for Channel<Signer> {
52215239
(5, self.config, required),
52225240
(7, self.shutdown_scriptpubkey, option),
52235241
(9, self.target_closing_feerate_sats_per_kw, option),
5242+
(11, self.monitor_pending_finalized_fulfills, vec_type),
52245243
});
52255244

52265245
Ok(())
@@ -5454,13 +5473,15 @@ impl<'a, Signer: Sign, K: Deref> ReadableArgs<&'a K> for Channel<Signer>
54545473

54555474
let mut announcement_sigs = None;
54565475
let mut target_closing_feerate_sats_per_kw = None;
5476+
let mut monitor_pending_finalized_fulfills = Some(Vec::new());
54575477
read_tlv_fields!(reader, {
54585478
(0, announcement_sigs, option),
54595479
(1, minimum_depth, option),
54605480
(3, counterparty_selected_channel_reserve_satoshis, option),
54615481
(5, config, option), // Note that if none is provided we will *not* overwrite the existing one.
54625482
(7, shutdown_scriptpubkey, option),
54635483
(9, target_closing_feerate_sats_per_kw, option),
5484+
(11, monitor_pending_finalized_fulfills, vec_type),
54645485
});
54655486

54665487
let mut secp_ctx = Secp256k1::new();
@@ -5496,6 +5517,7 @@ impl<'a, Signer: Sign, K: Deref> ReadableArgs<&'a K> for Channel<Signer>
54965517
monitor_pending_commitment_signed,
54975518
monitor_pending_forwards,
54985519
monitor_pending_failures,
5520+
monitor_pending_finalized_fulfills: monitor_pending_finalized_fulfills.unwrap(),
54995521

55005522
pending_update_fee,
55015523
holding_cell_update_fee,

lightning/src/ln/channelmanager.rs

Lines changed: 19 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -997,7 +997,7 @@ macro_rules! handle_monitor_err {
997997
($self: ident, $err: expr, $channel_state: expr, $entry: expr, $action_type: path, $resend_raa: expr, $resend_commitment: expr) => {
998998
handle_monitor_err!($self, $err, $channel_state, $entry, $action_type, $resend_raa, $resend_commitment, Vec::new(), Vec::new())
999999
};
1000-
($self: ident, $err: expr, $short_to_id: expr, $chan: expr, $action_type: path, $resend_raa: expr, $resend_commitment: expr, $failed_forwards: expr, $failed_fails: expr, $chan_id: expr) => {
1000+
($self: ident, $err: expr, $short_to_id: expr, $chan: expr, $action_type: path, $resend_raa: expr, $resend_commitment: expr, $failed_forwards: expr, $failed_fails: expr, $failed_finalized_fulfills: expr, $chan_id: expr) => {
10011001
match $err {
10021002
ChannelMonitorUpdateErr::PermanentFailure => {
10031003
log_error!($self.logger, "Closing channel {} due to monitor update ChannelMonitorUpdateErr::PermanentFailure", log_bytes!($chan_id[..]));
@@ -1018,7 +1018,7 @@ macro_rules! handle_monitor_err {
10181018
(res, true)
10191019
},
10201020
ChannelMonitorUpdateErr::TemporaryFailure => {
1021-
log_info!($self.logger, "Disabling channel {} due to monitor update TemporaryFailure. On restore will send {} and process {} forwards and {} fails",
1021+
log_info!($self.logger, "Disabling channel {} due to monitor update TemporaryFailure. On restore will send {} and process {} forwards, {} fails, and {} fulfill finalizations",
10221022
log_bytes!($chan_id[..]),
10231023
if $resend_commitment && $resend_raa {
10241024
match $action_type {
@@ -1029,25 +1029,29 @@ macro_rules! handle_monitor_err {
10291029
else if $resend_raa { "RAA" }
10301030
else { "nothing" },
10311031
(&$failed_forwards as &Vec<(PendingHTLCInfo, u64)>).len(),
1032-
(&$failed_fails as &Vec<(HTLCSource, PaymentHash, HTLCFailReason)>).len());
1032+
(&$failed_fails as &Vec<(HTLCSource, PaymentHash, HTLCFailReason)>).len(),
1033+
(&$failed_finalized_fulfills as &Vec<HTLCSource>).len());
10331034
if !$resend_commitment {
10341035
debug_assert!($action_type == RAACommitmentOrder::RevokeAndACKFirst || !$resend_raa);
10351036
}
10361037
if !$resend_raa {
10371038
debug_assert!($action_type == RAACommitmentOrder::CommitmentFirst || !$resend_commitment);
10381039
}
1039-
$chan.monitor_update_failed($resend_raa, $resend_commitment, $failed_forwards, $failed_fails);
1040+
$chan.monitor_update_failed($resend_raa, $resend_commitment, $failed_forwards, $failed_fails, $failed_finalized_fulfills);
10401041
(Err(MsgHandleErrInternal::from_chan_no_close(ChannelError::Ignore("Failed to update ChannelMonitor".to_owned()), *$chan_id)), false)
10411042
},
10421043
}
10431044
};
1044-
($self: ident, $err: expr, $channel_state: expr, $entry: expr, $action_type: path, $resend_raa: expr, $resend_commitment: expr, $failed_forwards: expr, $failed_fails: expr) => { {
1045-
let (res, drop) = handle_monitor_err!($self, $err, $channel_state.short_to_id, $entry.get_mut(), $action_type, $resend_raa, $resend_commitment, $failed_forwards, $failed_fails, $entry.key());
1045+
($self: ident, $err: expr, $channel_state: expr, $entry: expr, $action_type: path, $resend_raa: expr, $resend_commitment: expr, $failed_forwards: expr, $failed_fails: expr, $failed_finalized_fulfills: expr) => { {
1046+
let (res, drop) = handle_monitor_err!($self, $err, $channel_state.short_to_id, $entry.get_mut(), $action_type, $resend_raa, $resend_commitment, $failed_forwards, $failed_fails, $failed_finalized_fulfills, $entry.key());
10461047
if drop {
10471048
$entry.remove_entry();
10481049
}
10491050
res
10501051
} };
1052+
($self: ident, $err: expr, $channel_state: expr, $entry: expr, $action_type: path, $resend_raa: expr, $resend_commitment: expr, $failed_forwards: expr, $failed_fails: expr) => {
1053+
handle_monitor_err!($self, $err, $channel_state, $entry, $action_type, $resend_raa, $resend_commitment, $failed_forwards, $failed_fails, Vec::new());
1054+
}
10511055
}
10521056

10531057
macro_rules! return_monitor_err {
@@ -1422,7 +1426,7 @@ impl<Signer: Sign, M: Deref, T: Deref, K: Deref, F: Deref, L: Deref> ChannelMana
14221426
if let Some(monitor_update) = monitor_update {
14231427
if let Err(e) = self.chain_monitor.update_channel(chan_entry.get().get_funding_txo().unwrap(), monitor_update) {
14241428
let (result, is_permanent) =
1425-
handle_monitor_err!(self, e, channel_state.short_to_id, chan_entry.get_mut(), RAACommitmentOrder::CommitmentFirst, false, false, Vec::new(), Vec::new(), chan_entry.key());
1429+
handle_monitor_err!(self, e, channel_state.short_to_id, chan_entry.get_mut(), RAACommitmentOrder::CommitmentFirst, false, false, Vec::new(), Vec::new(), Vec::new(), chan_entry.key());
14261430
if is_permanent {
14271431
remove_channel!(channel_state, chan_entry);
14281432
break result;
@@ -2827,7 +2831,7 @@ impl<Signer: Sign, M: Deref, T: Deref, K: Deref, F: Deref, L: Deref> ChannelMana
28272831
let ret_err = match res {
28282832
Ok(Some((update_fee, commitment_signed, monitor_update))) => {
28292833
if let Err(e) = self.chain_monitor.update_channel(chan.get_funding_txo().unwrap(), monitor_update) {
2830-
let (res, drop) = handle_monitor_err!(self, e, short_to_id, chan, RAACommitmentOrder::CommitmentFirst, false, true, Vec::new(), Vec::new(), chan_id);
2834+
let (res, drop) = handle_monitor_err!(self, e, short_to_id, chan, RAACommitmentOrder::CommitmentFirst, false, true, Vec::new(), Vec::new(), Vec::new(), chan_id);
28312835
if drop { retain_channel = false; }
28322836
res
28332837
} else {
@@ -3408,6 +3412,7 @@ impl<Signer: Sign, M: Deref, T: Deref, K: Deref, F: Deref, L: Deref> ChannelMana
34083412
msg: self.get_channel_update_for_unicast(channel.get()).unwrap(),
34093413
})
34103414
} else { None };
3415+
// TODO: Handle updates.finalized_claims!
34113416
chan_restoration_res = handle_chan_restoration_locked!(self, channel_lock, channel_state, channel, updates.raa, updates.commitment_update, updates.order, None, updates.forwards, updates.funding_broadcastable, updates.funding_locked);
34123417
if let Some(upd) = channel_update {
34133418
channel_state.pending_msg_events.push(upd);
@@ -3503,7 +3508,7 @@ impl<Signer: Sign, M: Deref, T: Deref, K: Deref, F: Deref, L: Deref> ChannelMana
35033508
// hasn't persisted to disk yet - we can't lose money on a transaction that we haven't
35043509
// accepted payment from yet. We do, however, need to wait to send our funding_locked
35053510
// until we have persisted our monitor.
3506-
chan.monitor_update_failed(false, false, Vec::new(), Vec::new());
3511+
chan.monitor_update_failed(false, false, Vec::new(), Vec::new(), Vec::new());
35073512
},
35083513
}
35093514
}
@@ -3621,7 +3626,7 @@ impl<Signer: Sign, M: Deref, T: Deref, K: Deref, F: Deref, L: Deref> ChannelMana
36213626
if let Some(monitor_update) = monitor_update {
36223627
if let Err(e) = self.chain_monitor.update_channel(chan_entry.get().get_funding_txo().unwrap(), monitor_update) {
36233628
let (result, is_permanent) =
3624-
handle_monitor_err!(self, e, channel_state.short_to_id, chan_entry.get_mut(), RAACommitmentOrder::CommitmentFirst, false, false, Vec::new(), Vec::new(), chan_entry.key());
3629+
handle_monitor_err!(self, e, channel_state.short_to_id, chan_entry.get_mut(), RAACommitmentOrder::CommitmentFirst, false, false, Vec::new(), Vec::new(), Vec::new(), chan_entry.key());
36253630
if is_permanent {
36263631
remove_channel!(channel_state, chan_entry);
36273632
break result;
@@ -3917,12 +3922,14 @@ impl<Signer: Sign, M: Deref, T: Deref, K: Deref, F: Deref, L: Deref> ChannelMana
39173922
assert!(raa_updates.commitment_update.is_none());
39183923
assert!(raa_updates.to_forward_htlcs.is_empty());
39193924
assert!(raa_updates.failed_htlcs.is_empty());
3925+
assert!(raa_updates.finalized_claim_htlcs.is_empty());
39203926
break Err(MsgHandleErrInternal::ignore_no_close("Previous monitor update failure prevented responses to RAA".to_owned()));
39213927
} else {
39223928
if let Err(e) = handle_monitor_err!(self, e, channel_state, chan,
39233929
RAACommitmentOrder::CommitmentFirst, false,
39243930
raa_updates.commitment_update.is_some(),
3925-
raa_updates.to_forward_htlcs, raa_updates.failed_htlcs) {
3931+
raa_updates.to_forward_htlcs, raa_updates.failed_htlcs,
3932+
raa_updates.finalized_claim_htlcs) {
39263933
break Err(e);
39273934
} else { unreachable!(); }
39283935
}
@@ -4176,7 +4183,7 @@ impl<Signer: Sign, M: Deref, T: Deref, K: Deref, F: Deref, L: Deref> ChannelMana
41764183
if let Some((commitment_update, monitor_update)) = commitment_opt {
41774184
if let Err(e) = self.chain_monitor.update_channel(chan.get_funding_txo().unwrap(), monitor_update) {
41784185
has_monitor_update = true;
4179-
let (res, close_channel) = handle_monitor_err!(self, e, short_to_id, chan, RAACommitmentOrder::CommitmentFirst, false, true, Vec::new(), Vec::new(), channel_id);
4186+
let (res, close_channel) = handle_monitor_err!(self, e, short_to_id, chan, RAACommitmentOrder::CommitmentFirst, false, true, Vec::new(), Vec::new(), Vec::new(), channel_id);
41804187
handle_errors.push((chan.get_counterparty_node_id(), res));
41814188
if close_channel { return false; }
41824189
} else {

0 commit comments

Comments
 (0)