mirror of
https://github.com/lightninglabs/loop
synced 2024-11-09 19:10:47 +00:00
Merge pull request #789 from bhandras/sweep-batcher-db-parent-batch-fixup
sweepbatcher: fix spawning condition "unattached" spend notifiers
This commit is contained in:
commit
e75ebaee86
@ -102,10 +102,6 @@ JOIN
|
|||||||
sweeps ON sweep_batches.id = sweeps.batch_id
|
sweeps ON sweep_batches.id = sweeps.batch_id
|
||||||
WHERE
|
WHERE
|
||||||
sweeps.swap_hash = $1
|
sweeps.swap_hash = $1
|
||||||
AND
|
|
||||||
sweeps.completed = TRUE
|
|
||||||
AND
|
|
||||||
sweep_batches.confirmed = TRUE
|
|
||||||
`
|
`
|
||||||
|
|
||||||
func (q *Queries) GetParentBatch(ctx context.Context, swapHash []byte) (SweepBatch, error) {
|
func (q *Queries) GetParentBatch(ctx context.Context, swapHash []byte) (SweepBatch, error) {
|
||||||
|
@ -73,11 +73,7 @@ FROM
|
|||||||
JOIN
|
JOIN
|
||||||
sweeps ON sweep_batches.id = sweeps.batch_id
|
sweeps ON sweep_batches.id = sweeps.batch_id
|
||||||
WHERE
|
WHERE
|
||||||
sweeps.swap_hash = $1
|
sweeps.swap_hash = $1;
|
||||||
AND
|
|
||||||
sweeps.completed = TRUE
|
|
||||||
AND
|
|
||||||
sweep_batches.confirmed = TRUE;
|
|
||||||
|
|
||||||
-- name: GetBatchSweptAmount :one
|
-- name: GetBatchSweptAmount :one
|
||||||
SELECT
|
SELECT
|
||||||
|
@ -196,10 +196,6 @@ func (s *SQLStore) GetParentBatch(ctx context.Context, swapHash lntypes.Hash) (
|
|||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
|
|
||||||
if err != nil {
|
|
||||||
return nil, err
|
|
||||||
}
|
|
||||||
|
|
||||||
return convertBatchRow(batch), nil
|
return convertBatchRow(batch), nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -418,7 +418,24 @@ func (b *Batcher) handleSweep(ctx context.Context, sweep *sweep,
|
|||||||
// can't attach its notifier to the batch as that is no longer running.
|
// can't attach its notifier to the batch as that is no longer running.
|
||||||
// Instead we directly detect and return the spend here.
|
// Instead we directly detect and return the spend here.
|
||||||
if completed && *notifier != (SpendNotifier{}) {
|
if completed && *notifier != (SpendNotifier{}) {
|
||||||
return b.monitorSpendAndNotify(ctx, sweep, notifier)
|
// Verify that the parent batch is confirmed. Note that a batch
|
||||||
|
// is only considered confirmed after it has received three
|
||||||
|
// on-chain confirmations to prevent issues caused by reorgs.
|
||||||
|
parentBatch, err := b.store.GetParentBatch(ctx, sweep.swapHash)
|
||||||
|
if err != nil {
|
||||||
|
log.Errorf("unable to get parent batch for sweep %x: "+
|
||||||
|
"%v", sweep.swapHash[:6], err)
|
||||||
|
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
// The parent batch is indeed confirmed, meaning it is complete
|
||||||
|
// and we won't be able to attach this sweep to it.
|
||||||
|
if parentBatch.State == batchConfirmed {
|
||||||
|
return b.monitorSpendAndNotify(
|
||||||
|
ctx, sweep, parentBatch.ID, notifier,
|
||||||
|
)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
sweep.notifier = notifier
|
sweep.notifier = notifier
|
||||||
@ -688,19 +705,13 @@ func (b *Batcher) FetchUnconfirmedBatches(ctx context.Context) ([]*batch,
|
|||||||
// monitorSpendAndNotify monitors the spend of a specific outpoint and writes
|
// monitorSpendAndNotify monitors the spend of a specific outpoint and writes
|
||||||
// the response back to the response channel.
|
// the response back to the response channel.
|
||||||
func (b *Batcher) monitorSpendAndNotify(ctx context.Context, sweep *sweep,
|
func (b *Batcher) monitorSpendAndNotify(ctx context.Context, sweep *sweep,
|
||||||
notifier *SpendNotifier) error {
|
parentBatchID int32, notifier *SpendNotifier) error {
|
||||||
|
|
||||||
spendCtx, cancel := context.WithCancel(ctx)
|
spendCtx, cancel := context.WithCancel(ctx)
|
||||||
defer cancel()
|
defer cancel()
|
||||||
|
|
||||||
// First get the batch that completed the sweep.
|
|
||||||
parentBatch, err := b.store.GetParentBatch(ctx, sweep.swapHash)
|
|
||||||
if err != nil {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
|
|
||||||
// Then we get the total amount that was swept by the batch.
|
// Then we get the total amount that was swept by the batch.
|
||||||
totalSwept, err := b.store.TotalSweptAmount(ctx, parentBatch.ID)
|
totalSwept, err := b.store.TotalSweptAmount(ctx, parentBatchID)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user