cosmos-sdk/x/slashing/handler.go

72 lines
1.9 KiB
Go
Raw Normal View History

2018-05-28 12:38:02 -07:00
package slashing
import (
sdk "github.com/cosmos/cosmos-sdk/types"
2018-12-19 19:28:38 -08:00
"github.com/cosmos/cosmos-sdk/x/slashing/tags"
2018-05-28 12:38:02 -07:00
)
func NewHandler(k Keeper) sdk.Handler {
return func(ctx sdk.Context, msg sdk.Msg) sdk.Result {
// NOTE msg already has validate basic run
switch msg := msg.(type) {
2018-08-22 08:56:13 -07:00
case MsgUnjail:
return handleMsgUnjail(ctx, msg, k)
2018-05-28 12:38:02 -07:00
default:
return sdk.ErrTxDecode("invalid message parse in staking module").Result()
}
}
}
2018-08-22 08:56:13 -07:00
// Validators must submit a transaction to unjail itself after
// having been jailed (and thus unbonded) for downtime
func handleMsgUnjail(ctx sdk.Context, msg MsgUnjail, k Keeper) sdk.Result {
2018-06-01 15:27:37 -07:00
validator := k.validatorSet.Validator(ctx, msg.ValidatorAddr)
2018-05-28 12:38:02 -07:00
if validator == nil {
return ErrNoValidatorForAddress(k.codespace).Result()
}
// cannot be unjailed if no self-delegation exists
selfDel := k.validatorSet.Delegation(ctx, sdk.AccAddress(msg.ValidatorAddr), msg.ValidatorAddr)
if selfDel == nil {
return ErrMissingSelfDelegation(k.codespace).Result()
}
if validator.ShareTokens(selfDel.GetShares()).TruncateInt().LT(validator.GetMinSelfDelegation()) {
return ErrSelfDelegationTooLowToUnjail(k.codespace).Result()
2019-02-08 12:44:19 -08:00
}
// cannot be unjailed if not jailed
2018-08-22 08:56:13 -07:00
if !validator.GetJailed() {
return ErrValidatorNotJailed(k.codespace).Result()
}
consAddr := sdk.ConsAddress(validator.GetConsPubKey().Address())
2018-05-28 14:55:39 -07:00
info, found := k.getValidatorSigningInfo(ctx, consAddr)
2018-05-28 13:08:13 -07:00
if !found {
return ErrNoValidatorForAddress(k.codespace).Result()
}
// cannot be unjailed if tombstoned
2019-01-10 17:22:49 -08:00
if info.Tombstoned {
return ErrValidatorJailed(k.codespace).Result()
}
// cannot be unjailed until out of jail
if ctx.BlockHeader().Time.Before(info.JailedUntil) {
2018-05-28 13:08:13 -07:00
return ErrValidatorJailed(k.codespace).Result()
}
2018-05-28 12:38:02 -07:00
2018-10-15 14:01:29 -07:00
// unjail the validator
k.validatorSet.Unjail(ctx, consAddr)
2018-05-28 12:38:02 -07:00
2018-12-19 19:28:38 -08:00
tags := sdk.NewTags(
tags.Action, tags.ActionValidatorUnjailed,
tags.Validator, msg.ValidatorAddr.String(),
2018-12-19 19:28:38 -08:00
)
2018-05-28 14:55:39 -07:00
2018-05-28 12:38:02 -07:00
return sdk.Result{
Tags: tags,
}
}