forked from cosmos/cosmos-sdk
-
Notifications
You must be signed in to change notification settings - Fork 10
/
msgs.go
294 lines (246 loc) · 8.87 KB
/
msgs.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
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
package v1beta1
import (
"fmt"
"github.com/cosmos/gogoproto/proto"
"github.com/shapeshift/cosmos-sdk/math"
"sigs.k8s.io/yaml"
codectypes "github.com/shapeshift/cosmos-sdk/codec/types"
sdk "github.com/shapeshift/cosmos-sdk/types"
sdkerrors "github.com/shapeshift/cosmos-sdk/types/errors"
"github.com/shapeshift/cosmos-sdk/x/gov/codec"
"github.com/shapeshift/cosmos-sdk/x/gov/types"
)
// Governance message types and routes
const (
TypeMsgDeposit = "deposit"
TypeMsgVote = "vote"
TypeMsgVoteWeighted = "weighted_vote"
TypeMsgSubmitProposal = "submit_proposal"
)
var (
_, _, _, _ sdk.Msg = &MsgSubmitProposal{}, &MsgDeposit{}, &MsgVote{}, &MsgVoteWeighted{}
_ codectypes.UnpackInterfacesMessage = &MsgSubmitProposal{}
)
// NewMsgSubmitProposal creates a new MsgSubmitProposal.
//
//nolint:interfacer
func NewMsgSubmitProposal(content Content, initialDeposit sdk.Coins, proposer sdk.AccAddress) (*MsgSubmitProposal, error) {
m := &MsgSubmitProposal{
InitialDeposit: initialDeposit,
Proposer: proposer.String(),
}
err := m.SetContent(content)
if err != nil {
return nil, err
}
return m, nil
}
// GetInitialDeposit returns the initial deposit of MsgSubmitProposal.
func (m *MsgSubmitProposal) GetInitialDeposit() sdk.Coins { return m.InitialDeposit }
// GetProposer returns the proposer address of MsgSubmitProposal.
func (m *MsgSubmitProposal) GetProposer() sdk.AccAddress {
proposer, _ := sdk.AccAddressFromBech32(m.Proposer)
return proposer
}
// GetContent returns the content of MsgSubmitProposal.
func (m *MsgSubmitProposal) GetContent() Content {
content, ok := m.Content.GetCachedValue().(Content)
if !ok {
return nil
}
return content
}
// SetInitialDeposit sets the given initial deposit for MsgSubmitProposal.
func (m *MsgSubmitProposal) SetInitialDeposit(coins sdk.Coins) {
m.InitialDeposit = coins
}
// SetProposer sets the given proposer address for MsgSubmitProposal.
func (m *MsgSubmitProposal) SetProposer(address fmt.Stringer) {
m.Proposer = address.String()
}
// SetContent sets the content for MsgSubmitProposal.
func (m *MsgSubmitProposal) SetContent(content Content) error {
msg, ok := content.(proto.Message)
if !ok {
return fmt.Errorf("can't proto marshal %T", msg)
}
any, err := codectypes.NewAnyWithValue(msg)
if err != nil {
return err
}
m.Content = any
return nil
}
// Route implements the sdk.Msg interface.
func (m MsgSubmitProposal) Route() string { return types.RouterKey }
// Type implements the sdk.Msg interface.
func (m MsgSubmitProposal) Type() string { return TypeMsgSubmitProposal }
// ValidateBasic implements the sdk.Msg interface.
func (m MsgSubmitProposal) ValidateBasic() error {
if _, err := sdk.AccAddressFromBech32(m.Proposer); err != nil {
return sdkerrors.ErrInvalidAddress.Wrapf("invalid proposer address: %s", err)
}
if !m.InitialDeposit.IsValid() {
return sdkerrors.Wrap(sdkerrors.ErrInvalidCoins, m.InitialDeposit.String())
}
if m.InitialDeposit.IsAnyNegative() {
return sdkerrors.Wrap(sdkerrors.ErrInvalidCoins, m.InitialDeposit.String())
}
content := m.GetContent()
if content == nil {
return sdkerrors.Wrap(types.ErrInvalidProposalContent, "missing content")
}
if !IsValidProposalType(content.ProposalType()) {
return sdkerrors.Wrap(types.ErrInvalidProposalType, content.ProposalType())
}
if err := content.ValidateBasic(); err != nil {
return err
}
return nil
}
// GetSignBytes returns the message bytes to sign over.
func (m MsgSubmitProposal) GetSignBytes() []byte {
bz := codec.ModuleCdc.MustMarshalJSON(&m)
return sdk.MustSortJSON(bz)
}
// GetSigners returns the expected signers for a MsgSubmitProposal.
func (m MsgSubmitProposal) GetSigners() []sdk.AccAddress {
proposer, _ := sdk.AccAddressFromBech32(m.Proposer)
return []sdk.AccAddress{proposer}
}
// String implements the Stringer interface
func (m MsgSubmitProposal) String() string {
out, _ := yaml.Marshal(m)
return string(out)
}
// UnpackInterfaces implements UnpackInterfacesMessage.UnpackInterfaces
func (m MsgSubmitProposal) UnpackInterfaces(unpacker codectypes.AnyUnpacker) error {
var content Content
return unpacker.UnpackAny(m.Content, &content)
}
// NewMsgDeposit creates a new MsgDeposit instance
//
//nolint:interfacer
func NewMsgDeposit(depositor sdk.AccAddress, proposalID uint64, amount sdk.Coins) *MsgDeposit {
return &MsgDeposit{proposalID, depositor.String(), amount}
}
// Route implements the sdk.Msg interface.
func (msg MsgDeposit) Route() string { return types.RouterKey }
// Type implements the sdk.Msg interface.
func (msg MsgDeposit) Type() string { return TypeMsgDeposit }
// ValidateBasic implements the sdk.Msg interface.
func (msg MsgDeposit) ValidateBasic() error {
if _, err := sdk.AccAddressFromBech32(msg.Depositor); err != nil {
return sdkerrors.ErrInvalidAddress.Wrapf("invalid depositor address: %s", err)
}
if !msg.Amount.IsValid() {
return sdkerrors.Wrap(sdkerrors.ErrInvalidCoins, msg.Amount.String())
}
if msg.Amount.IsAnyNegative() {
return sdkerrors.Wrap(sdkerrors.ErrInvalidCoins, msg.Amount.String())
}
return nil
}
// String implements the Stringer interface
func (msg MsgDeposit) String() string {
out, _ := yaml.Marshal(msg)
return string(out)
}
// GetSignBytes returns the message bytes to sign over.
func (msg MsgDeposit) GetSignBytes() []byte {
bz := codec.ModuleCdc.MustMarshalJSON(&msg)
return sdk.MustSortJSON(bz)
}
// GetSigners returns the expected signers for a MsgDeposit.
func (msg MsgDeposit) GetSigners() []sdk.AccAddress {
depositor, _ := sdk.AccAddressFromBech32(msg.Depositor)
return []sdk.AccAddress{depositor}
}
// NewMsgVote creates a message to cast a vote on an active proposal
//
//nolint:interfacer
func NewMsgVote(voter sdk.AccAddress, proposalID uint64, option VoteOption) *MsgVote {
return &MsgVote{proposalID, voter.String(), option}
}
// Route implements the sdk.Msg interface.
func (msg MsgVote) Route() string { return types.RouterKey }
// Type implements the sdk.Msg interface.
func (msg MsgVote) Type() string { return TypeMsgVote }
// ValidateBasic implements the sdk.Msg interface.
func (msg MsgVote) ValidateBasic() error {
if _, err := sdk.AccAddressFromBech32(msg.Voter); err != nil {
return sdkerrors.ErrInvalidAddress.Wrapf("invalid voter address: %s", err)
}
if !ValidVoteOption(msg.Option) {
return sdkerrors.Wrap(types.ErrInvalidVote, msg.Option.String())
}
return nil
}
// String implements the Stringer interface
func (msg MsgVote) String() string {
out, _ := yaml.Marshal(msg)
return string(out)
}
// GetSignBytes returns the message bytes to sign over.
func (msg MsgVote) GetSignBytes() []byte {
bz := codec.ModuleCdc.MustMarshalJSON(&msg)
return sdk.MustSortJSON(bz)
}
// GetSigners returns the expected signers for a MsgVote.
func (msg MsgVote) GetSigners() []sdk.AccAddress {
voter, _ := sdk.AccAddressFromBech32(msg.Voter)
return []sdk.AccAddress{voter}
}
// NewMsgVoteWeighted creates a message to cast a vote on an active proposal
//
//nolint:interfacer
func NewMsgVoteWeighted(voter sdk.AccAddress, proposalID uint64, options WeightedVoteOptions) *MsgVoteWeighted {
return &MsgVoteWeighted{proposalID, voter.String(), options}
}
// Route implements the sdk.Msg interface.
func (msg MsgVoteWeighted) Route() string { return types.RouterKey }
// Type implements the sdk.Msg interface.
func (msg MsgVoteWeighted) Type() string { return TypeMsgVoteWeighted }
// ValidateBasic implements the sdk.Msg interface.
func (msg MsgVoteWeighted) ValidateBasic() error {
if _, err := sdk.AccAddressFromBech32(msg.Voter); err != nil {
return sdkerrors.ErrInvalidAddress.Wrapf("invalid voter address: %s", err)
}
if len(msg.Options) == 0 {
return sdkerrors.Wrap(sdkerrors.ErrInvalidRequest, WeightedVoteOptions(msg.Options).String())
}
totalWeight := math.LegacyNewDec(0)
usedOptions := make(map[VoteOption]bool)
for _, option := range msg.Options {
if !ValidWeightedVoteOption(option) {
return sdkerrors.Wrap(types.ErrInvalidVote, option.String())
}
totalWeight = totalWeight.Add(option.Weight)
if usedOptions[option.Option] {
return sdkerrors.Wrap(types.ErrInvalidVote, "Duplicated vote option")
}
usedOptions[option.Option] = true
}
if totalWeight.GT(math.LegacyNewDec(1)) {
return sdkerrors.Wrap(types.ErrInvalidVote, "Total weight overflow 1.00")
}
if totalWeight.LT(math.LegacyNewDec(1)) {
return sdkerrors.Wrap(types.ErrInvalidVote, "Total weight lower than 1.00")
}
return nil
}
// String implements the Stringer interface
func (msg MsgVoteWeighted) String() string {
out, _ := yaml.Marshal(msg)
return string(out)
}
// GetSignBytes returns the message bytes to sign over.
func (msg MsgVoteWeighted) GetSignBytes() []byte {
bz := codec.ModuleCdc.MustMarshalJSON(&msg)
return sdk.MustSortJSON(bz)
}
// GetSigners returns the expected signers for a MsgVoteWeighted.
func (msg MsgVoteWeighted) GetSigners() []sdk.AccAddress {
voter, _ := sdk.AccAddressFromBech32(msg.Voter)
return []sdk.AccAddress{voter}
}