From c5ca4a8af09836dc78fbe32c13db8528e62a2342 Mon Sep 17 00:00:00 2001 From: Boris Nagaev Date: Thu, 5 Sep 2024 13:20:29 -0300 Subject: [PATCH] sweepbatcher: add method DryRun DryRun tests if the batcher can start, but does not start it. This method is useful to check if dependency databases are ready. --- sweepbatcher/sweep_batcher.go | 31 ++++++++++++++++++++++++++++++ sweepbatcher/sweep_batcher_test.go | 16 +++++++++++++++ 2 files changed, 47 insertions(+) diff --git a/sweepbatcher/sweep_batcher.go b/sweepbatcher/sweep_batcher.go index 6b819e433..56523213b 100644 --- a/sweepbatcher/sweep_batcher.go +++ b/sweepbatcher/sweep_batcher.go @@ -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) diff --git a/sweepbatcher/sweep_batcher_test.go b/sweepbatcher/sweep_batcher_test.go index 11c56143b..2449c4f2d 100644 --- a/sweepbatcher/sweep_batcher_test.go +++ b/sweepbatcher/sweep_batcher_test.go @@ -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 } @@ -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