-
Notifications
You must be signed in to change notification settings - Fork 14
/
msg_server.go
239 lines (201 loc) · 7.01 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
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
package keeper
import (
"context"
"encoding/base64"
"encoding/json"
"fmt"
"strconv"
errorsmod "cosmossdk.io/errors"
"github.com/armon/go-metrics"
"github.com/cosmos/cosmos-sdk/telemetry"
sdk "github.com/cosmos/cosmos-sdk/types"
errortypes "github.com/cosmos/cosmos-sdk/types/errors"
govtypes "github.com/cosmos/cosmos-sdk/x/gov/types"
"github.com/cosmos/cosmos-sdk/x/gov/types/v1"
"github.com/cosmos/cosmos-sdk/x/gov/types/v1beta1"
"github.com/functionx/fx-core/v5/x/gov/types"
)
type msgServer struct {
v1.MsgServer
Keeper
}
// NewMsgServerImpl returns an implementation of the gov MsgServer interface
// for the provided Keeper.
func NewMsgServerImpl(m v1.MsgServer, k Keeper) *msgServer {
return &msgServer{MsgServer: m, Keeper: k}
}
var (
_ v1.MsgServer = msgServer{}
_ types.MsgServer = msgServer{}
)
func (k msgServer) SubmitProposal(goCtx context.Context, msg *v1.MsgSubmitProposal) (*v1.MsgSubmitProposalResponse, error) {
ctx := sdk.UnwrapSDKContext(goCtx)
proposalMsgs, err := msg.GetMsgs()
if err != nil {
return nil, err
}
proposal, err := k.Keeper.SubmitProposal(ctx, proposalMsgs, msg.Metadata)
if err != nil {
return nil, err
}
bytes, err := proposal.Marshal()
if err != nil {
return nil, err
}
ctx.GasMeter().ConsumeGas(3*ctx.KVGasConfig().WriteCostPerByte*uint64(len(bytes)),
"submit proposal")
defer telemetry.IncrCounter(1, govtypes.ModuleName, "proposal")
initialDeposit := k.Keeper.GetMinInitialDeposit(ctx, types.ExtractMsgTypeURL(proposal.Messages))
if sdk.NewCoins(msg.GetInitialDeposit()...).IsAllLT(sdk.NewCoins(initialDeposit)) {
return nil, errorsmod.Wrapf(types.ErrInitialAmountTooLow, "%s is smaller than %s", msg.GetInitialDeposit(), initialDeposit)
}
proposer, err := sdk.AccAddressFromBech32(msg.GetProposer())
if err != nil {
return nil, err
}
votingStarted, err := k.Keeper.AddDeposit(ctx, proposal.Id, proposer, msg.GetInitialDeposit())
if err != nil {
return nil, err
}
ctx.EventManager().EmitEvent(sdk.NewEvent(
sdk.EventTypeMessage,
sdk.NewAttribute(sdk.AttributeKeyModule, govtypes.AttributeValueCategory),
sdk.NewAttribute(sdk.AttributeKeySender, msg.GetProposer()),
))
if votingStarted {
submitEvent := sdk.NewEvent(govtypes.EventTypeSubmitProposal,
sdk.NewAttribute(govtypes.AttributeKeyVotingPeriodStart, fmt.Sprintf("%d", proposal.Id)),
)
ctx.EventManager().EmitEvent(submitEvent)
}
return &v1.MsgSubmitProposalResponse{
ProposalId: proposal.Id,
}, nil
}
func (k msgServer) Deposit(goCtx context.Context, msg *v1.MsgDeposit) (*v1.MsgDepositResponse, error) {
ctx := sdk.UnwrapSDKContext(goCtx)
accAddr, err := sdk.AccAddressFromBech32(msg.Depositor)
if err != nil {
return nil, err
}
votingStarted, err := k.Keeper.AddDeposit(ctx, msg.ProposalId, accAddr, msg.Amount)
if err != nil {
return nil, err
}
defer telemetry.IncrCounterWithLabels(
[]string{govtypes.ModuleName, "deposit"},
1,
[]metrics.Label{
telemetry.NewLabel("proposal_id", strconv.Itoa(int(msg.ProposalId))),
},
)
ctx.EventManager().EmitEvent(sdk.NewEvent(
sdk.EventTypeMessage,
sdk.NewAttribute(sdk.AttributeKeyModule, govtypes.AttributeValueCategory),
sdk.NewAttribute(sdk.AttributeKeySender, msg.Depositor),
))
if votingStarted {
ctx.EventManager().EmitEvent(sdk.NewEvent(
govtypes.EventTypeProposalDeposit,
sdk.NewAttribute(govtypes.AttributeKeyVotingPeriodStart, fmt.Sprintf("%d", msg.ProposalId)),
))
}
return &v1.MsgDepositResponse{}, nil
}
func (k msgServer) UpdateParams(c context.Context, req *types.MsgUpdateParams) (*types.MsgUpdateParamsResponse, error) {
if k.authority != req.Authority {
return nil, errorsmod.Wrapf(govtypes.ErrInvalidSigner, "invalid authority; expected %s, got %s", k.authority, req.Authority)
}
ctx := sdk.UnwrapSDKContext(c)
if err := k.SetParams(ctx, &req.Params); err != nil {
return nil, err
}
return &types.MsgUpdateParamsResponse{}, nil
}
func (k msgServer) UpdateEGFParams(c context.Context, req *types.MsgUpdateEGFParams) (*types.MsgUpdateEGFParamsResponse, error) {
if k.authority != req.Authority {
return nil, errorsmod.Wrapf(govtypes.ErrInvalidSigner, "invalid authority; expected %s, got %s", k.authority, req.Authority)
}
ctx := sdk.UnwrapSDKContext(c)
if err := k.SetEGFParams(ctx, &req.Params); err != nil {
return nil, err
}
return &types.MsgUpdateEGFParamsResponse{}, nil
}
type LegacyMsgServer struct {
govAcct string
server v1.MsgServer
}
// NewLegacyMsgServerImpl returns an implementation of the v1beta1 legacy MsgServer interface. It wraps around
// the current MsgServer
func NewLegacyMsgServerImpl(govAcct string, v1Server v1.MsgServer) v1beta1.MsgServer {
return &LegacyMsgServer{govAcct: govAcct, server: v1Server}
}
var _ v1beta1.MsgServer = LegacyMsgServer{}
func (k LegacyMsgServer) SubmitProposal(goCtx context.Context, msg *v1beta1.MsgSubmitProposal) (*v1beta1.MsgSubmitProposalResponse, error) {
contentMsg, err := v1.NewLegacyContent(msg.GetContent(), k.govAcct)
if err != nil {
return nil, fmt.Errorf("error converting legacy content into proposal message: %w", err)
}
// TODO proposal metadata contain title, summary and metadata, for compatibility with cosmos v0.46.x,
// when upgrade to v0.47.x, will migrate to new proposal struct
fxMetadata := types.NewFXMetadata(msg.GetContent().GetTitle(), msg.GetContent().GetDescription(), "")
mdBytes, err := json.Marshal(fxMetadata)
if err != nil {
return nil, errorsmod.Wrapf(errortypes.ErrJSONMarshal, "fx metadata: %s", err.Error())
}
proposal, err := v1.NewMsgSubmitProposal(
[]sdk.Msg{contentMsg},
msg.InitialDeposit,
msg.Proposer,
base64.StdEncoding.EncodeToString(mdBytes),
)
if err != nil {
return nil, err
}
resp, err := k.server.SubmitProposal(goCtx, proposal)
if err != nil {
return nil, err
}
return &v1beta1.MsgSubmitProposalResponse{ProposalId: resp.ProposalId}, nil
}
func (k LegacyMsgServer) Vote(goCtx context.Context, msg *v1beta1.MsgVote) (*v1beta1.MsgVoteResponse, error) {
_, err := k.server.Vote(goCtx, &v1.MsgVote{
ProposalId: msg.ProposalId,
Voter: msg.Voter,
Option: v1.VoteOption(msg.Option),
})
if err != nil {
return nil, err
}
return &v1beta1.MsgVoteResponse{}, nil
}
func (k LegacyMsgServer) VoteWeighted(goCtx context.Context, msg *v1beta1.MsgVoteWeighted) (*v1beta1.MsgVoteWeightedResponse, error) {
opts := make([]*v1.WeightedVoteOption, len(msg.Options))
for idx, opt := range msg.Options {
opts[idx] = &v1.WeightedVoteOption{
Option: v1.VoteOption(opt.Option),
Weight: opt.Weight.String(),
}
}
_, err := k.server.VoteWeighted(goCtx, &v1.MsgVoteWeighted{
ProposalId: msg.ProposalId,
Voter: msg.Voter,
Options: opts,
})
if err != nil {
return nil, err
}
return &v1beta1.MsgVoteWeightedResponse{}, nil
}
func (k LegacyMsgServer) Deposit(goCtx context.Context, msg *v1beta1.MsgDeposit) (*v1beta1.MsgDepositResponse, error) {
_, err := k.server.Deposit(goCtx, &v1.MsgDeposit{
ProposalId: msg.ProposalId,
Depositor: msg.Depositor,
Amount: msg.Amount,
})
if err != nil {
return nil, err
}
return &v1beta1.MsgDepositResponse{}, nil
}