Skip to content

Commit d9f55be

Browse files
committed
Add payment_hash to PaymentSent #999
1 parent 801d6e5 commit d9f55be

7 files changed

+65
-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
@@ -308,7 +308,7 @@ fn do_test_monitor_temporary_update_fail(disconnect_count: usize) {
308308
let channel_id = create_announced_chan_between_nodes(&nodes, 0, 1, InitFeatures::known(), InitFeatures::known()).2;
309309
let logger = test_utils::TestLogger::new();
310310

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

313313
// Now try to send a second payment which will fail to send
314314
let (payment_preimage_2, payment_hash_2, payment_secret_2) = get_payment_preimage_hash!(nodes[1]);
@@ -344,8 +344,9 @@ fn do_test_monitor_temporary_update_fail(disconnect_count: usize) {
344344
let events_3 = nodes[0].node.get_and_clear_pending_events();
345345
assert_eq!(events_3.len(), 1);
346346
match events_3[0] {
347-
Event::PaymentSent { ref payment_preimage } => {
347+
Event::PaymentSent { ref payment_preimage, ref payment_hash } => {
348348
assert_eq!(*payment_preimage, payment_preimage_1);
349+
assert_eq!(*payment_hash, payment_hash_1);
349350
},
350351
_ => panic!("Unexpected event"),
351352
}
@@ -436,8 +437,9 @@ fn do_test_monitor_temporary_update_fail(disconnect_count: usize) {
436437
let events_3 = nodes[0].node.get_and_clear_pending_events();
437438
assert_eq!(events_3.len(), 1);
438439
match events_3[0] {
439-
Event::PaymentSent { ref payment_preimage } => {
440+
Event::PaymentSent { ref payment_preimage, ref payment_hash } => {
440441
assert_eq!(*payment_preimage, payment_preimage_1);
442+
assert_eq!(*payment_hash, payment_hash_1);
441443
},
442444
_ => panic!("Unexpected event"),
443445
}
@@ -1362,7 +1364,7 @@ fn claim_while_disconnected_monitor_update_fail() {
13621364
let logger = test_utils::TestLogger::new();
13631365

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

13671369
nodes[0].node.peer_disconnected(&nodes[1].node.get_our_node_id(), false);
13681370
nodes[1].node.peer_disconnected(&nodes[0].node.get_our_node_id(), false);
@@ -1463,8 +1465,9 @@ fn claim_while_disconnected_monitor_update_fail() {
14631465
let events = nodes[0].node.get_and_clear_pending_events();
14641466
assert_eq!(events.len(), 1);
14651467
match events[0] {
1466-
Event::PaymentSent { ref payment_preimage } => {
1468+
Event::PaymentSent { ref payment_preimage, ref payment_hash } => {
14671469
assert_eq!(*payment_preimage, payment_preimage_1);
1470+
assert_eq!(*payment_hash, payment_hash_1);
14681471
},
14691472
_ => panic!("Unexpected event"),
14701473
}
@@ -1845,7 +1848,7 @@ fn monitor_update_claim_fail_no_response() {
18451848
let logger = test_utils::TestLogger::new();
18461849

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

18501853
// Now start forwarding a second payment, skipping the last RAA so B is in AwaitingRAA
18511854
let (payment_preimage_2, payment_hash_2, payment_secret_2) = get_payment_preimage_hash!(nodes[1]);
@@ -1887,8 +1890,9 @@ fn monitor_update_claim_fail_no_response() {
18871890
let events = nodes[0].node.get_and_clear_pending_events();
18881891
assert_eq!(events.len(), 1);
18891892
match events[0] {
1890-
Event::PaymentSent { ref payment_preimage } => {
1893+
Event::PaymentSent { ref payment_preimage, ref payment_hash } => {
18911894
assert_eq!(*payment_preimage, payment_preimage_1);
1895+
assert_eq!(*payment_hash, payment_hash_1);
18921896
},
18931897
_ => panic!("Unexpected event"),
18941898
}

lightning/src/ln/channelmanager.rs

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3161,8 +3161,12 @@ impl<Signer: Sign, M: Deref, T: Deref, K: Deref, F: Deref, L: Deref> ChannelMana
31613161
sessions.remove(&session_priv_bytes)
31623162
} else { false };
31633163
if found_payment {
3164+
let payment_hash = PaymentHash(Sha256::hash(&payment_preimage.0).into_inner());
31643165
self.pending_events.lock().unwrap().push(
3165-
events::Event::PaymentSent { payment_preimage }
3166+
events::Event::PaymentSent {
3167+
payment_preimage,
3168+
payment_hash: payment_hash
3169+
}
31663170
);
31673171
} else {
31683172
log_trace!(self.logger, "Received duplicative fulfill for HTLC with payment_preimage {}", log_bytes!(payment_preimage.0));
@@ -5772,8 +5776,9 @@ mod tests {
57725776
// further events will be generated for subsequence path successes.
57735777
let events = nodes[0].node.get_and_clear_pending_events();
57745778
match events[0] {
5775-
Event::PaymentSent { payment_preimage: ref preimage } => {
5779+
Event::PaymentSent { payment_preimage: ref preimage, payment_hash: ref hash } => {
57765780
assert_eq!(payment_preimage, *preimage);
5781+
assert_eq!(our_payment_hash, *hash);
57775782
},
57785783
_ => panic!("Unexpected event"),
57795784
}

lightning/src/ln/functional_test_utils.rs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1011,10 +1011,12 @@ macro_rules! expect_payment_received {
10111011
macro_rules! expect_payment_sent {
10121012
($node: expr, $expected_payment_preimage: expr) => {
10131013
let events = $node.node.get_and_clear_pending_events();
1014+
let expected_payment_hash = PaymentHash(Sha256::hash(&$expected_payment_preimage.0).into_inner());
10141015
assert_eq!(events.len(), 1);
10151016
match events[0] {
1016-
Event::PaymentSent { ref payment_preimage } => {
1017+
Event::PaymentSent { ref payment_preimage, ref payment_hash } => {
10171018
assert_eq!($expected_payment_preimage, *payment_preimage);
1019+
assert_eq!(expected_payment_hash, *payment_hash);
10181020
},
10191021
_ => panic!("Unexpected event"),
10201022
}

lightning/src/ln/functional_tests.rs

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

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

24872487
// Broadcast legit commitment tx from C on B's chain
24882488
// Broadcast HTLC Success transaction by C on received output from C's commitment tx on B's chain
@@ -2636,12 +2636,13 @@ fn test_htlc_on_chain_success() {
26362636
let mut first_claimed = false;
26372637
for event in events {
26382638
match event {
2639-
Event::PaymentSent { payment_preimage } => {
2640-
if payment_preimage == our_payment_preimage {
2639+
Event::PaymentSent { payment_preimage, payment_hash } => {
2640+
if payment_preimage == our_payment_preimage && payment_hash == payment_hash_1 {
26412641
assert!(!first_claimed);
26422642
first_claimed = true;
26432643
} else {
26442644
assert_eq!(payment_preimage, our_payment_preimage_2);
2645+
assert_eq!(payment_hash, payment_hash_2);
26452646
}
26462647
},
26472648
_ => panic!("Unexpected event"),
@@ -3299,7 +3300,7 @@ fn test_simple_peer_disconnect() {
32993300
nodes[1].node.peer_disconnected(&nodes[0].node.get_our_node_id(), false);
33003301
reconnect_nodes(&nodes[0], &nodes[1], (false, false), (0, 0), (0, 0), (0, 0), (0, 0), (0, 0), (false, false));
33013302

3302-
let payment_preimage_3 = route_payment(&nodes[0], &vec!(&nodes[1], &nodes[2])[..], 1000000).0;
3303+
let (payment_preimage_3, payment_hash_3, _) = route_payment(&nodes[0], &vec!(&nodes[1], &nodes[2])[..], 1000000);
33033304
let payment_preimage_4 = route_payment(&nodes[0], &vec!(&nodes[1], &nodes[2])[..], 1000000).0;
33043305
let payment_hash_5 = route_payment(&nodes[0], &vec!(&nodes[1], &nodes[2])[..], 1000000).1;
33053306
let payment_hash_6 = route_payment(&nodes[0], &vec!(&nodes[1], &nodes[2])[..], 1000000).1;
@@ -3315,8 +3316,9 @@ fn test_simple_peer_disconnect() {
33153316
let events = nodes[0].node.get_and_clear_pending_events();
33163317
assert_eq!(events.len(), 2);
33173318
match events[0] {
3318-
Event::PaymentSent { payment_preimage } => {
3319+
Event::PaymentSent { payment_preimage, payment_hash } => {
33193320
assert_eq!(payment_preimage, payment_preimage_3);
3321+
assert_eq!(payment_hash, payment_hash_3);
33203322
},
33213323
_ => panic!("Unexpected event"),
33223324
}
@@ -3483,8 +3485,9 @@ fn do_test_drop_messages_peer_disconnect(messages_delivered: u8, simulate_broken
34833485
let events_4 = nodes[0].node.get_and_clear_pending_events();
34843486
assert_eq!(events_4.len(), 1);
34853487
match events_4[0] {
3486-
Event::PaymentSent { ref payment_preimage } => {
3488+
Event::PaymentSent { ref payment_preimage, ref payment_hash } => {
34873489
assert_eq!(payment_preimage_1, *payment_preimage);
3490+
assert_eq!(payment_hash_1, *payment_hash);
34883491
},
34893492
_ => panic!("Unexpected event"),
34903493
}
@@ -3523,8 +3526,9 @@ fn do_test_drop_messages_peer_disconnect(messages_delivered: u8, simulate_broken
35233526
let events_4 = nodes[0].node.get_and_clear_pending_events();
35243527
assert_eq!(events_4.len(), 1);
35253528
match events_4[0] {
3526-
Event::PaymentSent { ref payment_preimage } => {
3529+
Event::PaymentSent { ref payment_preimage, ref payment_hash } => {
35273530
assert_eq!(payment_preimage_1, *payment_preimage);
3531+
assert_eq!(payment_hash_1, *payment_hash);
35283532
},
35293533
_ => panic!("Unexpected event"),
35303534
}
@@ -3729,7 +3733,7 @@ fn test_drop_messages_peer_disconnect_dual_htlc() {
37293733
create_announced_chan_between_nodes(&nodes, 0, 1, InitFeatures::known(), InitFeatures::known());
37303734
let logger = test_utils::TestLogger::new();
37313735

3732-
let (payment_preimage_1, _, _) = route_payment(&nodes[0], &[&nodes[1]], 1000000);
3736+
let (payment_preimage_1, payment_hash_1, _) = route_payment(&nodes[0], &[&nodes[1]], 1000000);
37333737

37343738
// Now try to send a second payment which will fail to send
37353739
let (payment_preimage_2, payment_hash_2, payment_secret_2) = get_payment_preimage_hash!(nodes[1]);
@@ -3763,8 +3767,9 @@ fn test_drop_messages_peer_disconnect_dual_htlc() {
37633767
let events_3 = nodes[0].node.get_and_clear_pending_events();
37643768
assert_eq!(events_3.len(), 1);
37653769
match events_3[0] {
3766-
Event::PaymentSent { ref payment_preimage } => {
3770+
Event::PaymentSent { ref payment_preimage, ref payment_hash } => {
37673771
assert_eq!(*payment_preimage, payment_preimage_1);
3772+
assert_eq!(*payment_hash, payment_hash_1);
37683773
},
37693774
_ => panic!("Unexpected event"),
37703775
}
@@ -5150,8 +5155,9 @@ fn test_duplicate_payment_hash_one_failure_one_success() {
51505155

51515156
let events = nodes[0].node.get_and_clear_pending_events();
51525157
match events[0] {
5153-
Event::PaymentSent { ref payment_preimage } => {
5158+
Event::PaymentSent { ref payment_preimage, ref payment_hash } => {
51545159
assert_eq!(*payment_preimage, our_payment_preimage);
5160+
assert_eq!(*payment_hash, duplicate_payment_hash);
51555161
}
51565162
_ => panic!("Unexpected event"),
51575163
}
@@ -5631,7 +5637,7 @@ fn do_htlc_claim_local_commitment_only(use_dust: bool) {
56315637
let nodes = create_network(2, &node_cfgs, &node_chanmgrs);
56325638
let chan = create_announced_chan_between_nodes(&nodes, 0, 1, InitFeatures::known(), InitFeatures::known());
56335639

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

56365642
// Claim the payment, but don't deliver A's commitment_signed, resulting in the HTLC only being
56375643
// present in B's local commitment transaction, but none of A's commitment transactions.
@@ -5643,8 +5649,9 @@ fn do_htlc_claim_local_commitment_only(use_dust: bool) {
56435649
let events = nodes[0].node.get_and_clear_pending_events();
56445650
assert_eq!(events.len(), 1);
56455651
match events[0] {
5646-
Event::PaymentSent { payment_preimage } => {
5652+
Event::PaymentSent { payment_preimage, payment_hash } => {
56475653
assert_eq!(payment_preimage, our_payment_preimage);
5654+
assert_eq!(payment_hash, our_payment_hash);
56485655
},
56495656
_ => panic!("Unexpected event"),
56505657
}
@@ -6075,8 +6082,9 @@ fn test_free_and_fail_holding_cell_htlcs() {
60756082
let events = nodes[0].node.get_and_clear_pending_events();
60766083
assert_eq!(events.len(), 1);
60776084
match events[0] {
6078-
Event::PaymentSent { ref payment_preimage } => {
6085+
Event::PaymentSent { ref payment_preimage, ref payment_hash } => {
60796086
assert_eq!(*payment_preimage, payment_preimage_1);
6087+
assert_eq!(*payment_hash, payment_hash_1);
60806088
}
60816089
_ => panic!("Unexpected event"),
60826090
}

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
@@ -80,7 +80,7 @@ fn updates_shutdown_wait() {
8080
let chan_2 = create_announced_chan_between_nodes(&nodes, 1, 2, InitFeatures::known(), InitFeatures::known());
8181
let logger = test_utils::TestLogger::new();
8282

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

8585
nodes[0].node.close_channel(&chan_1.2).unwrap();
8686
let node_0_shutdown = get_event_msg!(nodes[0], MessageSendEvent::SendShutdown, nodes[1].node.get_our_node_id());
@@ -125,8 +125,9 @@ fn updates_shutdown_wait() {
125125
let events = nodes[0].node.get_and_clear_pending_events();
126126
assert_eq!(events.len(), 1);
127127
match events[0] {
128-
Event::PaymentSent { ref payment_preimage } => {
128+
Event::PaymentSent { ref payment_preimage, ref payment_hash } => {
129129
assert_eq!(our_payment_preimage, *payment_preimage);
130+
assert_eq!(our_payment_hash, *payment_hash);
130131
},
131132
_ => panic!("Unexpected event"),
132133
}
@@ -233,7 +234,7 @@ fn do_test_shutdown_rebroadcast(recv_count: u8) {
233234
let chan_1 = create_announced_chan_between_nodes(&nodes, 0, 1, InitFeatures::known(), InitFeatures::known());
234235
let chan_2 = create_announced_chan_between_nodes(&nodes, 1, 2, InitFeatures::known(), InitFeatures::known());
235236

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

238239
nodes[1].node.close_channel(&chan_1.2).unwrap();
239240
let node_1_shutdown = get_event_msg!(nodes[1], MessageSendEvent::SendShutdown, nodes[0].node.get_our_node_id());
@@ -298,8 +299,9 @@ fn do_test_shutdown_rebroadcast(recv_count: u8) {
298299
let events = nodes[0].node.get_and_clear_pending_events();
299300
assert_eq!(events.len(), 1);
300301
match events[0] {
301-
Event::PaymentSent { ref payment_preimage } => {
302+
Event::PaymentSent { ref payment_preimage, ref payment_hash } => {
302303
assert_eq!(our_payment_preimage, *payment_preimage);
304+
assert_eq!(our_payment_hash, *payment_hash);
303305
},
304306
_ => panic!("Unexpected event"),
305307
}

lightning/src/util/events.rs

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,8 @@ use routing::network_graph::NetworkUpdate;
2121
use util::ser::{Writeable, Writer, MaybeReadable, Readable, VecReadWrapper, VecWriteWrapper};
2222

2323
use bitcoin::blockdata::script::Script;
24-
24+
use bitcoin::hashes::Hash;
25+
use bitcoin::hashes::sha256::Hash as Sha256;
2526
use bitcoin::secp256k1::key::PublicKey;
2627

2728
use io;
@@ -122,6 +123,9 @@ pub enum Event {
122123
/// Note that this serves as a payment receipt, if you wish to have such a thing, you must
123124
/// store it somehow!
124125
payment_preimage: PaymentPreimage,
126+
/// The hash which was given to [`ChannelManager::send_payment`].
127+
/// [`ChannelManager::send_payment`]: crate::ln::channelmanager::ChannelManager::send_payment
128+
payment_hash: PaymentHash,
125129
},
126130
/// Indicates an outbound payment we made failed. Probably some intermediary node dropped
127131
/// something. You may wish to retry with a different route.
@@ -222,10 +226,11 @@ impl Writeable for Event {
222226
(8, payment_preimage, option),
223227
});
224228
},
225-
&Event::PaymentSent { ref payment_preimage } => {
229+
&Event::PaymentSent { ref payment_preimage, ref payment_hash} => {
226230
2u8.write(writer)?;
227231
write_tlv_fields!(writer, {
228232
(0, payment_preimage, required),
233+
(1, payment_hash, required),
229234
});
230235
},
231236
&Event::PaymentFailed { ref payment_hash, ref rejected_by_dest, ref network_update, ref all_paths_failed,
@@ -309,11 +314,17 @@ impl MaybeReadable for Event {
309314
2u8 => {
310315
let f = || {
311316
let mut payment_preimage = PaymentPreimage([0; 32]);
317+
let mut payment_hash = None;
312318
read_tlv_fields!(reader, {
313319
(0, payment_preimage, required),
320+
(1, payment_hash, option),
314321
});
322+
if payment_hash.is_none() {
323+
payment_hash = Some(PaymentHash(Sha256::hash(&payment_preimage.0[..]).into_inner()));
324+
}
315325
Ok(Some(Event::PaymentSent {
316326
payment_preimage,
327+
payment_hash: payment_hash.unwrap(),
317328
}))
318329
};
319330
f()

0 commit comments

Comments
 (0)