Skip to content

Commit e0f0535

Browse files
committed
accounts: use clock.Clock
In this commit, we use the clock.Clock type instead of directly calling time.Now().
1 parent a87d200 commit e0f0535

File tree

6 files changed

+45
-27
lines changed

6 files changed

+45
-27
lines changed

accounts/checkers_test.go

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import (
88
"time"
99

1010
"github.com/btcsuite/btcd/chaincfg"
11+
"github.com/lightningnetwork/lnd/clock"
1112
"github.com/lightningnetwork/lnd/lnrpc"
1213
"github.com/lightningnetwork/lnd/lnrpc/routerrpc"
1314
"github.com/lightningnetwork/lnd/lntypes"
@@ -523,7 +524,8 @@ func testSendPayment(t *testing.T, uri string) {
523524
errFunc := func(err error) {
524525
lndMock.mainErrChan <- err
525526
}
526-
store := NewTestDB(t)
527+
clock := clock.NewTestClock(time.Now())
528+
store := NewTestDB(t, clock)
527529
service, err := NewService(store, errFunc)
528530
require.NoError(t, err)
529531

@@ -546,7 +548,7 @@ func testSendPayment(t *testing.T, uri string) {
546548

547549
// Create an account and add it to the context.
548550
acct, err := service.NewAccount(
549-
ctx, 5000, time.Now().Add(time.Hour), "test",
551+
ctx, 5000, clock.Now().Add(time.Hour), "test",
550552
)
551553
require.NoError(t, err)
552554

@@ -720,7 +722,8 @@ func TestSendPaymentV2(t *testing.T) {
720722
errFunc := func(err error) {
721723
lndMock.mainErrChan <- err
722724
}
723-
store := NewTestDB(t)
725+
clock := clock.NewTestClock(time.Now())
726+
store := NewTestDB(t, clock)
724727
service, err := NewService(store, errFunc)
725728
require.NoError(t, err)
726729

@@ -743,7 +746,7 @@ func TestSendPaymentV2(t *testing.T) {
743746

744747
// Create an account and add it to the context.
745748
acct, err := service.NewAccount(
746-
ctx, 5000, time.Now().Add(time.Hour), "test",
749+
ctx, 5000, clock.Now().Add(time.Hour), "test",
747750
)
748751
require.NoError(t, err)
749752

@@ -908,7 +911,8 @@ func TestSendToRouteV2(t *testing.T) {
908911
errFunc := func(err error) {
909912
lndMock.mainErrChan <- err
910913
}
911-
store := NewTestDB(t)
914+
clock := clock.NewTestClock(time.Now())
915+
store := NewTestDB(t, clock)
912916
service, err := NewService(store, errFunc)
913917
require.NoError(t, err)
914918

@@ -931,7 +935,7 @@ func TestSendToRouteV2(t *testing.T) {
931935

932936
// Create an account and add it to the context.
933937
acct, err := service.NewAccount(
934-
ctx, 5000, time.Now().Add(time.Hour), "test",
938+
ctx, 5000, clock.Now().Add(time.Hour), "test",
935939
)
936940
require.NoError(t, err)
937941

accounts/service_test.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import (
88

99
"github.com/lightninglabs/lndclient"
1010
"github.com/lightningnetwork/lnd/channeldb"
11+
"github.com/lightningnetwork/lnd/clock"
1112
invpkg "github.com/lightningnetwork/lnd/invoices"
1213
"github.com/lightningnetwork/lnd/lnrpc"
1314
"github.com/lightningnetwork/lnd/lnrpc/routerrpc"
@@ -829,7 +830,7 @@ func TestAccountService(t *testing.T) {
829830
errFunc := func(err error) {
830831
lndMock.mainErrChan <- err
831832
}
832-
store := NewTestDB(t)
833+
store := NewTestDB(t, clock.NewTestClock(time.Now()))
833834
service, err := NewService(store, errFunc)
834835
require.NoError(t, err)
835836

accounts/store_kvdb.go

Lines changed: 13 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ import (
1212
"time"
1313

1414
"github.com/btcsuite/btcwallet/walletdb"
15+
"github.com/lightningnetwork/lnd/clock"
1516
"github.com/lightningnetwork/lnd/fn"
1617
"github.com/lightningnetwork/lnd/kvdb"
1718
"github.com/lightningnetwork/lnd/lntypes"
@@ -58,12 +59,13 @@ var (
5859

5960
// BoltStore wraps the bolt DB that stores all accounts and their balances.
6061
type BoltStore struct {
61-
db kvdb.Backend
62+
db kvdb.Backend
63+
clock clock.Clock
6264
}
6365

6466
// NewBoltStore creates a BoltStore instance and the corresponding bucket in the
6567
// bolt DB if it does not exist yet.
66-
func NewBoltStore(dir, fileName string) (*BoltStore, error) {
68+
func NewBoltStore(dir, fileName string, clock clock.Clock) (*BoltStore, error) {
6769
// Ensure that the path to the directory exists.
6870
if _, err := os.Stat(dir); os.IsNotExist(err) {
6971
if err := os.MkdirAll(dir, dbPathPermission); err != nil {
@@ -97,7 +99,10 @@ func NewBoltStore(dir, fileName string) (*BoltStore, error) {
9799
}
98100

99101
// Return the DB wrapped in a BoltStore object.
100-
return &BoltStore{db: db}, nil
102+
return &BoltStore{
103+
db: db,
104+
clock: clock,
105+
}, nil
101106
}
102107

103108
// Close closes the underlying bolt DB.
@@ -169,7 +174,7 @@ func (s *BoltStore) NewAccount(ctx context.Context, balance lnwire.MilliSatoshi,
169174
}
170175

171176
account.ID = id
172-
return storeAccount(bucket, account)
177+
return s.storeAccount(bucket, account)
173178
}, func() {
174179
account.ID = zeroID
175180
})
@@ -193,7 +198,7 @@ func (s *BoltStore) UpdateAccount(_ context.Context,
193198
return ErrAccountBucketNotFound
194199
}
195200

196-
return storeAccount(bucket, account)
201+
return s.storeAccount(bucket, account)
197202
}, func() {})
198203
}
199204

@@ -274,7 +279,7 @@ func (s *BoltStore) updateAccount(id AccountID,
274279
return fmt.Errorf("error updating account, %w", err)
275280
}
276281

277-
err = storeAccount(bucket, account)
282+
err = s.storeAccount(bucket, account)
278283
if err != nil {
279284
return fmt.Errorf("error storing account, %w", err)
280285
}
@@ -285,10 +290,10 @@ func (s *BoltStore) updateAccount(id AccountID,
285290

286291
// storeAccount serializes and writes the given account to the given account
287292
// bucket.
288-
func storeAccount(accountBucket kvdb.RwBucket,
293+
func (s *BoltStore) storeAccount(accountBucket kvdb.RwBucket,
289294
account *OffChainBalanceAccount) error {
290295

291-
account.LastUpdate = time.Now()
296+
account.LastUpdate = s.clock.Now()
292297

293298
accountBinary, err := serializeAccount(account)
294299
if err != nil {

accounts/store_test.go

Lines changed: 11 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import (
55
"testing"
66
"time"
77

8+
"github.com/lightningnetwork/lnd/clock"
89
"github.com/lightningnetwork/lnd/fn"
910
"github.com/lightningnetwork/lnd/lnrpc"
1011
"github.com/lightningnetwork/lnd/lntypes"
@@ -17,7 +18,8 @@ func TestAccountStore(t *testing.T) {
1718
t.Parallel()
1819
ctx := context.Background()
1920

20-
store := NewTestDB(t)
21+
clock := clock.NewTestClock(time.Now())
22+
store := NewTestDB(t, clock)
2123

2224
// Create an account that does not expire.
2325
acct1, err := store.NewAccount(ctx, 0, time.Time{}, "foo")
@@ -39,7 +41,7 @@ func TestAccountStore(t *testing.T) {
3941

4042
// Update all values of the account that we can modify.
4143
acct1.CurrentBalance = -500
42-
acct1.ExpirationDate = time.Now()
44+
acct1.ExpirationDate = clock.Now()
4345
acct1.Payments[lntypes.Hash{12, 34, 56, 78}] = &PaymentEntry{
4446
Status: lnrpc.Payment_FAILED,
4547
FullAmount: 123456,
@@ -114,7 +116,8 @@ func TestAccountUpdateMethods(t *testing.T) {
114116
ctx := context.Background()
115117

116118
t.Run("UpdateAccountBalanceAndExpiry", func(t *testing.T) {
117-
store := NewTestDB(t)
119+
clock := clock.NewTestClock(time.Now())
120+
store := NewTestDB(t, clock)
118121

119122
// Ensure that the function errors out if we try update an
120123
// account that does not exist.
@@ -151,7 +154,7 @@ func TestAccountUpdateMethods(t *testing.T) {
151154
assertBalanceAndExpiry(newBalance, time.Time{})
152155

153156
// Now update just the expiry of the account.
154-
newExpiry := time.Now().Add(time.Hour)
157+
newExpiry := clock.Now().Add(time.Hour)
155158
err = store.UpdateAccountBalanceAndExpiry(
156159
ctx, acct.ID, fn.None[lnwire.MilliSatoshi](),
157160
fn.Some(newExpiry),
@@ -161,7 +164,7 @@ func TestAccountUpdateMethods(t *testing.T) {
161164

162165
// Update both the balance and expiry of the account.
163166
newBalance = 456
164-
newExpiry = time.Now().Add(2 * time.Hour)
167+
newExpiry = clock.Now().Add(2 * time.Hour)
165168
err = store.UpdateAccountBalanceAndExpiry(
166169
ctx, acct.ID, fn.Some(newBalance), fn.Some(newExpiry),
167170
)
@@ -179,7 +182,7 @@ func TestAccountUpdateMethods(t *testing.T) {
179182
})
180183

181184
t.Run("AddAccountInvoice", func(t *testing.T) {
182-
store := NewTestDB(t)
185+
store := NewTestDB(t, clock.NewTestClock(time.Now()))
183186

184187
acct, err := store.NewAccount(ctx, 0, time.Time{}, "foo")
185188
require.NoError(t, err)
@@ -231,7 +234,7 @@ func TestAccountUpdateMethods(t *testing.T) {
231234
})
232235

233236
t.Run("IncreaseAccountBalance", func(t *testing.T) {
234-
store := NewTestDB(t)
237+
store := NewTestDB(t, clock.NewTestClock(time.Now()))
235238

236239
// Increasing the balance of an account that doesn't exist
237240
// should error out.
@@ -265,7 +268,7 @@ func TestLastInvoiceIndexes(t *testing.T) {
265268
t.Parallel()
266269
ctx := context.Background()
267270

268-
store := NewTestDB(t)
271+
store := NewTestDB(t, clock.NewTestClock(time.Now()))
269272

270273
_, _, err := store.LastIndexes(ctx)
271274
require.ErrorIs(t, err, ErrNoInvoiceIndexKnown)

accounts/test_kvdb.go

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import (
44
"errors"
55
"testing"
66

7+
"github.com/lightningnetwork/lnd/clock"
78
"github.com/stretchr/testify/require"
89
)
910

@@ -12,14 +13,16 @@ import (
1213
var ErrDBClosed = errors.New("database not open")
1314

1415
// NewTestDB is a helper function that creates an BBolt database for testing.
15-
func NewTestDB(t *testing.T) *BoltStore {
16-
return NewTestDBFromPath(t, t.TempDir())
16+
func NewTestDB(t *testing.T, clock clock.Clock) *BoltStore {
17+
return NewTestDBFromPath(t, t.TempDir(), clock)
1718
}
1819

1920
// NewTestDBFromPath is a helper function that creates a new BoltStore with a
2021
// connection to an existing BBolt database for testing.
21-
func NewTestDBFromPath(t *testing.T, dbPath string) *BoltStore {
22-
store, err := NewBoltStore(dbPath, DBFilename)
22+
func NewTestDBFromPath(t *testing.T, dbPath string,
23+
clock clock.Clock) *BoltStore {
24+
25+
store, err := NewBoltStore(dbPath, DBFilename, clock)
2326
require.NoError(t, err)
2427

2528
t.Cleanup(func() {

terminal.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@ import (
3838
"github.com/lightningnetwork/lnd"
3939
"github.com/lightningnetwork/lnd/build"
4040
"github.com/lightningnetwork/lnd/chainreg"
41+
"github.com/lightningnetwork/lnd/clock"
4142
"github.com/lightningnetwork/lnd/fn"
4243
"github.com/lightningnetwork/lnd/funding"
4344
"github.com/lightningnetwork/lnd/htlcswitch"
@@ -415,6 +416,7 @@ func (g *LightningTerminal) start(ctx context.Context) error {
415416

416417
g.accountsStore, err = accounts.NewBoltStore(
417418
filepath.Dir(g.cfg.MacaroonPath), accounts.DBFilename,
419+
clock.NewDefaultClock(),
418420
)
419421
if err != nil {
420422
return fmt.Errorf("error creating accounts store: %w", err)

0 commit comments

Comments
 (0)