-
Notifications
You must be signed in to change notification settings - Fork 41
/
proposal.go
220 lines (179 loc) · 7.39 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
package types
import (
"cosmossdk.io/errors"
sdk "github.com/cosmos/cosmos-sdk/types"
govtypes "github.com/cosmos/cosmos-sdk/x/gov/types/v1beta1"
)
// constants
const (
ProposalContractRegistrationRequest string = "ProposalContractRegistrationRequest"
ProposalBatchContractRegistrationRequest string = "ProposalBatchContractRegistrationRequest"
ProposalBatchContractDeregistration string = "ProposalBatchContractDeregistration"
ProposalBatchStoreCode string = "ProposalBatchStoreCode"
)
func init() {
govtypes.RegisterProposalType(ProposalContractRegistrationRequest)
govtypes.RegisterProposalType(ProposalBatchContractRegistrationRequest)
govtypes.RegisterProposalType(ProposalBatchContractDeregistration)
govtypes.RegisterProposalType(ProposalBatchStoreCode)
}
// Implements Proposal Interface
var _ govtypes.Content = &ContractRegistrationRequestProposal{}
var _ govtypes.Content = &BatchContractRegistrationRequestProposal{}
var _ govtypes.Content = &BatchContractDeregistrationProposal{}
var _ govtypes.Content = &BatchStoreCodeProposal{}
// NewContractRegistrationRequestProposal returns new instance of ContractRegistrationRequestProposal
func NewContractRegistrationRequestProposal(title, description string, contractRegistrationRequest ContractRegistrationRequest) *ContractRegistrationRequestProposal {
return &ContractRegistrationRequestProposal{
Title: title,
Description: description,
ContractRegistrationRequest: contractRegistrationRequest,
}
}
// GetTitle returns the title of this proposal.
func (p *ContractRegistrationRequestProposal) GetTitle() string {
return p.Title
}
// GetDescription returns the description of this proposal.
func (p *ContractRegistrationRequestProposal) GetDescription() string {
return p.Description
}
// ProposalRoute returns router key of this proposal.
func (p *ContractRegistrationRequestProposal) ProposalRoute() string { return RouterKey }
// ProposalType returns proposal type of this proposal.
func (p *ContractRegistrationRequestProposal) ProposalType() string {
return ProposalContractRegistrationRequest
}
// ValidateBasic returns ValidateBasic result of this proposal.
func (p *ContractRegistrationRequestProposal) ValidateBasic() error {
if err := p.ContractRegistrationRequest.Validate(); err != nil {
return err
}
return govtypes.ValidateAbstract(p)
}
func (req *ContractRegistrationRequest) Validate() error {
// Check if contract address is valid
if _, err := sdk.AccAddressFromBech32(req.ContractAddress); err != nil {
return errors.Wrapf(ErrInvalidContractAddress, "ContractRegistrationRequestProposal: Error parsing registry contract address %s", err.Error())
}
if req.GranterAddress != "" {
if _, err := sdk.AccAddressFromBech32(req.GranterAddress); err != nil {
return errors.Wrapf(ErrInvalidContractAddress, "ContractRegistrationRequestProposal: Error parsing granter address %s", err.Error())
}
}
if req.FundingMode == FundingMode_Unspecified {
return errors.Wrapf(ErrInvalidFundingMode, "ContractRegistrationRequestProposal: FundingMode must be specified")
}
if (req.FundingMode == FundingMode_GrantOnly || req.FundingMode == FundingMode_Dual) && req.GranterAddress == "" {
return errors.Wrapf(ErrInvalidFundingMode, "GranterAddress must be specified")
}
if req.FundingMode == FundingMode_SelfFunded && req.GranterAddress != "" {
return errors.Wrapf(ErrInvalidFundingMode, "GranterAddress must be empty for self-funded contracts")
}
return nil
}
// GetTitle returns the title of this proposal.
func (p *BatchContractRegistrationRequestProposal) GetTitle() string {
return p.Title
}
// GetDescription returns the description of this proposal.
func (p *BatchContractRegistrationRequestProposal) GetDescription() string {
return p.Description
}
// ProposalRoute returns router key of this proposal.
func (p *BatchContractRegistrationRequestProposal) ProposalRoute() string { return RouterKey }
// ProposalType returns proposal type of this proposal.
func (p *BatchContractRegistrationRequestProposal) ProposalType() string {
return ProposalBatchContractRegistrationRequest
}
// ValidateBasic returns ValidateBasic result of this proposal.
func (p *BatchContractRegistrationRequestProposal) ValidateBasic() error {
for _, req := range p.ContractRegistrationRequests {
if err := req.Validate(); err != nil {
return err
}
}
if hasDuplicatesContractRegistrationRequest(p.ContractRegistrationRequests) {
return errors.Wrapf(ErrDuplicateContract, "BatchContractRegistrationRequestProposal: Duplicate contract registration requests")
}
return govtypes.ValidateAbstract(p)
}
// GetTitle returns the title of this proposal.
func (p *BatchContractDeregistrationProposal) GetTitle() string {
return p.Title
}
// GetDescription returns the description of this proposal.
func (p *BatchContractDeregistrationProposal) GetDescription() string {
return p.Description
}
// ProposalRoute returns router key of this proposal.
func (p *BatchContractDeregistrationProposal) ProposalRoute() string { return RouterKey }
// ProposalType returns proposal type of this proposal.
func (p *BatchContractDeregistrationProposal) ProposalType() string {
return ProposalBatchContractDeregistration
}
// ValidateBasic returns ValidateBasic result of this proposal.
func (p *BatchContractDeregistrationProposal) ValidateBasic() error {
if len(p.Contracts) == 0 {
return errors.Wrapf(ErrNoContractAddresses, "BatchContractDeregistrationProposal: Contract list was empty")
}
found := make(map[string]struct{})
for _, contract := range p.Contracts {
// Check if contract address is valid
addr, err := sdk.AccAddressFromBech32(contract)
if err != nil {
return errors.Wrapf(ErrInvalidContractAddress, "BatchContractDeregistrationProposal: Error parsing contract address %s", err.Error())
}
// Check that there are no duplicate contract addresses
if _, ok := found[addr.String()]; ok {
return errors.Wrapf(ErrDuplicateContract, "BatchContractDeregistrationProposal: Duplicate contract in contracts to deregister")
} else {
found[addr.String()] = struct{}{}
}
}
return govtypes.ValidateAbstract(p)
}
// GetTitle returns the title of this proposal.
func (p *BatchStoreCodeProposal) GetTitle() string {
return p.Title
}
// GetDescription returns the description of this proposal.
func (p *BatchStoreCodeProposal) GetDescription() string {
return p.Description
}
// ProposalRoute returns router key of this proposal.
func (p *BatchStoreCodeProposal) ProposalRoute() string { return RouterKey }
// ProposalType returns proposal type of this proposal.
func (p *BatchStoreCodeProposal) ProposalType() string {
return ProposalBatchStoreCode
}
// ValidateBasic returns ValidateBasic result of this proposal.
func (p *BatchStoreCodeProposal) ValidateBasic() error {
for idx := range p.Proposals {
if err := p.Proposals[idx].ValidateBasic(); err != nil {
return err
}
}
return govtypes.ValidateAbstract(p)
}
func HasDuplicates(slice []string) bool {
seen := make(map[string]struct{})
for _, item := range slice {
if _, ok := seen[item]; ok {
return true
}
seen[item] = struct{}{}
}
return false
}
func hasDuplicatesContractRegistrationRequest(slice []ContractRegistrationRequest) bool {
seen := make(map[string]struct{})
for _, item := range slice {
addr := sdk.MustAccAddressFromBech32(item.ContractAddress).String()
if _, ok := seen[addr]; ok {
return true
}
seen[addr] = struct{}{}
}
return false
}