/
params.go
143 lines (124 loc) · 4.87 KB
/
params.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
package types
import (
"bytes"
"fmt"
"time"
"github.com/cosmos/cosmos-sdk/config"
sdk "github.com/cosmos/cosmos-sdk/types"
"github.com/cosmos/cosmos-sdk/x/params"
)
// Staking params default values
const (
// DefaultUnbondingTime reflects three weeks in seconds as the default
// unbonding time.
// TODO: Justify our choice of default here.
DefaultUnbondingTime = config.DefaultUnbondingTime
// Default maximum number of bonded validators
DefaultMaxValidators = config.DefaultMaxValidators
DefaultEpoch uint16 = config.DefaultBlocksPerEpoch
DefaultMaxValsToAddShares uint16 = config.DefaultMaxValsToVote
)
var (
// DefaultMinDelegation is the limit value of delegation or undelegation
DefaultMinDelegation = config.DefaultMinDelegation
// DefaultMinSelfDelegation is the default value of each validator's msd (hard code)
DefaultMinSelfDelegation = sdk.NewDec(10000)
)
// nolint - Keys for parameter access
var (
KeyUnbondingTime = []byte("UnbondingTime")
KeyMaxValidators = []byte("MaxValidators")
KeyBondDenom = []byte("BondDenom")
KeyEpoch = []byte("BlocksPerEpoch") // how many blocks each epoch has
KeyTheEndOfLastEpoch = []byte("TheEndOfLastEpoch") // a block height that is the end of last epoch
KeyMaxValsToAddShares = []byte("MaxValsToAddShares")
KeyMinDelegation = []byte("MinDelegation")
KeyMinSelfDelegation = []byte("MinSelfDelegation")
)
var _ params.ParamSet = (*Params)(nil)
// Params defines the high level settings for staking
type Params struct {
// time duration of unbonding
UnbondingTime time.Duration `json:"unbonding_time" yaml:"unbonding_time"`
// note: we need to be a bit careful about potential overflow here, since this is user-determined
// maximum number of validators (max uint16 = 65535)
MaxValidators uint16 `json:"max_bonded_validators" yaml:"max_bonded_validators"`
// epoch for validator update
Epoch uint16 `json:"epoch" yaml:"epoch"`
MaxValsToAddShares uint16 `json:"max_validators_to_add_shares" yaml:"max_validators_to_add_shares"`
// bondable coin denomination
BondDenom string `json:"bond_denom" yaml:"bond_denom"`
// limited amount of delegate
MinDelegation sdk.Dec `json:"min_delegation" yaml:"min_delegation"`
// validator's self declared minimum self delegation
MinSelfDelegation sdk.Dec `json:"min_self_delegation" yaml:"min_self_delegation"`
}
// NewParams creates a new Params instance
func NewParams(unbondingTime time.Duration, maxValidators uint16, bondDenom string, epoch uint16, maxValsToAddShares uint16,
minDelegation sdk.Dec, minSelfDelegation sdk.Dec) Params {
return Params{
UnbondingTime: unbondingTime,
MaxValidators: maxValidators,
BondDenom: bondDenom,
Epoch: epoch,
MaxValsToAddShares: maxValsToAddShares,
MinDelegation: minDelegation,
MinSelfDelegation: minSelfDelegation,
}
}
// ParamSetPairs is the implements params.ParamSet
func (p *Params) ParamSetPairs() params.ParamSetPairs {
return params.ParamSetPairs{
{Key: KeyUnbondingTime, Value: &p.UnbondingTime},
{Key: KeyMaxValidators, Value: &p.MaxValidators},
{Key: KeyBondDenom, Value: &p.BondDenom},
{Key: KeyEpoch, Value: &p.Epoch},
{Key: KeyMaxValsToAddShares, Value: &p.MaxValsToAddShares},
{Key: KeyMinDelegation, Value: &p.MinDelegation},
{Key: KeyMinSelfDelegation, Value: &p.MinSelfDelegation},
}
}
// Equal returns a boolean determining if two Param types are identical
// TODO: This is slower than comparing struct fields directly
func (p Params) Equal(p2 Params) bool {
bz1 := ModuleCdc.MustMarshalBinaryLengthPrefixed(&p)
bz2 := ModuleCdc.MustMarshalBinaryLengthPrefixed(&p2)
return bytes.Equal(bz1, bz2)
}
// DefaultParams returns a default set of parameters
func DefaultParams() Params {
return NewParams(
DefaultUnbondingTime, DefaultMaxValidators,
sdk.DefaultBondDenom, DefaultEpoch,
DefaultMaxValsToAddShares, DefaultMinDelegation,
DefaultMinSelfDelegation,
)
}
// String returns a human readable string representation of the Params
func (p Params) String() string {
return fmt.Sprintf(`Params:
Unbonding Time: %s
Max Validators: %d
Epoch: %d
Bonded Coin Denom: %s
MaxValsToAddShares: %d
MinDelegation %d
MinSelfDelegation %d`,
p.UnbondingTime, p.MaxValidators, p.Epoch, p.BondDenom, p.MaxValsToAddShares, p.MinDelegation, p.MinSelfDelegation)
}
// Validate gives a quick validity check for 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")
}
if p.Epoch == 0 {
return fmt.Errorf("staking parameter Epoch must be a positive integer")
}
if p.MaxValsToAddShares == 0 {
return fmt.Errorf("staking parameter MaxValsToAddShares must be a positive integer")
}
return nil
}