@@ -3,6 +3,7 @@ package sweepbatcher
33import (
44 "context"
55 "strings"
6+ "sync"
67 "testing"
78 "time"
89
@@ -1029,3 +1030,83 @@ 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+ go func () {
1055+ defer wg .Done ()
1056+ err = batcher .Run (ctx )
1057+ }()
1058+
1059+ // Wait for the batcher to be initialized.
1060+ <- batcher .initDone
1061+
1062+ // Create a sweep request.
1063+ sweepReq := SweepRequest {
1064+ SwapHash : lntypes.Hash {1 , 1 , 1 },
1065+ Value : 111 ,
1066+ Outpoint : wire.OutPoint {
1067+ Hash : chainhash.Hash {1 , 1 },
1068+ Index : 1 ,
1069+ },
1070+ Notifier : & dummyNotifier ,
1071+ }
1072+
1073+ swap := & loopdb.LoopOutContract {
1074+ SwapContract : loopdb.SwapContract {
1075+ CltvExpiry : 111 ,
1076+ AmountRequested : 111 ,
1077+ },
1078+
1079+ SwapInvoice : swapInvoice ,
1080+ }
1081+
1082+ err = store .CreateLoopOut (ctx , sweepReq .SwapHash , swap )
1083+ require .NoError (t , err )
1084+ store .AssertLoopOutStored ()
1085+
1086+ // Deliver sweep request to batcher.
1087+ batcher .sweepReqs <- sweepReq
1088+
1089+ // Since a batch was created we check that it registered for its primary
1090+ // sweep's spend.
1091+ <- lnd .RegisterSpendChannel
1092+
1093+ // Once batcher receives sweep request it will eventually spin up a
1094+ // batch.
1095+ require .Eventually (t , func () bool {
1096+ // Make sure that the sweep was stored and we have exactly one
1097+ // active batch.
1098+ return batcherStore .AssertSweepStored (sweepReq .SwapHash ) &&
1099+ len (batcher .batches ) == 1
1100+ }, test .Timeout , eventuallyCheckFrequency )
1101+
1102+ // Make sure we have two batches stored.
1103+ batches , err := batcherStore .FetchUnconfirmedSweepBatches (ctx )
1104+ require .NoError (t , err )
1105+ require .Len (t , batches , 2 )
1106+
1107+ // Now make it quit by canceling the context.
1108+ cancel ()
1109+ wg .Wait ()
1110+
1111+ require .Error (t , context .Canceled , err )
1112+ }
0 commit comments