/
params.go
195 lines (168 loc) · 6.78 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
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
package types
import (
"bytes"
"fmt"
"time"
sdk "github.com/furyaxyz/fuxchain/libs/cosmos-sdk/types"
"github.com/furyaxyz/fuxchain/x/common"
"github.com/furyaxyz/fuxchain/x/params"
)
const (
// Update the validator set every 252 blocks by default
DefaultBlocksPerEpoch = 252
// Default maximum number of validators to vote
DefaultMaxValsToVote = 30
// Default validate rate update interval by hours
DefaultValidateRateUpdateInterval = 24
DefaultHistoricalEntries uint32 = 10000
)
// Staking params default values
const (
// Default unbonding duration, 14 days
DefaultUnbondingTime time.Duration = time.Hour * 24 * 7 * 2
// Default maximum number of bonded validators
DefaultMaxValidators uint16 = 21
DefaultEpoch uint16 = DefaultBlocksPerEpoch
DefaultMaxValsToAddShares uint16 = DefaultMaxValsToVote
DefaultConsensusType = common.PoA
)
var (
// DefaultMinDelegation is the limit value of delegation or undelegation
DefaultMinDelegation = sdk.NewDecWithPrec(1, 4)
// DefaultMinSelfDelegation is the default value of each validator's msd (hard code)
DefaultMinSelfDelegation = sdk.NewDec(0)
DefaultDPoSMinSelfDelegation = sdk.NewDec(10000)
)
// nolint - Keys for parameter access
var (
KeyUnbondingTime = []byte("UnbondingTime")
KeyMaxValidators = []byte("MaxValidators")
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")
KeyHistoricalEntries = []byte("HistoricalEntries")
KeyConsensusType = []byte("ConsensusType")
KeyEnableDposOp = []byte("EnableDposOp")
)
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"`
// 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"`
HistoricalEntries uint32 `protobuf:"varint,4,opt,name=historical_entries,json=historicalEntries,proto3" json:"historical_entries,omitempty" yaml:"historical_entries"`
ConsensusType common.ConsensusType `json:"consensus_type"`
EnableDposOp bool `json:"enable_dpos_op"`
}
// NewParams creates a new Params instance
func NewParams(unbondingTime time.Duration, maxValidators uint16, epoch uint16, maxValsToAddShares uint16, minDelegation sdk.Dec,
minSelfDelegation sdk.Dec, historicalEntries uint32, consensusType common.ConsensusType, enableDposOp bool) Params {
return Params{
UnbondingTime: unbondingTime,
MaxValidators: maxValidators,
Epoch: epoch,
MaxValsToAddShares: maxValsToAddShares,
MinDelegation: minDelegation,
MinSelfDelegation: minSelfDelegation,
HistoricalEntries: historicalEntries,
ConsensusType: consensusType,
EnableDposOp: enableDposOp,
}
}
// TODO: to supplement the validate function for every pair of param
func validateParams(value interface{}) error {
return nil
}
// ParamSetPairs is the implements params.ParamSet
func (p *Params) ParamSetPairs() params.ParamSetPairs {
return params.ParamSetPairs{
{Key: KeyUnbondingTime, Value: &p.UnbondingTime, ValidatorFn: common.ValidateDurationPositive("unbonding time")},
{Key: KeyMaxValidators, Value: &p.MaxValidators, ValidatorFn: common.ValidateUint16Positive("max validators")},
{Key: KeyEpoch, Value: &p.Epoch, ValidatorFn: common.ValidateUint16Positive("epoch")},
{Key: KeyMaxValsToAddShares, Value: &p.MaxValsToAddShares, ValidatorFn: common.ValidateUint16Positive("max vals to add shares")},
{Key: KeyMinDelegation, Value: &p.MinDelegation, ValidatorFn: common.ValidateDecPositive("min delegation")},
{Key: KeyMinSelfDelegation, Value: &p.MinSelfDelegation, ValidatorFn: common.ValidateDecNotNeg("min self delegation")},
{Key: KeyHistoricalEntries, Value: &p.HistoricalEntries, ValidatorFn: validateHistoricalEntries},
{Key: KeyConsensusType, Value: &p.ConsensusType, ValidatorFn: common.ValidateConsensusType("consensus type")},
{Key: KeyEnableDposOp, Value: &p.EnableDposOp, ValidatorFn: common.ValidateBool("enable operation")},
}
}
func validateHistoricalEntries(i interface{}) error {
_, ok := i.(uint32)
if !ok {
return fmt.Errorf("invalid parameter type: %T", i)
}
return nil
}
// 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,
DefaultEpoch,
DefaultMaxValsToAddShares,
DefaultMinDelegation,
DefaultMinSelfDelegation,
DefaultHistoricalEntries,
DefaultConsensusType,
false,
)
}
func DefaultDposParams() Params {
return NewParams(
DefaultUnbondingTime,
DefaultMaxValidators,
DefaultEpoch,
DefaultMaxValsToAddShares,
DefaultMinDelegation,
DefaultDPoSMinSelfDelegation,
DefaultHistoricalEntries,
common.DPoS,
true,
)
}
// 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
MaxValsToAddShares: %d
MinDelegation %d
MinSelfDelegation %d
ConsensusType: %s
EnableDposOp %t,`,
p.UnbondingTime, p.MaxValidators, p.Epoch, p.MaxValsToAddShares, p.MinDelegation,
p.MinSelfDelegation, p.ConsensusType, p.EnableDposOp)
}
// Validate gives a quick validity check for a set of params
func (p Params) Validate() error {
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
}