@@ -3,6 +3,7 @@ package sweepbatcher
33import (
44 "context"
55 "errors"
6+ "sync"
67 "testing"
78 "time"
89
@@ -1028,3 +1029,85 @@ func TestGetFeePortionForSweep(t *testing.T) {
10281029 })
10291030 }
10301031}
1032+
1033+ // TestRestoringEmptyBatch tests that the batcher can be restored with an empty
1034+ // batch.
1035+ func TestRestoringEmptyBatch (t * testing.T ) {
1036+ defer test .Guard (t )()
1037+
1038+ lnd := test .NewMockLnd ()
1039+ ctx , cancel := context .WithCancel (context .Background ())
1040+
1041+ store := loopdb .NewStoreMock (t )
1042+
1043+ batcherStore := NewStoreMock ()
1044+ _ , err := batcherStore .InsertSweepBatch (ctx , & dbBatch {})
1045+ require .NoError (t , err )
1046+
1047+ batcher := NewBatcher (lnd .WalletKit , lnd .ChainNotifier , lnd .Signer ,
1048+ testMuSig2SignSweep , nil , lnd .ChainParams , batcherStore , store )
1049+
1050+ var wg sync.WaitGroup
1051+ wg .Add (1 )
1052+
1053+ var runErr error
1054+ go func () {
1055+ defer wg .Done ()
1056+ runErr = 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 only one batch stored (as we dropped the dormant
1103+ // one).
1104+ batches , err := batcherStore .FetchUnconfirmedSweepBatches (ctx )
1105+ require .NoError (t , err )
1106+ require .Len (t , batches , 1 )
1107+
1108+ // Now make it quit by canceling the context.
1109+ cancel ()
1110+ wg .Wait ()
1111+
1112+ checkBatcherError (t , runErr )
1113+ }
0 commit comments