-
Notifications
You must be signed in to change notification settings - Fork 179
/
proposal.go
216 lines (180 loc) · 6.72 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
package types
import (
"fmt"
sdk "github.com/okex/exchain/libs/cosmos-sdk/types"
"github.com/okex/exchain/libs/tendermint/global"
tmtypes "github.com/okex/exchain/libs/tendermint/types"
govtypes "github.com/okex/exchain/x/gov/types"
"strings"
)
const (
// proposalTypeManageTreasures defines the type for a ManageContractTreasures
proposalTypeManageTreasures = "ManageTreasures"
// proposalTypeModifyNextBlockUpdate defines the type for a ModifyNextBlockUpdate
proposalTypeModifyNextBlockUpdate = "ModifyNextBlockUpdate"
// RouterKey uses module name for routing
RouterKey = ModuleName
)
func init() {
govtypes.RegisterProposalType(proposalTypeManageTreasures)
govtypes.RegisterProposalType(proposalTypeModifyNextBlockUpdate)
govtypes.RegisterProposalTypeCodec(ManageTreasuresProposal{}, "okexchain/mint/ManageTreasuresProposal")
govtypes.RegisterProposalTypeCodec(ModifyNextBlockUpdateProposal{}, "okexchain/mint/ModifyNextBlockUpdateProposal")
}
var (
_ govtypes.Content = (*ManageTreasuresProposal)(nil)
_ govtypes.Content = (*ModifyNextBlockUpdateProposal)(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())
}
// ModifyNextBlockUpdateProposal - structure for the proposal modify next block update
type ModifyNextBlockUpdateProposal struct {
Title string `json:"title" yaml:"title"`
Description string `json:"description" yaml:"description"`
BlockNum uint64 `json:"block_num" yaml:"block_num"`
}
// NewModifyNextBlockUpdateProposal creates a new instance of ModifyNextBlockUpdateProposal
func NewModifyNextBlockUpdateProposal(title, description string, blockNum uint64) ModifyNextBlockUpdateProposal {
return ModifyNextBlockUpdateProposal{
Title: title,
Description: description,
BlockNum: blockNum,
}
}
// GetTitle returns title of the proposal object
func (mp ModifyNextBlockUpdateProposal) GetTitle() string {
return mp.Title
}
// GetDescription returns description of proposal object
func (mp ModifyNextBlockUpdateProposal) GetDescription() string {
return mp.Description
}
// ProposalRoute returns route key of the proposal object
func (mp ModifyNextBlockUpdateProposal) ProposalRoute() string {
return RouterKey
}
// ProposalType returns type of the proposal object
func (mp ModifyNextBlockUpdateProposal) ProposalType() string {
return proposalTypeModifyNextBlockUpdate
}
// ValidateBasic validates the proposal
func (mp ModifyNextBlockUpdateProposal) ValidateBasic() sdk.Error {
if global.GetGlobalHeight() > 0 && !tmtypes.HigherThanVenus5(global.GetGlobalHeight()) {
return ErrNotReachedVenus5Height
}
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 bigger than max 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 bigger than max description length")
}
if mp.ProposalType() != proposalTypeModifyNextBlockUpdate {
return govtypes.ErrInvalidProposalType(mp.ProposalType())
}
if global.GetGlobalHeight() > 0 && mp.BlockNum <= uint64(global.GetGlobalHeight()) {
return ErrCodeInvalidHeight
}
return nil
}
// String returns a human readable string representation of a ModifyNextBlockUpdateProposal
func (mp ModifyNextBlockUpdateProposal) String() string {
var builder strings.Builder
builder.WriteString(
fmt.Sprintf(`ModifyNextBlockUpdateProposal:
Title: %s
Description: %s
Type: %s
BlockNum: %d
`,
mp.Title, mp.Description, mp.ProposalType(), mp.BlockNum),
)
return strings.TrimSpace(builder.String())
}