@@ -2,6 +2,7 @@ package accounts
2
2
3
3
import (
4
4
"context"
5
+ "github.com/lightningnetwork/lnd/lnwire"
5
6
"testing"
6
7
"time"
7
8
@@ -71,6 +72,14 @@ func TestAccountStore(t *testing.T) {
71
72
)
72
73
require .NoError (t , err )
73
74
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
+
74
83
// Update the in-memory account so that we can compare it with the
75
84
// account we get from the store.
76
85
acct1 .CurrentBalance = - 500
@@ -85,11 +94,30 @@ func TestAccountStore(t *testing.T) {
85
94
}
86
95
acct1 .Invoices [lntypes.Hash {12 , 34 , 56 , 78 }] = struct {}{}
87
96
acct1 .Invoices [lntypes.Hash {34 , 56 , 78 , 90 }] = struct {}{}
97
+ acct1 .CurrentBalance += 10000
98
+ acct1 .CurrentBalance -= 5000
88
99
89
100
dbAccount , err = store .Account (ctx , acct1 .ID )
90
101
require .NoError (t , err )
91
102
assertEqualAccounts (t , acct1 , dbAccount )
92
103
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
+
93
121
// Sleep just a tiny bit to make sure we are never too quick to measure
94
122
// the expiry, even though the time is nanosecond scale and writing to
95
123
// the store and reading again should take at least a couple of
@@ -262,12 +290,12 @@ func TestAccountUpdateMethods(t *testing.T) {
262
290
assertInvoices (hash1 , hash2 )
263
291
})
264
292
265
- t .Run ("IncreaseAccountBalance " , func (t * testing.T ) {
293
+ t .Run ("CreditAccount " , func (t * testing.T ) {
266
294
store := NewTestDB (t , clock .NewTestClock (time .Now ()))
267
295
268
296
// Increasing the balance of an account that doesn't exist
269
297
// should error out.
270
- err := store .IncreaseAccountBalance (ctx , AccountID {}, 100 )
298
+ err := store .CreditAccount (ctx , AccountID {}, 100 )
271
299
require .ErrorIs (t , err , ErrAccNotFound )
272
300
273
301
acct , err := store .NewAccount (ctx , 123 , time.Time {}, "foo" )
@@ -284,7 +312,7 @@ func TestAccountUpdateMethods(t *testing.T) {
284
312
285
313
// Increase the balance by 100 and assert that the new balance
286
314
// is 223.
287
- err = store .IncreaseAccountBalance (ctx , acct .ID , 100 )
315
+ err = store .CreditAccount (ctx , acct .ID , 100 )
288
316
require .NoError (t , err )
289
317
290
318
assertBalance (223 )
0 commit comments