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

Persist pending payment amount #623

Merged
merged 1 commit into from
Nov 22, 2023

Conversation

dleutenegger
Copy link
Contributor

Resolves #599

Builds upon #621

Copy link
Collaborator

@dangeross dangeross left a comment

Choose a reason for hiding this comment

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

Looking good, just need to change how the attempted_amount_msat is persisted

id: invoice.payment_hash.clone(),
payment_type: PaymentType::Sent,
payment_time: SystemTime::now().duration_since(UNIX_EPOCH)?.as_secs() as i64,
amount_msat,
Copy link
Collaborator

Choose a reason for hiding this comment

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

This can be set to 0 and instead call persister.insert_payment_external_info() to store the attempted_amount_msat in the payment_external_info sync table.

Suggested change
amount_msat,
amount_msat: 0,

Copy link
Member

@roeierez roeierez Nov 17, 2023

Choose a reason for hiding this comment

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

This can be set to 0 and instead call persister.insert_payment_external_info() to store the attempted_amount_msat in the payment_external_info sync table.

@dangeross why do we need to sync the payment_external_info with pending payment?
I think of pending payments as transient by nature.

Copy link
Collaborator

Choose a reason for hiding this comment

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

Maybe I misunderstood the PR, but it looks like the intention is to store the pending amount in attempted_amount_msat. @dleutenegger ?

Copy link
Member

Choose a reason for hiding this comment

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

@dangeross either this or the migration was added by mistake because I don't see any update to the sync table.
Let's wait for @dleutenegger answer.

Copy link
Contributor Author

@dleutenegger dleutenegger Nov 17, 2023

Choose a reason for hiding this comment

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

@roeierez yes the intention is to persist the attempted_amount_msat. But as @dangeross pointed out here, the actual call to persist just got missed in the persist_new_bolt11_payment_attempt-method

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Addressed in 49e5938

},
},
}])?;
Ok(())
Copy link
Collaborator

Choose a reason for hiding this comment

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

Suggested change
Ok(())
self.persister.insert_payment_external_info(
&invoice.payment_hash,
None,
None,
None,
None,
amount_msat,
)?;
Ok(())

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Addressed in 49e5938

@@ -531,5 +531,6 @@ pub(crate) fn current_sync_migrations() -> Vec<&'static str> {
END;
",
"ALTER TABLE payments_external_info ADD COLUMN failed_amount_msat INTEGER;",
"ALTER TABLE payments_external_info RENAME COLUMN failed_amount_msat TO attempted_amount_msat;",
Copy link
Collaborator

Choose a reason for hiding this comment

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

Here @roeierez

Copy link
Member

Choose a reason for hiding this comment

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

Yes I saw that but I assumed it is a mistake since I don't see any inserts/updates to this table as part of this PR.

@dleutenegger dleutenegger force-pushed the fix/pending-payment-amount branch 3 times, most recently from c7942fd to 638ff60 Compare November 17, 2023 10:58
@dleutenegger dleutenegger force-pushed the fix/pending-payment-amount branch from 638ff60 to b700555 Compare November 17, 2023 11:49
},
}])?;

self.persister.insert_payment_external_info(
Copy link
Member

Choose a reason for hiding this comment

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

Why do we need to add the amount to the sync db? Is there a reason to sync pending payments?

Copy link
Contributor Author

@dleutenegger dleutenegger Nov 17, 2023

Choose a reason for hiding this comment

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

Because the attempted_amount_msat is also used for failed open invoices to display the actual amount that was attempted to be paid.
And I assume, although not a 100% sure, that other signers would be able to sync the pending payment from Greenlight as well and thus need the data to display the accurate amount.

Copy link
Member

Choose a reason for hiding this comment

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

But we already handled failed opened invoices. Why persist for every invoice? For success payments the amount is retrieved correctly.
I see disadvantage for this way of sync where the synced data become larger without any real advantage.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I see your point, but if we don't sync the attempted amount we couldn't display the actual intended amount for pending payments on all remote signers, as amount_msat is only the amount delivered to destination (we could display it on the sending signer if we would just store it locally), which seems problematic to me as this would result it inconsistencies.
The other option I see would be to clean up the synced entry after payment success.

I would like to get your input on this

Copy link
Member

Choose a reason for hiding this comment

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

I see your point, but if we don't sync the attempted amount we couldn't display the actual intended amount for pending payments on all remote signers, as amount_msat is only the amount delivered to destination (we could display it on the sending signer if we would just store it locally), which seems problematic to me as this would result it inconsistencies. The other option I see would be to clean up the synced entry after payment success.

I would like to get your input on this

The signer is synced with the channel state from greenlight not using our own sync db mechanism to it is irrelevant to the signer sync.
The sdk itself using the sync db to synchronize but it doesn't do that in realtime and we don't have a way to notify remote sdk instances to sync so I don't see how adding it to the sync db contributes here.
In addition immediately after the send_payment is called remote sdk that will sync (actually call list_pays behind the scene) will receive the pending payment as pending so that will should your concern as well.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Do I understand this correctly; Other signers might get the pending payment (list_pays) before they sync the remote DB?

Copy link
Member

Choose a reason for hiding this comment

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

Do I understand this correctly; Other signers might get the pending payment (list_pays) before they sync the remote DB?

In short Yes, exactly.
Let me elaborate a bit on the way we sync data across sdk instances which should make it even more clear:
there are two ways we synchronize:

  1. Sync remote data that exists on greenlight (payments, invoices, channels, etc...) this data comes from greenlight and is fetched by simple calling greenlight gRpc API. All of it is encapsulated inside the sync method.
  2. Sync remote data that doesn't exist in greenlight and only the sdk "knows" about it (lnurl metadata, swaps, etc...) for this kind of data we use the sync.db and whenever such data changes locally the sdk ensures it is uploaded to the greenlight storage (using internal backup API) so other sdk instances can pull it.

The pending payments are related to the first kind of sync (greenlight data) so in theory we don't need to do anything about it as if one sdk instance pays, other instances will receive it as soon as they call sync.
Your requirement was a local requirement as far as I understand, to be able to see the pending payment immediately after calling send_payment. For that purpose only adding locally is needed and no need to add it to the sync.db as well.
Hope that makes sense.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

We came up with the following solution proposal:
Adding a new table similar to the payments_external_info-table but stored locally, which stores the attempted_msat amount. Which will be cleaned up once the payment failed/succeeded.

Storing it within Payments doesn't seem to work from our perspective as pending payments get deleted and replaced once a sync occurs.

WDYT @roeierez

@dleutenegger dleutenegger force-pushed the fix/pending-payment-amount branch 2 times, most recently from 8b6562b to 2b5e43c Compare November 21, 2023 10:07
@roeierez
Copy link
Member

@dleutenegger could you please rebase it on top of master, currently there are some conflicts.

@dleutenegger dleutenegger force-pushed the fix/pending-payment-amount branch from 2b5e43c to 9367191 Compare November 21, 2023 15:01
Copy link
Member

@roeierez roeierez left a comment

Choose a reason for hiding this comment

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

LGTM

@roeierez
Copy link
Member

Will wait for @dangeross to finish his review before merging.

Copy link
Collaborator

@dangeross dangeross left a comment

Choose a reason for hiding this comment

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

Looks good!

@roeierez roeierez merged commit 5fc10f3 into breez:main Nov 22, 2023
7 checks passed
@dleutenegger dleutenegger deleted the fix/pending-payment-amount branch November 22, 2023 15:13
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.

Bug: pending outgoing payment of open bolt11 invoice returns amount inferior to the one attempted to be sent
3 participants