Skip to content

Commit 6d63027

Browse files
committed
looprpc: add simple pagination to the ListSwaps command
This commit adds the ability to set a max_swaps and an index_offset flag to the ListSwaps command. These new fields are applied AFTER the initial filtering step. The response also now passes additional information about the index count and total swaps filtered.
1 parent bfe7991 commit 6d63027

File tree

5 files changed

+153
-36
lines changed

5 files changed

+153
-36
lines changed

cmd/loop/swaps.go

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,14 @@ var listSwapsCommand = cli.Command{
3535
labelFlag,
3636
channelFlag,
3737
lastHopFlag,
38+
cli.Uint64Flag{
39+
Name: "max_swaps",
40+
Usage: "Max number of swaps to return after filtering",
41+
},
42+
cli.Uint64Flag{
43+
Name: "index_offset",
44+
Usage: "index to start listing filtered swaps from",
45+
},
3846
},
3947
}
4048

@@ -102,6 +110,8 @@ func listSwaps(ctx *cli.Context) error {
102110
resp, err := client.ListSwaps(
103111
context.Background(), &looprpc.ListSwapsRequest{
104112
ListSwapFilter: filter,
113+
IndexOffset: uint32(ctx.Uint("index_offset")),
114+
MaxSwaps: uint32(ctx.Uint("max_swaps")),
105115
},
106116
)
107117
if err != nil {

loopd/swapclient_server.go

Lines changed: 23 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -564,7 +564,9 @@ func (s *swapClientServer) ListSwaps(ctx context.Context,
564564

565565
var (
566566
rpcSwaps = []*looprpc.SwapStatus{}
567-
idx = 0
567+
idx = uint32(0)
568+
lastIdx = uint32(0)
569+
maxSwaps = int(req.MaxSwaps)
568570
)
569571

570572
s.swapsLock.Lock()
@@ -580,14 +582,29 @@ func (s *swapClientServer) ListSwaps(ctx context.Context,
580582
continue
581583
}
582584

583-
rpcSwap, err := s.marshallSwap(ctx, &swp)
584-
if err != nil {
585-
return nil, err
585+
// Check if this swap is within our pagination window.
586+
if idx >= req.IndexOffset {
587+
// If maxSwaps is 0, we return all swaps.
588+
// Otherwise, check if we've reached our limit.
589+
if maxSwaps == 0 || len(rpcSwaps) < maxSwaps {
590+
rpcSwap, err := s.marshallSwap(ctx, &swp)
591+
if err != nil {
592+
return nil, err
593+
}
594+
rpcSwaps = append(rpcSwaps, rpcSwap)
595+
lastIdx = idx
596+
}
597+
586598
}
587-
rpcSwaps = append(rpcSwaps, rpcSwap)
588599
idx++
589600
}
590-
return &looprpc.ListSwapsResponse{Swaps: rpcSwaps}, nil
601+
602+
response := looprpc.ListSwapsResponse{
603+
Swaps: rpcSwaps,
604+
LastIndexOffset: lastIdx,
605+
TotalFilteredSwaps: idx,
606+
}
607+
return &response, nil
591608
}
592609

593610
// filterSwap filters the given swap based on the provided filter.

looprpc/client.pb.go

Lines changed: 78 additions & 30 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

looprpc/client.proto

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -675,6 +675,12 @@ enum FailureReason {
675675
message ListSwapsRequest {
676676
// Optional filter to only return swaps that match the filter.
677677
ListSwapsFilter list_swap_filter = 1;
678+
679+
// Allow specifying an starting index when returning filtered swaps.
680+
uint32 index_offset = 2;
681+
682+
// Set a maximum number of swaps to return in the response.
683+
uint32 max_swaps = 3;
678684
}
679685

680686
message ListSwapsFilter {
@@ -711,6 +717,16 @@ message ListSwapsResponse {
711717
The list of all currently known swaps and their status.
712718
*/
713719
repeated SwapStatus swaps = 1;
720+
721+
// The last returned swap index returned.
722+
uint32 last_index_offset = 2;
723+
724+
/*
725+
The total number of swaps found after filtering, but before any pagination
726+
takes place. The caller can use this field as they iterate over all swaps
727+
using pagination.
728+
*/
729+
uint32 total_filtered_swaps = 4;
714730
}
715731

716732
message SwapInfoRequest {

looprpc/client.swagger.json

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -625,6 +625,22 @@
625625
"in": "query",
626626
"required": false,
627627
"type": "boolean"
628+
},
629+
{
630+
"name": "index_offset",
631+
"description": "Allow specifying an starting index when returning filtered swaps.",
632+
"in": "query",
633+
"required": false,
634+
"type": "integer",
635+
"format": "int64"
636+
},
637+
{
638+
"name": "max_swaps",
639+
"description": "Set a maximum number of swaps to return in the response.",
640+
"in": "query",
641+
"required": false,
642+
"type": "integer",
643+
"format": "int64"
628644
}
629645
],
630646
"tags": [
@@ -1415,6 +1431,16 @@
14151431
"$ref": "#/definitions/looprpcSwapStatus"
14161432
},
14171433
"description": "The list of all currently known swaps and their status."
1434+
},
1435+
"last_index_offset": {
1436+
"type": "integer",
1437+
"format": "int64",
1438+
"description": "The last returned swap index returned."
1439+
},
1440+
"total_filtered_swaps": {
1441+
"type": "integer",
1442+
"format": "int64",
1443+
"description": "The total number of swaps found after filtering, but before any pagination\ntakes place. The caller can use this field as they iterate over all swaps\nusing pagination."
14181444
}
14191445
}
14201446
},

0 commit comments

Comments
 (0)