cosmos-sdk/x/params/migrations/v036/types.go

176 lines
5.1 KiB
Go
Raw Normal View History

// Package v036 is used for legacy migration scripts. Actual migration scripts
// for v036 have been removed, but the v039->v042 migration script still
// references types from this file, so we're keeping it for now.
package v036
import (
"fmt"
"strings"
"github.com/cosmos/cosmos-sdk/codec"
refactor: Rename x/{mod}/legacy to x/{mod}/migrations (#9628) <!-- The default pull request template is for types feat, fix, or refactor. For other templates, add one of the following parameters to the url: - template=docs.md - template=other.md --> ## Description Closes: #8700 <!-- Add a description of the changes that this PR introduces and the files that are the most critical to review. --> --- ### Author Checklist *All items are required. Please add a note to the item if the item is not applicable and please add links to any relevant follow up issues.* I have... - [x] included the correct [type prefix](https://github.com/commitizen/conventional-commit-types/blob/v3.0.0/index.json) in the PR title - [x] added `!` to the type prefix if API or client breaking change - [x] targeted the correct branch (see [PR Targeting](https://github.com/cosmos/cosmos-sdk/blob/master/CONTRIBUTING.md#pr-targeting)) - [x] provided a link to the relevant issue or specification - [x] followed the guidelines for [building modules](https://github.com/cosmos/cosmos-sdk/blob/master/docs/building-modules) - [x] included the necessary unit and integration [tests](https://github.com/cosmos/cosmos-sdk/blob/master/CONTRIBUTING.md#testing) - [x] added a changelog entry to `CHANGELOG.md` - [x] included comments for [documenting Go code](https://blog.golang.org/godoc) - [x] updated the relevant documentation or specification - [ ] reviewed "Files changed" and left comments if necessary - [ ] confirmed all CI checks have passed ### Reviewers Checklist *All items are required. Please add a note if the item is not applicable and please add your handle next to the items reviewed if you only reviewed selected items.* I have... - [ ] confirmed the correct [type prefix](https://github.com/commitizen/conventional-commit-types/blob/v3.0.0/index.json) in the PR title - [ ] confirmed `!` in the type prefix if API or client breaking change - [ ] confirmed all author checklist items have been addressed - [ ] reviewed state machine logic - [ ] reviewed API design and naming - [ ] reviewed documentation is accurate - [ ] reviewed tests and test coverage - [ ] manually tested (if applicable)
2021-07-05 07:55:51 -07:00
v036gov "github.com/cosmos/cosmos-sdk/x/gov/migrations/v036"
)
const (
// ModuleName defines the name of the module
ModuleName = "params"
// RouterKey defines the routing key for a ParameterChangeProposal
RouterKey = "params"
)
const (
// ProposalTypeChange defines the type for a ParameterChangeProposal
ProposalTypeChange = "ParameterChange"
)
// Param module codespace constants
const (
DefaultCodespace = "params"
CodeUnknownSubspace = 1
CodeSettingParameter = 2
CodeEmptyData = 3
)
// Assert ParameterChangeProposal implements v036gov.Content at compile-time
var _ v036gov.Content = ParameterChangeProposal{}
// ParameterChangeProposal defines a proposal which contains multiple parameter
// changes.
type ParameterChangeProposal struct {
Title string `json:"title" yaml:"title"`
Description string `json:"description" yaml:"description"`
Changes []ParamChange `json:"changes" yaml:"changes"`
}
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 }
// GetDescription 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 := v036gov.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
Subkey: %X
Value: %X
`, pc.Subspace, pc.Key, pc.Subkey, pc.Value))
}
return b.String()
}
// ParamChange defines a parameter change.
type ParamChange struct {
Subspace string `json:"subspace" yaml:"subspace"`
Key string `json:"key" yaml:"key"`
Subkey string `json:"subkey,omitempty" yaml:"subkey,omitempty"`
Value string `json:"value" yaml:"value"`
}
func NewParamChange(subspace, key, value string) ParamChange {
return ParamChange{subspace, key, "", value}
}
func NewParamChangeWithSubkey(subspace, key, subkey, value string) ParamChange {
return ParamChange{subspace, key, subkey, value}
}
// String implements the Stringer interface.
func (pc ParamChange) String() string {
return fmt.Sprintf(`Param Change:
Subspace: %s
Key: %s
Subkey: %X
Value: %X
`, pc.Subspace, pc.Key, pc.Subkey, pc.Value)
}
// ValidateChange 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(DefaultCodespace)
}
for _, pc := range changes {
if len(pc.Subspace) == 0 {
return ErrEmptySubspace(DefaultCodespace)
}
if len(pc.Key) == 0 {
return ErrEmptyKey(DefaultCodespace)
}
if len(pc.Value) == 0 {
return ErrEmptyValue(DefaultCodespace)
}
}
return nil
}
// ErrUnknownSubspace returns an unknown subspace error.
func ErrUnknownSubspace(codespace string, space string) error {
return fmt.Errorf("unknown subspace %s", space)
}
// ErrSettingParameter returns an error for failing to set a parameter.
func ErrSettingParameter(codespace string, key, subkey, value, msg string) error {
return fmt.Errorf("error setting parameter %s on %s (%s): %s", value, key, subkey, msg)
}
// ErrEmptyChanges returns an error for empty parameter changes.
func ErrEmptyChanges(codespace string) error {
return fmt.Errorf("submitted parameter changes are empty")
}
// ErrEmptySubspace returns an error for an empty subspace.
func ErrEmptySubspace(codespace string) error {
return fmt.Errorf("parameter subspace is empty")
}
// ErrEmptyKey returns an error for when an empty key is given.
func ErrEmptyKey(codespace string) error {
return fmt.Errorf("parameter key is empty")
}
// ErrEmptyValue returns an error for when an empty key is given.
func ErrEmptyValue(codespace string) error {
return fmt.Errorf("parameter value is empty")
}
func RegisterLegacyAminoCodec(cdc *codec.LegacyAmino) {
cdc.RegisterConcrete(ParameterChangeProposal{}, "cosmos-sdk/ParameterChangeProposal", nil)
}