Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

sweepbatcher: add method DryRun #822

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
31 changes: 31 additions & 0 deletions sweepbatcher/sweep_batcher.go
Original file line number Diff line number Diff line change
Expand Up @@ -517,6 +517,37 @@ func NewBatcher(wallet lndclient.WalletKitClient,
}
}

// DryRun tests if the batcher can start, but does not start it.
// This method is useful to check if dependency databases are ready.
func (b *Batcher) DryRun(ctx context.Context) error {
// First we fetch all the batches that are not in a confirmed state from
// the database. We will then load their sweeps to make sure it works.
batches, err := b.FetchUnconfirmedBatches(ctx)
if err != nil {
return fmt.Errorf("b.FetchUnconfirmedBatches failed: %w", err)
}

for _, batch := range batches {
dbSweeps, err := b.store.FetchBatchSweeps(ctx, batch.id)
if err != nil {
return fmt.Errorf("store.FetchBatchSweeps failed: %w",
err)
}

for _, dbSweep := range dbSweeps {
swapHash := dbSweep.SwapHash
_, err = b.sweepStore.FetchSweep(ctx, swapHash)
if err != nil {
return fmt.Errorf("failed to fetch sweep "+
"data for %x: %w", swapHash[:6], err)
}
}
}

// Everything was loaded successfully.
return nil
}

// Run starts the batcher and processes incoming sweep requests.
func (b *Batcher) Run(ctx context.Context) error {
runCtx, cancel := context.WithCancel(ctx)
Expand Down
16 changes: 16 additions & 0 deletions sweepbatcher/sweep_batcher_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2810,11 +2810,16 @@ func testRestoringPreservesConfTarget(t *testing.T, store testStore,

type sweepFetcherMock struct {
store map[lntypes.Hash]*SweepInfo
err error
}

func (f *sweepFetcherMock) FetchSweep(ctx context.Context, hash lntypes.Hash) (
*SweepInfo, error) {

if f.err != nil {
return nil, f.err
}

return f.store[hash], nil
}

Expand Down Expand Up @@ -2972,6 +2977,17 @@ func testSweepFetcher(t *testing.T, store testStore,

// Make sure the batcher exited without an error.
checkBatcherError(t, runErr)

// Check DryRun method.
batcher2 := NewBatcher(lnd.WalletKit, lnd.ChainNotifier, lnd.Signer,
nil, testVerifySchnorrSig, lnd.ChainParams,
batcherStore, sweepFetcher, WithCustomFeeRate(customFeeRate),
WithCustomSignMuSig2(testSignMuSig2func))
ctx = context.Background()
sweepFetcher.err = errors.New("test FetchSweep error")
require.ErrorIs(t, batcher2.DryRun(ctx), sweepFetcher.err)
sweepFetcher.err = nil
require.NoError(t, batcher2.DryRun(ctx))
}

// testSweepBatcherCloseDuringAdding tests that sweep batcher works correctly
Expand Down
Loading