Skip to content

Commit 8e3d1d4

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 7269fa2 commit 8e3d1d4

File tree

5 files changed

+50
-16
lines changed

5 files changed

+50
-16
lines changed

lightning/src/ln/chanmon_update_fail_tests.rs

+13-6
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, via_channel_id, via_user_channel_id: _ } => {
204204
assert_eq!(payment_hash_1, *payment_hash);
205205
assert_eq!(amount_msat, 1_000_000);
206+
assert_eq!(via_channel_id, Some(channel_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, via_channel_id, via_user_channel_id: _ } => {
572573
assert_eq!(payment_hash_2, *payment_hash);
573574
assert_eq!(amount_msat, 1_000_000);
575+
assert_eq!(via_channel_id, Some(channel_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, via_channel_id, via_user_channel_id: _ } => {
689691
assert_eq!(payment_hash, our_payment_hash);
690692
assert_eq!(amount_msat, 1_000_000);
693+
assert_eq!(via_channel_id, Some(channel_id));
691694
match &purpose {
692695
PaymentPurpose::InvoicePayment { payment_preimage, payment_secret, .. } => {
693696
assert!(payment_preimage.is_none());
@@ -1638,7 +1641,8 @@ fn test_monitor_update_fail_claim() {
16381641
commitment_signed_dance!(nodes[1], nodes[2], payment_event.commitment_msg, false, true);
16391642

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

@@ -1659,9 +1663,11 @@ fn test_monitor_update_fail_claim() {
16591663
let events = nodes[0].node.get_and_clear_pending_events();
16601664
assert_eq!(events.len(), 2);
16611665
match events[0] {
1662-
Event::PaymentReceived { ref payment_hash, ref purpose, amount_msat } => {
1666+
Event::PaymentReceived { ref payment_hash, ref purpose, amount_msat, via_channel_id, via_user_channel_id } => {
16631667
assert_eq!(payment_hash_2, *payment_hash);
16641668
assert_eq!(1_000_000, amount_msat);
1669+
assert_eq!(via_channel_id, Some(channel_id));
1670+
assert_eq!(via_user_channel_id, Some(42));
16651671
match &purpose {
16661672
PaymentPurpose::InvoicePayment { payment_preimage, payment_secret, .. } => {
16671673
assert!(payment_preimage.is_none());
@@ -1673,9 +1679,10 @@ fn test_monitor_update_fail_claim() {
16731679
_ => panic!("Unexpected event"),
16741680
}
16751681
match events[1] {
1676-
Event::PaymentReceived { ref payment_hash, ref purpose, amount_msat } => {
1682+
Event::PaymentReceived { ref payment_hash, ref purpose, amount_msat, via_channel_id, via_user_channel_id: _ } => {
16771683
assert_eq!(payment_hash_3, *payment_hash);
16781684
assert_eq!(1_000_000, amount_msat);
1685+
assert_eq!(via_channel_id, Some(channel_id));
16791686
match &purpose {
16801687
PaymentPurpose::InvoicePayment { payment_preimage, payment_secret, .. } => {
16811688
assert!(payment_preimage.is_none());

lightning/src/ln/channelmanager.rs

+9
Original file line numberDiff line numberDiff line change
@@ -3484,11 +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 prev_channel_id = prev_funding_outpoint.to_channel_id();
3488+
let via_user_channel_id = channel_state.by_id.get(&prev_channel_id).map(|chan| chan.get_user_id());
34873489
htlcs.push(claimable_htlc);
34883490
new_events.push(events::Event::PaymentReceived {
34893491
payment_hash,
34903492
purpose: purpose(),
34913493
amount_msat: total_value,
3494+
via_channel_id: Some(prev_channel_id),
3495+
via_user_channel_id,
34923496
});
34933497
payment_received_generated = true;
34943498
} else {
@@ -3520,17 +3524,22 @@ impl<M: Deref, T: Deref, K: Deref, F: Deref, L: Deref> ChannelManager<M, T, K, F
35203524
continue
35213525
}
35223526
};
3527+
35233528
check_total_value!(payment_data, payment_preimage);
35243529
},
35253530
OnionPayload::Spontaneous(preimage) => {
35263531
match channel_state.claimable_htlcs.entry(payment_hash) {
35273532
hash_map::Entry::Vacant(e) => {
35283533
let purpose = events::PaymentPurpose::SpontaneousPayment(preimage);
35293534
e.insert((purpose.clone(), vec![claimable_htlc]));
3535+
let prev_channel_id = prev_funding_outpoint.to_channel_id();
3536+
let via_user_channel_id = channel_state.by_id.get(&prev_channel_id).map(|chan| chan.get_user_id());
35303537
new_events.push(events::Event::PaymentReceived {
35313538
payment_hash,
35323539
amount_msat: outgoing_amt_msat,
35333540
purpose,
3541+
via_channel_id: Some(prev_channel_id),
3542+
via_user_channel_id,
35343543
});
35353544
},
35363545
hash_map::Entry::Occupied(_) => {

lightning/src/ln/functional_test_utils.rs

+4-2
Original file line numberDiff line numberDiff line change
@@ -1444,7 +1444,7 @@ macro_rules! expect_payment_received {
14441444
let events = $node.node.get_and_clear_pending_events();
14451445
assert_eq!(events.len(), 1);
14461446
match events[0] {
1447-
$crate::util::events::Event::PaymentReceived { ref payment_hash, ref purpose, amount_msat } => {
1447+
$crate::util::events::Event::PaymentReceived { ref payment_hash, ref purpose, amount_msat, via_channel_id: _, via_user_channel_id: _ } => {
14481448
assert_eq!($expected_payment_hash, *payment_hash);
14491449
assert_eq!($expected_recv_value, amount_msat);
14501450
match purpose {
@@ -1738,7 +1738,7 @@ 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, ref via_channel_id, ref via_user_channel_id } => {
17421742
assert_eq!(our_payment_hash, *payment_hash);
17431743
match &purpose {
17441744
PaymentPurpose::InvoicePayment { payment_preimage, payment_secret, .. } => {
@@ -1751,6 +1751,8 @@ pub fn do_pass_along_path<'a, 'b, 'c>(origin_node: &Node<'a, 'b, 'c>, expected_p
17511751
},
17521752
}
17531753
assert_eq!(amount_msat, recv_value);
1754+
assert!(node.node.list_channels().iter().any(|details| details.channel_id == via_channel_id.unwrap()));
1755+
assert!(node.node.list_channels().iter().any(|details| details.user_channel_id == via_user_channel_id.unwrap()));
17541756
},
17551757
_ => panic!("Unexpected event"),
17561758
}

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, via_user_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, via_user_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, via_user_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)