Skip to content

Commit a135eb8

Browse files
authored
Merge pull request #755 from starius/sweepbatcher-changes
sweepbatcher: small refactorings
2 parents 951f981 + 870b60f commit a135eb8

File tree

5 files changed

+34
-27
lines changed

5 files changed

+34
-27
lines changed

sweepbatcher/store.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ type BaseDB interface {
3838
GetSwapUpdates(ctx context.Context, swapHash []byte) (
3939
[]sqlc.SwapUpdate, error)
4040

41-
// FetchUnconfirmedSweepBatches fetches all the batches from the
41+
// GetUnconfirmedBatches fetches all the batches from the
4242
// database that are not in a confirmed state.
4343
GetUnconfirmedBatches(ctx context.Context) ([]sqlc.SweepBatch, error)
4444

sweepbatcher/store_mock.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ func NewStoreMock() *StoreMock {
2323
}
2424
}
2525

26-
// FetchUnconfirmedBatches fetches all the loop out sweep batches from the
26+
// FetchUnconfirmedSweepBatches fetches all the loop out sweep batches from the
2727
// database that are not in a confirmed state.
2828
func (s *StoreMock) FetchUnconfirmedSweepBatches(ctx context.Context) (
2929
[]*dbBatch, error) {

sweepbatcher/sweep_batch.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -215,7 +215,7 @@ type batch struct {
215215
// signerClient is the signer client used to sign the batch transaction.
216216
signerClient lndclient.SignerClient
217217

218-
// muSig2Kit includes all the required functionality to collect
218+
// muSig2SignSweep includes all the required functionality to collect
219219
// and verify signatures by the swap server in order to cooperatively
220220
// sweep funds.
221221
muSig2SignSweep MuSig2SignSweep

sweepbatcher/sweep_batcher.go

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -82,6 +82,14 @@ type BatcherStore interface {
8282
TotalSweptAmount(ctx context.Context, id int32) (btcutil.Amount, error)
8383
}
8484

85+
// LoopOutFetcher is used to load LoopOut swaps from the database.
86+
// It is implemented by loopdb.SwapStore.
87+
type LoopOutFetcher interface {
88+
// FetchLoopOutSwap returns the loop out swap with the given hash.
89+
FetchLoopOutSwap(ctx context.Context,
90+
hash lntypes.Hash) (*loopdb.LoopOut, error)
91+
}
92+
8593
// MuSig2SignSweep is a function that can be used to sign a sweep transaction
8694
// cooperatively with the swap server.
8795
type MuSig2SignSweep func(ctx context.Context,
@@ -183,9 +191,8 @@ type Batcher struct {
183191
// batcher and the batches.
184192
store BatcherStore
185193

186-
// swapStore includes all the database interactions that are needed for
187-
// interacting with swaps.
188-
swapStore loopdb.SwapStore
194+
// swapStore is used to load LoopOut swaps from the database.
195+
swapStore LoopOutFetcher
189196

190197
// wg is a waitgroup that is used to wait for all the goroutines to
191198
// exit.
@@ -197,7 +204,7 @@ func NewBatcher(wallet lndclient.WalletKitClient,
197204
chainNotifier lndclient.ChainNotifierClient,
198205
signerClient lndclient.SignerClient, musig2ServerSigner MuSig2SignSweep,
199206
verifySchnorrSig VerifySchnorrSig, chainparams *chaincfg.Params,
200-
store BatcherStore, swapStore loopdb.SwapStore) *Batcher {
207+
store BatcherStore, swapStore LoopOutFetcher) *Batcher {
201208

202209
return &Batcher{
203210
batches: make(map[int32]*batch),

sweepbatcher/sweep_batcher_test.go

Lines changed: 20 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -98,14 +98,14 @@ func TestSweepBatcherBatchCreation(t *testing.T) {
9898
store.AssertLoopOutStored()
9999

100100
// Deliver sweep request to batcher.
101-
batcher.sweepReqs <- sweepReq1
101+
require.NoError(t, batcher.AddSweep(&sweepReq1))
102102

103103
// Since a batch was created we check that it registered for its primary
104104
// sweep's spend.
105105
<-lnd.RegisterSpendChannel
106106

107107
// Insert the same swap twice, this should be a noop.
108-
batcher.sweepReqs <- sweepReq1
108+
require.NoError(t, batcher.AddSweep(&sweepReq1))
109109

110110
// Once batcher receives sweep request it will eventually spin up a
111111
// batch.
@@ -137,7 +137,7 @@ func TestSweepBatcherBatchCreation(t *testing.T) {
137137
require.NoError(t, err)
138138
store.AssertLoopOutStored()
139139

140-
batcher.sweepReqs <- sweepReq2
140+
require.NoError(t, batcher.AddSweep(&sweepReq2))
141141

142142
// Batcher should not create a second batch as timeout distance is small
143143
// enough.
@@ -169,7 +169,7 @@ func TestSweepBatcherBatchCreation(t *testing.T) {
169169
require.NoError(t, err)
170170
store.AssertLoopOutStored()
171171

172-
batcher.sweepReqs <- sweepReq3
172+
require.NoError(t, batcher.AddSweep(&sweepReq3))
173173

174174
// Batcher should create a second batch as timeout distance is greater
175175
// than the threshold
@@ -251,7 +251,7 @@ func TestSweepBatcherSimpleLifecycle(t *testing.T) {
251251
store.AssertLoopOutStored()
252252

253253
// Deliver sweep request to batcher.
254-
batcher.sweepReqs <- sweepReq1
254+
require.NoError(t, batcher.AddSweep(&sweepReq1))
255255

256256
// Eventually request will be consumed and a new batch will spin up.
257257
require.Eventually(t, func() bool {
@@ -435,15 +435,15 @@ func TestSweepBatcherSweepReentry(t *testing.T) {
435435
store.AssertLoopOutStored()
436436

437437
// Feed the sweeps to the batcher.
438-
batcher.sweepReqs <- sweepReq1
438+
require.NoError(t, batcher.AddSweep(&sweepReq1))
439439

440440
// After inserting the primary (first) sweep, a spend monitor should be
441441
// registered.
442442
<-lnd.RegisterSpendChannel
443443

444-
batcher.sweepReqs <- sweepReq2
444+
require.NoError(t, batcher.AddSweep(&sweepReq2))
445445

446-
batcher.sweepReqs <- sweepReq3
446+
require.NoError(t, batcher.AddSweep(&sweepReq3))
447447

448448
// Batcher should create a batch for the sweeps.
449449
require.Eventually(t, func() bool {
@@ -593,7 +593,7 @@ func TestSweepBatcherNonWalletAddr(t *testing.T) {
593593
store.AssertLoopOutStored()
594594

595595
// Deliver sweep request to batcher.
596-
batcher.sweepReqs <- sweepReq1
596+
require.NoError(t, batcher.AddSweep(&sweepReq1))
597597

598598
// Once batcher receives sweep request it will eventually spin up a
599599
// batch.
@@ -606,7 +606,7 @@ func TestSweepBatcherNonWalletAddr(t *testing.T) {
606606
<-lnd.RegisterSpendChannel
607607

608608
// Insert the same swap twice, this should be a noop.
609-
batcher.sweepReqs <- sweepReq1
609+
require.NoError(t, batcher.AddSweep(&sweepReq1))
610610

611611
// Create a second sweep request that has a timeout distance less than
612612
// our configured threshold.
@@ -633,7 +633,7 @@ func TestSweepBatcherNonWalletAddr(t *testing.T) {
633633
require.NoError(t, err)
634634
store.AssertLoopOutStored()
635635

636-
batcher.sweepReqs <- sweepReq2
636+
require.NoError(t, batcher.AddSweep(&sweepReq2))
637637

638638
// Batcher should create a second batch as first batch is a non wallet
639639
// addr batch.
@@ -670,7 +670,7 @@ func TestSweepBatcherNonWalletAddr(t *testing.T) {
670670
require.NoError(t, err)
671671
store.AssertLoopOutStored()
672672

673-
batcher.sweepReqs <- sweepReq3
673+
require.NoError(t, batcher.AddSweep(&sweepReq3))
674674

675675
// Batcher should create a new batch as timeout distance is greater than
676676
// the threshold
@@ -879,7 +879,7 @@ func TestSweepBatcherComposite(t *testing.T) {
879879
store.AssertLoopOutStored()
880880

881881
// Deliver sweep request to batcher.
882-
batcher.sweepReqs <- sweepReq1
882+
require.NoError(t, batcher.AddSweep(&sweepReq1))
883883

884884
// Once batcher receives sweep request it will eventually spin up a
885885
// batch.
@@ -892,17 +892,17 @@ func TestSweepBatcherComposite(t *testing.T) {
892892
<-lnd.RegisterSpendChannel
893893

894894
// Insert the same swap twice, this should be a noop.
895-
batcher.sweepReqs <- sweepReq1
895+
require.NoError(t, batcher.AddSweep(&sweepReq1))
896896

897-
batcher.sweepReqs <- sweepReq2
897+
require.NoError(t, batcher.AddSweep(&sweepReq2))
898898

899899
// Batcher should not create a second batch as timeout distance is small
900900
// enough.
901901
require.Eventually(t, func() bool {
902902
return len(batcher.batches) == 1
903903
}, test.Timeout, eventuallyCheckFrequency)
904904

905-
batcher.sweepReqs <- sweepReq3
905+
require.NoError(t, batcher.AddSweep(&sweepReq3))
906906

907907
// Batcher should create a second batch as this sweep pays to a non
908908
// wallet address.
@@ -914,7 +914,7 @@ func TestSweepBatcherComposite(t *testing.T) {
914914
// sweep's spend.
915915
<-lnd.RegisterSpendChannel
916916

917-
batcher.sweepReqs <- sweepReq4
917+
require.NoError(t, batcher.AddSweep(&sweepReq4))
918918

919919
// Batcher should create a third batch as timeout distance is greater
920920
// than the threshold.
@@ -926,15 +926,15 @@ func TestSweepBatcherComposite(t *testing.T) {
926926
// sweep's spend.
927927
<-lnd.RegisterSpendChannel
928928

929-
batcher.sweepReqs <- sweepReq5
929+
require.NoError(t, batcher.AddSweep(&sweepReq5))
930930

931931
// Batcher should not create a fourth batch as timeout distance is small
932932
// enough for it to join the last batch.
933933
require.Eventually(t, func() bool {
934934
return len(batcher.batches) == 3
935935
}, test.Timeout, eventuallyCheckFrequency)
936936

937-
batcher.sweepReqs <- sweepReq6
937+
require.NoError(t, batcher.AddSweep(&sweepReq6))
938938

939939
// Batcher should create a fourth batch as this sweep pays to a non
940940
// wallet address.
@@ -1084,7 +1084,7 @@ func TestRestoringEmptyBatch(t *testing.T) {
10841084
store.AssertLoopOutStored()
10851085

10861086
// Deliver sweep request to batcher.
1087-
batcher.sweepReqs <- sweepReq
1087+
require.NoError(t, batcher.AddSweep(&sweepReq))
10881088

10891089
// Since a batch was created we check that it registered for its primary
10901090
// sweep's spend.

0 commit comments

Comments
 (0)