-
Notifications
You must be signed in to change notification settings - Fork 199
/
gasConfigHandler.go
272 lines (224 loc) · 10.2 KB
/
gasConfigHandler.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
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
package economics
import (
"fmt"
"math/big"
"sort"
"strconv"
"sync"
"github.com/multiversx/mx-chain-core-go/core"
"github.com/multiversx/mx-chain-core-go/core/check"
"github.com/multiversx/mx-chain-go/common"
"github.com/multiversx/mx-chain-go/config"
"github.com/multiversx/mx-chain-go/process"
"github.com/multiversx/mx-chain-go/statusHandler"
)
const epsilon = 0.00000001
type gasConfig struct {
gasLimitSettingEpoch uint32
maxGasLimitPerBlock uint64
maxGasLimitPerMiniBlock uint64
maxGasLimitPerMetaBlock uint64
maxGasLimitPerMetaMiniBlock uint64
maxGasLimitPerTx uint64
minGasLimit uint64
extraGasLimitGuardedTx uint64
}
type gasConfigHandler struct {
statusHandler core.AppStatusHandler
gasLimitSettings []*gasConfig
minGasPrice uint64
gasPerDataByte uint64
genesisTotalSupply *big.Int
maxGasPriceSetGuardian uint64
mut sync.RWMutex
}
// newGasConfigHandler returns a new instance of gasConfigHandler
func newGasConfigHandler(economics *config.EconomicsConfig) (*gasConfigHandler, error) {
gasConfigSlice, err := checkAndParseFeeSettings(economics.FeeSettings)
if err != nil {
return nil, err
}
sort.Slice(gasConfigSlice, func(i, j int) bool {
return gasConfigSlice[i].gasLimitSettingEpoch < gasConfigSlice[j].gasLimitSettingEpoch
})
minGasPrice, gasPerDataByte, genesisTotalSupply, maxGasPriceSetGuardian, err := convertGenericValues(economics)
if err != nil {
return nil, err
}
return &gasConfigHandler{
statusHandler: statusHandler.NewNilStatusHandler(),
gasLimitSettings: gasConfigSlice,
minGasPrice: minGasPrice,
gasPerDataByte: gasPerDataByte,
genesisTotalSupply: genesisTotalSupply,
maxGasPriceSetGuardian: maxGasPriceSetGuardian,
}, nil
}
// setStatusHandler sets the provided status handler if not nil
func (handler *gasConfigHandler) setStatusHandler(statusHandler core.AppStatusHandler) error {
if check.IfNil(statusHandler) {
return core.ErrNilAppStatusHandler
}
handler.mut.Lock()
handler.statusHandler = statusHandler
handler.mut.Unlock()
return nil
}
// getMinGasLimit returns min gas limit in a specific epoch
func (handler *gasConfigHandler) getMinGasLimit(epoch uint32) uint64 {
gc := handler.getGasConfigForEpoch(epoch)
return gc.minGasLimit
}
// getExtraGasLimitGuardedTx returns extra gas limit for guarded tx in a specific epoch
func (handler *gasConfigHandler) getExtraGasLimitGuardedTx(epoch uint32) uint64 {
gc := handler.getGasConfigForEpoch(epoch)
return gc.extraGasLimitGuardedTx
}
// getMaxGasLimitPerMetaBlock returns max gas limit per meta block in a specific epoch
func (handler *gasConfigHandler) getMaxGasLimitPerMetaBlock(epoch uint32) uint64 {
gc := handler.getGasConfigForEpoch(epoch)
return gc.maxGasLimitPerMetaBlock
}
// getMaxGasLimitPerBlock returns max gas limit per block in a specific epoch
func (handler *gasConfigHandler) getMaxGasLimitPerBlock(epoch uint32) uint64 {
gc := handler.getGasConfigForEpoch(epoch)
return gc.maxGasLimitPerBlock
}
// getMaxGasLimitPerMetaMiniBlock returns max gas limit per meta mini block in a specific epoch
func (handler *gasConfigHandler) getMaxGasLimitPerMetaMiniBlock(epoch uint32) uint64 {
gc := handler.getGasConfigForEpoch(epoch)
return gc.maxGasLimitPerMetaMiniBlock
}
// getMaxGasLimitPerMiniBlock returns max gas limit per mini block in a specific epoch
func (handler *gasConfigHandler) getMaxGasLimitPerMiniBlock(epoch uint32) uint64 {
gc := handler.getGasConfigForEpoch(epoch)
return gc.maxGasLimitPerMiniBlock
}
// getMaxGasLimitPerBlockForSafeCrossShard returns maximum gas limit per block for safe cross shard in a specific epoch
func (handler *gasConfigHandler) getMaxGasLimitPerBlockForSafeCrossShard(epoch uint32) uint64 {
gc := handler.getGasConfigForEpoch(epoch)
return core.MinUint64(gc.maxGasLimitPerBlock, gc.maxGasLimitPerMetaBlock)
}
// getMaxGasLimitPerMiniBlockForSafeCrossShard returns maximum gas limit per mini block for safe cross shard in a specific epoch
func (handler *gasConfigHandler) getMaxGasLimitPerMiniBlockForSafeCrossShard(epoch uint32) uint64 {
gc := handler.getGasConfigForEpoch(epoch)
return core.MinUint64(gc.maxGasLimitPerMiniBlock, gc.maxGasLimitPerMetaMiniBlock)
}
// getMaxGasLimitPerTx returns max gas limit per tx in a specific epoch
func (handler *gasConfigHandler) getMaxGasLimitPerTx(epoch uint32) uint64 {
gc := handler.getGasConfigForEpoch(epoch)
return gc.maxGasLimitPerTx
}
func (handler *gasConfigHandler) updateGasConfigMetrics(epoch uint32) {
gc := handler.getGasConfigForEpoch(epoch)
log.Debug("economics: gasConfigHandler",
"epoch", gc.gasLimitSettingEpoch,
"maxGasLimitPerBlock", gc.maxGasLimitPerBlock,
"maxGasLimitPerMiniBlock", gc.maxGasLimitPerMiniBlock,
"maxGasLimitPerMetaBlock", gc.maxGasLimitPerMetaBlock,
"maxGasLimitPerMetaMiniBlock", gc.maxGasLimitPerMetaMiniBlock,
"maxGasLimitPerTx", gc.maxGasLimitPerTx,
"minGasLimit", gc.minGasLimit,
)
handler.mut.RLock()
handler.statusHandler.SetUInt64Value(common.MetricMaxGasPerTransaction, gc.maxGasLimitPerTx)
handler.mut.RUnlock()
}
func (handler *gasConfigHandler) getGasConfigForEpoch(epoch uint32) *gasConfig {
gasConfigSetting := handler.gasLimitSettings[0]
for i := 1; i < len(handler.gasLimitSettings); i++ {
if epoch >= handler.gasLimitSettings[i].gasLimitSettingEpoch {
gasConfigSetting = handler.gasLimitSettings[i]
}
}
return gasConfigSetting
}
func checkAndParseFeeSettings(feeSettings config.FeeSettings) ([]*gasConfig, error) {
if feeSettings.GasPriceModifier > 1.0 || feeSettings.GasPriceModifier < epsilon {
return nil, process.ErrInvalidGasModifier
}
if len(feeSettings.GasLimitSettings) == 0 {
return nil, process.ErrEmptyGasLimitSettings
}
gasConfigSlice := make([]*gasConfig, 0, len(feeSettings.GasLimitSettings))
for _, gasLimitSetting := range feeSettings.GasLimitSettings {
gc, err := checkAndParseGasLimitSettings(gasLimitSetting)
if err != nil {
return nil, err
}
gasConfigSlice = append(gasConfigSlice, gc)
}
return gasConfigSlice, nil
}
func checkAndParseGasLimitSettings(gasLimitSetting config.GasLimitSetting) (*gasConfig, error) {
conversionBase := 10
bitConversionSize := 64
gc := &gasConfig{}
var err error
gc.gasLimitSettingEpoch = gasLimitSetting.EnableEpoch
gc.minGasLimit, err = strconv.ParseUint(gasLimitSetting.MinGasLimit, conversionBase, bitConversionSize)
if err != nil {
return nil, process.ErrInvalidMinimumGasLimitForTx
}
gc.maxGasLimitPerBlock, err = strconv.ParseUint(gasLimitSetting.MaxGasLimitPerBlock, conversionBase, bitConversionSize)
if err != nil {
return nil, fmt.Errorf("%w for epoch %d", process.ErrInvalidMaxGasLimitPerBlock, gasLimitSetting.EnableEpoch)
}
gc.maxGasLimitPerMiniBlock, err = strconv.ParseUint(gasLimitSetting.MaxGasLimitPerMiniBlock, conversionBase, bitConversionSize)
if err != nil {
return nil, fmt.Errorf("%w for epoch %d", process.ErrInvalidMaxGasLimitPerMiniBlock, gasLimitSetting.EnableEpoch)
}
gc.maxGasLimitPerMetaBlock, err = strconv.ParseUint(gasLimitSetting.MaxGasLimitPerMetaBlock, conversionBase, bitConversionSize)
if err != nil {
return nil, fmt.Errorf("%w for epoch %d", process.ErrInvalidMaxGasLimitPerMetaBlock, gasLimitSetting.EnableEpoch)
}
gc.maxGasLimitPerMetaMiniBlock, err = strconv.ParseUint(gasLimitSetting.MaxGasLimitPerMetaMiniBlock, conversionBase, bitConversionSize)
if err != nil {
return nil, fmt.Errorf("%w for epoch %d", process.ErrInvalidMaxGasLimitPerMetaMiniBlock, gasLimitSetting.EnableEpoch)
}
gc.maxGasLimitPerTx, err = strconv.ParseUint(gasLimitSetting.MaxGasLimitPerTx, conversionBase, bitConversionSize)
if err != nil {
return nil, fmt.Errorf("%w for epoch %d", process.ErrInvalidMaxGasLimitPerTx, gasLimitSetting.EnableEpoch)
}
gc.extraGasLimitGuardedTx, err = strconv.ParseUint(gasLimitSetting.ExtraGasLimitGuardedTx, conversionBase, bitConversionSize)
if err != nil {
return nil, fmt.Errorf("%w for epoch %d", process.ErrInvalidExtraGasLimitGuardedTx, gasLimitSetting.EnableEpoch)
}
if gc.maxGasLimitPerBlock < gc.minGasLimit {
return nil, fmt.Errorf("%w: maxGasLimitPerBlock = %d minGasLimit = %d in epoch %d", process.ErrInvalidMaxGasLimitPerBlock, gc.maxGasLimitPerBlock, gc.minGasLimit, gasLimitSetting.EnableEpoch)
}
if gc.maxGasLimitPerMiniBlock < gc.minGasLimit {
return nil, fmt.Errorf("%w: maxGasLimitPerMiniBlock = %d minGasLimit = %d in epoch %d", process.ErrInvalidMaxGasLimitPerMiniBlock, gc.maxGasLimitPerMiniBlock, gc.minGasLimit, gasLimitSetting.EnableEpoch)
}
if gc.maxGasLimitPerMetaBlock < gc.minGasLimit {
return nil, fmt.Errorf("%w: maxGasLimitPerMetaBlock = %d minGasLimit = %d in epoch %d", process.ErrInvalidMaxGasLimitPerMetaBlock, gc.maxGasLimitPerMetaBlock, gc.minGasLimit, gasLimitSetting.EnableEpoch)
}
if gc.maxGasLimitPerMetaMiniBlock < gc.minGasLimit {
return nil, fmt.Errorf("%w: maxGasLimitPerMetaMiniBlock = %d minGasLimit = %d in epoch %d", process.ErrInvalidMaxGasLimitPerMetaMiniBlock, gc.maxGasLimitPerMetaMiniBlock, gc.minGasLimit, gasLimitSetting.EnableEpoch)
}
if gc.maxGasLimitPerTx < gc.minGasLimit {
return nil, fmt.Errorf("%w: maxGasLimitPerTx = %d minGasLimit = %d in epoch %d", process.ErrInvalidMaxGasLimitPerTx, gc.maxGasLimitPerTx, gc.minGasLimit, gasLimitSetting.EnableEpoch)
}
return gc, nil
}
func convertGenericValues(economics *config.EconomicsConfig) (uint64, uint64, *big.Int, uint64, error) {
conversionBase := 10
bitConversionSize := 64
minGasPrice, err := strconv.ParseUint(economics.FeeSettings.MinGasPrice, conversionBase, bitConversionSize)
if err != nil {
return 0, 0, nil, 0, process.ErrInvalidMinimumGasPrice
}
gasPerDataByte, err := strconv.ParseUint(economics.FeeSettings.GasPerDataByte, conversionBase, bitConversionSize)
if err != nil {
return 0, 0, nil, 0, process.ErrInvalidGasPerDataByte
}
genesisTotalSupply, ok := big.NewInt(0).SetString(economics.GlobalSettings.GenesisTotalSupply, conversionBase)
if !ok {
return 0, 0, nil, 0, process.ErrInvalidGenesisTotalSupply
}
maxGasPriceSetGuardian, err := strconv.ParseUint(economics.FeeSettings.MaxGasPriceSetGuardian, conversionBase, bitConversionSize)
if err != nil {
return 0, 0, nil, 0, process.ErrInvalidMaxGasPriceSetGuardian
}
return minGasPrice, gasPerDataByte, genesisTotalSupply, maxGasPriceSetGuardian, nil
}