forked from evmos/evmos
-
Notifications
You must be signed in to change notification settings - Fork 3
/
params.go
204 lines (169 loc) · 5.41 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
196
197
198
199
200
201
202
203
204
package types
import (
"fmt"
sdkmath "cosmossdk.io/math"
sdk "github.com/cosmos/cosmos-sdk/types"
paramtypes "github.com/cosmos/cosmos-sdk/x/params/types"
"github.com/ethereum/go-ethereum/params"
)
var (
// DefaultMinGasMultiplier is 0.5 or 50%
DefaultMinGasMultiplier = sdk.NewDecWithPrec(50, 2)
// DefaultMinGasPrice is 0 (i.e disabled)
DefaultMinGasPrice = sdk.ZeroDec()
// DefaultEnableHeight is 0 (i.e disabled)
DefaultEnableHeight = int64(0)
// DefaultNoBaseFee is false
DefaultNoBaseFee = false
)
// Parameter keys
var (
ParamsKey = []byte("Params")
ParamStoreKeyNoBaseFee = []byte("NoBaseFee")
ParamStoreKeyBaseFeeChangeDenominator = []byte("BaseFeeChangeDenominator")
ParamStoreKeyElasticityMultiplier = []byte("ElasticityMultiplier")
ParamStoreKeyBaseFee = []byte("BaseFee")
ParamStoreKeyEnableHeight = []byte("EnableHeight")
ParamStoreKeyMinGasPrice = []byte("MinGasPrice")
ParamStoreKeyMinGasMultiplier = []byte("MinGasMultiplier")
)
// ParamKeyTable returns the parameter key table.
func ParamKeyTable() paramtypes.KeyTable {
return paramtypes.NewKeyTable().RegisterParamSet(&Params{})
}
// ParamSetPairs returns the parameter set pairs.
func (p *Params) ParamSetPairs() paramtypes.ParamSetPairs {
return paramtypes.ParamSetPairs{
paramtypes.NewParamSetPair(ParamStoreKeyNoBaseFee, &p.NoBaseFee, validateBool),
paramtypes.NewParamSetPair(ParamStoreKeyBaseFeeChangeDenominator, &p.BaseFeeChangeDenominator, validateBaseFeeChangeDenominator),
paramtypes.NewParamSetPair(ParamStoreKeyElasticityMultiplier, &p.ElasticityMultiplier, validateElasticityMultiplier),
paramtypes.NewParamSetPair(ParamStoreKeyBaseFee, &p.BaseFee, validateBaseFee),
paramtypes.NewParamSetPair(ParamStoreKeyEnableHeight, &p.EnableHeight, validateEnableHeight),
paramtypes.NewParamSetPair(ParamStoreKeyMinGasPrice, &p.MinGasPrice, validateMinGasPrice),
paramtypes.NewParamSetPair(ParamStoreKeyMinGasMultiplier, &p.MinGasMultiplier, validateMinGasPrice),
}
}
// NewParams creates a new Params instance
func NewParams(
noBaseFee bool,
baseFeeChangeDenom,
elasticityMultiplier uint32,
baseFee uint64,
enableHeight int64,
minGasPrice sdk.Dec,
minGasPriceMultiplier sdk.Dec,
) Params {
return Params{
NoBaseFee: noBaseFee,
BaseFeeChangeDenominator: baseFeeChangeDenom,
ElasticityMultiplier: elasticityMultiplier,
BaseFee: sdkmath.NewIntFromUint64(baseFee),
EnableHeight: enableHeight,
MinGasPrice: minGasPrice,
MinGasMultiplier: minGasPriceMultiplier,
}
}
// DefaultParams returns default evm parameters
func DefaultParams() Params {
return Params{
NoBaseFee: DefaultNoBaseFee,
BaseFeeChangeDenominator: params.BaseFeeChangeDenominator,
ElasticityMultiplier: params.ElasticityMultiplier,
BaseFee: sdkmath.NewIntFromUint64(params.InitialBaseFee),
EnableHeight: DefaultEnableHeight,
MinGasPrice: DefaultMinGasPrice,
MinGasMultiplier: DefaultMinGasMultiplier,
}
}
// Validate performs basic validation on fee market parameters.
func (p Params) Validate() error {
if p.BaseFeeChangeDenominator == 0 {
return fmt.Errorf("base fee change denominator cannot be 0")
}
if p.BaseFee.IsNegative() {
return fmt.Errorf("initial base fee cannot be negative: %s", p.BaseFee)
}
if p.EnableHeight < 0 {
return fmt.Errorf("enable height cannot be negative: %d", p.EnableHeight)
}
if err := validateMinGasMultiplier(p.MinGasMultiplier); err != nil {
return err
}
return validateMinGasPrice(p.MinGasPrice)
}
func validateBool(i interface{}) error {
_, ok := i.(bool)
if !ok {
return fmt.Errorf("invalid parameter type: %T", i)
}
return nil
}
func (p *Params) IsBaseFeeEnabled(height int64) bool {
return !p.NoBaseFee && height >= p.EnableHeight
}
func validateMinGasPrice(i interface{}) error {
v, ok := i.(sdk.Dec)
if !ok {
return fmt.Errorf("invalid parameter type: %T", i)
}
if v.IsNil() {
return fmt.Errorf("invalid parameter: nil")
}
if v.IsNegative() {
return fmt.Errorf("value cannot be negative: %s", i)
}
return nil
}
func validateBaseFeeChangeDenominator(i interface{}) error {
value, ok := i.(uint32)
if !ok {
return fmt.Errorf("invalid parameter type: %T", i)
}
if value == 0 {
return fmt.Errorf("base fee change denominator cannot be 0")
}
return nil
}
func validateElasticityMultiplier(i interface{}) error {
_, ok := i.(uint32)
if !ok {
return fmt.Errorf("invalid parameter type: %T", i)
}
return nil
}
func validateBaseFee(i interface{}) error {
value, ok := i.(sdkmath.Int)
if !ok {
return fmt.Errorf("invalid parameter type: %T", i)
}
if value.IsNegative() {
return fmt.Errorf("base fee cannot be negative")
}
return nil
}
func validateEnableHeight(i interface{}) error {
value, ok := i.(int64)
if !ok {
return fmt.Errorf("invalid parameter type: %T", i)
}
if value < 0 {
return fmt.Errorf("enable height cannot be negative: %d", value)
}
return nil
}
func validateMinGasMultiplier(i interface{}) error {
v, ok := i.(sdk.Dec)
if !ok {
return fmt.Errorf("invalid parameter type: %T", i)
}
if v.IsNil() {
return fmt.Errorf("invalid parameter: nil")
}
if v.IsNegative() {
return fmt.Errorf("value cannot be negative: %s", v)
}
if v.GT(sdk.OneDec()) {
return fmt.Errorf("value cannot be greater than 1: %s", v)
}
return nil
}