@@ -1183,6 +1183,21 @@ impl_writeable_tlv_based_enum_upgradable!(MonitorUpdateCompletionAction,
1183
1183
},
1184
1184
);
1185
1185
1186
+ #[derive(Clone, Debug, PartialEq, Eq)]
1187
+ pub(crate) struct PaymentCompleteUpdate {
1188
+ counterparty_node_id: PublicKey,
1189
+ channel_funding_outpoint: OutPoint,
1190
+ channel_id: ChannelId,
1191
+ htlc_id: SentHTLCId,
1192
+ }
1193
+
1194
+ impl_writeable_tlv_based!(PaymentCompleteUpdate, {
1195
+ (1, channel_funding_outpoint, required),
1196
+ (3, counterparty_node_id, required),
1197
+ (5, channel_id, required),
1198
+ (7, htlc_id, required),
1199
+ });
1200
+
1186
1201
#[derive(Clone, Debug, PartialEq, Eq)]
1187
1202
pub(crate) enum EventCompletionAction {
1188
1203
ReleaseRAAChannelMonitorUpdate {
@@ -3298,7 +3313,7 @@ macro_rules! handle_monitor_update_completion {
3298
3313
$self.finalize_claims(updates.finalized_claimed_htlcs);
3299
3314
for failure in updates.failed_htlcs.drain(..) {
3300
3315
let receiver = HTLCDestination::NextHopChannel { node_id: Some(counterparty_node_id), channel_id };
3301
- $self.fail_htlc_backwards_internal(&failure.0, &failure.1, &failure.2, receiver);
3316
+ $self.fail_htlc_backwards_internal(&failure.0, &failure.1, &failure.2, receiver, None );
3302
3317
}
3303
3318
} }
3304
3319
}
@@ -3923,7 +3938,7 @@ where
3923
3938
for htlc_source in failed_htlcs.drain(..) {
3924
3939
let reason = HTLCFailReason::from_failure_code(0x4000 | 8);
3925
3940
let receiver = HTLCDestination::NextHopChannel { node_id: Some(*counterparty_node_id), channel_id: *channel_id };
3926
- self.fail_htlc_backwards_internal(&htlc_source.0, &htlc_source.1, &reason, receiver);
3941
+ self.fail_htlc_backwards_internal(&htlc_source.0, &htlc_source.1, &reason, receiver, None );
3927
3942
}
3928
3943
3929
3944
if let Some(shutdown_result) = shutdown_result {
@@ -4046,7 +4061,7 @@ where
4046
4061
let (source, payment_hash, counterparty_node_id, channel_id) = htlc_source;
4047
4062
let reason = HTLCFailReason::from_failure_code(0x4000 | 8);
4048
4063
let receiver = HTLCDestination::NextHopChannel { node_id: Some(counterparty_node_id), channel_id };
4049
- self.fail_htlc_backwards_internal(&source, &payment_hash, &reason, receiver);
4064
+ self.fail_htlc_backwards_internal(&source, &payment_hash, &reason, receiver, None );
4050
4065
}
4051
4066
if let Some((_, funding_txo, _channel_id, monitor_update)) = shutdown_res.monitor_update {
4052
4067
debug_assert!(false, "This should have been handled in `locked_close_channel`");
@@ -5642,7 +5657,7 @@ where
5642
5657
5643
5658
let failure_reason = HTLCFailReason::from_failure_code(0x4000 | 10);
5644
5659
let destination = HTLCDestination::UnknownNextHop { requested_forward_scid: short_channel_id };
5645
- self.fail_htlc_backwards_internal(&htlc_source, &payment.forward_info.payment_hash, &failure_reason, destination);
5660
+ self.fail_htlc_backwards_internal(&htlc_source, &payment.forward_info.payment_hash, &failure_reason, destination, None );
5646
5661
} else { unreachable!() } // Only `PendingHTLCRouting::Forward`s are intercepted
5647
5662
5648
5663
Ok(())
@@ -6324,7 +6339,13 @@ where
6324
6339
&self.pending_events, &self.logger, |args| self.send_payment_along_path(args));
6325
6340
6326
6341
for (htlc_source, payment_hash, failure_reason, destination) in failed_forwards.drain(..) {
6327
- self.fail_htlc_backwards_internal(&htlc_source, &payment_hash, &failure_reason, destination);
6342
+ self.fail_htlc_backwards_internal(
6343
+ &htlc_source,
6344
+ &payment_hash,
6345
+ &failure_reason,
6346
+ destination,
6347
+ None,
6348
+ );
6328
6349
}
6329
6350
self.forward_htlcs(&mut phantom_receives);
6330
6351
@@ -6686,7 +6707,7 @@ where
6686
6707
let source = HTLCSource::PreviousHopData(htlc_source.0.clone());
6687
6708
let reason = HTLCFailReason::from_failure_code(23);
6688
6709
let receiver = HTLCDestination::FailedPayment { payment_hash: htlc_source.1 };
6689
- self.fail_htlc_backwards_internal(&source, &htlc_source.1, &reason, receiver);
6710
+ self.fail_htlc_backwards_internal(&source, &htlc_source.1, &reason, receiver, None );
6690
6711
}
6691
6712
6692
6713
for (err, counterparty_node_id) in handle_errors.drain(..) {
@@ -6751,7 +6772,7 @@ where
6751
6772
let reason = self.get_htlc_fail_reason_from_failure_code(failure_code, &htlc);
6752
6773
let source = HTLCSource::PreviousHopData(htlc.prev_hop);
6753
6774
let receiver = HTLCDestination::FailedPayment { payment_hash: *payment_hash };
6754
- self.fail_htlc_backwards_internal(&source, &payment_hash, &reason, receiver);
6775
+ self.fail_htlc_backwards_internal(&source, &payment_hash, &reason, receiver, None );
6755
6776
}
6756
6777
}
6757
6778
}
@@ -6830,18 +6851,26 @@ where
6830
6851
for (htlc_src, payment_hash) in htlcs_to_fail.drain(..) {
6831
6852
let reason = HTLCFailReason::reason(failure_code, onion_failure_data.clone());
6832
6853
let receiver = HTLCDestination::NextHopChannel { node_id: Some(counterparty_node_id.clone()), channel_id };
6833
- self.fail_htlc_backwards_internal(&htlc_src, &payment_hash, &reason, receiver);
6854
+ self.fail_htlc_backwards_internal(&htlc_src, &payment_hash, &reason, receiver, None );
6834
6855
}
6835
6856
}
6836
6857
6837
- fn fail_htlc_backwards_internal(&self, source: &HTLCSource, payment_hash: &PaymentHash, onion_error: &HTLCFailReason, destination: HTLCDestination) {
6838
- let push_forward_event = self.fail_htlc_backwards_internal_without_forward_event(source, payment_hash, onion_error, destination);
6858
+ fn fail_htlc_backwards_internal(
6859
+ &self, source: &HTLCSource, payment_hash: &PaymentHash, onion_error: &HTLCFailReason,
6860
+ destination: HTLCDestination,
6861
+ from_monitor_update_completion: Option<PaymentCompleteUpdate>,
6862
+ ) {
6863
+ let push_forward_event = self.fail_htlc_backwards_internal_without_forward_event(source, payment_hash, onion_error, destination, from_monitor_update_completion);
6839
6864
if push_forward_event { self.push_pending_forwards_ev(); }
6840
6865
}
6841
6866
6842
6867
/// Fails an HTLC backwards to the sender of it to us.
6843
6868
/// Note that we do not assume that channels corresponding to failed HTLCs are still available.
6844
- fn fail_htlc_backwards_internal_without_forward_event(&self, source: &HTLCSource, payment_hash: &PaymentHash, onion_error: &HTLCFailReason, destination: HTLCDestination) -> bool {
6869
+ fn fail_htlc_backwards_internal_without_forward_event(
6870
+ &self, source: &HTLCSource, payment_hash: &PaymentHash, onion_error: &HTLCFailReason,
6871
+ destination: HTLCDestination,
6872
+ mut from_monitor_update_completion: Option<PaymentCompleteUpdate>,
6873
+ ) -> bool {
6845
6874
// Ensure that no peer state channel storage lock is held when calling this function.
6846
6875
// This ensures that future code doesn't introduce a lock-order requirement for
6847
6876
// `forward_htlcs` to be locked after the `per_peer_state` peer locks, which calling
@@ -6862,9 +6891,28 @@ where
6862
6891
let mut push_forward_event;
6863
6892
match source {
6864
6893
HTLCSource::OutboundRoute { ref path, ref session_priv, ref payment_id, .. } => {
6865
- push_forward_event = self.pending_outbound_payments.fail_htlc(source, payment_hash, onion_error, path,
6866
- session_priv, payment_id, self.probing_cookie_secret, &self.secp_ctx,
6867
- &self.pending_events, &self.logger);
6894
+ push_forward_event = self.pending_outbound_payments.fail_htlc(
6895
+ source,
6896
+ payment_hash,
6897
+ onion_error,
6898
+ path,
6899
+ session_priv,
6900
+ payment_id,
6901
+ self.probing_cookie_secret,
6902
+ &self.secp_ctx,
6903
+ &self.pending_events,
6904
+ &self.logger,
6905
+ &mut from_monitor_update_completion,
6906
+ );
6907
+ if let Some(update) = from_monitor_update_completion {
6908
+ // If `fail_htlc` didn't `take` the post-event action, we should go ahead and
6909
+ // complete it here as the failure was duplicative - we've already handled it.
6910
+ // This should mostly only happen on startup, but it is possible to hit it in
6911
+ // rare cases where a MonitorUpdate is replayed after restart because a
6912
+ // ChannelMonitor wasn't persisted after it was applied (even though the
6913
+ // ChannelManager was).
6914
+ // TODO
6915
+ }
6868
6916
},
6869
6917
HTLCSource::PreviousHopData(HTLCPreviousHopData {
6870
6918
ref short_channel_id, ref htlc_id, ref incoming_packet_shared_secret,
@@ -6979,7 +7027,7 @@ where
6979
7027
let reason = self.get_htlc_fail_reason_from_failure_code(FailureCode::InvalidOnionPayload(None), &htlc);
6980
7028
let source = HTLCSource::PreviousHopData(htlc.prev_hop);
6981
7029
let receiver = HTLCDestination::FailedPayment { payment_hash };
6982
- self.fail_htlc_backwards_internal(&source, &payment_hash, &reason, receiver);
7030
+ self.fail_htlc_backwards_internal(&source, &payment_hash, &reason, receiver, None );
6983
7031
}
6984
7032
return;
6985
7033
}
@@ -7085,7 +7133,7 @@ where
7085
7133
let source = HTLCSource::PreviousHopData(htlc.prev_hop);
7086
7134
let reason = HTLCFailReason::reason(0x4000 | 15, htlc_msat_height_data);
7087
7135
let receiver = HTLCDestination::FailedPayment { payment_hash };
7088
- self.fail_htlc_backwards_internal(&source, &payment_hash, &reason, receiver);
7136
+ self.fail_htlc_backwards_internal(&source, &payment_hash, &reason, receiver, None );
7089
7137
}
7090
7138
self.claimable_payments.lock().unwrap().pending_claiming_payments.remove(&payment_hash);
7091
7139
}
@@ -7356,14 +7404,34 @@ This indicates a bug inside LDK. Please report this error at https://github.com/
7356
7404
if let Some(pubkey) = next_channel_counterparty_node_id {
7357
7405
debug_assert_eq!(pubkey, path.hops[0].pubkey);
7358
7406
}
7359
- let ev_completion_action = EventCompletionAction::ReleaseRAAChannelMonitorUpdate {
7360
- channel_funding_outpoint: Some(next_channel_outpoint),
7361
- channel_id: next_channel_id,
7362
- counterparty_node_id: path.hops[0].pubkey,
7407
+ let mut ev_completion_action =
7408
+ Some(EventCompletionAction::ReleaseRAAChannelMonitorUpdate {
7409
+ channel_funding_outpoint: Some(next_channel_outpoint),
7410
+ channel_id: next_channel_id,
7411
+ counterparty_node_id: path.hops[0].pubkey,
7412
+ });
7413
+ self.pending_outbound_payments.claim_htlc(
7414
+ payment_id,
7415
+ payment_preimage,
7416
+ session_priv,
7417
+ path,
7418
+ from_onchain,
7419
+ &mut ev_completion_action,
7420
+ &self.pending_events,
7421
+ &self.logger,
7422
+ );
7423
+ // If an event was generated, `claim_htlc` set `ev_completion_action` to None, if
7424
+ // not, we should go ahead and run it now (as the claim was duplicative), at least
7425
+ // if a PaymentClaimed event with the same action isn't already pending.
7426
+ let have_action = if ev_completion_action.is_some() {
7427
+ let pending_events = self.pending_events.lock().unwrap();
7428
+ pending_events.iter().any(|(_, act)| *act == ev_completion_action)
7429
+ } else {
7430
+ false
7363
7431
};
7364
- self.pending_outbound_payments.claim_htlc(payment_id, payment_preimage,
7365
- session_priv, path, from_onchain, ev_completion_action, & self.pending_events,
7366
- &self.logger);
7432
+ if !have_action {
7433
+ self.handle_post_event_actions(ev_completion_action);
7434
+ }
7367
7435
},
7368
7436
HTLCSource::PreviousHopData(hop_data) => {
7369
7437
let prev_channel_id = hop_data.channel_id;
@@ -8709,7 +8777,7 @@ This indicates a bug inside LDK. Please report this error at https://github.com/
8709
8777
for htlc_source in dropped_htlcs.drain(..) {
8710
8778
let receiver = HTLCDestination::NextHopChannel { node_id: Some(counterparty_node_id.clone()), channel_id: msg.channel_id };
8711
8779
let reason = HTLCFailReason::from_failure_code(0x4000 | 8);
8712
- self.fail_htlc_backwards_internal(&htlc_source.0, &htlc_source.1, &reason, receiver);
8780
+ self.fail_htlc_backwards_internal(&htlc_source.0, &htlc_source.1, &reason, receiver, None );
8713
8781
}
8714
8782
if let Some(shutdown_res) = finish_shutdown {
8715
8783
self.finish_close_channel(shutdown_res);
@@ -9120,7 +9188,13 @@ This indicates a bug inside LDK. Please report this error at https://github.com/
9120
9188
}
9121
9189
9122
9190
for (htlc_source, payment_hash, failure_reason, destination) in failed_intercept_forwards.drain(..) {
9123
- push_forward_event |= self.fail_htlc_backwards_internal_without_forward_event(&htlc_source, &payment_hash, &failure_reason, destination);
9191
+ push_forward_event |= self.fail_htlc_backwards_internal_without_forward_event(
9192
+ &htlc_source,
9193
+ &payment_hash,
9194
+ &failure_reason,
9195
+ destination,
9196
+ None,
9197
+ );
9124
9198
}
9125
9199
9126
9200
if !new_intercept_events.is_empty() {
@@ -9461,7 +9535,13 @@ This indicates a bug inside LDK. Please report this error at https://github.com/
9461
9535
log_trace!(logger, "Failing HTLC with hash {} from our monitor", &htlc_update.payment_hash);
9462
9536
let receiver = HTLCDestination::NextHopChannel { node_id: counterparty_node_id, channel_id };
9463
9537
let reason = HTLCFailReason::from_failure_code(0x4000 | 8);
9464
- self.fail_htlc_backwards_internal(&htlc_update.source, &htlc_update.payment_hash, &reason, receiver);
9538
+ self.fail_htlc_backwards_internal(
9539
+ &htlc_update.source,
9540
+ &htlc_update.payment_hash,
9541
+ &reason,
9542
+ receiver,
9543
+ None,
9544
+ );
9465
9545
}
9466
9546
},
9467
9547
MonitorEvent::HolderForceClosed(_) | MonitorEvent::HolderForceClosedWithInfo { .. } => {
@@ -10806,8 +10886,8 @@ where
10806
10886
}
10807
10887
}
10808
10888
10809
- fn handle_post_event_actions(&self, actions: Vec<EventCompletionAction> ) {
10810
- for action in actions {
10889
+ fn handle_post_event_actions<I: IntoIterator<Item = EventCompletionAction>> (&self, actions: I ) {
10890
+ for action in actions.into_iter() {
10811
10891
match action {
10812
10892
EventCompletionAction::ReleaseRAAChannelMonitorUpdate {
10813
10893
channel_funding_outpoint: _,
@@ -11313,7 +11393,7 @@ where
11313
11393
}
11314
11394
11315
11395
for (source, payment_hash, reason, destination) in timed_out_htlcs.drain(..) {
11316
- self.fail_htlc_backwards_internal(&source, &payment_hash, &reason, destination);
11396
+ self.fail_htlc_backwards_internal(&source, &payment_hash, &reason, destination, None );
11317
11397
}
11318
11398
}
11319
11399
@@ -13378,7 +13458,7 @@ where
13378
13458
log_error!(logger, " The ChannelMonitor for channel {} is at counterparty commitment transaction number {} but the ChannelManager is at counterparty commitment transaction number {}.",
13379
13459
&channel.context.channel_id(), monitor.get_cur_counterparty_commitment_number(), channel.get_cur_counterparty_commitment_transaction_number());
13380
13460
}
13381
- let mut shutdown_result = channel.context.force_shutdown(true, ClosureReason::OutdatedChannelManager);
13461
+ let shutdown_result = channel.context.force_shutdown(true, ClosureReason::OutdatedChannelManager);
13382
13462
if shutdown_result.unbroadcasted_batch_funding_txid.is_some() {
13383
13463
return Err(DecodeError::InvalidValue);
13384
13464
}
@@ -13399,7 +13479,9 @@ where
13399
13479
counterparty_node_id, funding_txo, channel_id, update
13400
13480
});
13401
13481
}
13402
- failed_htlcs.append(&mut shutdown_result.dropped_outbound_htlcs);
13482
+ for (source, hash, cp_id, chan_id) in shutdown_result.dropped_outbound_htlcs {
13483
+ failed_htlcs.push((source, hash, cp_id, chan_id, None));
13484
+ }
13403
13485
channel_closures.push_back((events::Event::ChannelClosed {
13404
13486
channel_id: channel.context.channel_id(),
13405
13487
user_channel_id: channel.context.get_user_id(),
@@ -13426,7 +13508,13 @@ where
13426
13508
log_info!(logger,
13427
13509
"Failing HTLC with hash {} as it is missing in the ChannelMonitor for channel {} but was present in the (stale) ChannelManager",
13428
13510
&channel.context.channel_id(), &payment_hash);
13429
- failed_htlcs.push((channel_htlc_source.clone(), *payment_hash, channel.context.get_counterparty_node_id(), channel.context.channel_id()));
13511
+ failed_htlcs.push((
13512
+ channel_htlc_source.clone(),
13513
+ *payment_hash,
13514
+ channel.context.get_counterparty_node_id(),
13515
+ channel.context.channel_id(),
13516
+ None,
13517
+ ));
13430
13518
}
13431
13519
}
13432
13520
} else {
@@ -13955,14 +14043,23 @@ where
13955
14043
// generating a `PaymentPathSuccessful` event but regenerating
13956
14044
// it and the `PaymentSent` on every restart until the
13957
14045
// `ChannelMonitor` is removed.
13958
- let compl_action =
14046
+ let mut compl_action = Some(
13959
14047
EventCompletionAction::ReleaseRAAChannelMonitorUpdate {
13960
14048
channel_funding_outpoint: Some(monitor.get_funding_txo().0),
13961
14049
channel_id: monitor.channel_id(),
13962
14050
counterparty_node_id: path.hops[0].pubkey,
13963
- };
13964
- pending_outbounds.claim_htlc(payment_id, preimage, session_priv,
13965
- path, false, compl_action, &pending_events, &&logger);
14051
+ },
14052
+ );
14053
+ pending_outbounds.claim_htlc(
14054
+ payment_id,
14055
+ preimage,
14056
+ session_priv,
14057
+ path,
14058
+ false,
14059
+ &mut compl_action,
14060
+ &pending_events,
14061
+ &&logger,
14062
+ );
13966
14063
pending_events_read = pending_events.into_inner().unwrap();
13967
14064
}
13968
14065
},
@@ -13975,11 +14072,18 @@ where
13975
14072
"Failing HTLC with payment hash {} as it was resolved on-chain.",
13976
14073
payment_hash
13977
14074
);
14075
+ let completion_action = Some(PaymentCompleteUpdate {
14076
+ counterparty_node_id: node_id,
14077
+ channel_funding_outpoint: monitor.get_funding_txo().0,
14078
+ channel_id: monitor.channel_id(),
14079
+ htlc_id: SentHTLCId::from_source(&htlc_source),
14080
+ });
13978
14081
failed_htlcs.push((
13979
14082
htlc_source,
13980
14083
payment_hash,
13981
14084
node_id,
13982
14085
monitor.channel_id(),
14086
+ completion_action,
13983
14087
));
13984
14088
} else {
13985
14089
log_warn!(
@@ -14574,12 +14678,13 @@ where
14574
14678
}
14575
14679
}
14576
14680
14577
- for htlc_source in failed_htlcs.drain(..) {
14578
- let (source, payment_hash , counterparty_id, channel_id) = htlc_source;
14681
+ for htlc_source in failed_htlcs {
14682
+ let (source, hash , counterparty_id, channel_id, ev_action ) = htlc_source;
14579
14683
let receiver =
14580
14684
HTLCDestination::NextHopChannel { node_id: Some(counterparty_id), channel_id };
14581
14685
let reason = HTLCFailReason::from_failure_code(0x4000 | 8);
14582
- channel_manager.fail_htlc_backwards_internal(&source, &payment_hash, &reason, receiver);
14686
+ channel_manager
14687
+ .fail_htlc_backwards_internal(&source, &hash, &reason, receiver, ev_action);
14583
14688
}
14584
14689
14585
14690
for (source, preimage, downstream_value, downstream_closed, downstream_node_id, downstream_funding, downstream_channel_id) in pending_claims_to_replay {
0 commit comments