Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add fee_paid_msat to PaymentSuccessful event #271

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 {
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@tnull I added the following macros following the pattern in the file. for some reason, the macros I added are marked as "unused" although they are used in the multihop test. what am i missing?
Also, should I split the commit for adding those two, or its ok to be in the same commit?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

"unused_macros: unused macro definition: expect_payment_successful_event"

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ah, yeah, that's because the file is included individually in all integration tests but not all will use this. No worries, you can just leave it as is and I'll clean up in a follow-up.

($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
Loading