Skip to content

Commit a40f5b3

Browse files
committed
sets receiver_node_id to the phantom node pubkey when phantom_shared_secret is found on the claimable_htlcs
1 parent 5ea8d2c commit a40f5b3

File tree

5 files changed

+50
-14
lines changed

5 files changed

+50
-14
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/channelmanager.rs

+31-4
Original file line numberDiff line numberDiff line change
@@ -3484,9 +3484,15 @@ impl<M: Deref, T: Deref, K: Deref, F: Deref, L: Deref> ChannelManager<M, T, K, F
34843484
log_bytes!(payment_hash.0), total_value, $payment_data.total_msat);
34853485
fail_htlc!(claimable_htlc, payment_hash);
34863486
} else if total_value == $payment_data.total_msat {
3487+
let phantom_shared_secret = claimable_htlc.prev_hop.phantom_shared_secret;
34873488
htlcs.push(claimable_htlc);
3489+
let mut receiver_node_id = self.our_network_pubkey;
3490+
if phantom_shared_secret.is_some(){
3491+
receiver_node_id = self.keys_manager.get_node_id(Recipient::PhantomNode)
3492+
.expect("Failed to get node_id for phantom node recipient");
3493+
}
34883494
new_events.push(events::Event::PaymentReceived {
3489-
receiver_node_id: Some(self.our_network_pubkey),
3495+
receiver_node_id: Some(receiver_node_id),
34903496
payment_hash,
34913497
purpose: purpose(),
34923498
amount_msat: total_value,
@@ -3527,9 +3533,16 @@ impl<M: Deref, T: Deref, K: Deref, F: Deref, L: Deref> ChannelManager<M, T, K, F
35273533
match channel_state.claimable_htlcs.entry(payment_hash) {
35283534
hash_map::Entry::Vacant(e) => {
35293535
let purpose = events::PaymentPurpose::SpontaneousPayment(preimage);
3536+
let phantom_shared_secret = &claimable_htlc.prev_hop.phantom_shared_secret.clone();
35303537
e.insert((purpose.clone(), vec![claimable_htlc]));
3538+
let mut receiver_node_id = Some(self.our_network_pubkey);
3539+
if phantom_shared_secret.is_some(){
3540+
let phantom_pubkey = self.keys_manager.get_node_id(Recipient::PhantomNode)
3541+
.expect("Failed to get node_id for phantom node recipient");
3542+
receiver_node_id = Some(phantom_pubkey)
3543+
}
35313544
new_events.push(events::Event::PaymentReceived {
3532-
receiver_node_id: Some(self.our_network_pubkey),
3545+
receiver_node_id,
35333546
payment_hash,
35343547
amount_msat: outgoing_amt_msat,
35353548
purpose,
@@ -4196,6 +4209,7 @@ impl<M: Deref, T: Deref, K: Deref, F: Deref, L: Deref> ChannelManager<M, T, K, F
41964209
let mut claimed_any_htlcs = false;
41974210
let mut channel_state_lock = self.channel_state.lock().unwrap();
41984211
let channel_state = &mut *channel_state_lock;
4212+
let mut receiver_node_id = Some(self.our_network_pubkey);
41994213
for htlc in sources.iter() {
42004214
let chan_id = match self.short_to_chan_info.read().unwrap().get(&htlc.prev_hop.short_channel_id) {
42014215
Some((_cp_id, chan_id)) => chan_id.clone(),
@@ -4227,6 +4241,12 @@ impl<M: Deref, T: Deref, K: Deref, F: Deref, L: Deref> ChannelManager<M, T, K, F
42274241
break;
42284242
}
42294243
}
4244+
let phantom_shared_secret = htlc.prev_hop.phantom_shared_secret;
4245+
if phantom_shared_secret.is_some(){
4246+
let phantom_pubkey = self.keys_manager.get_node_id(Recipient::PhantomNode)
4247+
.expect("Failed to get node_id for phantom node recipient");
4248+
receiver_node_id = Some(phantom_pubkey)
4249+
}
42304250

42314251
claimable_amt_msat += htlc.value;
42324252
}
@@ -4276,7 +4296,7 @@ impl<M: Deref, T: Deref, K: Deref, F: Deref, L: Deref> ChannelManager<M, T, K, F
42764296

42774297
if claimed_any_htlcs {
42784298
self.pending_events.lock().unwrap().push(events::Event::PaymentClaimed {
4279-
receiver_node_id: Some(self.our_network_pubkey),
4299+
receiver_node_id,
42804300
payment_hash,
42814301
purpose: payment_purpose,
42824302
amount_msat: claimable_amt_msat,
@@ -7490,6 +7510,13 @@ impl<'a, M: Deref, T: Deref, K: Deref, F: Deref, L: Deref>
74907510
if let Some((payment_purpose, claimable_htlcs)) = claimable_htlcs.remove(&payment_hash) {
74917511
log_info!(args.logger, "Re-claiming HTLCs with payment hash {} as we've released the preimage to a ChannelMonitor!", log_bytes!(payment_hash.0));
74927512
let mut claimable_amt_msat = 0;
7513+
let mut receiver_node_id = Some(our_network_pubkey);
7514+
let phantom_shared_secret = claimable_htlcs[0].prev_hop.phantom_shared_secret;
7515+
if phantom_shared_secret.is_some(){
7516+
let phantom_pubkey = args.keys_manager.get_node_id(Recipient::PhantomNode)
7517+
.expect("Failed to get node_id for phantom node recipient");
7518+
receiver_node_id = Some(phantom_pubkey)
7519+
}
74937520
for claimable_htlc in claimable_htlcs {
74947521
claimable_amt_msat += claimable_htlc.value;
74957522

@@ -7517,7 +7544,7 @@ impl<'a, M: Deref, T: Deref, K: Deref, F: Deref, L: Deref>
75177544
}
75187545
}
75197546
pending_events_read.push(events::Event::PaymentClaimed {
7520-
receiver_node_id: Some(our_network_pubkey),
7547+
receiver_node_id,
75217548
payment_hash,
75227549
purpose: payment_purpose,
75237550
amount_msat: claimable_amt_msat,

lightning/src/ln/functional_test_utils.rs

+5-4
Original file line numberDiff line numberDiff line change
@@ -1433,21 +1433,22 @@ macro_rules! expect_pending_htlcs_forwardable_from_events {
14331433
}
14341434
}}
14351435
}
1436-
1436+
// expect_payment_received!(&nodes[fwd_idx], payment_hash, payment_secret, payment_amt, payment_preimage_opt);
14371437
#[macro_export]
14381438
#[cfg(any(test, feature = "_bench_unstable", feature = "_test_utils"))]
14391439
macro_rules! expect_payment_received {
14401440
($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)
1441+
expect_payment_received!($node, $expected_payment_hash, $expected_payment_secret, $expected_recv_value, None, $node.node.get_our_node_id())
14421442
};
1443-
($node: expr, $expected_payment_hash: expr, $expected_payment_secret: expr, $expected_recv_value: expr, $expected_payment_preimage: expr) => {
1443+
($node: expr, $expected_payment_hash: expr, $expected_payment_secret: expr, $expected_recv_value: expr, $expected_payment_preimage: expr, $expected_receiver_node_id: expr) => {
14441444
let events = $node.node.get_and_clear_pending_events();
14451445
assert_eq!(events.len(), 1);
14461446
match events[0] {
14471447
$crate::util::events::Event::PaymentReceived { ref payment_hash, ref purpose, amount_msat, receiver_node_id } => {
14481448
assert_eq!($expected_payment_hash, *payment_hash);
14491449
assert_eq!($expected_recv_value, amount_msat);
1450-
assert_eq!($node.node.get_our_node_id(), receiver_node_id.unwrap());
1450+
assert_eq!($expected_receiver_node_id, receiver_node_id.unwrap());
1451+
14511452
match purpose {
14521453
$crate::util::events::PaymentPurpose::InvoicePayment { payment_preimage, payment_secret, .. } => {
14531454
assert_eq!(&$expected_payment_preimage, payment_preimage);

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

+12-4
Original file line numberDiff line numberDiff line change
@@ -342,7 +342,11 @@ 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
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::PhantomKeysManager
346350
receiver_node_id: Option<PublicKey>,
347351
/// The hash for which the preimage should be handed to the ChannelManager. Note that LDK will
348352
/// not stop you from registering duplicate payment hashes for inbound payments.
@@ -368,7 +372,11 @@ pub enum Event {
368372
///
369373
/// [`ChannelManager::claim_funds`]: crate::ln::channelmanager::ChannelManager::claim_funds
370374
PaymentClaimed {
371-
/// The node that received the payment
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::PhantomKeysManager
372380
receiver_node_id: Option<PublicKey>,
373381
/// The payment hash of the claimed payment. Note that LDK will not stop you from
374382
/// registering duplicate payment hashes for inbound payments.
@@ -743,7 +751,7 @@ impl Writeable for Event {
743751
// We never write out FundingGenerationReady events as, upon disconnection, peers
744752
// drop any channels which have not yet exchanged funding_signed.
745753
},
746-
&Event::PaymentReceived { ref payment_hash, ref amount_msat, ref purpose, receiver_node_id } => {
754+
&Event::PaymentReceived { ref payment_hash, ref amount_msat, ref purpose, ref receiver_node_id } => {
747755
1u8.write(writer)?;
748756
let mut payment_secret = None;
749757
let payment_preimage;
@@ -859,7 +867,7 @@ impl Writeable for Event {
859867
// We never write the OpenChannelRequest events as, upon disconnection, peers
860868
// drop any channels which have not yet exchanged funding_signed.
861869
},
862-
&Event::PaymentClaimed { ref payment_hash, ref amount_msat, ref purpose, receiver_node_id } => {
870+
&Event::PaymentClaimed { ref payment_hash, ref amount_msat, ref purpose, ref receiver_node_id } => {
863871
19u8.write(writer)?;
864872
write_tlv_fields!(writer, {
865873
(0, payment_hash, required),

0 commit comments

Comments
 (0)