2020-10-16 07:53:49 -07:00
|
|
|
package keeper
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"time"
|
|
|
|
|
|
|
|
metrics "github.com/armon/go-metrics"
|
|
|
|
tmstrings "github.com/tendermint/tendermint/libs/strings"
|
|
|
|
|
2020-10-19 06:04:55 -07:00
|
|
|
cryptotypes "github.com/cosmos/cosmos-sdk/crypto/types"
|
2020-10-16 07:53:49 -07:00
|
|
|
"github.com/cosmos/cosmos-sdk/telemetry"
|
|
|
|
sdk "github.com/cosmos/cosmos-sdk/types"
|
|
|
|
sdkerrors "github.com/cosmos/cosmos-sdk/types/errors"
|
|
|
|
"github.com/cosmos/cosmos-sdk/x/staking/types"
|
|
|
|
)
|
|
|
|
|
|
|
|
type msgServer struct {
|
|
|
|
Keeper
|
|
|
|
}
|
|
|
|
|
|
|
|
// NewMsgServerImpl returns an implementation of the bank MsgServer interface
|
|
|
|
// for the provided Keeper.
|
|
|
|
func NewMsgServerImpl(keeper Keeper) types.MsgServer {
|
|
|
|
return &msgServer{Keeper: keeper}
|
|
|
|
}
|
|
|
|
|
|
|
|
var _ types.MsgServer = msgServer{}
|
|
|
|
|
|
|
|
func (k msgServer) CreateValidator(goCtx context.Context, msg *types.MsgCreateValidator) (*types.MsgCreateValidatorResponse, error) {
|
|
|
|
ctx := sdk.UnwrapSDKContext(goCtx)
|
|
|
|
|
|
|
|
valAddr, err := sdk.ValAddressFromBech32(msg.ValidatorAddress)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
// check to see if the pubkey or sender has been registered before
|
|
|
|
if _, found := k.GetValidator(ctx, valAddr); found {
|
|
|
|
return nil, types.ErrValidatorOwnerExists
|
|
|
|
}
|
|
|
|
|
2020-10-19 06:04:55 -07:00
|
|
|
pk, ok := msg.Pubkey.GetCachedValue().(cryptotypes.PubKey)
|
|
|
|
if !ok {
|
2020-11-09 08:01:43 -08:00
|
|
|
return nil, sdkerrors.Wrapf(sdkerrors.ErrInvalidType, "Expecting cryptotypes.PubKey, got %T", pk)
|
2020-10-16 07:53:49 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
if _, found := k.GetValidatorByConsAddr(ctx, sdk.GetConsAddress(pk)); found {
|
|
|
|
return nil, types.ErrValidatorPubKeyExists
|
|
|
|
}
|
|
|
|
|
|
|
|
bondDenom := k.BondDenom(ctx)
|
|
|
|
if msg.Value.Denom != bondDenom {
|
|
|
|
return nil, sdkerrors.Wrapf(types.ErrBadDenom, "got %s, expected %s", msg.Value.Denom, bondDenom)
|
|
|
|
}
|
|
|
|
|
|
|
|
if _, err := msg.Description.EnsureLength(); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
cp := ctx.ConsensusParams()
|
|
|
|
if cp != nil && cp.Validator != nil {
|
|
|
|
if !tmstrings.StringInSlice(pk.Type(), cp.Validator.PubKeyTypes) {
|
|
|
|
return nil, sdkerrors.Wrapf(
|
|
|
|
types.ErrValidatorPubKeyTypeNotSupported,
|
|
|
|
"got: %s, expected: %s", pk.Type(), cp.Validator.PubKeyTypes,
|
|
|
|
)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-10-23 05:07:52 -07:00
|
|
|
validator, err := types.NewValidator(valAddr, pk, msg.Description)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2020-10-16 07:53:49 -07:00
|
|
|
commission := types.NewCommissionWithTime(
|
|
|
|
msg.Commission.Rate, msg.Commission.MaxRate,
|
|
|
|
msg.Commission.MaxChangeRate, ctx.BlockHeader().Time,
|
|
|
|
)
|
|
|
|
|
|
|
|
validator, err = validator.SetInitialCommission(commission)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
delegatorAddress, err := sdk.AccAddressFromBech32(msg.DelegatorAddress)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
validator.MinSelfDelegation = msg.MinSelfDelegation
|
|
|
|
|
|
|
|
k.SetValidator(ctx, validator)
|
|
|
|
k.SetValidatorByConsAddr(ctx, validator)
|
|
|
|
k.SetNewValidatorByPowerIndex(ctx, validator)
|
|
|
|
|
|
|
|
// call the after-creation hook
|
|
|
|
k.AfterValidatorCreated(ctx, validator.GetOperator())
|
|
|
|
|
|
|
|
// move coins from the msg.Address account to a (self-delegation) delegator account
|
|
|
|
// the validator account and global shares are updated within here
|
|
|
|
// NOTE source will always be from a wallet which are unbonded
|
|
|
|
_, err = k.Keeper.Delegate(ctx, delegatorAddress, msg.Value.Amount, types.Unbonded, validator, true)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
ctx.EventManager().EmitEvents(sdk.Events{
|
|
|
|
sdk.NewEvent(
|
|
|
|
types.EventTypeCreateValidator,
|
|
|
|
sdk.NewAttribute(types.AttributeKeyValidator, msg.ValidatorAddress),
|
|
|
|
sdk.NewAttribute(sdk.AttributeKeyAmount, msg.Value.Amount.String()),
|
|
|
|
),
|
|
|
|
sdk.NewEvent(
|
|
|
|
sdk.EventTypeMessage,
|
|
|
|
sdk.NewAttribute(sdk.AttributeKeyModule, types.AttributeValueCategory),
|
|
|
|
sdk.NewAttribute(sdk.AttributeKeySender, msg.DelegatorAddress),
|
|
|
|
),
|
|
|
|
})
|
|
|
|
|
|
|
|
return &types.MsgCreateValidatorResponse{}, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (k msgServer) EditValidator(goCtx context.Context, msg *types.MsgEditValidator) (*types.MsgEditValidatorResponse, error) {
|
|
|
|
ctx := sdk.UnwrapSDKContext(goCtx)
|
|
|
|
valAddr, err := sdk.ValAddressFromBech32(msg.ValidatorAddress)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
// validator must already be registered
|
|
|
|
validator, found := k.GetValidator(ctx, valAddr)
|
|
|
|
if !found {
|
|
|
|
return nil, types.ErrNoValidatorFound
|
|
|
|
}
|
|
|
|
|
|
|
|
// replace all editable fields (clients should autofill existing values)
|
|
|
|
description, err := validator.Description.UpdateDescription(msg.Description)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
validator.Description = description
|
|
|
|
|
|
|
|
if msg.CommissionRate != nil {
|
|
|
|
commission, err := k.UpdateValidatorCommission(ctx, validator, *msg.CommissionRate)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
// call the before-modification hook since we're about to update the commission
|
|
|
|
k.BeforeValidatorModified(ctx, valAddr)
|
|
|
|
|
|
|
|
validator.Commission = commission
|
|
|
|
}
|
|
|
|
|
|
|
|
if msg.MinSelfDelegation != nil {
|
|
|
|
if !msg.MinSelfDelegation.GT(validator.MinSelfDelegation) {
|
|
|
|
return nil, types.ErrMinSelfDelegationDecreased
|
|
|
|
}
|
|
|
|
|
|
|
|
if msg.MinSelfDelegation.GT(validator.Tokens) {
|
|
|
|
return nil, types.ErrSelfDelegationBelowMinimum
|
|
|
|
}
|
|
|
|
|
|
|
|
validator.MinSelfDelegation = (*msg.MinSelfDelegation)
|
|
|
|
}
|
|
|
|
|
|
|
|
k.SetValidator(ctx, validator)
|
|
|
|
|
|
|
|
ctx.EventManager().EmitEvents(sdk.Events{
|
|
|
|
sdk.NewEvent(
|
|
|
|
types.EventTypeEditValidator,
|
|
|
|
sdk.NewAttribute(types.AttributeKeyCommissionRate, validator.Commission.String()),
|
|
|
|
sdk.NewAttribute(types.AttributeKeyMinSelfDelegation, validator.MinSelfDelegation.String()),
|
|
|
|
),
|
|
|
|
sdk.NewEvent(
|
|
|
|
sdk.EventTypeMessage,
|
|
|
|
sdk.NewAttribute(sdk.AttributeKeyModule, types.AttributeValueCategory),
|
|
|
|
sdk.NewAttribute(sdk.AttributeKeySender, msg.ValidatorAddress),
|
|
|
|
),
|
|
|
|
})
|
|
|
|
|
|
|
|
return &types.MsgEditValidatorResponse{}, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (k msgServer) Delegate(goCtx context.Context, msg *types.MsgDelegate) (*types.MsgDelegateResponse, error) {
|
|
|
|
ctx := sdk.UnwrapSDKContext(goCtx)
|
|
|
|
valAddr, valErr := sdk.ValAddressFromBech32(msg.ValidatorAddress)
|
|
|
|
if valErr != nil {
|
|
|
|
return nil, valErr
|
|
|
|
}
|
|
|
|
|
|
|
|
validator, found := k.GetValidator(ctx, valAddr)
|
|
|
|
if !found {
|
|
|
|
return nil, types.ErrNoValidatorFound
|
|
|
|
}
|
|
|
|
|
|
|
|
delegatorAddress, err := sdk.AccAddressFromBech32(msg.DelegatorAddress)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
bondDenom := k.BondDenom(ctx)
|
|
|
|
if msg.Amount.Denom != bondDenom {
|
|
|
|
return nil, sdkerrors.Wrapf(types.ErrBadDenom, "got %s, expected %s", msg.Amount.Denom, bondDenom)
|
|
|
|
}
|
|
|
|
|
|
|
|
// NOTE: source funds are always unbonded
|
2021-04-28 23:57:46 -07:00
|
|
|
newShares, err := k.Keeper.Delegate(ctx, delegatorAddress, msg.Amount.Amount, types.Unbonded, validator, true)
|
2020-10-16 07:53:49 -07:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2020-11-10 05:05:15 -08:00
|
|
|
if msg.Amount.Amount.IsInt64() {
|
|
|
|
defer func() {
|
|
|
|
telemetry.IncrCounter(1, types.ModuleName, "delegate")
|
|
|
|
telemetry.SetGaugeWithLabels(
|
|
|
|
[]string{"tx", "msg", msg.Type()},
|
|
|
|
float32(msg.Amount.Amount.Int64()),
|
|
|
|
[]metrics.Label{telemetry.NewLabel("denom", msg.Amount.Denom)},
|
|
|
|
)
|
|
|
|
}()
|
|
|
|
}
|
2020-10-16 07:53:49 -07:00
|
|
|
|
|
|
|
ctx.EventManager().EmitEvents(sdk.Events{
|
|
|
|
sdk.NewEvent(
|
|
|
|
types.EventTypeDelegate,
|
|
|
|
sdk.NewAttribute(types.AttributeKeyValidator, msg.ValidatorAddress),
|
|
|
|
sdk.NewAttribute(sdk.AttributeKeyAmount, msg.Amount.Amount.String()),
|
2021-04-28 23:57:46 -07:00
|
|
|
sdk.NewAttribute(types.AttributeKeyNewShares, newShares.String()),
|
2020-10-16 07:53:49 -07:00
|
|
|
),
|
|
|
|
sdk.NewEvent(
|
|
|
|
sdk.EventTypeMessage,
|
|
|
|
sdk.NewAttribute(sdk.AttributeKeyModule, types.AttributeValueCategory),
|
|
|
|
sdk.NewAttribute(sdk.AttributeKeySender, msg.DelegatorAddress),
|
|
|
|
),
|
|
|
|
})
|
|
|
|
|
|
|
|
return &types.MsgDelegateResponse{}, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (k msgServer) BeginRedelegate(goCtx context.Context, msg *types.MsgBeginRedelegate) (*types.MsgBeginRedelegateResponse, error) {
|
|
|
|
ctx := sdk.UnwrapSDKContext(goCtx)
|
|
|
|
valSrcAddr, err := sdk.ValAddressFromBech32(msg.ValidatorSrcAddress)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
delegatorAddress, err := sdk.AccAddressFromBech32(msg.DelegatorAddress)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
shares, err := k.ValidateUnbondAmount(
|
|
|
|
ctx, delegatorAddress, valSrcAddr, msg.Amount.Amount,
|
|
|
|
)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
bondDenom := k.BondDenom(ctx)
|
|
|
|
if msg.Amount.Denom != bondDenom {
|
|
|
|
return nil, sdkerrors.Wrapf(types.ErrBadDenom, "got %s, expected %s", msg.Amount.Denom, bondDenom)
|
|
|
|
}
|
|
|
|
|
|
|
|
valDstAddr, err := sdk.ValAddressFromBech32(msg.ValidatorDstAddress)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
completionTime, err := k.BeginRedelegation(
|
|
|
|
ctx, delegatorAddress, valSrcAddr, valDstAddr, shares,
|
|
|
|
)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2020-11-10 08:07:40 -08:00
|
|
|
if msg.Amount.Amount.IsInt64() {
|
|
|
|
defer func() {
|
|
|
|
telemetry.IncrCounter(1, types.ModuleName, "redelegate")
|
|
|
|
telemetry.SetGaugeWithLabels(
|
|
|
|
[]string{"tx", "msg", msg.Type()},
|
|
|
|
float32(msg.Amount.Amount.Int64()),
|
|
|
|
[]metrics.Label{telemetry.NewLabel("denom", msg.Amount.Denom)},
|
|
|
|
)
|
|
|
|
}()
|
|
|
|
}
|
2020-10-16 07:53:49 -07:00
|
|
|
|
|
|
|
ctx.EventManager().EmitEvents(sdk.Events{
|
|
|
|
sdk.NewEvent(
|
|
|
|
types.EventTypeRedelegate,
|
|
|
|
sdk.NewAttribute(types.AttributeKeySrcValidator, msg.ValidatorSrcAddress),
|
|
|
|
sdk.NewAttribute(types.AttributeKeyDstValidator, msg.ValidatorDstAddress),
|
|
|
|
sdk.NewAttribute(sdk.AttributeKeyAmount, msg.Amount.Amount.String()),
|
|
|
|
sdk.NewAttribute(types.AttributeKeyCompletionTime, completionTime.Format(time.RFC3339)),
|
|
|
|
),
|
|
|
|
sdk.NewEvent(
|
|
|
|
sdk.EventTypeMessage,
|
|
|
|
sdk.NewAttribute(sdk.AttributeKeyModule, types.AttributeValueCategory),
|
|
|
|
sdk.NewAttribute(sdk.AttributeKeySender, msg.DelegatorAddress),
|
|
|
|
),
|
|
|
|
})
|
|
|
|
|
|
|
|
return &types.MsgBeginRedelegateResponse{
|
|
|
|
CompletionTime: completionTime,
|
|
|
|
}, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (k msgServer) Undelegate(goCtx context.Context, msg *types.MsgUndelegate) (*types.MsgUndelegateResponse, error) {
|
|
|
|
ctx := sdk.UnwrapSDKContext(goCtx)
|
|
|
|
|
|
|
|
addr, err := sdk.ValAddressFromBech32(msg.ValidatorAddress)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
delegatorAddress, err := sdk.AccAddressFromBech32(msg.DelegatorAddress)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
shares, err := k.ValidateUnbondAmount(
|
|
|
|
ctx, delegatorAddress, addr, msg.Amount.Amount,
|
|
|
|
)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
bondDenom := k.BondDenom(ctx)
|
|
|
|
if msg.Amount.Denom != bondDenom {
|
|
|
|
return nil, sdkerrors.Wrapf(types.ErrBadDenom, "got %s, expected %s", msg.Amount.Denom, bondDenom)
|
|
|
|
}
|
|
|
|
|
|
|
|
completionTime, err := k.Keeper.Undelegate(ctx, delegatorAddress, addr, shares)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2020-11-10 05:05:15 -08:00
|
|
|
if msg.Amount.Amount.IsInt64() {
|
|
|
|
defer func() {
|
|
|
|
telemetry.IncrCounter(1, types.ModuleName, "undelegate")
|
|
|
|
telemetry.SetGaugeWithLabels(
|
|
|
|
[]string{"tx", "msg", msg.Type()},
|
|
|
|
float32(msg.Amount.Amount.Int64()),
|
|
|
|
[]metrics.Label{telemetry.NewLabel("denom", msg.Amount.Denom)},
|
|
|
|
)
|
|
|
|
}()
|
|
|
|
}
|
2020-10-16 07:53:49 -07:00
|
|
|
|
|
|
|
ctx.EventManager().EmitEvents(sdk.Events{
|
|
|
|
sdk.NewEvent(
|
|
|
|
types.EventTypeUnbond,
|
|
|
|
sdk.NewAttribute(types.AttributeKeyValidator, msg.ValidatorAddress),
|
|
|
|
sdk.NewAttribute(sdk.AttributeKeyAmount, msg.Amount.Amount.String()),
|
|
|
|
sdk.NewAttribute(types.AttributeKeyCompletionTime, completionTime.Format(time.RFC3339)),
|
|
|
|
),
|
|
|
|
sdk.NewEvent(
|
|
|
|
sdk.EventTypeMessage,
|
|
|
|
sdk.NewAttribute(sdk.AttributeKeyModule, types.AttributeValueCategory),
|
|
|
|
sdk.NewAttribute(sdk.AttributeKeySender, msg.DelegatorAddress),
|
|
|
|
),
|
|
|
|
})
|
|
|
|
|
|
|
|
return &types.MsgUndelegateResponse{
|
|
|
|
CompletionTime: completionTime,
|
|
|
|
}, nil
|
|
|
|
}
|