Skip to content

Commit d38b7c5

Browse files
committed
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.
1 parent 22dd2e8 commit d38b7c5

File tree

4 files changed

+33
-18
lines changed

4 files changed

+33
-18
lines changed

loopout_test.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -296,7 +296,7 @@ func testCustomSweepConfTarget(t *testing.T) {
296296

297297
errChan := make(chan error, 2)
298298

299-
batcherStore := sweepbatcher.NewStoreMock()
299+
batcherStore := sweepbatcher.NewStoreMock(cfg.store)
300300

301301
batcher := sweepbatcher.NewBatcher(
302302
lnd.WalletKit, lnd.ChainNotifier, lnd.Signer,
@@ -529,7 +529,7 @@ func testPreimagePush(t *testing.T) {
529529

530530
errChan := make(chan error, 2)
531531

532-
batcherStore := sweepbatcher.NewStoreMock()
532+
batcherStore := sweepbatcher.NewStoreMock(cfg.store)
533533

534534
batcher := sweepbatcher.NewBatcher(
535535
lnd.WalletKit, lnd.ChainNotifier, lnd.Signer,
@@ -950,7 +950,7 @@ func TestLoopOutMuSig2Sweep(t *testing.T) {
950950

951951
errChan := make(chan error, 2)
952952

953-
batcherStore := sweepbatcher.NewStoreMock()
953+
batcherStore := sweepbatcher.NewStoreMock(cfg.store)
954954

955955
batcher := sweepbatcher.NewBatcher(
956956
lnd.WalletKit, lnd.ChainNotifier, lnd.Signer,

sweepbatcher/store_mock.go

Lines changed: 22 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ package sweepbatcher
33
import (
44
"context"
55
"errors"
6+
"fmt"
67
"sort"
78

89
"github.com/btcsuite/btcd/btcutil"
@@ -11,15 +12,17 @@ import (
1112

1213
// StoreMock implements a mock client swap store.
1314
type StoreMock struct {
14-
batches map[int32]dbBatch
15-
sweeps map[lntypes.Hash]dbSweep
15+
batches map[int32]dbBatch
16+
sweeps map[lntypes.Hash]dbSweep
17+
swapStore LoopOutFetcher
1618
}
1719

1820
// NewStoreMock instantiates a new mock store.
19-
func NewStoreMock() *StoreMock {
21+
func NewStoreMock(swapStore LoopOutFetcher) *StoreMock {
2022
return &StoreMock{
21-
batches: make(map[int32]dbBatch),
22-
sweeps: make(map[lntypes.Hash]dbSweep),
23+
batches: make(map[int32]dbBatch),
24+
sweeps: make(map[lntypes.Hash]dbSweep),
25+
swapStore: swapStore,
2326
}
2427
}
2528

@@ -90,9 +93,21 @@ func (s *StoreMock) FetchBatchSweeps(ctx context.Context,
9093
result := []*dbSweep{}
9194
for _, sweep := range s.sweeps {
9295
sweep := sweep
93-
if sweep.BatchID == id {
94-
result = append(result, &sweep)
96+
if sweep.BatchID != id {
97+
continue
9598
}
99+
100+
// Load swap from loopdb.
101+
swap, err := s.swapStore.FetchLoopOutSwap(
102+
ctx, sweep.SwapHash,
103+
)
104+
if err != nil {
105+
return nil, fmt.Errorf("failed to fetch swap "+
106+
"for SwapHash=%v", sweep.SwapHash)
107+
}
108+
sweep.LoopOut = swap
109+
110+
result = append(result, &sweep)
96111
}
97112

98113
sort.Slice(result, func(i, j int) bool {

sweepbatcher/sweep_batcher_test.go

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,7 @@ func TestSweepBatcherBatchCreation(t *testing.T) {
6464

6565
store := loopdb.NewStoreMock(t)
6666

67-
batcherStore := NewStoreMock()
67+
batcherStore := NewStoreMock(store)
6868

6969
batcher := NewBatcher(lnd.WalletKit, lnd.ChainNotifier, lnd.Signer,
7070
testMuSig2SignSweep, nil, lnd.ChainParams, batcherStore, store)
@@ -218,7 +218,7 @@ func TestSweepBatcherSimpleLifecycle(t *testing.T) {
218218

219219
store := loopdb.NewStoreMock(t)
220220

221-
batcherStore := NewStoreMock()
221+
batcherStore := NewStoreMock(store)
222222

223223
batcher := NewBatcher(lnd.WalletKit, lnd.ChainNotifier, lnd.Signer,
224224
testMuSig2SignSweep, nil, lnd.ChainParams, batcherStore, store)
@@ -355,7 +355,7 @@ func TestSweepBatcherSweepReentry(t *testing.T) {
355355

356356
store := loopdb.NewStoreMock(t)
357357

358-
batcherStore := NewStoreMock()
358+
batcherStore := NewStoreMock(store)
359359

360360
batcher := NewBatcher(lnd.WalletKit, lnd.ChainNotifier, lnd.Signer,
361361
testMuSig2SignSweep, nil, lnd.ChainParams, batcherStore, store)
@@ -562,7 +562,7 @@ func TestSweepBatcherNonWalletAddr(t *testing.T) {
562562

563563
store := loopdb.NewStoreMock(t)
564564

565-
batcherStore := NewStoreMock()
565+
batcherStore := NewStoreMock(store)
566566

567567
batcher := NewBatcher(lnd.WalletKit, lnd.ChainNotifier, lnd.Signer,
568568
testMuSig2SignSweep, nil, lnd.ChainParams, batcherStore, store)
@@ -727,7 +727,7 @@ func TestSweepBatcherComposite(t *testing.T) {
727727

728728
store := loopdb.NewStoreMock(t)
729729

730-
batcherStore := NewStoreMock()
730+
batcherStore := NewStoreMock(store)
731731

732732
batcher := NewBatcher(lnd.WalletKit, lnd.ChainNotifier, lnd.Signer,
733733
testMuSig2SignSweep, nil, lnd.ChainParams, batcherStore, store)
@@ -1044,7 +1044,7 @@ func TestRestoringEmptyBatch(t *testing.T) {
10441044

10451045
store := loopdb.NewStoreMock(t)
10461046

1047-
batcherStore := NewStoreMock()
1047+
batcherStore := NewStoreMock(store)
10481048
_, err := batcherStore.InsertSweepBatch(ctx, &dbBatch{})
10491049
require.NoError(t, err)
10501050

@@ -1158,7 +1158,7 @@ func TestHandleSweepTwice(t *testing.T) {
11581158

11591159
store := newLoopStoreMock()
11601160

1161-
batcherStore := NewStoreMock()
1161+
batcherStore := NewStoreMock(store)
11621162

11631163
batcher := NewBatcher(lnd.WalletKit, lnd.ChainNotifier, lnd.Signer,
11641164
testMuSig2SignSweep, nil, lnd.ChainParams, batcherStore, store)

testcontext_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -77,7 +77,7 @@ func newSwapClient(config *clientConfig) *Client {
7777

7878
lndServices := config.LndServices
7979

80-
batcherStore := sweepbatcher.NewStoreMock()
80+
batcherStore := sweepbatcher.NewStoreMock(config.Store)
8181

8282
batcher := sweepbatcher.NewBatcher(
8383
config.LndServices.WalletKit, config.LndServices.ChainNotifier,

0 commit comments

Comments
 (0)