-
Notifications
You must be signed in to change notification settings - Fork 14
/
handler.go
131 lines (107 loc) · 3.12 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
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
package slashing
import (
"encoding/json"
"fmt"
sdk "github.com/cosmos/cosmos-sdk/types"
)
// NewHandler creates a new handler for slashing module
func NewHandler(keeper Keeper) sdk.Handler {
return func(ctx sdk.Context, msg sdk.Msg) sdk.Result {
switch msg := msg.(type) {
case MsgSlashArgument:
return handleMsgSlashArgument(ctx, keeper, msg)
case MsgAddAdmin:
return handleMsgAddAdmin(ctx, keeper, msg)
case MsgRemoveAdmin:
return handleMsgRemoveAdmin(ctx, keeper, msg)
case MsgUpdateParams:
return handleMsgUpdateParams(ctx, keeper, msg)
default:
errMsg := fmt.Sprintf("Unrecognized slashing message type: %T", msg)
return sdk.ErrUnknownRequest(errMsg).Result()
}
}
}
func handleMsgSlashArgument(ctx sdk.Context, keeper Keeper, msg MsgSlashArgument) sdk.Result {
if err := msg.ValidateBasic(); err != nil {
return err.Result()
}
slash, punishmentResults, err := keeper.CreateSlash(ctx, msg.ArgumentID, msg.SlashType, msg.SlashReason, msg.SlashDetailedReason, msg.Creator)
if err != nil {
return err.Result()
}
res, jsonErr := ModuleCodec.MarshalJSON(slash)
if jsonErr != nil {
return sdk.ErrInternal(fmt.Sprintf("Marshal result error: %s", jsonErr)).Result()
}
ctx.EventManager().EmitEvent(
sdk.NewEvent(
sdk.EventTypeMessage,
sdk.NewAttribute(AttributeKeyMinSlashCountKey, fmt.Sprintf("%d", keeper.GetParams(ctx).MinSlashCount)),
),
)
if len(punishmentResults) > 0 {
_json, jsonErr := json.Marshal(punishmentResults)
if jsonErr != nil {
return sdk.ErrInternal(fmt.Sprintf("Marshal result error: %s", jsonErr)).Result()
}
ctx.EventManager().EmitEvent(
sdk.NewEvent(
sdk.EventTypeMessage,
sdk.NewAttribute(AttributeKeySlashResults, string(_json)),
),
)
}
return sdk.Result{
Data: res,
Events: ctx.EventManager().Events(),
}
}
func handleMsgAddAdmin(ctx sdk.Context, k Keeper, msg MsgAddAdmin) sdk.Result {
if err := msg.ValidateBasic(); err != nil {
return err.Result()
}
err := k.AddAdmin(ctx, msg.Admin, msg.Creator)
if err != nil {
return err.Result()
}
res, jsonErr := ModuleCodec.MarshalJSON(true)
if jsonErr != nil {
return sdk.ErrInternal(fmt.Sprintf("Marshal result error: %s", jsonErr)).Result()
}
return sdk.Result{
Data: res,
}
}
func handleMsgRemoveAdmin(ctx sdk.Context, k Keeper, msg MsgRemoveAdmin) sdk.Result {
if err := msg.ValidateBasic(); err != nil {
return err.Result()
}
err := k.RemoveAdmin(ctx, msg.Admin, msg.Remover)
if err != nil {
return err.Result()
}
res, jsonErr := ModuleCodec.MarshalJSON(true)
if jsonErr != nil {
return sdk.ErrInternal(fmt.Sprintf("Marshal result error: %s", jsonErr)).Result()
}
return sdk.Result{
Data: res,
}
}
func handleMsgUpdateParams(ctx sdk.Context, k Keeper, msg MsgUpdateParams) sdk.Result {
if err := msg.ValidateBasic(); err != nil {
return err.Result()
}
err := k.UpdateParams(ctx, msg.Updater, msg.Updates, msg.UpdatedFields)
if err != nil {
return err.Result()
}
res, jsonErr := ModuleCodec.MarshalJSON(true)
if jsonErr != nil {
return sdk.ErrInternal(fmt.Sprintf("Marshal result error: %s", jsonErr)).Result()
}
return sdk.Result{
Data: res,
}
}