Skip to content

Commit

Permalink
Merge pull request #944 from ellemouton/sql5Accounts5
Browse files Browse the repository at this point in the history
[sql-5] accounts: use clock.Clock
  • Loading branch information
ellemouton authored Jan 23, 2025
2 parents e7dc483 + 42a4c82 commit 2724161
Show file tree
Hide file tree
Showing 7 changed files with 47 additions and 29 deletions.
16 changes: 10 additions & 6 deletions accounts/checkers_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import (
"time"

"github.com/btcsuite/btcd/chaincfg"
"github.com/lightningnetwork/lnd/clock"
"github.com/lightningnetwork/lnd/lnrpc"
"github.com/lightningnetwork/lnd/lnrpc/routerrpc"
"github.com/lightningnetwork/lnd/lntypes"
Expand Down Expand Up @@ -523,7 +524,8 @@ func testSendPayment(t *testing.T, uri string) {
errFunc := func(err error) {
lndMock.mainErrChan <- err
}
store := NewTestDB(t)
clock := clock.NewTestClock(time.Now())
store := NewTestDB(t, clock)
service, err := NewService(store, errFunc)
require.NoError(t, err)

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

// Create an account and add it to the context.
acct, err := service.NewAccount(
ctx, 5000, time.Now().Add(time.Hour), "test",
ctx, 5000, clock.Now().Add(time.Hour), "test",
)
require.NoError(t, err)

Expand Down Expand Up @@ -720,7 +722,8 @@ func TestSendPaymentV2(t *testing.T) {
errFunc := func(err error) {
lndMock.mainErrChan <- err
}
store := NewTestDB(t)
clock := clock.NewTestClock(time.Now())
store := NewTestDB(t, clock)
service, err := NewService(store, errFunc)
require.NoError(t, err)

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

// Create an account and add it to the context.
acct, err := service.NewAccount(
ctx, 5000, time.Now().Add(time.Hour), "test",
ctx, 5000, clock.Now().Add(time.Hour), "test",
)
require.NoError(t, err)

Expand Down Expand Up @@ -908,7 +911,8 @@ func TestSendToRouteV2(t *testing.T) {
errFunc := func(err error) {
lndMock.mainErrChan <- err
}
store := NewTestDB(t)
clock := clock.NewTestClock(time.Now())
store := NewTestDB(t, clock)
service, err := NewService(store, errFunc)
require.NoError(t, err)

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

// Create an account and add it to the context.
acct, err := service.NewAccount(
ctx, 5000, time.Now().Add(time.Hour), "test",
ctx, 5000, clock.Now().Add(time.Hour), "test",
)
require.NoError(t, err)

Expand Down
3 changes: 2 additions & 1 deletion accounts/service_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import (

"github.com/lightninglabs/lndclient"
"github.com/lightningnetwork/lnd/channeldb"
"github.com/lightningnetwork/lnd/clock"
invpkg "github.com/lightningnetwork/lnd/invoices"
"github.com/lightningnetwork/lnd/lnrpc"
"github.com/lightningnetwork/lnd/lnrpc/routerrpc"
Expand Down Expand Up @@ -832,7 +833,7 @@ func TestAccountService(t *testing.T) {
errFunc := func(err error) {
lndMock.mainErrChan <- err
}
store := NewTestDB(t)
store := NewTestDB(t, clock.NewTestClock(time.Now()))
service, err := NewService(store, errFunc)
require.NoError(t, err)

Expand Down
21 changes: 13 additions & 8 deletions accounts/store_kvdb.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import (
"time"

"github.com/btcsuite/btcwallet/walletdb"
"github.com/lightningnetwork/lnd/clock"
"github.com/lightningnetwork/lnd/fn"
"github.com/lightningnetwork/lnd/kvdb"
"github.com/lightningnetwork/lnd/lnrpc"
Expand Down Expand Up @@ -59,12 +60,13 @@ var (

// BoltStore wraps the bolt DB that stores all accounts and their balances.
type BoltStore struct {
db kvdb.Backend
db kvdb.Backend
clock clock.Clock
}

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

// Return the DB wrapped in a BoltStore object.
return &BoltStore{db: db}, nil
return &BoltStore{
db: db,
clock: clock,
}, nil
}

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

account.ID = id
return storeAccount(bucket, account)
return s.storeAccount(bucket, account)
}, func() {
account.ID = zeroID
})
Expand All @@ -194,7 +199,7 @@ func (s *BoltStore) UpdateAccount(_ context.Context,
return ErrAccountBucketNotFound
}

return storeAccount(bucket, account)
return s.storeAccount(bucket, account)
}, func() {})
}

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

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

// storeAccount serializes and writes the given account to the given account
// bucket.
func storeAccount(accountBucket kvdb.RwBucket,
func (s *BoltStore) storeAccount(accountBucket kvdb.RwBucket,
account *OffChainBalanceAccount) error {

account.LastUpdate = time.Now()
account.LastUpdate = s.clock.Now()

accountBinary, err := serializeAccount(account)
if err != nil {
Expand Down
21 changes: 12 additions & 9 deletions accounts/store_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"testing"
"time"

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

store := NewTestDB(t)
clock := clock.NewTestClock(time.Now())
store := NewTestDB(t, clock)

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

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

t.Run("UpdateAccountBalanceAndExpiry", func(t *testing.T) {
store := NewTestDB(t)
clock := clock.NewTestClock(time.Now())
store := NewTestDB(t, clock)

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

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

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

t.Run("AddAccountInvoice", func(t *testing.T) {
store := NewTestDB(t)
store := NewTestDB(t, clock.NewTestClock(time.Now()))

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

t.Run("IncreaseAccountBalance", func(t *testing.T) {
store := NewTestDB(t)
store := NewTestDB(t, clock.NewTestClock(time.Now()))

// Increasing the balance of an account that doesn't exist
// should error out.
Expand Down Expand Up @@ -259,7 +262,7 @@ func TestAccountUpdateMethods(t *testing.T) {
})

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

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

store := NewTestDB(t)
store := NewTestDB(t, clock.NewTestClock(time.Now()))

_, _, err := store.LastIndexes(ctx)
require.ErrorIs(t, err, ErrNoInvoiceIndexKnown)
Expand Down
11 changes: 7 additions & 4 deletions accounts/test_kvdb.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"errors"
"testing"

"github.com/lightningnetwork/lnd/clock"
"github.com/stretchr/testify/require"
)

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

// NewTestDB is a helper function that creates an BBolt database for testing.
func NewTestDB(t *testing.T) *BoltStore {
return NewTestDBFromPath(t, t.TempDir())
func NewTestDB(t *testing.T, clock clock.Clock) *BoltStore {
return NewTestDBFromPath(t, t.TempDir(), clock)
}

// NewTestDBFromPath is a helper function that creates a new BoltStore with a
// connection to an existing BBolt database for testing.
func NewTestDBFromPath(t *testing.T, dbPath string) *BoltStore {
store, err := NewBoltStore(dbPath, DBFilename)
func NewTestDBFromPath(t *testing.T, dbPath string,
clock clock.Clock) *BoltStore {

store, err := NewBoltStore(dbPath, DBFilename, clock)
require.NoError(t, err)

t.Cleanup(func() {
Expand Down
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ require (
github.com/lightninglabs/taproot-assets v0.5.1-rc1
github.com/lightningnetwork/lnd v0.18.4-beta
github.com/lightningnetwork/lnd/cert v1.2.2
github.com/lightningnetwork/lnd/clock v1.1.1
github.com/lightningnetwork/lnd/fn v1.2.3
github.com/lightningnetwork/lnd/kvdb v1.4.10
github.com/lightningnetwork/lnd/tlv v1.2.6
Expand Down Expand Up @@ -137,7 +138,6 @@ require (
github.com/lightninglabs/neutrino v0.16.1-0.20240425105051-602843d34ffd // indirect
github.com/lightninglabs/neutrino/cache v1.1.2 // indirect
github.com/lightningnetwork/lightning-onion v1.2.1-0.20240712235311-98bd56499dfb // indirect
github.com/lightningnetwork/lnd/clock v1.1.1 // indirect
github.com/lightningnetwork/lnd/healthcheck v1.2.5 // indirect
github.com/lightningnetwork/lnd/queue v1.1.1 // indirect
github.com/lightningnetwork/lnd/sqldb v1.0.4 // indirect
Expand Down
2 changes: 2 additions & 0 deletions terminal.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ import (
"github.com/lightningnetwork/lnd"
"github.com/lightningnetwork/lnd/build"
"github.com/lightningnetwork/lnd/chainreg"
"github.com/lightningnetwork/lnd/clock"
"github.com/lightningnetwork/lnd/fn"
"github.com/lightningnetwork/lnd/funding"
"github.com/lightningnetwork/lnd/htlcswitch"
Expand Down Expand Up @@ -415,6 +416,7 @@ func (g *LightningTerminal) start(ctx context.Context) error {

g.accountsStore, err = accounts.NewBoltStore(
filepath.Dir(g.cfg.MacaroonPath), accounts.DBFilename,
clock.NewDefaultClock(),
)
if err != nil {
return fmt.Errorf("error creating accounts store: %w", err)
Expand Down

0 comments on commit 2724161

Please sign in to comment.