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

[sql-2] accounts: start replacing calls to UpdateAccount #938

Merged
merged 4 commits into from
Jan 17, 2025

Conversation

ellemouton
Copy link
Member

@ellemouton ellemouton commented Jan 16, 2025

To prepare the accounts.Store to be more SQL compatible, we will want to replace the current
UpdateAccount method. Today it takes a full OffChainAccount struct, serialises it and replaces
any existing in the DB. This method is not very SQL appropriate as the better pattern is to instead
have specific update methods that read: "Update field X and account with ID Y".

So this PR starts this process of adding more SQL friendly methods to the interface and thereby
phasing out the use of UpdateAccount. By doing so, we also move very kvdb specific logic out
of the accounts.InterceptorService.

I'm splitting this up over about 3 or 4 PRs since some of the replacements are more complicated than
others.

This PR focuses on just replacing the simple ones:

  • UpdateAccountBalanceAndExpiry
  • AddAccountInvoice
  • IncreaseAccountBalance

accounts/service.go Show resolved Hide resolved
accounts/store_test.go Outdated Show resolved Hide resolved
accounts/store_kvdb.go Outdated Show resolved Hide resolved
accounts/store_test.go Show resolved Hide resolved
accounts/interface.go Outdated Show resolved Hide resolved
accounts/service.go Outdated Show resolved Hide resolved
accounts/interface.go Show resolved Hide resolved
Copy link
Contributor

@bitromortac bitromortac left a comment

Choose a reason for hiding this comment

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

Changes LGTM, the itest failure seems unrelated, not sure if this has been seen before.

@ellemouton
Copy link
Member Author

ellemouton commented Jan 17, 2025

thanks @bitromortac , yeah this one has popped up a few times. I've made an issue for it now: #940

i'll still re-kick the job though (after next review) so that we can have a green here before merge

Copy link
Contributor

@ViktorTigerstrom ViktorTigerstrom 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 🚀!

Agree with Tobias on idea of converting optional types one layer higher, so great to hear that there'll be a new PR for that :)

Also for clarification I've tested just for sanity sake that the update to use btcutil.Amount in the accounts UpdateAccount method doesn't affect the current cli method behaviour (as the actual rpc servers functions aren't covered by tests).

Comment on lines 442 to 444
s.invoiceToAccount[hash] = id

return s.store.UpdateAccount(ctx, account)
return s.store.AddAccountInvoice(ctx, id, hash)
Copy link
Contributor

Choose a reason for hiding this comment

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

Just noting that this changes the behaviour from the previous code, where we will now assign s.invoiceToAccount[hash] the id even if an account for that id doesn't exist in the db.
So if users are were to manipulate the account id with the context to an unknown id, that would occur.

I do think this should be fine even if that would somehow happen, despite the "memory leak" with the added entry to the map, as it must represent a real invoice which has added a lot of memory allocation either way, and the user wouldn't get credited if it was paid.

Copy link
Member Author

Choose a reason for hiding this comment

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

good catch! I've updated it to only assign to the in-mem map if the DB call succeeded. lemme know what you think


// Now, update just the balance of the account.
newBalance := lnwire.MilliSatoshi(123)
err = store.UpdateAccountBalanceAndExpiry(
Copy link
Contributor

Choose a reason for hiding this comment

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

This applies to all tests:
I think we should also test how these db functions behave if an unknown id is passed into the functions.

Copy link
Member Author

Choose a reason for hiding this comment

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

added

In this commit, we remove one call to the UpdateAccount store method and
replace it with a call to a new UpdateAccountBalanceAndExpiry method
which updates an accounts balance and/or expiry fields and finds the
account via the given ID. This method signature is more appropriate for
a SQL backend than the UpdateAccount method.
And use it instead of UpdateAccount for the InterceptorService's
AssociateInvoice method.
And use it instead of UpdateAccount in the InterceptorService's
invoiceUpdate method.
... to take a more strict btcutil.Amount type for the account balance
parameter.
Copy link
Member Author

@ellemouton ellemouton left a comment

Choose a reason for hiding this comment

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

thanks @ViktorTigerstrom 🙏

Comment on lines 442 to 444
s.invoiceToAccount[hash] = id

return s.store.UpdateAccount(ctx, account)
return s.store.AddAccountInvoice(ctx, id, hash)
Copy link
Member Author

Choose a reason for hiding this comment

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

good catch! I've updated it to only assign to the in-mem map if the DB call succeeded. lemme know what you think

@ellemouton ellemouton merged commit edaf59d into lightninglabs:master Jan 17, 2025
13 checks passed
@ellemouton ellemouton deleted the sql2Accounts2 branch January 17, 2025 14:30
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
no-changelog This PR is does not require a release notes entry sql-ize
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants