2019-06-05 16:26:17 -07:00
|
|
|
package types
|
2018-12-20 11:09:43 -08:00
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
|
2020-02-18 04:50:13 -08:00
|
|
|
yaml "gopkg.in/yaml.v2"
|
2020-02-19 03:52:20 -08:00
|
|
|
|
|
|
|
paramtypes "github.com/cosmos/cosmos-sdk/x/params/types"
|
2018-12-20 11:09:43 -08:00
|
|
|
)
|
|
|
|
|
|
|
|
// Default parameter values
|
|
|
|
const (
|
2019-02-04 15:35:12 -08:00
|
|
|
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")
|
2019-02-04 15:35:12 -08:00
|
|
|
KeyTxSizeCostPerByte = []byte("TxSizeCostPerByte")
|
2018-12-20 11:09:43 -08:00
|
|
|
KeySigVerifyCostED25519 = []byte("SigVerifyCostED25519")
|
|
|
|
KeySigVerifyCostSecp256k1 = []byte("SigVerifyCostSecp256k1")
|
|
|
|
)
|
|
|
|
|
2020-02-19 03:52:20 -08:00
|
|
|
var _ paramtypes.ParamSet = &Params{}
|
2018-12-20 11:09:43 -08:00
|
|
|
|
2019-05-16 08:25:32 -07:00
|
|
|
// NewParams creates a new Params object
|
2020-02-18 04:50:13 -08:00
|
|
|
func NewParams(
|
|
|
|
maxMemoCharacters, txSigLimit, txSizeCostPerByte, sigVerifyCostED25519, sigVerifyCostSecp256k1 uint64,
|
|
|
|
) Params {
|
2019-05-16 08:25:32 -07:00
|
|
|
return Params{
|
|
|
|
MaxMemoCharacters: maxMemoCharacters,
|
|
|
|
TxSigLimit: txSigLimit,
|
|
|
|
TxSizeCostPerByte: txSizeCostPerByte,
|
|
|
|
SigVerifyCostED25519: sigVerifyCostED25519,
|
|
|
|
SigVerifyCostSecp256k1: sigVerifyCostSecp256k1,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-02-08 13:35:19 -08:00
|
|
|
// ParamKeyTable for auth module
|
2020-02-19 03:52:20 -08:00
|
|
|
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.
|
2020-02-19 03:52:20 -08:00
|
|
|
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,
|
2019-02-04 15:35:12 -08:00
|
|
|
TxSizeCostPerByte: DefaultTxSizeCostPerByte,
|
2018-12-20 11:09:43 -08:00
|
|
|
SigVerifyCostED25519: DefaultSigVerifyCostED25519,
|
|
|
|
SigVerifyCostSecp256k1: DefaultSigVerifyCostSecp256k1,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-03-04 13:31:42 -08:00
|
|
|
// 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 {
|
2020-02-18 04:50:13 -08:00
|
|
|
out, _ := yaml.Marshal(p)
|
|
|
|
return string(out)
|
2018-12-20 11:09:43 -08:00
|
|
|
}
|
2019-09-12 12:22:25 -07: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
|
|
|
|
}
|
|
|
|
|
2019-09-12 12:22:25 -07:00
|
|
|
// 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-09-12 12:22:25 -07:00
|
|
|
}
|
2019-12-10 08:48:57 -08:00
|
|
|
if err := validateSigVerifyCostED25519(p.SigVerifyCostED25519); err != nil {
|
|
|
|
return err
|
2019-09-12 12:22:25 -07:00
|
|
|
}
|
2019-12-10 08:48:57 -08:00
|
|
|
if err := validateSigVerifyCostSecp256k1(p.SigVerifyCostSecp256k1); err != nil {
|
|
|
|
return err
|
2019-09-12 12:22:25 -07:00
|
|
|
}
|
2020-03-31 07:18:02 -07:00
|
|
|
if err := validateMaxMemoCharacters(p.MaxMemoCharacters); err != nil {
|
2019-12-10 08:48:57 -08:00
|
|
|
return err
|
2019-09-12 12:22:25 -07:00
|
|
|
}
|
2019-12-10 08:48:57 -08:00
|
|
|
if err := validateTxSizeCostPerByte(p.TxSizeCostPerByte); err != nil {
|
|
|
|
return err
|
2019-09-12 12:22:25 -07:00
|
|
|
}
|
2019-12-10 08:48:57 -08:00
|
|
|
|
2019-09-12 12:22:25 -07:00
|
|
|
return nil
|
|
|
|
}
|