Skip to content

Commit d1e8d9c

Browse files
Refactor PaymentReceived event for keysend receives
1 parent f60a65f commit d1e8d9c

File tree

5 files changed

+182
-83
lines changed

5 files changed

+182
-83
lines changed

lightning/src/ln/chanmon_update_fail_tests.rs

Lines changed: 41 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ use ln::msgs::{ChannelMessageHandler, ErrorAction, RoutingMessageHandler};
2828
use routing::router::get_route;
2929
use util::config::UserConfig;
3030
use util::enforcing_trait_impls::EnforcingSigner;
31-
use util::events::{Event, MessageSendEvent, MessageSendEventsProvider};
31+
use util::events::{Event, MessageSendEvent, MessageSendEventsProvider, PaymentPurpose};
3232
use util::errors::APIError;
3333
use util::ser::{ReadableArgs, Writeable};
3434
use util::test_utils::TestBroadcaster;
@@ -220,11 +220,16 @@ fn do_test_simple_monitor_temporary_update_fail(disconnect: bool, persister_fail
220220
let events_3 = nodes[1].node.get_and_clear_pending_events();
221221
assert_eq!(events_3.len(), 1);
222222
match events_3[0] {
223-
Event::PaymentReceived { ref payment_hash, ref payment_preimage, ref payment_secret, amt, user_payment_id: _ } => {
223+
Event::PaymentReceived { ref payment_hash, ref purpose, amt } => {
224224
assert_eq!(payment_hash_1, *payment_hash);
225-
assert!(payment_preimage.is_none());
226-
assert_eq!(payment_secret_1, *payment_secret);
227225
assert_eq!(amt, 1000000);
226+
match &purpose {
227+
PaymentPurpose::InvoicePayment { payment_preimage, payment_secret, .. } => {
228+
assert!(payment_preimage.is_none());
229+
assert_eq!(payment_secret_1, *payment_secret);
230+
},
231+
_ => panic!("expected PaymentPurpose::InvoicePayment")
232+
}
228233
},
229234
_ => panic!("Unexpected event"),
230235
}
@@ -589,11 +594,16 @@ fn do_test_monitor_temporary_update_fail(disconnect_count: usize) {
589594
let events_5 = nodes[1].node.get_and_clear_pending_events();
590595
assert_eq!(events_5.len(), 1);
591596
match events_5[0] {
592-
Event::PaymentReceived { ref payment_hash, ref payment_preimage, ref payment_secret, amt, user_payment_id: _ } => {
597+
Event::PaymentReceived { ref payment_hash, ref purpose, amt } => {
593598
assert_eq!(payment_hash_2, *payment_hash);
594-
assert!(payment_preimage.is_none());
595-
assert_eq!(payment_secret_2, *payment_secret);
596599
assert_eq!(amt, 1000000);
600+
match &purpose {
601+
PaymentPurpose::InvoicePayment { payment_preimage, payment_secret, .. } => {
602+
assert!(payment_preimage.is_none());
603+
assert_eq!(payment_secret_2, *payment_secret);
604+
},
605+
_ => panic!("expected PaymentPurpose::InvoicePayment")
606+
}
597607
},
598608
_ => panic!("Unexpected event"),
599609
}
@@ -704,11 +714,16 @@ fn test_monitor_update_fail_cs() {
704714
let events = nodes[1].node.get_and_clear_pending_events();
705715
assert_eq!(events.len(), 1);
706716
match events[0] {
707-
Event::PaymentReceived { payment_hash, payment_preimage, payment_secret, amt, user_payment_id: _ } => {
717+
Event::PaymentReceived { payment_hash, ref purpose, amt } => {
708718
assert_eq!(payment_hash, our_payment_hash);
709-
assert!(payment_preimage.is_none());
710-
assert_eq!(our_payment_secret, payment_secret);
711719
assert_eq!(amt, 1000000);
720+
match &purpose {
721+
PaymentPurpose::InvoicePayment { payment_preimage, payment_secret, .. } => {
722+
assert!(payment_preimage.is_none());
723+
assert_eq!(our_payment_secret, *payment_secret);
724+
},
725+
_ => panic!("expected PaymentPurpose::InvoicePayment")
726+
}
712727
},
713728
_ => panic!("Unexpected event"),
714729
};
@@ -1712,20 +1727,30 @@ fn test_monitor_update_fail_claim() {
17121727
let events = nodes[0].node.get_and_clear_pending_events();
17131728
assert_eq!(events.len(), 2);
17141729
match events[0] {
1715-
Event::PaymentReceived { ref payment_hash, ref payment_preimage, ref payment_secret, amt, user_payment_id: _ } => {
1730+
Event::PaymentReceived { ref payment_hash, ref purpose, amt } => {
17161731
assert_eq!(payment_hash_2, *payment_hash);
1717-
assert!(payment_preimage.is_none());
1718-
assert_eq!(payment_secret_2, *payment_secret);
17191732
assert_eq!(1_000_000, amt);
1733+
match &purpose {
1734+
PaymentPurpose::InvoicePayment { payment_preimage, payment_secret, .. } => {
1735+
assert!(payment_preimage.is_none());
1736+
assert_eq!(payment_secret_2, *payment_secret);
1737+
},
1738+
_ => panic!("expected PaymentPurpose::InvoicePayment")
1739+
}
17201740
},
17211741
_ => panic!("Unexpected event"),
17221742
}
17231743
match events[1] {
1724-
Event::PaymentReceived { ref payment_hash, ref payment_preimage, ref payment_secret, amt, user_payment_id: _ } => {
1744+
Event::PaymentReceived { ref payment_hash, ref purpose, amt } => {
17251745
assert_eq!(payment_hash_3, *payment_hash);
1726-
assert!(payment_preimage.is_none());
1727-
assert_eq!(payment_secret_3, *payment_secret);
17281746
assert_eq!(1_000_000, amt);
1747+
match &purpose {
1748+
PaymentPurpose::InvoicePayment { payment_preimage, payment_secret, .. } => {
1749+
assert!(payment_preimage.is_none());
1750+
assert_eq!(payment_secret_3, *payment_secret);
1751+
},
1752+
_ => panic!("expected PaymentPurpose::InvoicePayment")
1753+
}
17291754
},
17301755
_ => panic!("Unexpected event"),
17311756
}

lightning/src/ln/channelmanager.rs

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2327,10 +2327,12 @@ impl<Signer: Sign, M: Deref, T: Deref, K: Deref, F: Deref, L: Deref> ChannelMana
23272327
} else if total_value == payment_data.total_msat {
23282328
new_events.push(events::Event::PaymentReceived {
23292329
payment_hash,
2330-
payment_preimage: inbound_payment.get().payment_preimage,
2331-
payment_secret: payment_data.payment_secret,
2330+
purpose: events::PaymentPurpose::InvoicePayment {
2331+
payment_preimage: inbound_payment.get().payment_preimage,
2332+
payment_secret: payment_data.payment_secret,
2333+
user_payment_id: inbound_payment.get().user_payment_id,
2334+
},
23322335
amt: total_value,
2333-
user_payment_id: inbound_payment.get().user_payment_id,
23342336
});
23352337
// Only ever generate at most one PaymentReceived
23362338
// per registered payment_hash, even if it isn't
@@ -3778,7 +3780,7 @@ impl<Signer: Sign, M: Deref, T: Deref, K: Deref, F: Deref, L: Deref> ChannelMana
37783780
/// The [`PaymentHash`] (and corresponding [`PaymentPreimage`]) must be globally unique. This
37793781
/// method may return an Err if another payment with the same payment_hash is still pending.
37803782
///
3781-
/// `user_payment_id` will be provided back in [`PaymentReceived::user_payment_id`] events to
3783+
/// `user_payment_id` will be provided back in [`PaymentPurpose::InvoicePayment::user_payment_id`] events to
37823784
/// allow tracking of which events correspond with which calls to this and
37833785
/// [`create_inbound_payment`]. `user_payment_id` has no meaning inside of LDK, it is simply
37843786
/// copied to events and otherwise ignored. It may be used to correlate PaymentReceived events
@@ -3812,7 +3814,7 @@ impl<Signer: Sign, M: Deref, T: Deref, K: Deref, F: Deref, L: Deref> ChannelMana
38123814
///
38133815
/// [`create_inbound_payment`]: Self::create_inbound_payment
38143816
/// [`PaymentReceived`]: events::Event::PaymentReceived
3815-
/// [`PaymentReceived::user_payment_id`]: events::Event::PaymentReceived::user_payment_id
3817+
/// [`PaymentPurpose::InvoicePayment::user_payment_id`]: events::PaymentPurpose::InvoicePayment::user_payment_id
38163818
pub fn create_inbound_payment_for_hash(&self, payment_hash: PaymentHash, min_value_msat: Option<u64>, invoice_expiry_delta_secs: u32, user_payment_id: u64) -> Result<PaymentSecret, APIError> {
38173819
self.set_payment_hash_secret_map(payment_hash, None, min_value_msat, invoice_expiry_delta_secs, user_payment_id)
38183820
}
@@ -5098,7 +5100,7 @@ pub mod bench {
50985100
use routing::router::get_route;
50995101
use util::test_utils;
51005102
use util::config::UserConfig;
5101-
use util::events::{Event, MessageSendEvent, MessageSendEventsProvider};
5103+
use util::events::{Event, MessageSendEvent, MessageSendEventsProvider, PaymentPurpose};
51025104

51035105
use bitcoin::hashes::Hash;
51045106
use bitcoin::hashes::sha256::Hash as Sha256;

lightning/src/ln/functional_test_utils.rs

Lines changed: 17 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ use ln::msgs::{ChannelMessageHandler,RoutingMessageHandler};
2323
use util::enforcing_trait_impls::EnforcingSigner;
2424
use util::test_utils;
2525
use util::test_utils::TestChainMonitor;
26-
use util::events::{Event, MessageSendEvent, MessageSendEventsProvider};
26+
use util::events::{Event, MessageSendEvent, MessageSendEventsProvider, PaymentPurpose};
2727
use util::errors::APIError;
2828
use util::config::UserConfig;
2929
use util::ser::{ReadableArgs, Writeable, Readable};
@@ -976,11 +976,16 @@ macro_rules! expect_payment_received {
976976
let events = $node.node.get_and_clear_pending_events();
977977
assert_eq!(events.len(), 1);
978978
match events[0] {
979-
Event::PaymentReceived { ref payment_hash, ref payment_preimage, ref payment_secret, amt, user_payment_id: _ } => {
979+
Event::PaymentReceived { ref payment_hash, ref purpose, amt } => {
980980
assert_eq!($expected_payment_hash, *payment_hash);
981-
assert!(payment_preimage.is_none());
982-
assert_eq!($expected_payment_secret, *payment_secret);
983981
assert_eq!($expected_recv_value, amt);
982+
match purpose {
983+
PaymentPurpose::InvoicePayment { payment_preimage, payment_secret, .. } => {
984+
assert!(payment_preimage.is_none());
985+
assert_eq!($expected_payment_secret, *payment_secret);
986+
},
987+
_ => {},
988+
}
984989
},
985990
_ => panic!("Unexpected event"),
986991
}
@@ -1069,10 +1074,15 @@ pub fn pass_along_path<'a, 'b, 'c>(origin_node: &Node<'a, 'b, 'c>, expected_path
10691074
if payment_received_expected {
10701075
assert_eq!(events_2.len(), 1);
10711076
match events_2[0] {
1072-
Event::PaymentReceived { ref payment_hash, ref payment_preimage, ref payment_secret, amt, user_payment_id: _ } => {
1077+
Event::PaymentReceived { ref payment_hash, ref purpose, amt} => {
10731078
assert_eq!(our_payment_hash, *payment_hash);
1074-
assert!(payment_preimage.is_none());
1075-
assert_eq!(our_payment_secret, *payment_secret);
1079+
match &purpose {
1080+
PaymentPurpose::InvoicePayment { payment_preimage, payment_secret, .. } => {
1081+
assert!(payment_preimage.is_none());
1082+
assert_eq!(our_payment_secret, *payment_secret);
1083+
},
1084+
_ => {},
1085+
}
10761086
assert_eq!(amt, recv_value);
10771087
},
10781088
_ => panic!("Unexpected event"),

lightning/src/ln/functional_tests.rs

Lines changed: 42 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ use ln::msgs;
3030
use ln::msgs::{ChannelMessageHandler,RoutingMessageHandler,HTLCFailChannelUpdate, ErrorAction};
3131
use util::enforcing_trait_impls::EnforcingSigner;
3232
use util::{byte_utils, test_utils};
33-
use util::events::{Event, MessageSendEvent, MessageSendEventsProvider};
33+
use util::events::{Event, MessageSendEvent, MessageSendEventsProvider, PaymentPurpose};
3434
use util::errors::APIError;
3535
use util::ser::{Writeable, ReadableArgs};
3636
use util::config::UserConfig;
@@ -2105,20 +2105,30 @@ fn test_channel_reserve_holding_cell_htlcs() {
21052105
let events = nodes[2].node.get_and_clear_pending_events();
21062106
assert_eq!(events.len(), 2);
21072107
match events[0] {
2108-
Event::PaymentReceived { ref payment_hash, ref payment_preimage, ref payment_secret, amt, user_payment_id: _ } => {
2108+
Event::PaymentReceived { ref payment_hash, ref purpose, amt } => {
21092109
assert_eq!(our_payment_hash_21, *payment_hash);
2110-
assert!(payment_preimage.is_none());
2111-
assert_eq!(our_payment_secret_21, *payment_secret);
21122110
assert_eq!(recv_value_21, amt);
2111+
match &purpose {
2112+
PaymentPurpose::InvoicePayment { payment_preimage, payment_secret, .. } => {
2113+
assert!(payment_preimage.is_none());
2114+
assert_eq!(our_payment_secret_21, *payment_secret);
2115+
},
2116+
_ => panic!("expected PaymentPurpose::InvoicePayment")
2117+
}
21132118
},
21142119
_ => panic!("Unexpected event"),
21152120
}
21162121
match events[1] {
2117-
Event::PaymentReceived { ref payment_hash, ref payment_preimage, ref payment_secret, amt, user_payment_id: _ } => {
2122+
Event::PaymentReceived { ref payment_hash, ref purpose, amt } => {
21182123
assert_eq!(our_payment_hash_22, *payment_hash);
2119-
assert!(payment_preimage.is_none());
2120-
assert_eq!(our_payment_secret_22, *payment_secret);
21212124
assert_eq!(recv_value_22, amt);
2125+
match &purpose {
2126+
PaymentPurpose::InvoicePayment { payment_preimage, payment_secret, .. } => {
2127+
assert!(payment_preimage.is_none());
2128+
assert_eq!(our_payment_secret_22, *payment_secret);
2129+
},
2130+
_ => panic!("expected PaymentPurpose::InvoicePayment")
2131+
}
21222132
},
21232133
_ => panic!("Unexpected event"),
21242134
}
@@ -3740,11 +3750,16 @@ fn do_test_drop_messages_peer_disconnect(messages_delivered: u8, simulate_broken
37403750
let events_2 = nodes[1].node.get_and_clear_pending_events();
37413751
assert_eq!(events_2.len(), 1);
37423752
match events_2[0] {
3743-
Event::PaymentReceived { ref payment_hash, ref payment_preimage, ref payment_secret, amt, user_payment_id: _ } => {
3753+
Event::PaymentReceived { ref payment_hash, ref purpose, amt } => {
37443754
assert_eq!(payment_hash_1, *payment_hash);
3745-
assert!(payment_preimage.is_none());
3746-
assert_eq!(payment_secret_1, *payment_secret);
37473755
assert_eq!(amt, 1000000);
3756+
match &purpose {
3757+
PaymentPurpose::InvoicePayment { payment_preimage, payment_secret, .. } => {
3758+
assert!(payment_preimage.is_none());
3759+
assert_eq!(payment_secret_1, *payment_secret);
3760+
},
3761+
_ => panic!("expected PaymentPurpose::InvoicePayment")
3762+
}
37483763
},
37493764
_ => panic!("Unexpected event"),
37503765
}
@@ -4138,10 +4153,15 @@ fn test_drop_messages_peer_disconnect_dual_htlc() {
41384153
let events_5 = nodes[1].node.get_and_clear_pending_events();
41394154
assert_eq!(events_5.len(), 1);
41404155
match events_5[0] {
4141-
Event::PaymentReceived { ref payment_hash, ref payment_preimage, ref payment_secret, amt: _, user_payment_id: _ } => {
4156+
Event::PaymentReceived { ref payment_hash, ref purpose, .. } => {
41424157
assert_eq!(payment_hash_2, *payment_hash);
4143-
assert!(payment_preimage.is_none());
4144-
assert_eq!(payment_secret_2, *payment_secret);
4158+
match &purpose {
4159+
PaymentPurpose::InvoicePayment { payment_preimage, payment_secret, .. } => {
4160+
assert!(payment_preimage.is_none());
4161+
assert_eq!(payment_secret_2, *payment_secret);
4162+
},
4163+
_ => panic!("expected PaymentPurpose::InvoicePayment")
4164+
}
41454165
},
41464166
_ => panic!("Unexpected event"),
41474167
}
@@ -8648,9 +8668,14 @@ fn test_preimage_storage() {
86488668
let events = nodes[1].node.get_and_clear_pending_events();
86498669
assert_eq!(events.len(), 1);
86508670
match events[0] {
8651-
Event::PaymentReceived { payment_preimage, user_payment_id, .. } => {
8652-
assert_eq!(user_payment_id, 42);
8653-
claim_payment(&nodes[0], &[&nodes[1]], payment_preimage.unwrap());
8671+
Event::PaymentReceived { ref purpose, .. } => {
8672+
match &purpose {
8673+
PaymentPurpose::InvoicePayment { payment_preimage, user_payment_id, .. } => {
8674+
assert_eq!(*user_payment_id, 42);
8675+
claim_payment(&nodes[0], &[&nodes[1]], payment_preimage.unwrap());
8676+
},
8677+
_ => panic!("expected PaymentPurpose::InvoicePayment")
8678+
}
86548679
},
86558680
_ => panic!("Unexpected event"),
86568681
}
@@ -8714,7 +8739,7 @@ fn test_secret_timeout() {
87148739
let events = nodes[1].node.get_and_clear_pending_events();
87158740
assert_eq!(events.len(), 1);
87168741
match events[0] {
8717-
Event::PaymentReceived { payment_preimage, payment_secret, user_payment_id, .. } => {
8742+
Event::PaymentReceived { purpose: PaymentPurpose::InvoicePayment { payment_preimage, payment_secret, user_payment_id }, .. } => {
87188743
assert!(payment_preimage.is_none());
87198744
assert_eq!(user_payment_id, 42);
87208745
assert_eq!(payment_secret, our_payment_secret);

0 commit comments

Comments
 (0)