@@ -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
0 commit comments