Skip to content

Commit

Permalink
add test TestQueryAllPairsValConAddrByConsumerChainID
Browse files Browse the repository at this point in the history
  • Loading branch information
ThanhNhann committed Dec 16, 2023
1 parent a5647a8 commit cee0c53
Show file tree
Hide file tree
Showing 2 changed files with 61 additions and 1 deletion.
6 changes: 5 additions & 1 deletion x/ccv/provider/keeper/grpc_query.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 <providerValConAddrs : consumerValConAddrs>
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),
Expand Down
56 changes: 56 additions & 0 deletions x/ccv/provider/keeper/grpc_query_test.go
Original file line number Diff line number Diff line change
@@ -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])
}

0 comments on commit cee0c53

Please sign in to comment.