@@ -2,6 +2,7 @@ package accounts
22
33import (
44 "context"
5+ "github.com/lightningnetwork/lnd/lnwire"
56 "testing"
67 "time"
78
@@ -71,6 +72,14 @@ func TestAccountStore(t *testing.T) {
7172 )
7273 require .NoError (t , err )
7374
75+ // Adjust the account balance by first crediting 10000, and then
76+ // debiting 5000.
77+ err = store .CreditAccount (ctx , acct1 .ID , lnwire .MilliSatoshi (10000 ))
78+ require .NoError (t , err )
79+
80+ err = store .DebitAccount (ctx , acct1 .ID , lnwire .MilliSatoshi (5000 ))
81+ require .NoError (t , err )
82+
7483 // Update the in-memory account so that we can compare it with the
7584 // account we get from the store.
7685 acct1 .CurrentBalance = - 500
@@ -85,11 +94,30 @@ func TestAccountStore(t *testing.T) {
8594 }
8695 acct1 .Invoices [lntypes.Hash {12 , 34 , 56 , 78 }] = struct {}{}
8796 acct1 .Invoices [lntypes.Hash {34 , 56 , 78 , 90 }] = struct {}{}
97+ acct1 .CurrentBalance += 10000
98+ acct1 .CurrentBalance -= 5000
8899
89100 dbAccount , err = store .Account (ctx , acct1 .ID )
90101 require .NoError (t , err )
91102 assertEqualAccounts (t , acct1 , dbAccount )
92103
104+ // Test that adjusting the balance to exactly 0 should work, while
105+ // adjusting the balance to below 0 should fail.
106+ err = store .DebitAccount (
107+ ctx , acct1 .ID , lnwire .MilliSatoshi (acct1 .CurrentBalance ),
108+ )
109+ require .NoError (t , err )
110+
111+ acct1 .CurrentBalance = 0
112+
113+ dbAccount , err = store .Account (ctx , acct1 .ID )
114+ require .NoError (t , err )
115+ assertEqualAccounts (t , acct1 , dbAccount )
116+
117+ // Adjusting the value to below 0 should fail.
118+ err = store .DebitAccount (ctx , acct1 .ID , lnwire .MilliSatoshi (1 ))
119+ require .ErrorContains (t , err , "balance would be below 0" )
120+
93121 // Sleep just a tiny bit to make sure we are never too quick to measure
94122 // the expiry, even though the time is nanosecond scale and writing to
95123 // the store and reading again should take at least a couple of
@@ -262,12 +290,12 @@ func TestAccountUpdateMethods(t *testing.T) {
262290 assertInvoices (hash1 , hash2 )
263291 })
264292
265- t .Run ("IncreaseAccountBalance " , func (t * testing.T ) {
293+ t .Run ("CreditAccount " , func (t * testing.T ) {
266294 store := NewTestDB (t , clock .NewTestClock (time .Now ()))
267295
268296 // Increasing the balance of an account that doesn't exist
269297 // should error out.
270- err := store .IncreaseAccountBalance (ctx , AccountID {}, 100 )
298+ err := store .CreditAccount (ctx , AccountID {}, 100 )
271299 require .ErrorIs (t , err , ErrAccNotFound )
272300
273301 acct , err := store .NewAccount (ctx , 123 , time.Time {}, "foo" )
@@ -284,7 +312,7 @@ func TestAccountUpdateMethods(t *testing.T) {
284312
285313 // Increase the balance by 100 and assert that the new balance
286314 // is 223.
287- err = store .IncreaseAccountBalance (ctx , acct .ID , 100 )
315+ err = store .CreditAccount (ctx , acct .ID , 100 )
288316 require .NoError (t , err )
289317
290318 assertBalance (223 )
0 commit comments