Skip to content

Commit 42a4c82

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

File tree

7 files changed

+47
-29
lines changed

7 files changed

+47
-29
lines changed

accounts/checkers_test.go

+10-6
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

+2-1
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"
@@ -832,7 +833,7 @@ func TestAccountService(t *testing.T) {
832833
errFunc := func(err error) {
833834
lndMock.mainErrChan <- err
834835
}
835-
store := NewTestDB(t)
836+
store := NewTestDB(t, clock.NewTestClock(time.Now()))
836837
service, err := NewService(store, errFunc)
837838
require.NoError(t, err)
838839

accounts/store_kvdb.go

+13-8
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/lnrpc"
@@ -59,12 +60,13 @@ var (
5960

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

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

100102
// Return the DB wrapped in a BoltStore object.
101-
return &BoltStore{db: db}, nil
103+
return &BoltStore{
104+
db: db,
105+
clock: clock,
106+
}, nil
102107
}
103108

104109
// Close closes the underlying bolt DB.
@@ -170,7 +175,7 @@ func (s *BoltStore) NewAccount(ctx context.Context, balance lnwire.MilliSatoshi,
170175
}
171176

172177
account.ID = id
173-
return storeAccount(bucket, account)
178+
return s.storeAccount(bucket, account)
174179
}, func() {
175180
account.ID = zeroID
176181
})
@@ -194,7 +199,7 @@ func (s *BoltStore) UpdateAccount(_ context.Context,
194199
return ErrAccountBucketNotFound
195200
}
196201

197-
return storeAccount(bucket, account)
202+
return s.storeAccount(bucket, account)
198203
}, func() {})
199204
}
200205

@@ -365,7 +370,7 @@ func (s *BoltStore) updateAccount(id AccountID,
365370
return fmt.Errorf("error updating account, %w", err)
366371
}
367372

368-
err = storeAccount(bucket, account)
373+
err = s.storeAccount(bucket, account)
369374
if err != nil {
370375
return fmt.Errorf("error storing account, %w", err)
371376
}
@@ -376,10 +381,10 @@ func (s *BoltStore) updateAccount(id AccountID,
376381

377382
// storeAccount serializes and writes the given account to the given account
378383
// bucket.
379-
func storeAccount(accountBucket kvdb.RwBucket,
384+
func (s *BoltStore) storeAccount(accountBucket kvdb.RwBucket,
380385
account *OffChainBalanceAccount) error {
381386

382-
account.LastUpdate = time.Now()
387+
account.LastUpdate = s.clock.Now()
383388

384389
accountBinary, err := serializeAccount(account)
385390
if err != nil {

accounts/store_test.go

+12-9
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.
@@ -259,7 +262,7 @@ func TestAccountUpdateMethods(t *testing.T) {
259262
})
260263

261264
t.Run("Upsert and Delete AccountPayment", func(t *testing.T) {
262-
store := NewTestDB(t)
265+
store := NewTestDB(t, clock.NewTestClock(time.Now()))
263266

264267
acct, err := store.NewAccount(ctx, 1000, time.Time{}, "foo")
265268
require.NoError(t, err)
@@ -507,7 +510,7 @@ func TestLastInvoiceIndexes(t *testing.T) {
507510
t.Parallel()
508511
ctx := context.Background()
509512

510-
store := NewTestDB(t)
513+
store := NewTestDB(t, clock.NewTestClock(time.Now()))
511514

512515
_, _, err := store.LastIndexes(ctx)
513516
require.ErrorIs(t, err, ErrNoInvoiceIndexKnown)

accounts/test_kvdb.go

+7-4
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() {

go.mod

+1-1
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ require (
2626
github.com/lightninglabs/taproot-assets v0.5.1-rc1
2727
github.com/lightningnetwork/lnd v0.18.4-beta
2828
github.com/lightningnetwork/lnd/cert v1.2.2
29+
github.com/lightningnetwork/lnd/clock v1.1.1
2930
github.com/lightningnetwork/lnd/fn v1.2.3
3031
github.com/lightningnetwork/lnd/kvdb v1.4.10
3132
github.com/lightningnetwork/lnd/tlv v1.2.6
@@ -137,7 +138,6 @@ require (
137138
github.com/lightninglabs/neutrino v0.16.1-0.20240425105051-602843d34ffd // indirect
138139
github.com/lightninglabs/neutrino/cache v1.1.2 // indirect
139140
github.com/lightningnetwork/lightning-onion v1.2.1-0.20240712235311-98bd56499dfb // indirect
140-
github.com/lightningnetwork/lnd/clock v1.1.1 // indirect
141141
github.com/lightningnetwork/lnd/healthcheck v1.2.5 // indirect
142142
github.com/lightningnetwork/lnd/queue v1.1.1 // indirect
143143
github.com/lightningnetwork/lnd/sqldb v1.0.4 // indirect

terminal.go

+2
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)