Skip to content

Commit 1036214

Browse files
committed
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.
1 parent 1ab73a6 commit 1036214

16 files changed

+349
-58
lines changed

client.go

Lines changed: 28 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ import (
1212
"github.com/btcsuite/btcd/btcec/v2"
1313
"github.com/btcsuite/btcd/btcec/v2/schnorr"
1414
"github.com/btcsuite/btcd/btcutil"
15+
"github.com/btcsuite/btcd/chaincfg/chainhash"
1516
"github.com/lightninglabs/aperture/l402"
1617
"github.com/lightninglabs/lndclient"
1718
"github.com/lightninglabs/loop/assets"
@@ -146,6 +147,10 @@ type ClientConfig struct {
146147
// be attempted.
147148
LoopOutMaxParts uint32
148149

150+
// SkippedTxns is the list of existing HTLC txids to skip when starting
151+
// Loop. This should only be used if affected by the historical bug.
152+
SkippedTxns []string
153+
149154
// TotalPaymentTimeout is the total amount of time until we time out
150155
// off-chain payments (used in loop out).
151156
TotalPaymentTimeout time.Duration
@@ -246,11 +251,7 @@ func NewClient(dbDir string, loopDB loopdb.SwapStore,
246251
sweeper, loopDB, cfg.Lnd.ChainParams, getHeight,
247252
)
248253

249-
batcher := sweepbatcher.NewBatcher(
250-
cfg.Lnd.WalletKit, cfg.Lnd.ChainNotifier, cfg.Lnd.Signer,
251-
swapServerClient.MultiMuSig2SignSweep, verifySchnorrSig,
252-
cfg.Lnd.ChainParams, sweeperDb, sweepStore,
253-
254+
batcherOpts := []sweepbatcher.BatcherOption{
254255
// Disable 100 sats/kw fee bump every block and retarget feerate
255256
// every block according to the current mempool condition.
256257
sweepbatcher.WithCustomFeeRate(
@@ -265,8 +266,29 @@ func NewClient(dbDir string, loopDB loopdb.SwapStore,
265266
// delay time to sweepbatcher's handling. The delay used in
266267
// loopout.go is repushDelay.
267268
sweepbatcher.WithPublishDelay(
268-
repushDelay+additionalDelay,
269+
repushDelay + additionalDelay,
269270
),
271+
}
272+
273+
if len(cfg.SkippedTxns) != 0 {
274+
skippedTxns := make(map[chainhash.Hash]struct{})
275+
for _, txid := range cfg.SkippedTxns {
276+
txid, err := chainhash.NewHashFromStr(txid)
277+
if err != nil {
278+
return nil, nil, fmt.Errorf("failed to parse "+
279+
"txid to skip %v: %w", txid, err)
280+
}
281+
skippedTxns[*txid] = struct{}{}
282+
}
283+
batcherOpts = append(batcherOpts, sweepbatcher.WithSkippedTxns(
284+
skippedTxns,
285+
))
286+
}
287+
288+
batcher := sweepbatcher.NewBatcher(
289+
cfg.Lnd.WalletKit, cfg.Lnd.ChainNotifier, cfg.Lnd.Signer,
290+
swapServerClient.MultiMuSig2SignSweep, verifySchnorrSig,
291+
cfg.Lnd.ChainParams, sweeperDb, sweepStore, batcherOpts...,
270292
)
271293

272294
executor = newExecutor(&executorConfig{

loopd/config.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -184,6 +184,8 @@ type Config struct {
184184

185185
LoopOutMaxParts uint32 `long:"loopoutmaxparts" description:"The maximum number of payment parts that may be used for a loop out swap."`
186186

187+
SkippedTxns []string `long:"skippedtxns" description:"The list of existing HTLC txids to skip when starting Loop. This should only be used if affected by the historical bug." hidden:"true"`
188+
187189
TotalPaymentTimeout time.Duration `long:"totalpaymenttimeout" description:"The timeout to use for off-chain payments."`
188190
MaxPaymentRetries int `long:"maxpaymentretries" description:"The maximum number of times an off-chain payment may be retried."`
189191

loopd/utils.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,7 @@ func getClient(cfg *Config, swapDb loopdb.SwapStore,
5050
MaxL402Cost: btcutil.Amount(cfg.MaxL402Cost),
5151
MaxL402Fee: btcutil.Amount(cfg.MaxL402Fee),
5252
LoopOutMaxParts: cfg.LoopOutMaxParts,
53+
SkippedTxns: cfg.SkippedTxns,
5354
TotalPaymentTimeout: cfg.TotalPaymentTimeout,
5455
MaxPaymentRetries: cfg.MaxPaymentRetries,
5556
MaxStaticAddrHtlcFeePercentage: cfg.MaxStaticAddrHtlcFeePercentage,

loopdb/sqlc/batch.sql.go

Lines changed: 16 additions & 12 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
ALTER TABLE sweep_batches DROP COLUMN cancelled;
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
ALTER TABLE sweep_batches ADD COLUMN cancelled BOOLEAN NOT NULL DEFAULT FALSE;

loopdb/sqlc/models.go

Lines changed: 1 addition & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

loopdb/sqlc/querier.go

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

loopdb/sqlc/queries/batch.sql

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ SELECT
44
FROM
55
sweep_batches
66
WHERE
7-
confirmed = FALSE;
7+
confirmed = FALSE AND cancelled = FALSE;
88

99
-- name: InsertBatch :one
1010
INSERT INTO sweep_batches (
@@ -23,8 +23,10 @@ INSERT INTO sweep_batches (
2323
$6
2424
) RETURNING id;
2525

26-
-- name: DropBatch :exec
27-
DELETE FROM sweep_batches WHERE id = $1;
26+
-- name: CancelBatch :exec
27+
UPDATE sweep_batches SET
28+
cancelled = TRUE
29+
WHERE id = $1;
2830

2931
-- name: UpdateBatch :exec
3032
UPDATE sweep_batches SET

sweepbatcher/presigned.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -126,6 +126,8 @@ func (b *batch) getOrderedSweeps(ctx context.Context) ([]sweep, error) {
126126
return nil, fmt.Errorf("FetchBatchSweeps(%d) failed: %w", b.id,
127127
err)
128128
}
129+
dbSweeps = filterDbSweeps(b.cfg.skippedTxns, dbSweeps)
130+
129131
if len(dbSweeps) != len(utxo2sweep) {
130132
return nil, fmt.Errorf("FetchBatchSweeps(%d) returned %d "+
131133
"sweeps, len(b.sweeps) is %d", b.id, len(dbSweeps),

0 commit comments

Comments
 (0)