@@ -2,6 +2,7 @@ package accounts
22
33import (
44 "context"
5+ "github.com/lightningnetwork/lnd/lnwire"
56 "testing"
67 "time"
78
@@ -71,6 +72,18 @@ func TestAccountStore(t *testing.T) {
7172 )
7273 require .NoError (t , err )
7374
75+ // Adjust the account balance by first adding 10000, and then deducting
76+ // 5000.
77+ err = store .AdjustAccountBalance (
78+ ctx , acct1 .ID , lnwire .MilliSatoshi (10000 ), true ,
79+ )
80+ require .NoError (t , err )
81+
82+ err = store .AdjustAccountBalance (
83+ ctx , acct1 .ID , lnwire .MilliSatoshi (5000 ), false ,
84+ )
85+ require .NoError (t , err )
86+
7487 // Update the in-memory account so that we can compare it with the
7588 // account we get from the store.
7689 acct1 .CurrentBalance = - 500
@@ -85,11 +98,32 @@ func TestAccountStore(t *testing.T) {
8598 }
8699 acct1 .Invoices [lntypes.Hash {12 , 34 , 56 , 78 }] = struct {}{}
87100 acct1 .Invoices [lntypes.Hash {34 , 56 , 78 , 90 }] = struct {}{}
101+ acct1 .CurrentBalance += 10000
102+ acct1 .CurrentBalance -= 5000
103+
104+ dbAccount , err = store .Account (ctx , acct1 .ID )
105+ require .NoError (t , err )
106+ assertEqualAccounts (t , acct1 , dbAccount )
107+
108+ // Test that adjusting the balance to exactly 0 should work, while
109+ // adjusting the balance to below 0 should fail.
110+ err = store .AdjustAccountBalance (
111+ ctx , acct1 .ID , lnwire .MilliSatoshi (acct1 .CurrentBalance ), false ,
112+ )
113+ require .NoError (t , err )
114+
115+ acct1 .CurrentBalance = 0
88116
89117 dbAccount , err = store .Account (ctx , acct1 .ID )
90118 require .NoError (t , err )
91119 assertEqualAccounts (t , acct1 , dbAccount )
92120
121+ // Adjusting the value to below 0 should fail.
122+ err = store .AdjustAccountBalance (
123+ ctx , acct1 .ID , lnwire .MilliSatoshi (1 ), false ,
124+ )
125+ require .ErrorContains (t , err , "balance would be below 0" )
126+
93127 // Sleep just a tiny bit to make sure we are never too quick to measure
94128 // the expiry, even though the time is nanosecond scale and writing to
95129 // the store and reading again should take at least a couple of
0 commit comments