Skip to content

Commit 5634f57

Browse files
committed
f - Add payment hash to PaymentPathSuccessful
1 parent 5a47b62 commit 5634f57

File tree

3 files changed

+45
-16
lines changed

3 files changed

+45
-16
lines changed

lightning/src/ln/channelmanager.rs

Lines changed: 22 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -449,6 +449,7 @@ pub(crate) enum PendingOutboundPayment {
449449
/// and add a pending payment that was already fulfilled.
450450
Fulfilled {
451451
session_privs: HashSet<[u8; 32]>,
452+
payment_hash: Option<PaymentHash>,
452453
},
453454
}
454455

@@ -472,23 +473,32 @@ impl PendingOutboundPayment {
472473
}
473474
}
474475

476+
fn payment_hash(&self) -> Option<PaymentHash> {
477+
match self {
478+
PendingOutboundPayment::Legacy { .. } => None,
479+
PendingOutboundPayment::Retryable { payment_hash, .. } => Some(*payment_hash),
480+
PendingOutboundPayment::Fulfilled { payment_hash, .. } => *payment_hash,
481+
}
482+
}
483+
475484
fn mark_fulfilled(&mut self) {
476485
let mut session_privs = HashSet::new();
477486
core::mem::swap(&mut session_privs, match self {
478487
PendingOutboundPayment::Legacy { session_privs } |
479488
PendingOutboundPayment::Retryable { session_privs, .. } |
480-
PendingOutboundPayment::Fulfilled { session_privs }
489+
PendingOutboundPayment::Fulfilled { session_privs, .. }
481490
=> session_privs
482491
});
483-
*self = PendingOutboundPayment::Fulfilled { session_privs };
492+
let payment_hash = self.payment_hash();
493+
*self = PendingOutboundPayment::Fulfilled { session_privs, payment_hash };
484494
}
485495

