2018-05-28 12:38:02 -07:00
|
|
|
package slashing
|
|
|
|
|
|
|
|
import (
|
2019-04-15 09:25:04 -07:00
|
|
|
"fmt"
|
|
|
|
|
2018-05-28 12:38:02 -07:00
|
|
|
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)
|
2019-04-15 09:25:04 -07:00
|
|
|
|
2018-05-28 12:38:02 -07:00
|
|
|
default:
|
2019-04-15 09:25:04 -07:00
|
|
|
errMsg := fmt.Sprintf("unrecognized slashing message type: %T", msg)
|
|
|
|
return sdk.ErrUnknownRequest(errMsg).Result()
|
2018-05-28 12:38:02 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
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 {
|
2019-06-04 15:06:58 -07:00
|
|
|
validator := k.sk.Validator(ctx, msg.ValidatorAddr)
|
2018-05-28 12:38:02 -07:00
|
|
|
if validator == nil {
|
|
|
|
return ErrNoValidatorForAddress(k.codespace).Result()
|
|
|
|
}
|
|
|
|
|
2019-03-01 16:04:37 -08:00
|
|
|
// cannot be unjailed if no self-delegation exists
|
2019-06-04 15:06:58 -07:00
|
|
|
selfDel := k.sk.Delegation(ctx, sdk.AccAddress(msg.ValidatorAddr), msg.ValidatorAddr)
|
2019-03-01 16:04:37 -08:00
|
|
|
if selfDel == nil {
|
|
|
|
return ErrMissingSelfDelegation(k.codespace).Result()
|
|
|
|
}
|
2018-08-31 12:21:12 -07:00
|
|
|
|
2019-03-25 14:13:02 -07:00
|
|
|
if validator.TokensFromShares(selfDel.GetShares()).TruncateInt().LT(validator.GetMinSelfDelegation()) {
|
2019-03-01 16:04:37 -08:00
|
|
|
return ErrSelfDelegationTooLowToUnjail(k.codespace).Result()
|
2019-02-08 12:44:19 -08:00
|
|
|
}
|
|
|
|
|
2019-02-07 17:41:23 -08:00
|
|
|
// cannot be unjailed if not jailed
|
2019-04-02 06:43:22 -07:00
|
|
|
if !validator.IsJailed() {
|
2018-08-22 08:56:13 -07:00
|
|
|
return ErrValidatorNotJailed(k.codespace).Result()
|
2018-07-02 22:47:40 -07:00
|
|
|
}
|
|
|
|
|
2018-09-24 21:09:31 -07:00
|
|
|
consAddr := sdk.ConsAddress(validator.GetConsPubKey().Address())
|
2018-05-28 14:55:39 -07:00
|
|
|
|
2018-09-24 21:09:31 -07:00
|
|
|
info, found := k.getValidatorSigningInfo(ctx, consAddr)
|
2018-05-28 13:08:13 -07:00
|
|
|
if !found {
|
|
|
|
return ErrNoValidatorForAddress(k.codespace).Result()
|
|
|
|
}
|
|
|
|
|
2019-02-07 17:41:23 -08:00
|
|
|
// cannot be unjailed if tombstoned
|
2019-01-10 17:22:49 -08:00
|
|
|
if info.Tombstoned {
|
|
|
|
return ErrValidatorJailed(k.codespace).Result()
|
|
|
|
}
|
|
|
|
|
2018-08-31 12:21:12 -07:00
|
|
|
// cannot be unjailed until out of jail
|
2018-08-12 00:33:48 -07:00
|
|
|
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
|
2019-06-04 15:06:58 -07:00
|
|
|
k.sk.Unjail(ctx, consAddr)
|
2018-05-28 12:38:02 -07:00
|
|
|
|
2018-12-19 19:28:38 -08:00
|
|
|
tags := sdk.NewTags(
|
2019-04-09 05:55:01 -07:00
|
|
|
tags.Category, tags.TxCategory,
|
2019-04-10 10:03:44 -07:00
|
|
|
tags.Sender, 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,
|
|
|
|
}
|
|
|
|
}
|