@@ -3,6 +3,7 @@ package sweepbatcher
33import (
44 "context"
55 "strings"
6+ "sync"
67 "testing"
78 "time"
89
@@ -1029,3 +1030,85 @@ 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 only one batch stored (as we dropped the dormant
1104+ // one).
1105+ batches , err := batcherStore .FetchUnconfirmedSweepBatches (ctx )
1106+ require .NoError (t , err )
1107+ require .Len (t , batches , 1 )
1108+
1109+ // Now make it quit by canceling the context.
1110+ cancel ()
1111+ wg .Wait ()
1112+
1113+ require .Error (t , context .Canceled , runErr )
1114+ }
0 commit comments