Skip to content

Commit

Permalink
Add payment_id to Payment{Successful,Failed,Received} events
Browse files Browse the repository at this point in the history
  • Loading branch information
tnull committed May 15, 2024
1 parent 51e2580 commit 1fab656
Show file tree
Hide file tree
Showing 4 changed files with 40 additions and 13 deletions.
6 changes: 3 additions & 3 deletions bindings/ldk_node.udl
Original file line number Diff line number Diff line change
Expand Up @@ -186,9 +186,9 @@ enum BuildError {

[Enum]
interface Event {
PaymentSuccessful(PaymentHash payment_hash, u64? fee_paid_msat);
PaymentFailed(PaymentHash payment_hash, PaymentFailureReason? reason);
PaymentReceived(PaymentHash payment_hash, u64 amount_msat);
PaymentSuccessful(PaymentId? payment_id, PaymentHash payment_hash, u64? fee_paid_msat);
PaymentFailed(PaymentId? payment_id, PaymentHash payment_hash, PaymentFailureReason? reason);
PaymentReceived(PaymentId? payment_id, PaymentHash payment_hash, u64 amount_msat);
ChannelPending(ChannelId channel_id, UserChannelId user_channel_id, ChannelId former_temporary_channel_id, PublicKey counterparty_node_id, OutPoint funding_txo);
ChannelReady(ChannelId channel_id, UserChannelId user_channel_id, PublicKey? counterparty_node_id);
ChannelClosed(ChannelId channel_id, UserChannelId user_channel_id, PublicKey? counterparty_node_id, ClosureReason? reason);
Expand Down
33 changes: 30 additions & 3 deletions src/event.rs
Original file line number Diff line number Diff line change
Expand Up @@ -46,13 +46,21 @@ use std::time::Duration;
pub enum Event {
/// A sent payment was successful.
PaymentSuccessful {
/// A local identifier used to track the payment.
///
/// Will only be `None` for events serialized with LDK Node v0.2.1 or prior.
payment_id: Option<PaymentId>,
/// The hash of the payment.
payment_hash: PaymentHash,
/// The total fee which was spent at intermediate hops in this payment.
fee_paid_msat: Option<u64>,
},
/// A sent payment has failed.
PaymentFailed {
/// A local identifier used to track the payment.
///
/// Will only be `None` for events serialized with LDK Node v0.2.1 or prior.
payment_id: Option<PaymentId>,
/// The hash of the payment.
payment_hash: PaymentHash,
/// The reason why the payment failed.
Expand All @@ -62,6 +70,10 @@ pub enum Event {
},
/// A payment has been received.
PaymentReceived {
/// A local identifier used to track the payment.
///
/// Will only be `None` for events serialized with LDK Node v0.2.1 or prior.
payment_id: Option<PaymentId>,
/// The hash of the payment.
payment_hash: PaymentHash,
/// The value, in thousandths of a satoshi, that has been received.
Expand Down Expand Up @@ -110,13 +122,16 @@ impl_writeable_tlv_based_enum!(Event,
(0, PaymentSuccessful) => {
(0, payment_hash, required),
(1, fee_paid_msat, option),
(3, payment_id, option),
},
(1, PaymentFailed) => {
(0, payment_hash, required),
(1, reason, option),
(3, payment_id, option),
},
(2, PaymentReceived) => {
(0, payment_hash, required),
(1, payment_id, option),
(2, amount_msat, required),
},
(3, ChannelReady) => {
Expand Down Expand Up @@ -635,7 +650,11 @@ where
};

self.event_queue
.add_event(Event::PaymentReceived { payment_hash, amount_msat })
.add_event(Event::PaymentReceived {
payment_id: Some(payment_id),
payment_hash,
amount_msat,
})
.unwrap_or_else(|e| {
log_error!(self.logger, "Failed to push to event queue: {}", e);
panic!("Failed to push to event queue");
Expand Down Expand Up @@ -683,7 +702,11 @@ where
});

self.event_queue
.add_event(Event::PaymentSuccessful { payment_hash, fee_paid_msat })
.add_event(Event::PaymentSuccessful {
payment_id: Some(payment_id),
payment_hash,
fee_paid_msat,
})
.unwrap_or_else(|e| {
log_error!(self.logger, "Failed to push to event queue: {}", e);
panic!("Failed to push to event queue");
Expand All @@ -706,7 +729,11 @@ where
panic!("Failed to access payment store");
});
self.event_queue
.add_event(Event::PaymentFailed { payment_hash, reason })
.add_event(Event::PaymentFailed {
payment_id: Some(payment_id),
payment_hash,
reason,
})
.unwrap_or_else(|e| {
log_error!(self.logger, "Failed to push to event queue: {}", e);
panic!("Failed to push to event queue");
Expand Down
10 changes: 5 additions & 5 deletions tests/common/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -82,11 +82,11 @@ pub(crate) use expect_channel_ready_event;
macro_rules! expect_payment_received_event {
($node: expr, $amount_msat: expr) => {{
match $node.wait_next_event() {
ref e @ Event::PaymentReceived { payment_hash, amount_msat } => {
ref e @ Event::PaymentReceived { payment_id, amount_msat, .. } => {
println!("{} got event {:?}", $node.node_id(), e);
assert_eq!(amount_msat, $amount_msat);
$node.event_handled();
payment_hash
payment_id
},
ref e => {
panic!("{} got unexpected event!: {:?}", std::stringify!(node_b), e);
Expand All @@ -98,12 +98,12 @@ macro_rules! expect_payment_received_event {
pub(crate) use expect_payment_received_event;

macro_rules! expect_payment_successful_event {
($node: expr, $payment_hash: expr, $fee_paid_msat: expr) => {{
($node: expr, $payment_id: expr, $fee_paid_msat: expr) => {{
match $node.wait_next_event() {
ref e @ Event::PaymentSuccessful { payment_hash, fee_paid_msat } => {
ref e @ Event::PaymentSuccessful { payment_id, fee_paid_msat, .. } => {
println!("{} got event {:?}", $node.node_id(), e);
assert_eq!(fee_paid_msat, $fee_paid_msat);
assert_eq!(payment_hash, $payment_hash);
assert_eq!(payment_id, $payment_id);
$node.event_handled();
},
ref e => {
Expand Down
4 changes: 2 additions & 2 deletions tests/integration_tests_rust.rs
Original file line number Diff line number Diff line change
Expand Up @@ -136,9 +136,9 @@ fn multi_hop_sending() {
let invoice = nodes[4].bolt11_payment().receive(2_500_000, &"asdf", 9217).unwrap();
nodes[0].bolt11_payment().send(&invoice).unwrap();

let payment_hash = expect_payment_received_event!(&nodes[4], 2_500_000);
let payment_id = expect_payment_received_event!(&nodes[4], 2_500_000);
let fee_paid_msat = Some(2000);
expect_payment_successful_event!(nodes[0], payment_hash, fee_paid_msat);
expect_payment_successful_event!(nodes[0], payment_id, fee_paid_msat);
}

#[test]
Expand Down

0 comments on commit 1fab656

Please sign in to comment.