Skip to content

Commit a4e0102

Browse files
authored
Merge pull request #781 from starius/sweepbatcher-exectx
loopdb: parameterize ExecTx with subset of methods used by a package
2 parents 4ad86b6 + d6aa4fe commit a4e0102

File tree

10 files changed

+152
-70
lines changed

10 files changed

+152
-70
lines changed

instantout/reservation/manager_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -112,7 +112,7 @@ func newManagerTestContext(t *testing.T) *ManagerTestContext {
112112

113113
dbFixture := loopdb.NewTestDB(t)
114114

115-
store := NewSQLStore(dbFixture)
115+
store := NewSQLStore(loopdb.NewTypedStore[Querier](dbFixture))
116116

117117
mockReservationClient := new(mockReservationClient)
118118

instantout/reservation/store.go

Lines changed: 18 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -16,9 +16,9 @@ import (
1616
"github.com/lightningnetwork/lnd/keychain"
1717
)
1818

19-
// BaseDB is the interface that contains all the queries generated
19+
// Querier is the interface that contains all the queries generated
2020
// by sqlc for the reservation table.
21-
type BaseDB interface {
21+
type Querier interface {
2222
// CreateReservation stores the reservation in the database.
2323
CreateReservation(ctx context.Context,
2424
arg sqlc.CreateReservationParams) error
@@ -35,14 +35,24 @@ type BaseDB interface {
3535
// made.
3636
GetReservations(ctx context.Context) ([]sqlc.Reservation, error)
3737

38-
// UpdateReservation inserts a new reservation update.
38+
// InsertReservationUpdate inserts a new reservation update.
39+
InsertReservationUpdate(ctx context.Context,
40+
arg sqlc.InsertReservationUpdateParams) error
41+
42+
// UpdateReservation updates a reservation.
3943
UpdateReservation(ctx context.Context,
4044
arg sqlc.UpdateReservationParams) error
45+
}
46+
47+
// BaseDB is the interface that contains all the queries generated
48+
// by sqlc for the reservation table and transaction functionality.
49+
type BaseDB interface {
50+
Querier
4151

4252
// ExecTx allows for executing a function in the context of a database
4353
// transaction.
4454
ExecTx(ctx context.Context, txOptions loopdb.TxOptions,
45-
txBody func(*sqlc.Queries) error) error
55+
txBody func(Querier) error) error
4656
}
4757

4858
// SQLStore manages the reservations in the database.
@@ -82,7 +92,7 @@ func (r *SQLStore) CreateReservation(ctx context.Context,
8292
}
8393

8494
return r.baseDb.ExecTx(ctx, loopdb.NewSqlWriteOpts(),
85-
func(q *sqlc.Queries) error {
95+
func(q Querier) error {
8696
err := q.CreateReservation(ctx, args)
8797
if err != nil {
8898
return err
@@ -122,7 +132,7 @@ func (r *SQLStore) UpdateReservation(ctx context.Context,
122132
}
123133

124134
return r.baseDb.ExecTx(ctx, loopdb.NewSqlWriteOpts(),
125-
func(q *sqlc.Queries) error {
135+
func(q Querier) error {
126136
err := q.UpdateReservation(ctx, updateArgs)
127137
if err != nil {
128138
return err
@@ -138,7 +148,7 @@ func (r *SQLStore) GetReservation(ctx context.Context,
138148

139149
var reservation *Reservation
140150
err := r.baseDb.ExecTx(ctx, loopdb.NewSqlReadOpts(),
141-
func(q *sqlc.Queries) error {
151+
func(q Querier) error {
142152
var err error
143153
reservationRow, err := q.GetReservation(
144154
ctx, reservationId[:],
@@ -182,7 +192,7 @@ func (r *SQLStore) ListReservations(ctx context.Context) ([]*Reservation,
182192
var result []*Reservation
183193

184194
err := r.baseDb.ExecTx(ctx, loopdb.NewSqlReadOpts(),
185-
func(q *sqlc.Queries) error {
195+
func(q Querier) error {
186196
var err error
187197

188198
reservations, err := q.GetReservations(ctx)

instantout/reservation/store_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ func TestSqlStore(t *testing.T) {
1919
testDb := loopdb.NewTestDB(t)
2020
defer testDb.Close()
2121

22-
store := NewSQLStore(testDb)
22+
store := NewSQLStore(loopdb.NewTypedStore[Querier](testDb))
2323

2424
// Create a reservation and store it.
2525
reservation := &Reservation{

instantout/store.go

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -21,9 +21,9 @@ import (
2121
"github.com/lightningnetwork/lnd/lnwallet/chainfee"
2222
)
2323

24-
// InstantOutBaseDB is the interface that contains all the queries generated
24+
// Querier is the interface that contains all the queries generated
2525
// by sqlc for the instantout table.
26-
type InstantOutBaseDB interface {
26+
type Querier interface {
2727
// InsertSwap inserts a new base swap.
2828
InsertSwap(ctx context.Context, arg sqlc.InsertSwapParams) error
2929

@@ -53,11 +53,17 @@ type InstantOutBaseDB interface {
5353
// GetInstantOutSwaps retrieves all instant out swaps.
5454
GetInstantOutSwaps(ctx context.Context) ([]sqlc.GetInstantOutSwapsRow,
5555
error)
56+
}
57+
58+
// InstantOutBaseDB is the interface that contains all the queries generated
59+
// by sqlc for the instantout table and transaction functionality.
60+
type InstantOutBaseDB interface {
61+
Querier
5662

5763
// ExecTx allows for executing a function in the context of a database
5864
// transaction.
5965
ExecTx(ctx context.Context, txOptions loopdb.TxOptions,
60-
txBody func(*sqlc.Queries) error) error
66+
txBody func(Querier) error) error
6167
}
6268

6369
// ReservationStore is the interface that is required to load the reservations
@@ -133,7 +139,7 @@ func (s *SQLStore) CreateInstantLoopOut(ctx context.Context,
133139
}
134140

135141
return s.baseDb.ExecTx(ctx, loopdb.NewSqlWriteOpts(),
136-
func(q *sqlc.Queries) error {
142+
func(q Querier) error {
137143
err := q.InsertSwap(ctx, swapArgs)
138144
if err != nil {
139145
return err
@@ -205,7 +211,7 @@ func (s *SQLStore) UpdateInstantLoopOut(ctx context.Context,
205211
}
206212

207213
return s.baseDb.ExecTx(ctx, loopdb.NewSqlWriteOpts(),
208-
func(q *sqlc.Queries) error {
214+
func(q Querier) error {
209215
err := q.UpdateInstantOut(ctx, updateParams)
210216
if err != nil {
211217
return err

loopd/daemon.go

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -417,7 +417,10 @@ func (d *Daemon) initialize(withMacaroonService bool) error {
417417
return err
418418
}
419419

420-
sweeperDb := sweepbatcher.NewSQLStore(baseDb, chainParams)
420+
sweeperDb := sweepbatcher.NewSQLStore(
421+
loopdb.NewTypedStore[sweepbatcher.Querier](baseDb),
422+
chainParams,
423+
)
421424

422425
// Create an instance of the loop client library.
423426
swapClient, clientCleanup, err := getClient(
@@ -501,7 +504,9 @@ func (d *Daemon) initialize(withMacaroonService bool) error {
501504
)
502505
// Create the reservation and instantout managers.
503506
if d.cfg.EnableExperimental {
504-
reservationStore := reservation.NewSQLStore(baseDb)
507+
reservationStore := reservation.NewSQLStore(
508+
loopdb.NewTypedStore[reservation.Querier](baseDb),
509+
)
505510
reservationConfig := &reservation.Config{
506511
Store: reservationStore,
507512
Wallet: d.lnd.WalletKit,
@@ -516,7 +521,8 @@ func (d *Daemon) initialize(withMacaroonService bool) error {
516521

517522
// Create the instantout services.
518523
instantOutStore := instantout.NewSQLStore(
519-
baseDb, clock.NewDefaultClock(), reservationStore,
524+
loopdb.NewTypedStore[instantout.Querier](baseDb),
525+
clock.NewDefaultClock(), reservationStore,
520526
d.lnd.ChainParams,
521527
)
522528
instantOutConfig := &instantout.Config{

loopd/view.go

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,10 @@ func view(config *Config, lisCfg *ListenerCfg) error {
3232
return err
3333
}
3434

35-
sweeperDb := sweepbatcher.NewSQLStore(baseDb, chainParams)
35+
sweeperDb := sweepbatcher.NewSQLStore(
36+
loopdb.NewTypedStore[sweepbatcher.Querier](baseDb),
37+
chainParams,
38+
)
3639

3740
swapClient, cleanup, err := getClient(
3841
config, swapDb, sweeperDb, &lnd.LndServices,

0 commit comments

Comments
 (0)