cosmos-sdk/x/auth/types/params.go

173 lines
4.7 KiB
Go
Raw Normal View History

package types
2018-12-20 11:09:43 -08:00
import (
"fmt"
refactor: Implementing sigs.k8s.io YAML to remove .proto yaml annotations (#9780) ## Description Draft of: #9705 Started off with changing codec `MarshalYaml` function to directly go from JSON to yaml using the new library. Replaced the only usage of UnmarshalYaml per request. --- ### 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... - [ ] included the correct [type prefix](https://github.com/commitizen/conventional-commit-types/blob/v3.0.0/index.json) in the PR title - [ ] added `!` to the type prefix if API or client breaking change - [ ] targeted the correct branch (see [PR Targeting](https://github.com/cosmos/cosmos-sdk/blob/master/CONTRIBUTING.md#pr-targeting)) - [ ] provided a link to the relevant issue or specification - [ ] followed the guidelines for [building modules](https://github.com/cosmos/cosmos-sdk/blob/master/docs/building-modules) - [ ] included the necessary unit and integration [tests](https://github.com/cosmos/cosmos-sdk/blob/master/CONTRIBUTING.md#testing) - [ ] added a changelog entry to `CHANGELOG.md` - [ ] included comments for [documenting Go code](https://blog.golang.org/godoc) - [ ] 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... - [x] confirmed the correct [type prefix](https://github.com/commitizen/conventional-commit-types/blob/v3.0.0/index.json) in the PR title - [x] confirmed `!` in the type prefix if API or client breaking change - [ ] confirmed all author checklist items have been addressed - [x] reviewed state machine logic - [x] reviewed API design and naming - [x] reviewed documentation is accurate - [x] reviewed tests and test coverage - [x] manually tested (if applicable)
2021-09-24 07:37:34 -07:00
"sigs.k8s.io/yaml"
paramtypes "github.com/cosmos/cosmos-sdk/x/params/types"
2018-12-20 11:09:43 -08:00
)
// Default parameter values
const (
DefaultMaxMemoCharacters uint64 = 256
DefaultTxSigLimit uint64 = 7
DefaultTxSizeCostPerByte uint64 = 10
DefaultSigVerifyCostED25519 uint64 = 590
DefaultSigVerifyCostSecp256k1 uint64 = 1000
2018-12-20 11:09:43 -08:00
)
// Parameter keys
var (
KeyMaxMemoCharacters = []byte("MaxMemoCharacters")
KeyTxSigLimit = []byte("TxSigLimit")
KeyTxSizeCostPerByte = []byte("TxSizeCostPerByte")
2018-12-20 11:09:43 -08:00
KeySigVerifyCostED25519 = []byte("SigVerifyCostED25519")
KeySigVerifyCostSecp256k1 = []byte("SigVerifyCostSecp256k1")
)
var _ paramtypes.ParamSet = &Params{}
2018-12-20 11:09:43 -08:00
Merge PR #4159: Module/Genesis Generalization * first commit * gaia cleanup * ... * staking multihooks * missing module function return args * bank module name constant * working, module interface for x/ * got this thing compiling * make test compiles and passes * remove expanded simulation invariants * genesis issue * continued * continued * register crisis routes thought mm * begin blocker to mm * end blocker to mm * empty routes not initialized * move gaia initChainer sanity check to baseapp * remove codecs from module manager * reorging genesis stuff * module manager passed by reference/bugfixes from working last commit int int * move invariant checks from gaia to crisis * typo * basic refactors cmd/gaia/init * working * MultiStakingHooks from types to x/staking/types int * default module manager order of operations from input modules * working * typo * add AppModuleBasic * moduleBasicManager / non-test code compiles * working attempting to get tests passing * make test passes * sim random genesis fix * export bug * ... * genutil module * genutil working * refactored - happy with non-testing code in cmd/ * ... * lint fixes * comment improvement * cli test fix * compile housing * working through compile errors * working gettin' compilin' * non-test code compiles * move testnet to its own module * reworking tests int * bez staging PR 1 comments * concise module function-of names * moved all tests from genesis_test.go to other genutil tests * genaccounts package, add genutil and genaccounts to app.go * docs for genutil genaccounts * genaccounts iterate fn * non-test code with genaccounts/ now compiles * working test compiling * debugging tests * resolved all make test compile errors * test debuggin * resolved all unit tests, introduced param module * cli-test compile fixes * staking initialization bug * code comment improvements, changelog entries * BasicGaiaApp -> ModuleBasics * highlevel explanation in types/module.go * @alexanderbez comment revisions * @fedekunze PR comments * @alexanderbez PR comments (x2) * @cwgoes comments (minor updates) * @fedekunze suggestions * panic on init with multiple validator updates from different modules * initchain panic makes validate genesis fail int * AppModuleGenesis seperation int * test * remove init panic logic in validate genesis replaced with TODO * set maxprocs to match system's GOMAXPROCS * Update circleci * Cap maxprocs in CI to 4 * @alexanderbez recent comments addressed * less blocks in twouble sims int * runsim error output flag * -e on import_export as well * error out int * Try to fix failures * runsim
2019-05-16 08:25:32 -07:00
// NewParams creates a new Params object
func NewParams(
maxMemoCharacters, txSigLimit, txSizeCostPerByte, sigVerifyCostED25519, sigVerifyCostSecp256k1 uint64,
) Params {
Merge PR #4159: Module/Genesis Generalization * first commit * gaia cleanup * ... * staking multihooks * missing module function return args * bank module name constant * working, module interface for x/ * got this thing compiling * make test compiles and passes * remove expanded simulation invariants * genesis issue * continued * continued * register crisis routes thought mm * begin blocker to mm * end blocker to mm * empty routes not initialized * move gaia initChainer sanity check to baseapp * remove codecs from module manager * reorging genesis stuff * module manager passed by reference/bugfixes from working last commit int int * move invariant checks from gaia to crisis * typo * basic refactors cmd/gaia/init * working * MultiStakingHooks from types to x/staking/types int * default module manager order of operations from input modules * working * typo * add AppModuleBasic * moduleBasicManager / non-test code compiles * working attempting to get tests passing * make test passes * sim random genesis fix * export bug * ... * genutil module * genutil working * refactored - happy with non-testing code in cmd/ * ... * lint fixes * comment improvement * cli test fix * compile housing * working through compile errors * working gettin' compilin' * non-test code compiles * move testnet to its own module * reworking tests int * bez staging PR 1 comments * concise module function-of names * moved all tests from genesis_test.go to other genutil tests * genaccounts package, add genutil and genaccounts to app.go * docs for genutil genaccounts * genaccounts iterate fn * non-test code with genaccounts/ now compiles * working test compiling * debugging tests * resolved all make test compile errors * test debuggin * resolved all unit tests, introduced param module * cli-test compile fixes * staking initialization bug * code comment improvements, changelog entries * BasicGaiaApp -> ModuleBasics * highlevel explanation in types/module.go * @alexanderbez comment revisions * @fedekunze PR comments * @alexanderbez PR comments (x2) * @cwgoes comments (minor updates) * @fedekunze suggestions * panic on init with multiple validator updates from different modules * initchain panic makes validate genesis fail int * AppModuleGenesis seperation int * test * remove init panic logic in validate genesis replaced with TODO * set maxprocs to match system's GOMAXPROCS * Update circleci * Cap maxprocs in CI to 4 * @alexanderbez recent comments addressed * less blocks in twouble sims int * runsim error output flag * -e on import_export as well * error out int * Try to fix failures * runsim
2019-05-16 08:25:32 -07:00
return Params{
MaxMemoCharacters: maxMemoCharacters,
TxSigLimit: txSigLimit,
TxSizeCostPerByte: txSizeCostPerByte,
SigVerifyCostED25519: sigVerifyCostED25519,
SigVerifyCostSecp256k1: sigVerifyCostSecp256k1,
}
}
// ParamKeyTable for auth module
func ParamKeyTable() paramtypes.KeyTable {
return paramtypes.NewKeyTable().RegisterParamSet(&Params{})
2018-12-20 11:09:43 -08:00
}
2019-02-04 18:13:04 -08:00
// ParamSetPairs implements the ParamSet interface and returns all the key/value pairs
2018-12-20 11:09:43 -08:00
// pairs of auth module's parameters.
func (p *Params) ParamSetPairs() paramtypes.ParamSetPairs {
return paramtypes.ParamSetPairs{
paramtypes.NewParamSetPair(KeyMaxMemoCharacters, &p.MaxMemoCharacters, validateMaxMemoCharacters),
paramtypes.NewParamSetPair(KeyTxSigLimit, &p.TxSigLimit, validateTxSigLimit),
paramtypes.NewParamSetPair(KeyTxSizeCostPerByte, &p.TxSizeCostPerByte, validateTxSizeCostPerByte),
paramtypes.NewParamSetPair(KeySigVerifyCostED25519, &p.SigVerifyCostED25519, validateSigVerifyCostED25519),
paramtypes.NewParamSetPair(KeySigVerifyCostSecp256k1, &p.SigVerifyCostSecp256k1, validateSigVerifyCostSecp256k1),
2018-12-20 11:09:43 -08:00
}
}
// DefaultParams returns a default set of parameters.
func DefaultParams() Params {
return Params{
MaxMemoCharacters: DefaultMaxMemoCharacters,
TxSigLimit: DefaultTxSigLimit,
TxSizeCostPerByte: DefaultTxSizeCostPerByte,
2018-12-20 11:09:43 -08:00
SigVerifyCostED25519: DefaultSigVerifyCostED25519,
SigVerifyCostSecp256k1: DefaultSigVerifyCostSecp256k1,
}
}
// SigVerifyCostSecp256r1 returns gas fee of secp256r1 signature verification.
// Set by benchmarking current implementation:
// BenchmarkSig/secp256k1 4334 277167 ns/op 4128 B/op 79 allocs/op
// BenchmarkSig/secp256r1 10000 108769 ns/op 1672 B/op 33 allocs/op
// Based on the results above secp256k1 is 2.7x is slwer. However we propose to discount it
// because we are we don't compare the cgo implementation of secp256k1, which is faster.
func (p Params) SigVerifyCostSecp256r1() uint64 {
return p.SigVerifyCostSecp256k1 / 2
}
2018-12-20 11:09:43 -08:00
// String implements the stringer interface.
func (p Params) String() string {
out, _ := yaml.Marshal(p)
return string(out)
2018-12-20 11:09:43 -08:00
}
2019-12-10 08:48:57 -08:00
func validateTxSigLimit(i interface{}) error {
v, ok := i.(uint64)
if !ok {
return fmt.Errorf("invalid parameter type: %T", i)
}
if v == 0 {
return fmt.Errorf("invalid tx signature limit: %d", v)
}
return nil
}
func validateSigVerifyCostED25519(i interface{}) error {
v, ok := i.(uint64)
if !ok {
return fmt.Errorf("invalid parameter type: %T", i)
}
if v == 0 {
return fmt.Errorf("invalid ED25519 signature verification cost: %d", v)
}
return nil
}
func validateSigVerifyCostSecp256k1(i interface{}) error {
v, ok := i.(uint64)
if !ok {
return fmt.Errorf("invalid parameter type: %T", i)
}
if v == 0 {
return fmt.Errorf("invalid SECK256k1 signature verification cost: %d", v)
}
return nil
}
func validateMaxMemoCharacters(i interface{}) error {
v, ok := i.(uint64)
if !ok {
return fmt.Errorf("invalid parameter type: %T", i)
}
if v == 0 {
return fmt.Errorf("invalid max memo characters: %d", v)
}
return nil
}
func validateTxSizeCostPerByte(i interface{}) error {
v, ok := i.(uint64)
if !ok {
return fmt.Errorf("invalid parameter type: %T", i)
}
if v == 0 {
return fmt.Errorf("invalid tx size cost per byte: %d", v)
}
return nil
}
// Validate checks that the parameters have valid values.
func (p Params) Validate() error {
2019-12-10 08:48:57 -08:00
if err := validateTxSigLimit(p.TxSigLimit); err != nil {
return err
}
2019-12-10 08:48:57 -08:00
if err := validateSigVerifyCostED25519(p.SigVerifyCostED25519); err != nil {
return err
}
2019-12-10 08:48:57 -08:00
if err := validateSigVerifyCostSecp256k1(p.SigVerifyCostSecp256k1); err != nil {
return err
}
if err := validateMaxMemoCharacters(p.MaxMemoCharacters); err != nil {
2019-12-10 08:48:57 -08:00
return err
}
2019-12-10 08:48:57 -08:00
if err := validateTxSizeCostPerByte(p.TxSizeCostPerByte); err != nil {
return err
}
2019-12-10 08:48:57 -08:00
return nil
}