From 6957163458aac445eeafe81e7dcf8e7f70172a54 Mon Sep 17 00:00:00 2001 From: Daniel Date: Tue, 1 Nov 2022 14:28:40 +0000 Subject: [PATCH] Impl failing query test --- x/ccv/provider/handler_test.go | 16 ++++---- x/ccv/provider/keeper/grpc_query_test.go | 52 +++++++++++++++++------- 2 files changed, 45 insertions(+), 23 deletions(-) diff --git a/x/ccv/provider/handler_test.go b/x/ccv/provider/handler_test.go index cc809829be..c7c8e8d397 100644 --- a/x/ccv/provider/handler_test.go +++ b/x/ccv/provider/handler_test.go @@ -31,15 +31,15 @@ func TestInvalidMsg(t *testing.T) { func TestDesignateConsensusKeyForConsumerChain(t *testing.T) { - consumerTMProtoPublicKey := func() cryptotypes.PubKey { + consumerSdkPubKey := func() cryptotypes.PubKey { _, cpk0 := testutil.GetTMCryptoPublicKeyFromSeed(0) ret, err := cryptocodec.FromTmProtoPublicKey(cpk0) require.NoError(t, err) return ret } - validatorAddressAndStakingType := func() (sdk.ValAddress, stakingtypes.Validator) { - mockPV, _ := testutil.GetTMCryptoPublicKeyFromSeed(0) + valSdkAddressAndValObject := func() (sdk.ValAddress, stakingtypes.Validator) { + mockPV, _ := testutil.GetTMCryptoPublicKeyFromSeed(1) tmPubKeyI, err := mockPV.GetPubKey() require.NoError(t, err) sdkPubKeyI, err := cryptocodec.FromTmPubKeyInterface(tmPubKeyI) @@ -52,7 +52,7 @@ func TestDesignateConsensusKeyForConsumerChain(t *testing.T) { return addr, v } - valSdkAddr, valSdkType := validatorAddressAndStakingType() + valSdkAddr, valSdkObject := valSdkAddressAndValObject() testCases := []struct { name string @@ -73,7 +73,7 @@ func TestDesignateConsensusKeyForConsumerChain(t *testing.T) { mocks.MockStakingKeeper.EXPECT().GetValidator( ctx, valSdkAddr, // Return a valid validator, found! - ).Return(valSdkType, true).Times(1), + ).Return(valSdkObject, true).Times(1), ) }, expError: false, @@ -113,9 +113,9 @@ func TestDesignateConsensusKeyForConsumerChain(t *testing.T) { // Make chain queryable k.SetConsumerClientId(ctx, "chainid", "") - tmConsPubKey, err := valSdkType.TmConsPublicKey() + tmConsPubKey, err := valSdkObject.TmConsPublicKey() require.NoError(t, err) - tmPubKey, err := cryptocodec.ToTmProtoPublicKey(consumerTMProtoPublicKey()) + tmPubKey, err := cryptocodec.ToTmProtoPublicKey(consumerSdkPubKey()) require.NoError(t, err) // Use the consumer key already err = k.KeyMap(ctx, "chainid").SetProviderPubKeyToConsumerPubKey(tmConsPubKey, tmPubKey) @@ -139,7 +139,7 @@ func TestDesignateConsensusKeyForConsumerChain(t *testing.T) { tc.setup(ctx, k, mocks) msg, err := types.NewMsgDesignateConsensusKeyForConsumerChain(tc.chainID, - valSdkAddr, consumerTMProtoPublicKey(), + valSdkAddr, consumerSdkPubKey(), ) require.NoError(t, err) diff --git a/x/ccv/provider/keeper/grpc_query_test.go b/x/ccv/provider/keeper/grpc_query_test.go index 2e5ff4a184..4d231cf891 100644 --- a/x/ccv/provider/keeper/grpc_query_test.go +++ b/x/ccv/provider/keeper/grpc_query_test.go @@ -6,35 +6,47 @@ import ( "github.com/golang/mock/gomock" "github.com/stretchr/testify/require" + gocontext "context" + + "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" + "github.com/cosmos/cosmos-sdk/simapp" sdk "github.com/cosmos/cosmos-sdk/types" + stakingtypes "github.com/cosmos/cosmos-sdk/x/staking/types" 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" ) func TestGRPCQueryConsumerChainValidatorKeyMapping(t *testing.T) { - consumerTMProtoPublicKey := func() cryptotypes.PubKey { - _, cpk0 := testutil.GetTMCryptoPublicKeyFromSeed(0) - ret, err := cryptocodec.FromTmProtoPublicKey(cpk0) + consumerKeyData := func() (cryptotypes.PubKey, tmprotocrypto.PublicKey) { + _, tmprotoPublicKey := testutil.GetTMCryptoPublicKeyFromSeed(0) + sdkPubKey, err := cryptocodec.FromTmProtoPublicKey(tmprotoPublicKey) require.NoError(t, err) - return ret + return sdkPubKey, tmprotoPublicKey } - valAddress := func() sdk.ValAddress { - mockPV, _ := testutil.GetTMCryptoPublicKeyFromSeed(0) + 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) - return addr + consensusAny, err := codectypes.NewAnyWithValue(sdkPubKeyI) + require.NoError(t, err) + v := stakingtypes.Validator{ConsensusPubkey: consensusAny} + return addr, v, tmprotoPublicKey } - consumerKey := consumerTMProtoPublicKey() - valAddr := valAddress() + consumerSdkPubKeyExpect, consumerTmPublickey := consumerKeyData() + valSdkAddr, valSdkObject, valTMProtoPublicKey := providerKeyAndValidatorData() testCases := []struct { name string @@ -46,16 +58,17 @@ func TestGRPCQueryConsumerChainValidatorKeyMapping(t *testing.T) { { name: "success", setup: func(ctx sdk.Context, k keeper.Keeper, mocks testkeeper.MockedKeepers) { - // Make chain queryable k.SetConsumerClientId(ctx, "chainid", "") - + // Make validator queryable gomock.InOrder( mocks.MockStakingKeeper.EXPECT().GetValidator( - ctx, valAddr, + ctx, valSdkAddr, // Return a valid validator, found! - ).Return(valSdkType, true).Times(1), + ).Return(valSdkObject, true).Times(1), ) + // Set a mapping + k.KeyMap(ctx, "chainid").SetProviderPubKeyToConsumerPubKey(valTMProtoPublicKey, consumerTmPublickey) }, expError: false, chainID: "chainid", @@ -66,19 +79,28 @@ func TestGRPCQueryConsumerChainValidatorKeyMapping(t *testing.T) { k, ctx, ctrl, mocks := testkeeper.GetProviderKeeperAndCtx(t, testkeeper.NewInMemKeeperParams(t)) + // TODO: no idea if these 4 lines are right + app := simapp.Setup(false) + queryHelper := baseapp.NewQueryServerTestHelper(ctx, app.InterfaceRegistry()) + types.RegisterQueryServer(queryHelper, k) + queryClient := types.NewQueryClient(queryHelper) + tc.setup(ctx, k, mocks) req := types.QueryConsumerChainValidatorKeyMappingRequest{ ChainId: "chainid", - ProviderValidatorAddress: valAddr.String(), + ProviderValidatorAddress: valSdkAddr.String(), } - res, err := k.QueryConsumerChainValidatorKeyMapping + res, err := queryClient.QueryConsumerChainValidatorKeyMapping(gocontext.Background(), &req) if tc.expError { require.Error(t, err, "invalid case did not return error") } else { require.NoError(t, err, "valid case returned error") + consumerSdkPubKeyActual, ok := res.ConsumerValidatorPubkey.GetCachedValue().(cryptotypes.PubKey) + require.True(t, ok) + require.Equal(t, consumerSdkPubKeyExpect, consumerSdkPubKeyActual) } ctrl.Finish()