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

Fix amount calculation and track paid fees in PaymentKind::Onchain #468

Merged
merged 2 commits into from
Feb 22, 2025

Conversation

tnull
Copy link
Collaborator

@tnull tnull commented Feb 12, 2025

We recently started tracking onchain payments in the store. It turns out the calculated amount was slightly off as in the outbound case it would include the paid fees. Here, we fix this minor bug and start tracking the fees separately.

@tnull tnull added this to the 0.5 milestone Feb 12, 2025
@tnull tnull requested a review from jkczyz February 12, 2025 14:27
Comment on lines 305 to 306
/// The on-chain fees that were paid for this payment.
fee_msat: u64,
Copy link
Contributor

Choose a reason for hiding this comment

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

Any reason we track fees for on-chain payments but not for lightning payments? I suppose we could for outbound lightning payments, but IIUC we can't know for inbound payments.

Also, should we use sats here instead of msats? Or is this to align with msats used in PaymentDetails?

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Any reason we track fees for on-chain payments but not for lightning payments? I suppose we could for outbound lightning payments, but IIUC we can't know for inbound payments.

Hmm, you mean adding this as an optional field one layer above at PaymentDetails directly? I guess we could, but note that the field will only get updated for outbound payments once they succeeded. Now pushed a fixup in that direction, let me know if you find that preferable.

Also, should we use sats here instead of msats? Or is this to align with msats used in PaymentDetails?

Yes, originally it was to align the units, but if we don't make it a PaymentDetails field we could consider switching to sat I guess.

Copy link
Contributor

Choose a reason for hiding this comment

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

LGTM. Seems it would be useful to include.

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Alright. Should I squash then? Will try to separate the amount calculation fix into a dedicated commit.

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Went ahead and did so.

@tnull tnull force-pushed the 2025-02-add-fee-field-to-onchain branch from 68bf90c to 5cf578b Compare February 13, 2025 13:23
We recently started tracking onchain payments in the store. It turns out
the calculated amount was slightly off as in the outbound case it would
include the paid fees. Here, we fix this minor bug.
@tnull tnull force-pushed the 2025-02-add-fee-field-to-onchain branch 2 times, most recently from e661887 to f429ee9 Compare February 13, 2025 14:11
@tnull tnull force-pushed the 2025-02-add-fee-field-to-onchain branch from f429ee9 to ea2ffd3 Compare February 14, 2025 09:06
@tnull
Copy link
Collaborator Author

tnull commented Feb 14, 2025

Force-pushed with the following changes:

> git diff-tree -U2 f429ee99 b4df52ce
diff --git a/tests/integration_tests_rust.rs b/tests/integration_tests_rust.rs
index b1d95dc1..19e7806d 100644
--- a/tests/integration_tests_rust.rs
+++ b/tests/integration_tests_rust.rs
@@ -357,7 +357,32 @@ fn onchain_send_receive() {
        let txid =
                node_b.onchain_payment().send_to_address(&addr_a, amount_to_send_sats, None).unwrap();
-       generate_blocks_and_wait(&bitcoind.client, &electrsd.client, 6);
        wait_for_tx(&electrsd.client, txid);
+       node_a.sync_wallets().unwrap();
+       node_b.sync_wallets().unwrap();
+
+       let payment_id = PaymentId(txid.to_byte_array());
+       let payment_a = node_a.payment(&payment_id).unwrap();
+       assert_eq!(payment_a.status, PaymentStatus::Pending);
+       match payment_a.kind {
+               PaymentKind::Onchain { status, .. } => {
+                       assert!(matches!(status, ConfirmationStatus::Unconfirmed));
+               },
+               _ => panic!("Unexpected payment kind"),
+       }
+       assert!(payment_a.fee_paid_msat > Some(0));
+       let payment_b = node_b.payment(&payment_id).unwrap();
+       assert_eq!(payment_b.status, PaymentStatus::Pending);
+       match payment_a.kind {
+               PaymentKind::Onchain { status, .. } => {
+                       assert!(matches!(status, ConfirmationStatus::Unconfirmed));
+               },
+               _ => panic!("Unexpected payment kind"),
+       }
+       assert!(payment_b.fee_paid_msat > Some(0));
+       assert_eq!(payment_a.amount_msat, Some(amount_to_send_sats * 1000));
+       assert_eq!(payment_a.amount_msat, payment_b.amount_msat);
+       assert_eq!(payment_a.fee_paid_msat, payment_b.fee_paid_msat);

+       generate_blocks_and_wait(&bitcoind.client, &electrsd.client, 6);
        node_a.sync_wallets().unwrap();
        node_b.sync_wallets().unwrap();
@@ -377,8 +402,5 @@ fn onchain_send_receive() {
        assert_eq!(node_b_payments.len(), 3);

-       let payment_id = PaymentId(txid.to_byte_array());
        let payment_a = node_a.payment(&payment_id).unwrap();
-       assert_eq!(payment_a.amount_msat, Some(amount_to_send_sats * 1000));
-       assert!(payment_a.fee_paid_msat > Some(0));
        match payment_a.kind {
                PaymentKind::Onchain { txid: _txid, status } => {
@@ -390,6 +412,4 @@ fn onchain_send_receive() {

        let payment_b = node_a.payment(&payment_id).unwrap();
-       assert_eq!(payment_b.amount_msat, Some(amount_to_send_sats * 1000));
-       assert!(payment_b.fee_paid_msat > Some(0));
        match payment_b.kind {
                PaymentKind::Onchain { txid: _txid, status } => {

@tnull tnull force-pushed the 2025-02-add-fee-field-to-onchain branch from ea2ffd3 to b4df52c Compare February 14, 2025 09:12
jkczyz
jkczyz previously approved these changes Feb 14, 2025
Comment on lines +151 to +156
let payment = $node.payment(&$payment_id.unwrap()).unwrap();
assert_eq!(payment.fee_paid_msat, fee_paid_msat);
Copy link
Contributor

Choose a reason for hiding this comment

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

Could you assert it is None in expect_payment_received_event?

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Hmm, as mentioned above, it won't be None for onchain payments? Do you think this is confusing? Should we hard-code it to be None in the inbound case, even though we know the transactions fees there, too (in contrast to Lightning)?

Copy link
Contributor

Choose a reason for hiding this comment

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

Ah, sorry. I think that I assumed expect_payment_successful_event was only used for lightning payments. We could condition the assertion on the PaymentKind not being on-chain, maybe?

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Alright, now amended with the following changes:

> git diff-tree -U2  b4df52ce 0b7af80a
diff --git a/tests/common/mod.rs b/tests/common/mod.rs
index ab0aa7c1..2ad73682 100644
--- a/tests/common/mod.rs
+++ b/tests/common/mod.rs
@@ -104,4 +104,8 @@ macro_rules! expect_payment_received_event {
                                println!("{} got event {:?}", $node.node_id(), e);
                                assert_eq!(amount_msat, $amount_msat);
+                               let payment = $node.payment(&payment_id.unwrap()).unwrap();
+                               if !matches!(payment.kind, PaymentKind::Onchain { .. }) {
+                                       assert_eq!(payment.fee_paid_msat, None);
+                               }
                                $node.event_handled();
                                payment_id

Since we split-out the paid fees anways, we optionally start tracking them in
`PaymentDetails` for all payment types.
@tnull tnull force-pushed the 2025-02-add-fee-field-to-onchain branch from b4df52c to 0b7af80 Compare February 21, 2025 09:18
@tnull tnull merged commit 116fb63 into lightningdevkit:main Feb 22, 2025
14 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants