Skip to content

Commit

Permalink
add msg server
Browse files Browse the repository at this point in the history
  • Loading branch information
GNaD13 committed Nov 10, 2023
1 parent 76e666c commit 2962e73
Show file tree
Hide file tree
Showing 8 changed files with 663 additions and 40 deletions.
12 changes: 9 additions & 3 deletions proto/composable/keyrotation/tx.proto
Original file line number Diff line number Diff line change
Expand Up @@ -5,15 +5,21 @@ import "cosmos/msg/v1/msg.proto";
import "amino/amino.proto";
import "gogoproto/gogo.proto";
import "cosmos_proto/cosmos.proto";
import "google/protobuf/any.proto";

option go_package = "x/keyrotation/types";

// Msg defines the x/mint Msg service.
// Msg defines the x/keyrotation Msg service.
service Msg {
option (cosmos.msg.v1.service) = true;
// CreateValidator defines a method for creating a new validator.
rpc RotateConsPubKey(MsgRotateConsPubKey) returns (MsgRotateConsPubKeyResponse);
}

message MsgRotateConsPubKey {
string val_address = 1;

string validator_address = 1;
google.protobuf.Any pubkey = 2 [(cosmos_proto.accepts_interface) = "cosmos.crypto.PubKey"];
}

message MsgRotateConsPubKeyResponse {
}
2 changes: 1 addition & 1 deletion x/keyrotation/keeper/abci.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,5 +6,5 @@ import (
)

func (k Keeper) EndBlock(ctx sdk.Context) []abci.ValidatorUpdate {

return nil
}
41 changes: 41 additions & 0 deletions x/keyrotation/keeper/msg_server.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
package keeper

import (
"context"

sdk "github.com/cosmos/cosmos-sdk/types"
"github.com/notional-labs/composable/v6/x/keyrotation/types"

errorsmod "cosmossdk.io/errors"
sdkerrors "github.com/cosmos/cosmos-sdk/types/errors"
)

var _ types.MsgServer = msgServer{}

// NewMsgServerImpl returns an implementation of the MsgServer interface
// for the provided Keeper.
func NewMsgServerImpl(keeper Keeper) types.MsgServer {
return &msgServer{
Keeper: keeper,
}
}

type msgServer struct {
Keeper
}

func (k Keeper) RotateConsPubKey(goCtx context.Context, msg *types.MsgRotateConsPubKey) (*types.MsgRotateConsPubKeyResponse, error) {
ctx := sdk.UnwrapSDKContext(goCtx)

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

// check to see if the validator not exist
if _, found := k.sk.GetValidator(ctx, valAddr); !found {
return nil, errorsmod.Wrap(sdkerrors.ErrInvalidRequest, "validator not exists")
}

return &types.MsgRotateConsPubKeyResponse{}, nil
}
14 changes: 12 additions & 2 deletions x/keyrotation/types/codec.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,10 @@ package types

