2019-04-30 09:31:38 -07:00
|
|
|
package types
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"strings"
|
|
|
|
|
2020-02-12 08:34:09 -08:00
|
|
|
"gopkg.in/yaml.v2"
|
|
|
|
|
2019-04-30 09:31:38 -07:00
|
|
|
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 }
|
|
|
|
|
2019-08-08 12:51:18 -07:00
|
|
|
// ProposalRoute returns the routing key of a parameter change proposal.
|
2019-04-30 09:31:38 -07:00
|
|
|
func (pcp ParameterChangeProposal) ProposalRoute() string { return RouterKey }
|
|
|
|
|
|
|
|
// ProposalType returns the type of a parameter change proposal.
|
|
|
|
func (pcp ParameterChangeProposal) ProposalType() string { return ProposalTypeChange }
|
|
|
|
|
2019-05-20 06:13:32 -07:00
|
|
|
// ValidateBasic validates the parameter change proposal
|
2019-12-27 09:57:54 -08:00
|
|
|
func (pcp ParameterChangeProposal) ValidateBasic() error {
|
|
|
|
err := govtypes.ValidateAbstract(pcp)
|
2019-04-30 09:31:38 -07:00
|
|
|
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
|
2019-12-10 08:48:57 -08:00
|
|
|
`, pc.Subspace, pc.Key, pc.Value))
|
2019-04-30 09:31:38 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
return b.String()
|
|
|
|
}
|
|
|
|
|
2019-05-27 17:47:12 -07:00
|
|
|
func NewParamChange(subspace, key, value string) ParamChange {
|
2019-12-10 08:48:57 -08:00
|
|
|
return ParamChange{subspace, key, value}
|
2019-04-30 09:31:38 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
// String implements the Stringer interface.
|
|
|
|
func (pc ParamChange) String() string {
|
2020-02-12 08:34:09 -08:00
|
|
|
out, _ := yaml.Marshal(pc)
|
|
|
|
return string(out)
|
2019-04-30 09:31:38 -07:00
|
|
|
}
|
|
|
|
|
2019-08-08 12:51:18 -07:00
|
|
|
// ValidateChanges performs basic validation checks over a set of ParamChange. It
|
2019-04-30 09:31:38 -07:00
|
|
|
// returns an error if any ParamChange is invalid.
|
2019-12-27 09:57:54 -08:00
|
|
|
func ValidateChanges(changes []ParamChange) error {
|
2019-04-30 09:31:38 -07:00
|
|
|
if len(changes) == 0 {
|
2019-12-27 09:57:54 -08:00
|
|
|
return ErrEmptyChanges
|
2019-04-30 09:31:38 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
for _, pc := range changes {
|
|
|
|
if len(pc.Subspace) == 0 {
|
2019-12-27 09:57:54 -08:00
|
|
|
return ErrEmptySubspace
|
2019-04-30 09:31:38 -07:00
|
|
|
}
|
|
|
|
if len(pc.Key) == 0 {
|
2019-12-27 09:57:54 -08:00
|
|
|
return ErrEmptyKey
|
2019-04-30 09:31:38 -07:00
|
|
|
}
|
|
|
|
if len(pc.Value) == 0 {
|
2019-12-27 09:57:54 -08:00
|
|
|
return ErrEmptyValue
|
2019-04-30 09:31:38 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|