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-4] accounts: remove last usages of UpdateAccount #941

Merged
merged 4 commits into from
Jan 23, 2025

Conversation

ellemouton
Copy link
Member

@ellemouton ellemouton commented Jan 19, 2025

With this PR, we complete the Prepare the accounts Store interface to be ready for a SQL impl & Prep unit tests to be ready to pass against a different backend steps in #917 .

We do 2 things here:

  1. we revert one of the changes from a previous PR where we change the balance type of UpdateAccountBalanceAndExpiry from an int64 to a more strict lnwire.Millisatoshi type. We revert that here though so that we can use the UpdateAccountBalanceAndExpiry in a unit test that uses UpdateAccount to set the balance to a negative value (which currently is allowed).

  2. The rest of the PR removes the last couple of usages of UpdateAccount from tests.

@ellemouton ellemouton self-assigned this Jan 19, 2025
@ellemouton ellemouton added sql-ize no-changelog This PR is does not require a release notes entry labels Jan 19, 2025
@ellemouton ellemouton force-pushed the sql4Accounts4 branch 4 times, most recently from 026cff8 to 3472b00 Compare January 21, 2025 10:58
accounts/interface.go Show resolved Hide resolved
accounts/service_test.go Show resolved Hide resolved
accounts/service_test.go Outdated Show resolved Hide resolved
accounts/service_test.go Outdated 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.

LGTM ⚡

@@ -96,8 +128,8 @@ func assertEqualAccounts(t *testing.T, expected,
actual.LastUpdate = time.Time{}

require.Equal(t, expected, actual)
require.Equal(t, expectedExpiry.UnixNano(), actualExpiry.UnixNano())
require.Equal(t, expectedUpdate.UnixNano(), actualUpdate.UnixNano())
require.Equal(t, expectedExpiry.Unix(), actualExpiry.Unix())
Copy link
Contributor

Choose a reason for hiding this comment

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

I think this one is not needed, the other one could be made stricter with the next PR where the clock is used

Copy link
Member Author

Choose a reason for hiding this comment

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

not sure what you mean by "this one" and "the other one"

Copy link
Member Author

Choose a reason for hiding this comment

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

reverted it for now. But i think we miiiight need to change it back to Unix() when we have the sqlite backend cause i dont think it does nanosecond precision. But will leave that for that PR 👍

Copy link
Contributor

Choose a reason for hiding this comment

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

not sure what you mean by "this one" and "the other one"

for this, I referred to the marked line, because the expectedExpiry had the exact same timestamp, so Equal would have still worked, just the updated timestamp had a small diff

accounts/service_test.go Show resolved Hide resolved
accounts/service_test.go Outdated Show resolved Hide resolved
accounts/service_test.go Show resolved Hide resolved
@ellemouton
Copy link
Member Author

thanks @bitromortac ! gonna put it in "ready for review" since it is acked so that i can address all review comments in 1 go :)

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.

LGTM once feedback has been addressed :)!

Comment on lines +43 to +72
// Update the balance and expiry.
err = store.UpdateAccountBalanceAndExpiry(
ctx, acct1.ID, fn.Some(int64(-500)), fn.Some(now),
)
require.NoError(t, err)

// Add 2 payments.
_, err = store.UpsertAccountPayment(
ctx, acct1.ID, lntypes.Hash{12, 34, 56, 78}, 123456,
lnrpc.Payment_FAILED,
)
require.NoError(t, err)

_, err = store.UpsertAccountPayment(
ctx, acct1.ID, lntypes.Hash{34, 56, 78, 90}, 789456123789,
lnrpc.Payment_SUCCEEDED,
)
require.NoError(t, err)

// Add 2 invoices.
err = store.AddAccountInvoice(
ctx, acct1.ID, lntypes.Hash{12, 34, 56, 78},
)
require.NoError(t, err)
err = store.AddAccountInvoice(
ctx, acct1.ID, lntypes.Hash{34, 56, 78, 90},
)
require.NoError(t, err)
Copy link
Contributor

Choose a reason for hiding this comment

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

nit: would personally put these after updating the in-memory acct1, as that makes more logical sense, and would also then change it to something like:

	for paymentHash, entry := range acct1.Payments {
		_, err = store.UpsertAccountPayment(
			ctx, acct1.ID, paymentHash, entry.FullAmount,
			entry.Status,
		)
		require.NoError(t, err)
	}

	for paymentHash := range acct1.Invoices {
		err = store.AddAccountInvoice(ctx, acct1.ID, paymentHash)
		require.NoError(t, err)
	}

To not have to repeat hashes and amounts.

Copy link
Member Author

Choose a reason for hiding this comment

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

To not have to repeat hashes and amounts.

I kind of like keeping tests very explicit. Else a bug in one version can be transferred to the other version.

For example, if you are testing an func Add(x, y int) int function, then one should test explicitly like:

x := 1
y := 2
expected := 3
result := Add(x, y)
require.Equal(3, result)

instead of:

x := 1
y := 2
expected := x+y
result := Add(x, y)
require.Equal(expected, result)

In our mission to replace UpdateAccount, we need
UpdatAccountBalanceAndExpiry to take a negative balance so that we can
use it to replace the behaviour of UpdateAccount as it stands today.
@ellemouton ellemouton force-pushed the sql4Accounts4 branch 2 times, most recently from cf15ebd to 9e45401 Compare January 23, 2025 07:16
Remove the call to UpdateAccount from the TestAccountStore test and
instead replace it with all the other calls we have added.
We remove the last few calls to UpdateAccount from the
TestAccountService test. Previously, the UpdateAccount call was used in
this test to also _insert_ new accounts and to force the account ID to
be set to specific values. So here we need to instead call the store's
NewAccount method to insert a new account, then we get the AccountID
from the returned value and we need to then use this returned ID (which
is no longer forceable) in the remainder of the tests.
In the case where no startup error is expected, we should assert that no
error is returned from Start.
@ellemouton ellemouton merged commit 91bc912 into lightninglabs:master Jan 23, 2025
13 checks passed
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