2018-02-23 15:57:31 -08:00
|
|
|
package stake
|
|
|
|
|
|
|
|
import (
|
2018-08-31 12:21:12 -07:00
|
|
|
"bytes"
|
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"
|
2018-06-26 19:00:12 -07:00
|
|
|
"github.com/cosmos/cosmos-sdk/x/stake/keeper"
|
|
|
|
"github.com/cosmos/cosmos-sdk/x/stake/tags"
|
|
|
|
"github.com/cosmos/cosmos-sdk/x/stake/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)
|
|
|
|
case types.MsgBeginUnbonding:
|
|
|
|
return handleMsgBeginUnbonding(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
|
2018-12-19 19:28:38 -08:00
|
|
|
func EndBlocker(ctx sdk.Context, k keeper.Keeper) ([]abci.ValidatorUpdate, sdk.Tags) {
|
|
|
|
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.
|
2018-10-07 21:43:47 -07:00
|
|
|
matureUnbonds := k.DequeueAllMatureUnbondingQueue(ctx, ctx.BlockHeader().Time)
|
|
|
|
for _, dvPair := range matureUnbonds {
|
|
|
|
err := k.CompleteUnbonding(ctx, dvPair.DelegatorAddr, dvPair.ValidatorAddr)
|
|
|
|
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, ActionCompleteUnbonding,
|
|
|
|
tags.Delegator, []byte(dvPair.DelegatorAddr.String()),
|
|
|
|
tags.SrcValidator, []byte(dvPair.ValidatorAddr.String()),
|
|
|
|
))
|
|
|
|
}
|
|
|
|
|
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 {
|
|
|
|
err := k.CompleteRedelegation(ctx, dvvTriplet.DelegatorAddr, dvvTriplet.ValidatorSrcAddr, dvvTriplet.ValidatorDstAddr)
|
|
|
|
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,
|
|
|
|
tags.Delegator, []byte(dvvTriplet.DelegatorAddr.String()),
|
|
|
|
tags.SrcValidator, []byte(dvvTriplet.ValidatorSrcAddr.String()),
|
|
|
|
tags.DstValidator, []byte(dvvTriplet.ValidatorDstAddr.String()),
|
|
|
|
))
|
|
|
|
}
|
2018-12-19 19:28:38 -08: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
|
2018-05-09 21:01:58 -07:00
|
|
|
_, found := k.GetValidator(ctx, msg.ValidatorAddr)
|
2018-03-20 14:21:18 -07:00
|
|
|
if found {
|
2018-07-09 17:42:57 -07:00
|
|
|
return ErrValidatorOwnerExists(k.Codespace()).Result()
|
|
|
|
}
|
2018-09-24 15:23:58 -07:00
|
|
|
|
2018-09-24 21:09:31 -07:00
|
|
|
_, found = k.GetValidatorByConsAddr(ctx, sdk.GetConsAddress(msg.PubKey))
|
2018-07-09 17:42:57 -07:00
|
|
|
if found {
|
|
|
|
return ErrValidatorPubKeyExists(k.Codespace()).Result()
|
2018-02-27 18:07:20 -08:00
|
|
|
}
|
2018-09-24 15:23:58 -07:00
|
|
|
|
2018-07-10 17:16:37 -07:00
|
|
|
if msg.Delegation.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
|
|
|
|
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) {
|
|
|
|
return ErrValidatorPubKeyTypeUnsupported(k.Codespace(), tmPubKey.Type, ctx.ConsensusParams().Validator.PubKeyTypes).Result()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-05-09 21:01:58 -07:00
|
|
|
validator := NewValidator(msg.ValidatorAddr, 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()
|
|
|
|
}
|
|
|
|
|
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-01-08 21:28:46 -08:00
|
|
|
k.AfterValidatorCreated(ctx, validator.OperatorAddr)
|
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
|
2018-09-24 15:23:58 -07:00
|
|
|
_, err = k.Delegate(ctx, msg.DelegatorAddr, msg.Delegation, validator, true)
|
2018-04-03 10:03:49 -07:00
|
|
|
if err != nil {
|
|
|
|
return err.Result()
|
|
|
|
}
|
2018-06-26 19:00:12 -07:00
|
|
|
|
|
|
|
tags := sdk.NewTags(
|
|
|
|
tags.DstValidator, []byte(msg.ValidatorAddr.String()),
|
|
|
|
tags.Moniker, []byte(msg.Description.Moniker),
|
|
|
|
tags.Identity, []byte(msg.Description.Identity),
|
|
|
|
)
|
2018-09-24 15:23:58 -07:00
|
|
|
|
2018-05-10 08:19:06 -07:00
|
|
|
return sdk.Result{
|
|
|
|
Tags: tags,
|
|
|
|
}
|
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
|
|
|
|
validator, found := k.GetValidator(ctx, msg.ValidatorAddr)
|
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
|
|
|
|
|
|
|
k.BeforeValidatorModified(ctx, msg.ValidatorAddr)
|
2018-10-03 09:37:06 -07:00
|
|
|
validator.Commission = commission
|
2018-09-24 15:23:58 -07:00
|
|
|
}
|
|
|
|
|
2018-07-27 21:42:07 -07:00
|
|
|
k.SetValidator(ctx, validator)
|
2018-09-24 15:23:58 -07:00
|
|
|
|
2018-05-09 21:01:58 -07:00
|
|
|
tags := sdk.NewTags(
|
2018-06-26 19:00:12 -07:00
|
|
|
tags.DstValidator, []byte(msg.ValidatorAddr.String()),
|
|
|
|
tags.Moniker, []byte(description.Moniker),
|
|
|
|
tags.Identity, []byte(description.Identity),
|
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{
|
|
|
|
Tags: tags,
|
|
|
|
}
|
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 {
|
2018-05-09 21:01:58 -07:00
|
|
|
validator, found := k.GetValidator(ctx, msg.ValidatorAddr)
|
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
|
|
|
|
2018-07-10 17:16:37 -07:00
|
|
|
if msg.Delegation.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
|
|
|
|
2018-09-08 01:44:58 -07:00
|
|
|
if validator.Jailed && !bytes.Equal(validator.OperatorAddr, msg.DelegatorAddr) {
|
2018-08-22 08:56:13 -07:00
|
|
|
return ErrValidatorJailed(k.Codespace()).Result()
|
2018-03-13 11:27:52 -07:00
|
|
|
}
|
2018-08-31 12:21:12 -07:00
|
|
|
|
2018-07-12 16:38:35 -07:00
|
|
|
_, err := k.Delegate(ctx, msg.DelegatorAddr, msg.Delegation, validator, true)
|
2018-04-03 10:03:49 -07:00
|
|
|
if err != nil {
|
|
|
|
return err.Result()
|
|
|
|
}
|
2018-06-26 19:00:12 -07:00
|
|
|
|
|
|
|
tags := sdk.NewTags(
|
|
|
|
tags.Delegator, []byte(msg.DelegatorAddr.String()),
|
|
|
|
tags.DstValidator, []byte(msg.ValidatorAddr.String()),
|
|
|
|
)
|
2018-08-31 12:21:12 -07:00
|
|
|
|
2018-05-10 08:19:06 -07:00
|
|
|
return sdk.Result{
|
|
|
|
Tags: tags,
|
|
|
|
}
|
2018-02-23 15:57:31 -08:00
|
|
|
}
|
|
|
|
|
2018-06-26 19:00:12 -07:00
|
|
|
func handleMsgBeginUnbonding(ctx sdk.Context, msg types.MsgBeginUnbonding, k keeper.Keeper) sdk.Result {
|
2018-10-07 21:43:47 -07:00
|
|
|
ubd, err := k.BeginUnbonding(ctx, msg.DelegatorAddr, msg.ValidatorAddr, msg.SharesAmount)
|
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
|
|
|
|
2018-11-04 18:28:38 -08:00
|
|
|
finishTime := types.MsgCdc.MustMarshalBinaryLengthPrefixed(ubd.MinTime)
|
2018-06-26 19:00:12 -07:00
|
|
|
tags := sdk.NewTags(
|
|
|
|
tags.Delegator, []byte(msg.DelegatorAddr.String()),
|
|
|
|
tags.SrcValidator, []byte(msg.ValidatorAddr.String()),
|
2018-12-19 19:28:38 -08:00
|
|
|
tags.EndTime, []byte(ubd.MinTime.Format(time.RFC3339)),
|
2018-06-26 19:00:12 -07:00
|
|
|
)
|
2018-12-19 19:28:38 -08:00
|
|
|
|
2018-10-07 21:43:47 -07:00
|
|
|
return sdk.Result{Data: finishTime, Tags: tags}
|
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 {
|
2018-10-07 21:43:47 -07:00
|
|
|
red, err := k.BeginRedelegation(ctx, msg.DelegatorAddr, msg.ValidatorSrcAddr,
|
2018-06-26 19:00:12 -07:00
|
|
|
msg.ValidatorDstAddr, msg.SharesAmount)
|
|
|
|
if err != nil {
|
|
|
|
return err.Result()
|
2018-02-23 15:57:31 -08:00
|
|
|
}
|
|
|
|
|
2018-11-04 18:28:38 -08:00
|
|
|
finishTime := types.MsgCdc.MustMarshalBinaryLengthPrefixed(red.MinTime)
|
2018-12-19 19:28:38 -08:00
|
|
|
resTags := sdk.NewTags(
|
2018-06-26 19:00:12 -07:00
|
|
|
tags.Delegator, []byte(msg.DelegatorAddr.String()),
|
|
|
|
tags.SrcValidator, []byte(msg.ValidatorSrcAddr.String()),
|
|
|
|
tags.DstValidator, []byte(msg.ValidatorDstAddr.String()),
|
2018-12-19 19:28:38 -08:00
|
|
|
tags.EndTime, []byte(red.MinTime.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
|
|
|
}
|