forked from allinbits/modules
-
Notifications
You must be signed in to change notification settings - Fork 0
/
handler.go
88 lines (73 loc) · 2.74 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
package poa
import (
"fmt"
sdk "github.com/cosmos/cosmos-sdk/types"
sdkerrors "github.com/cosmos/cosmos-sdk/types/errors"
stakingtypes "github.com/cosmos/cosmos-sdk/x/staking/types"
"github.com/shivam2320/modules/x/poa/keeper"
"github.com/shivam2320/modules/x/poa/msg"
"github.com/shivam2320/modules/x/poa/types"
)
// NewHandler ...
func NewHandler(k keeper.Keeper) sdk.Handler {
return func(ctx sdk.Context, msg sdk.Msg) (*sdk.Result, error) {
ctx = ctx.WithEventManager(sdk.NewEventManager())
switch msg := msg.(type) {
case types.MsgCreateValidatorPOA:
return handleMsgCreateValidatorPOA(ctx, msg, k)
case types.MsgVoteValidator:
return handleMsgVoteValidator(ctx, msg, k)
default:
errMsg := fmt.Sprintf("unrecognized %s message type: %T", types.ModuleName, msg)
return nil, sdkerrors.Wrap(sdkerrors.ErrUnknownRequest, errMsg)
}
}
}
func handleMsgCreateValidatorPOA(ctx sdk.Context, msg msg.MsgCreateValidatorPOA, k keeper.Keeper) (*sdk.Result, error) {
if _, found := k.GetValidator(ctx, msg.Name); found {
return nil, sdkerrors.Wrap(types.ErrBadValidatorAddr, fmt.Sprintf("unrecognized %s validator already exists: %T", types.ModuleName, msg))
}
validator := types.NewValidator(
msg.Name,
msg.Address,
msg.PubKey,
msg.Description,
)
k.SetValidator(ctx, msg.Name, validator)
ctx.EventManager().EmitEvents(sdk.Events{
sdk.NewEvent(
stakingtypes.EventTypeCreateValidator,
sdk.NewAttribute(stakingtypes.AttributeKeyValidator, msg.Address.String()),
sdk.NewAttribute(sdk.AttributeKeySender, msg.Owner.String()),
),
})
return &sdk.Result{Events: ctx.EventManager().Events()}, nil
}
func handleMsgVoteValidator(ctx sdk.Context, msg msg.MsgVoteValidator, k keeper.Keeper) (*sdk.Result, error) {
_, found := k.GetValidator(ctx, msg.Name)
if !found {
return nil, sdkerrors.Wrap(types.ErrNoValidatorFound, fmt.Sprintf("unrecognized %s validator does not exist: %T", types.ModuleName, msg))
}
val, found := k.GetValidatorByAddress(ctx, msg.Voter)
if !found {
return nil, sdkerrors.Wrap(types.ErrEmptyValidatorAddr, fmt.Sprintf("unrecognized %s voting validator does not exist: %T", types.ModuleName, msg))
}
// if the validator hasn't been accepted they cannot vote
if !val.Accepted {
return nil, sdkerrors.Wrap(types.ErrNoAcceptedValidatorFound, fmt.Sprintf("error %s validator is not accepted by consensus: %T", types.ModuleName, msg))
}
vote := types.NewVote(
msg.Voter,
msg.Name,
msg.InFavor,
)
k.SetVote(ctx, vote)
ctx.EventManager().EmitEvents(sdk.Events{
sdk.NewEvent(
types.EventTypeVote,
sdk.NewAttribute(stakingtypes.AttributeKeyValidator, msg.Voter.String()),
sdk.NewAttribute(types.AttributeKeyCandidate, msg.Name),
),
})
return &sdk.Result{Events: ctx.EventManager().Events()}, nil
}