/
msg_server.go
119 lines (98 loc) · 3.25 KB
/
msg_server.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
package keeper
import (
"context"
sdk "github.com/cosmos/cosmos-sdk/types"
ledgertypes "github.com/stafihub/stafihub/x/ledger/types"
relayerstypes "github.com/stafihub/stafihub/x/relayers/types"
rvalidatortypes "github.com/stafihub/stafihub/x/rvalidator/types"
"github.com/stafihub/stafihub/x/rvote/types"
sudotypes "github.com/stafihub/stafihub/x/sudo/types"
)
type msgServer struct {
Keeper
}
// NewMsgServerImpl returns an implementation of the MsgServer interface
// for the provided Keeper.
func NewMsgServerImpl(keeper Keeper) types.MsgServer {
return &msgServer{Keeper: keeper}
}
var _ types.MsgServer = msgServer{}
func (k msgServer) SetProposalLife(goCtx context.Context, msg *types.MsgSetProposalLife) (*types.MsgSetProposalLifeResponse, error) {
ctx := sdk.UnwrapSDKContext(goCtx)
if !k.sudoKeeper.IsAdmin(ctx, msg.Creator) {
return nil, sudotypes.ErrCreatorNotAdmin
}
k.Keeper.SetProposalLife(ctx, msg.ProposalLife)
return &types.MsgSetProposalLifeResponse{}, nil
}
func (k msgServer) SubmitProposal(goCtx context.Context, msg *types.MsgSubmitProposal) (*types.MsgSubmitProposalResponse, error) {
ctx := sdk.UnwrapSDKContext(goCtx)
content := msg.GetContent()
arena := content.ProposalRoute()
// will use ledger relayers in this case
if content.ProposalRoute() == rvalidatortypes.ModuleName && content.ProposalType() == rvalidatortypes.TypeUpdateRValidatorReportProposal {
arena = ledgertypes.ModuleName
}
relayerFlag := k.relayerKeeper.HasRelayer(ctx, arena, content.GetDenom(), msg.Proposer)
if !relayerFlag {
return nil, types.ErrInvalidProposer
}
prop, err := k.PreSubmitProposal(ctx, content, msg.Proposer)
if err != nil {
return nil, err
}
if prop.Status == types.StatusApproved {
return nil, types.ErrProposalAlreadyApproved
}
if prop.IsExpired(ctx.BlockHeight()) {
return nil, types.ErrProposalAlreadyExpired
}
threshold, ok := k.relayerKeeper.GetThreshold(ctx, arena, content.GetDenom())
if !ok {
return nil, relayerstypes.ErrThresholdNotFound
}
if uint32(len(prop.Voted)) >= threshold.Value {
prop.Status = types.StatusApproved
}
res := &types.MsgSubmitProposalResponse{PropId: prop.PropId(), Status: prop.Status}
if prop.Status != types.StatusApproved {
k.SetProposal(ctx, prop)
return res, nil
}
rtr := k.Keeper.Router()
handler := rtr.GetRoute(prop.ProposalRoute())
cacheCtx, writeCache := ctx.CacheContext()
if err := handler(cacheCtx, prop.GetContent()); err != nil {
return nil, err
}
k.SetProposal(ctx, prop)
// write state to the underlying multi-store
writeCache()
return res, nil
}
func (k msgServer) PreSubmitProposal(ctx sdk.Context, content types.Content, proposer string) (*types.Proposal, error) {
propId := content.GetPropId()
prop, ok := k.GetProposal(ctx, propId)
if !ok {
prop = &types.Proposal{
Status: types.StatusInitiated,
StartBlock: ctx.BlockHeight(),
Voted: []string{proposer},
}
proposalLife := k.ProposalLife(ctx)
if proposalLife == 0 {
prop.ExpireBlock = 0
} else {
prop.ExpireBlock = prop.StartBlock + proposalLife
}
if err := prop.SetContent(content); err != nil {
return nil, err
}
} else {
if prop.HasVoted(proposer) {
return nil, relayerstypes.ErrAlreadyVoted
}
prop.Voted = append(prop.Voted, proposer)
}
return prop, nil
}