@@ -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,180 @@ 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+ expectedFirstIdx uint32
651+ expectedLastIdx uint32
652+ expectedFilteredTotalCount uint32
653+ }{
654+ {
655+ name : "fetch all swaps no pagination" ,
656+ mockSwaps : []loop.SwapInfo {swapIn1 , swapOut1 , swapOut2 },
657+ req : & looprpc.ListSwapsRequest {},
658+ expectedReturnedSwapCount : 3 ,
659+ expectedFirstIdx : 0 ,
660+ expectedLastIdx : 2 ,
661+ expectedFilteredTotalCount : 3 ,
662+ },
663+ {
664+ name : "fetch swaps-ins no pagination" ,
665+ mockSwaps : []loop.SwapInfo {swapIn1 , swapOut1 , swapOut2 },
666+ req : & looprpc.ListSwapsRequest {
667+ ListSwapFilter : & looprpc.ListSwapsFilter {
668+ SwapType : looprpc .ListSwapsFilter_LOOP_IN },
669+ },
670+ expectedReturnedSwapCount : 1 ,
671+ expectedFirstIdx : 0 ,
672+ expectedLastIdx : 0 ,
673+ expectedFilteredTotalCount : 1 ,
674+ },
675+ {
676+ name : "fetch swaps-outs no pagination" ,
677+ mockSwaps : []loop.SwapInfo {swapIn1 , swapOut1 , swapOut2 },
678+ req : & looprpc.ListSwapsRequest {
679+ ListSwapFilter : & looprpc.ListSwapsFilter {
680+ SwapType : looprpc .ListSwapsFilter_LOOP_OUT ,
681+ },
682+ },
683+ expectedReturnedSwapCount : 2 ,
684+ expectedFirstIdx : 0 ,
685+ expectedLastIdx : 1 ,
686+ expectedFilteredTotalCount : 2 ,
687+ },
688+ {
689+ name : "fetch swaps-outs increment start_index" ,
690+ mockSwaps : []loop.SwapInfo {swapIn1 , swapOut1 , swapOut2 },
691+ req : & looprpc.ListSwapsRequest {
692+ ListSwapFilter : & looprpc.ListSwapsFilter {
693+ SwapType : looprpc .ListSwapsFilter_LOOP_OUT ,
694+ },
695+ IndexOffset : 1 ,
696+ },
697+ expectedReturnedSwapCount : 1 ,
698+ expectedFirstIdx : 1 ,
699+ expectedLastIdx : 1 ,
700+ expectedFilteredTotalCount : 2 ,
701+ },
702+ {
703+ name : "fetch all swaps set swap limit" ,
704+ mockSwaps : []loop.SwapInfo {swapIn1 , swapOut1 , swapOut2 },
705+ req : & looprpc.ListSwapsRequest {
706+ MaxSwaps : 2 ,
707+ },
708+ expectedReturnedSwapCount : 2 ,
709+ expectedFirstIdx : 0 ,
710+ expectedLastIdx : 1 ,
711+ expectedFilteredTotalCount : 3 ,
712+ },
713+ {
714+ name : "fetch all swaps set swap limit set start index" ,
715+ mockSwaps : []loop.SwapInfo {swapIn1 , swapOut1 , swapOut2 },
716+ req : & looprpc.ListSwapsRequest {
717+ IndexOffset : 1 ,
718+ MaxSwaps : 1 ,
719+ },
720+ expectedReturnedSwapCount : 1 ,
721+ expectedFirstIdx : 1 ,
722+ expectedLastIdx : 1 ,
723+ expectedFilteredTotalCount : 3 ,
724+ },
725+ {
726+ name : "fetch all swaps set start index out of bounds" ,
727+ mockSwaps : []loop.SwapInfo {swapIn1 , swapOut1 , swapOut2 },
728+ req : & looprpc.ListSwapsRequest {
729+ IndexOffset : 5 ,
730+ },
731+ expectedReturnedSwapCount : 0 ,
732+ expectedFirstIdx : 5 ,
733+ expectedLastIdx : 0 ,
734+ expectedFilteredTotalCount : 3 ,
735+ },
736+ {
737+ name : "fetch all swaps set limit to 0" ,
738+ mockSwaps : []loop.SwapInfo {swapIn1 , swapOut1 , swapOut2 },
739+ req : & looprpc.ListSwapsRequest {
740+ MaxSwaps : 0 ,
741+ },
742+ expectedReturnedSwapCount : 3 ,
743+ expectedFirstIdx : 0 ,
744+ expectedLastIdx : 2 ,
745+ expectedFilteredTotalCount : 3 ,
746+ },
747+ }
748+
749+ for _ , test := range tests {
750+ t .Run (test .name , func (t * testing.T ) {
751+ t .Parallel ()
752+
753+ // Create the swap client server with our mock client
754+ server := & swapClientServer {
755+ swaps : make (map [lntypes.Hash ]loop.SwapInfo ),
756+ }
757+
758+ // Populate the server's swap cache with our mock swaps
759+ for _ , swap := range test .mockSwaps {
760+ server .swaps [swap .SwapHash ] = swap
761+ }
762+
763+ // Call the ListSwaps method.
764+ resp , err := server .ListSwaps (context .Background (), test .req )
765+ require .NoError (t , err )
766+
767+ // Verify the response matches our expectations.
768+ require .Equal (t , test .expectedReturnedSwapCount , len (resp .Swaps ),
769+ "incorrect returned count" )
770+ // require.Equal(t, test.expectedFirstIdx, resp.FirstIndexOffset,
771+ // "incorrect first index")
772+ require .Equal (t , test .expectedLastIdx , resp .LastIndexOffset ,
773+ "incorrect last index" )
774+ require .Equal (t , test .expectedFilteredTotalCount , resp .TotalFilteredSwaps ,
775+ "incorrect total" )
776+ })
777+ }
778+ }
0 commit comments