-
Notifications
You must be signed in to change notification settings - Fork 4
/
params.go
157 lines (134 loc) · 5.57 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
package types
import (
"bytes"
"fmt"
"github.com/zenchainprotocol/zenchain-node/x/common"
"time"
sdk "github.com/cosmos/cosmos-sdk/types"
"github.com/zenchainprotocol/zenchain-node/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
)
// 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
)
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(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,
}
}
// 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: KeyBondDenom, Value: &p.BondDenom, ValidatorFn: common.ValidateDenom("bond denom")},
{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.ValidateDecPositive("min self delegation")},
}
}
// 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
}