@@ -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,254 @@ 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+ unixTime := time .Unix (0 , 0 )
607+ firstSwapStartTime := unixTime .Add (10 * time .Minute )
608+ secondSwapStartTime := unixTime .Add (20 * time .Minute )
609+ thirdSwapStartTime := unixTime .Add (30 * time .Minute )
610+
611+ // Create a set of test swaps of various types which contain the minimal
612+ // viable amount of info to successfully be run through marshallSwap.
613+ swapInOrder0 := loop.SwapInfo {
614+ SwapStateData : loopdb.SwapStateData {
615+ State : loopdb .StateInitiated ,
616+ Cost : loopdb.SwapCost {},
617+ },
618+ SwapContract : loopdb.SwapContract {
619+ InitiationTime : firstSwapStartTime ,
620+ },
621+ LastUpdate : time .Now (),
622+ SwapHash : lntypes.Hash {1 },
623+ SwapType : swap .TypeIn ,
624+ HtlcAddressP2WSH : testnetAddr ,
625+ HtlcAddressP2TR : testnetAddr ,
626+ }
627+
628+ swapOutOrder1 := loop.SwapInfo {
629+ SwapStateData : loopdb.SwapStateData {
630+ State : loopdb .StateInitiated ,
631+ Cost : loopdb.SwapCost {},
632+ },
633+ SwapContract : loopdb.SwapContract {
634+ InitiationTime : secondSwapStartTime ,
635+ },
636+ LastUpdate : time .Now (),
637+ SwapHash : lntypes.Hash {2 },
638+ SwapType : swap .TypeOut ,
639+ HtlcAddressP2WSH : testnetAddr ,
640+ HtlcAddressP2TR : testnetAddr ,
641+ }
642+
643+ swapOutOrder2 := loop.SwapInfo {
644+ SwapStateData : loopdb.SwapStateData {
645+ State : loopdb .StateInitiated ,
646+ Cost : loopdb.SwapCost {},
647+ },
648+ SwapContract : loopdb.SwapContract {
649+ InitiationTime : thirdSwapStartTime ,
650+ },
651+ LastUpdate : time .Now (),
652+ SwapHash : lntypes.Hash {3 },
653+ SwapType : swap .TypeOut ,
654+ HtlcAddressP2WSH : testnetAddr ,
655+ HtlcAddressP2TR : testnetAddr ,
656+ }
657+
658+ mockSwaps := []loop.SwapInfo {swapInOrder0 , swapOutOrder1 , swapOutOrder2 }
659+
660+ tests := []struct {
661+ name string
662+ // Define the mock swaps that will be stored in the mock client.
663+ mockSwaps []loop.SwapInfo
664+ req * looprpc.ListSwapsRequest
665+ // These hashes must be in the correct return order as the response.
666+ expectedReturnedSwaps []lntypes.Hash
667+ expectedNextStartTime int64
668+ }{
669+ {
670+ name : "fetch with defaults" ,
671+ mockSwaps : mockSwaps ,
672+ req : & looprpc.ListSwapsRequest {},
673+ expectedReturnedSwaps : []lntypes.Hash {
674+ swapInOrder0 .SwapHash ,
675+ swapOutOrder1 .SwapHash ,
676+ swapOutOrder2 .SwapHash ,
677+ },
678+ expectedNextStartTime : 0 ,
679+ },
680+ {
681+ name : "fetch with swaptype=loopin filter" ,
682+ mockSwaps : mockSwaps ,
683+ req : & looprpc.ListSwapsRequest {
684+ ListSwapFilter : & looprpc.ListSwapsFilter {
685+ SwapType : looprpc .ListSwapsFilter_LOOP_IN },
686+ },
687+ expectedReturnedSwaps : []lntypes.Hash {
688+ swapInOrder0 .SwapHash ,
689+ },
690+ expectedNextStartTime : 0 ,
691+ },
692+ {
693+ name : "fetch with swaptype=loopout filter" ,
694+ mockSwaps : mockSwaps ,
695+ req : & looprpc.ListSwapsRequest {
696+ ListSwapFilter : & looprpc.ListSwapsFilter {
697+ SwapType : looprpc .ListSwapsFilter_LOOP_OUT ,
698+ },
699+ },
700+ expectedReturnedSwaps : []lntypes.Hash {
701+ swapOutOrder1 .SwapHash ,
702+ swapOutOrder2 .SwapHash ,
703+ },
704+ expectedNextStartTime : 0 ,
705+ },
706+ {
707+ name : "fetch with limit" ,
708+ mockSwaps : mockSwaps ,
709+ req : & looprpc.ListSwapsRequest {
710+ MaxSwaps : 2 ,
711+ },
712+ expectedReturnedSwaps : []lntypes.Hash {
713+ swapInOrder0 .SwapHash ,
714+ swapOutOrder1 .SwapHash ,
715+ },
716+ expectedNextStartTime : secondSwapStartTime .UnixNano () + 1 ,
717+ },
718+ {
719+ name : "fetch with limit set to default" ,
720+ mockSwaps : mockSwaps ,
721+ req : & looprpc.ListSwapsRequest {
722+ MaxSwaps : 0 ,
723+ },
724+ expectedReturnedSwaps : []lntypes.Hash {
725+ swapInOrder0 .SwapHash ,
726+ swapOutOrder1 .SwapHash ,
727+ swapOutOrder2 .SwapHash ,
728+ },
729+ expectedNextStartTime : 0 ,
730+ },
731+ {
732+ name : "fetch with time filter #1" ,
733+ mockSwaps : mockSwaps ,
734+ req : & looprpc.ListSwapsRequest {
735+ ListSwapFilter : & looprpc.ListSwapsFilter {
736+ StartTimestampNs : unixTime .Add (25 * time .Minute ).UnixNano (),
737+ },
738+ },
739+ expectedReturnedSwaps : []lntypes.Hash {
740+ swapOutOrder2 .SwapHash ,
741+ },
742+ expectedNextStartTime : 0 ,
743+ },
744+ {
745+ name : "fetch with time filter #2" ,
746+ mockSwaps : mockSwaps ,
747+ req : & looprpc.ListSwapsRequest {
748+ ListSwapFilter : & looprpc.ListSwapsFilter {
749+ StartTimestampNs : unixTime .Add (5 * time .Minute ).UnixNano (),
750+ },
751+ },
752+ expectedReturnedSwaps : []lntypes.Hash {
753+ swapInOrder0 .SwapHash ,
754+ swapOutOrder1 .SwapHash ,
755+ swapOutOrder2 .SwapHash ,
756+ },
757+ expectedNextStartTime : 0 ,
758+ },
759+ {
760+ name : "fetch with swaptype=loopout filter, time filter, limit set" ,
761+ mockSwaps : mockSwaps ,
762+ req : & looprpc.ListSwapsRequest {
763+ ListSwapFilter : & looprpc.ListSwapsFilter {
764+ SwapType : looprpc .ListSwapsFilter_LOOP_OUT ,
765+ StartTimestampNs : unixTime .Add (15 * time .Minute ).UnixNano (),
766+ },
767+ MaxSwaps : 1 ,
768+ },
769+ expectedReturnedSwaps : []lntypes.Hash {
770+ swapOutOrder1 .SwapHash ,
771+ },
772+ expectedNextStartTime : secondSwapStartTime .UnixNano () + 1 ,
773+ },
774+ {
775+ name : "fetch with time filter, limit set" ,
776+ mockSwaps : mockSwaps ,
777+ req : & looprpc.ListSwapsRequest {
778+ ListSwapFilter : & looprpc.ListSwapsFilter {
779+ StartTimestampNs : unixTime .UnixNano (),
780+ },
781+ MaxSwaps : 2 ,
782+ },
783+ expectedReturnedSwaps : []lntypes.Hash {
784+ swapInOrder0 .SwapHash ,
785+ swapOutOrder1 .SwapHash ,
786+ },
787+ expectedNextStartTime : secondSwapStartTime .UnixNano () + 1 ,
788+ },
789+ {
790+ name : "fetch with time filter, limit set 2" ,
791+ mockSwaps : mockSwaps ,
792+ req : & looprpc.ListSwapsRequest {
793+ ListSwapFilter : & looprpc.ListSwapsFilter {
794+ StartTimestampNs : unixTime .UnixNano (),
795+ },
796+ MaxSwaps : 3 ,
797+ },
798+ expectedReturnedSwaps : []lntypes.Hash {
799+ swapInOrder0 .SwapHash ,
800+ swapOutOrder1 .SwapHash ,
801+ swapOutOrder2 .SwapHash ,
802+ },
803+ expectedNextStartTime : 0 ,
804+ },
805+ }
806+
807+ for _ , test := range tests {
808+ t .Run (test .name , func (t * testing.T ) {
809+ t .Parallel ()
810+
811+ // Create the swap client server with our mock client.
812+ server := & swapClientServer {
813+ swaps : make (map [lntypes.Hash ]loop.SwapInfo ),
814+ }
815+
816+ // Populate the server's swap cache with our mock swaps.
817+ for _ , swap := range test .mockSwaps {
818+ server .swaps [swap .SwapHash ] = swap
819+ }
820+
821+ // Call the ListSwaps method.
822+ resp , err := server .ListSwaps (context .Background (), test .req )
823+ require .NoError (t , err )
824+
825+ require .Len (
826+ t ,
827+ resp .Swaps ,
828+ len (test .expectedReturnedSwaps ),
829+ "incorrect returned count" ,
830+ )
831+
832+ // Check order of returned swaps is exactly as expected.
833+ for idx , aswap := range resp .Swaps {
834+ newhash , err := lntypes .MakeHash (aswap .GetIdBytes ())
835+ require .NoError (t , err )
836+ require .Equal (
837+ t ,
838+ test .expectedReturnedSwaps [idx ],
839+ newhash ,
840+ "iteration order mismatch" ,
841+ )
842+ }
843+
844+ require .Equal (
845+ t ,
846+ test .expectedNextStartTime ,
847+ resp .NextStartTime ,
848+ "incorrect next start time" ,
849+ )
850+ })
851+ }
852+ }
0 commit comments