From d38b7c55a7bf2aa547c70d536e97e31bf3c25287 Mon Sep 17 00:00:00 2001 From: Boris Nagaev Date: Wed, 29 May 2024 15:03:37 -0300 Subject: [PATCH] sweepbatcher/StoreMock: load LoopOut from loopdb Method sweepbatcher.Store.FetchBatchSweeps (implementation using real DB) runs JOIN query to load LoopOut from swaps table. Now the mock does the same. It is needed to test store and load scenarios in tests. --- loopout_test.go | 6 +++--- sweepbatcher/store_mock.go | 29 ++++++++++++++++++++++------- sweepbatcher/sweep_batcher_test.go | 14 +++++++------- testcontext_test.go | 2 +- 4 files changed, 33 insertions(+), 18 deletions(-) diff --git a/loopout_test.go b/loopout_test.go index dfbeb620..7e5045cb 100644 --- a/loopout_test.go +++ b/loopout_test.go @@ -296,7 +296,7 @@ func testCustomSweepConfTarget(t *testing.T) { errChan := make(chan error, 2) - batcherStore := sweepbatcher.NewStoreMock() + batcherStore := sweepbatcher.NewStoreMock(cfg.store) batcher := sweepbatcher.NewBatcher( lnd.WalletKit, lnd.ChainNotifier, lnd.Signer, @@ -529,7 +529,7 @@ func testPreimagePush(t *testing.T) { errChan := make(chan error, 2) - batcherStore := sweepbatcher.NewStoreMock() + batcherStore := sweepbatcher.NewStoreMock(cfg.store) batcher := sweepbatcher.NewBatcher( lnd.WalletKit, lnd.ChainNotifier, lnd.Signer, @@ -950,7 +950,7 @@ func TestLoopOutMuSig2Sweep(t *testing.T) { errChan := make(chan error, 2) - batcherStore := sweepbatcher.NewStoreMock() + batcherStore := sweepbatcher.NewStoreMock(cfg.store) batcher := sweepbatcher.NewBatcher( lnd.WalletKit, lnd.ChainNotifier, lnd.Signer, diff --git a/sweepbatcher/store_mock.go b/sweepbatcher/store_mock.go index 57cdd34b..96d5dcf4 100644 --- a/sweepbatcher/store_mock.go +++ b/sweepbatcher/store_mock.go @@ -3,6 +3,7 @@ package sweepbatcher import ( "context" "errors" + "fmt" "sort" "github.com/btcsuite/btcd/btcutil" @@ -11,15 +12,17 @@ import ( // StoreMock implements a mock client swap store. type StoreMock struct { - batches map[int32]dbBatch - sweeps map[lntypes.Hash]dbSweep + batches map[int32]dbBatch + sweeps map[lntypes.Hash]dbSweep + swapStore LoopOutFetcher } // NewStoreMock instantiates a new mock store. -func NewStoreMock() *StoreMock { +func NewStoreMock(swapStore LoopOutFetcher) *StoreMock { return &StoreMock{ - batches: make(map[int32]dbBatch), - sweeps: make(map[lntypes.Hash]dbSweep), + batches: make(map[int32]dbBatch), + sweeps: make(map[lntypes.Hash]dbSweep), + swapStore: swapStore, } } @@ -90,9 +93,21 @@ func (s *StoreMock) FetchBatchSweeps(ctx context.Context, result := []*dbSweep{} for _, sweep := range s.sweeps { sweep := sweep - if sweep.BatchID == id { - result = append(result, &sweep) + if sweep.BatchID != id { + continue } + + // Load swap from loopdb. + swap, err := s.swapStore.FetchLoopOutSwap( + ctx, sweep.SwapHash, + ) + if err != nil { + return nil, fmt.Errorf("failed to fetch swap "+ + "for SwapHash=%v", sweep.SwapHash) + } + sweep.LoopOut = swap + + result = append(result, &sweep) } sort.Slice(result, func(i, j int) bool { diff --git a/sweepbatcher/sweep_batcher_test.go b/sweepbatcher/sweep_batcher_test.go index afa7ab97..67528c71 100644 --- a/sweepbatcher/sweep_batcher_test.go +++ b/sweepbatcher/sweep_batcher_test.go @@ -64,7 +64,7 @@ func TestSweepBatcherBatchCreation(t *testing.T) { store := loopdb.NewStoreMock(t) - batcherStore := NewStoreMock() + batcherStore := NewStoreMock(store) batcher := NewBatcher(lnd.WalletKit, lnd.ChainNotifier, lnd.Signer, testMuSig2SignSweep, nil, lnd.ChainParams, batcherStore, store) @@ -218,7 +218,7 @@ func TestSweepBatcherSimpleLifecycle(t *testing.T) { store := loopdb.NewStoreMock(t) - batcherStore := NewStoreMock() + batcherStore := NewStoreMock(store) batcher := NewBatcher(lnd.WalletKit, lnd.ChainNotifier, lnd.Signer, testMuSig2SignSweep, nil, lnd.ChainParams, batcherStore, store) @@ -355,7 +355,7 @@ func TestSweepBatcherSweepReentry(t *testing.T) { store := loopdb.NewStoreMock(t) - batcherStore := NewStoreMock() + batcherStore := NewStoreMock(store) batcher := NewBatcher(lnd.WalletKit, lnd.ChainNotifier, lnd.Signer, testMuSig2SignSweep, nil, lnd.ChainParams, batcherStore, store) @@ -562,7 +562,7 @@ func TestSweepBatcherNonWalletAddr(t *testing.T) { store := loopdb.NewStoreMock(t) - batcherStore := NewStoreMock() + batcherStore := NewStoreMock(store) batcher := NewBatcher(lnd.WalletKit, lnd.ChainNotifier, lnd.Signer, testMuSig2SignSweep, nil, lnd.ChainParams, batcherStore, store) @@ -727,7 +727,7 @@ func TestSweepBatcherComposite(t *testing.T) { store := loopdb.NewStoreMock(t) - batcherStore := NewStoreMock() + batcherStore := NewStoreMock(store) batcher := NewBatcher(lnd.WalletKit, lnd.ChainNotifier, lnd.Signer, testMuSig2SignSweep, nil, lnd.ChainParams, batcherStore, store) @@ -1044,7 +1044,7 @@ func TestRestoringEmptyBatch(t *testing.T) { store := loopdb.NewStoreMock(t) - batcherStore := NewStoreMock() + batcherStore := NewStoreMock(store) _, err := batcherStore.InsertSweepBatch(ctx, &dbBatch{}) require.NoError(t, err) @@ -1158,7 +1158,7 @@ func TestHandleSweepTwice(t *testing.T) { store := newLoopStoreMock() - batcherStore := NewStoreMock() + batcherStore := NewStoreMock(store) batcher := NewBatcher(lnd.WalletKit, lnd.ChainNotifier, lnd.Signer, testMuSig2SignSweep, nil, lnd.ChainParams, batcherStore, store) diff --git a/testcontext_test.go b/testcontext_test.go index 423eb31d..9f8f120d 100644 --- a/testcontext_test.go +++ b/testcontext_test.go @@ -77,7 +77,7 @@ func newSwapClient(config *clientConfig) *Client { lndServices := config.LndServices - batcherStore := sweepbatcher.NewStoreMock() + batcherStore := sweepbatcher.NewStoreMock(config.Store) batcher := sweepbatcher.NewBatcher( config.LndServices.WalletKit, config.LndServices.ChainNotifier,