-
Notifications
You must be signed in to change notification settings - Fork 0
/
proposal.go
245 lines (204 loc) · 7.42 KB
/
proposal.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
package types
import (
"encoding/json"
"fmt"
sdk "github.com/furyaxyz/fuxchain/libs/cosmos-sdk/types"
"github.com/furyaxyz/fuxchain/libs/system"
"github.com/furyaxyz/fuxchain/libs/tendermint/global"
govtypes "github.com/furyaxyz/fuxchain/x/gov/types"
"strings"
)
const (
// proposalTypeManageTreasures defines the type for a ManageContractTreasures
proposalTypeManageTreasures = "ManageTreasures"
// ProposalTypeExtra defines the type for a MintExtraProposal
ProposalTypeExtra = "MintExtra"
ActionNextBlockUpdate = "NextBlockUpdate"
ActionMintedPerBlock = "MintedPerBlock"
// RouterKey uses module name for routing
RouterKey = ModuleName
)
func init() {
govtypes.RegisterProposalType(proposalTypeManageTreasures)
govtypes.RegisterProposalType(ProposalTypeExtra)
govtypes.RegisterProposalTypeCodec(ManageTreasuresProposal{}, system.Chain+"/mint/ManageTreasuresProposal")
govtypes.RegisterProposalTypeCodec(ExtraProposal{}, system.Chain+"/mint/ExtraProposal")
}
var (
_ govtypes.Content = (*ManageTreasuresProposal)(nil)
_ govtypes.Content = (*ExtraProposal)(nil)
)
// ManageTreasuresProposal - structure for the proposal to add or delete treasures
type ManageTreasuresProposal struct {
Title string `json:"title" yaml:"title"`
Description string `json:"description" yaml:"description"`
Treasures []Treasure `json:"treasures" yaml:"treasures"`
IsAdded bool `json:"is_added" yaml:"is_added"`
}
// NewManageTreasuresProposal creates a new instance of ManageTreasuresProposal
func NewManageTreasuresProposal(title, description string, treasures []Treasure, isAdded bool,
) ManageTreasuresProposal {
return ManageTreasuresProposal{
Title: title,
Description: description,
Treasures: treasures,
IsAdded: isAdded,
}
}
// GetTitle returns title of a manage treasures proposal object
func (mp ManageTreasuresProposal) GetTitle() string {
return mp.Title
}
// GetDescription returns description of a manage treasures proposal object
func (mp ManageTreasuresProposal) GetDescription() string {
return mp.Description
}
// ProposalRoute returns route key of a manage treasures proposal object
func (mp ManageTreasuresProposal) ProposalRoute() string {
return RouterKey
}
// ProposalType returns type of a manage treasures proposal object
func (mp ManageTreasuresProposal) ProposalType() string {
return proposalTypeManageTreasures
}
// ValidateBasic validates a manage treasures proposal
func (mp ManageTreasuresProposal) ValidateBasic() sdk.Error {
if len(strings.TrimSpace(mp.Title)) == 0 {
return govtypes.ErrInvalidProposalContent("title is required")
}
if len(mp.Title) > govtypes.MaxTitleLength {
return govtypes.ErrInvalidProposalContent("title length is longer than the maximum title length")
}
if len(mp.Description) == 0 {
return govtypes.ErrInvalidProposalContent("description is required")
}
if len(mp.Description) > govtypes.MaxDescriptionLength {
return govtypes.ErrInvalidProposalContent("description length is longer than the maximum description length")
}
if mp.ProposalType() != proposalTypeManageTreasures {
return govtypes.ErrInvalidProposalType(mp.ProposalType())
}
if treasuresLen := len(mp.Treasures); treasuresLen == 0 {
return ErrEmptyTreasures
}
if isTreasureDuplicated(mp.Treasures) {
return ErrDuplicatedTreasure
}
if err := ValidateTreasures(mp.Treasures); err != nil {
return ErrTreasuresInternal(err)
}
return nil
}
// String returns a human readable string representation of a ManageTreasuresProposal
func (mp ManageTreasuresProposal) String() string {
var builder strings.Builder
builder.WriteString(
fmt.Sprintf(`ManageTreasuresProposal:
Title: %s
Description: %s
Type: %s
IsAdded: %t
Treasures:
`,
mp.Title, mp.Description, mp.ProposalType(), mp.IsAdded),
)
for i := 0; i < len(mp.Treasures); i++ {
builder.WriteString("\t\t\t\t\t\t")
builder.WriteString("Address:")
builder.WriteString(mp.Treasures[i].Address.String())
builder.WriteString("\t\tProportion:")
builder.WriteString(mp.Treasures[i].Proportion.String())
builder.Write([]byte{'\n'})
}
return strings.TrimSpace(builder.String())
}
type ExtraProposal struct {
Title string `json:"title" yaml:"title"`
Description string `json:"description" yaml:"description"`
Action string `json:"action" yaml:"action"`
Extra string `json:"extra" yaml:"extra"`
}
// NewExtraProposal creates a new extra proposal.
func NewExtraProposal(title, description, action, extra string) ExtraProposal {
return ExtraProposal{title, description, action, extra}
}
// GetTitle returns the title of a community pool spend proposal.
func (p ExtraProposal) GetTitle() string { return p.Title }
// GetDescription returns the description of a community pool spend proposal.
func (p ExtraProposal) GetDescription() string { return p.Description }
// GetDescription returns the routing key of a community pool spend proposal.
func (p ExtraProposal) ProposalRoute() string { return RouterKey }
// ProposalType returns the type of a community pool spend proposal.
func (p ExtraProposal) ProposalType() string { return ProposalTypeExtra }
// ValidateBasic runs basic stateless validity checks
func (p ExtraProposal) ValidateBasic() error {
err := govtypes.ValidateAbstract(ModuleName, p)
if err != nil {
return err
}
if len(strings.TrimSpace(p.Action)) == 0 {
return govtypes.ErrInvalidProposalContent("extra proposal's action is required")
}
if len(p.Action) > govtypes.MaxExtraActionLength {
return govtypes.ErrInvalidProposalContent("extra proposal's action length is bigger than max length")
}
if len(strings.TrimSpace(p.Extra)) == 0 {
return govtypes.ErrInvalidProposalContent("extra proposal's extra is required")
}
if len(p.Extra) > govtypes.MaxExtraBodyLength {
return govtypes.ErrInvalidProposalContent("extra proposal's extra body length is bigger than max length")
}
switch p.Action {
case ActionNextBlockUpdate:
_, err = NewNextBlockUpdate(p.Extra)
return err
case ActionMintedPerBlock:
_, err = NewMintedPerBlockParams(p.Extra)
return err
default:
return ErrUnknownExtraProposalAction
}
return nil
}
// String implements the Stringer interface.
func (p ExtraProposal) String() string {
var b strings.Builder
b.WriteString(fmt.Sprintf(`Community Pool Spend Proposal:
Title: %s
Description: %s
Action: %s
Extra: %s
`, p.Title, p.Description, p.Action, p.Extra))
return b.String()
}
type NextBlockUpdateParams struct {
BlockNum uint64 `json:"block_num" yaml:"block_num"`
}
func NewNextBlockUpdate(data string) (NextBlockUpdateParams, error) {
var param NextBlockUpdateParams
err := json.Unmarshal([]byte(data), ¶m)
if err != nil {
return param, ErrExtraProposalParams("parse json error")
}
if global.GetGlobalHeight() > 0 && param.BlockNum <= uint64(global.GetGlobalHeight()) {
return param, ErrCodeInvalidHeight
}
return param, nil
}
type MintedPerBlockParams struct {
Coin sdk.SysCoin `json:"coin" yaml:"coin"` // minted per block on this proposal.
}
func NewMintedPerBlockParams(jsonData string) (MintedPerBlockParams, error) {
var param MintedPerBlockParams
err := json.Unmarshal([]byte(jsonData), ¶m)
if err != nil {
return param, ErrExtraProposalParams("parse json error")
}
if param.Coin.Amount.IsNil() || param.Coin.Denom != sdk.DefaultBondDenom {
return param, ErrExtraProposalParams("coin is nil")
}
if param.Coin.IsNegative() {
return param, ErrExtraProposalParams("coin is negative")
}
return param, nil
}