From e5737436cd9fda7b926a11596aff6f28e3e3ca1c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Viktor=20Tigerstr=C3=B6m?= Date: Thu, 1 Feb 2024 11:22:08 +0200 Subject: [PATCH] accounts + main: allow new accounts with 0 balance --- accounts/service_test.go | 27 +++++++++++++++++++++++---- accounts/store.go | 4 ---- accounts/store_test.go | 7 +------ cmd/litcli/accounts.go | 4 ---- 4 files changed, 24 insertions(+), 18 deletions(-) diff --git a/accounts/service_test.go b/accounts/service_test.go index 6a7c736bb..dc169c3b8 100644 --- a/accounts/service_test.go +++ b/accounts/service_test.go @@ -571,9 +571,10 @@ func TestAccountService(t *testing.T) { acct := &OffChainBalanceAccount{ ID: testID, Type: TypeInitialBalance, - CurrentBalance: 1234, + CurrentBalance: 0, Invoices: AccountInvoices{ - testHash: {}, + testHash: {}, + testHash2: {}, }, Payments: make(AccountPayments), } @@ -589,7 +590,7 @@ func TestAccountService(t *testing.T) { AddIndex: 12, SettleIndex: 12, Hash: testHash, - AmountPaid: 777, + AmountPaid: 1000, State: invpkg.ContractSettled, } @@ -598,7 +599,25 @@ func TestAccountService(t *testing.T) { acct, err := s.store.Account(testID) require.NoError(t, err) - return acct.CurrentBalance == (1234 + 777) + return acct.CurrentBalance == 1000 + }) + + // Then settle a second invoice. + lnd.invoiceChan <- &lndclient.Invoice{ + AddIndex: 13, + SettleIndex: 13, + Hash: testHash2, + AmountPaid: 777, + State: invpkg.ContractSettled, + } + + // Ensure that the balance now adds up to the sum of + // both invoices. + assertEventually(t, func() bool { + acct, err := s.store.Account(testID) + require.NoError(t, err) + + return acct.CurrentBalance == (1000 + 777) }) }, }, { diff --git a/accounts/store.go b/accounts/store.go index 37b048907..ebaf937be 100644 --- a/accounts/store.go +++ b/accounts/store.go @@ -107,10 +107,6 @@ func (s *BoltStore) NewAccount(balance lnwire.MilliSatoshi, expirationDate time.Time, label string) (*OffChainBalanceAccount, error) { - if balance == 0 { - return nil, fmt.Errorf("a new account cannot have balance of 0") - } - // If a label is set, it must be unique, as we use it to identify the // account in some of the RPCs. It also can't be mistaken for a hex // encoded account ID to avoid confusion and make it easier for the CLI diff --git a/accounts/store_test.go b/accounts/store_test.go index 145935406..2f661febc 100644 --- a/accounts/store_test.go +++ b/accounts/store_test.go @@ -16,13 +16,8 @@ func TestAccountStore(t *testing.T) { store, err := NewBoltStore(t.TempDir(), DBFilename) require.NoError(t, err) - // An initial balance of 0 is not allowed, but later we can reach a - // zero balance. - _, err = store.NewAccount(0, time.Time{}, "") - require.ErrorContains(t, err, "cannot have balance of 0") - // Create an account that does not expire. - acct1, err := store.NewAccount(123, time.Time{}, "foo") + acct1, err := store.NewAccount(0, time.Time{}, "foo") require.NoError(t, err) require.False(t, acct1.HasExpired()) diff --git a/cmd/litcli/accounts.go b/cmd/litcli/accounts.go index 52b01959f..ab36a0cd4 100644 --- a/cmd/litcli/accounts.go +++ b/cmd/litcli/accounts.go @@ -114,10 +114,6 @@ func createAccount(ctx *cli.Context) error { args = args.Tail() } - if initialBalance <= 0 { - return fmt.Errorf("initial balance cannot be smaller than 1") - } - req := &litrpc.CreateAccountRequest{ AccountBalance: initialBalance, ExpirationDate: expirationDate,