486496
/// panics if path is None and !self.is_fulfilled
487497
fn remove(&mut self, session_priv: &[u8; 32], path: Option<&Vec<RouteHop>>) -> bool {
488498
let remove_res = match self {
489499
PendingOutboundPayment::Legacy { session_privs } |
490500
PendingOutboundPayment::Retryable { session_privs, .. } |
491-
PendingOutboundPayment::Fulfilled { session_privs } => {
501+
PendingOutboundPayment::Fulfilled { session_privs, .. } => {
492502
session_privs.remove(session_priv)
493503
}
494504
};
@@ -529,7 +539,7 @@ impl PendingOutboundPayment {
529539
match self {
530540
PendingOutboundPayment::Legacy { session_privs } |
531541
PendingOutboundPayment::Retryable { session_privs, .. } |
532-
PendingOutboundPayment::Fulfilled { session_privs } => {
542+
PendingOutboundPayment::Fulfilled { session_privs, .. } => {
533543
session_privs.len()
534544
}
535545
}
@@ -3503,6 +3513,7 @@ impl<Signer: Sign, M: Deref, T: Deref, K: Deref, F: Deref, L: Deref> ChannelMana
35033513
pending_events.push(
35043514
events::Event::PaymentPathSuccessful {
35053515
payment_id,
3516+
payment_hash: payment.get().payment_hash(),
35063517
path,
35073518
}
35083519
);
@@ -3546,9 +3557,11 @@ impl<Signer: Sign, M: Deref, T: Deref, K: Deref, F: Deref, L: Deref> ChannelMana
35463557
// TODO: We should have a second monitor event that informs us of payments
35473558
// irrevocably fulfilled.
35483559
if payment.get_mut().remove(&session_priv_bytes, Some(&path)) {
3560+
let payment_hash = Some(PaymentHash(Sha256::hash(&payment_preimage.0).into_inner()));
35493561
pending_events.push(
35503562
events::Event::PaymentPathSuccessful {
35513563
payment_id,
3564+
payment_hash,
35523565
path,
35533566
}
35543567
);
@@ -5576,6 +5589,7 @@ impl_writeable_tlv_based_enum_upgradable!(PendingOutboundPayment,
55765589
},
55775590
(1, Fulfilled) => {
55785591
(0, session_privs, required),
5592+
(1, payment_hash, option),
55795593
},
55805594
(2, Retryable) => {
55815595
(0, session_privs, required),
@@ -6355,15 +6369,17 @@ mod tests {
63556369
_ => panic!("Unexpected event"),
63566370
}
63576371
match events[1] {
6358-
Event::PaymentPathSuccessful { payment_id: ref actual_payment_id, ref path } => {
6372+
Event::PaymentPathSuccessful { payment_id: ref actual_payment_id, ref payment_hash, ref path } => {
63596373
assert_eq!(payment_id, *actual_payment_id);
6374+
assert_eq!(our_payment_hash, *payment_hash.as_ref().unwrap());
63606375
assert_eq!(route.paths[0], *path);
63616376
},
63626377
_ => panic!("Unexpected event"),
63636378
}
63646379
match events[2] {
6365-
Event::PaymentPathSuccessful { payment_id: ref actual_payment_id, ref path } => {
6380+
Event::PaymentPathSuccessful { payment_id: ref actual_payment_id, ref payment_hash, ref path } => {
63666381
assert_eq!(payment_id, *actual_payment_id);
6382+
assert_eq!(our_payment_hash, *payment_hash.as_ref().unwrap());
63676383
assert_eq!(route.paths[0], *path);
63686384
},
63696385
_ => panic!("Unexpected event"),

lightning/src/ln/functional_test_utils.rs

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1104,21 +1104,25 @@ macro_rules! expect_payment_sent {
11041104
} else {
11051105
assert_eq!(events.len(), 1);
11061106
}
1107-
match events[0] {
1108-
Event::PaymentSent { payment_id: _, ref payment_preimage, ref payment_hash, ref fee_paid_msat } => {
1107+
let expected_payment_id = match events[0] {
1108+
Event::PaymentSent { ref payment_id, ref payment_preimage, ref payment_hash, ref fee_paid_msat } => {
11091109
assert_eq!($expected_payment_preimage, *payment_preimage);
11101110
assert_eq!(expected_payment_hash, *payment_hash);
11111111
assert!(fee_paid_msat.is_some());
11121112
if $expected_fee_msat_opt.is_some() {
11131113
assert_eq!(*fee_paid_msat, $expected_fee_msat_opt);
11141114
}
1115+
payment_id.unwrap()
11151116
},
11161117
_ => panic!("Unexpected event"),
1117-
}
1118+
};
11181119
if $expect_paths {
11191120
for i in 1..events.len() {
11201121
match events[i] {
1121-
Event::PaymentPathSuccessful { .. } => {},
1122+
Event::PaymentPathSuccessful { payment_id, payment_hash, .. } => {
1123+
assert_eq!(payment_id, expected_payment_id);
1124+
assert_eq!(payment_hash, Some(expected_payment_hash));
1125+
},
11221126
_ => panic!("Unexpected event"),
11231127
}
11241128
}

lightning/src/util/events.rs

Lines changed: 15 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -216,7 +216,7 @@ pub enum Event {
216216
/// Note that this serves as a payment receipt, if you wish to have such a thing, you must
217217
/// store it somehow!
218218
payment_preimage: PaymentPreimage,
219-
/// The hash which was given to [`ChannelManager::send_payment`].
219+
/// The hash that was given to [`ChannelManager::send_payment`].
220220
///
221221
/// [`ChannelManager::send_payment`]: crate::ln::channelmanager::ChannelManager::send_payment
222222
payment_hash: PaymentHash,
@@ -240,7 +240,9 @@ pub enum Event {
240240
/// [`ChannelManager::send_payment`]: crate::ln::channelmanager::ChannelManager::send_payment
241241
/// [`ChannelManager::retry_payment`]: crate::ln::channelmanager::ChannelManager::retry_payment
242242
payment_id: Option<PaymentId>,
243-
/// The hash which was given to ChannelManager::send_payment.
243+
/// The hash that was given to [`ChannelManager::send_payment`].
244+
///
245+
/// [`ChannelManager::send_payment`]: crate::ln::channelmanager::ChannelManager::send_payment
244246
payment_hash: PaymentHash,
245247
/// Indicates the payment was rejected for some reason by the recipient. This implies that
246248
/// the payment has failed, not just the route in question. If this is not set, you may
@@ -349,6 +351,10 @@ pub enum Event {
349351
/// [`ChannelManager::send_payment`]: crate::ln::channelmanager::ChannelManager::send_payment
350352
/// [`ChannelManager::retry_payment`]: crate::ln::channelmanager::ChannelManager::retry_payment
351353
payment_id: PaymentId,
354+
/// The hash that was given to [`ChannelManager::send_payment`].
355+
///
356+
/// [`ChannelManager::send_payment`]: crate::ln::channelmanager::ChannelManager::send_payment
357+
payment_hash: Option<PaymentHash>,
352358
/// The payment path that was successful.
353359
path: Vec<RouteHop>,
354360
},
@@ -451,11 +457,12 @@ impl Writeable for Event {
451457
(2, transaction, required)
452458
})
453459
},
454-
&Event::PaymentPathSuccessful { ref payment_id, ref path } => {
460+
&Event::PaymentPathSuccessful { ref payment_id, ref payment_hash, ref path } => {
455461
13u8.write(writer)?;
456462
write_tlv_fields!(writer, {
457463
(0, payment_id, required),
458-
(2, VecWriteWrapper(path), required)
464+
(2, payment_hash, option),
465+
(4, VecWriteWrapper(path), required)
459466
})
460467
},
461468
// Note that, going forward, all new events must only write data inside of
@@ -623,12 +630,14 @@ impl MaybeReadable for Event {
623630
13u8 => {
624631
let f = || {
625632
let mut payment_id = PaymentId([0; 32]);
633+
let mut payment_hash = None;
626634
let mut path = VecReadWrapper(Vec::new());
627635
read_tlv_fields!(reader, {
628636
(0, payment_id, required),
629-
(2, path, required),
637+
(2, payment_hash, option),
638+
(4, path, required),
630639
});
631-
Ok(Some(Event::PaymentPathSuccessful { payment_id, path: path.0 } ))
640+
Ok(Some(Event::PaymentPathSuccessful { payment_id, payment_hash, path: path.0 } ))
632641
};
633642
f()
634643
},

0 commit comments

Comments
 (0)