Skip to content

Commit d6aa4fe

Browse files
committed
loopdb: all BaseDB's methods use receiver name db
1 parent 4c2d874 commit d6aa4fe

File tree

2 files changed

+40
-40
lines changed

2 files changed

+40
-40
lines changed

loopdb/sql_store.go

Lines changed: 36 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -18,12 +18,12 @@ import (
1818
)
1919

2020
// FetchLoopOutSwaps returns all swaps currently in the store.
21-
func (s *BaseDB) FetchLoopOutSwaps(ctx context.Context) ([]*LoopOut,
21+
func (db *BaseDB) FetchLoopOutSwaps(ctx context.Context) ([]*LoopOut,
2222
error) {
2323

2424
var loopOuts []*LoopOut
2525

26-
err := s.ExecTx(ctx, NewSqlReadOpts(), func(tx *sqlc.Queries) error {
26+
err := db.ExecTx(ctx, NewSqlReadOpts(), func(tx *sqlc.Queries) error {
2727
swaps, err := tx.GetLoopOutSwaps(ctx)
2828
if err != nil {
2929
return err
@@ -40,7 +40,7 @@ func (s *BaseDB) FetchLoopOutSwaps(ctx context.Context) ([]*LoopOut,
4040
}
4141

4242
loopOut, err := ConvertLoopOutRow(
43-
s.network, sqlc.GetLoopOutSwapRow(swap),
43+
db.network, sqlc.GetLoopOutSwapRow(swap),
4444
updates,
4545
)
4646
if err != nil {
@@ -60,12 +60,12 @@ func (s *BaseDB) FetchLoopOutSwaps(ctx context.Context) ([]*LoopOut,
6060
}
6161

6262
// FetchLoopOutSwap returns the loop out swap with the given hash.
63-
func (s *BaseDB) FetchLoopOutSwap(ctx context.Context,
63+
func (db *BaseDB) FetchLoopOutSwap(ctx context.Context,
6464
hash lntypes.Hash) (*LoopOut, error) {
6565

6666
var loopOut *LoopOut
6767

68-
err := s.ExecTx(ctx, NewSqlReadOpts(), func(tx *sqlc.Queries) error {
68+
err := db.ExecTx(ctx, NewSqlReadOpts(), func(tx *sqlc.Queries) error {
6969
swap, err := tx.GetLoopOutSwap(ctx, hash[:])
7070
if err != nil {
7171
return err
@@ -77,7 +77,7 @@ func (s *BaseDB) FetchLoopOutSwap(ctx context.Context,
7777
}
7878

7979
loopOut, err = ConvertLoopOutRow(
80-
s.network, swap, updates,
80+
db.network, swap, updates,
8181
)
8282
if err != nil {
8383
return err
@@ -93,11 +93,11 @@ func (s *BaseDB) FetchLoopOutSwap(ctx context.Context,
9393
}
9494

9595
// CreateLoopOut adds an initiated swap to the store.
96-
func (s *BaseDB) CreateLoopOut(ctx context.Context, hash lntypes.Hash,
96+
func (db *BaseDB) CreateLoopOut(ctx context.Context, hash lntypes.Hash,
9797
swap *LoopOutContract) error {
9898

9999
writeOpts := NewSqlWriteOpts()
100-
return s.ExecTx(ctx, writeOpts, func(tx *sqlc.Queries) error {
100+
return db.ExecTx(ctx, writeOpts, func(tx *sqlc.Queries) error {
101101
insertArgs := loopToInsertArgs(
102102
hash, &swap.SwapContract,
103103
)
@@ -131,11 +131,11 @@ func (s *BaseDB) CreateLoopOut(ctx context.Context, hash lntypes.Hash,
131131
}
132132

133133
// BatchCreateLoopOut adds multiple initiated swaps to the store.
134-
func (s *BaseDB) BatchCreateLoopOut(ctx context.Context,
134+
func (db *BaseDB) BatchCreateLoopOut(ctx context.Context,
135135
swaps map[lntypes.Hash]*LoopOutContract) error {
136136

137137
writeOpts := NewSqlWriteOpts()
138-
return s.ExecTx(ctx, writeOpts, func(tx *sqlc.Queries) error {
138+
return db.ExecTx(ctx, writeOpts, func(tx *sqlc.Queries) error {
139139
for swapHash, swap := range swaps {
140140
swap := swap
141141

@@ -174,19 +174,19 @@ func (s *BaseDB) BatchCreateLoopOut(ctx context.Context,
174174
// UpdateLoopOut stores a new event for a target loop out swap. This
175175
// appends to the event log for a particular swap as it goes through
176176
// the various stages in its lifetime.
177-
func (s *BaseDB) UpdateLoopOut(ctx context.Context, hash lntypes.Hash,
177+
func (db *BaseDB) UpdateLoopOut(ctx context.Context, hash lntypes.Hash,
178178
time time.Time, state SwapStateData) error {
179179

180-
return s.updateLoop(ctx, hash, time, state)
180+
return db.updateLoop(ctx, hash, time, state)
181181
}
182182

183183
// FetchLoopInSwaps returns all swaps currently in the store.
184-
func (s *BaseDB) FetchLoopInSwaps(ctx context.Context) (
184+
func (db *BaseDB) FetchLoopInSwaps(ctx context.Context) (
185185
[]*LoopIn, error) {
186186

187187
var loopIns []*LoopIn
188188

189-
err := s.ExecTx(ctx, NewSqlReadOpts(), func(tx *sqlc.Queries) error {
189+
err := db.ExecTx(ctx, NewSqlReadOpts(), func(tx *sqlc.Queries) error {
190190
swaps, err := tx.GetLoopInSwaps(ctx)
191191
if err != nil {
192192
return err
@@ -200,7 +200,7 @@ func (s *BaseDB) FetchLoopInSwaps(ctx context.Context) (
200200
return err
201201
}
202202

203-
loopIn, err := s.convertLoopInRow(
203+
loopIn, err := db.convertLoopInRow(
204204
swap, updates,
205205
)
206206
if err != nil {
@@ -220,11 +220,11 @@ func (s *BaseDB) FetchLoopInSwaps(ctx context.Context) (
220220
}
221221

222222
// CreateLoopIn adds an initiated swap to the store.
223-
func (s *BaseDB) CreateLoopIn(ctx context.Context, hash lntypes.Hash,
223+
func (db *BaseDB) CreateLoopIn(ctx context.Context, hash lntypes.Hash,
224224
swap *LoopInContract) error {
225225

226226
writeOpts := NewSqlWriteOpts()
227-
return s.ExecTx(ctx, writeOpts, func(tx *sqlc.Queries) error {
227+
return db.ExecTx(ctx, writeOpts, func(tx *sqlc.Queries) error {
228228
insertArgs := loopToInsertArgs(
229229
hash, &swap.SwapContract,
230230
)
@@ -257,11 +257,11 @@ func (s *BaseDB) CreateLoopIn(ctx context.Context, hash lntypes.Hash,
257257
}
258258

259259
// BatchCreateLoopIn adds multiple initiated swaps to the store.
260-
func (s *BaseDB) BatchCreateLoopIn(ctx context.Context,
260+
func (db *BaseDB) BatchCreateLoopIn(ctx context.Context,
261261
swaps map[lntypes.Hash]*LoopInContract) error {
262262

263263
writeOpts := NewSqlWriteOpts()
264-
return s.ExecTx(ctx, writeOpts, func(tx *sqlc.Queries) error {
264+
return db.ExecTx(ctx, writeOpts, func(tx *sqlc.Queries) error {
265265
for swapHash, swap := range swaps {
266266
swap := swap
267267

@@ -301,21 +301,21 @@ func (s *BaseDB) BatchCreateLoopIn(ctx context.Context,
301301
// UpdateLoopIn stores a new event for a target loop in swap. This
302302
// appends to the event log for a particular swap as it goes through
303303
// the various stages in its lifetime.
304-
func (s *BaseDB) UpdateLoopIn(ctx context.Context, hash lntypes.Hash,
304+
func (db *BaseDB) UpdateLoopIn(ctx context.Context, hash lntypes.Hash,
305305
time time.Time, state SwapStateData) error {
306306

307-
return s.updateLoop(ctx, hash, time, state)
307+
return db.updateLoop(ctx, hash, time, state)
308308
}
309309

310310
// PutLiquidityParams writes the serialized `manager.Parameters` bytes
311311
// into the bucket.
312312
//
313313
// NOTE: it's the caller's responsibility to encode the param. Atm,
314314
// it's encoding using the proto package's `Marshal` method.
315-
func (s *BaseDB) PutLiquidityParams(ctx context.Context,
315+
func (db *BaseDB) PutLiquidityParams(ctx context.Context,
316316
params []byte) error {
317317

318-
err := s.Queries.UpsertLiquidityParams(ctx, params)
318+
err := db.Queries.UpsertLiquidityParams(ctx, params)
319319
if err != nil {
320320
return err
321321
}
@@ -328,11 +328,11 @@ func (s *BaseDB) PutLiquidityParams(ctx context.Context,
328328
//
329329
// NOTE: it's the caller's responsibility to decode the param. Atm,
330330
// it's decoding using the proto package's `Unmarshal` method.
331-
func (s *BaseDB) FetchLiquidityParams(ctx context.Context) ([]byte,
331+
func (db *BaseDB) FetchLiquidityParams(ctx context.Context) ([]byte,
332332
error) {
333333

334334
var params []byte
335-
params, err := s.Queries.FetchLiquidityParams(ctx)
335+
params, err := db.Queries.FetchLiquidityParams(ctx)
336336
if errors.Is(err, sql.ErrNoRows) {
337337
return params, nil
338338
} else if err != nil {
@@ -348,11 +348,11 @@ var _ SwapStore = (*BaseDB)(nil)
348348

349349
// updateLoop updates the swap with the given hash by inserting a new update
350350
// in the swap_updates table.
351-
func (s *BaseDB) updateLoop(ctx context.Context, hash lntypes.Hash,
351+
func (db *BaseDB) updateLoop(ctx context.Context, hash lntypes.Hash,
352352
time time.Time, state SwapStateData) error {
353353

354354
writeOpts := NewSqlWriteOpts()
355-
return s.ExecTx(ctx, writeOpts, func(tx *sqlc.Queries) error {
355+
return db.ExecTx(ctx, writeOpts, func(tx *sqlc.Queries) error {
356356
updateParams := sqlc.InsertSwapUpdateParams{
357357
SwapHash: hash[:],
358358
UpdateTimestamp: time.UTC(),
@@ -376,11 +376,11 @@ func (s *BaseDB) updateLoop(ctx context.Context, hash lntypes.Hash,
376376
}
377377

378378
// BatchInsertUpdate inserts multiple swap updates to the store.
379-
func (s *BaseDB) BatchInsertUpdate(ctx context.Context,
379+
func (db *BaseDB) BatchInsertUpdate(ctx context.Context,
380380
updateData map[lntypes.Hash][]BatchInsertUpdateData) error {
381381

382382
writeOpts := NewSqlWriteOpts()
383-
return s.ExecTx(ctx, writeOpts, func(tx *sqlc.Queries) error {
383+
return db.ExecTx(ctx, writeOpts, func(tx *sqlc.Queries) error {
384384
for swapHash, updates := range updateData {
385385
for _, update := range updates {
386386
updateParams := sqlc.InsertSwapUpdateParams{
@@ -409,11 +409,11 @@ func (s *BaseDB) BatchInsertUpdate(ctx context.Context,
409409

410410
// BatchUpdateLoopOutSwapCosts updates the swap costs for a batch of loop out
411411
// swaps.
412-
func (b *BaseDB) BatchUpdateLoopOutSwapCosts(ctx context.Context,
412+
func (db *BaseDB) BatchUpdateLoopOutSwapCosts(ctx context.Context,
413413
costs map[lntypes.Hash]SwapCost) error {
414414

415415
writeOpts := NewSqlWriteOpts()
416-
return b.ExecTx(ctx, writeOpts, func(tx *sqlc.Queries) error {
416+
return db.ExecTx(ctx, writeOpts, func(tx *sqlc.Queries) error {
417417
for swapHash, cost := range costs {
418418
lastUpdateID, err := tx.GetLastUpdateID(
419419
ctx, swapHash[:],
@@ -440,10 +440,10 @@ func (b *BaseDB) BatchUpdateLoopOutSwapCosts(ctx context.Context,
440440
}
441441

442442
// HasMigration returns true if the migration with the given ID has been done.
443-
func (b *BaseDB) HasMigration(ctx context.Context, migrationID string) (
443+
func (db *BaseDB) HasMigration(ctx context.Context, migrationID string) (
444444
bool, error) {
445445

446-
migration, err := b.GetMigration(ctx, migrationID)
446+
migration, err := db.GetMigration(ctx, migrationID)
447447
if err != nil && !errors.Is(err, sql.ErrNoRows) {
448448
return false, err
449449
}
@@ -452,8 +452,8 @@ func (b *BaseDB) HasMigration(ctx context.Context, migrationID string) (
452452
}
453453

454454
// SetMigration marks the migration with the given ID as done.
455-
func (b *BaseDB) SetMigration(ctx context.Context, migrationID string) error {
456-
return b.InsertMigration(ctx, sqlc.InsertMigrationParams{
455+
func (db *BaseDB) SetMigration(ctx context.Context, migrationID string) error {
456+
return db.InsertMigration(ctx, sqlc.InsertMigrationParams{
457457
MigrationID: migrationID,
458458
MigrationTs: sql.NullTime{
459459
Time: time.Now().UTC(),
@@ -627,7 +627,7 @@ func ConvertLoopOutRow(network *chaincfg.Params, row sqlc.GetLoopOutSwapRow,
627627

628628
// convertLoopInRow converts a database row containing a loop in swap to a
629629
// LoopIn struct.
630-
func (s *BaseDB) convertLoopInRow(row sqlc.GetLoopInSwapsRow,
630+
func (db *BaseDB) convertLoopInRow(row sqlc.GetLoopInSwapsRow,
631631
updates []sqlc.SwapUpdate) (*LoopIn, error) {
632632

633633
htlcKeys, err := fetchHtlcKeys(

loopdb/sqlite.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -224,9 +224,9 @@ func (db *BaseDB) ExecTx(ctx context.Context, txOptions TxOptions,
224224

225225
// FixFaultyTimestamps fixes faulty timestamps in the database, caused
226226
// by using milliseconds instead of seconds as the publication deadline.
227-
func (b *BaseDB) FixFaultyTimestamps(ctx context.Context) error {
227+
func (db *BaseDB) FixFaultyTimestamps(ctx context.Context) error {
228228
// Manually fetch all the loop out swaps.
229-
rows, err := b.DB.QueryContext(
229+
rows, err := db.DB.QueryContext(
230230
ctx, "SELECT swap_hash, swap_invoice, publication_deadline FROM loopout_swaps",
231231
)
232232
if err != nil {
@@ -262,7 +262,7 @@ func (b *BaseDB) FixFaultyTimestamps(ctx context.Context) error {
262262
return err
263263
}
264264

265-
tx, err := b.BeginTx(ctx, &SqliteTxOptions{})
265+
tx, err := db.BeginTx(ctx, &SqliteTxOptions{})
266266
if err != nil {
267267
return err
268268
}
@@ -283,7 +283,7 @@ func (b *BaseDB) FixFaultyTimestamps(ctx context.Context) error {
283283
continue
284284
}
285285

286-
payReq, err := zpay32.Decode(swap.SwapInvoice, b.network)
286+
payReq, err := zpay32.Decode(swap.SwapInvoice, db.network)
287287
if err != nil {
288288
return err
289289
}

0 commit comments

Comments
 (0)