Skip to content

Commit

Permalink
Implement DesignateConsensusKeyForConsumerChain body
Browse files Browse the repository at this point in the history
  • Loading branch information
Daniel committed Oct 28, 2022
1 parent 5d2dec9 commit ecc9260
Showing 1 changed file with 57 additions and 2 deletions.
59 changes: 57 additions & 2 deletions x/ccv/provider/keeper/msg_server.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,12 @@ package keeper
import (
"context"

cryptocodec "github.com/cosmos/cosmos-sdk/crypto/codec"
cryptotypes "github.com/cosmos/cosmos-sdk/crypto/types"
sdk "github.com/cosmos/cosmos-sdk/types"
sdkerrors "github.com/cosmos/cosmos-sdk/types/errors"
"github.com/cosmos/interchain-security/x/ccv/provider/types"
tmstrings "github.com/tendermint/tendermint/libs/strings"
)

type msgServer struct {
Expand All @@ -22,9 +26,60 @@ var _ types.MsgServer = msgServer{}
// CreateValidator defines a method for creating a new validator
func (k msgServer) DesignateConsensusKeyForConsumerChain(goCtx context.Context, msg *types.MsgDesignateConsensusKeyForConsumerChain) (*types.MsgDesignateConsensusKeyForConsumerChainResponse, error) {
ctx := sdk.UnwrapSDKContext(goCtx)
_ = ctx

// TODO:
if _, found := k.GetConsumerClientId(ctx, msg.ChainId); found {
return nil, types.ErrNoConsumerChainFound
}

providerValidatorAddr, err := sdk.ValAddressFromBech32(msg.ProviderValidatorAddress)
if err != nil {
return nil, err
}

// validator must already be registered
validator, found := k.stakingKeeper.GetValidator(ctx, providerValidatorAddr)
if !found {
return nil, types.ErrNoValidatorFound
}
providerTMPublicKey, err := validator.TmConsPublicKey()
if err != nil {
return nil, sdkerrors.Wrapf(
types.ErrInvalidValidatorPubKey,
"%w; Second error",
err,
)
}

consumerSDKPublicKey, ok := msg.ConsumerValidatorPubkey.GetCachedValue().(cryptotypes.PubKey)
if !ok {
return nil, sdkerrors.Wrapf(sdkerrors.ErrInvalidType, "Expecting cryptotypes.PubKey, got %T", consumerSDKPublicKey)
}

cp := ctx.ConsensusParams()
if cp != nil && cp.Validator != nil {
if !tmstrings.StringInSlice(consumerSDKPublicKey.Type(), cp.Validator.PubKeyTypes) {
return nil, sdkerrors.Wrapf(
types.ErrValidatorPubKeyTypeNotSupported,
"got: %s, expected: %s", consumerSDKPublicKey.Type(), cp.Validator.PubKeyTypes,
)
}
}

consumerTMPublicKey, err := cryptocodec.ToTmProtoPublicKey(consumerSDKPublicKey)
if err != nil {
return nil, sdkerrors.Wrapf(
types.ErrInvalidValidatorPubKey,
"%w; Second error",
err,
)
}

k.KeyMap(ctx, msg.ChainId).SetProviderPubKeyToConsumerPubKey(
providerTMPublicKey,
consumerTMPublicKey,
)

// TODO: emit events?

return &types.MsgDesignateConsensusKeyForConsumerChainResponse{}, nil
}

0 comments on commit ecc9260

Please sign in to comment.