-
Notifications
You must be signed in to change notification settings - Fork 3.5k
/
proposal.go
102 lines (80 loc) · 2.65 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
package proposal
import (
"fmt"
"strings"
yaml "gopkg.in/yaml.v2"
govtypes "github.com/cosmos/cosmos-sdk/x/gov/types"
)
const (
// ProposalTypeChange defines the type for a ParameterChangeProposal
ProposalTypeChange = "ParameterChange"
)
// Assert ParameterChangeProposal implements govtypes.Content at compile-time
var _ govtypes.Content = &ParameterChangeProposal{}
func init() {
govtypes.RegisterProposalType(ProposalTypeChange)
govtypes.RegisterProposalTypeCodec(&ParameterChangeProposal{}, "cosmos-sdk/ParameterChangeProposal")
}
func NewParameterChangeProposal(title, description string, changes []ParamChange) *ParameterChangeProposal {
return &ParameterChangeProposal{title, description, changes}
}
// GetTitle returns the title of a parameter change proposal.
func (pcp *ParameterChangeProposal) GetTitle() string { return pcp.Title }
// GetDescription returns the description of a parameter change proposal.
func (pcp *ParameterChangeProposal) GetDescription() string { return pcp.Description }
// ProposalRoute returns the routing key of a parameter change proposal.
func (pcp *ParameterChangeProposal) ProposalRoute() string { return RouterKey }
// ProposalType returns the type of a parameter change proposal.
func (pcp *ParameterChangeProposal) ProposalType() string { return ProposalTypeChange }
// ValidateBasic validates the parameter change proposal
func (pcp *ParameterChangeProposal) ValidateBasic() error {
err := govtypes.ValidateAbstract(pcp)
if err != nil {
return err
}
return ValidateChanges(pcp.Changes)
}
// String implements the Stringer interface.
func (pcp ParameterChangeProposal) String() string {
var b strings.Builder
b.WriteString(fmt.Sprintf(`Parameter Change Proposal:
Title: %s
Description: %s
Changes:
`, pcp.Title, pcp.Description))
for _, pc := range pcp.Changes {
b.WriteString(fmt.Sprintf(` Param Change:
Subspace: %s
Key: %s
Value: %X
`, pc.Subspace, pc.Key, pc.Value))
}
return b.String()
}
func NewParamChange(subspace, key, value string) ParamChange {
return ParamChange{subspace, key, value}
}
// String implements the Stringer interface.
func (pc ParamChange) String() string {
out, _ := yaml.Marshal(pc)
return string(out)
}
// ValidateChanges performs basic validation checks over a set of ParamChange. It
// returns an error if any ParamChange is invalid.
func ValidateChanges(changes []ParamChange) error {
if len(changes) == 0 {
return ErrEmptyChanges
}
for _, pc := range changes {
if len(pc.Subspace) == 0 {
return ErrEmptySubspace
}
if len(pc.Key) == 0 {
return ErrEmptyKey
}
if len(pc.Value) == 0 {
return ErrEmptyValue
}
}
return nil
}