Skip to content

Commit dd09b2d

Browse files
committed
adds 'our_node_id' to 'Event::Payment{Received,Claimed}', includes updates to functional test cases, documentation, and supports phantom nodes
1 parent 7269fa2 commit dd09b2d

File tree

7 files changed

+78
-18
lines changed

7 files changed

+78
-18
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

+31-1
Original file line numberDiff line numberDiff line change
@@ -3445,7 +3445,12 @@ impl<M: Deref, T: Deref, K: Deref, F: Deref, L: Deref> ChannelManager<M, T, K, F
34453445
));
34463446
}
34473447
}
3448-
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+
}
34493454
macro_rules! check_total_value {
34503455
($payment_data: expr, $payment_preimage: expr) => {{
34513456
let mut payment_received_generated = false;
@@ -3486,6 +3491,7 @@ impl<M: Deref, T: Deref, K: Deref, F: Deref, L: Deref> ChannelManager<M, T, K, F
34863491
} else if total_value == $payment_data.total_msat {
34873492
htlcs.push(claimable_htlc);
34883493
new_events.push(events::Event::PaymentReceived {
3494+
receiver_node_id: Some(receiver_node_id),
34893495
payment_hash,
34903496
purpose: purpose(),
34913497
amount_msat: total_value,
@@ -3526,8 +3532,16 @@ impl<M: Deref, T: Deref, K: Deref, F: Deref, L: Deref> ChannelManager<M, T, K, F
35263532
match channel_state.claimable_htlcs.entry(payment_hash) {
35273533
hash_map::Entry::Vacant(e) => {
35283534
let purpose = events::PaymentPurpose::SpontaneousPayment(preimage);
3535+
let phantom_shared_secret = &claimable_htlc.prev_hop.phantom_shared_secret.clone();
35293536
e.insert((purpose.clone(), vec![claimable_htlc]));
3537+
let mut receiver_node_id = Some(self.our_network_pubkey);
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 = Some(phantom_pubkey)
3542+
}
35303543
new_events.push(events::Event::PaymentReceived {
3544+
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

+7-5
Original file line numberDiff line numberDiff line change
@@ -1433,20 +1433,21 @@ 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());
1450+
14501451
match purpose {
14511452
$crate::util::events::PaymentPurpose::InvoicePayment { payment_preimage, payment_secret, .. } => {
14521453
assert_eq!(&$expected_payment_preimage, payment_preimage);
@@ -1738,8 +1739,9 @@ pub fn do_pass_along_path<'a, 'b, 'c>(origin_node: &Node<'a, 'b, 'c>, expected_p
17381739
if payment_received_expected {
17391740
assert_eq!(events_2.len(), 1);
17401741
match events_2[0] {
1741-
Event::PaymentReceived { ref payment_hash, ref purpose, amount_msat } => {
1742+
Event::PaymentReceived { ref payment_hash, ref purpose, amount_msat, receiver_node_id } => {
17421743
assert_eq!(our_payment_hash, *payment_hash);
1744+
assert_eq!(node.node.get_our_node_id(), receiver_node_id.unwrap());
17431745
match &purpose {
17441746
PaymentPurpose::InvoicePayment { payment_preimage, payment_secret, .. } => {
17451747
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

+22-2
Original file line numberDiff line numberDiff line change
@@ -342,6 +342,12 @@ 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 0.0.113 and above.
348+
///
349+
/// [phantom node payments]: crate::chain::keysinterface::PhantomKeysManager
350+
receiver_node_id: Option<PublicKey>,
345351
/// The hash for which the preimage should be handed to the ChannelManager. Note that LDK will
346352
/// not stop you from registering duplicate payment hashes for inbound payments.
347353
payment_hash: PaymentHash,
@@ -366,6 +372,12 @@ pub enum Event {
366372
///
367373
/// [`ChannelManager::claim_funds`]: crate::ln::channelmanager::ChannelManager::claim_funds
368374
PaymentClaimed {
375+
/// The node that received the payment.
376+
/// This is useful to identify payments which were received via [phantom node payments].
377+
/// This field will always be filled in when the event was generated by LDK versions 0.0.113 and above.
378+
///
379+
/// [phantom node payments]: crate::chain::keysinterface::PhantomKeysManager
380+
receiver_node_id: Option<PublicKey>,
369381
/// The payment hash of the claimed payment. Note that LDK will not stop you from
370382
/// registering duplicate payment hashes for inbound payments.
371383
payment_hash: PaymentHash,
@@ -739,7 +751,7 @@ impl Writeable for Event {
739751
// We never write out FundingGenerationReady events as, upon disconnection, peers
740752
// drop any channels which have not yet exchanged funding_signed.
741753
},
742-
&Event::PaymentReceived { ref payment_hash, ref amount_msat, ref purpose } => {
754+
&Event::PaymentReceived { ref payment_hash, ref amount_msat, ref purpose, ref receiver_node_id } => {
743755
1u8.write(writer)?;
744756
let mut payment_secret = None;
745757
let payment_preimage;
@@ -754,6 +766,7 @@ impl Writeable for Event {
754766
}
755767
write_tlv_fields!(writer, {
756768
(0, payment_hash, required),
769+
(1, receiver_node_id, option),
757770
(2, payment_secret, option),
758771
(4, amount_msat, required),
759772
(6, 0u64, required), // user_payment_id required for compatibility with 0.0.103 and earlier
@@ -854,10 +867,11 @@ impl Writeable for Event {
854867
// We never write the OpenChannelRequest events as, upon disconnection, peers
855868
// drop any channels which have not yet exchanged funding_signed.
856869
},
857-
&Event::PaymentClaimed { ref payment_hash, ref amount_msat, ref purpose } => {
870+
&Event::PaymentClaimed { ref payment_hash, ref amount_msat, ref purpose, ref receiver_node_id } => {
858871
19u8.write(writer)?;
859872
write_tlv_fields!(writer, {
860873
(0, payment_hash, required),
874+
(1, receiver_node_id, option),
861875
(2, purpose, required),
862876
(4, amount_msat, required),
863877
});
@@ -923,9 +937,11 @@ impl MaybeReadable for Event {
923937
let mut payment_preimage = None;
924938
let mut payment_secret = None;
925939
let mut amount_msat = 0;
940+
let mut receiver_node_id = None;
926941
let mut _user_payment_id = None::<u64>; // For compatibility with 0.0.103 and earlier
927942
read_tlv_fields!(reader, {
928943
(0, payment_hash, required),
944+
(1, receiver_node_id, option),
929945
(2, payment_secret, option),
930946
(4, amount_msat, required),
931947
(6, _user_payment_id, option),
@@ -940,6 +956,7 @@ impl MaybeReadable for Event {
940956
None => return Err(msgs::DecodeError::InvalidValue),
941957
};
942958
Ok(Some(Event::PaymentReceived {
959+
receiver_node_id,
943960
payment_hash,
944961
amount_msat,
945962
purpose,
@@ -1117,13 +1134,16 @@ impl MaybeReadable for Event {
11171134
let mut payment_hash = PaymentHash([0; 32]);
11181135
let mut purpose = None;
11191136
let mut amount_msat = 0;
1137+
let mut receiver_node_id = None;
11201138
read_tlv_fields!(reader, {
11211139
(0, payment_hash, required),
1140+
(1, receiver_node_id, option),
11221141
(2, purpose, ignorable),
11231142
(4, amount_msat, required),
11241143
});
11251144
if purpose.is_none() { return Ok(None); }
11261145
Ok(Some(Event::PaymentClaimed {
1146+
receiver_node_id,
11271147
payment_hash,
11281148
purpose: purpose.unwrap(),
11291149
amount_msat,

0 commit comments

Comments
 (0)