Skip to content

Commit

Permalink
Merge pull request #883 from lavanet/CNS-640-add-query-to-fetch-all-a…
Browse files Browse the repository at this point in the history
…ctive-conflicts-opened-by-a-consumer

CNS-640: add query to fetch all active conflicts opened by a consumer
  • Loading branch information
Yaroms authored Oct 12, 2023
2 parents c5a381d + 4bf8aab commit 7a7fcd0
Show file tree
Hide file tree
Showing 8 changed files with 652 additions and 33 deletions.
13 changes: 13 additions & 0 deletions proto/lavanet/lava/conflict/query.proto
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,11 @@ service Query {
option (google.api.http).get = "/lavanet/lava/conflict/conflict_vote";
}

// Gets a consumer's active conflict list.
rpc ConsumerConflicts(QueryConsumerConflictsRequest) returns (QueryConsumerConflictsResponse) {
option (google.api.http).get = "/lavanet/lava/conflict/consumer_conflicts/{consumer}";
}

// this line is used by starport scaffolding # 2
}

Expand Down Expand Up @@ -56,4 +61,12 @@ message QueryAllConflictVoteResponse {
cosmos.base.query.v1beta1.PageResponse pagination = 2;
}

message QueryConsumerConflictsRequest {
string consumer = 1;
}

message QueryConsumerConflictsResponse {
repeated string conflicts = 1;
}

// this line is used by starport scaffolding # 3
1 change: 1 addition & 0 deletions scripts/cli_test.sh
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@ trace lavad q protocol params >/dev/null
echo "Testing conflict q commands"
trace lavad q conflict params >/dev/null
trace lavad q conflict list-conflict-vote >/dev/null
trace lavad q conflict consumer-conflicts $(lavad keys show user1 -a) >/dev/null
# trace lavad q conflict show-conflict-vote stam >/dev/null ## canot test that here

echo "Testing downtime q commands"
Expand Down
1 change: 1 addition & 0 deletions x/conflict/client/cli/query.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ func GetQueryCmd(queryRoute string) *cobra.Command {
cmd.AddCommand(CmdQueryParams())
cmd.AddCommand(CmdListConflictVote())
cmd.AddCommand(CmdShowConflictVote())
cmd.AddCommand(CmdConsumerConflicts())
// this line is used by starport scaffolding # 1

return cmd
Expand Down
36 changes: 36 additions & 0 deletions x/conflict/client/cli/query_consumer_conflicts.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
package cli

import (
"github.com/cosmos/cosmos-sdk/client"
"github.com/cosmos/cosmos-sdk/client/flags"
"github.com/lavanet/lava/x/conflict/types"
"github.com/spf13/cobra"
)

func CmdConsumerConflicts() *cobra.Command {
cmd := &cobra.Command{
Use: "consumer-conflicts <consumer>",
Short: "Gets a consumer's active conflict list",
Args: cobra.ExactArgs(1),
RunE: func(cmd *cobra.Command, args []string) error {
clientCtx := client.GetClientContextFromCmd(cmd)

queryClient := types.NewQueryClient(clientCtx)

params := &types.QueryConsumerConflictsRequest{
Consumer: args[0],
}

res, err := queryClient.ConsumerConflicts(cmd.Context(), params)
if err != nil {
return err
}

return clientCtx.PrintProto(res)
},
}

flags.AddQueryFlagsToCmd(cmd)

return cmd
}
28 changes: 28 additions & 0 deletions x/conflict/keeper/grpc_query_consumer_conflicts.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
package keeper

import (
"context"

sdk "github.com/cosmos/cosmos-sdk/types"
"github.com/lavanet/lava/x/conflict/types"
"google.golang.org/grpc/codes"
"google.golang.org/grpc/status"
)

func (k Keeper) ConsumerConflicts(c context.Context, req *types.QueryConsumerConflictsRequest) (*types.QueryConsumerConflictsResponse, error) {
if req == nil {
return nil, status.Error(codes.InvalidArgument, "invalid request")
}

var conflictIndices []string

ctx := sdk.UnwrapSDKContext(c)
conflicts := k.GetAllConflictVote(ctx)
for _, conflict := range conflicts {
if conflict.ClientAddress == req.Consumer {
conflictIndices = append(conflictIndices, conflict.Index)
}
}

return &types.QueryConsumerConflictsResponse{Conflicts: conflictIndices}, nil
}
52 changes: 52 additions & 0 deletions x/conflict/keeper/grpc_query_consumer_conflicts_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
package keeper_test

import (
"strconv"
"testing"

sdk "github.com/cosmos/cosmos-sdk/types"
keepertest "github.com/lavanet/lava/testutil/keeper"
"github.com/lavanet/lava/x/conflict/types"
"github.com/stretchr/testify/require"
)

// Prevent strconv unused error
var _ = strconv.IntSize

func TestConsumerConflicts(t *testing.T) {
keeper, ctx := keepertest.ConflictKeeper(t)
wctx := sdk.WrapSDKContext(ctx)
msgs := createNConflictVote(keeper, ctx, 10)
for i, msg := range msgs {
msg.ClientAddress = "client" + strconv.Itoa(i%2)
keeper.SetConflictVote(ctx, msg)
}

for _, tc := range []struct {
desc string
client string
expectedConflicts []string
}{
{
desc: "client0 conflicts",
client: "client0",
expectedConflicts: []string{"0", "2", "4", "6", "8"},
},
{
desc: "client1 conflicts",
client: "client1",
expectedConflicts: []string{"1", "3", "5", "7", "9"},
},
{
desc: "invalid client",
client: "dummy",
expectedConflicts: []string{},
},
} {
t.Run(tc.desc, func(t *testing.T) {
res, err := keeper.ConsumerConflicts(wctx, &types.QueryConsumerConflictsRequest{Consumer: tc.client})
require.Nil(t, err)
require.ElementsMatch(t, res.Conflicts, tc.expectedConflicts)
})
}
}
Loading

0 comments on commit 7a7fcd0

Please sign in to comment.