@@ -4,15 +4,19 @@ import (
44 "context"
55 "os"
66 "testing"
7+ "time"
78
89 "github.com/btcsuite/btcd/btcutil"
910 "github.com/btcsuite/btcd/chaincfg"
1011 "github.com/btcsuite/btclog/v2"
1112 "github.com/lightninglabs/lndclient"
1213 "github.com/lightninglabs/loop"
1314 "github.com/lightninglabs/loop/labels"
15+ "github.com/lightninglabs/loop/loopdb"
1416 "github.com/lightninglabs/loop/looprpc"
17+ "github.com/lightninglabs/loop/swap"
1518 mock_lnd "github.com/lightninglabs/loop/test"
19+ "github.com/lightningnetwork/lnd/lntypes"
1620 "github.com/lightningnetwork/lnd/lnwire"
1721 "github.com/lightningnetwork/lnd/routing/route"
1822 "github.com/stretchr/testify/require"
@@ -595,3 +599,181 @@ func TestHasBandwidth(t *testing.T) {
595599 })
596600 }
597601}
602+
603+ // TestListSwapsFilterAndPagination tests the filtering and
604+ // paging of the ListSwaps command.
605+ func TestListSwapsFilterAndPagination (t * testing.T ) {
606+ // Create a set of test swaps of various types which contain the minimal
607+ // viable amount of info to successfully be run through marshallSwap.
608+ swapIn1 := loop.SwapInfo {
609+ SwapStateData : loopdb.SwapStateData {
610+ State : loopdb .StateInitiated ,
611+ Cost : loopdb.SwapCost {},
612+ },
613+ LastUpdate : time .Now (),
614+ SwapHash : lntypes.Hash {1 },
615+ SwapType : swap .Type (swap .TypeIn ),
616+ HtlcAddressP2WSH : testnetAddr ,
617+ HtlcAddressP2TR : testnetAddr ,
618+ }
619+
620+ swapOut1 := loop.SwapInfo {
621+ SwapStateData : loopdb.SwapStateData {
622+ State : loopdb .StateInitiated ,
623+ Cost : loopdb.SwapCost {},
624+ },
625+ LastUpdate : time .Now (),
626+ SwapHash : lntypes.Hash {2 },
627+ SwapType : swap .Type (swap .TypeOut ),
628+ HtlcAddressP2WSH : testnetAddr ,
629+ HtlcAddressP2TR : testnetAddr ,
630+ }
631+
632+ swapOut2 := loop.SwapInfo {
633+ SwapStateData : loopdb.SwapStateData {
634+ State : loopdb .StateInitiated ,
635+ Cost : loopdb.SwapCost {},
636+ },
637+ LastUpdate : time .Now (),
638+ SwapHash : lntypes.Hash {3 },
639+ SwapType : swap .Type (swap .TypeOut ),
640+ HtlcAddressP2WSH : testnetAddr ,
641+ HtlcAddressP2TR : testnetAddr ,
642+ }
643+
644+ tests := []struct {
645+ name string
646+ // Define the mock swaps that will be stored in the mock client.
647+ mockSwaps []loop.SwapInfo
648+ req * looprpc.ListSwapsRequest
649+ expectedReturnedSwapCount int
650+ expectedLastIdx uint32
651+ expectedFilteredTotalCount uint32
652+ }{
653+ {
654+ name : "fetch all swaps no pagination" ,
655+ mockSwaps : []loop.SwapInfo {swapIn1 , swapOut1 , swapOut2 },
656+ req : & looprpc.ListSwapsRequest {},
657+ expectedReturnedSwapCount : 3 ,
658+ expectedLastIdx : 2 ,
659+ expectedFilteredTotalCount : 3 ,
660+ },
661+ {
662+ name : "fetch swaps-ins no pagination" ,
663+ mockSwaps : []loop.SwapInfo {swapIn1 , swapOut1 , swapOut2 },
664+ req : & looprpc.ListSwapsRequest {
665+ ListSwapFilter : & looprpc.ListSwapsFilter {
666+ SwapType : looprpc .ListSwapsFilter_LOOP_IN },
667+ },
668+ expectedReturnedSwapCount : 1 ,
669+ expectedLastIdx : 0 ,
670+ expectedFilteredTotalCount : 1 ,
671+ },
672+ {
673+ name : "fetch swaps-outs no pagination" ,
674+ mockSwaps : []loop.SwapInfo {swapIn1 , swapOut1 , swapOut2 },
675+ req : & looprpc.ListSwapsRequest {
676+ ListSwapFilter : & looprpc.ListSwapsFilter {
677+ SwapType : looprpc .ListSwapsFilter_LOOP_OUT ,
678+ },
679+ },
680+ expectedReturnedSwapCount : 2 ,
681+ expectedLastIdx : 1 ,
682+ expectedFilteredTotalCount : 2 ,
683+ },
684+ {
685+ name : "fetch swaps-outs increment start_index" ,
686+ mockSwaps : []loop.SwapInfo {swapIn1 , swapOut1 , swapOut2 },
687+ req : & looprpc.ListSwapsRequest {
688+ ListSwapFilter : & looprpc.ListSwapsFilter {
689+ SwapType : looprpc .ListSwapsFilter_LOOP_OUT ,
690+ },
691+ IndexOffset : 1 ,
692+ },
693+ expectedReturnedSwapCount : 1 ,
694+ expectedLastIdx : 1 ,
695+ expectedFilteredTotalCount : 2 ,
696+ },
697+ {
698+ name : "fetch all swaps set swap limit" ,
699+ mockSwaps : []loop.SwapInfo {swapIn1 , swapOut1 , swapOut2 },
700+ req : & looprpc.ListSwapsRequest {
701+ MaxSwaps : 2 ,
702+ },
703+ expectedReturnedSwapCount : 2 ,
704+ expectedLastIdx : 1 ,
705+ expectedFilteredTotalCount : 3 ,
706+ },
707+ {
708+ name : "fetch all swaps set swap limit set start index" ,
709+ mockSwaps : []loop.SwapInfo {swapIn1 , swapOut1 , swapOut2 },
710+ req : & looprpc.ListSwapsRequest {
711+ IndexOffset : 1 ,
712+ MaxSwaps : 1 ,
713+ },
714+ expectedReturnedSwapCount : 1 ,
715+ expectedLastIdx : 1 ,
716+ expectedFilteredTotalCount : 3 ,
717+ },
718+ {
719+ name : "fetch all swaps set start index out of bounds" ,
720+ mockSwaps : []loop.SwapInfo {swapIn1 , swapOut1 , swapOut2 },
721+ req : & looprpc.ListSwapsRequest {
722+ IndexOffset : 5 ,
723+ },
724+ expectedReturnedSwapCount : 0 ,
725+ expectedLastIdx : 0 ,
726+ expectedFilteredTotalCount : 3 ,
727+ },
728+ {
729+ name : "fetch all swaps set limit to 0" ,
730+ mockSwaps : []loop.SwapInfo {swapIn1 , swapOut1 , swapOut2 },
731+ req : & looprpc.ListSwapsRequest {
732+ MaxSwaps : 0 ,
733+ },
734+ expectedReturnedSwapCount : 3 ,
735+ expectedLastIdx : 2 ,
736+ expectedFilteredTotalCount : 3 ,
737+ },
738+ }
739+
740+ for _ , test := range tests {
741+ t .Run (test .name , func (t * testing.T ) {
742+ t .Parallel ()
743+
744+ // Create the swap client server with our mock client.
745+ server := & swapClientServer {
746+ swaps : make (map [lntypes.Hash ]loop.SwapInfo ),
747+ }
748+
749+ // Populate the server's swap cache with our mock swaps.
750+ for _ , swap := range test .mockSwaps {
751+ server .swaps [swap .SwapHash ] = swap
752+ }
753+
754+ // Call the ListSwaps method.
755+ resp , err := server .ListSwaps (context .Background (), test .req )
756+ require .NoError (t , err )
757+
758+ require .Equal (
759+ t ,
760+ test .expectedReturnedSwapCount ,
761+ len (resp .Swaps ),
762+ "incorrect returned count" )
763+
764+ require .Equal (
765+ t ,
766+ test .expectedLastIdx ,
767+ resp .LastIndexOffset ,
768+ "incorrect last index" ,
769+ )
770+
771+ require .Equal (
772+ t ,
773+ test .expectedFilteredTotalCount ,
774+ resp .TotalFilteredSwaps ,
775+ "incorrect total" ,
776+ )
777+ })
778+ }
779+ }
0 commit comments