-
Notifications
You must be signed in to change notification settings - Fork 191
/
params.go
132 lines (110 loc) · 3.5 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
package types
import (
"fmt"
yaml "gopkg.in/yaml.v2"
sdk "github.com/cosmos/cosmos-sdk/types"
paramtypes "github.com/cosmos/cosmos-sdk/x/params/types"
)
// Parameter keys
var (
ParamStoreKeyRewardEpochDurationBlocks = []byte("reward_epoch_duration_blocks")
ParamStoreKeyRewardSmoothingBlocks = []byte("reward_smoothing_blocks")
ParamStoreKeyPerEpochRewardFraction = []byte("per_epoch_reward_fraction")
)
// ParamKeyTable returns the parameter key table.
func ParamKeyTable() paramtypes.KeyTable {
return paramtypes.NewKeyTable().RegisterParamSet(&Params{})
}
// DefaultParams returns default distribution parameters
func DefaultParams() Params {
return Params{
RewardEpochDurationBlocks: 0,
RewardSmoothingBlocks: 1,
PerEpochRewardFraction: sdk.OneDec(),
}
}
func (p Params) String() string {
out, _ := yaml.Marshal(p)
return string(out)
}
func (p Params) GetSmoothingBlocks() int64 {
smoothingBlocks := p.RewardSmoothingBlocks
if smoothingBlocks < 0 {
epochBlocks := p.RewardEpochDurationBlocks
if epochBlocks == 0 {
epochBlocks = 1
}
smoothingBlocks = epochBlocks
}
return smoothingBlocks
}
// RewardRate calculates the rate for dispensing the pool of coins over
// the specified number of blocks. Fractions are rounded up. In other
// words, it returns the smallest Coins such that pool is exhausted
// after #blocks withdrawals.
func (p Params) RewardRate(pool sdk.Coins, blocks int64) sdk.Coins {
coins := make([]sdk.Coin, 0, len(pool))
if blocks > 0 {
for _, coin := range pool {
if coin.IsZero() {
continue
}
// divide by blocks, rounding fractions up
// (coin.Amount - 1)/blocks + 1
rate := coin.Amount.SubRaw(1).QuoRaw(blocks).AddRaw(1)
coins = append(coins, sdk.NewCoin(coin.GetDenom(), rate))
}
}
return sdk.NewCoins(coins...)
}
// ParamSetPairs returns the parameter set pairs.
func (p *Params) ParamSetPairs() paramtypes.ParamSetPairs {
return paramtypes.ParamSetPairs{
paramtypes.NewParamSetPair(ParamStoreKeyRewardEpochDurationBlocks, &p.RewardEpochDurationBlocks, validateRewardEpochDurationBlocks),
paramtypes.NewParamSetPair(ParamStoreKeyRewardSmoothingBlocks, &p.RewardSmoothingBlocks, validateRewardSmoothingBlocks),
paramtypes.NewParamSetPair(ParamStoreKeyPerEpochRewardFraction, &p.PerEpochRewardFraction, validatePerEpochRewardFraction),
}
}
// ValidateBasic performs basic validation on distribution parameters.
func (p Params) ValidateBasic() error {
if err := validateRewardEpochDurationBlocks(p.RewardEpochDurationBlocks); err != nil {
return err
}
if err := validatePerEpochRewardFraction(p.PerEpochRewardFraction); err != nil {
return err
}
return nil
}
func validateRewardSmoothingBlocks(i interface{}) error {
v, ok := i.(int64)
if !ok {
return fmt.Errorf("invalid parameter type: %T", i)
}
if v < 0 {
return fmt.Errorf("reward smoothing blocks must be nonnegative: %d", v)
}
return nil
}
func validateRewardEpochDurationBlocks(i interface{}) error {
v, ok := i.(int64)
if !ok {
return fmt.Errorf("invalid parameter type: %T", i)
}
if v < 0 {
return fmt.Errorf("reward epoch duration blocks must be nonnegative: %d", v)
}
return nil
}
func validatePerEpochRewardFraction(i interface{}) error {
v, ok := i.(sdk.Dec)
if !ok {
return fmt.Errorf("invalid parameter type: %T", i)
}
if v.IsNegative() {
return fmt.Errorf("per epoch reward fraction must be nonnegative: %s", v)
}
if v.GT(sdk.OneDec()) {
return fmt.Errorf("per epoch reward fraction must be less than or equal to one: %s", v)
}
return nil
}