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.
This commit is contained in:
Boris Nagaev 2024-05-29 15:03:37 -03:00
parent 22dd2e8bd1
commit d38b7c55a7
No known key found for this signature in database
4 changed files with 33 additions and 18 deletions

View file

@ -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,

View file

@ -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 {

View file

@ -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)

View file

@ -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,