import (
"github.com/cosmos/cosmos-sdk/codec"
"github.com/cosmos/cosmos-sdk/codec/legacy"
codectypes "github.com/cosmos/cosmos-sdk/codec/types"
cryptocodec "github.com/cosmos/cosmos-sdk/crypto/codec"
"github.com/cosmos/cosmos-sdk/types/msgservice"
authzcodec "github.com/cosmos/cosmos-sdk/x/authz/codec"
govcodec "github.com/cosmos/cosmos-sdk/x/gov/codec"
groupcodec "github.com/cosmos/cosmos-sdk/x/group/codec"
Expand All @@ -13,9 +15,17 @@ import (

// RegisterLegacyAminoCodec registers the account interfaces and concrete types on the
// provided LegacyAmino codec. These types are used for Amino JSON serialization
func RegisterLegacyAminoCodec(cdc *codec.LegacyAmino) {}
func RegisterLegacyAminoCodec(cdc *codec.LegacyAmino) {
legacy.RegisterAminoMsg(cdc, &MsgRotateConsPubKey{}, "composable/MsgAddRateLimit")
}

func RegisterInterfaces(registry codectypes.InterfaceRegistry) {}
func RegisterInterfaces(registry codectypes.InterfaceRegistry) {
registry.RegisterImplementations(
(*sdk.Msg)(nil),
&MsgRotateConsPubKey{},
)
msgservice.RegisterMsgServiceDesc(registry, &_Msg_serviceDesc)
}

var (
amino = codec.NewLegacyAmino()
Expand Down
25 changes: 1 addition & 24 deletions x/keyrotation/types/expected_keepers.go
Original file line number Diff line number Diff line change
@@ -1,34 +1,11 @@
package types

import (
"cosmossdk.io/math"
sdk "github.com/cosmos/cosmos-sdk/types"
stakingtypes "github.com/cosmos/cosmos-sdk/x/staking/types"
)

// StakingKeeper expected staking keeper
type StakingKeeper interface {
// iterate through validators by operator address, execute func for each validator
IterateValidators(sdk.Context,
func(index int64, validator stakingtypes.ValidatorI) (stop bool))

Validator(sdk.Context, sdk.ValAddress) stakingtypes.ValidatorI // get a particular validator by operator address
ValidatorByConsAddr(sdk.Context, sdk.ConsAddress) stakingtypes.ValidatorI // get a particular validator by consensus address

// slash the validator and delegators of the validator, specifying offence height, offence power, and slash fraction
Slash(sdk.Context, sdk.ConsAddress, int64, int64, sdk.Dec) math.Int
SlashWithInfractionReason(sdk.Context, sdk.ConsAddress, int64, int64, sdk.Dec, stakingtypes.Infraction) math.Int
Jail(sdk.Context, sdk.ConsAddress) // jail a validator
Unjail(sdk.Context, sdk.ConsAddress) // unjail a validator

// Delegation allows for getting a particular delegation for a given validator
// and delegator outside the scope of the staking module.
Delegation(sdk.Context, sdk.AccAddress, sdk.ValAddress) stakingtypes.DelegationI
GetAllValidators(ctx sdk.Context) (validators []stakingtypes.Validator)

// MaxValidators returns the maximum amount of bonded validators
MaxValidators(sdk.Context) uint32

// IsValidatorJailed returns if the validator is jailed.
IsValidatorJailed(ctx sdk.Context, addr sdk.ConsAddress) bool
GetValidator(sdk.Context, sdk.ValAddress) (stakingtypes.Validator, bool)
}
2 changes: 2 additions & 0 deletions x/keyrotation/types/keys.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,4 +5,6 @@ const (

// QuerierRoute defines the module's query routing key
QuerierRoute = ModuleName

RouterKey = ModuleName
)
78 changes: 78 additions & 0 deletions x/keyrotation/types/msgs.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
package types

import (
errorsmod "cosmossdk.io/errors"
codectypes "github.com/cosmos/cosmos-sdk/codec/types"
cryptotypes "github.com/cosmos/cosmos-sdk/crypto/types"
sdk "github.com/cosmos/cosmos-sdk/types"
sdkerrors "github.com/cosmos/cosmos-sdk/types/errors"
)

const (
TypeMsgRotateConsPubKey = "rotate_cons_pubkey"
)

var (
_ sdk.Msg = &MsgRotateConsPubKey{}
)

func NewMsgRotateConsPubKey(
valAddr sdk.ValAddress,
pubKey cryptotypes.PubKey,
) (*MsgRotateConsPubKey, error) {
var pkAny *codectypes.Any
if pubKey != nil {
var err error
if pkAny, err = codectypes.NewAnyWithValue(pubKey); err != nil {
return nil, err
}
}
return &MsgRotateConsPubKey{
ValidatorAddress: valAddr.String(),
Pubkey: pkAny,
}, nil
}

// Route implements the sdk.Msg interface.
func (msg MsgRotateConsPubKey) Route() string { return RouterKey }

// Type implements the sdk.Msg interface.
func (msg MsgRotateConsPubKey) Type() string { return TypeMsgRotateConsPubKey }

// GetSigners implements the sdk.Msg interface. It returns the address(es) that
// must sign over msg.GetSignBytes().
// If the validator address is not same as delegator's, then the validator must
// sign the msg as well.
func (msg MsgRotateConsPubKey) GetSigners() []sdk.AccAddress {
valAddr, _ := sdk.ValAddressFromBech32(msg.ValidatorAddress)

valAccAddr := sdk.AccAddress(valAddr)

return []sdk.AccAddress{valAccAddr}
}

// GetSignBytes returns the message bytes to sign over.
func (msg MsgRotateConsPubKey) GetSignBytes() []byte {
bz := ModuleCdc.MustMarshalJSON(&msg)
return sdk.MustSortJSON(bz)
}

// ValidateBasic implements the sdk.Msg interface.
func (msg MsgRotateConsPubKey) ValidateBasic() error {
_, err := sdk.ValAddressFromBech32(msg.ValidatorAddress)
if err != nil {
return sdkerrors.ErrInvalidAddress.Wrapf("invalid validator address: %s", err)
}

if msg.Pubkey == nil {
return errorsmod.Wrap(sdkerrors.ErrInvalidRequest, "empty pubkey")
}

return nil
}

// UnpackInterfaces implements UnpackInterfacesMessage.UnpackInterfaces
func (msg MsgRotateConsPubKey) UnpackInterfaces(unpacker codectypes.AnyUnpacker) error {
var pubKey cryptotypes.PubKey
return unpacker.UnpackAny(msg.Pubkey, &pubKey)
}
Loading

0 comments on commit 2962e73

Please sign in to comment.