Skip to content

Commit

Permalink
Add fee_paid_msat to PaymentSuccessful event (#271)
Browse files Browse the repository at this point in the history
The fee as returned from `PaymentSent` event generated by LDK and is
  saved in `PaymentSuccessful` event.
  • Loading branch information
jbesraa authored Mar 7, 2024
1 parent fcb85b5 commit 02f0000
Show file tree
Hide file tree
Showing 4 changed files with 49 additions and 5 deletions.
2 changes: 1 addition & 1 deletion bindings/ldk_node.udl
Original file line number Diff line number Diff line change
Expand Up @@ -154,7 +154,7 @@ enum BuildError {

[Enum]
interface Event {
PaymentSuccessful(PaymentHash payment_hash);
PaymentSuccessful(PaymentHash payment_hash, u64? fee_paid_msat);
PaymentFailed(PaymentHash payment_hash, PaymentFailureReason? reason);
PaymentReceived(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);
Expand Down
5 changes: 4 additions & 1 deletion src/event.rs
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,8 @@ pub enum Event {
PaymentSuccessful {
/// 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 {
Expand Down Expand Up @@ -106,6 +108,7 @@ pub enum Event {
impl_writeable_tlv_based_enum!(Event,
(0, PaymentSuccessful) => {
(0, payment_hash, required),
(1, fee_paid_msat, option),
},
(1, PaymentFailed) => {
(0, payment_hash, required),
Expand Down Expand Up @@ -611,7 +614,7 @@ where
);
}
self.event_queue
.add_event(Event::PaymentSuccessful { payment_hash })
.add_event(Event::PaymentSuccessful { 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 Down
37 changes: 37 additions & 0 deletions tests/common.rs
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,43 @@ macro_rules! expect_channel_ready_event {

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 } => {
println!("{} got event {:?}", $node.node_id(), e);
assert_eq!(amount_msat, $amount_msat);
$node.event_handled();
let result = Ok(payment_hash);
result
},
ref e => {
panic!("{} got unexpected event!: {:?}", std::stringify!(node_b), e);
},
}
}};
}

pub(crate) use expect_payment_received_event;

macro_rules! expect_payment_successful_event {
($node: expr, $payment_hash: expr, $fee_paid_msat: expr) => {{
match $node.wait_next_event() {
ref e @ Event::PaymentSuccessful { payment_hash, fee_paid_msat } => {
println!("{} got event {:?}", $node.node_id(), e);
assert_eq!(fee_paid_msat, $fee_paid_msat);
assert_eq!(payment_hash, $payment_hash);
$node.event_handled();
},
ref e => {
panic!("{} got unexpected event!: {:?}", std::stringify!(node_b), e);
},
}
}};
}

pub(crate) use expect_payment_successful_event;

pub(crate) fn setup_bitcoind_and_electrsd() -> (BitcoinD, ElectrsD) {
let bitcoind_exe =
env::var("BITCOIND_EXE").ok().or_else(|| bitcoind::downloaded_exe_path().ok()).expect(
Expand Down
10 changes: 7 additions & 3 deletions tests/integration_tests_rust.rs
Original file line number Diff line number Diff line change
@@ -1,14 +1,16 @@
mod common;

use common::{
do_channel_full_cycle, expect_event, generate_blocks_and_wait, open_channel,
do_channel_full_cycle, expect_event, expect_payment_received_event,
expect_payment_successful_event, generate_blocks_and_wait, open_channel,
premine_and_distribute_funds, random_config, setup_bitcoind_and_electrsd, setup_builder,
setup_node, setup_two_nodes, wait_for_tx, TestSyncStore,
};

use ldk_node::{Builder, Event, NodeError};

use bitcoin::{Amount, Network};
use lightning::ln::PaymentHash;

use std::sync::Arc;

Expand Down Expand Up @@ -133,8 +135,10 @@ fn multi_hop_sending() {
let invoice = nodes[4].receive_payment(2_500_000, &"asdf", 9217).unwrap();
nodes[0].send_payment(&invoice).unwrap();

expect_event!(nodes[4], PaymentReceived);
expect_event!(nodes[0], PaymentSuccessful);
let payment_hash: Result<PaymentHash, ()> =
expect_payment_received_event!(&nodes[4], 2_500_000);
let fee_paid_msat = Some(2000);
expect_payment_successful_event!(nodes[0], payment_hash.unwrap(), fee_paid_msat);
}

#[test]
Expand Down

0 comments on commit 02f0000

Please sign in to comment.