Skip to content

Commit e5fa4aa

Browse files
committed
adds 'receiver_node_id' to 'Event::Payment{Received,Claimed}'
1 parent 3c7365d commit e5fa4aa

File tree

7 files changed

+78
-17
lines changed

7 files changed

+78
-17
lines changed

lightning-invoice/src/utils.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -1081,7 +1081,7 @@ mod test {
10811081
nodes[fwd_idx].node.process_pending_htlc_forwards();
10821082

10831083
let payment_preimage_opt = if user_generated_pmt_hash { None } else { Some(payment_preimage) };
1084-
expect_payment_received!(&nodes[fwd_idx], payment_hash, payment_secret, payment_amt, payment_preimage_opt);
1084+
expect_payment_received!(&nodes[fwd_idx], payment_hash, payment_secret, payment_amt, payment_preimage_opt, route.paths[0].last().unwrap().pubkey);
10851085
do_claim_payment_along_route(&nodes[0], &vec!(&vec!(&nodes[fwd_idx])[..]), false, payment_preimage);
10861086
let events = nodes[0].node.get_and_clear_pending_events();
10871087
assert_eq!(events.len(), 2);

lightning/src/ln/chanmon_update_fail_tests.rs

+10-5
Original file line numberDiff line numberDiff line change
@@ -200,9 +200,10 @@ fn do_test_simple_monitor_temporary_update_fail(disconnect: bool) {
200200
let events_3 = nodes[1].node.get_and_clear_pending_events();
201201
assert_eq!(events_3.len(), 1);
202202
match events_3[0] {
203-
Event::PaymentReceived { ref payment_hash, ref purpose, amount_msat } => {
203+
Event::PaymentReceived { ref payment_hash, ref purpose, amount_msat, receiver_node_id } => {
204204
assert_eq!(payment_hash_1, *payment_hash);
205205
assert_eq!(amount_msat, 1_000_000);
206+
assert_eq!(receiver_node_id.unwrap(), nodes[1].node.get_our_node_id());
206207
match &purpose {
207208
PaymentPurpose::InvoicePayment { payment_preimage, payment_secret, .. } => {
208209
assert!(payment_preimage.is_none());
@@ -568,9 +569,10 @@ fn do_test_monitor_temporary_update_fail(disconnect_count: usize) {
568569
let events_5 = nodes[1].node.get_and_clear_pending_events();
569570
assert_eq!(events_5.len(), 1);
570571
match events_5[0] {
571-
Event::PaymentReceived { ref payment_hash, ref purpose, amount_msat } => {
572+
Event::PaymentReceived { ref payment_hash, ref purpose, amount_msat, receiver_node_id } => {
572573
assert_eq!(payment_hash_2, *payment_hash);
573574
assert_eq!(amount_msat, 1_000_000);
575+
assert_eq!(receiver_node_id.unwrap(), nodes[1].node.get_our_node_id());
574576
match &purpose {
575577
PaymentPurpose::InvoicePayment { payment_preimage, payment_secret, .. } => {
576578
assert!(payment_preimage.is_none());
@@ -685,9 +687,10 @@ fn test_monitor_update_fail_cs() {
685687
let events = nodes[1].node.get_and_clear_pending_events();
686688
assert_eq!(events.len(), 1);
687689
match events[0] {
688-
Event::PaymentReceived { payment_hash, ref purpose, amount_msat } => {
690+
Event::PaymentReceived { payment_hash, ref purpose, amount_msat, receiver_node_id } => {
689691
assert_eq!(payment_hash, our_payment_hash);
690692
assert_eq!(amount_msat, 1_000_000);
693+
assert_eq!(receiver_node_id.unwrap(), nodes[1].node.get_our_node_id());
691694
match &purpose {
692695
PaymentPurpose::InvoicePayment { payment_preimage, payment_secret, .. } => {
693696
assert!(payment_preimage.is_none());
@@ -1659,9 +1662,10 @@ fn test_monitor_update_fail_claim() {
16591662
let events = nodes[0].node.get_and_clear_pending_events();
16601663
assert_eq!(events.len(), 2);
16611664
match events[0] {
1662-
Event::PaymentReceived { ref payment_hash, ref purpose, amount_msat } => {
1665+
Event::PaymentReceived { ref payment_hash, ref purpose, amount_msat, receiver_node_id } => {
16631666
assert_eq!(payment_hash_2, *payment_hash);
16641667
assert_eq!(1_000_000, amount_msat);
1668+
assert_eq!(receiver_node_id.unwrap(), nodes[0].node.get_our_node_id());
16651669
match &purpose {
16661670
PaymentPurpose::InvoicePayment { payment_preimage, payment_secret, .. } => {
16671671
assert!(payment_preimage.is_none());
@@ -1673,9 +1677,10 @@ fn test_monitor_update_fail_claim() {
16731677
_ => panic!("Unexpected event"),
16741678
}
16751679
match events[1] {
1676-
Event::PaymentReceived { ref payment_hash, ref purpose, amount_msat } => {
1680+
Event::PaymentReceived { ref payment_hash, ref purpose, amount_msat, receiver_node_id } => {
16771681
assert_eq!(payment_hash_3, *payment_hash);
16781682
assert_eq!(1_000_000, amount_msat);
1683+
assert_eq!(receiver_node_id.unwrap(), nodes[0].node.get_our_node_id());
16791684
match &purpose {
16801685
PaymentPurpose::InvoicePayment { payment_preimage, payment_secret, .. } => {
16811686
assert!(payment_preimage.is_none());

lightning/src/ln/channelmanager.rs

+30
Original file line numberDiff line numberDiff line change
@@ -3445,6 +3445,12 @@ impl<M: Deref, T: Deref, K: Deref, F: Deref, L: Deref> ChannelManager<M, T, K, F
34453445
));
34463446
}
34473447
}
3448+
let phantom_shared_secret = claimable_htlc.prev_hop.phantom_shared_secret;
3449+
let mut receiver_node_id = self.our_network_pubkey;
3450+
if phantom_shared_secret.is_some() {
3451+
receiver_node_id = self.keys_manager.get_node_id(Recipient::PhantomNode)
3452+
.expect("Failed to get node_id for phantom node recipient");
3453+
}
34483454

34493455
macro_rules! check_total_value {
34503456
($payment_data: expr, $payment_preimage: expr) => {{
@@ -3486,6 +3492,7 @@ impl<M: Deref, T: Deref, K: Deref, F: Deref, L: Deref> ChannelManager<M, T, K, F
34863492
} else if total_value == $payment_data.total_msat {
34873493
htlcs.push(claimable_htlc);
34883494
new_events.push(events::Event::PaymentReceived {
3495+
receiver_node_id: Some(receiver_node_id),
34893496
payment_hash,
34903497
purpose: purpose(),
34913498
amount_msat: total_value,
@@ -3526,8 +3533,15 @@ impl<M: Deref, T: Deref, K: Deref, F: Deref, L: Deref> ChannelManager<M, T, K, F
35263533
match channel_state.claimable_htlcs.entry(payment_hash) {
35273534
hash_map::Entry::Vacant(e) => {
35283535
let purpose = events::PaymentPurpose::SpontaneousPayment(preimage);
3536+
let phantom_shared_secret = claimable_htlc.prev_hop.phantom_shared_secret;
35293537
e.insert((purpose.clone(), vec![claimable_htlc]));
3538+
if phantom_shared_secret.is_some() {
3539+
let phantom_pubkey = self.keys_manager.get_node_id(Recipient::PhantomNode)
3540+
.expect("Failed to get node_id for phantom node recipient");
3541+
receiver_node_id = phantom_pubkey
3542+
}
35303543
new_events.push(events::Event::PaymentReceived {
3544+
receiver_node_id: Some(receiver_node_id),
35313545
payment_hash,
35323546
amount_msat: outgoing_amt_msat,
35333547
purpose,
@@ -4194,6 +4208,7 @@ impl<M: Deref, T: Deref, K: Deref, F: Deref, L: Deref> ChannelManager<M, T, K, F
41944208
let mut claimed_any_htlcs = false;
41954209
let mut channel_state_lock = self.channel_state.lock().unwrap();
41964210
let channel_state = &mut *channel_state_lock;
4211+
let mut receiver_node_id = Some(self.our_network_pubkey);
41974212
for htlc in sources.iter() {
41984213
let chan_id = match self.short_to_chan_info.read().unwrap().get(&htlc.prev_hop.short_channel_id) {
41994214
Some((_cp_id, chan_id)) => chan_id.clone(),
@@ -4225,6 +4240,12 @@ impl<M: Deref, T: Deref, K: Deref, F: Deref, L: Deref> ChannelManager<M, T, K, F
42254240
break;
42264241
}
42274242
}
4243+
let phantom_shared_secret = htlc.prev_hop.phantom_shared_secret;
4244+
if phantom_shared_secret.is_some() {
4245+
let phantom_pubkey = self.keys_manager.get_node_id(Recipient::PhantomNode)
4246+
.expect("Failed to get node_id for phantom node recipient");
4247+
receiver_node_id = Some(phantom_pubkey)
4248+
}
42284249

42294250
claimable_amt_msat += htlc.value;
42304251
}
@@ -4274,6 +4295,7 @@ impl<M: Deref, T: Deref, K: Deref, F: Deref, L: Deref> ChannelManager<M, T, K, F
42744295

42754296
if claimed_any_htlcs {
42764297
self.pending_events.lock().unwrap().push(events::Event::PaymentClaimed {
4298+
receiver_node_id,
42774299
payment_hash,
42784300
purpose: payment_purpose,
42794301
amount_msat: claimable_amt_msat,
@@ -7487,6 +7509,13 @@ impl<'a, M: Deref, T: Deref, K: Deref, F: Deref, L: Deref>
74877509
if let Some((payment_purpose, claimable_htlcs)) = claimable_htlcs.remove(&payment_hash) {
74887510
log_info!(args.logger, "Re-claiming HTLCs with payment hash {} as we've released the preimage to a ChannelMonitor!", log_bytes!(payment_hash.0));
74897511
let mut claimable_amt_msat = 0;
7512+
let mut receiver_node_id = Some(our_network_pubkey);
7513+
let phantom_shared_secret = claimable_htlcs[0].prev_hop.phantom_shared_secret;
7514+
if phantom_shared_secret.is_some() {
7515+
let phantom_pubkey = args.keys_manager.get_node_id(Recipient::PhantomNode)
7516+
.expect("Failed to get node_id for phantom node recipient");
7517+
receiver_node_id = Some(phantom_pubkey)
7518+
}
74907519
for claimable_htlc in claimable_htlcs {
74917520
claimable_amt_msat += claimable_htlc.value;
74927521

@@ -7514,6 +7543,7 @@ impl<'a, M: Deref, T: Deref, K: Deref, F: Deref, L: Deref>
75147543
}
75157544
}
75167545
pending_events_read.push(events::Event::PaymentClaimed {
7546+
receiver_node_id,
75177547
payment_hash,
75187548
purpose: payment_purpose,
75197549
amount_msat: claimable_amt_msat,

lightning/src/ln/functional_test_utils.rs

+6-5
Original file line numberDiff line numberDiff line change
@@ -1433,20 +1433,20 @@ macro_rules! expect_pending_htlcs_forwardable_from_events {
14331433
}
14341434
}}
14351435
}
1436-
14371436
#[macro_export]
14381437
#[cfg(any(test, feature = "_bench_unstable", feature = "_test_utils"))]
14391438
macro_rules! expect_payment_received {
14401439
($node: expr, $expected_payment_hash: expr, $expected_payment_secret: expr, $expected_recv_value: expr) => {
1441-
expect_payment_received!($node, $expected_payment_hash, $expected_payment_secret, $expected_recv_value, None)
1440+
expect_payment_received!($node, $expected_payment_hash, $expected_payment_secret, $expected_recv_value, None, $node.node.get_our_node_id())
14421441
};
1443-
($node: expr, $expected_payment_hash: expr, $expected_payment_secret: expr, $expected_recv_value: expr, $expected_payment_preimage: expr) => {
1442+
($node: expr, $expected_payment_hash: expr, $expected_payment_secret: expr, $expected_recv_value: expr, $expected_payment_preimage: expr, $expected_receiver_node_id: expr) => {
14441443
let events = $node.node.get_and_clear_pending_events();
14451444
assert_eq!(events.len(), 1);
14461445
match events[0] {
1447-
$crate::util::events::Event::PaymentReceived { ref payment_hash, ref purpose, amount_msat } => {
1446+
$crate::util::events::Event::PaymentReceived { ref payment_hash, ref purpose, amount_msat, receiver_node_id } => {
14481447
assert_eq!($expected_payment_hash, *payment_hash);
14491448
assert_eq!($expected_recv_value, amount_msat);
1449+
assert_eq!($expected_receiver_node_id, receiver_node_id.unwrap());
14501450
match purpose {
14511451
$crate::util::events::PaymentPurpose::InvoicePayment { payment_preimage, payment_secret, .. } => {
14521452
assert_eq!(&$expected_payment_preimage, payment_preimage);
@@ -1738,8 +1738,9 @@ pub fn do_pass_along_path<'a, 'b, 'c>(origin_node: &Node<'a, 'b, 'c>, expected_p
17381738
if payment_received_expected {
17391739
assert_eq!(events_2.len(), 1);
17401740
match events_2[0] {
1741-
Event::PaymentReceived { ref payment_hash, ref purpose, amount_msat } => {
1741+
Event::PaymentReceived { ref payment_hash, ref purpose, amount_msat, receiver_node_id } => {
17421742
assert_eq!(our_payment_hash, *payment_hash);
1743+
assert_eq!(node.node.get_our_node_id(), receiver_node_id.unwrap());
17431744
match &purpose {
17441745
PaymentPurpose::InvoicePayment { payment_preimage, payment_secret, .. } => {
17451746
assert_eq!(expected_preimage, *payment_preimage);

lightning/src/ln/functional_tests.rs

+6-3
Original file line numberDiff line numberDiff line change
@@ -1956,9 +1956,10 @@ fn test_channel_reserve_holding_cell_htlcs() {
19561956
let events = nodes[2].node.get_and_clear_pending_events();
19571957
assert_eq!(events.len(), 2);
19581958
match events[0] {
1959-
Event::PaymentReceived { ref payment_hash, ref purpose, amount_msat } => {
1959+
Event::PaymentReceived { ref payment_hash, ref purpose, amount_msat, receiver_node_id } => {
19601960
assert_eq!(our_payment_hash_21, *payment_hash);
19611961
assert_eq!(recv_value_21, amount_msat);
1962+
assert_eq!(nodes[2].node.get_our_node_id(), receiver_node_id.unwrap());
19621963
match &purpose {
19631964
PaymentPurpose::InvoicePayment { payment_preimage, payment_secret, .. } => {
19641965
assert!(payment_preimage.is_none());
@@ -1970,9 +1971,10 @@ fn test_channel_reserve_holding_cell_htlcs() {
19701971
_ => panic!("Unexpected event"),
19711972
}
19721973
match events[1] {
1973-
Event::PaymentReceived { ref payment_hash, ref purpose, amount_msat } => {
1974+
Event::PaymentReceived { ref payment_hash, ref purpose, amount_msat, receiver_node_id } => {
19741975
assert_eq!(our_payment_hash_22, *payment_hash);
19751976
assert_eq!(recv_value_22, amount_msat);
1977+
assert_eq!(nodes[2].node.get_our_node_id(), receiver_node_id.unwrap());
19761978
match &purpose {
19771979
PaymentPurpose::InvoicePayment { payment_preimage, payment_secret, .. } => {
19781980
assert!(payment_preimage.is_none());
@@ -3723,9 +3725,10 @@ fn do_test_drop_messages_peer_disconnect(messages_delivered: u8, simulate_broken
37233725
let events_2 = nodes[1].node.get_and_clear_pending_events();
37243726
assert_eq!(events_2.len(), 1);
37253727
match events_2[0] {
3726-
Event::PaymentReceived { ref payment_hash, ref purpose, amount_msat } => {
3728+
Event::PaymentReceived { ref payment_hash, ref purpose, amount_msat, receiver_node_id } => {
37273729
assert_eq!(payment_hash_1, *payment_hash);
37283730
assert_eq!(amount_msat, 1_000_000);
3731+
assert_eq!(receiver_node_id.unwrap(), nodes[1].node.get_our_node_id());
37293732
match &purpose {
37303733
PaymentPurpose::InvoicePayment { payment_preimage, payment_secret, .. } => {
37313734
assert!(payment_preimage.is_none());

lightning/src/ln/onion_route_tests.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -1229,7 +1229,7 @@ fn test_phantom_failure_reject_payment() {
12291229
nodes[1].node.process_pending_htlc_forwards();
12301230
expect_pending_htlcs_forwardable_ignore!(nodes[1]);
12311231
nodes[1].node.process_pending_htlc_forwards();
1232-
expect_payment_received!(nodes[1], payment_hash, payment_secret, recv_amt_msat);
1232+
expect_payment_received!(nodes[1], payment_hash, payment_secret, recv_amt_msat, None, route.paths[0].last().unwrap().pubkey);
12331233
nodes[1].node.fail_htlc_backwards(&payment_hash);
12341234
expect_pending_htlcs_forwardable_and_htlc_handling_failed_ignore!(nodes[1], vec![HTLCDestination::FailedPayment { payment_hash }]);
12351235
nodes[1].node.process_pending_htlc_forwards();

lightning/src/util/events.rs

+24-2
Original file line numberDiff line numberDiff line change
@@ -342,6 +342,13 @@ pub enum Event {
342342
/// [`ChannelManager::claim_funds`]: crate::ln::channelmanager::ChannelManager::claim_funds
343343
/// [`ChannelManager::fail_htlc_backwards`]: crate::ln::channelmanager::ChannelManager::fail_htlc_backwards
344344
PaymentReceived {
345+
/// The node that received the payment.
346+
/// This is useful to identify payments which were received via [phantom node payments].
347+
/// This field will always be filled in when the event was generated by LDK
348+
/// versions 0.0.113 and above.
349+
///
350+
/// [phantom node payments]: crate::chain::keysinterface::PhantomKeysManager
351+
receiver_node_id: Option<PublicKey>,
345352
/// The hash for which the preimage should be handed to the ChannelManager. Note that LDK will
346353
/// not stop you from registering duplicate payment hashes for inbound payments.
347354
payment_hash: PaymentHash,
@@ -366,6 +373,13 @@ pub enum Event {
366373
///
367374
/// [`ChannelManager::claim_funds`]: crate::ln::channelmanager::ChannelManager::claim_funds
368375
PaymentClaimed {
376+
/// The node that received the payment.
377+
/// This is useful to identify payments which were received via [phantom node payments].
378+
/// This field will always be filled in when the event was generated by LDK
379+
/// versions 0.0.113 and above.
380+
///
381+
/// [phantom node payments]: crate::chain::keysinterface::PhantomKeysManager
382+
receiver_node_id: Option<PublicKey>,
369383
/// The payment hash of the claimed payment. Note that LDK will not stop you from
370384
/// registering duplicate payment hashes for inbound payments.
371385
payment_hash: PaymentHash,
@@ -739,7 +753,7 @@ impl Writeable for Event {
739753
// We never write out FundingGenerationReady events as, upon disconnection, peers
740754
// drop any channels which have not yet exchanged funding_signed.
741755
},
742-
&Event::PaymentReceived { ref payment_hash, ref amount_msat, ref purpose } => {
756+
&Event::PaymentReceived { ref payment_hash, ref amount_msat, ref purpose, ref receiver_node_id } => {
743757
1u8.write(writer)?;
744758
let mut payment_secret = None;
745759
let payment_preimage;
@@ -754,6 +768,7 @@ impl Writeable for Event {
754768
}
755769
write_tlv_fields!(writer, {
756770
(0, payment_hash, required),
771+
(1, receiver_node_id, option),
757772
(2, payment_secret, option),
758773
(4, amount_msat, required),
759774
(6, 0u64, required), // user_payment_id required for compatibility with 0.0.103 and earlier
@@ -854,10 +869,11 @@ impl Writeable for Event {
854869
// We never write the OpenChannelRequest events as, upon disconnection, peers
855870
// drop any channels which have not yet exchanged funding_signed.
856871
},
857-
&Event::PaymentClaimed { ref payment_hash, ref amount_msat, ref purpose } => {
872+
&Event::PaymentClaimed { ref payment_hash, ref amount_msat, ref purpose, ref receiver_node_id } => {
858873
19u8.write(writer)?;
859874
write_tlv_fields!(writer, {
860875
(0, payment_hash, required),
876+
(1, receiver_node_id, option),
861877
(2, purpose, required),
862878
(4, amount_msat, required),
863879
});
@@ -923,9 +939,11 @@ impl MaybeReadable for Event {
923939
let mut payment_preimage = None;
924940
let mut payment_secret = None;
925941
let mut amount_msat = 0;
942+
let mut receiver_node_id = None;
926943
let mut _user_payment_id = None::<u64>; // For compatibility with 0.0.103 and earlier
927944
read_tlv_fields!(reader, {
928945
(0, payment_hash, required),
946+
(1, receiver_node_id, option),
929947
(2, payment_secret, option),
930948
(4, amount_msat, required),
931949
(6, _user_payment_id, option),
@@ -940,6 +958,7 @@ impl MaybeReadable for Event {
940958
None => return Err(msgs::DecodeError::InvalidValue),
941959
};
942960
Ok(Some(Event::PaymentReceived {
961+
receiver_node_id,
943962
payment_hash,
944963
amount_msat,
945964
purpose,
@@ -1117,13 +1136,16 @@ impl MaybeReadable for Event {
11171136
let mut payment_hash = PaymentHash([0; 32]);
11181137
let mut purpose = None;
11191138
let mut amount_msat = 0;
1139+
let mut receiver_node_id = None;
11201140
read_tlv_fields!(reader, {
11211141
(0, payment_hash, required),
1142+
(1, receiver_node_id, option),
11221143
(2, purpose, ignorable),
11231144
(4, amount_msat, required),
11241145
});
11251146
if purpose.is_none() { return Ok(None); }
11261147
Ok(Some(Event::PaymentClaimed {
1148+
receiver_node_id,
11271149
payment_hash,
11281150
purpose: purpose.unwrap(),
11291151
amount_msat,

0 commit comments

Comments
 (0)