From 939c9b4ccf856d0bcdddabfe4eff78130daf697f Mon Sep 17 00:00:00 2001 From: Andras Banki-Horvath Date: Fri, 24 May 2024 16:20:50 +0200 Subject: [PATCH] loopdb+sweepbatcher: add the DropBatch call --- loopdb/sqlc/batch.sql.go | 9 +++++++++ loopdb/sqlc/querier.go | 1 + loopdb/sqlc/queries/batch.sql | 3 +++ sweepbatcher/store.go | 22 ++++++++++++++++++++++ sweepbatcher/store_mock.go | 6 ++++++ 5 files changed, 41 insertions(+) diff --git a/loopdb/sqlc/batch.sql.go b/loopdb/sqlc/batch.sql.go index dae2ad1..c8fab1f 100644 --- a/loopdb/sqlc/batch.sql.go +++ b/loopdb/sqlc/batch.sql.go @@ -25,6 +25,15 @@ func (q *Queries) ConfirmBatch(ctx context.Context, id int32) error { return err } +const dropBatch = `-- name: DropBatch :exec +DELETE FROM sweep_batches WHERE id = $1 +` + +func (q *Queries) DropBatch(ctx context.Context, id int32) error { + _, err := q.db.ExecContext(ctx, dropBatch, id) + return err +} + const getBatchSweeps = `-- name: GetBatchSweeps :many SELECT sweeps.id, sweeps.swap_hash, sweeps.batch_id, sweeps.outpoint_txid, sweeps.outpoint_index, sweeps.amt, sweeps.completed, diff --git a/loopdb/sqlc/querier.go b/loopdb/sqlc/querier.go index b1f0903..d12c6ed 100644 --- a/loopdb/sqlc/querier.go +++ b/loopdb/sqlc/querier.go @@ -11,6 +11,7 @@ import ( type Querier interface { ConfirmBatch(ctx context.Context, id int32) error CreateReservation(ctx context.Context, arg CreateReservationParams) error + DropBatch(ctx context.Context, id int32) error FetchLiquidityParams(ctx context.Context) ([]byte, error) GetBatchSweeps(ctx context.Context, batchID int32) ([]GetBatchSweepsRow, error) GetBatchSweptAmount(ctx context.Context, batchID int32) (int64, error) diff --git a/loopdb/sqlc/queries/batch.sql b/loopdb/sqlc/queries/batch.sql index a9793f9..5951dfe 100644 --- a/loopdb/sqlc/queries/batch.sql +++ b/loopdb/sqlc/queries/batch.sql @@ -23,6 +23,9 @@ INSERT INTO sweep_batches ( $6 ) RETURNING id; +-- name: DropBatch :exec +DELETE FROM sweep_batches WHERE id = $1; + -- name: UpdateBatch :exec UPDATE sweep_batches SET confirmed = $2, diff --git a/sweepbatcher/store.go b/sweepbatcher/store.go index 3fcc32e..bdbcb50 100644 --- a/sweepbatcher/store.go +++ b/sweepbatcher/store.go @@ -3,6 +3,7 @@ package sweepbatcher import ( "context" "database/sql" + "fmt" "github.com/btcsuite/btcd/btcutil" "github.com/btcsuite/btcd/chaincfg" @@ -46,6 +47,9 @@ type BaseDB interface { InsertBatch(ctx context.Context, arg sqlc.InsertBatchParams) ( int32, error) + // DropBatch drops a batch from the database. + DropBatch(ctx context.Context, id int32) error + // UpdateBatch updates a batch in the database. UpdateBatch(ctx context.Context, arg sqlc.UpdateBatchParams) error @@ -108,6 +112,24 @@ func (s *SQLStore) InsertSweepBatch(ctx context.Context, batch *dbBatch) (int32, return s.baseDb.InsertBatch(ctx, batchToInsertArgs(*batch)) } +// DropBatch drops a batch from the database. Note that we only use this call +// for batches that have no sweeps and so we'd not be able to resume. +func (s *SQLStore) DropBatch(ctx context.Context, id int32) error { + readOpts := loopdb.NewSqlReadOpts() + return s.baseDb.ExecTx(ctx, readOpts, func(tx *sqlc.Queries) error { + dbSweeps, err := tx.GetBatchSweeps(ctx, id) + if err != nil { + return err + } + + if len(dbSweeps) != 0 { + return fmt.Errorf("cannot drop a non-empty batch") + } + + return tx.DropBatch(ctx, id) + }) +} + // UpdateSweepBatch updates a batch in the database. func (s *SQLStore) UpdateSweepBatch(ctx context.Context, batch *dbBatch) error { return s.baseDb.UpdateBatch(ctx, batchToUpdateArgs(*batch)) diff --git a/sweepbatcher/store_mock.go b/sweepbatcher/store_mock.go index f27fcb4..0538034 100644 --- a/sweepbatcher/store_mock.go +++ b/sweepbatcher/store_mock.go @@ -56,6 +56,12 @@ func (s *StoreMock) InsertSweepBatch(ctx context.Context, return id, nil } +// DropBatch drops a batch from the database. +func (s *StoreMock) DropBatch(ctx context.Context, id int32) error { + delete(s.batches, id) + return nil +} + // UpdateSweepBatch updates a batch in the database. func (s *StoreMock) UpdateSweepBatch(ctx context.Context, batch *dbBatch) error {