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

improve payment sync #1099

Merged
merged 10 commits into from
Oct 11, 2024
Merged

improve payment sync #1099

merged 10 commits into from
Oct 11, 2024

Conversation

JssDWt
Copy link
Contributor

@JssDWt JssDWt commented Oct 7, 2024

  • make payment sync call all invoice/send_pay endpoints simultaneously.
  • fix missing amounts in payments by taking amount from the invoice like previous
  • fix wrong description for payments, now take the invoice description
  • fix issue where pending payments were wrongfully removed from the db, removed the pending payment deletion. They should be updated by sync automatically.

Hopefully fixes breez/c-breez#909

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

)?;
let mut send_pays = Vec::new();
for hash in hashes {
for hash_group in hash_groups {
Copy link
Member

Choose a reason for hiding this comment

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

I think we can use IN filter expression to get all these in one shot but this is also good for now. Only the first will probably end with lots of queries.

})
.await?
.into_inner();
let updated_invoices = updated_invoices_res?.into_inner();
if let Some(last) = updated_invoices.invoices.last() {
new_sync_state.list_invoices_index.updated = last.created_index()
Copy link
Collaborator

Choose a reason for hiding this comment

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

Looking at the CLN API for listinvoices there is also an updated_index. Just want to confirm created_index is correct.

}
let updated_send_pays = updated_send_pays_res?.into_inner();
if let Some(last) = updated_send_pays.payments.last() {
new_sync_state.send_pays_index.updated = last.created_index()
Copy link
Collaborator

Choose a reason for hiding this comment

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

Same here with the updated_index/created_index

0 => Ok(Self::Pending),
1 => Ok(Self::Failed),
2 => Ok(Self::Complete),
_ => Err(NodeError::generic("invalid send_pay status")),
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
_ => Err(NodeError::generic("invalid send_pay status")),
_ => Err(NodeError::generic("Invalid send_pay status")),

Ok(SendPay {
created_index: value
.created_index
.ok_or(NodeError::generic("missing created index"))?,
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_or(NodeError::generic("missing created index"))?,
.ok_or(NodeError::generic("Missing created index"))?,

Comment on lines 2091 to 2120
let status = if value.state & 2 > 0 {
PaymentStatus::Complete
} else if value.state & 1 > 0 {
PaymentStatus::Pending
} else {
PaymentStatus::Failed
};
Copy link
Contributor

@hydra-yse hydra-yse Oct 10, 2024

Choose a reason for hiding this comment

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

I'm not sure this works. From https://github.com/breez/breez-sdk-greenlight/pull/1095/files#diff-7466a53546c6cded460ef5502a9168b13cc930d94fdf7d0ea8166bb10c75fe7eR843-R858, here are the cases:

  • send_pay.status == Pending (0) => SendPayAgg.state = 0 + 1 = 1
  • send_pay.status == Complete (1) => SendPayAgg.state = 0 + 2 = 2
  • send_pay.status == Failed (2) => SendPayAgg.state = 0 + 4 = 4

When we go ahead and decode the state, we get:

  • SendPayAgg.state == 1 => 1 & 2 (=0), 1 & 1 = 1 => Pending OK
  • SendPayAgg.state == 2 => 2 & 2 (=2) => Complete OK
  • SendPayAgg.state == 4 => 4 & 2 (=0), 4 & 1 (=0) => Failed OK

Edit: Double-checked. Everything seems fine 👍

@JssDWt JssDWt force-pushed the jssdwt-improve-payment-sync branch from c453a72 to 47548b7 Compare October 11, 2024 09:39
@JssDWt
Copy link
Contributor Author

JssDWt commented Oct 11, 2024

Discussed with @hydra-yse the amount was not initialized, that's why the amounts were 0. Thanks for catching that @hydra-yse!

@JssDWt JssDWt force-pushed the jssdwt-improve-payment-sync branch from 417fbf9 to 185df2f Compare October 11, 2024 12:11
- request all payments and invoices at the same time
- only query used payment groups from the db
Now sync only fetches the latest payments, don't delete pending payments
from the database. They will be updated by payment syc automagically.
This fixes races between fetching updated and created.

There was also a bug where the 'updated' index would be set to the 'created' index accidentally.
@JssDWt JssDWt force-pushed the jssdwt-improve-payment-sync branch from 185df2f to 2d01527 Compare October 11, 2024 12:29
@JssDWt JssDWt merged commit 2d01527 into main Oct 11, 2024
9 checks passed
@JssDWt JssDWt deleted the jssdwt-improve-payment-sync branch October 14, 2024 10:43
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.

The amounts for sending are not displayed
4 participants