Skip to content

Commit 8c4d17c

Browse files
committed
session: add a clock to the DB
1 parent e923fff commit 8c4d17c

File tree

3 files changed

+18
-7
lines changed

3 files changed

+18
-7
lines changed

session/kvdb_store.go

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

1212
"github.com/btcsuite/btcd/btcec/v2"
13+
"github.com/lightningnetwork/lnd/clock"
1314
"go.etcd.io/bbolt"
1415
"gopkg.in/macaroon-bakery.v2/bakery"
1516
"gopkg.in/macaroon.v2"
@@ -79,13 +80,15 @@ const (
7980
// BoltStore is a bolt-backed persistent store.
8081
type BoltStore struct {
8182
*bbolt.DB
83+
84+
clock clock.Clock
8285
}
8386

8487
// A compile-time check to ensure that BoltStore implements the Store interface.
8588
var _ Store = (*BoltStore)(nil)
8689

8790
// NewDB creates a new bolt database that can be found at the given directory.
88-
func NewDB(dir, fileName string) (*BoltStore, error) {
91+
func NewDB(dir, fileName string, clock clock.Clock) (*BoltStore, error) {
8992
firstInit := false
9093
path := filepath.Join(dir, fileName)
9194

@@ -108,7 +111,10 @@ func NewDB(dir, fileName string) (*BoltStore, error) {
108111
return nil, err
109112
}
110113

111-
return &BoltStore{DB: db}, nil
114+
return &BoltStore{
115+
DB: db,
116+
clock: clock,
117+
}, nil
112118
}
113119

114120
// fileExists reports whether the named file or directory exists.

session/store_test.go

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,14 +6,17 @@ import (
66
"time"
77

88
"github.com/btcsuite/btcd/btcec/v2"
9+
"github.com/lightningnetwork/lnd/clock"
910
"github.com/stretchr/testify/require"
1011
)
1112

13+
var testTime = time.Date(2020, 1, 1, 0, 0, 0, 0, time.UTC)
14+
1215
// TestBasicSessionStore tests the basic getters and setters of the session
1316
// store.
1417
func TestBasicSessionStore(t *testing.T) {
1518
// Set up a new DB.
16-
db, err := NewDB(t.TempDir(), "test.db")
19+
db, err := NewDB(t.TempDir(), "test.db", clock.NewTestClock(testTime))
1720
require.NoError(t, err)
1821
t.Cleanup(func() {
1922
_ = db.Close()
@@ -89,7 +92,7 @@ func TestBasicSessionStore(t *testing.T) {
8992
// TestLinkingSessions tests that session linking works as expected.
9093
func TestLinkingSessions(t *testing.T) {
9194
// Set up a new DB.
92-
db, err := NewDB(t.TempDir(), "test.db")
95+
db, err := NewDB(t.TempDir(), "test.db", clock.NewTestClock(testTime))
9396
require.NoError(t, err)
9497
t.Cleanup(func() {
9598
_ = db.Close()
@@ -125,7 +128,7 @@ func TestLinkingSessions(t *testing.T) {
125128
// of the GetGroupID and GetSessionIDs methods.
126129
func TestLinkedSessions(t *testing.T) {
127130
// Set up a new DB.
128-
db, err := NewDB(t.TempDir(), "test.db")
131+
db, err := NewDB(t.TempDir(), "test.db", clock.NewTestClock(testTime))
129132
require.NoError(t, err)
130133
t.Cleanup(func() {
131134
_ = db.Close()
@@ -192,7 +195,7 @@ func TestLinkedSessions(t *testing.T) {
192195
// method correctly checks if each session in a group passes a predicate.
193196
func TestCheckSessionGroupPredicate(t *testing.T) {
194197
// Set up a new DB.
195-
db, err := NewDB(t.TempDir(), "test.db")
198+
db, err := NewDB(t.TempDir(), "test.db", clock.NewTestClock(testTime))
196199
require.NoError(t, err)
197200
t.Cleanup(func() {
198201
_ = db.Close()

terminal.go

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -446,7 +446,9 @@ func (g *LightningTerminal) start(ctx context.Context) error {
446446

447447
// Create an instance of the local Terminal Connect session store DB.
448448
networkDir := filepath.Join(g.cfg.LitDir, g.cfg.Network)
449-
g.sessionDB, err = session.NewDB(networkDir, session.DBFilename)
449+
g.sessionDB, err = session.NewDB(
450+
networkDir, session.DBFilename, clock.NewDefaultClock(),
451+
)
450452
if err != nil {
451453
return fmt.Errorf("error creating session DB: %v", err)
452454
}

0 commit comments

Comments
 (0)