/
proposal.go
145 lines (121 loc) · 5.44 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
package types
import (
"strings"
sdk "github.com/cosmos/cosmos-sdk/types"
sdkerrors "github.com/cosmos/cosmos-sdk/types/errors"
govtypes "github.com/cosmos/cosmos-sdk/x/gov/types"
)
type ProposalType string
const (
ProposalTypeSetCodeAuthorization ProposalType = "ProposalTypeSetCodeAuthorization"
ProposalTypeRemoveCodeAuthorization ProposalType = "ProposalTypeRemoveCodeAuthorization"
ProposalTypeSetContractAuthorization ProposalType = "ProposalTypeSetContractAuthorization"
ProposalTypeRemoveContractAuthorization ProposalType = "ProposalTypeRemoveContractAuthorization"
)
// EnableAllProposals contains all twasm gov types as keys.
var EnableAllProposals = []ProposalType{
ProposalTypeSetCodeAuthorization,
ProposalTypeRemoveCodeAuthorization,
ProposalTypeSetContractAuthorization,
ProposalTypeRemoveContractAuthorization,
}
func init() { // register new content types with the sdk
govtypes.RegisterProposalType(string(ProposalTypeSetCodeAuthorization))
govtypes.RegisterProposalType(string(ProposalTypeRemoveCodeAuthorization))
govtypes.RegisterProposalType(string(ProposalTypeSetContractAuthorization))
govtypes.RegisterProposalType(string(ProposalTypeRemoveContractAuthorization))
govtypes.RegisterProposalTypeCodec(&SetCodeAuthorizationProposal{}, "globalfee/SetCodeAuthorizationProposal")
govtypes.RegisterProposalTypeCodec(&RemoveCodeAuthorizationProposal{}, "globalfee/RemoveCodeAuthorizationProposal")
govtypes.RegisterProposalTypeCodec(&SetContractAuthorizationProposal{}, "globalfee/SetContractAuthorizationProposal")
govtypes.RegisterProposalTypeCodec(&RemoveContractAuthorizationProposal{}, "globalfee/RemoveContractAuthorizationProposal")
}
// ProposalRoute returns the routing key of a parameter change proposal.
func (p SetCodeAuthorizationProposal) ProposalRoute() string { return RouterKey }
// ProposalType returns the type
func (p SetCodeAuthorizationProposal) ProposalType() string {
return string(ProposalTypeSetCodeAuthorization)
}
// ValidateBasic validates the proposal
func (p SetCodeAuthorizationProposal) ValidateBasic() error {
if err := validateProposalCommons(p.Title, p.Description); err != nil {
return err
}
return p.CodeAuthorization.Validate()
}
// MarshalYAML pretty prints the wasm byte code
func (p SetCodeAuthorizationProposal) MarshalYAML() (interface{}, error) {
return p, nil
}
// ProposalRoute returns the routing key of a parameter change proposal.
func (p RemoveCodeAuthorizationProposal) ProposalRoute() string { return RouterKey }
// ProposalType returns the type
func (p RemoveCodeAuthorizationProposal) ProposalType() string {
return string(ProposalTypeRemoveCodeAuthorization)
}
// ValidateBasic validates the proposal
func (p RemoveCodeAuthorizationProposal) ValidateBasic() error {
return validateProposalCommons(p.Title, p.Description)
}
// MarshalYAML pretty prints the wasm byte code
func (p RemoveCodeAuthorizationProposal) MarshalYAML() (interface{}, error) {
return p, nil
}
// ProposalRoute returns the routing key of a parameter change proposal.
func (p SetContractAuthorizationProposal) ProposalRoute() string { return RouterKey }
// ProposalType returns the type
func (p SetContractAuthorizationProposal) ProposalType() string {
return string(ProposalTypeSetContractAuthorization)
}
// ValidateBasic validates the proposal
func (p SetContractAuthorizationProposal) ValidateBasic() error {
if err := validateProposalCommons(p.Title, p.Description); err != nil {
return err
}
return p.ContractAuthorization.Validate()
}
// MarshalYAML pretty prints the wasm byte code
func (p SetContractAuthorizationProposal) MarshalYAML() (interface{}, error) {
return p, nil
}
// ProposalRoute returns the routing key of a parameter change proposal.
func (p RemoveContractAuthorizationProposal) ProposalRoute() string { return RouterKey }
// ProposalType returns the type
func (p RemoveContractAuthorizationProposal) ProposalType() string {
return string(ProposalTypeRemoveContractAuthorization)
}
// ValidateBasic validates the proposal
func (p RemoveContractAuthorizationProposal) ValidateBasic() error {
if err := validateProposalCommons(p.Title, p.Description); err != nil {
return err
}
if _, err := sdk.AccAddressFromBech32(p.ContractAddress); err != nil {
return sdkerrors.Wrap(err, "contract")
}
return nil
}
// MarshalYAML pretty prints the wasm byte code
func (p RemoveContractAuthorizationProposal) MarshalYAML() (interface{}, error) {
return p, nil
}
// common validations
func validateProposalCommons(title, description string) error {
if strings.TrimSpace(title) != title {
return sdkerrors.Wrap(govtypes.ErrInvalidProposalContent, "proposal title must not start/end with white spaces")
}
if len(title) == 0 {
return sdkerrors.Wrap(govtypes.ErrInvalidProposalContent, "proposal title cannot be blank")
}
if len(title) > govtypes.MaxTitleLength {
return sdkerrors.Wrapf(govtypes.ErrInvalidProposalContent, "proposal title is longer than max length of %d", govtypes.MaxTitleLength)
}
if strings.TrimSpace(description) != description {
return sdkerrors.Wrap(govtypes.ErrInvalidProposalContent, "proposal description must not start/end with white spaces")
}
if len(description) == 0 {
return sdkerrors.Wrap(govtypes.ErrInvalidProposalContent, "proposal description cannot be blank")
}
if len(description) > govtypes.MaxDescriptionLength {
return sdkerrors.Wrapf(govtypes.ErrInvalidProposalContent, "proposal description is longer than max length of %d", govtypes.MaxDescriptionLength)
}
return nil
}