-
Notifications
You must be signed in to change notification settings - Fork 182
/
handler.go
50 lines (41 loc) · 1.48 KB
/
handler.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
package slashing
import (
sdk "github.com/okex/exchain/libs/cosmos-sdk/types"
sdkerrors "github.com/okex/exchain/libs/cosmos-sdk/types/errors"
"github.com/okex/exchain/x/slashing/internal/types"
)
// NewHandler creates an sdk.Handler for all the slashing type messages
func NewHandler(k Keeper) sdk.Handler {
return func(ctx sdk.Context, msg sdk.Msg) (*sdk.Result, error) {
ctx.SetEventManager(sdk.NewEventManager())
switch msg := msg.(type) {
case MsgUnjail:
return handleMsgUnjail(ctx, msg, k)
default:
return nil, sdkerrors.Wrapf(sdkerrors.ErrUnknownRequest, "unrecognized %s message type: %T", ModuleName, msg)
}
}
}
// 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, error) {
validator := k.GetStakingKeeper().Validator(ctx, msg.ValidatorAddr)
if validator == nil {
return nil, sdkerrors.Wrapf(ErrNoValidatorForAddress, "Unjail failed")
}
if validator.GetMinSelfDelegation().IsZero() {
return nil, sdkerrors.Wrapf(ErrMissingSelfDelegation, "Unjail failed")
}
err := k.Unjail(ctx, msg.ValidatorAddr)
if err != nil {
return nil, err
}
ctx.EventManager().EmitEvent(
sdk.NewEvent(
sdk.EventTypeMessage,
sdk.NewAttribute(sdk.AttributeKeyModule, types.AttributeValueCategory),
sdk.NewAttribute(sdk.AttributeKeySender, msg.ValidatorAddr.String()),
),
)
return &sdk.Result{Events: ctx.EventManager().Events()}, nil
}