Skip to content

Commit

Permalink
Impl failing query test
Browse files Browse the repository at this point in the history
  • Loading branch information
Daniel committed Nov 1, 2022
1 parent 6ebc077 commit 6957163
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 23 deletions.
16 changes: 8 additions & 8 deletions x/ccv/provider/handler_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -52,7 +52,7 @@ func TestDesignateConsensusKeyForConsumerChain(t *testing.T) {
return addr, v
}

valSdkAddr, valSdkType := validatorAddressAndStakingType()
valSdkAddr, valSdkObject := valSdkAddressAndValObject()

testCases := []struct {
name string
Expand All @@ -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,
Expand Down Expand Up @@ -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)
Expand All @@ -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)
Expand Down
52 changes: 37 additions & 15 deletions x/ccv/provider/keeper/grpc_query_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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",
Expand All @@ -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()
Expand Down

0 comments on commit 6957163

Please sign in to comment.