sweepbatcher: add an option to ignore HTLC txids

Added option WithSkippedTxns, which has one historical problematic tx by default.
Sweeps originating from these transactions are omitted when reading from DB.

loopdb: add column sweep_batches.cancelled and replaced DropBatch with
CancelBatch. It is needed, because sweep.batch_id is a foreign key to batch.

Changed StoreMock.InsertSweepBatch not to reuse batch_id. This is needed by
the test, which checks that new batch has fresh ID.
This commit is contained in:
Boris Nagaev 2025-06-17 14:01:59 -03:00
parent 1ab73a6272
commit 1036214160
No known key found for this signature in database
16 changed files with 349 additions and 58 deletions

View file

@ -10,6 +10,17 @@ import (
"database/sql"
)
const cancelBatch = `-- name: CancelBatch :exec
UPDATE sweep_batches SET
cancelled = TRUE
WHERE id = $1
`
func (q *Queries) CancelBatch(ctx context.Context, id int32) error {
_, err := q.db.ExecContext(ctx, cancelBatch, id)
return err
}
const confirmBatch = `-- name: ConfirmBatch :exec
UPDATE
sweep_batches
@ -24,15 +35,6 @@ 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
id, swap_hash, batch_id, outpoint, amt, completed
@ -94,7 +96,7 @@ func (q *Queries) GetBatchSweptAmount(ctx context.Context, batchID int32) (int64
const getParentBatch = `-- name: GetParentBatch :one
SELECT
sweep_batches.id, sweep_batches.confirmed, sweep_batches.batch_tx_id, sweep_batches.batch_pk_script, sweep_batches.last_rbf_height, sweep_batches.last_rbf_sat_per_kw, sweep_batches.max_timeout_distance
sweep_batches.id, sweep_batches.confirmed, sweep_batches.batch_tx_id, sweep_batches.batch_pk_script, sweep_batches.last_rbf_height, sweep_batches.last_rbf_sat_per_kw, sweep_batches.max_timeout_distance, sweep_batches.cancelled
FROM
sweep_batches
JOIN
@ -114,6 +116,7 @@ func (q *Queries) GetParentBatch(ctx context.Context, outpoint string) (SweepBat
&i.LastRbfHeight,
&i.LastRbfSatPerKw,
&i.MaxTimeoutDistance,
&i.Cancelled,
)
return i, err
}
@ -136,11 +139,11 @@ func (q *Queries) GetSweepStatus(ctx context.Context, outpoint string) (bool, er
const getUnconfirmedBatches = `-- name: GetUnconfirmedBatches :many
SELECT
id, confirmed, batch_tx_id, batch_pk_script, last_rbf_height, last_rbf_sat_per_kw, max_timeout_distance
id, confirmed, batch_tx_id, batch_pk_script, last_rbf_height, last_rbf_sat_per_kw, max_timeout_distance, cancelled
FROM
sweep_batches
WHERE
confirmed = FALSE
confirmed = FALSE AND cancelled = FALSE
`
func (q *Queries) GetUnconfirmedBatches(ctx context.Context) ([]SweepBatch, error) {
@ -160,6 +163,7 @@ func (q *Queries) GetUnconfirmedBatches(ctx context.Context) ([]SweepBatch, erro
&i.LastRbfHeight,
&i.LastRbfSatPerKw,
&i.MaxTimeoutDistance,
&i.Cancelled,
); err != nil {
return nil, err
}

View file

@ -0,0 +1 @@
ALTER TABLE sweep_batches DROP COLUMN cancelled;

View file

@ -0,0 +1 @@
ALTER TABLE sweep_batches ADD COLUMN cancelled BOOLEAN NOT NULL DEFAULT FALSE;

View file

@ -197,6 +197,7 @@ type SweepBatch struct {
LastRbfHeight sql.NullInt32
LastRbfSatPerKw sql.NullInt32
MaxTimeoutDistance int32
Cancelled bool
}
type SweepsOld struct {

View file

@ -12,13 +12,13 @@ import (
type Querier interface {
AllDeposits(ctx context.Context) ([]Deposit, error)
AllStaticAddresses(ctx context.Context) ([]StaticAddress, error)
CancelBatch(ctx context.Context, id int32) error
ConfirmBatch(ctx context.Context, id int32) error
CreateDeposit(ctx context.Context, arg CreateDepositParams) error
CreateReservation(ctx context.Context, arg CreateReservationParams) error
CreateStaticAddress(ctx context.Context, arg CreateStaticAddressParams) error
CreateWithdrawal(ctx context.Context, arg CreateWithdrawalParams) error
CreateWithdrawalDeposit(ctx context.Context, arg CreateWithdrawalDepositParams) error
DropBatch(ctx context.Context, id int32) error
FetchLiquidityParams(ctx context.Context) ([]byte, error)
GetAllWithdrawals(ctx context.Context) ([]Withdrawal, error)
GetBatchSweeps(ctx context.Context, batchID int32) ([]Sweep, error)

View file

@ -4,7 +4,7 @@ SELECT
FROM
sweep_batches
WHERE
confirmed = FALSE;
confirmed = FALSE AND cancelled = FALSE;
-- name: InsertBatch :one
INSERT INTO sweep_batches (
@ -23,8 +23,10 @@ INSERT INTO sweep_batches (
$6
) RETURNING id;
-- name: DropBatch :exec
DELETE FROM sweep_batches WHERE id = $1;
-- name: CancelBatch :exec
UPDATE sweep_batches SET
cancelled = TRUE
WHERE id = $1;
-- name: UpdateBatch :exec
UPDATE sweep_batches SET