Skip to content

Commit 64b9e83

Browse files
authored
Merge pull request #1766 from tee8z/event-node-received
adds node_id to Event::Payment{Received, Claimed}
2 parents 46aa613 + babde3a commit 64b9e83

File tree

7 files changed

+72
-17
lines changed

7 files changed

+72
-17
lines changed

lightning-invoice/src/utils.rs

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

10851085
let payment_preimage_opt = if user_generated_pmt_hash { None } else { Some(payment_preimage) };
1086-
expect_payment_received!(&nodes[fwd_idx], payment_hash, payment_secret, payment_amt, payment_preimage_opt);
1086+
expect_payment_received!(&nodes[fwd_idx], payment_hash, payment_secret, payment_amt, payment_preimage_opt, route.paths[0].last().unwrap().pubkey);
10871087
do_claim_payment_along_route(&nodes[0], &vec!(&vec!(&nodes[fwd_idx])[..]), false, payment_preimage);
10881088
let events = nodes[0].node.get_and_clear_pending_events();
10891089
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());
@@ -567,9 +568,10 @@ fn do_test_monitor_temporary_update_fail(disconnect_count: usize) {
567568
let events_5 = nodes[1].node.get_and_clear_pending_events();
568569
assert_eq!(events_5.len(), 1);
569570
match events_5[0] {
570-
Event::PaymentReceived { ref payment_hash, ref purpose, amount_msat } => {
571+
Event::PaymentReceived { ref payment_hash, ref purpose, amount_msat, receiver_node_id } => {
571572
assert_eq!(payment_hash_2, *payment_hash);
572573
assert_eq!(amount_msat, 1_000_000);
574+
assert_eq!(receiver_node_id.unwrap(), nodes[1].node.get_our_node_id());
573575
match &purpose {
574576
PaymentPurpose::InvoicePayment { payment_preimage, payment_secret, .. } => {
575577
assert!(payment_preimage.is_none());
@@ -682,9 +684,10 @@ fn test_monitor_update_fail_cs() {
682684
let events = nodes[1].node.get_and_clear_pending_events();
683685
assert_eq!(events.len(), 1);
684686
match events[0] {
685-
Event::PaymentReceived { payment_hash, ref purpose, amount_msat } => {
687+
Event::PaymentReceived { payment_hash, ref purpose, amount_msat, receiver_node_id } => {
686688
assert_eq!(payment_hash, our_payment_hash);
687689
assert_eq!(amount_msat, 1_000_000);
690+
assert_eq!(receiver_node_id.unwrap(), nodes[1].node.get_our_node_id());
688691
match &purpose {
689692
PaymentPurpose::InvoicePayment { payment_preimage, payment_secret, .. } => {
690693
assert!(payment_preimage.is_none());
@@ -1645,9 +1648,10 @@ fn test_monitor_update_fail_claim() {
16451648
let events = nodes[0].node.get_and_clear_pending_events();
16461649
assert_eq!(events.len(), 2);
16471650
match events[0] {
1648-
Event::PaymentReceived { ref payment_hash, ref purpose, amount_msat } => {
1651+
Event::PaymentReceived { ref payment_hash, ref purpose, amount_msat, receiver_node_id } => {
16491652
assert_eq!(payment_hash_2, *payment_hash);
16501653
assert_eq!(1_000_000, amount_msat);
1654+
assert_eq!(receiver_node_id.unwrap(), nodes[0].node.get_our_node_id());
16511655
match &purpose {
16521656
PaymentPurpose::InvoicePayment { payment_preimage, payment_secret, .. } => {
16531657
assert!(payment_preimage.is_none());
@@ -1659,9 +1663,10 @@ fn test_monitor_update_fail_claim() {
16591663
_ => panic!("Unexpected event"),
16601664
}
16611665
match events[1] {
1662-
Event::PaymentReceived { ref payment_hash, ref purpose, amount_msat } => {
1666+
Event::PaymentReceived { ref payment_hash, ref purpose, amount_msat, receiver_node_id } => {
16631667
assert_eq!(payment_hash_3, *payment_hash);
16641668
assert_eq!(1_000_000, amount_msat);
1669+
assert_eq!(receiver_node_id.unwrap(), nodes[0].node.get_our_node_id());
16651670
match &purpose {
16661671
PaymentPurpose::InvoicePayment { payment_preimage, payment_secret, .. } => {
16671672
assert!(payment_preimage.is_none());

lightning/src/ln/channelmanager.rs

+24
Original file line numberDiff line numberDiff line change
@@ -3323,6 +3323,12 @@ impl<M: Deref, T: Deref, K: Deref, F: Deref, L: Deref> ChannelManager<M, T, K, F
33233323
));
33243324
}
33253325
}
3326+
let phantom_shared_secret = claimable_htlc.prev_hop.phantom_shared_secret;
3327+
let mut receiver_node_id = self.our_network_pubkey;
3328+
if phantom_shared_secret.is_some() {
3329+
receiver_node_id = self.keys_manager.get_node_id(Recipient::PhantomNode)
3330+
.expect("Failed to get node_id for phantom node recipient");
3331+
}
33263332

33273333
macro_rules! check_total_value {
33283334
($payment_data: expr, $payment_preimage: expr) => {{
@@ -3365,6 +3371,7 @@ impl<M: Deref, T: Deref, K: Deref, F: Deref, L: Deref> ChannelManager<M, T, K, F
33653371
} else if total_value == $payment_data.total_msat {
33663372
htlcs.push(claimable_htlc);
33673373
new_events.push(events::Event::PaymentReceived {
3374+
receiver_node_id: Some(receiver_node_id),
33683375
payment_hash,
33693376
purpose: purpose(),
33703377
amount_msat: total_value,
@@ -3407,6 +3414,7 @@ impl<M: Deref, T: Deref, K: Deref, F: Deref, L: Deref> ChannelManager<M, T, K, F
34073414
let purpose = events::PaymentPurpose::SpontaneousPayment(preimage);
34083415
e.insert((purpose.clone(), vec![claimable_htlc]));
34093416
new_events.push(events::Event::PaymentReceived {
3417+
receiver_node_id: Some(receiver_node_id),
34103418
payment_hash,
34113419
amount_msat: outgoing_amt_msat,
34123420
purpose,
@@ -4070,6 +4078,7 @@ impl<M: Deref, T: Deref, K: Deref, F: Deref, L: Deref> ChannelManager<M, T, K, F
40704078
let mut claimed_any_htlcs = false;
40714079
let mut channel_state_lock = self.channel_state.lock().unwrap();
40724080
let channel_state = &mut *channel_state_lock;
4081+
let mut receiver_node_id = Some(self.our_network_pubkey);
40734082
for htlc in sources.iter() {
40744083
let chan_id = match self.short_to_chan_info.read().unwrap().get(&htlc.prev_hop.short_channel_id) {
40754084
Some((_cp_id, chan_id)) => chan_id.clone(),
@@ -4101,6 +4110,12 @@ impl<M: Deref, T: Deref, K: Deref, F: Deref, L: Deref> ChannelManager<M, T, K, F
41014110
break;
41024111
}
41034112
}
4113+
let phantom_shared_secret = htlc.prev_hop.phantom_shared_secret;
4114+
if phantom_shared_secret.is_some() {
4115+
let phantom_pubkey = self.keys_manager.get_node_id(Recipient::PhantomNode)
4116+
.expect("Failed to get node_id for phantom node recipient");
4117+
receiver_node_id = Some(phantom_pubkey)
4118+
}
41044119

41054120
claimable_amt_msat += htlc.value;
41064121
}
@@ -4150,6 +4165,7 @@ impl<M: Deref, T: Deref, K: Deref, F: Deref, L: Deref> ChannelManager<M, T, K, F
41504165

41514166
if claimed_any_htlcs {
41524167
self.pending_events.lock().unwrap().push(events::Event::PaymentClaimed {
4168+
receiver_node_id,
41534169
payment_hash,
41544170
purpose: payment_purpose,
41554171
amount_msat: claimable_amt_msat,
@@ -7447,6 +7463,13 @@ impl<'a, M: Deref, T: Deref, K: Deref, F: Deref, L: Deref>
74477463
if let Some((payment_purpose, claimable_htlcs)) = claimable_htlcs.remove(&payment_hash) {
74487464
log_info!(args.logger, "Re-claiming HTLCs with payment hash {} as we've released the preimage to a ChannelMonitor!", log_bytes!(payment_hash.0));
74497465
let mut claimable_amt_msat = 0;
7466+
let mut receiver_node_id = Some(our_network_pubkey);
7467+
let phantom_shared_secret = claimable_htlcs[0].prev_hop.phantom_shared_secret;
7468+
if phantom_shared_secret.is_some() {
7469+
let phantom_pubkey = args.keys_manager.get_node_id(Recipient::PhantomNode)
7470+
.expect("Failed to get node_id for phantom node recipient");
7471+
receiver_node_id = Some(phantom_pubkey)
7472+
}
74507473
for claimable_htlc in claimable_htlcs {
74517474
claimable_amt_msat += claimable_htlc.value;
74527475

@@ -7474,6 +7497,7 @@ impl<'a, M: Deref, T: Deref, K: Deref, F: Deref, L: Deref>
74747497
}
74757498
}
74767499
pending_events_read.push(events::Event::PaymentClaimed {
7500+
receiver_node_id,
74777501
payment_hash,
74787502
purpose: payment_purpose,
74797503
amount_msat: claimable_amt_msat,

lightning/src/ln/functional_test_utils.rs

+6-5
Original file line numberDiff line numberDiff line change
@@ -1469,20 +1469,20 @@ macro_rules! expect_pending_htlcs_forwardable_from_events {
14691469
}
14701470
}}
14711471
}
1472-
14731472
#[macro_export]
14741473
#[cfg(any(test, feature = "_bench_unstable", feature = "_test_utils"))]
14751474
macro_rules! expect_payment_received {
14761475
($node: expr, $expected_payment_hash: expr, $expected_payment_secret: expr, $expected_recv_value: expr) => {
1477-
expect_payment_received!($node, $expected_payment_hash, $expected_payment_secret, $expected_recv_value, None)
1476+
expect_payment_received!($node, $expected_payment_hash, $expected_payment_secret, $expected_recv_value, None, $node.node.get_our_node_id())
14781477
};
1479-
($node: expr, $expected_payment_hash: expr, $expected_payment_secret: expr, $expected_recv_value: expr, $expected_payment_preimage: expr) => {
1478+
($node: expr, $expected_payment_hash: expr, $expected_payment_secret: expr, $expected_recv_value: expr, $expected_payment_preimage: expr, $expected_receiver_node_id: expr) => {
14801479
let events = $node.node.get_and_clear_pending_events();
14811480
assert_eq!(events.len(), 1);
14821481
match events[0] {
1483-
$crate::util::events::Event::PaymentReceived { ref payment_hash, ref purpose, amount_msat } => {
1482+
$crate::util::events::Event::PaymentReceived { ref payment_hash, ref purpose, amount_msat, receiver_node_id } => {
14841483
assert_eq!($expected_payment_hash, *payment_hash);
14851484
assert_eq!($expected_recv_value, amount_msat);
1485+
assert_eq!($expected_receiver_node_id, receiver_node_id.unwrap());
14861486
match purpose {
14871487
$crate::util::events::PaymentPurpose::InvoicePayment { payment_preimage, payment_secret, .. } => {
14881488
assert_eq!(&$expected_payment_preimage, payment_preimage);
@@ -1774,8 +1774,9 @@ pub fn do_pass_along_path<'a, 'b, 'c>(origin_node: &Node<'a, 'b, 'c>, expected_p
17741774
if payment_received_expected {
17751775
assert_eq!(events_2.len(), 1);
17761776
match events_2[0] {
1777-
Event::PaymentReceived { ref payment_hash, ref purpose, amount_msat } => {
1777+
Event::PaymentReceived { ref payment_hash, ref purpose, amount_msat, receiver_node_id } => {
17781778
assert_eq!(our_payment_hash, *payment_hash);
1779+
assert_eq!(node.node.get_our_node_id(), receiver_node_id.unwrap());
17791780
match &purpose {
17801781
PaymentPurpose::InvoicePayment { payment_preimage, payment_secret, .. } => {
17811782
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());
@@ -3734,9 +3736,10 @@ fn do_test_drop_messages_peer_disconnect(messages_delivered: u8, simulate_broken
37343736
let events_2 = nodes[1].node.get_and_clear_pending_events();
37353737
assert_eq!(events_2.len(), 1);
37363738
match events_2[0] {
3737-
Event::PaymentReceived { ref payment_hash, ref purpose, amount_msat } => {
3739+
Event::PaymentReceived { ref payment_hash, ref purpose, amount_msat, receiver_node_id } => {
37383740
assert_eq!(payment_hash_1, *payment_hash);
37393741
assert_eq!(amount_msat, 1_000_000);
3742+
assert_eq!(receiver_node_id.unwrap(), nodes[1].node.get_our_node_id());
37403743
match &purpose {
37413744
PaymentPurpose::InvoicePayment { payment_preimage, payment_secret, .. } => {
37423745
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 versions
348+
/// 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 versions
379+
/// 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)