2018-06-26 19:00:12 -07:00
|
|
|
package types
|
2018-05-12 15:22:59 -07:00
|
|
|
|
|
|
|
import (
|
2018-05-18 15:57:47 -07:00
|
|
|
"bytes"
|
2018-08-21 08:32:54 -07:00
|
|
|
"fmt"
|
2018-08-12 00:33:48 -07:00
|
|
|
"time"
|
2018-05-18 15:57:47 -07:00
|
|
|
|
2018-09-13 11:17:32 -07:00
|
|
|
"github.com/cosmos/cosmos-sdk/codec"
|
2019-02-13 15:01:50 -08:00
|
|
|
sdk "github.com/cosmos/cosmos-sdk/types"
|
2018-08-31 02:03:43 -07:00
|
|
|
"github.com/cosmos/cosmos-sdk/x/params"
|
2018-05-12 15:22:59 -07:00
|
|
|
)
|
|
|
|
|
2018-10-05 10:56:17 -07:00
|
|
|
const (
|
2019-02-07 17:41:23 -08:00
|
|
|
// DefaultUnbondingTime reflects three weeks in seconds as the default
|
2018-10-05 10:56:17 -07:00
|
|
|
// unbonding time.
|
2019-02-07 17:41:23 -08:00
|
|
|
// TODO: Justify our choice of default here.
|
|
|
|
DefaultUnbondingTime time.Duration = time.Second * 60 * 60 * 24 * 3
|
|
|
|
|
|
|
|
// Default maximum number of bonded validators
|
|
|
|
DefaultMaxValidators uint16 = 100
|
|
|
|
|
|
|
|
// Default maximum entries in a UBD/RED pair
|
|
|
|
DefaultMaxEntries uint16 = 7
|
2018-10-05 10:56:17 -07:00
|
|
|
)
|
2018-07-03 21:21:36 -07:00
|
|
|
|
2018-09-17 08:28:13 -07:00
|
|
|
// nolint - Keys for parameter access
|
2018-09-27 11:52:29 -07:00
|
|
|
var (
|
2018-10-19 11:36:00 -07:00
|
|
|
KeyUnbondingTime = []byte("UnbondingTime")
|
|
|
|
KeyMaxValidators = []byte("MaxValidators")
|
2019-01-24 14:44:31 -08:00
|
|
|
KeyMaxEntries = []byte("KeyMaxEntries")
|
2018-10-19 11:36:00 -07:00
|
|
|
KeyBondDenom = []byte("BondDenom")
|
2018-09-17 08:28:13 -07:00
|
|
|
)
|
2018-08-31 02:03:43 -07:00
|
|
|
|
2018-10-10 13:01:30 -07:00
|
|
|
var _ params.ParamSet = (*Params)(nil)
|
2018-10-06 06:50:58 -07:00
|
|
|
|
2018-05-12 15:22:59 -07:00
|
|
|
// Params defines the high level settings for staking
|
|
|
|
type Params struct {
|
2019-01-21 16:52:03 -08:00
|
|
|
UnbondingTime time.Duration `json:"unbonding_time"` // time duration of unbonding
|
2019-02-07 17:41:23 -08:00
|
|
|
MaxValidators uint16 `json:"max_validators"` // maximum number of validators (max uint16 = 65535)
|
2019-01-24 14:44:31 -08:00
|
|
|
MaxEntries uint16 `json:"max_entries"` // max entries for either unbonding delegation or redelegation (per pair/trio)
|
2019-02-07 17:41:23 -08:00
|
|
|
// note: we need to be a bit careful about potential overflow here, since this is user-determined
|
|
|
|
BondDenom string `json:"bond_denom"` // bondable coin denomination
|
2018-05-12 15:22:59 -07:00
|
|
|
}
|
|
|
|
|
2019-01-24 14:44:31 -08:00
|
|
|
func NewParams(unbondingTime time.Duration, maxValidators, maxEntries uint16,
|
|
|
|
bondDenom string) Params {
|
|
|
|
|
|
|
|
return Params{
|
|
|
|
UnbondingTime: unbondingTime,
|
|
|
|
MaxValidators: maxValidators,
|
|
|
|
MaxEntries: maxEntries,
|
|
|
|
BondDenom: bondDenom,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-10-10 13:01:30 -07:00
|
|
|
// Implements params.ParamSet
|
2019-02-04 18:13:04 -08:00
|
|
|
func (p *Params) ParamSetPairs() params.ParamSetPairs {
|
|
|
|
return params.ParamSetPairs{
|
2018-09-17 08:28:13 -07:00
|
|
|
{KeyUnbondingTime, &p.UnbondingTime},
|
|
|
|
{KeyMaxValidators, &p.MaxValidators},
|
2019-01-24 14:44:31 -08:00
|
|
|
{KeyMaxEntries, &p.MaxEntries},
|
2018-09-17 08:28:13 -07:00
|
|
|
{KeyBondDenom, &p.BondDenom},
|
2018-08-31 02:03:43 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-07-03 21:21:36 -07:00
|
|
|
// Equal returns a boolean determining if two Param types are identical.
|
2019-02-07 17:41:23 -08:00
|
|
|
// TODO: This is slower than comparing struct fields directly
|
2018-06-26 19:00:12 -07:00
|
|
|
func (p Params) Equal(p2 Params) bool {
|
2018-11-04 18:28:38 -08:00
|
|
|
bz1 := MsgCdc.MustMarshalBinaryLengthPrefixed(&p)
|
|
|
|
bz2 := MsgCdc.MustMarshalBinaryLengthPrefixed(&p2)
|
2018-05-18 15:57:47 -07:00
|
|
|
return bytes.Equal(bz1, bz2)
|
2018-05-12 15:22:59 -07:00
|
|
|
}
|
|
|
|
|
2018-07-03 21:21:36 -07:00
|
|
|
// DefaultParams returns a default set of parameters.
|
2018-05-25 07:17:49 -07:00
|
|
|
func DefaultParams() Params {
|
2019-02-13 15:01:50 -08:00
|
|
|
return NewParams(DefaultUnbondingTime, DefaultMaxValidators, DefaultMaxEntries, sdk.DefaultBondDenom)
|
2018-05-12 15:22:59 -07:00
|
|
|
}
|
2018-08-21 08:32:54 -07:00
|
|
|
|
2019-01-22 09:28:39 -08:00
|
|
|
// String returns a human readable string representation of the parameters.
|
|
|
|
func (p Params) String() string {
|
|
|
|
return fmt.Sprintf(`Params:
|
2019-01-25 03:12:04 -08:00
|
|
|
Unbonding Time: %s
|
|
|
|
Max Validators: %d
|
|
|
|
Max Entries: %d
|
|
|
|
Bonded Coin Denom: %s`, p.UnbondingTime,
|
2019-01-24 14:44:31 -08:00
|
|
|
p.MaxValidators, p.MaxEntries, p.BondDenom)
|
2018-08-21 08:32:54 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
// unmarshal the current staking params value from store key or panic
|
2018-09-13 11:17:32 -07:00
|
|
|
func MustUnmarshalParams(cdc *codec.Codec, value []byte) Params {
|
2018-08-21 08:32:54 -07:00
|
|
|
params, err := UnmarshalParams(cdc, value)
|
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
return params
|
|
|
|
}
|
|
|
|
|
|
|
|
// unmarshal the current staking params value from store key
|
2018-09-13 11:17:32 -07:00
|
|
|
func UnmarshalParams(cdc *codec.Codec, value []byte) (params Params, err error) {
|
2018-11-04 18:28:38 -08:00
|
|
|
err = cdc.UnmarshalBinaryLengthPrefixed(value, ¶ms)
|
2018-08-21 08:32:54 -07:00
|
|
|
if err != nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
return
|
|
|
|
}
|
2019-02-07 17:41:23 -08:00
|
|
|
|
|
|
|
// validate a set of params
|
|
|
|
func (p Params) Validate() error {
|
|
|
|
if p.BondDenom == "" {
|
|
|
|
return fmt.Errorf("staking parameter BondDenom can't be an empty string")
|
|
|
|
}
|
|
|
|
if p.MaxValidators == 0 {
|
|
|
|
return fmt.Errorf("staking parameter MaxValidators must be a positive integer")
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|