Skip to content

Commit e635db0

Browse files
authored
Merge pull request #1062 from galderz/t_payment_hash_999
2 parents 6582aae + 204bfd2 commit e635db0

7 files changed

+66
-30
lines changed

lightning/src/ln/chanmon_update_fail_tests.rs

Lines changed: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -310,7 +310,7 @@ fn do_test_monitor_temporary_update_fail(disconnect_count: usize) {
310310
let channel_id = create_announced_chan_between_nodes(&nodes, 0, 1, InitFeatures::known(), InitFeatures::known()).2;
311311
let logger = test_utils::TestLogger::new();
312312

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

315315
// Now try to send a second payment which will fail to send
316316
let (payment_preimage_2, payment_hash_2, payment_secret_2) = get_payment_preimage_hash!(nodes[1]);
@@ -346,8 +346,9 @@ fn do_test_monitor_temporary_update_fail(disconnect_count: usize) {
346346
let events_3 = nodes[0].node.get_and_clear_pending_events();
347347
assert_eq!(events_3.len(), 1);
348348
match events_3[0] {
349-
Event::PaymentSent { ref payment_preimage } => {
349+
Event::PaymentSent { ref payment_preimage, ref payment_hash } => {
350350
assert_eq!(*payment_preimage, payment_preimage_1);
351+
assert_eq!(*payment_hash, payment_hash_1);
351352
},
352353
_ => panic!("Unexpected event"),
353354
}
@@ -438,8 +439,9 @@ fn do_test_monitor_temporary_update_fail(disconnect_count: usize) {
438439
let events_3 = nodes[0].node.get_and_clear_pending_events();
439440
assert_eq!(events_3.len(), 1);
440441
match events_3[0] {
441-
Event::PaymentSent { ref payment_preimage } => {
442+
Event::PaymentSent { ref payment_preimage, ref payment_hash } => {
442443
assert_eq!(*payment_preimage, payment_preimage_1);
444+
assert_eq!(*payment_hash, payment_hash_1);
443445
},
444446
_ => panic!("Unexpected event"),
445447
}
@@ -1364,7 +1366,7 @@ fn claim_while_disconnected_monitor_update_fail() {
13641366
let logger = test_utils::TestLogger::new();
13651367

13661368
// Forward a payment for B to claim
1367-
let (payment_preimage_1, _, _) = route_payment(&nodes[0], &[&nodes[1]], 1000000);
1369+
let (payment_preimage_1, payment_hash_1, _) = route_payment(&nodes[0], &[&nodes[1]], 1000000);
13681370

13691371
nodes[0].node.peer_disconnected(&nodes[1].node.get_our_node_id(), false);
13701372
nodes[1].node.peer_disconnected(&nodes[0].node.get_our_node_id(), false);
@@ -1465,8 +1467,9 @@ fn claim_while_disconnected_monitor_update_fail() {
14651467
let events = nodes[0].node.get_and_clear_pending_events();
14661468
assert_eq!(events.len(), 1);
14671469
match events[0] {
1468-
Event::PaymentSent { ref payment_preimage } => {
1470+
Event::PaymentSent { ref payment_preimage, ref payment_hash } => {
14691471
assert_eq!(*payment_preimage, payment_preimage_1);
1472+
assert_eq!(*payment_hash, payment_hash_1);
14701473
},
14711474
_ => panic!("Unexpected event"),
14721475
}
@@ -1847,7 +1850,7 @@ fn monitor_update_claim_fail_no_response() {
18471850
let logger = test_utils::TestLogger::new();
18481851

18491852
// Forward a payment for B to claim
1850-
let (payment_preimage_1, _, _) = route_payment(&nodes[0], &[&nodes[1]], 1000000);
1853+
let (payment_preimage_1, payment_hash_1, _) = route_payment(&nodes[0], &[&nodes[1]], 1000000);
18511854

18521855
// Now start forwarding a second payment, skipping the last RAA so B is in AwaitingRAA
18531856
let (payment_preimage_2, payment_hash_2, payment_secret_2) = get_payment_preimage_hash!(nodes[1]);
@@ -1889,8 +1892,9 @@ fn monitor_update_claim_fail_no_response() {
18891892
let events = nodes[0].node.get_and_clear_pending_events();
18901893
assert_eq!(events.len(), 1);
18911894
match events[0] {
1892-
Event::PaymentSent { ref payment_preimage } => {
1895+
Event::PaymentSent { ref payment_preimage, ref payment_hash } => {
18931896
assert_eq!(*payment_preimage, payment_preimage_1);
1897+
assert_eq!(*payment_hash, payment_hash_1);
18941898
},
18951899
_ => panic!("Unexpected event"),
18961900
}

lightning/src/ln/channelmanager.rs

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3308,8 +3308,12 @@ impl<Signer: Sign, M: Deref, T: Deref, K: Deref, F: Deref, L: Deref> ChannelMana
33083308
sessions.remove(&session_priv_bytes, path.last().unwrap().fee_msat)
33093309
} else { false };
33103310
if found_payment {
3311+
let payment_hash = PaymentHash(Sha256::hash(&payment_preimage.0).into_inner());
33113312
self.pending_events.lock().unwrap().push(
3312-
events::Event::PaymentSent { payment_preimage }
3313+
events::Event::PaymentSent {
3314+
payment_preimage,
3315+
payment_hash: payment_hash
3316+
}
33133317
);
33143318
} else {
33153319
log_trace!(self.logger, "Received duplicative fulfill for HTLC with payment_preimage {}", log_bytes!(payment_preimage.0));
@@ -6017,8 +6021,9 @@ mod tests {
60176021
// further events will be generated for subsequence path successes.
60186022
let events = nodes[0].node.get_and_clear_pending_events();
60196023
match events[0] {
6020-
Event::PaymentSent { payment_preimage: ref preimage } => {
6024+
Event::PaymentSent { payment_preimage: ref preimage, payment_hash: ref hash } => {
60216025
assert_eq!(payment_preimage, *preimage);
6026+
assert_eq!(our_payment_hash, *hash);
60226027
},
60236028
_ => panic!("Unexpected event"),
60246029
}

lightning/src/ln/functional_test_utils.rs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1043,10 +1043,12 @@ macro_rules! expect_payment_received {
10431043
macro_rules! expect_payment_sent {
10441044
($node: expr, $expected_payment_preimage: expr) => {
10451045
let events = $node.node.get_and_clear_pending_events();
1046+
let expected_payment_hash = PaymentHash(Sha256::hash(&$expected_payment_preimage.0).into_inner());
10461047
assert_eq!(events.len(), 1);
10471048
match events[0] {
1048-
Event::PaymentSent { ref payment_preimage } => {
1049+
Event::PaymentSent { ref payment_preimage, ref payment_hash } => {
10491050
assert_eq!($expected_payment_preimage, *payment_preimage);
1051+
assert_eq!(expected_payment_hash, *payment_hash);
10501052
},
10511053
_ => panic!("Unexpected event"),
10521054
}

lightning/src/ln/functional_tests.rs

Lines changed: 22 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -2521,8 +2521,8 @@ fn test_htlc_on_chain_success() {
25212521
send_payment(&nodes[0], &vec!(&nodes[1], &nodes[2])[..], 8000000);
25222522
send_payment(&nodes[0], &vec!(&nodes[1], &nodes[2])[..], 8000000);
25232523

2524-
let (our_payment_preimage, _payment_hash, _payment_secret) = route_payment(&nodes[0], &vec!(&nodes[1], &nodes[2]), 3000000);
2525-
let (our_payment_preimage_2, _payment_hash_2, _payment_secret_2) = route_payment(&nodes[0], &vec!(&nodes[1], &nodes[2]), 3000000);
2524+
let (our_payment_preimage, payment_hash_1, _payment_secret) = route_payment(&nodes[0], &vec!(&nodes[1], &nodes[2]), 3000000);
2525+
let (our_payment_preimage_2, payment_hash_2, _payment_secret_2) = route_payment(&nodes[0], &vec!(&nodes[1], &nodes[2]), 3000000);
25262526

25272527
// Broadcast legit commitment tx from C on B's chain
25282528
// Broadcast HTLC Success transaction by C on received output from C's commitment tx on B's chain
@@ -2682,12 +2682,13 @@ fn test_htlc_on_chain_success() {
26822682
let mut first_claimed = false;
26832683
for event in events {
26842684
match event {
2685-
Event::PaymentSent { payment_preimage } => {
2686-
if payment_preimage == our_payment_preimage {
2685+
Event::PaymentSent { payment_preimage, payment_hash } => {
2686+
if payment_preimage == our_payment_preimage && payment_hash == payment_hash_1 {
26872687
assert!(!first_claimed);
26882688
first_claimed = true;
26892689
} else {
26902690
assert_eq!(payment_preimage, our_payment_preimage_2);
2691+
assert_eq!(payment_hash, payment_hash_2);
26912692
}
26922693
},
26932694
Event::ChannelClosed { reason: ClosureReason::CommitmentTxConfirmed, .. } => {},
@@ -3370,7 +3371,7 @@ fn test_simple_peer_disconnect() {
33703371
nodes[1].node.peer_disconnected(&nodes[0].node.get_our_node_id(), false);
33713372
reconnect_nodes(&nodes[0], &nodes[1], (false, false), (0, 0), (0, 0), (0, 0), (0, 0), (0, 0), (false, false));
33723373

3373-
let payment_preimage_3 = route_payment(&nodes[0], &vec!(&nodes[1], &nodes[2])[..], 1000000).0;
3374+
let (payment_preimage_3, payment_hash_3, _) = route_payment(&nodes[0], &vec!(&nodes[1], &nodes[2])[..], 1000000);
33743375
let payment_preimage_4 = route_payment(&nodes[0], &vec!(&nodes[1], &nodes[2])[..], 1000000).0;
33753376
let payment_hash_5 = route_payment(&nodes[0], &vec!(&nodes[1], &nodes[2])[..], 1000000).1;
33763377
let payment_hash_6 = route_payment(&nodes[0], &vec!(&nodes[1], &nodes[2])[..], 1000000).1;
@@ -3386,8 +3387,9 @@ fn test_simple_peer_disconnect() {
33863387
let events = nodes[0].node.get_and_clear_pending_events();
33873388
assert_eq!(events.len(), 2);
33883389
match events[0] {
3389-
Event::PaymentSent { payment_preimage } => {
3390+
Event::PaymentSent { payment_preimage, payment_hash } => {
33903391
assert_eq!(payment_preimage, payment_preimage_3);
3392+
assert_eq!(payment_hash, payment_hash_3);
33913393
},
33923394
_ => panic!("Unexpected event"),
33933395
}
@@ -3554,8 +3556,9 @@ fn do_test_drop_messages_peer_disconnect(messages_delivered: u8, simulate_broken
35543556
let events_4 = nodes[0].node.get_and_clear_pending_events();
35553557
assert_eq!(events_4.len(), 1);
35563558
match events_4[0] {
3557-
Event::PaymentSent { ref payment_preimage } => {
3559+
Event::PaymentSent { ref payment_preimage, ref payment_hash } => {
35583560
assert_eq!(payment_preimage_1, *payment_preimage);
3561+
assert_eq!(payment_hash_1, *payment_hash);
35593562
},
35603563
_ => panic!("Unexpected event"),
35613564
}
@@ -3594,8 +3597,9 @@ fn do_test_drop_messages_peer_disconnect(messages_delivered: u8, simulate_broken
35943597
let events_4 = nodes[0].node.get_and_clear_pending_events();
35953598
assert_eq!(events_4.len(), 1);
35963599
match events_4[0] {
3597-
Event::PaymentSent { ref payment_preimage } => {
3600+
Event::PaymentSent { ref payment_preimage, ref payment_hash } => {
35983601
assert_eq!(payment_preimage_1, *payment_preimage);
3602+
assert_eq!(payment_hash_1, *payment_hash);
35993603
},
36003604
_ => panic!("Unexpected event"),
36013605
}
@@ -3800,7 +3804,7 @@ fn test_drop_messages_peer_disconnect_dual_htlc() {
38003804
create_announced_chan_between_nodes(&nodes, 0, 1, InitFeatures::known(), InitFeatures::known());
38013805
let logger = test_utils::TestLogger::new();
38023806

3803-
let (payment_preimage_1, _, _) = route_payment(&nodes[0], &[&nodes[1]], 1000000);
3807+
let (payment_preimage_1, payment_hash_1, _) = route_payment(&nodes[0], &[&nodes[1]], 1000000);
38043808

38053809
// Now try to send a second payment which will fail to send
38063810
let (payment_preimage_2, payment_hash_2, payment_secret_2) = get_payment_preimage_hash!(nodes[1]);
@@ -3834,8 +3838,9 @@ fn test_drop_messages_peer_disconnect_dual_htlc() {
38343838
let events_3 = nodes[0].node.get_and_clear_pending_events();
38353839
assert_eq!(events_3.len(), 1);
38363840
match events_3[0] {
3837-
Event::PaymentSent { ref payment_preimage } => {
3841+
Event::PaymentSent { ref payment_preimage, ref payment_hash } => {
38383842
assert_eq!(*payment_preimage, payment_preimage_1);
3843+
assert_eq!(*payment_hash, payment_hash_1);
38393844
},
38403845
_ => panic!("Unexpected event"),
38413846
}
@@ -5251,8 +5256,9 @@ fn test_duplicate_payment_hash_one_failure_one_success() {
52515256

52525257
let events = nodes[0].node.get_and_clear_pending_events();
52535258
match events[0] {
5254-
Event::PaymentSent { ref payment_preimage } => {
5259+
Event::PaymentSent { ref payment_preimage, ref payment_hash } => {
52555260
assert_eq!(*payment_preimage, our_payment_preimage);
5261+
assert_eq!(*payment_hash, duplicate_payment_hash);
52565262
}
52575263
_ => panic!("Unexpected event"),
52585264
}
@@ -5754,7 +5760,7 @@ fn do_htlc_claim_local_commitment_only(use_dust: bool) {
57545760
let nodes = create_network(2, &node_cfgs, &node_chanmgrs);
57555761
let chan = create_announced_chan_between_nodes(&nodes, 0, 1, InitFeatures::known(), InitFeatures::known());
57565762

5757-
let (our_payment_preimage, _, _) = route_payment(&nodes[0], &[&nodes[1]], if use_dust { 50000 } else { 3000000 });
5763+
let (our_payment_preimage, our_payment_hash, _) = route_payment(&nodes[0], &[&nodes[1]], if use_dust { 50000 } else { 3000000 });
57585764

57595765
// Claim the payment, but don't deliver A's commitment_signed, resulting in the HTLC only being
57605766
// present in B's local commitment transaction, but none of A's commitment transactions.
@@ -5766,8 +5772,9 @@ fn do_htlc_claim_local_commitment_only(use_dust: bool) {
57665772
let events = nodes[0].node.get_and_clear_pending_events();
57675773
assert_eq!(events.len(), 1);
57685774
match events[0] {
5769-
Event::PaymentSent { payment_preimage } => {
5775+
Event::PaymentSent { payment_preimage, payment_hash } => {
57705776
assert_eq!(payment_preimage, our_payment_preimage);
5777+
assert_eq!(payment_hash, our_payment_hash);
57715778
},
57725779
_ => panic!("Unexpected event"),
57735780
}
@@ -6201,8 +6208,9 @@ fn test_free_and_fail_holding_cell_htlcs() {
62016208
let events = nodes[0].node.get_and_clear_pending_events();
62026209
assert_eq!(events.len(), 1);
62036210
match events[0] {
6204-
Event::PaymentSent { ref payment_preimage } => {
6211+
Event::PaymentSent { ref payment_preimage, ref payment_hash } => {
62056212
assert_eq!(*payment_preimage, payment_preimage_1);
6213+
assert_eq!(*payment_hash, payment_hash_1);
62066214
}
62076215
_ => panic!("Unexpected event"),
62086216
}

lightning/src/ln/reorg_tests.rs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
use chain::channelmonitor::{ANTI_REORG_DELAY, ChannelMonitor};
1313
use chain::transaction::OutPoint;
1414
use chain::{Confirm, Watch};
15+
use ln::PaymentHash;
1516
use ln::channelmanager::{ChannelManager, ChannelManagerReadArgs};
1617
use ln::features::InitFeatures;
1718
use ln::msgs::{ChannelMessageHandler, ErrorAction};
@@ -24,6 +25,8 @@ use util::ser::{ReadableArgs, Writeable};
2425
use bitcoin::blockdata::block::{Block, BlockHeader};
2526
use bitcoin::blockdata::script::Builder;
2627
use bitcoin::blockdata::opcodes;
28+
use bitcoin::hashes::sha256::Hash as Sha256;
29+
use bitcoin::hashes::Hash;
2730
use bitcoin::hash_types::BlockHash;
2831
use bitcoin::secp256k1::Secp256k1;
2932

lightning/src/ln/shutdown_tests.rs

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -82,7 +82,7 @@ fn updates_shutdown_wait() {
8282
let chan_2 = create_announced_chan_between_nodes(&nodes, 1, 2, InitFeatures::known(), InitFeatures::known());
8383
let logger = test_utils::TestLogger::new();
8484

85-
let (our_payment_preimage, _, _) = route_payment(&nodes[0], &[&nodes[1], &nodes[2]], 100000);
85+
let (our_payment_preimage, our_payment_hash, _) = route_payment(&nodes[0], &[&nodes[1], &nodes[2]], 100000);
8686

8787
nodes[0].node.close_channel(&chan_1.2).unwrap();
8888
let node_0_shutdown = get_event_msg!(nodes[0], MessageSendEvent::SendShutdown, nodes[1].node.get_our_node_id());
@@ -127,8 +127,9 @@ fn updates_shutdown_wait() {
127127
let events = nodes[0].node.get_and_clear_pending_events();
128128
assert_eq!(events.len(), 1);
129129
match events[0] {
130-
Event::PaymentSent { ref payment_preimage } => {
130+
Event::PaymentSent { ref payment_preimage, ref payment_hash } => {
131131
assert_eq!(our_payment_preimage, *payment_preimage);
132+
assert_eq!(our_payment_hash, *payment_hash);
132133
},
133134
_ => panic!("Unexpected event"),
134135
}
@@ -242,7 +243,7 @@ fn do_test_shutdown_rebroadcast(recv_count: u8) {
242243
let chan_1 = create_announced_chan_between_nodes(&nodes, 0, 1, InitFeatures::known(), InitFeatures::known());
243244
let chan_2 = create_announced_chan_between_nodes(&nodes, 1, 2, InitFeatures::known(), InitFeatures::known());
244245

245-
let (our_payment_preimage, _, _) = route_payment(&nodes[0], &[&nodes[1], &nodes[2]], 100000);
246+
let (our_payment_preimage, our_payment_hash, _) = route_payment(&nodes[0], &[&nodes[1], &nodes[2]], 100000);
246247

247248
nodes[1].node.close_channel(&chan_1.2).unwrap();
248249
let node_1_shutdown = get_event_msg!(nodes[1], MessageSendEvent::SendShutdown, nodes[0].node.get_our_node_id());
@@ -307,8 +308,9 @@ fn do_test_shutdown_rebroadcast(recv_count: u8) {
307308
let events = nodes[0].node.get_and_clear_pending_events();
308309
assert_eq!(events.len(), 1);
309310
match events[0] {
310-
Event::PaymentSent { ref payment_preimage } => {
311+
Event::PaymentSent { ref payment_preimage, ref payment_hash } => {
311312
assert_eq!(our_payment_preimage, *payment_preimage);
313+
assert_eq!(our_payment_hash, *payment_hash);
312314
},
313315
_ => panic!("Unexpected event"),
314316
}

lightning/src/util/events.rs

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,8 @@ use util::ser::{BigSize, FixedLengthReader, Writeable, Writer, MaybeReadable, Re
2323
use routing::router::RouteHop;
2424

2525
use bitcoin::blockdata::script::Script;
26-
26+
use bitcoin::hashes::Hash;
27+
use bitcoin::hashes::sha256::Hash as Sha256;
2728
use bitcoin::secp256k1::key::PublicKey;
2829

2930
use io;
@@ -177,6 +178,10 @@ pub enum Event {
177178
/// Note that this serves as a payment receipt, if you wish to have such a thing, you must
178179
/// store it somehow!
179180
payment_preimage: PaymentPreimage,
181+
/// The hash which was given to [`ChannelManager::send_payment`].
182+
///
183+
/// [`ChannelManager::send_payment`]: crate::ln::channelmanager::ChannelManager::send_payment
184+
payment_hash: PaymentHash,
180185
},
181186
/// Indicates an outbound payment we made failed. Probably some intermediary node dropped
182187
/// something. You may wish to retry with a different route.
@@ -287,10 +292,11 @@ impl Writeable for Event {
287292
(8, payment_preimage, option),
288293
});
289294
},
290-
&Event::PaymentSent { ref payment_preimage } => {
295+
&Event::PaymentSent { ref payment_preimage, ref payment_hash} => {
291296
2u8.write(writer)?;
292297
write_tlv_fields!(writer, {
293298
(0, payment_preimage, required),
299+
(1, payment_hash, required),
294300
});
295301
},
296302
&Event::PaymentPathFailed { ref payment_hash, ref rejected_by_dest, ref network_update,
@@ -387,11 +393,17 @@ impl MaybeReadable for Event {
387393
2u8 => {
388394
let f = || {
389395
let mut payment_preimage = PaymentPreimage([0; 32]);
396+
let mut payment_hash = None;
390397
read_tlv_fields!(reader, {
391398
(0, payment_preimage, required),
399+
(1, payment_hash, option),
392400
});
401+
if payment_hash.is_none() {
402+
payment_hash = Some(PaymentHash(Sha256::hash(&payment_preimage.0[..]).into_inner()));
403+
}
393404
Ok(Some(Event::PaymentSent {
394405
payment_preimage,
406+
payment_hash: payment_hash.unwrap(),
395407
}))
396408
};
397409
f()

0 commit comments

Comments
 (0)