Skip to content

Commit 46d4568

Browse files
committed
Add payment_hash to PaymentSent lightningdevkit#999
1 parent 4368b56 commit 46d4568

7 files changed

+60
-31
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, Some(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, Some(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, Some(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, Some(payment_hash_1));
18921896
},
18931897
_ => panic!("Unexpected event"),
18941898
}

lightning/src/ln/channelmanager.rs

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3101,7 +3101,7 @@ impl<Signer: Sign, M: Deref, T: Deref, K: Deref, F: Deref, L: Deref> ChannelMana
31013101
} else { unreachable!(); }
31023102
}
31033103

3104-
fn claim_funds_internal(&self, mut channel_state_lock: MutexGuard<ChannelHolder<Signer>>, source: HTLCSource, payment_preimage: PaymentPreimage, forwarded_htlc_value_msat: Option<u64>, from_onchain: bool) {
3104+
fn claim_funds_internal(&self, mut channel_state_lock: MutexGuard<ChannelHolder<Signer>>, source: HTLCSource, payment_preimage: PaymentPreimage, payment_hash: PaymentHash, forwarded_htlc_value_msat: Option<u64>, from_onchain: bool) {
31053105
match source {
31063106
HTLCSource::OutboundRoute { session_priv, .. } => {
31073107
mem::drop(channel_state_lock);
@@ -3112,7 +3112,8 @@ impl<Signer: Sign, M: Deref, T: Deref, K: Deref, F: Deref, L: Deref> ChannelMana
31123112
} {
31133113
let mut pending_events = self.pending_events.lock().unwrap();
31143114
pending_events.push(events::Event::PaymentSent {
3115-
payment_preimage
3115+
payment_preimage,
3116+
payment_hash: Some(payment_hash),
31163117
});
31173118
} else {
31183119
log_trace!(self.logger, "Received duplicative fulfill for HTLC with payment_preimage {}", log_bytes!(payment_preimage.0));
@@ -3578,7 +3579,8 @@ impl<Signer: Sign, M: Deref, T: Deref, K: Deref, F: Deref, L: Deref> ChannelMana
35783579
hash_map::Entry::Vacant(_) => return Err(MsgHandleErrInternal::send_err_msg_no_close("Failed to find corresponding channel".to_owned(), msg.channel_id))
35793580
}
35803581
};
3581-
self.claim_funds_internal(channel_lock, htlc_source, msg.payment_preimage.clone(), Some(forwarded_htlc_value), false);
3582+
let payment_hash = PaymentHash(Sha256::hash(&msg.payment_preimage.0).into_inner());
3583+
self.claim_funds_internal(channel_lock, htlc_source, msg.payment_preimage.clone(), payment_hash, Some(forwarded_htlc_value), false);
35823584
Ok(())
35833585
}
35843586

@@ -3883,7 +3885,7 @@ impl<Signer: Sign, M: Deref, T: Deref, K: Deref, F: Deref, L: Deref> ChannelMana
38833885
MonitorEvent::HTLCEvent(htlc_update) => {
38843886
if let Some(preimage) = htlc_update.payment_preimage {
38853887
log_trace!(self.logger, "Claiming HTLC with preimage {} from our monitor", log_bytes!(preimage.0));
3886-
self.claim_funds_internal(self.channel_state.lock().unwrap(), htlc_update.source, preimage, htlc_update.onchain_value_satoshis.map(|v| v * 1000), true);
3888+
self.claim_funds_internal(self.channel_state.lock().unwrap(), htlc_update.source, preimage, htlc_update.payment_hash, htlc_update.onchain_value_satoshis.map(|v| v * 1000), true);
38873889
} else {
38883890
log_trace!(self.logger, "Failing HTLC with hash {} from our monitor", log_bytes!(htlc_update.payment_hash.0));
38893891
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() });
@@ -5593,14 +5595,16 @@ mod tests {
55935595
// There's an existing bug that generates a PaymentSent event for each MPP path, so handle that here.
55945596
let events = nodes[0].node.get_and_clear_pending_events();
55955597
match events[0] {
5596-
Event::PaymentSent { payment_preimage: ref preimage } => {
5598+
Event::PaymentSent { payment_preimage: ref preimage, payment_hash: ref hash } => {
55975599
assert_eq!(payment_preimage, *preimage);
5600+
assert_eq!(Some(our_payment_hash), *hash);
55985601
},
55995602
_ => panic!("Unexpected event"),
56005603
}
56015604
match events[1] {
5602-
Event::PaymentSent { payment_preimage: ref preimage } => {
5605+
Event::PaymentSent { payment_preimage: ref preimage, payment_hash: ref hash } => {
56035606
assert_eq!(payment_preimage, *preimage);
5607+
assert_eq!(Some(our_payment_hash), *hash);
56045608
},
56055609
_ => panic!("Unexpected event"),
56065610
}

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 = Some(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: 20 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -2632,12 +2632,13 @@ fn test_htlc_on_chain_success() {
26322632
let mut first_claimed = false;
26332633
for event in events {
26342634
match event {
2635-
Event::PaymentSent { payment_preimage } => {
2636-
if payment_preimage == our_payment_preimage {
2635+
Event::PaymentSent { payment_preimage, payment_hash } => {
2636+
if payment_preimage == our_payment_preimage && payment_hash == Some(_payment_hash) {
26372637
assert!(!first_claimed);
26382638
first_claimed = true;
26392639
} else {
26402640
assert_eq!(payment_preimage, our_payment_preimage_2);
2641+
assert_eq!(payment_hash, Some(_payment_hash_2));
26412642
}
26422643
},
26432644
_ => 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, Some(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!(Some(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!(Some(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, Some(payment_hash_1));
37683773
},
37693774
_ => panic!("Unexpected event"),
37703775
}
@@ -5200,8 +5205,9 @@ fn test_duplicate_payment_hash_one_failure_one_success() {
52005205

52015206
let events = nodes[0].node.get_and_clear_pending_events();
52025207
match events[0] {
5203-
Event::PaymentSent { ref payment_preimage } => {
5208+
Event::PaymentSent { ref payment_preimage, ref payment_hash } => {
52045209
assert_eq!(*payment_preimage, our_payment_preimage);
5210+
assert_eq!(*payment_hash, Some(duplicate_payment_hash));
52055211
}
52065212
_ => panic!("Unexpected event"),
52075213
}
@@ -5682,7 +5688,7 @@ fn do_htlc_claim_local_commitment_only(use_dust: bool) {
56825688
let nodes = create_network(2, &node_cfgs, &node_chanmgrs);
56835689
let chan = create_announced_chan_between_nodes(&nodes, 0, 1, InitFeatures::known(), InitFeatures::known());
56845690

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

56875693
// Claim the payment, but don't deliver A's commitment_signed, resulting in the HTLC only being
56885694
// present in B's local commitment transaction, but none of A's commitment transactions.
@@ -5694,8 +5700,9 @@ fn do_htlc_claim_local_commitment_only(use_dust: bool) {
56945700
let events = nodes[0].node.get_and_clear_pending_events();
56955701
assert_eq!(events.len(), 1);
56965702
match events[0] {
5697-
Event::PaymentSent { payment_preimage } => {
5703+
Event::PaymentSent { payment_preimage, payment_hash } => {
56985704
assert_eq!(payment_preimage, our_payment_preimage);
5705+
assert_eq!(payment_hash, Some(our_payment_hash));
56995706
},
57005707
_ => panic!("Unexpected event"),
57015708
}
@@ -6122,8 +6129,9 @@ fn test_free_and_fail_holding_cell_htlcs() {
61226129
let events = nodes[0].node.get_and_clear_pending_events();
61236130
assert_eq!(events.len(), 1);
61246131
match events[0] {
6125-
Event::PaymentSent { ref payment_preimage } => {
6132+
Event::PaymentSent { ref payment_preimage, ref payment_hash } => {
61266133
assert_eq!(*payment_preimage, payment_preimage_1);
6134+
assert_eq!(*payment_hash, Some(payment_hash_1));
61276135
}
61286136
_ => panic!("Unexpected event"),
61296137
}

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, HTLCFailChannelUpdate};
@@ -23,6 +24,8 @@ use util::ser::{ReadableArgs, Writeable};
2324
use bitcoin::blockdata::block::{Block, BlockHeader};
2425
use bitcoin::blockdata::script::Builder;
2526
use bitcoin::blockdata::opcodes;
27+
use bitcoin::hashes::sha256::Hash as Sha256;
28+
use bitcoin::hashes::Hash;
2629
use bitcoin::hash_types::BlockHash;
2730
use bitcoin::secp256k1::Secp256k1;
2831

lightning/src/ln/shutdown_tests.rs

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

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

8484
nodes[0].node.close_channel(&chan_1.2).unwrap();
8585
let node_0_shutdown = get_event_msg!(nodes[0], MessageSendEvent::SendShutdown, nodes[1].node.get_our_node_id());
@@ -124,8 +124,9 @@ fn updates_shutdown_wait() {
124124
let events = nodes[0].node.get_and_clear_pending_events();
125125
assert_eq!(events.len(), 1);
126126
match events[0] {
127-
Event::PaymentSent { ref payment_preimage } => {
127+
Event::PaymentSent { ref payment_preimage, ref payment_hash } => {
128128
assert_eq!(our_payment_preimage, *payment_preimage);
129+
assert_eq!(Some(our_payment_hash), *payment_hash);
129130
},
130131
_ => panic!("Unexpected event"),
131132
}
@@ -238,7 +239,7 @@ fn do_test_shutdown_rebroadcast(recv_count: u8) {
238239
let chan_1 = create_announced_chan_between_nodes(&nodes, 0, 1, InitFeatures::known(), InitFeatures::known());
239240
let chan_2 = create_announced_chan_between_nodes(&nodes, 1, 2, InitFeatures::known(), InitFeatures::known());
240241

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

243244
nodes[1].node.close_channel(&chan_1.2).unwrap();
244245
let node_1_shutdown = get_event_msg!(nodes[1], MessageSendEvent::SendShutdown, nodes[0].node.get_our_node_id());
@@ -303,8 +304,9 @@ fn do_test_shutdown_rebroadcast(recv_count: u8) {
303304
let events = nodes[0].node.get_and_clear_pending_events();
304305
assert_eq!(events.len(), 1);
305306
match events[0] {
306-
Event::PaymentSent { ref payment_preimage } => {
307+
Event::PaymentSent { ref payment_preimage, ref payment_hash } => {
307308
assert_eq!(our_payment_preimage, *payment_preimage);
309+
assert_eq!(Some(our_payment_hash), *payment_hash);
308310
},
309311
_ => panic!("Unexpected event"),
310312
}

lightning/src/util/events.rs

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -118,6 +118,8 @@ pub enum Event {
118118
/// Note that this serves as a payment receipt, if you wish to have such a thing, you must
119119
/// store it somehow!
120120
payment_preimage: PaymentPreimage,
121+
/// The hash which was given to ChannelManager::send_payment.
122+
payment_hash: Option<PaymentHash>,
121123
},
122124
/// Indicates an outbound payment we made failed. Probably some intermediary node dropped
123125
/// something. You may wish to retry with a different route.
@@ -205,10 +207,11 @@ impl Writeable for Event {
205207
(8, payment_preimage, option),
206208
});
207209
},
208-
&Event::PaymentSent { ref payment_preimage } => {
210+
&Event::PaymentSent { ref payment_preimage, ref payment_hash} => {
209211
2u8.write(writer)?;
210212
write_tlv_fields!(writer, {
211213
(0, payment_preimage, required),
214+
(1, payment_hash, option),
212215
});
213216
},
214217
&Event::PaymentFailed { ref payment_hash, ref rejected_by_dest,
@@ -290,11 +293,14 @@ impl MaybeReadable for Event {
290293
2u8 => {
291294
let f = || {
292295
let mut payment_preimage = PaymentPreimage([0; 32]);
296+
let mut payment_hash = None;
293297
read_tlv_fields!(reader, {
294298
(0, payment_preimage, required),
299+
(1, payment_hash, option),
295300
});
296301
Ok(Some(Event::PaymentSent {
297302
payment_preimage,
303+
payment_hash,
298304
}))
299305
};
300306
f()

0 commit comments

Comments
 (0)