Skip to content

Commit

Permalink
Simplify grpc_query_test
Browse files Browse the repository at this point in the history
  • Loading branch information
Daniel committed Nov 2, 2022
1 parent 959dc45 commit fbd056b
Showing 1 changed file with 23 additions and 48 deletions.
71 changes: 23 additions & 48 deletions x/ccv/provider/keeper/grpc_query_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,81 +7,56 @@ import (
"github.com/stretchr/testify/require"

"github.com/cosmos/cosmos-sdk/baseapp"
codectypes "github.com/cosmos/cosmos-sdk/codec/types"
cryptocodec "github.com/cosmos/cosmos-sdk/crypto/codec"
cryptotypes "github.com/cosmos/cosmos-sdk/crypto/types"
sdkcodectypes "github.com/cosmos/cosmos-sdk/codec/types"
"github.com/cosmos/cosmos-sdk/simapp"
sdk "github.com/cosmos/cosmos-sdk/types"
stakingtypes "github.com/cosmos/cosmos-sdk/x/staking/types"
sdktypes "github.com/cosmos/cosmos-sdk/types"
testcrypto "github.com/cosmos/interchain-security/testutil/crypto"
testkeeper "github.com/cosmos/interchain-security/testutil/keeper"
testutil "github.com/cosmos/interchain-security/testutil/sample"
keeper "github.com/cosmos/interchain-security/x/ccv/provider/keeper"
"github.com/cosmos/interchain-security/x/ccv/provider/types"
tmprotocrypto "github.com/tendermint/tendermint/proto/tendermint/crypto"
providerkeeper "github.com/cosmos/interchain-security/x/ccv/provider/keeper"
providertypes "github.com/cosmos/interchain-security/x/ccv/provider/types"
)

func TestGRPCQueryConsumerChainValidatorKeyMapping(t *testing.T) {

consumerKeyData := func() (cryptotypes.PubKey, tmprotocrypto.PublicKey) {
_, tmprotoPublicKey := testutil.GetTMCryptoPublicKeyFromSeed(0)
sdkPubKey, err := cryptocodec.FromTmProtoPublicKey(tmprotoPublicKey)
require.NoError(t, err)
return sdkPubKey, tmprotoPublicKey
}

providerKeyAndValidatorData := func() (sdk.ValAddress, stakingtypes.Validator, tmprotocrypto.PublicKey) {
mockPV, tmprotoPublicKey := testutil.GetTMCryptoPublicKeyFromSeed(1)
tmPubKeyI, err := mockPV.GetPubKey()
require.NoError(t, err)
sdkPubKeyI, err := cryptocodec.FromTmPubKeyInterface(tmPubKeyI)
require.NoError(t, err)
addr, err := sdk.ValAddressFromHex(tmPubKeyI.Address().String())
require.NoError(t, err)
consensusAny, err := codectypes.NewAnyWithValue(sdkPubKeyI)
require.NoError(t, err)
v := stakingtypes.Validator{ConsensusPubkey: consensusAny}
return addr, v, tmprotoPublicKey
}

consumerSdkPubKeyExpect, consumerTmPublickey := consumerKeyData()
valSdkAddr, valSdkObject, valTMProtoPublicKey := providerKeyAndValidatorData()
testValProvider := testcrypto.NewValidatorFromIntSeed(0)
testValConsumer := testcrypto.NewValidatorFromIntSeed(1)

testCases := []struct {
name string
// State-mutating setup specific to this test case
setup func(sdk.Context, keeper.Keeper, testkeeper.MockedKeepers, string)
setup func(sdktypes.Context, providerkeeper.Keeper, testkeeper.MockedKeepers, string)
expError bool
chainID string
}{
{
name: "success",
setup: func(ctx sdk.Context, k keeper.Keeper, mocks testkeeper.MockedKeepers, chainID string) {
setup: func(ctx sdktypes.Context, k providerkeeper.Keeper, mocks testkeeper.MockedKeepers, chainID string) {
// Make chain queryable
k.SetConsumerClientId(ctx, chainID, "")
// Make validator queryable
gomock.InOrder(
mocks.MockStakingKeeper.EXPECT().GetValidator(
ctx, valSdkAddr,
// Return a valid validator, found!
).Return(valSdkObject, true).Times(1),
ctx, testValProvider.SDKValAddress(),
// Return a valid validator, found
).Return(testValProvider.SDKStakingValidator(), true).Times(1),
)
// Set a mapping
k.KeyMap(ctx, chainID).SetProviderPubKeyToConsumerPubKey(valTMProtoPublicKey, consumerTmPublickey)
k.KeyMap(ctx, chainID).SetProviderPubKeyToConsumerPubKey(testValProvider.TMProtoCryptoPublicKey(), testValConsumer.TMProtoCryptoPublicKey())
},
expError: false,
chainID: "chainid",
},
{
name: "mapping doesn't exist",
setup: func(ctx sdk.Context, k keeper.Keeper, mocks testkeeper.MockedKeepers, chainID string) {
setup: func(ctx sdktypes.Context, k providerkeeper.Keeper, mocks testkeeper.MockedKeepers, chainID string) {
// Make chain queryable
k.SetConsumerClientId(ctx, chainID, "")
// Make validator queryable
gomock.InOrder(
mocks.MockStakingKeeper.EXPECT().GetValidator(
ctx, valSdkAddr,
// Return a valid validator, found!
).Return(valSdkObject, true).Times(1),
ctx, testValProvider.SDKValAddress(),
// Return a valid validator, found
).Return(testValProvider.SDKStakingValidator(), true).Times(1),
)
},
expError: true,
Expand All @@ -95,24 +70,24 @@ func TestGRPCQueryConsumerChainValidatorKeyMapping(t *testing.T) {

app := simapp.Setup(false)
queryHelper := baseapp.NewQueryServerTestHelper(ctx, app.InterfaceRegistry())
types.RegisterQueryServer(queryHelper, k)
queryClient := types.NewQueryClient(queryHelper)
providertypes.RegisterQueryServer(queryHelper, k)
queryClient := providertypes.NewQueryClient(queryHelper)

tc.setup(ctx, k, mocks, tc.chainID)

req := types.QueryConsumerChainValidatorKeyMappingRequest{
req := providertypes.QueryConsumerChainValidatorKeyMappingRequest{
ChainId: tc.chainID,
ProviderValidatorAddress: valSdkAddr.String(),
ProviderValidatorAddress: testValProvider.SDKValAddress().String(),
}

goCtx := sdk.WrapSDKContext(ctx)
goCtx := sdktypes.WrapSDKContext(ctx)
res, err := queryClient.QueryConsumerChainValidatorKeyMapping(goCtx, &req)

if tc.expError {
require.Error(t, err, "invalid case did not return error")
} else {
require.NoError(t, err, "valid case returned error")
consumerValidatorPubKeyAnyExpect, err := codectypes.NewAnyWithValue(consumerSdkPubKeyExpect)
consumerValidatorPubKeyAnyExpect, err := sdkcodectypes.NewAnyWithValue(testValConsumer.SDKPubKey())
require.NoError(t, err, "faulty test")
require.Equal(t, consumerValidatorPubKeyAnyExpect.Value, res.ConsumerValidatorPubKey.Value)
}
Expand Down

0 comments on commit fbd056b

Please sign in to comment.