Skip to content

Commit c5e963b

Browse files
Prevent duplicate PaymentSent events
by removing all pending outbound payments associated with the same MPP payment after the preimage is received
1 parent d24db38 commit c5e963b

File tree

3 files changed

+10
-14
lines changed

3 files changed

+10
-14
lines changed

lightning/src/ln/channelmanager.rs

Lines changed: 2 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -3144,16 +3144,13 @@ impl<Signer: Sign, M: Deref, T: Deref, K: Deref, F: Deref, L: Deref> ChannelMana
31443144
let mut session_priv_bytes = [0; 32];
31453145
session_priv_bytes.copy_from_slice(&session_priv[..]);
31463146
let mut outbounds = self.pending_outbound_payments.lock().unwrap();
3147-
if let Some(sessions) = outbounds.get_mut(&mpp_id) {
3147+
if let Some(mut sessions) = outbounds.remove(&mpp_id) {
31483148
if sessions.remove(&session_priv_bytes) {
31493149
self.pending_events.lock().unwrap().push(
31503150
events::Event::PaymentSent { payment_preimage }
31513151
);
3152-
if sessions.len() == 0 {
3153-
outbounds.remove(&mpp_id);
3154-
}
31553152
return
3156-
}
3153+
} else { unreachable!(); }
31573154
}
31583155
log_trace!(self.logger, "Received duplicative fulfill for HTLC with payment_preimage {}", log_bytes!(payment_preimage.0));
31593156
},
@@ -5687,20 +5684,13 @@ mod tests {
56875684
nodes[0].node.handle_revoke_and_ack(&nodes[1].node.get_our_node_id(), &bs_third_raa);
56885685
check_added_monitors!(nodes[0], 1);
56895686

5690-
// There's an existing bug that generates a PaymentSent event for each MPP path, so handle that here.
56915687
let events = nodes[0].node.get_and_clear_pending_events();
56925688
match events[0] {
56935689
Event::PaymentSent { payment_preimage: ref preimage } => {
56945690
assert_eq!(payment_preimage, *preimage);
56955691
},
56965692
_ => panic!("Unexpected event"),
56975693
}
5698-
match events[1] {
5699-
Event::PaymentSent { payment_preimage: ref preimage } => {
5700-
assert_eq!(payment_preimage, *preimage);
5701-
},
5702-
_ => panic!("Unexpected event"),
5703-
}
57045694
}
57055695

57065696
#[test]

lightning/src/ln/functional_test_utils.rs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1237,9 +1237,11 @@ pub fn claim_payment_along_route<'a, 'b, 'c>(origin_node: &Node<'a, 'b, 'c>, exp
12371237

12381238
if !skip_last {
12391239
last_update_fulfill_dance!(origin_node, expected_route.first().unwrap());
1240-
expect_payment_sent!(origin_node, our_payment_preimage);
12411240
}
12421241
}
1242+
if !skip_last {
1243+
expect_payment_sent!(origin_node, our_payment_preimage);
1244+
}
12431245
}
12441246

12451247
pub fn claim_payment<'a, 'b, 'c>(origin_node: &Node<'a, 'b, 'c>, expected_route: &[&Node<'a, 'b, 'c>], our_payment_preimage: PaymentPreimage) {

lightning/src/util/events.rs

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -111,8 +111,12 @@ pub enum Event {
111111
/// payment is to pay an invoice or to send a spontaneous payment.
112112
purpose: PaymentPurpose,
113113
},
114-
/// Indicates an outbound payment we made succeeded (ie it made it all the way to its target
114+
/// Indicates an outbound payment we made succeeded (i.e. it made it all the way to its target
115115
/// and we got back the payment preimage for it).
116+
///
117+
/// Note for MPP payments: we generate this event as soon as one of the payment paths succeeds. In
118+
/// the case of a buggy payee, this may be followed by a PaymentFailed or MPPFragmentFailed event
119+
/// if subsequent path(s) fail. However, this case should be extremely rare.
116120
PaymentSent {
117121
/// The preimage to the hash given to ChannelManager::send_payment.
118122
/// Note that this serves as a payment receipt, if you wish to have such a thing, you must

0 commit comments

Comments
 (0)