Skip to content

Commit

Permalink
Merge pull request #718 from ViktorTigerstrom/2024-02-allow-zero-bala…
Browse files Browse the repository at this point in the history
…nce-accounts

accounts + main: Allow new accounts with 0 balance
  • Loading branch information
guggero authored Feb 1, 2024
2 parents c03b040 + e573743 commit abf231b
Show file tree
Hide file tree
Showing 4 changed files with 24 additions and 18 deletions.
27 changes: 23 additions & 4 deletions accounts/service_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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),
}
Expand All @@ -589,7 +590,7 @@ func TestAccountService(t *testing.T) {
AddIndex: 12,
SettleIndex: 12,
Hash: testHash,
AmountPaid: 777,
AmountPaid: 1000,
State: invpkg.ContractSettled,
}

Expand All @@ -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)
})
},
}, {
Expand Down
4 changes: 0 additions & 4 deletions accounts/store.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
7 changes: 1 addition & 6 deletions accounts/store_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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())

Expand Down
4 changes: 0 additions & 4 deletions cmd/litcli/accounts.go
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down

0 comments on commit abf231b

Please sign in to comment.