Skip to content

Commit 0fb6e3e

Browse files
committed
Expose the channel via which we received a payment
We expose the `channel_id` and `user_channel_id` via which we received a payment in the `PaymentReceived` event.
1 parent 6acf4d2 commit 0fb6e3e

File tree

5 files changed

+67
-16
lines changed

5 files changed

+67
-16
lines changed

lightning/src/ln/chanmon_update_fail_tests.rs

+13-6
Original file line numberDiff line numberDiff line change
@@ -201,9 +201,10 @@ fn do_test_simple_monitor_temporary_update_fail(disconnect: bool) {
201201
let events_3 = nodes[1].node.get_and_clear_pending_events();
202202
assert_eq!(events_3.len(), 1);
203203
match events_3[0] {
204-
Event::PaymentReceived { ref payment_hash, ref purpose, amount_msat } => {
204+
Event::PaymentReceived { ref payment_hash, ref purpose, amount_msat, via_channel_id, .. } => {
205205
assert_eq!(payment_hash_1, *payment_hash);
206206
assert_eq!(amount_msat, 1_000_000);
207+
assert_eq!(via_channel_id, Some(channel_id));
207208
match &purpose {
208209
PaymentPurpose::InvoicePayment { payment_preimage, payment_secret, .. } => {
209210
assert!(payment_preimage.is_none());
@@ -569,9 +570,10 @@ fn do_test_monitor_temporary_update_fail(disconnect_count: usize) {
569570
let events_5 = nodes[1].node.get_and_clear_pending_events();
570571
assert_eq!(events_5.len(), 1);
571572
match events_5[0] {
572-
Event::PaymentReceived { ref payment_hash, ref purpose, amount_msat } => {
573+
Event::PaymentReceived { ref payment_hash, ref purpose, amount_msat, via_channel_id, .. } => {
573574
assert_eq!(payment_hash_2, *payment_hash);
574575
assert_eq!(amount_msat, 1_000_000);
576+
assert_eq!(via_channel_id, Some(channel_id));
575577
match &purpose {
576578
PaymentPurpose::InvoicePayment { payment_preimage, payment_secret, .. } => {
577579
assert!(payment_preimage.is_none());
@@ -686,9 +688,10 @@ fn test_monitor_update_fail_cs() {
686688
let events = nodes[1].node.get_and_clear_pending_events();
687689
assert_eq!(events.len(), 1);
688690
match events[0] {
689-
Event::PaymentReceived { payment_hash, ref purpose, amount_msat } => {
691+
Event::PaymentReceived { payment_hash, ref purpose, amount_msat, via_channel_id, .. } => {
690692
assert_eq!(payment_hash, our_payment_hash);
691693
assert_eq!(amount_msat, 1_000_000);
694+
assert_eq!(via_channel_id, Some(channel_id));
692695
match &purpose {
693696
PaymentPurpose::InvoicePayment { payment_preimage, payment_secret, .. } => {
694697
assert!(payment_preimage.is_none());
@@ -1639,7 +1642,8 @@ fn test_monitor_update_fail_claim() {
16391642
commitment_signed_dance!(nodes[1], nodes[2], payment_event.commitment_msg, false, true);
16401643

16411644
// Now restore monitor updating on the 0<->1 channel and claim the funds on B.
1642-
let (outpoint, latest_update, _) = nodes[1].chain_monitor.latest_monitor_update_id.lock().unwrap().get(&chan_1.2).unwrap().clone();
1645+
let channel_id = chan_1.2;
1646+
let (outpoint, latest_update, _) = nodes[1].chain_monitor.latest_monitor_update_id.lock().unwrap().get(&channel_id).unwrap().clone();
16431647
nodes[1].chain_monitor.chain_monitor.force_channel_monitor_updated(outpoint, latest_update);
16441648
check_added_monitors!(nodes[1], 0);
16451649

@@ -1660,9 +1664,11 @@ fn test_monitor_update_fail_claim() {
16601664
let events = nodes[0].node.get_and_clear_pending_events();
16611665
assert_eq!(events.len(), 2);
16621666
match events[0] {
1663-
Event::PaymentReceived { ref payment_hash, ref purpose, amount_msat } => {
1667+
Event::PaymentReceived { ref payment_hash, ref purpose, amount_msat, via_channel_id, via_user_channel_id } => {
16641668
assert_eq!(payment_hash_2, *payment_hash);
16651669
assert_eq!(1_000_000, amount_msat);
1670+
assert_eq!(via_channel_id, Some(channel_id));
1671+
assert_eq!(via_user_channel_id, Some(42));
16661672
match &purpose {
16671673
PaymentPurpose::InvoicePayment { payment_preimage, payment_secret, .. } => {
16681674
assert!(payment_preimage.is_none());
@@ -1674,9 +1680,10 @@ fn test_monitor_update_fail_claim() {
16741680
_ => panic!("Unexpected event"),
16751681
}
16761682
match events[1] {
1677-
Event::PaymentReceived { ref payment_hash, ref purpose, amount_msat } => {
1683+
Event::PaymentReceived { ref payment_hash, ref purpose, amount_msat, via_channel_id, .. } => {
16781684
assert_eq!(payment_hash_3, *payment_hash);
16791685
assert_eq!(1_000_000, amount_msat);
1686+
assert_eq!(via_channel_id, Some(channel_id));
16801687
match &purpose {
16811688
PaymentPurpose::InvoicePayment { payment_preimage, payment_secret, .. } => {
16821689
assert!(payment_preimage.is_none());

lightning/src/ln/channelmanager.rs

+26
Original file line numberDiff line numberDiff line change
@@ -3470,11 +3470,24 @@ impl<M: Deref, T: Deref, K: Deref, F: Deref, L: Deref> ChannelManager<M, T, K, F
34703470
log_bytes!(payment_hash.0), total_value, $payment_data.total_msat);
34713471
fail_htlc!(claimable_htlc, payment_hash);
34723472
} else if total_value == $payment_data.total_msat {
3473+
let (via_channel_id, via_user_channel_id) =
3474+
if let Some((_, chan_id)) = self.short_to_chan_info.read().unwrap().get(&prev_short_channel_id) {
3475+
match channel_state.by_id.entry(*chan_id) {
3476+
hash_map::Entry::Occupied(prev_chan) =>
3477+
(Some(chan_id.clone()), Some(prev_chan.get().get_user_id())),
3478+
_ => (None, None),
3479+
}
3480+
} else {
3481+
(None, None)
3482+
};
3483+
34733484
htlcs.push(claimable_htlc);
34743485
new_events.push(events::Event::PaymentReceived {
34753486
payment_hash,
34763487
purpose: purpose(),
34773488
amount_msat: total_value,
3489+
via_channel_id,
3490+
via_user_channel_id,
34783491
});
34793492
payment_received_generated = true;
34803493
} else {
@@ -3506,17 +3519,30 @@ impl<M: Deref, T: Deref, K: Deref, F: Deref, L: Deref> ChannelManager<M, T, K, F
35063519
continue
35073520
}
35083521
};
3522+
35093523
check_total_value!(payment_data, payment_preimage);
35103524
},
35113525
OnionPayload::Spontaneous(preimage) => {
35123526
match channel_state.claimable_htlcs.entry(payment_hash) {
35133527
hash_map::Entry::Vacant(e) => {
35143528
let purpose = events::PaymentPurpose::SpontaneousPayment(preimage);
35153529
e.insert((purpose.clone(), vec![claimable_htlc]));
3530+
let (via_channel_id, via_user_channel_id) =
3531+
if let Some((_, chan_id)) = self.short_to_chan_info.read().unwrap().get(&prev_short_channel_id) {
3532+
match channel_state.by_id.entry(*chan_id) {
3533+
hash_map::Entry::Occupied(prev_chan) =>
3534+
(Some(chan_id.clone()), Some(prev_chan.get().get_user_id())),
3535+
_ => (None, None),
3536+
}
3537+
} else {
3538+
(None, None)
3539+
};
35163540
new_events.push(events::Event::PaymentReceived {
35173541
payment_hash,
35183542
amount_msat: outgoing_amt_msat,
35193543
purpose,
3544+
via_channel_id,
3545+
via_user_channel_id,
35203546
});
35213547
},
35223548
hash_map::Entry::Occupied(_) => {

lightning/src/ln/functional_test_utils.rs

+4-2
Original file line numberDiff line numberDiff line change
@@ -1387,7 +1387,7 @@ macro_rules! expect_payment_received {
13871387
let events = $node.node.get_and_clear_pending_events();
13881388
assert_eq!(events.len(), 1);
13891389
match events[0] {
1390-
$crate::util::events::Event::PaymentReceived { ref payment_hash, ref purpose, amount_msat } => {
1390+
$crate::util::events::Event::PaymentReceived { ref payment_hash, ref purpose, amount_msat, .. } => {
13911391
assert_eq!($expected_payment_hash, *payment_hash);
13921392
assert_eq!($expected_recv_value, amount_msat);
13931393
match purpose {
@@ -1681,7 +1681,7 @@ pub fn do_pass_along_path<'a, 'b, 'c>(origin_node: &Node<'a, 'b, 'c>, expected_p
16811681
if payment_received_expected {
16821682
assert_eq!(events_2.len(), 1);
16831683
match events_2[0] {
1684-
Event::PaymentReceived { ref payment_hash, ref purpose, amount_msat } => {
1684+
Event::PaymentReceived { ref payment_hash, ref purpose, amount_msat, ref via_channel_id, ref via_user_channel_id } => {
16851685
assert_eq!(our_payment_hash, *payment_hash);
16861686
match &purpose {
16871687
PaymentPurpose::InvoicePayment { payment_preimage, payment_secret, .. } => {
@@ -1694,6 +1694,8 @@ pub fn do_pass_along_path<'a, 'b, 'c>(origin_node: &Node<'a, 'b, 'c>, expected_p
16941694
},
16951695
}
16961696
assert_eq!(amount_msat, recv_value);
1697+
assert!(node.node.list_channels().iter().any(|details| details.channel_id == via_channel_id.unwrap()));
1698+
assert!(node.node.list_channels().iter().any(|details| details.user_channel_id == via_user_channel_id.unwrap()));
16971699
},
16981700
_ => panic!("Unexpected event"),
16991701
}

lightning/src/ln/functional_tests.rs

+11-7
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, via_channel_id, .. } => {
19601960
assert_eq!(our_payment_hash_21, *payment_hash);
19611961
assert_eq!(recv_value_21, amount_msat);
1962+
assert_eq!(via_channel_id, Some(chan_2.2));
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, via_channel_id, .. } => {
19741975
assert_eq!(our_payment_hash_22, *payment_hash);
19751976
assert_eq!(recv_value_22, amount_msat);
1977+
assert_eq!(via_channel_id, Some(chan_2.2));
19761978
match &purpose {
19771979
PaymentPurpose::InvoicePayment { payment_preimage, payment_secret, .. } => {
19781980
assert!(payment_preimage.is_none());
@@ -3612,15 +3614,16 @@ fn do_test_drop_messages_peer_disconnect(messages_delivered: u8, simulate_broken
36123614
let mut nodes = create_network(2, &node_cfgs, &node_chanmgrs);
36133615

36143616
let mut as_channel_ready = None;
3615-
if messages_delivered == 0 {
3616-
let (channel_ready, _, _) = create_chan_between_nodes_with_value_a(&nodes[0], &nodes[1], 100000, 10001, channelmanager::provided_init_features(), channelmanager::provided_init_features());
3617+
let channel_id = if messages_delivered == 0 {
3618+
let (channel_ready, chan_id, _) = create_chan_between_nodes_with_value_a(&nodes[0], &nodes[1], 100000, 10001, channelmanager::provided_init_features(), channelmanager::provided_init_features());
36173619
as_channel_ready = Some(channel_ready);
36183620
// nodes[1] doesn't receive the channel_ready message (it'll be re-sent on reconnect)
36193621
// Note that we store it so that if we're running with `simulate_broken_lnd` we can deliver
36203622
// it before the channel_reestablish message.
3623+
chan_id
36213624
} else {
3622-
create_announced_chan_between_nodes(&nodes, 0, 1, channelmanager::provided_init_features(), channelmanager::provided_init_features());
3623-
}
3625+
create_announced_chan_between_nodes(&nodes, 0, 1, channelmanager::provided_init_features(), channelmanager::provided_init_features()).2
3626+
};
36243627

36253628
let (route, payment_hash_1, payment_preimage_1, payment_secret_1) = get_route_and_payment_hash!(nodes[0], nodes[1], 1_000_000);
36263629

@@ -3723,9 +3726,10 @@ fn do_test_drop_messages_peer_disconnect(messages_delivered: u8, simulate_broken
37233726
let events_2 = nodes[1].node.get_and_clear_pending_events();
37243727
assert_eq!(events_2.len(), 1);
37253728
match events_2[0] {
3726-
Event::PaymentReceived { ref payment_hash, ref purpose, amount_msat } => {
3729+
Event::PaymentReceived { ref payment_hash, ref purpose, amount_msat, via_channel_id, .. } => {
37273730
assert_eq!(payment_hash_1, *payment_hash);
37283731
assert_eq!(amount_msat, 1_000_000);
3732+
assert_eq!(via_channel_id, Some(channel_id));
37293733
match &purpose {
37303734
PaymentPurpose::InvoicePayment { payment_preimage, payment_secret, .. } => {
37313735
assert!(payment_preimage.is_none());

lightning/src/util/events.rs

+13-1
Original file line numberDiff line numberDiff line change
@@ -350,6 +350,10 @@ pub enum Event {
350350
/// Information for claiming this received payment, based on whether the purpose of the
351351
/// payment is to pay an invoice or to send a spontaneous payment.
352352
purpose: PaymentPurpose,
353+
/// The `channel_id` indicating over which channel we received the payment.
354+
via_channel_id: Option<[u8; 32]>,
355+
/// The `user_channel_id` indicating over which channel we received the payment.
356+
via_user_channel_id: Option<u128>,
353357
},
354358
/// Indicates a payment has been claimed and we've received money!
355359
///
@@ -739,7 +743,7 @@ impl Writeable for Event {
739743
// We never write out FundingGenerationReady events as, upon disconnection, peers
740744
// drop any channels which have not yet exchanged funding_signed.
741745
},
742-
&Event::PaymentReceived { ref payment_hash, ref amount_msat, ref purpose } => {
746+
&Event::PaymentReceived { ref payment_hash, ref amount_msat, ref purpose, ref via_channel_id, ref via_user_channel_id } => {
743747
1u8.write(writer)?;
744748
let mut payment_secret = None;
745749
let payment_preimage;
@@ -754,7 +758,9 @@ impl Writeable for Event {
754758
}
755759
write_tlv_fields!(writer, {
756760
(0, payment_hash, required),
761+
(1, via_channel_id, option),
757762
(2, payment_secret, option),
763+
(3, via_user_channel_id, option),
758764
(4, amount_msat, required),
759765
(6, 0u64, required), // user_payment_id required for compatibility with 0.0.103 and earlier
760766
(8, payment_preimage, option),
@@ -924,9 +930,13 @@ impl MaybeReadable for Event {
924930
let mut payment_secret = None;
925931
let mut amount_msat = 0;
926932
let mut _user_payment_id = None::<u64>; // For compatibility with 0.0.103 and earlier
933+
let mut via_channel_id = None;
934+
let mut via_user_channel_id = None;
927935
read_tlv_fields!(reader, {
928936
(0, payment_hash, required),
937+
(1, via_channel_id, option),
929938
(2, payment_secret, option),
939+
(3, via_user_channel_id, option),
930940
(4, amount_msat, required),
931941
(6, _user_payment_id, option),
932942
(8, payment_preimage, option),
@@ -943,6 +953,8 @@ impl MaybeReadable for Event {
943953
payment_hash,
944954
amount_msat,
945955
purpose,
956+
via_channel_id,
957+
via_user_channel_id,
946958
}))
947959
};
948960
f()

0 commit comments

Comments
 (0)