From cee0c53b3b7be1aed388e77b8beaf18ab93e0cd8 Mon Sep 17 00:00:00 2001 From: ThanhNhann Date: Sun, 17 Dec 2023 00:16:34 +0700 Subject: [PATCH] add test TestQueryAllPairsValConAddrByConsumerChainID --- x/ccv/provider/keeper/grpc_query.go | 6 ++- x/ccv/provider/keeper/grpc_query_test.go | 56 ++++++++++++++++++++++++ 2 files changed, 61 insertions(+), 1 deletion(-) create mode 100644 x/ccv/provider/keeper/grpc_query_test.go diff --git a/x/ccv/provider/keeper/grpc_query.go b/x/ccv/provider/keeper/grpc_query.go index 68b400890f..888da43c5d 100644 --- a/x/ccv/provider/keeper/grpc_query.go +++ b/x/ccv/provider/keeper/grpc_query.go @@ -188,13 +188,17 @@ func (k Keeper) QueryAllPairsValConAddrByConsumerChainID(goCtx context.Context, return nil, status.Error(codes.InvalidArgument, "empty request") } + if req.ChainId == "" { + return nil, status.Error(codes.InvalidArgument, "empty chainId") + } + // list of pairs valconsensus addr pairValConAddrs := []*types.PairValConAddrProviderAndConsumer{} ctx := sdk.UnwrapSDKContext(goCtx) validatorConsumerAddrs := k.GetAllValidatorsByConsumerAddr(ctx, &req.ChainId) for _, data := range validatorConsumerAddrs { - pubKey, _, _ := k.GetKeyAssignmentReplacement(ctx, req.ChainId, types.ProviderConsAddress{Address: data.ConsumerAddr}) + pubKey, _, _ := k.GetKeyAssignmentReplacement(ctx, req.ChainId, types.ProviderConsAddress{Address: data.ProviderAddr}) pairValConAddrs = append(pairValConAddrs, &types.PairValConAddrProviderAndConsumer{ ProviderAddress: string(data.ProviderAddr), ConsumerAddress: string(data.ConsumerAddr), diff --git a/x/ccv/provider/keeper/grpc_query_test.go b/x/ccv/provider/keeper/grpc_query_test.go new file mode 100644 index 0000000000..2e824a613e --- /dev/null +++ b/x/ccv/provider/keeper/grpc_query_test.go @@ -0,0 +1,56 @@ +package keeper_test + +import ( + "testing" + + "github.com/stretchr/testify/require" + + cryptotestutil "github.com/cosmos/interchain-security/v3/testutil/crypto" + testkeeper "github.com/cosmos/interchain-security/v3/testutil/keeper" + "github.com/cosmos/interchain-security/v3/x/ccv/provider/types" +) + +func TestQueryAllPairsValConAddrByConsumerChainID(t *testing.T) { + chainID := consumer + providerAddr := types.NewProviderConsAddress([]byte("providerAddr")) + consumerAddr := types.NewConsumerConsAddress([]byte("consumerAddr")) + + consumerKey := cryptotestutil.NewCryptoIdentityFromIntSeed(1).TMProtoCryptoPublicKey() + + pk, ctx, ctrl, _ := testkeeper.GetProviderKeeperAndCtx(t, testkeeper.NewInMemKeeperParams(t)) + defer ctrl.Finish() + + pk.SetValidatorByConsumerAddr(ctx, chainID, consumerAddr, providerAddr) + pk.SetValidatorConsumerPubKey(ctx, chainID, providerAddr, consumerKey) + pk.SetKeyAssignmentReplacement(ctx, chainID, providerAddr, consumerKey, 100) + + consumerPubKey, found := pk.GetValidatorConsumerPubKey(ctx, chainID, providerAddr) + require.True(t, found, "consumer pubkey not found") + require.NotEmpty(t, consumerPubKey, "consumer pubkey is empty") + require.Equal(t, consumerPubKey, consumerKey) + + // Request is nil + _, err := pk.QueryAllPairsValConAddrByConsumerChainID(ctx, nil) + require.Error(t, err) + + // Request with chainId is empty + _, err = pk.QueryAllPairsValConAddrByConsumerChainID(ctx, &types.QueryAllPairsValConAddrByConsumerChainIDRequest{}) + require.Error(t, err) + + // Request with chainId is invalid + response, err := pk.QueryAllPairsValConAddrByConsumerChainID(ctx, &types.QueryAllPairsValConAddrByConsumerChainIDRequest{ChainId: "invalidChainId"}) + require.NoError(t, err) + require.Equal(t, []*types.PairValConAddrProviderAndConsumer{}, response.PairValConAddr) + + // Request is valid + response, err = pk.QueryAllPairsValConAddrByConsumerChainID(ctx, &types.QueryAllPairsValConAddrByConsumerChainIDRequest{ChainId: chainID}) + require.NoError(t, err) + + expectedResult := types.PairValConAddrProviderAndConsumer{ + ProviderAddress: "providerAddr", + ConsumerAddress: "consumerAddr", + ConsumerKey: &consumerKey, + } + require.Equal(t, &consumerKey, response.PairValConAddr[0].ConsumerKey) + require.Equal(t, &expectedResult, response.PairValConAddr[0]) +}