@@ -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
}
@@ -7358,14 +7406,34 @@ This indicates a bug inside LDK. Please report this error at https://github.com/
7358
7406
if let Some(pubkey) = next_channel_counterparty_node_id {
7359
7407
debug_assert_eq!(pubkey, path.hops[0].pubkey);
7360
7408
}
7361
- let ev_completion_action = EventCompletionAction::ReleaseRAAChannelMonitorUpdate {
7362
- channel_funding_outpoint: Some(next_channel_outpoint),
7363
- channel_id: next_channel_id,
7364
- counterparty_node_id: path.hops[0].pubkey,
7409
+ let mut ev_completion_action =
7410
+ Some(EventCompletionAction::ReleaseRAAChannelMonitorUpdate {
7411
+ channel_funding_outpoint: Some(next_channel_outpoint),
7412
+ channel_id: next_channel_id,
7413
+ counterparty_node_id: path.hops[0].pubkey,
7414
+ });
7415
+ self.pending_outbound_payments.claim_htlc(
7416
+ payment_id,
7417
+ payment_preimage,
7418
+ session_priv,
7419
+ path,
7420
+ from_onchain,
7421
+ &mut ev_completion_action,
7422
+ &self.pending_events,
7423
+ &self.logger,
7424
+ );
7425
+ // If an event was generated, `claim_htlc` set `ev_completion_action` to None, if
7426
+ // not, we should go ahead and run it now (as the claim was duplicative), at least
7427
+ // if a PaymentClaimed event with the same action isn't already pending.
7428
+ let have_action = if ev_completion_action.is_some() {
7429
+ let pending_events = self.pending_events.lock().unwrap();
7430
+ pending_events.iter().any(|(_, act)| *act == ev_completion_action)
7431
+ } else {
7432
+ false
7365
7433
};
7366
- self.pending_outbound_payments.claim_htlc(payment_id, payment_preimage,
7367
- session_priv, path, from_onchain, ev_completion_action, & self.pending_events,
7368
- &self.logger);
7434
+ if !have_action {
7435
+ self.handle_post_event_actions(ev_completion_action);
7436
+ }
7369
7437
},
7370
7438
HTLCSource::PreviousHopData(hop_data) => {
7371
7439
let prev_channel_id = hop_data.channel_id;
@@ -8711,7 +8779,7 @@ This indicates a bug inside LDK. Please report this error at https://github.com/
8711
8779
for htlc_source in dropped_htlcs.drain(..) {
8712
8780
let receiver = HTLCDestination::NextHopChannel { node_id: Some(counterparty_node_id.clone()), channel_id: msg.channel_id };
8713
8781
let reason = HTLCFailReason::from_failure_code(0x4000 | 8);
8714
- self.fail_htlc_backwards_internal(&htlc_source.0, &htlc_source.1, &reason, receiver);
8782
+ self.fail_htlc_backwards_internal(&htlc_source.0, &htlc_source.1, &reason, receiver, None );
8715
8783
}
8716
8784
if let Some(shutdown_res) = finish_shutdown {
8717
8785
self.finish_close_channel(shutdown_res);
@@ -9122,7 +9190,13 @@ This indicates a bug inside LDK. Please report this error at https://github.com/
9122
9190
}
9123
9191
9124
9192
for (htlc_source, payment_hash, failure_reason, destination) in failed_intercept_forwards.drain(..) {
9125
- push_forward_event |= self.fail_htlc_backwards_internal_without_forward_event(&htlc_source, &payment_hash, &failure_reason, destination);
9193
+ push_forward_event |= self.fail_htlc_backwards_internal_without_forward_event(
9194
+ &htlc_source,
9195
+ &payment_hash,
9196
+ &failure_reason,
9197
+ destination,
9198
+ None,
9199
+ );
9126
9200
}
9127
9201
9128
9202
if !new_intercept_events.is_empty() {
@@ -9463,7 +9537,13 @@ This indicates a bug inside LDK. Please report this error at https://github.com/
9463
9537
log_trace!(logger, "Failing HTLC with hash {} from our monitor", &htlc_update.payment_hash);
9464
9538
let receiver = HTLCDestination::NextHopChannel { node_id: counterparty_node_id, channel_id };
9465
9539
let reason = HTLCFailReason::from_failure_code(0x4000 | 8);
9466
- self.fail_htlc_backwards_internal(&htlc_update.source, &htlc_update.payment_hash, &reason, receiver);
9540
+ self.fail_htlc_backwards_internal(
9541
+ &htlc_update.source,
9542
+ &htlc_update.payment_hash,
9543
+ &reason,
9544
+ receiver,
9545
+ None,
9546
+ );
9467
9547
}
9468
9548
},
9469
9549
MonitorEvent::HolderForceClosed(_) | MonitorEvent::HolderForceClosedWithInfo { .. } => {
@@ -10808,8 +10888,8 @@ where
10808
10888
}
10809
10889
}
10810
10890
10811
- fn handle_post_event_actions(&self, actions: Vec<EventCompletionAction> ) {
10812
- for action in actions {
10891
+ fn handle_post_event_actions<I: IntoIterator<Item = EventCompletionAction>> (&self, actions: I ) {
10892
+ for action in actions.into_iter() {
10813
10893
match action {
10814
10894
EventCompletionAction::ReleaseRAAChannelMonitorUpdate {
10815
10895
channel_funding_outpoint: _,
@@ -11315,7 +11395,7 @@ where
11315
11395
}
11316
11396
11317
11397
for (source, payment_hash, reason, destination) in timed_out_htlcs.drain(..) {
11318
- self.fail_htlc_backwards_internal(&source, &payment_hash, &reason, destination);
11398
+ self.fail_htlc_backwards_internal(&source, &payment_hash, &reason, destination, None );
11319
11399
}
11320
11400
}
11321
11401
@@ -13380,7 +13460,7 @@ where
13380
13460
log_error!(logger, " The ChannelMonitor for channel {} is at counterparty commitment transaction number {} but the ChannelManager is at counterparty commitment transaction number {}.",
13381
13461
&channel.context.channel_id(), monitor.get_cur_counterparty_commitment_number(), channel.get_cur_counterparty_commitment_transaction_number());
13382
13462
}
13383
- let mut shutdown_result = channel.context.force_shutdown(true, ClosureReason::OutdatedChannelManager);
13463
+ let shutdown_result = channel.context.force_shutdown(true, ClosureReason::OutdatedChannelManager);
13384
13464
if shutdown_result.unbroadcasted_batch_funding_txid.is_some() {
13385
13465
return Err(DecodeError::InvalidValue);
13386
13466
}
@@ -13401,7 +13481,9 @@ where
13401
13481
counterparty_node_id, funding_txo, channel_id, update
13402
13482
});
13403
13483
}
13404
- failed_htlcs.append(&mut shutdown_result.dropped_outbound_htlcs);
13484
+ for (source, hash, cp_id, chan_id) in shutdown_result.dropped_outbound_htlcs {
13485
+ failed_htlcs.push((source, hash, cp_id, chan_id, None));
13486
+ }
13405
13487
channel_closures.push_back((events::Event::ChannelClosed {
13406
13488
channel_id: channel.context.channel_id(),
13407
13489
user_channel_id: channel.context.get_user_id(),
@@ -13428,7 +13510,13 @@ where
13428
13510
log_info!(logger,
13429
13511
"Failing HTLC with hash {} as it is missing in the ChannelMonitor for channel {} but was present in the (stale) ChannelManager",
13430
13512
&channel.context.channel_id(), &payment_hash);
13431
- failed_htlcs.push((channel_htlc_source.clone(), *payment_hash, channel.context.get_counterparty_node_id(), channel.context.channel_id()));
13513
+ failed_htlcs.push((
13514
+ channel_htlc_source.clone(),
13515
+ *payment_hash,
13516
+ channel.context.get_counterparty_node_id(),
13517
+ channel.context.channel_id(),
13518
+ None,
13519
+ ));
13432
13520
}
13433
13521
}
13434
13522
} else {
@@ -13956,14 +14044,23 @@ where
13956
14044
// generating a `PaymentPathSuccessful` event but regenerating
13957
14045
// it and the `PaymentSent` on every restart until the
13958
14046
// `ChannelMonitor` is removed.
13959
- let compl_action =
14047
+ let mut compl_action = Some(
13960
14048
EventCompletionAction::ReleaseRAAChannelMonitorUpdate {
13961
14049
channel_funding_outpoint: Some(monitor.get_funding_txo().0),
13962
14050
channel_id: monitor.channel_id(),
13963
14051
counterparty_node_id: path.hops[0].pubkey,
13964
- };
13965
- pending_outbounds.claim_htlc(payment_id, preimage, session_priv,
13966
- path, false, compl_action, &pending_events, &&logger);
14052
+ },
14053
+ );
14054
+ pending_outbounds.claim_htlc(
14055
+ payment_id,
14056
+ preimage,
14057
+ session_priv,
14058
+ path,
14059
+ false,
14060
+ &mut compl_action,
14061
+ &pending_events,
14062
+ &&logger,
14063
+ );
13967
14064
pending_events_read = pending_events.into_inner().unwrap();
13968
14065
}
13969
14066
},
@@ -13976,11 +14073,18 @@ where
13976
14073
"Failing HTLC with payment hash {} as it was resolved on-chain.",
13977
14074
payment_hash
13978
14075
);
14076
+ let completion_action = Some(PaymentCompleteUpdate {
14077
+ counterparty_node_id: node_id,
14078
+ channel_funding_outpoint: monitor.get_funding_txo().0,
14079
+ channel_id: monitor.channel_id(),
14080
+ htlc_id: SentHTLCId::from_source(&htlc_source),
14081
+ });
13979
14082
failed_htlcs.push((
13980
14083
htlc_source,
13981
14084
payment_hash,
13982
14085
node_id,
13983
14086
monitor.channel_id(),
14087
+ completion_action,
13984
14088
));
13985
14089
} else {
13986
14090
log_warn!(
@@ -14575,12 +14679,13 @@ where
14575
14679
}
14576
14680
}
14577
14681
14578
- for htlc_source in failed_htlcs.drain(..) {
14579
- let (source, payment_hash , counterparty_id, channel_id) = htlc_source;
14682
+ for htlc_source in failed_htlcs {
14683
+ let (source, hash , counterparty_id, channel_id, ev_action ) = htlc_source;
14580
14684
let receiver =
14581
14685
HTLCDestination::NextHopChannel { node_id: Some(counterparty_id), channel_id };
14582
14686
let reason = HTLCFailReason::from_failure_code(0x4000 | 8);
14583
- channel_manager.fail_htlc_backwards_internal(&source, &payment_hash, &reason, receiver);
14687
+ channel_manager
14688
+ .fail_htlc_backwards_internal(&source, &hash, &reason, receiver, ev_action);
14584
14689
}
14585
14690
14586
14691
for (source, preimage, downstream_value, downstream_closed, downstream_node_id, downstream_funding, downstream_channel_id) in pending_claims_to_replay {
0 commit comments