2019-01-11 12:08:01 -08:00
|
|
|
package staking
|
2018-02-23 15:57:31 -08:00
|
|
|
|
|
|
|
import (
|
2018-12-19 19:28:38 -08:00
|
|
|
"time"
|
2018-08-12 00:33:48 -07:00
|
|
|
|
2018-11-29 09:21:45 -08:00
|
|
|
abci "github.com/tendermint/tendermint/abci/types"
|
|
|
|
"github.com/tendermint/tendermint/libs/common"
|
|
|
|
tmtypes "github.com/tendermint/tendermint/types"
|
|
|
|
|
2018-02-23 15:57:31 -08:00
|
|
|
sdk "github.com/cosmos/cosmos-sdk/types"
|
2019-01-11 12:08:01 -08:00
|
|
|
"github.com/cosmos/cosmos-sdk/x/staking/keeper"
|
|
|
|
"github.com/cosmos/cosmos-sdk/x/staking/tags"
|
|
|
|
"github.com/cosmos/cosmos-sdk/x/staking/types"
|
2018-02-23 15:57:31 -08:00
|
|
|
)
|
|
|
|
|
2018-06-26 19:00:12 -07:00
|
|
|
func NewHandler(k keeper.Keeper) sdk.Handler {
|
2018-02-27 18:07:20 -08:00
|
|
|
return func(ctx sdk.Context, msg sdk.Msg) sdk.Result {
|
2018-03-20 06:56:07 -07:00
|
|
|
// NOTE msg already has validate basic run
|
2018-03-14 11:42:50 -07:00
|
|
|
switch msg := msg.(type) {
|
2018-06-26 19:00:12 -07:00
|
|
|
case types.MsgCreateValidator:
|
2018-05-31 12:22:46 -07:00
|
|
|
return handleMsgCreateValidator(ctx, msg, k)
|
2018-06-26 19:00:12 -07:00
|
|
|
case types.MsgEditValidator:
|
2018-05-31 12:22:46 -07:00
|
|
|
return handleMsgEditValidator(ctx, msg, k)
|
2018-06-26 19:00:12 -07:00
|
|
|
case types.MsgDelegate:
|
2018-03-27 17:26:52 -07:00
|
|
|
return handleMsgDelegate(ctx, msg, k)
|
2018-06-26 19:00:12 -07:00
|
|
|
case types.MsgBeginRedelegate:
|
|
|
|
return handleMsgBeginRedelegate(ctx, msg, k)
|
2019-01-17 09:53:22 -08:00
|
|
|
case types.MsgUndelegate:
|
|
|
|
return handleMsgUndelegate(ctx, msg, k)
|
2018-03-13 11:27:52 -07:00
|
|
|
default:
|
2018-03-23 09:15:41 -07:00
|
|
|
return sdk.ErrTxDecode("invalid message parse in staking module").Result()
|
2018-02-27 18:07:20 -08:00
|
|
|
}
|
2018-02-23 15:57:31 -08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-10-19 11:36:00 -07:00
|
|
|
// Called every block, update validator set
|
2019-04-05 13:20:58 -07:00
|
|
|
func EndBlocker(ctx sdk.Context, k keeper.Keeper) ([]abci.ValidatorUpdate, sdk.Tags) {
|
2018-12-19 19:28:38 -08:00
|
|
|
resTags := sdk.NewTags()
|
|
|
|
|
2018-11-04 22:11:03 -08:00
|
|
|
// Calculate validator set changes.
|
|
|
|
//
|
|
|
|
// NOTE: ApplyAndReturnValidatorSetUpdates has to come before
|
|
|
|
// UnbondAllMatureValidatorQueue.
|
|
|
|
// This fixes a bug when the unbonding period is instant (is the case in
|
|
|
|
// some of the tests). The test expected the validator to be completely
|
|
|
|
// unbonded after the Endblocker (go from Bonded -> Unbonding during
|
|
|
|
// ApplyAndReturnValidatorSetUpdates and then Unbonding -> Unbonded during
|
|
|
|
// UnbondAllMatureValidatorQueue).
|
2018-12-19 19:28:38 -08:00
|
|
|
validatorUpdates := k.ApplyAndReturnValidatorSetUpdates(ctx)
|
2018-11-04 22:11:03 -08:00
|
|
|
|
|
|
|
// Unbond all mature validators from the unbonding queue.
|
2018-10-14 17:37:06 -07:00
|
|
|
k.UnbondAllMatureValidatorQueue(ctx)
|
|
|
|
|
2018-11-04 22:11:03 -08:00
|
|
|
// Remove all mature unbonding delegations from the ubd queue.
|
2019-01-16 02:35:18 -08:00
|
|
|
matureUnbonds := k.DequeueAllMatureUBDQueue(ctx, ctx.BlockHeader().Time)
|
2018-10-07 21:43:47 -07:00
|
|
|
for _, dvPair := range matureUnbonds {
|
2019-02-25 07:16:52 -08:00
|
|
|
err := k.CompleteUnbonding(ctx, dvPair.DelegatorAddress, dvPair.ValidatorAddress)
|
2018-10-07 21:43:47 -07:00
|
|
|
if err != nil {
|
|
|
|
continue
|
|
|
|
}
|
2018-12-19 19:28:38 -08:00
|
|
|
|
|
|
|
resTags.AppendTags(sdk.NewTags(
|
2019-04-08 15:51:02 -07:00
|
|
|
tags.Action, tags.ActionCompleteUnbonding,
|
2019-02-25 07:16:52 -08:00
|
|
|
tags.Delegator, dvPair.DelegatorAddress.String(),
|
|
|
|
tags.SrcValidator, dvPair.ValidatorAddress.String(),
|
2018-10-07 21:43:47 -07:00
|
|
|
))
|
|
|
|
}
|
|
|
|
|
2018-11-04 22:11:03 -08:00
|
|
|
// Remove all mature redelegations from the red queue.
|
2018-10-07 21:43:47 -07:00
|
|
|
matureRedelegations := k.DequeueAllMatureRedelegationQueue(ctx, ctx.BlockHeader().Time)
|
|
|
|
for _, dvvTriplet := range matureRedelegations {
|
2019-02-25 07:16:52 -08:00
|
|
|
err := k.CompleteRedelegation(ctx, dvvTriplet.DelegatorAddress,
|
|
|
|
dvvTriplet.ValidatorSrcAddress, dvvTriplet.ValidatorDstAddress)
|
2018-10-07 21:43:47 -07:00
|
|
|
if err != nil {
|
|
|
|
continue
|
|
|
|
}
|
2018-12-19 19:28:38 -08:00
|
|
|
|
|
|
|
resTags.AppendTags(sdk.NewTags(
|
2018-10-07 21:43:47 -07:00
|
|
|
tags.Action, tags.ActionCompleteRedelegation,
|
2019-04-09 05:55:01 -07:00
|
|
|
tags.Category, tags.TxCategory,
|
2019-02-25 07:16:52 -08:00
|
|
|
tags.Delegator, dvvTriplet.DelegatorAddress.String(),
|
|
|
|
tags.SrcValidator, dvvTriplet.ValidatorSrcAddress.String(),
|
|
|
|
tags.DstValidator, dvvTriplet.ValidatorDstAddress.String(),
|
2018-10-07 21:43:47 -07:00
|
|
|
))
|
|
|
|
}
|
2018-12-19 19:28:38 -08:00
|
|
|
|
2019-04-05 13:20:58 -07:00
|
|
|
return validatorUpdates, resTags
|
2018-04-04 20:22:13 -07:00
|
|
|
}
|
|
|
|
|
2018-02-23 15:57:31 -08:00
|
|
|
// These functions assume everything has been authenticated,
|
|
|
|
// now we just perform action and save
|
2018-03-14 11:42:50 -07:00
|
|
|
|
2018-06-26 19:00:12 -07:00
|
|
|
func handleMsgCreateValidator(ctx sdk.Context, msg types.MsgCreateValidator, k keeper.Keeper) sdk.Result {
|
2018-02-27 18:07:20 -08:00
|
|
|
// check to see if the pubkey or sender has been registered before
|
2019-02-25 07:16:52 -08:00
|
|
|
if _, found := k.GetValidator(ctx, msg.ValidatorAddress); found {
|
2018-07-09 17:42:57 -07:00
|
|
|
return ErrValidatorOwnerExists(k.Codespace()).Result()
|
|
|
|
}
|
2018-09-24 15:23:58 -07:00
|
|
|
|
2019-02-07 17:41:23 -08:00
|
|
|
if _, found := k.GetValidatorByConsAddr(ctx, sdk.GetConsAddress(msg.PubKey)); found {
|
2018-07-09 17:42:57 -07:00
|
|
|
return ErrValidatorPubKeyExists(k.Codespace()).Result()
|
2018-02-27 18:07:20 -08:00
|
|
|
}
|
2018-09-24 15:23:58 -07:00
|
|
|
|
2019-01-17 09:53:22 -08:00
|
|
|
if msg.Value.Denom != k.GetParams(ctx).BondDenom {
|
2018-06-26 19:00:12 -07:00
|
|
|
return ErrBadDenom(k.Codespace()).Result()
|
2018-03-13 11:27:52 -07:00
|
|
|
}
|
2018-02-27 18:07:20 -08:00
|
|
|
|
2019-01-21 16:52:03 -08:00
|
|
|
if _, err := msg.Description.EnsureLength(); err != nil {
|
|
|
|
return err.Result()
|
|
|
|
}
|
|
|
|
|
2018-11-29 09:21:45 -08:00
|
|
|
if ctx.ConsensusParams() != nil {
|
|
|
|
tmPubKey := tmtypes.TM2PB.PubKey(msg.PubKey)
|
|
|
|
if !common.StringInSlice(tmPubKey.Type, ctx.ConsensusParams().Validator.PubKeyTypes) {
|
2019-01-21 16:52:03 -08:00
|
|
|
return ErrValidatorPubKeyTypeUnsupported(k.Codespace(),
|
|
|
|
tmPubKey.Type,
|
|
|
|
ctx.ConsensusParams().Validator.PubKeyTypes).Result()
|
2018-11-29 09:21:45 -08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-02-25 07:16:52 -08:00
|
|
|
validator := NewValidator(msg.ValidatorAddress, msg.PubKey, msg.Description)
|
2018-09-24 15:23:58 -07:00
|
|
|
commission := NewCommissionWithTime(
|
2018-10-10 23:26:54 -07:00
|
|
|
msg.Commission.Rate, msg.Commission.MaxRate,
|
2018-09-24 15:23:58 -07:00
|
|
|
msg.Commission.MaxChangeRate, ctx.BlockHeader().Time,
|
|
|
|
)
|
|
|
|
validator, err := validator.SetInitialCommission(commission)
|
|
|
|
if err != nil {
|
|
|
|
return err.Result()
|
|
|
|
}
|
|
|
|
|
2019-02-08 12:44:19 -08:00
|
|
|
validator.MinSelfDelegation = msg.MinSelfDelegation
|
|
|
|
|
2018-06-26 19:00:12 -07:00
|
|
|
k.SetValidator(ctx, validator)
|
2018-09-24 21:09:31 -07:00
|
|
|
k.SetValidatorByConsAddr(ctx, validator)
|
2018-10-03 09:37:06 -07:00
|
|
|
k.SetNewValidatorByPowerIndex(ctx, validator)
|
2018-02-23 15:57:31 -08:00
|
|
|
|
2019-02-07 17:41:23 -08:00
|
|
|
// call the after-creation hook
|
2019-02-25 07:16:52 -08:00
|
|
|
k.AfterValidatorCreated(ctx, validator.OperatorAddress)
|
2018-10-22 17:49:53 -07:00
|
|
|
|
2018-06-26 19:00:12 -07:00
|
|
|
// move coins from the msg.Address account to a (self-delegation) delegator account
|
2018-05-09 21:01:58 -07:00
|
|
|
// the validator account and global shares are updated within here
|
2019-02-25 07:16:52 -08:00
|
|
|
_, err = k.Delegate(ctx, msg.DelegatorAddress, msg.Value.Amount, validator, true)
|
2018-04-03 10:03:49 -07:00
|
|
|
if err != nil {
|
|
|
|
return err.Result()
|
|
|
|
}
|
2018-06-26 19:00:12 -07:00
|
|
|
|
2019-04-09 05:55:01 -07:00
|
|
|
resTags := sdk.NewTags(
|
|
|
|
tags.Category, tags.TxCategory,
|
2019-02-25 07:16:52 -08:00
|
|
|
tags.DstValidator, msg.ValidatorAddress.String(),
|
2019-04-08 15:51:02 -07:00
|
|
|
tags.Delegator, msg.DelegatorAddress.String(),
|
2018-06-26 19:00:12 -07:00
|
|
|
)
|
2018-09-24 15:23:58 -07:00
|
|
|
|
2018-05-10 08:19:06 -07:00
|
|
|
return sdk.Result{
|
2019-04-09 05:55:01 -07:00
|
|
|
Tags: resTags,
|
2018-05-10 08:19:06 -07:00
|
|
|
}
|
2018-02-23 15:57:31 -08:00
|
|
|
}
|
|
|
|
|
2018-06-26 19:00:12 -07:00
|
|
|
func handleMsgEditValidator(ctx sdk.Context, msg types.MsgEditValidator, k keeper.Keeper) sdk.Result {
|
2018-05-09 21:01:58 -07:00
|
|
|
// validator must already be registered
|
2019-02-25 07:16:52 -08:00
|
|
|
validator, found := k.GetValidator(ctx, msg.ValidatorAddress)
|
2018-03-20 14:21:18 -07:00
|
|
|
if !found {
|
2018-06-26 19:00:12 -07:00
|
|
|
return ErrNoValidatorFound(k.Codespace()).Result()
|
2018-03-13 11:27:52 -07:00
|
|
|
}
|
2018-02-23 15:57:31 -08:00
|
|
|
|
2018-03-28 08:08:22 -07:00
|
|
|
// replace all editable fields (clients should autofill existing values)
|
2018-06-26 19:00:12 -07:00
|
|
|
description, err := validator.Description.UpdateDescription(msg.Description)
|
|
|
|
if err != nil {
|
|
|
|
return err.Result()
|
|
|
|
}
|
2018-09-24 15:23:58 -07:00
|
|
|
|
2018-06-26 19:00:12 -07:00
|
|
|
validator.Description = description
|
2018-05-09 21:01:58 -07:00
|
|
|
|
2018-09-24 15:23:58 -07:00
|
|
|
if msg.CommissionRate != nil {
|
2018-10-03 09:37:06 -07:00
|
|
|
commission, err := k.UpdateValidatorCommission(ctx, validator, *msg.CommissionRate)
|
|
|
|
if err != nil {
|
2018-09-24 15:23:58 -07:00
|
|
|
return err.Result()
|
|
|
|
}
|
2019-01-08 21:28:46 -08:00
|
|
|
|
2019-02-07 17:41:23 -08:00
|
|
|
// call the before-modification hook since we're about to update the commission
|
2019-02-25 07:16:52 -08:00
|
|
|
k.BeforeValidatorModified(ctx, msg.ValidatorAddress)
|
2019-02-07 17:41:23 -08:00
|
|
|
|
2018-10-03 09:37:06 -07:00
|
|
|
validator.Commission = commission
|
2018-09-24 15:23:58 -07:00
|
|
|
}
|
|
|
|
|
2019-02-08 12:44:19 -08:00
|
|
|
if msg.MinSelfDelegation != nil {
|
|
|
|
if !(*msg.MinSelfDelegation).GT(validator.MinSelfDelegation) {
|
|
|
|
return ErrMinSelfDelegationDecreased(k.Codespace()).Result()
|
|
|
|
}
|
|
|
|
if (*msg.MinSelfDelegation).GT(validator.Tokens) {
|
|
|
|
return ErrSelfDelegationBelowMinimum(k.Codespace()).Result()
|
|
|
|
}
|
|
|
|
validator.MinSelfDelegation = (*msg.MinSelfDelegation)
|
|
|
|
}
|
|
|
|
|
2018-07-27 21:42:07 -07:00
|
|
|
k.SetValidator(ctx, validator)
|
2018-09-24 15:23:58 -07:00
|
|
|
|
2019-04-08 15:51:02 -07:00
|
|
|
resTags := sdk.NewTags(
|
2019-02-25 07:16:52 -08:00
|
|
|
tags.DstValidator, msg.ValidatorAddress.String(),
|
2018-05-09 21:01:58 -07:00
|
|
|
)
|
2018-09-24 15:23:58 -07:00
|
|
|
|
2018-05-10 12:55:51 -07:00
|
|
|
return sdk.Result{
|
2019-04-08 15:51:02 -07:00
|
|
|
Tags: resTags,
|
2018-05-10 12:55:51 -07:00
|
|
|
}
|
2018-02-23 15:57:31 -08:00
|
|
|
}
|
|
|
|
|
2018-06-26 19:00:12 -07:00
|
|
|
func handleMsgDelegate(ctx sdk.Context, msg types.MsgDelegate, k keeper.Keeper) sdk.Result {
|
2019-02-25 07:16:52 -08:00
|
|
|
validator, found := k.GetValidator(ctx, msg.ValidatorAddress)
|
2018-03-20 14:21:18 -07:00
|
|
|
if !found {
|
2018-06-26 19:00:12 -07:00
|
|
|
return ErrNoValidatorFound(k.Codespace()).Result()
|
2018-02-27 18:07:20 -08:00
|
|
|
}
|
2018-08-31 12:21:12 -07:00
|
|
|
|
2019-03-25 14:13:02 -07:00
|
|
|
if msg.Amount.Denom != k.GetParams(ctx).BondDenom {
|
2018-06-26 19:00:12 -07:00
|
|
|
return ErrBadDenom(k.Codespace()).Result()
|
2018-02-27 18:07:20 -08:00
|
|
|
}
|
2018-08-31 12:21:12 -07:00
|
|
|
|
2019-03-25 14:13:02 -07:00
|
|
|
_, err := k.Delegate(ctx, msg.DelegatorAddress, msg.Amount.Amount, validator, true)
|
2018-04-03 10:03:49 -07:00
|
|
|
if err != nil {
|
|
|
|
return err.Result()
|
|
|
|
}
|
2018-06-26 19:00:12 -07:00
|
|
|
|
2019-04-08 15:51:02 -07:00
|
|
|
resTags := sdk.NewTags(
|
2019-04-09 05:55:01 -07:00
|
|
|
tags.Category, tags.TxCategory,
|
2019-02-25 07:16:52 -08:00
|
|
|
tags.Delegator, msg.DelegatorAddress.String(),
|
|
|
|
tags.DstValidator, msg.ValidatorAddress.String(),
|
2018-06-26 19:00:12 -07:00
|
|
|
)
|
2018-08-31 12:21:12 -07:00
|
|
|
|
2018-05-10 08:19:06 -07:00
|
|
|
return sdk.Result{
|
2019-04-08 15:51:02 -07:00
|
|
|
Tags: resTags,
|
2018-05-10 08:19:06 -07:00
|
|
|
}
|
2018-02-23 15:57:31 -08:00
|
|
|
}
|
|
|
|
|
2019-01-17 09:53:22 -08:00
|
|
|
func handleMsgUndelegate(ctx sdk.Context, msg types.MsgUndelegate, k keeper.Keeper) sdk.Result {
|
2019-03-25 14:13:02 -07:00
|
|
|
shares, err := k.ValidateUnbondAmount(
|
|
|
|
ctx, msg.DelegatorAddress, msg.ValidatorAddress, msg.Amount.Amount,
|
|
|
|
)
|
|
|
|
if err != nil {
|
|
|
|
return err.Result()
|
|
|
|
}
|
|
|
|
|
|
|
|
completionTime, err := k.Undelegate(ctx, msg.DelegatorAddress, msg.ValidatorAddress, shares)
|
2018-03-16 13:36:16 -07:00
|
|
|
if err != nil {
|
2018-06-26 19:00:12 -07:00
|
|
|
return err.Result()
|
2018-03-16 13:36:16 -07:00
|
|
|
}
|
2018-04-03 19:26:39 -07:00
|
|
|
|
2019-01-16 02:35:18 -08:00
|
|
|
finishTime := types.MsgCdc.MustMarshalBinaryLengthPrefixed(completionTime)
|
2019-04-08 15:51:02 -07:00
|
|
|
resTags := sdk.NewTags(
|
2019-04-09 05:55:01 -07:00
|
|
|
tags.Category, tags.TxCategory,
|
2019-02-25 07:16:52 -08:00
|
|
|
tags.Delegator, msg.DelegatorAddress.String(),
|
|
|
|
tags.SrcValidator, msg.ValidatorAddress.String(),
|
2019-02-04 18:20:56 -08:00
|
|
|
tags.EndTime, completionTime.Format(time.RFC3339),
|
2018-06-26 19:00:12 -07:00
|
|
|
)
|
2018-12-19 19:28:38 -08:00
|
|
|
|
2019-04-08 15:51:02 -07:00
|
|
|
return sdk.Result{Data: finishTime, Tags: resTags}
|
2018-06-26 19:00:12 -07:00
|
|
|
}
|
2018-02-23 15:57:31 -08:00
|
|
|
|
2018-06-26 19:00:12 -07:00
|
|
|
func handleMsgBeginRedelegate(ctx sdk.Context, msg types.MsgBeginRedelegate, k keeper.Keeper) sdk.Result {
|
2019-03-25 14:13:02 -07:00
|
|
|
shares, err := k.ValidateUnbondAmount(
|
|
|
|
ctx, msg.DelegatorAddress, msg.ValidatorSrcAddress, msg.Amount.Amount,
|
|
|
|
)
|
|
|
|
if err != nil {
|
|
|
|
return err.Result()
|
|
|
|
}
|
|
|
|
|
|
|
|
completionTime, err := k.BeginRedelegation(
|
|
|
|
ctx, msg.DelegatorAddress, msg.ValidatorSrcAddress, msg.ValidatorDstAddress, shares,
|
|
|
|
)
|
2018-06-26 19:00:12 -07:00
|
|
|
if err != nil {
|
|
|
|
return err.Result()
|
2018-02-23 15:57:31 -08:00
|
|
|
}
|
|
|
|
|
2019-01-16 02:35:18 -08:00
|
|
|
finishTime := types.MsgCdc.MustMarshalBinaryLengthPrefixed(completionTime)
|
2018-12-19 19:28:38 -08:00
|
|
|
resTags := sdk.NewTags(
|
2019-04-09 05:55:01 -07:00
|
|
|
tags.Category, tags.TxCategory,
|
2019-02-25 07:16:52 -08:00
|
|
|
tags.Delegator, msg.DelegatorAddress.String(),
|
|
|
|
tags.SrcValidator, msg.ValidatorSrcAddress.String(),
|
|
|
|
tags.DstValidator, msg.ValidatorDstAddress.String(),
|
2019-02-04 18:20:56 -08:00
|
|
|
tags.EndTime, completionTime.Format(time.RFC3339),
|
2018-06-26 19:00:12 -07:00
|
|
|
)
|
2018-12-19 19:28:38 -08:00
|
|
|
|
|
|
|
return sdk.Result{Data: finishTime, Tags: resTags}
|
2018-02-23 15:57:31 -08:00
|
|
|
}
|