66 "encoding/hex"
77 "errors"
88 "fmt"
9+ "reflect"
910 "sort"
11+ "strings"
1012 "sync"
1113 "time"
1214
@@ -470,12 +472,11 @@ func (s *swapClientServer) Monitor(in *clientrpc.MonitorRequest,
470472// ListSwaps returns a list of all currently known swaps and their current
471473// status.
472474func (s * swapClientServer ) ListSwaps (_ context.Context ,
473- _ * clientrpc.ListSwapsRequest ) (* clientrpc.ListSwapsResponse , error ) {
475+ req * clientrpc.ListSwapsRequest ) (* clientrpc.ListSwapsResponse , error ) {
474476
475477 var (
476- rpcSwaps = make ( []* clientrpc.SwapStatus , len ( s . swaps ))
478+ rpcSwaps = []* clientrpc.SwapStatus {}
477479 idx = 0
478- err error
479480 )
480481
481482 s .swapsLock .Lock ()
@@ -487,15 +488,93 @@ func (s *swapClientServer) ListSwaps(_ context.Context,
487488 // additional index.
488489 for _ , swp := range s .swaps {
489490 swp := swp
490- rpcSwaps [idx ], err = s .marshallSwap (& swp )
491+
492+ // Filter the swap based on the provided filter.
493+ if ! filterSwap (& swp , req .ListSwapFilter ) {
494+ continue
495+ }
496+
497+ rpcSwap , err := s .marshallSwap (& swp )
491498 if err != nil {
492499 return nil , err
493500 }
501+ rpcSwaps = append (rpcSwaps , rpcSwap )
494502 idx ++
495503 }
496504 return & clientrpc.ListSwapsResponse {Swaps : rpcSwaps }, nil
497505}
498506
507+ // filterSwap filters the given swap based on the provided filter.
508+ func filterSwap (swapInfo * loop.SwapInfo , filter * clientrpc.ListSwapsFilter ) bool {
509+ if filter == nil {
510+ return true
511+ }
512+
513+ // If the swap type filter is set, we only return swaps that match the
514+ // filter.
515+ if filter .SwapType != clientrpc .ListSwapsFilter_ANY {
516+ switch filter .SwapType {
517+ case clientrpc .ListSwapsFilter_LOOP_IN :
518+ if swapInfo .SwapType != swap .TypeIn {
519+ return false
520+ }
521+
522+ case clientrpc .ListSwapsFilter_LOOP_OUT :
523+ if swapInfo .SwapType != swap .TypeOut {
524+ return false
525+ }
526+ }
527+ }
528+
529+ // If the pending only filter is set, we only return pending swaps.
530+ if filter .PendingOnly && ! swapInfo .State .IsPending () {
531+ return false
532+ }
533+
534+ // If the swap is of type loop out and the outgoing channel filter is
535+ // set, we only return swaps that match the filter.
536+ if swapInfo .SwapType == swap .TypeOut && filter .OutgoingChanSet != nil {
537+ // First we sort both channel sets to make sure we can compare
538+ // them.
539+ sort .Slice (swapInfo .OutgoingChanSet , func (i , j int ) bool {
540+ return swapInfo .OutgoingChanSet [i ] <
541+ swapInfo .OutgoingChanSet [j ]
542+ })
543+ sort .Slice (filter .OutgoingChanSet , func (i , j int ) bool {
544+ return filter .OutgoingChanSet [i ] <
545+ filter .OutgoingChanSet [j ]
546+ })
547+
548+ // Compare the outgoing channel set by using reflect.DeepEqual
549+ // which compares the underlying arrays.
550+ if ! reflect .DeepEqual (swapInfo .OutgoingChanSet ,
551+ filter .OutgoingChanSet ) {
552+
553+ return false
554+ }
555+ }
556+
557+ // If the swap is of type loop in and the last hop filter is set, we
558+ // only return swaps that match the filter.
559+ if swapInfo .SwapType == swap .TypeIn && filter .LoopInLastHop != nil {
560+ // Compare the last hop by using reflect.DeepEqual which
561+ // compares the underlying arrays.
562+ if ! reflect .DeepEqual (swapInfo .LastHop , filter .LoopInLastHop ) {
563+ return false
564+ }
565+ }
566+
567+ // If a label filter is set, we only return swaps that softly match the
568+ // filter.
569+ if filter .Label != "" {
570+ if ! strings .Contains (swapInfo .Label , filter .Label ) {
571+ return false
572+ }
573+ }
574+
575+ return true
576+ }
577+
499578// SwapInfo returns all known details about a single swap.
500579func (s * swapClientServer ) SwapInfo (_ context.Context ,
501580 req * clientrpc.SwapInfoRequest ) (* clientrpc.SwapStatus , error ) {
0 commit comments