Skip to content

Commit 88e4998

Browse files
committed
Add payment_hash to PaymentSent #999
1 parent 89be625 commit 88e4998

7 files changed

+146
-125
lines changed

lightning/src/ln/chanmon_update_fail_tests.rs

Lines changed: 29 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -213,7 +213,7 @@ fn do_test_simple_monitor_temporary_update_fail(disconnect: bool, persister_fail
213213
_ => panic!("Unexpected event"),
214214
}
215215

216-
claim_payment(&nodes[0], &[&nodes[1]], payment_preimage_1, 1_000_000);
216+
claim_payment(&nodes[0], &[&nodes[1]], payment_preimage_1, payment_hash_1, 1_000_000);
217217

218218
// Now set it to failed again...
219219
let (_, payment_hash_2) = get_payment_preimage_hash!(&nodes[0]);
@@ -286,7 +286,7 @@ fn do_test_monitor_temporary_update_fail(disconnect_count: usize) {
286286
let channel_id = create_announced_chan_between_nodes(&nodes, 0, 1, InitFeatures::known(), InitFeatures::known()).2;
287287
let logger = test_utils::TestLogger::new();
288288

289-
let (payment_preimage_1, _) = route_payment(&nodes[0], &[&nodes[1]], 1000000);
289+
let (payment_preimage_1, payment_hash_1) = route_payment(&nodes[0], &[&nodes[1]], 1000000);
290290

291291
// Now try to send a second payment which will fail to send
292292
let (payment_preimage_2, payment_hash_2) = get_payment_preimage_hash!(nodes[0]);
@@ -322,8 +322,9 @@ fn do_test_monitor_temporary_update_fail(disconnect_count: usize) {
322322
let events_3 = nodes[0].node.get_and_clear_pending_events();
323323
assert_eq!(events_3.len(), 1);
324324
match events_3[0] {
325-
Event::PaymentSent { ref payment_preimage } => {
325+
Event::PaymentSent { ref payment_preimage, ref payment_hash } => {
326326
assert_eq!(*payment_preimage, payment_preimage_1);
327+
assert_eq!(*payment_hash, payment_hash_1);
327328
},
328329
_ => panic!("Unexpected event"),
329330
}
@@ -414,8 +415,9 @@ fn do_test_monitor_temporary_update_fail(disconnect_count: usize) {
414415
let events_3 = nodes[0].node.get_and_clear_pending_events();
415416
assert_eq!(events_3.len(), 1);
416417
match events_3[0] {
417-
Event::PaymentSent { ref payment_preimage } => {
418+
Event::PaymentSent { ref payment_preimage, ref payment_hash } => {
418419
assert_eq!(*payment_preimage, payment_preimage_1);
420+
assert_eq!(*payment_hash, payment_hash_1);
419421
},
420422
_ => panic!("Unexpected event"),
421423
}
@@ -581,7 +583,7 @@ fn do_test_monitor_temporary_update_fail(disconnect_count: usize) {
581583
_ => panic!("Unexpected event"),
582584
}
583585

584-
claim_payment(&nodes[0], &[&nodes[1]], payment_preimage_2, 1_000_000);
586+
claim_payment(&nodes[0], &[&nodes[1]], payment_preimage_2, payment_hash_2, 1_000_000);
585587
}
586588

587589
#[test]
@@ -695,7 +697,7 @@ fn test_monitor_update_fail_cs() {
695697
_ => panic!("Unexpected event"),
696698
};
697699

698-
claim_payment(&nodes[0], &[&nodes[1]], payment_preimage, 1_000_000);
700+
claim_payment(&nodes[0], &[&nodes[1]], payment_preimage, our_payment_hash, 1_000_000);
699701
}
700702

701703
#[test]
@@ -746,7 +748,7 @@ fn test_monitor_update_fail_no_rebroadcast() {
746748
_ => panic!("Unexpected event"),
747749
}
748750

749-
claim_payment(&nodes[0], &[&nodes[1]], payment_preimage_1, 1_000_000);
751+
claim_payment(&nodes[0], &[&nodes[1]], payment_preimage_1, our_payment_hash, 1_000_000);
750752
}
751753

752754
#[test]
@@ -824,8 +826,8 @@ fn test_monitor_update_raa_while_paused() {
824826
expect_pending_htlcs_forwardable!(nodes[1]);
825827
expect_payment_received!(nodes[1], our_payment_hash_1, 1000000);
826828

827-
claim_payment(&nodes[0], &[&nodes[1]], payment_preimage_1, 1_000_000);
828-
claim_payment(&nodes[1], &[&nodes[0]], payment_preimage_2, 1_000_000);
829+
claim_payment(&nodes[0], &[&nodes[1]], payment_preimage_1, our_payment_hash_1, 1_000_000);
830+
claim_payment(&nodes[1], &[&nodes[0]], payment_preimage_2, our_payment_hash_2, 1_000_000);
829831
}
830832

831833
fn do_test_monitor_update_fail_raa(test_ignore_second_cs: bool) {
@@ -1078,10 +1080,10 @@ fn do_test_monitor_update_fail_raa(test_ignore_second_cs: bool) {
10781080
Event::PaymentReceived { payment_hash, .. } => assert_eq!(payment_hash, payment_hash_4.unwrap()),
10791081
_ => panic!("Unexpected event"),
10801082
};
1081-
claim_payment(&nodes[2], &[&nodes[1], &nodes[0]], payment_preimage_4.unwrap(), 1_000_000);
1083+
claim_payment(&nodes[2], &[&nodes[1], &nodes[0]], payment_preimage_4.unwrap(), payment_hash_4.unwrap(), 1_000_000);
10821084
}
10831085

1084-
claim_payment(&nodes[0], &[&nodes[1], &nodes[2]], payment_preimage_2, 1_000_000);
1086+
claim_payment(&nodes[0], &[&nodes[1], &nodes[2]], payment_preimage_2, payment_hash_2, 1_000_000);
10851087
}
10861088

10871089
#[test]
@@ -1288,9 +1290,9 @@ fn raa_no_response_awaiting_raa_state() {
12881290
expect_pending_htlcs_forwardable!(nodes[1]);
12891291
expect_payment_received!(nodes[1], payment_hash_3, 1000000);
12901292

1291-
claim_payment(&nodes[0], &[&nodes[1]], payment_preimage_1, 1_000_000);
1292-
claim_payment(&nodes[0], &[&nodes[1]], payment_preimage_2, 1_000_000);
1293-
claim_payment(&nodes[0], &[&nodes[1]], payment_preimage_3, 1_000_000);
1293+
claim_payment(&nodes[0], &[&nodes[1]], payment_preimage_1, payment_hash_1, 1_000_000);
1294+
claim_payment(&nodes[0], &[&nodes[1]], payment_preimage_2, payment_hash_2, 1_000_000);
1295+
claim_payment(&nodes[0], &[&nodes[1]], payment_preimage_3, payment_hash_3, 1_000_000);
12941296
}
12951297

12961298
#[test]
@@ -1309,7 +1311,7 @@ fn claim_while_disconnected_monitor_update_fail() {
13091311
let logger = test_utils::TestLogger::new();
13101312

13111313
// Forward a payment for B to claim
1312-
let (payment_preimage_1, _) = route_payment(&nodes[0], &[&nodes[1]], 1000000);
1314+
let (payment_preimage_1, payment_hash_1) = route_payment(&nodes[0], &[&nodes[1]], 1000000);
13131315

13141316
nodes[0].node.peer_disconnected(&nodes[1].node.get_our_node_id(), false);
13151317
nodes[1].node.peer_disconnected(&nodes[0].node.get_our_node_id(), false);
@@ -1410,13 +1412,14 @@ fn claim_while_disconnected_monitor_update_fail() {
14101412
let events = nodes[0].node.get_and_clear_pending_events();
14111413
assert_eq!(events.len(), 1);
14121414
match events[0] {
1413-
Event::PaymentSent { ref payment_preimage } => {
1415+
Event::PaymentSent { ref payment_preimage, ref payment_hash } => {
14141416
assert_eq!(*payment_preimage, payment_preimage_1);
1417+
assert_eq!(*payment_hash, payment_hash_1);
14151418
},
14161419
_ => panic!("Unexpected event"),
14171420
}
14181421

1419-
claim_payment(&nodes[0], &[&nodes[1]], payment_preimage_2, 1_000_000);
1422+
claim_payment(&nodes[0], &[&nodes[1]], payment_preimage_2, payment_hash_2, 1_000_000);
14201423
}
14211424

14221425
#[test]
@@ -1484,7 +1487,7 @@ fn monitor_failed_no_reestablish_response() {
14841487
expect_pending_htlcs_forwardable!(nodes[1]);
14851488
expect_payment_received!(nodes[1], payment_hash_1, 1000000);
14861489

1487-
claim_payment(&nodes[0], &[&nodes[1]], payment_preimage_1, 1_000_000);
1490+
claim_payment(&nodes[0], &[&nodes[1]], payment_preimage_1, payment_hash_1, 1_000_000);
14881491
}
14891492

14901493
#[test]
@@ -1585,8 +1588,8 @@ fn first_message_on_recv_ordering() {
15851588
expect_pending_htlcs_forwardable!(nodes[1]);
15861589
expect_payment_received!(nodes[1], payment_hash_2, 1000000);
15871590

1588-
claim_payment(&nodes[0], &[&nodes[1]], payment_preimage_1, 1_000_000);
1589-
claim_payment(&nodes[0], &[&nodes[1]], payment_preimage_2, 1_000_000);
1591+
claim_payment(&nodes[0], &[&nodes[1]], payment_preimage_1, payment_hash_1, 1_000_000);
1592+
claim_payment(&nodes[0], &[&nodes[1]], payment_preimage_2, payment_hash_2, 1_000_000);
15901593
}
15911594

15921595
#[test]
@@ -1741,7 +1744,7 @@ fn test_monitor_update_on_pending_forwards() {
17411744
nodes[0].node.process_pending_htlc_forwards();
17421745
expect_payment_received!(nodes[0], payment_hash_2, 1000000);
17431746

1744-
claim_payment(&nodes[2], &[&nodes[1], &nodes[0]], payment_preimage_2, 1_000_000);
1747+
claim_payment(&nodes[2], &[&nodes[1], &nodes[0]], payment_preimage_2, payment_hash_2, 1_000_000);
17451748
}
17461749

17471750
#[test]
@@ -1758,7 +1761,7 @@ fn monitor_update_claim_fail_no_response() {
17581761
let logger = test_utils::TestLogger::new();
17591762

17601763
// Forward a payment for B to claim
1761-
let (payment_preimage_1, _) = route_payment(&nodes[0], &[&nodes[1]], 1000000);
1764+
let (payment_preimage_1, payment_hash_1) = route_payment(&nodes[0], &[&nodes[1]], 1000000);
17621765

17631766
// Now start forwarding a second payment, skipping the last RAA so B is in AwaitingRAA
17641767
let (payment_preimage_2, payment_hash_2) = get_payment_preimage_hash!(nodes[0]);
@@ -1800,13 +1803,14 @@ fn monitor_update_claim_fail_no_response() {
18001803
let events = nodes[0].node.get_and_clear_pending_events();
18011804
assert_eq!(events.len(), 1);
18021805
match events[0] {
1803-
Event::PaymentSent { ref payment_preimage } => {
1806+
Event::PaymentSent { ref payment_preimage, ref payment_hash } => {
18041807
assert_eq!(*payment_preimage, payment_preimage_1);
1808+
assert_eq!(*payment_hash, payment_hash_1);
18051809
},
18061810
_ => panic!("Unexpected event"),
18071811
}
18081812

1809-
claim_payment(&nodes[0], &[&nodes[1]], payment_preimage_2, 1_000_000);
1813+
claim_payment(&nodes[0], &[&nodes[1]], payment_preimage_2, payment_hash_2, 1_000_000);
18101814
}
18111815

18121816
// confirm_a_first and restore_b_before_conf are wholly unrelated to earlier bools and
@@ -1970,5 +1974,5 @@ fn test_path_paused_mpp() {
19701974
assert_eq!(events.len(), 1);
19711975
pass_along_path(&nodes[0], &[&nodes[2], &nodes[3]], 200_000, payment_hash.clone(), Some(payment_secret), events.pop().unwrap(), true);
19721976

1973-
claim_payment_along_route_with_secret(&nodes[0], &[&[&nodes[1], &nodes[3]], &[&nodes[2], &nodes[3]]], false, payment_preimage, Some(payment_secret), 200_000);
1977+
claim_payment_along_route_with_secret(&nodes[0], &[&[&nodes[1], &nodes[3]], &[&nodes[2], &nodes[3]]], false, payment_preimage, payment_hash, Some(payment_secret), 200_000);
19741978
}

lightning/src/ln/channelmanager.rs

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2145,13 +2145,14 @@ impl<ChanSigner: ChannelKeys, M: Deref, T: Deref, K: Deref, F: Deref, L: Deref>
21452145
} else { unreachable!(); }
21462146
}
21472147

2148-
fn claim_funds_internal(&self, mut channel_state_lock: MutexGuard<ChannelHolder<ChanSigner>>, source: HTLCSource, payment_preimage: PaymentPreimage) {
2148+
fn claim_funds_internal(&self, mut channel_state_lock: MutexGuard<ChannelHolder<ChanSigner>>, source: HTLCSource, payment_preimage: PaymentPreimage, payment_hash: PaymentHash) {
21492149
match source {
21502150
HTLCSource::OutboundRoute { .. } => {
21512151
mem::drop(channel_state_lock);
21522152
let mut pending_events = self.pending_events.lock().unwrap();
21532153
pending_events.push(events::Event::PaymentSent {
2154-
payment_preimage
2154+
payment_preimage,
2155+
payment_hash
21552156
});
21562157
},
21572158
HTLCSource::PreviousHopData(hop_data) => {
@@ -2630,7 +2631,8 @@ impl<ChanSigner: ChannelKeys, M: Deref, T: Deref, K: Deref, F: Deref, L: Deref>
26302631
hash_map::Entry::Vacant(_) => return Err(MsgHandleErrInternal::send_err_msg_no_close("Failed to find corresponding channel".to_owned(), msg.channel_id))
26312632
}
26322633
};
2633-
self.claim_funds_internal(channel_lock, htlc_source, msg.payment_preimage.clone());
2634+
let payment_hash = PaymentHash(Sha256::hash(&msg.payment_preimage.0).into_inner());
2635+
self.claim_funds_internal(channel_lock, htlc_source, msg.payment_preimage.clone(), payment_hash);
26342636
Ok(())
26352637
}
26362638

@@ -3018,7 +3020,7 @@ impl<ChanSigner: ChannelKeys, M: Deref, T: Deref, K: Deref, F: Deref, L: Deref>
30183020
MonitorEvent::HTLCEvent(htlc_update) => {
30193021
if let Some(preimage) = htlc_update.payment_preimage {
30203022
log_trace!(self.logger, "Claiming HTLC with preimage {} from our monitor", log_bytes!(preimage.0));
3021-
self.claim_funds_internal(self.channel_state.lock().unwrap(), htlc_update.source, preimage);
3023+
self.claim_funds_internal(self.channel_state.lock().unwrap(), htlc_update.source, preimage, htlc_update.payment_hash.clone());
30223024
} else {
30233025
log_trace!(self.logger, "Failing HTLC with hash {} from our monitor", log_bytes!(htlc_update.payment_hash.0));
30243026
self.fail_htlc_backwards_internal(self.channel_state.lock().unwrap(), htlc_update.source, &htlc_update.payment_hash, HTLCFailReason::Reason { failure_code: 0x4000 | 8, data: Vec::new() });

lightning/src/ln/functional_test_utils.rs

Lines changed: 11 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -821,12 +821,13 @@ macro_rules! expect_payment_received {
821821
}
822822

823823
macro_rules! expect_payment_sent {
824-
($node: expr, $expected_payment_preimage: expr) => {
824+
($node: expr, $expected_payment_preimage: expr, $expected_payment_hash: expr) => {
825825
let events = $node.node.get_and_clear_pending_events();
826826
assert_eq!(events.len(), 1);
827827
match events[0] {
828-
Event::PaymentSent { ref payment_preimage } => {
828+
Event::PaymentSent { ref payment_preimage, ref payment_hash } => {
829829
assert_eq!($expected_payment_preimage, *payment_preimage);
830+
assert_eq!($expected_payment_hash, *payment_hash);
830831
},
831832
_ => panic!("Unexpected event"),
832833
}
@@ -921,7 +922,7 @@ pub fn send_along_route<'a, 'b, 'c>(origin_node: &Node<'a, 'b, 'c>, route: Route
921922
(our_payment_preimage, our_payment_hash)
922923
}
923924

924-
pub fn claim_payment_along_route_with_secret<'a, 'b, 'c>(origin_node: &Node<'a, 'b, 'c>, expected_paths: &[&[&Node<'a, 'b, 'c>]], skip_last: bool, our_payment_preimage: PaymentPreimage, our_payment_secret: Option<PaymentSecret>, expected_amount: u64) {
925+
pub fn claim_payment_along_route_with_secret<'a, 'b, 'c>(origin_node: &Node<'a, 'b, 'c>, expected_paths: &[&[&Node<'a, 'b, 'c>]], skip_last: bool, our_payment_preimage: PaymentPreimage, our_payment_hash: PaymentHash, our_payment_secret: Option<PaymentSecret>, expected_amount: u64) {
925926
for path in expected_paths.iter() {
926927
assert_eq!(path.last().unwrap().node.get_our_node_id(), expected_paths[0].last().unwrap().node.get_our_node_id());
927928
}
@@ -1004,17 +1005,17 @@ pub fn claim_payment_along_route_with_secret<'a, 'b, 'c>(origin_node: &Node<'a,
10041005

10051006
if !skip_last {
10061007
last_update_fulfill_dance!(origin_node, expected_route.first().unwrap());
1007-
expect_payment_sent!(origin_node, our_payment_preimage);
1008+
expect_payment_sent!(origin_node, our_payment_preimage, our_payment_hash);
10081009
}
10091010
}
10101011
}
10111012

1012-
pub fn claim_payment_along_route<'a, 'b, 'c>(origin_node: &Node<'a, 'b, 'c>, expected_route: &[&Node<'a, 'b, 'c>], skip_last: bool, our_payment_preimage: PaymentPreimage, expected_amount: u64) {
1013-
claim_payment_along_route_with_secret(origin_node, &[expected_route], skip_last, our_payment_preimage, None, expected_amount);
1013+
pub fn claim_payment_along_route<'a, 'b, 'c>(origin_node: &Node<'a, 'b, 'c>, expected_route: &[&Node<'a, 'b, 'c>], skip_last: bool, our_payment_preimage: PaymentPreimage, our_payment_hash: PaymentHash, expected_amount: u64) {
1014+
claim_payment_along_route_with_secret(origin_node, &[expected_route], skip_last, our_payment_preimage, our_payment_hash, None, expected_amount);
10141015
}
10151016

1016-
pub fn claim_payment<'a, 'b, 'c>(origin_node: &Node<'a, 'b, 'c>, expected_route: &[&Node<'a, 'b, 'c>], our_payment_preimage: PaymentPreimage, expected_amount: u64) {
1017-
claim_payment_along_route(origin_node, expected_route, false, our_payment_preimage, expected_amount);
1017+
pub fn claim_payment<'a, 'b, 'c>(origin_node: &Node<'a, 'b, 'c>, expected_route: &[&Node<'a, 'b, 'c>], our_payment_preimage: PaymentPreimage, our_payment_hash: PaymentHash, expected_amount: u64) {
1018+
claim_payment_along_route(origin_node, expected_route, false, our_payment_preimage, our_payment_hash, expected_amount);
10181019
}
10191020

10201021
pub const TEST_FINAL_CLTV: u32 = 32;
@@ -1048,8 +1049,8 @@ pub fn route_over_limit<'a, 'b, 'c>(origin_node: &Node<'a, 'b, 'c>, expected_rou
10481049
}
10491050

10501051
pub fn send_payment<'a, 'b, 'c>(origin: &Node<'a, 'b, 'c>, expected_route: &[&Node<'a, 'b, 'c>], recv_value: u64, expected_value: u64) {
1051-
let our_payment_preimage = route_payment(&origin, expected_route, recv_value).0;
1052-
claim_payment(&origin, expected_route, our_payment_preimage, expected_value);
1052+
let (our_payment_preimage, our_payment_hash) = route_payment(&origin, expected_route, recv_value);
1053+
claim_payment(&origin, expected_route, our_payment_preimage, our_payment_hash, expected_value);
10531054
}
10541055

10551056
pub fn fail_payment_along_route<'a, 'b, 'c>(origin_node: &Node<'a, 'b, 'c>, expected_route: &[&Node<'a, 'b, 'c>], skip_last: bool, our_payment_hash: PaymentHash) {

0 commit comments

Comments
 (0)