/
proposal.go
99 lines (84 loc) · 3.3 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
package types
import (
"strings"
errorsmod "cosmossdk.io/errors"
sdk "github.com/cosmos/cosmos-sdk/types"
govtypes "github.com/cosmos/cosmos-sdk/x/gov/types"
"github.com/cosmos/cosmos-sdk/x/gov/types/v1beta1"
)
type ProposalType string
const (
ProposalTypePromoteContract ProposalType = "PromoteToPrivilegedContract"
ProposalTypeDemoteContract ProposalType = "DemotePrivilegedContract"
)
// EnableAllProposals contains all twasm gov types as keys.
var EnableAllProposals = []ProposalType{
ProposalTypePromoteContract,
ProposalTypeDemoteContract,
}
func init() { // register new content types with the sdk
v1beta1.RegisterProposalType(string(ProposalTypePromoteContract))
v1beta1.RegisterProposalType(string(ProposalTypeDemoteContract))
}
// ProposalRoute returns the routing key of a parameter change proposal.
func (p PromoteToPrivilegedContractProposal) ProposalRoute() string { return RouterKey }
// ProposalType returns the type
func (p PromoteToPrivilegedContractProposal) ProposalType() string {
return string(ProposalTypePromoteContract)
}
// ValidateBasic validates the proposal
func (p PromoteToPrivilegedContractProposal) ValidateBasic() error {
if err := validateProposalCommons(p.Title, p.Description); err != nil {
return err
}
if _, err := sdk.AccAddressFromBech32(p.Contract); err != nil {
return errorsmod.Wrap(err, "contract")
}
return nil
}
// MarshalYAML pretty prints the wasm byte code
func (p PromoteToPrivilegedContractProposal) MarshalYAML() (interface{}, error) {
return p, nil
}
// ProposalRoute returns the routing key of a parameter change proposal.
func (p DemotePrivilegedContractProposal) ProposalRoute() string { return RouterKey }
// ProposalType returns the type
func (p DemotePrivilegedContractProposal) ProposalType() string {
return string(ProposalTypeDemoteContract)
}
// ValidateBasic validates the proposal
func (p DemotePrivilegedContractProposal) ValidateBasic() error {
if err := validateProposalCommons(p.Title, p.Description); err != nil {
return err
}
if _, err := sdk.AccAddressFromBech32(p.Contract); err != nil {
return errorsmod.Wrap(err, "contract")
}
return nil
}
// MarshalYAML pretty prints the wasm byte code
func (p DemotePrivilegedContractProposal) MarshalYAML() (interface{}, error) {
return p, nil
}
// common validations
func validateProposalCommons(title, description string) error {
if strings.TrimSpace(title) != title {
return errorsmod.Wrap(govtypes.ErrInvalidProposalContent, "proposal title must not start/end with white spaces")
}
if len(title) == 0 {
return errorsmod.Wrap(govtypes.ErrInvalidProposalContent, "proposal title cannot be blank")
}
if len(title) > v1beta1.MaxTitleLength {
return errorsmod.Wrapf(govtypes.ErrInvalidProposalContent, "proposal title is longer than max length of %d", v1beta1.MaxTitleLength)
}
if strings.TrimSpace(description) != description {
return errorsmod.Wrap(govtypes.ErrInvalidProposalContent, "proposal description must not start/end with white spaces")
}
if len(description) == 0 {
return errorsmod.Wrap(govtypes.ErrInvalidProposalContent, "proposal description cannot be blank")
}
if len(description) > v1beta1.MaxDescriptionLength {
return errorsmod.Wrapf(govtypes.ErrInvalidProposalContent, "proposal description is longer than max length of %d", v1beta1.MaxDescriptionLength)
}
return nil
}