Skip to content

Commit ed9c22f

Browse files
committed
sweepbatcher: test that empty batches won't prevent startup
1 parent c6c5f89 commit ed9c22f

File tree

2 files changed

+90
-0
lines changed

2 files changed

+90
-0
lines changed

sweepbatcher/sweep_batcher.go

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -154,6 +154,10 @@ type Batcher struct {
154154
// quit signals that the batch must stop.
155155
quit chan struct{}
156156

157+
// initDone is a channel that is closed when the batcher has been
158+
// initialized.
159+
initDone chan struct{}
160+
157161
// wallet is the wallet kit client that is used by batches.
158162
wallet lndclient.WalletKitClient
159163

@@ -201,6 +205,7 @@ func NewBatcher(wallet lndclient.WalletKitClient,
201205
sweepReqs: make(chan SweepRequest),
202206
errChan: make(chan error, 1),
203207
quit: make(chan struct{}),
208+
initDone: make(chan struct{}),
204209
wallet: wallet,
205210
chainNotifier: chainNotifier,
206211
signerClient: signerClient,
@@ -240,6 +245,9 @@ func (b *Batcher) Run(ctx context.Context) error {
240245
}
241246
}
242247

248+
// Signal that the batcher has been initialized.
249+
close(b.initDone)
250+
243251
for {
244252
select {
245253
case sweepReq := <-b.sweepReqs:

sweepbatcher/sweep_batcher_test.go

Lines changed: 82 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ package sweepbatcher
33
import (
44
"context"
55
"strings"
6+
"sync"
67
"testing"
78
"time"
89

@@ -1029,3 +1030,84 @@ func TestGetFeePortionForSweep(t *testing.T) {
10291030
})
10301031
}
10311032
}
1033+
1034+
// TestRestoringEmptyBatch tests that the batcher can be restored with an empty
1035+
// batch.
1036+
func TestRestoringEmptyBatch(t *testing.T) {
1037+
defer test.Guard(t)()
1038+
1039+
lnd := test.NewMockLnd()
1040+
ctx, cancel := context.WithCancel(context.Background())
1041+
1042+
store := loopdb.NewStoreMock(t)
1043+
1044+
batcherStore := NewStoreMock()
1045+
_, err := batcherStore.InsertSweepBatch(ctx, &dbBatch{})
1046+
require.NoError(t, err)
1047+
1048+
batcher := NewBatcher(lnd.WalletKit, lnd.ChainNotifier, lnd.Signer,
1049+
testMuSig2SignSweep, nil, lnd.ChainParams, batcherStore, store)
1050+
1051+
var wg sync.WaitGroup
1052+
wg.Add(1)
1053+
1054+
var runErr error
1055+
go func() {
1056+
defer wg.Done()
1057+
runErr = batcher.Run(ctx)
1058+
}()
1059+
1060+
// Wait for the batcher to be initialized.
1061+
<-batcher.initDone
1062+
1063+
// Create a sweep request.
1064+
sweepReq := SweepRequest{
1065+
SwapHash: lntypes.Hash{1, 1, 1},
1066+
Value: 111,
1067+
Outpoint: wire.OutPoint{
1068+
Hash: chainhash.Hash{1, 1},
1069+
Index: 1,
1070+
},
1071+
Notifier: &dummyNotifier,
1072+
}
1073+
1074+
swap := &loopdb.LoopOutContract{
1075+
SwapContract: loopdb.SwapContract{
1076+
CltvExpiry: 111,
1077+
AmountRequested: 111,
1078+
},
1079+
1080+
SwapInvoice: swapInvoice,
1081+
}
1082+
1083+
err = store.CreateLoopOut(ctx, sweepReq.SwapHash, swap)
1084+
require.NoError(t, err)
1085+
store.AssertLoopOutStored()
1086+
1087+
// Deliver sweep request to batcher.
1088+
batcher.sweepReqs <- sweepReq
1089+
1090+
// Since a batch was created we check that it registered for its primary
1091+
// sweep's spend.
1092+
<-lnd.RegisterSpendChannel
1093+
1094+
// Once batcher receives sweep request it will eventually spin up a
1095+
// batch.
1096+
require.Eventually(t, func() bool {
1097+
// Make sure that the sweep was stored and we have exactly one
1098+
// active batch.
1099+
return batcherStore.AssertSweepStored(sweepReq.SwapHash) &&
1100+
len(batcher.batches) == 1
1101+
}, test.Timeout, eventuallyCheckFrequency)
1102+
1103+
// Make sure we have two batches stored.
1104+
batches, err := batcherStore.FetchUnconfirmedSweepBatches(ctx)
1105+
require.NoError(t, err)
1106+
require.Len(t, batches, 2)
1107+
1108+
// Now make it quit by canceling the context.
1109+
cancel()
1110+
wg.Wait()
1111+
1112+
require.Error(t, context.Canceled, runErr)
1113+
}

0 commit comments

Comments
 (0)